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