]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Replaced lengthy iterator types with the auto keyword
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c 3 *
1bc6525b 4 * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk>
2953961c
JH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
269528f5 21#ifdef ENABLE_DECODE
4e5a4405 22#include <libsigrokdecode/libsigrokdecode.h>
269528f5 23#endif
4e5a4405 24
2953961c
JH
25#include "sigsession.h"
26
dc0867ff 27#include "devicemanager.h"
921b90c0 28#include "device/device.h"
ae2d1bc5 29#include "device/file.h"
119aff65 30
1b1ec774
JH
31#include "data/analog.h"
32#include "data/analogsnapshot.h"
6e89374a 33#include "data/decoderstack.h"
1b1ec774
JH
34#include "data/logic.h"
35#include "data/logicsnapshot.h"
7491a29f 36#include "data/decode/decoder.h"
82c7f640 37
aba1dd16 38#include "view/analogsignal.h"
b9329558 39#include "view/decodetrace.h"
8d634081 40#include "view/logicsignal.h"
28a4c9c5 41
2953961c
JH
42#include <assert.h>
43
e92cd4e4
JH
44#include <stdexcept>
45
4e5a4405
JH
46#include <boost/foreach.hpp>
47
728e5ef7
JH
48#include <sys/stat.h>
49
79efbc53
JH
50#include <QDebug>
51
819f4c25
JH
52using boost::dynamic_pointer_cast;
53using boost::function;
54using boost::lock_guard;
55using boost::mutex;
56using boost::shared_ptr;
d873f4d6 57using std::list;
819f4c25 58using std::map;
02412f0b 59using std::set;
819f4c25
JH
60using std::string;
61using std::vector;
28a4c9c5 62
51e77110
JH
63namespace pv {
64
2953961c 65// TODO: This should not be necessary
04abfae9 66SigSession* SigSession::_session = NULL;
2953961c 67
dc0867ff
JH
68SigSession::SigSession(DeviceManager &device_manager) :
69 _device_manager(device_manager),
5b7cf66c 70 _capture_state(Stopped)
2953961c
JH
71{
72 // TODO: This should not be necessary
04abfae9 73 _session = this;
d873f4d6
JH
74
75 set_default_device();
2953961c
JH
76}
77
78SigSession::~SigSession()
79{
85843b14
JH
80 using pv::device::Device;
81
5b7cf66c
JH
82 stop_capture();
83
6d483b8b
MC
84 if (_sampling_thread.joinable())
85 _sampling_thread.join();
2e2946fe 86
996b7c9d 87 _dev_inst->release();
0f784939 88
2953961c 89 // TODO: This should not be necessary
04abfae9 90 _session = NULL;
2953961c
JH
91}
92
94574501 93shared_ptr<device::DevInst> SigSession::get_device() const
dc0867ff 94{
19adbc2c 95 return _dev_inst;
dc0867ff
JH
96}
97
ae2d1bc5
JH
98void SigSession::set_device(
99 shared_ptr<device::DevInst> dev_inst) throw(QString)
d64d1596 100{
85843b14
JH
101 using pv::device::Device;
102
82596f46
JH
103 // Ensure we are not capturing before setting the device
104 stop_capture();
105
ae2d1bc5
JH
106 if (_dev_inst) {
107 sr_session_datafeed_callback_remove_all();
996b7c9d 108 _dev_inst->release();
ae2d1bc5
JH
109 }
110
111 _dev_inst = dev_inst;
112 _decode_traces.clear();
85843b14 113
ae2d1bc5 114 if (dev_inst) {
996b7c9d 115 dev_inst->use(this);
ae2d1bc5
JH
116 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
117 update_signals(dev_inst);
118 }
119}
85843b14 120
ae2d1bc5
JH
121void SigSession::set_file(const string &name) throw(QString)
122{
5ab6596d
JS
123 // Deselect the old device, because file type detection in File::create
124 // destroys the old session inside libsigrok.
ae2d1bc5
JH
125 set_device(shared_ptr<device::DevInst>());
126 set_device(shared_ptr<device::DevInst>(device::File::create(name)));
d64d1596
JH
127}
128
6fd0b639
JH
129void SigSession::set_default_device()
130{
131 shared_ptr<pv::device::DevInst> default_device;
132 const list< shared_ptr<device::Device> > &devices =
133 _device_manager.devices();
134
135 if (!devices.empty()) {
136 // Fall back to the first device in the list.
137 default_device = devices.front();
138
139 // Try and find the demo device and select that by default
140 BOOST_FOREACH (shared_ptr<pv::device::Device> dev, devices)
141 if (strcmp(dev->dev_inst()->driver->name,
142 "demo") == 0) {
143 default_device = dev;
144 break;
145 }
146 }
147
148 set_device(default_device);
149}
150
996b7c9d 151void SigSession::release_device(device::DevInst *dev_inst)
dc0867ff 152{
19adbc2c 153 (void)dev_inst;
996b7c9d 154 assert(_dev_inst.get() == dev_inst);
dc0867ff
JH
155
156 assert(_capture_state == Stopped);
94574501 157 _dev_inst = shared_ptr<device::DevInst>();
dc0867ff
JH
158}
159
5b7cf66c
JH
160SigSession::capture_state SigSession::get_capture_state() const
161{
949f8050 162 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
163 return _capture_state;
164}
165
d99dc9f2 166void SigSession::start_capture(function<void (const QString)> error_handler)
2e2946fe 167{
5b7cf66c
JH
168 stop_capture();
169
d64d1596 170 // Check that a device instance has been selected.
19adbc2c 171 if (!_dev_inst) {
d64d1596
JH
172 qDebug() << "No device selected";
173 return;
174 }
175
19adbc2c
JH
176 assert(_dev_inst->dev_inst());
177
9c48fa57
JH
178 // Check that at least one probe is enabled
179 const GSList *l;
4871ed92
UH
180 for (l = _dev_inst->dev_inst()->channels; l; l = l->next) {
181 sr_channel *const probe = (sr_channel*)l->data;
9c48fa57
JH
182 assert(probe);
183 if (probe->enabled)
184 break;
185 }
186
187 if (!l) {
4871ed92 188 error_handler(tr("No channels enabled."));
9c48fa57
JH
189 return;
190 }
191
192 // Begin the session
e042ad64 193 _sampling_thread = boost::thread(
19adbc2c
JH
194 &SigSession::sample_thread_proc, this, _dev_inst,
195 error_handler);
2e2946fe
JH
196}
197
5b7cf66c
JH
198void SigSession::stop_capture()
199{
333d5bbc 200 if (get_capture_state() == Stopped)
5b7cf66c
JH
201 return;
202
203 sr_session_stop();
204
205 // Check that sampling stopped
6d483b8b
MC
206 if (_sampling_thread.joinable())
207 _sampling_thread.join();
5b7cf66c
JH
208}
209
02412f0b
JH
210set< shared_ptr<data::SignalData> > SigSession::get_data() const
211{
212 lock_guard<mutex> lock(_signals_mutex);
213 set< shared_ptr<data::SignalData> > data;
214 BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) {
215 assert(sig);
216 data.insert(sig->data());
217 }
218
219 return data;
220}
221
38eeddea 222vector< shared_ptr<view::Signal> > SigSession::get_signals() const
2e2946fe 223{
3868e5fa 224 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
225 return _signals;
226}
227
269528f5 228#ifdef ENABLE_DECODE
4e5a4405 229bool SigSession::add_decoder(srd_decoder *const dec)
82c7f640 230{
8bd26d8b 231 map<const srd_channel*, shared_ptr<view::LogicSignal> > probes;
7491a29f 232 shared_ptr<data::DecoderStack> decoder_stack;
4e5a4405 233
e92cd4e4 234 try
82c7f640 235 {
119aff65 236 lock_guard<mutex> lock(_signals_mutex);
e0fc5810 237
4e5a4405 238 // Create the decoder
7491a29f 239 decoder_stack = shared_ptr<data::DecoderStack>(
bdc5c3b0 240 new data::DecoderStack(*this, dec));
4e5a4405 241
d2f46d27 242 // Make a list of all the probes
8bd26d8b
UH
243 std::vector<const srd_channel*> all_probes;
244 for(const GSList *i = dec->channels; i; i = i->next)
245 all_probes.push_back((const srd_channel*)i->data);
246 for(const GSList *i = dec->opt_channels; i; i = i->next)
247 all_probes.push_back((const srd_channel*)i->data);
d2f46d27
JH
248
249 // Auto select the initial probes
8bd26d8b 250 BOOST_FOREACH(const srd_channel *pdch, all_probes)
4e5a4405
JH
251 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
252 {
253 shared_ptr<view::LogicSignal> l =
254 dynamic_pointer_cast<view::LogicSignal>(s);
8bd26d8b 255 if (l && QString::fromUtf8(pdch->name).
27e8df22 256 toLower().contains(
4e5a4405 257 l->get_name().toLower()))
8bd26d8b 258 probes[pdch] = l;
4e5a4405 259 }
4e5a4405 260
7491a29f
JH
261 assert(decoder_stack);
262 assert(!decoder_stack->stack().empty());
263 assert(decoder_stack->stack().front());
264 decoder_stack->stack().front()->set_probes(probes);
4e5a4405
JH
265
266 // Create the decode signal
b9329558 267 shared_ptr<view::DecodeTrace> d(
7491a29f 268 new view::DecodeTrace(*this, decoder_stack,
06bb4e6a 269 _decode_traces.size()));
82c7f640
JH
270 _decode_traces.push_back(d);
271 }
e92cd4e4
JH
272 catch(std::runtime_error e)
273 {
274 return false;
275 }
276
82c7f640 277 signals_changed();
e92cd4e4 278
7491a29f
JH
279 // Do an initial decode
280 decoder_stack->begin_decode();
281
e92cd4e4 282 return true;
82c7f640
JH
283}
284
b9329558 285vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
38eeddea
JH
286{
287 lock_guard<mutex> lock(_signals_mutex);
288 return _decode_traces;
289}
290
b9329558 291void SigSession::remove_decode_signal(view::DecodeTrace *signal)
c51482b3 292{
f46e495e 293 for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++)
c51482b3
JH
294 if ((*i).get() == signal)
295 {
296 _decode_traces.erase(i);
297 signals_changed();
298 return;
299 }
300}
269528f5 301#endif
c51482b3 302
6ac96c2e
JH
303void SigSession::set_capture_state(capture_state state)
304{
949f8050 305 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 306 const bool changed = _capture_state != state;
6ac96c2e 307 _capture_state = state;
2b49eeb0
JH
308 if(changed)
309 capture_state_changed(state);
6ac96c2e
JH
310}
311
94574501 312void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst)
b087ba7f 313{
19adbc2c 314 assert(dev_inst);
b087ba7f
JH
315 assert(_capture_state == Stopped);
316
b087ba7f 317 unsigned int logic_probe_count = 0;
b087ba7f 318
82c7f640
JH
319 // Clear the decode traces
320 _decode_traces.clear();
321
b087ba7f 322 // Detect what data types we will receive
19adbc2c
JH
323 if(dev_inst) {
324 assert(dev_inst->dev_inst());
4871ed92 325 for (const GSList *l = dev_inst->dev_inst()->channels;
19adbc2c 326 l; l = l->next) {
4871ed92 327 const sr_channel *const probe = (const sr_channel *)l->data;
b087ba7f
JH
328 if (!probe->enabled)
329 continue;
330
331 switch(probe->type) {
4871ed92 332 case SR_CHANNEL_LOGIC:
b087ba7f
JH
333 logic_probe_count++;
334 break;
b087ba7f
JH
335 }
336 }
337 }
338
bb2cdfff 339 // Create data containers for the logic data snapshots
b087ba7f
JH
340 {
341 lock_guard<mutex> data_lock(_data_mutex);
342
343 _logic_data.reset();
344 if (logic_probe_count != 0) {
345 _logic_data.reset(new data::Logic(
346 logic_probe_count));
347 assert(_logic_data);
348 }
b087ba7f
JH
349 }
350
351 // Make the Signals list
39e680c2 352 do {
b087ba7f
JH
353 lock_guard<mutex> lock(_signals_mutex);
354
355 _signals.clear();
356
19adbc2c 357 if(!dev_inst)
39e680c2
JH
358 break;
359
19adbc2c 360 assert(dev_inst->dev_inst());
4871ed92 361 for (const GSList *l = dev_inst->dev_inst()->channels;
19adbc2c 362 l; l = l->next) {
39e680c2 363 shared_ptr<view::Signal> signal;
4871ed92 364 sr_channel *const probe = (sr_channel *)l->data;
39e680c2
JH
365 assert(probe);
366
367 switch(probe->type) {
4871ed92 368 case SR_CHANNEL_LOGIC:
39e680c2 369 signal = shared_ptr<view::Signal>(
83c23cc9 370 new view::LogicSignal(dev_inst,
8d3e0764 371 probe, _logic_data));
39e680c2
JH
372 break;
373
4871ed92 374 case SR_CHANNEL_ANALOG:
bb2cdfff
JH
375 {
376 shared_ptr<data::Analog> data(
377 new data::Analog());
39e680c2 378 signal = shared_ptr<view::Signal>(
83c23cc9 379 new view::AnalogSignal(dev_inst,
8d3e0764 380 probe, data));
39e680c2 381 break;
bb2cdfff 382 }
39e680c2
JH
383
384 default:
385 assert(0);
386 break;
b087ba7f 387 }
39e680c2
JH
388
389 assert(signal);
390 _signals.push_back(signal);
b087ba7f 391 }
39e680c2
JH
392
393 } while(0);
b087ba7f
JH
394
395 signals_changed();
396}
397
3ddcc083 398shared_ptr<view::Signal> SigSession::signal_from_probe(
4871ed92 399 const sr_channel *probe) const
3ddcc083
JH
400{
401 lock_guard<mutex> lock(_signals_mutex);
402 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
403 assert(sig);
404 if (sig->probe() == probe)
405 return sig;
406 }
407 return shared_ptr<view::Signal>();
408}
409
deef291c
JH
410void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
411{
412 GVariant *gvar;
413 uint64_t sample_rate = 0;
414
415 // Read out the sample rate
416 if(sdi->driver)
417 {
68162c29
BV
418 const int ret = sr_config_get(sdi->driver, sdi, NULL,
419 SR_CONF_SAMPLERATE, &gvar);
deef291c
JH
420 if (ret != SR_OK) {
421 qDebug("Failed to get samplerate\n");
422 return;
423 }
424
425 sample_rate = g_variant_get_uint64(gvar);
426 g_variant_unref(gvar);
427 }
428
bb2cdfff
JH
429 // Set the sample rate of all data
430 const set< shared_ptr<data::SignalData> > data_set = get_data();
431 BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) {
432 assert(data);
433 data->set_samplerate(sample_rate);
434 }
deef291c
JH
435}
436
94574501 437void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst,
f2edb557 438 function<void (const QString)> error_handler)
274d4f13 439{
19adbc2c
JH
440 assert(dev_inst);
441 assert(dev_inst->dev_inst());
f2edb557 442 assert(error_handler);
be73bdfa 443
ae2d1bc5 444 read_sample_rate(dev_inst->dev_inst());
274d4f13 445
ae2d1bc5
JH
446 try {
447 dev_inst->start();
448 } catch(const QString e) {
449 error_handler(e);
eec446e1
JH
450 return;
451 }
452
07dcf561
JH
453 set_capture_state(dev_inst->is_trigger_enabled() ?
454 AwaitingTrigger : Running);
eec446e1 455
ae2d1bc5 456 dev_inst->run();
eec446e1 457 set_capture_state(Stopped);
1a2fe44c
JH
458
459 // Confirm that SR_DF_END was received
bb2cdfff
JH
460 if (_cur_logic_snapshot)
461 {
462 qDebug("SR_DF_END was not received.");
463 assert(0);
464 }
eec446e1
JH
465}
466
467void SigSession::feed_in_header(const sr_dev_inst *sdi)
468{
deef291c 469 read_sample_rate(sdi);
be73bdfa
JH
470}
471
472void SigSession::feed_in_meta(const sr_dev_inst *sdi,
473 const sr_datafeed_meta &meta)
474{
94fe58ef
UH
475 (void)sdi;
476
be73bdfa
JH
477 for (const GSList *l = meta.config; l; l = l->next) {
478 const sr_config *const src = (const sr_config*)l->data;
479 switch (src->key) {
480 case SR_CONF_SAMPLERATE:
481 /// @todo handle samplerate changes
482 /// samplerate = (uint64_t *)src->value;
483 break;
484 default:
485 // Unknown metadata is not an error.
486 break;
487 }
aba1dd16 488 }
74043039
JH
489
490 signals_changed();
aba1dd16
JH
491}
492
82f50b10
JH
493void SigSession::feed_in_frame_begin()
494{
495 if (_cur_logic_snapshot || !_cur_analog_snapshots.empty())
496 frame_began();
497}
498
9c112671
JH
499void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
500{
501 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
502
503 if (!_logic_data)
9c112671 504 {
79efbc53
JH
505 qDebug() << "Unexpected logic packet";
506 return;
507 }
be73bdfa 508
79efbc53
JH
509 if (!_cur_logic_snapshot)
510 {
bb2cdfff 511 // This could be the first packet after a trigger
2b49eeb0
JH
512 set_capture_state(Running);
513
9c112671 514 // Create a new data snapshot
1b1ec774 515 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
27d7c96b 516 new data::LogicSnapshot(logic, _dev_inst->get_sample_limit()));
9c112671 517 _logic_data->push_snapshot(_cur_logic_snapshot);
82f50b10
JH
518
519 // @todo Putting this here means that only listeners querying
520 // for logic will be notified. Currently the only user of
521 // frame_began is DecoderStack, but in future we need to signal
522 // this after both analog and logic sweeps have begun.
523 frame_began();
9c112671
JH
524 }
525 else
526 {
527 // Append to the existing data snapshot
528 _cur_logic_snapshot->append_payload(logic);
529 }
530
1f374035 531 data_received();
9c112671
JH
532}
533
aba1dd16
JH
534void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
535{
536 lock_guard<mutex> lock(_data_mutex);
79efbc53 537
4871ed92 538 const unsigned int probe_count = g_slist_length(analog.channels);
bb2cdfff
JH
539 const size_t sample_count = analog.num_samples / probe_count;
540 const float *data = analog.data;
541 bool sweep_beginning = false;
be73bdfa 542
4871ed92 543 for (GSList *p = analog.channels; p; p = p->next)
79efbc53 544 {
bb2cdfff 545 shared_ptr<data::AnalogSnapshot> snapshot;
2b49eeb0 546
4871ed92 547 sr_channel *const probe = (sr_channel*)p->data;
bb2cdfff
JH
548 assert(probe);
549
550 // Try to get the snapshot of the probe
4871ed92 551 const map< const sr_channel*, shared_ptr<data::AnalogSnapshot> >::
bb2cdfff
JH
552 iterator iter = _cur_analog_snapshots.find(probe);
553 if (iter != _cur_analog_snapshots.end())
554 snapshot = (*iter).second;
555 else
556 {
557 // If no snapshot was found, this means we havn't
558 // created one yet. i.e. this is the first packet
559 // in the sweep containing this snapshot.
560 sweep_beginning = true;
561
562 // Create a snapshot, keep it in the maps of probes
563 snapshot = shared_ptr<data::AnalogSnapshot>(
27d7c96b 564 new data::AnalogSnapshot(_dev_inst->get_sample_limit()));
bb2cdfff
JH
565 _cur_analog_snapshots[probe] = snapshot;
566
567 // Find the annalog data associated with the probe
568 shared_ptr<view::AnalogSignal> sig =
569 dynamic_pointer_cast<view::AnalogSignal>(
570 signal_from_probe(probe));
571 assert(sig);
572
573 shared_ptr<data::Analog> data(sig->analog_data());
574 assert(data);
575
576 // Push the snapshot into the analog data.
577 data->push_snapshot(snapshot);
578 }
579
580 assert(snapshot);
581
582 // Append the samples in the snapshot
583 snapshot->append_interleaved_samples(data++, sample_count,
584 probe_count);
aba1dd16 585 }
bb2cdfff
JH
586
587 if (sweep_beginning) {
588 // This could be the first packet after a trigger
589 set_capture_state(Running);
aba1dd16
JH
590 }
591
1f374035 592 data_received();
aba1dd16 593}
9c112671 594
b0e1d01d
JH
595void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
596 const struct sr_datafeed_packet *packet)
597{
598 assert(sdi);
599 assert(packet);
600
601 switch (packet->type) {
602 case SR_DF_HEADER:
eec446e1 603 feed_in_header(sdi);
b0e1d01d
JH
604 break;
605
be73bdfa 606 case SR_DF_META:
aba1dd16 607 assert(packet->payload);
be73bdfa
JH
608 feed_in_meta(sdi,
609 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
610 break;
611
82f50b10
JH
612 case SR_DF_FRAME_BEGIN:
613 feed_in_frame_begin();
614 break;
615
2e2946fe 616 case SR_DF_LOGIC:
28a4c9c5 617 assert(packet->payload);
9c112671 618 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
619 break;
620
aba1dd16
JH
621 case SR_DF_ANALOG:
622 assert(packet->payload);
623 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
624 break;
625
2953961c 626 case SR_DF_END:
2e2946fe
JH
627 {
628 {
629 lock_guard<mutex> lock(_data_mutex);
630 _cur_logic_snapshot.reset();
bb2cdfff 631 _cur_analog_snapshots.clear();
2e2946fe 632 }
1f374035 633 frame_ended();
2953961c
JH
634 break;
635 }
2e2946fe 636 }
2953961c
JH
637}
638
04abfae9 639void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 640 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 641{
5045f16d 642 (void) cb_data;
04abfae9
JH
643 assert(_session);
644 _session->data_feed_in(sdi, packet);
2953961c 645}
51e77110
JH
646
647} // namespace pv