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