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