]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Made decode an optional feature disabled by default.
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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"
119aff65 28
1b1ec774
JH
29#include "data/analog.h"
30#include "data/analogsnapshot.h"
6e89374a 31#include "data/decoderstack.h"
1b1ec774
JH
32#include "data/logic.h"
33#include "data/logicsnapshot.h"
7491a29f 34#include "data/decode/decoder.h"
82c7f640 35
aba1dd16 36#include "view/analogsignal.h"
b9329558 37#include "view/decodetrace.h"
8d634081 38#include "view/logicsignal.h"
28a4c9c5 39
2953961c
JH
40#include <assert.h>
41
e92cd4e4
JH
42#include <stdexcept>
43
4e5a4405
JH
44#include <boost/foreach.hpp>
45
728e5ef7
JH
46#include <sys/stat.h>
47
79efbc53
JH
48#include <QDebug>
49
28a4c9c5 50using namespace boost;
e3f65ace 51using namespace std;
28a4c9c5 52
51e77110
JH
53namespace pv {
54
2953961c 55// TODO: This should not be necessary
04abfae9 56SigSession* SigSession::_session = NULL;
2953961c 57
dc0867ff
JH
58SigSession::SigSession(DeviceManager &device_manager) :
59 _device_manager(device_manager),
d64d1596 60 _sdi(NULL),
5b7cf66c 61 _capture_state(Stopped)
2953961c
JH
62{
63 // TODO: This should not be necessary
04abfae9 64 _session = this;
2953961c
JH
65}
66
67SigSession::~SigSession()
68{
5b7cf66c
JH
69 stop_capture();
70
e042ad64 71 _sampling_thread.join();
2e2946fe 72
0f784939
JH
73 if (_sdi)
74 _device_manager.release_device(_sdi);
75 _sdi = NULL;
76
2953961c 77 // TODO: This should not be necessary
04abfae9 78 _session = NULL;
2953961c
JH
79}
80
dc0867ff
JH
81struct sr_dev_inst* SigSession::get_device() const
82{
83 return _sdi;
84}
85
d64d1596
JH
86void SigSession::set_device(struct sr_dev_inst *sdi)
87{
88497156 88 if (_sdi)
dc0867ff 89 _device_manager.release_device(_sdi);
88497156 90 if (sdi)
dc0867ff 91 _device_manager.use_device(sdi, this);
d64d1596 92 _sdi = sdi;
b698553c 93 update_signals(sdi);
d64d1596
JH
94}
95
dc0867ff
JH
96void SigSession::release_device(struct sr_dev_inst *sdi)
97{
98 (void)sdi;
99
100 assert(_capture_state == Stopped);
101 _sdi = NULL;
b698553c 102 update_signals(NULL);
dc0867ff
JH
103}
104
f2edb557
JH
105void SigSession::load_file(const string &name,
106 function<void (const QString)> error_handler)
2953961c 107{
8a67bd9e 108 stop_capture();
e8c9f8a5
JH
109
110 if (sr_session_load(name.c_str()) == SR_OK) {
111 GSList *devlist = NULL;
112 sr_session_dev_list(&devlist);
113
114 if (!devlist || !devlist->data ||
115 sr_session_start() != SR_OK) {
116 error_handler(tr("Failed to start session."));
117 return;
118 }
119
deef291c 120 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
e8c9f8a5
JH
121 g_slist_free(devlist);
122
7d0c935c 123 _decode_traces.clear();
deef291c
JH
124 update_signals(sdi);
125 read_sample_rate(sdi);
126
e042ad64 127 _sampling_thread = boost::thread(
e8c9f8a5 128 &SigSession::load_session_thread_proc, this,
e042ad64 129 error_handler);
e8c9f8a5
JH
130
131 } else {
132 sr_input *in = NULL;
133
134 if (!(in = load_input_file_format(name.c_str(),
135 error_handler)))
136 return;
137
7d0c935c 138 _decode_traces.clear();
e8c9f8a5 139 update_signals(in->sdi);
deef291c 140 read_sample_rate(in->sdi);
e8c9f8a5 141
e042ad64 142 _sampling_thread = boost::thread(
e8c9f8a5 143 &SigSession::load_input_thread_proc, this,
e042ad64 144 name, in, error_handler);
e8c9f8a5 145 }
2953961c
JH
146}
147
5b7cf66c
JH
148SigSession::capture_state SigSession::get_capture_state() const
149{
949f8050 150 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
151 return _capture_state;
152}
153
d64d1596 154void SigSession::start_capture(uint64_t record_length,
f2edb557 155 function<void (const QString)> error_handler)
2e2946fe 156{
5b7cf66c
JH
157 stop_capture();
158
d64d1596
JH
159 // Check that a device instance has been selected.
160 if (!_sdi) {
161 qDebug() << "No device selected";
162 return;
163 }
164
9c48fa57
JH
165 // Check that at least one probe is enabled
166 const GSList *l;
d64d1596 167 for (l = _sdi->probes; l; l = l->next) {
9c48fa57
JH
168 sr_probe *const probe = (sr_probe*)l->data;
169 assert(probe);
170 if (probe->enabled)
171 break;
172 }
173
174 if (!l) {
175 error_handler(tr("No probes enabled."));
176 return;
177 }
178
179 // Begin the session
e042ad64 180 _sampling_thread = boost::thread(
d64d1596 181 &SigSession::sample_thread_proc, this, _sdi,
e042ad64 182 record_length, error_handler);
2e2946fe
JH
183}
184
5b7cf66c
JH
185void SigSession::stop_capture()
186{
333d5bbc 187 if (get_capture_state() == Stopped)
5b7cf66c
JH
188 return;
189
190 sr_session_stop();
191
192 // Check that sampling stopped
e042ad64 193 _sampling_thread.join();
5b7cf66c
JH
194}
195
38eeddea 196vector< shared_ptr<view::Signal> > SigSession::get_signals() const
2e2946fe 197{
3868e5fa 198 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
199 return _signals;
200}
201
1b1ec774 202boost::shared_ptr<data::Logic> SigSession::get_data()
2e2946fe
JH
203{
204 return _logic_data;
205}
206
269528f5 207#ifdef ENABLE_DECODE
4e5a4405 208bool SigSession::add_decoder(srd_decoder *const dec)
82c7f640 209{
4e5a4405 210 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
7491a29f 211 shared_ptr<data::DecoderStack> decoder_stack;
4e5a4405 212
e92cd4e4 213 try
82c7f640 214 {
119aff65 215 lock_guard<mutex> lock(_signals_mutex);
e0fc5810 216
4e5a4405 217 // Create the decoder
7491a29f
JH
218 decoder_stack = shared_ptr<data::DecoderStack>(
219 new data::DecoderStack(dec));
4e5a4405
JH
220
221 // Auto select the initial probes
222 for(const GSList *i = dec->probes; i; i = i->next)
223 {
224 const srd_probe *const probe = (const srd_probe*)i->data;
225 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
226 {
227 shared_ptr<view::LogicSignal> l =
228 dynamic_pointer_cast<view::LogicSignal>(s);
27e8df22
JH
229 if (l && QString::fromUtf8(probe->name).
230 toLower().contains(
4e5a4405
JH
231 l->get_name().toLower()))
232 probes[probe] = l;
233 }
234 }
235
7491a29f
JH
236 assert(decoder_stack);
237 assert(!decoder_stack->stack().empty());
238 assert(decoder_stack->stack().front());
239 decoder_stack->stack().front()->set_probes(probes);
4e5a4405
JH
240
241 // Create the decode signal
b9329558 242 shared_ptr<view::DecodeTrace> d(
7491a29f 243 new view::DecodeTrace(*this, decoder_stack,
06bb4e6a 244 _decode_traces.size()));
82c7f640
JH
245 _decode_traces.push_back(d);
246 }
e92cd4e4
JH
247 catch(std::runtime_error e)
248 {
249 return false;
250 }
251
82c7f640 252 signals_changed();
e92cd4e4 253
7491a29f
JH
254 // Do an initial decode
255 decoder_stack->begin_decode();
256
e92cd4e4 257 return true;
82c7f640
JH
258}
259
b9329558 260vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
38eeddea
JH
261{
262 lock_guard<mutex> lock(_signals_mutex);
263 return _decode_traces;
264}
265
b9329558 266void SigSession::remove_decode_signal(view::DecodeTrace *signal)
c51482b3 267{
b9329558 268 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
c51482b3
JH
269 _decode_traces.begin();
270 i != _decode_traces.end();
271 i++)
272 if ((*i).get() == signal)
273 {
274 _decode_traces.erase(i);
275 signals_changed();
276 return;
277 }
278}
269528f5 279#endif
c51482b3 280
6ac96c2e
JH
281void SigSession::set_capture_state(capture_state state)
282{
949f8050 283 lock_guard<mutex> lock(_sampling_mutex);
2b49eeb0 284 const bool changed = _capture_state != state;
6ac96c2e 285 _capture_state = state;
2b49eeb0
JH
286 if(changed)
287 capture_state_changed(state);
6ac96c2e
JH
288}
289
728e5ef7
JH
290/**
291 * Attempts to autodetect the format. Failing that
292 * @param filename The filename of the input file.
293 * @return A pointer to the 'struct sr_input_format' that should be used,
294 * or NULL if no input format was selected or auto-detected.
295 */
296sr_input_format* SigSession::determine_input_file_format(
297 const string &filename)
298{
299 int i;
300
301 /* If there are no input formats, return NULL right away. */
302 sr_input_format *const *const inputs = sr_input_list();
303 if (!inputs) {
304 g_critical("No supported input formats available.");
305 return NULL;
306 }
307
308 /* Otherwise, try to find an input module that can handle this file. */
309 for (i = 0; inputs[i]; i++) {
310 if (inputs[i]->format_match(filename.c_str()))
311 break;
312 }
313
314 /* Return NULL if no input module wanted to touch this. */
315 if (!inputs[i]) {
316 g_critical("Error: no matching input module found.");
317 return NULL;
318 }
319
320 return inputs[i];
321}
322
323sr_input* SigSession::load_input_file_format(const string &filename,
324 function<void (const QString)> error_handler,
325 sr_input_format *format)
326{
327 struct stat st;
328 sr_input *in;
329
330 if (!format && !(format =
331 determine_input_file_format(filename.c_str()))) {
332 /* The exact cause was already logged. */
333 return NULL;
334 }
335
336 if (stat(filename.c_str(), &st) == -1) {
337 error_handler(tr("Failed to load file"));
338 return NULL;
339 }
340
341 /* Initialize the input module. */
342 if (!(in = new sr_input)) {
343 qDebug("Failed to allocate input module.\n");
344 return NULL;
345 }
346
347 in->format = format;
348 in->param = NULL;
349 if (in->format->init &&
350 in->format->init(in, filename.c_str()) != SR_OK) {
351 qDebug("Input format init failed.\n");
352 return NULL;
353 }
354
355 sr_session_new();
356
357 if (sr_session_dev_add(in->sdi) != SR_OK) {
358 qDebug("Failed to use device.\n");
359 sr_session_destroy();
360 return NULL;
361 }
362
363 return in;
364}
365
b698553c 366void SigSession::update_signals(const sr_dev_inst *const sdi)
b087ba7f
JH
367{
368 assert(_capture_state == Stopped);
369
370 shared_ptr<view::Signal> signal;
371 unsigned int logic_probe_count = 0;
372 unsigned int analog_probe_count = 0;
373
82c7f640
JH
374 // Clear the decode traces
375 _decode_traces.clear();
376
b087ba7f 377 // Detect what data types we will receive
b698553c
JH
378 if(sdi) {
379 for (const GSList *l = sdi->probes; l; l = l->next) {
b087ba7f
JH
380 const sr_probe *const probe = (const sr_probe *)l->data;
381 if (!probe->enabled)
382 continue;
383
384 switch(probe->type) {
385 case SR_PROBE_LOGIC:
386 logic_probe_count++;
387 break;
388
389 case SR_PROBE_ANALOG:
390 analog_probe_count++;
391 break;
392 }
393 }
394 }
395
396 // Create data containers for the data snapshots
397 {
398 lock_guard<mutex> data_lock(_data_mutex);
399
400 _logic_data.reset();
401 if (logic_probe_count != 0) {
402 _logic_data.reset(new data::Logic(
403 logic_probe_count));
404 assert(_logic_data);
405 }
406
407 _analog_data.reset();
408 if (analog_probe_count != 0) {
409 _analog_data.reset(new data::Analog());
410 assert(_analog_data);
411 }
412 }
413
414 // Make the Signals list
415 {
416 lock_guard<mutex> lock(_signals_mutex);
417
418 _signals.clear();
419
b698553c
JH
420 if(sdi) {
421 for (const GSList *l = sdi->probes; l; l = l->next) {
aca00b1e 422 sr_probe *const probe = (sr_probe *)l->data;
b087ba7f 423 assert(probe);
b087ba7f
JH
424
425 switch(probe->type) {
426 case SR_PROBE_LOGIC:
427 signal = shared_ptr<view::Signal>(
c0f86852 428 new view::LogicSignal(*this, probe,
cec48d16 429 _logic_data));
b087ba7f
JH
430 break;
431
432 case SR_PROBE_ANALOG:
433 signal = shared_ptr<view::Signal>(
c0f86852 434 new view::AnalogSignal(*this, probe,
cec48d16 435 _analog_data));
b087ba7f
JH
436 break;
437 }
438
439 _signals.push_back(signal);
440 }
441 }
442 }
443
444 signals_changed();
445}
446
2b49eeb0
JH
447bool SigSession::is_trigger_enabled() const
448{
449 assert(_sdi);
450 for (const GSList *l = _sdi->probes; l; l = l->next) {
451 const sr_probe *const p = (const sr_probe *)l->data;
452 assert(p);
453 if (p->trigger && p->trigger[0] != '\0')
454 return true;
455 }
456
457 return false;
458}
459
deef291c
JH
460void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
461{
462 GVariant *gvar;
463 uint64_t sample_rate = 0;
464
465 // Read out the sample rate
466 if(sdi->driver)
467 {
68162c29
BV
468 const int ret = sr_config_get(sdi->driver, sdi, NULL,
469 SR_CONF_SAMPLERATE, &gvar);
deef291c
JH
470 if (ret != SR_OK) {
471 qDebug("Failed to get samplerate\n");
472 return;
473 }
474
475 sample_rate = g_variant_get_uint64(gvar);
476 g_variant_unref(gvar);
477 }
478
479 if(_analog_data)
480 _analog_data->set_samplerate(sample_rate);
481 if(_logic_data)
482 _logic_data->set_samplerate(sample_rate);
483}
484
e8c9f8a5 485void SigSession::load_session_thread_proc(
f2edb557 486 function<void (const QString)> error_handler)
8a67bd9e 487{
e8c9f8a5 488 (void)error_handler;
728e5ef7 489
e8c9f8a5
JH
490 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
491
492 set_capture_state(Running);
493
494 sr_session_run();
495
496 sr_session_destroy();
497 set_capture_state(Stopped);
498
499 // Confirm that SR_DF_END was received
500 assert(!_cur_logic_snapshot);
501 assert(!_cur_analog_snapshot);
502}
503
504void SigSession::load_input_thread_proc(const string name,
505 sr_input *in, function<void (const QString)> error_handler)
506{
507 (void)error_handler;
508
509 assert(in);
510 assert(in->format);
8a67bd9e 511
5045f16d 512 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
8a67bd9e 513
8a67bd9e
JH
514 set_capture_state(Running);
515
e8c9f8a5 516 in->format->loadfile(in, name.c_str());
8a67bd9e 517
728e5ef7 518 sr_session_destroy();
8a67bd9e 519 set_capture_state(Stopped);
1a2fe44c
JH
520
521 // Confirm that SR_DF_END was received
522 assert(!_cur_logic_snapshot);
523 assert(!_cur_analog_snapshot);
728e5ef7
JH
524
525 delete in;
8a67bd9e
JH
526}
527
2e2946fe 528void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
f2edb557
JH
529 uint64_t record_length,
530 function<void (const QString)> error_handler)
274d4f13 531{
be73bdfa 532 assert(sdi);
f2edb557 533 assert(error_handler);
be73bdfa 534
274d4f13 535 sr_session_new();
5045f16d 536 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
274d4f13
JH
537
538 if (sr_session_dev_add(sdi) != SR_OK) {
f2edb557 539 error_handler(tr("Failed to use device."));
274d4f13
JH
540 sr_session_destroy();
541 return;
542 }
543
be73bdfa 544 // Set the sample limit
68162c29 545 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
8eb9d311 546 g_variant_new_uint64(record_length)) != SR_OK) {
f2edb557
JH
547 error_handler(tr("Failed to configure "
548 "time-based sample limit."));
274d4f13
JH
549 sr_session_destroy();
550 return;
551 }
552
eec446e1 553 if (sr_session_start() != SR_OK) {
f2edb557 554 error_handler(tr("Failed to start session."));
eec446e1
JH
555 return;
556 }
557
2b49eeb0 558 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
eec446e1
JH
559
560 sr_session_run();
561 sr_session_destroy();
562
563 set_capture_state(Stopped);
1a2fe44c
JH
564
565 // Confirm that SR_DF_END was received
566 assert(!_cur_logic_snapshot);
567 assert(!_cur_analog_snapshot);
eec446e1
JH
568}
569
570void SigSession::feed_in_header(const sr_dev_inst *sdi)
571{
deef291c 572 read_sample_rate(sdi);
be73bdfa
JH
573}
574
575void SigSession::feed_in_meta(const sr_dev_inst *sdi,
576 const sr_datafeed_meta &meta)
577{
94fe58ef
UH
578 (void)sdi;
579
be73bdfa
JH
580 for (const GSList *l = meta.config; l; l = l->next) {
581 const sr_config *const src = (const sr_config*)l->data;
582 switch (src->key) {
583 case SR_CONF_SAMPLERATE:
584 /// @todo handle samplerate changes
585 /// samplerate = (uint64_t *)src->value;
586 break;
587 default:
588 // Unknown metadata is not an error.
589 break;
590 }
aba1dd16 591 }
74043039
JH
592
593 signals_changed();
aba1dd16
JH
594}
595
9c112671
JH
596void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
597{
598 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
599
600 if (!_logic_data)
9c112671 601 {
79efbc53
JH
602 qDebug() << "Unexpected logic packet";
603 return;
604 }
be73bdfa 605
79efbc53
JH
606 if (!_cur_logic_snapshot)
607 {
2b49eeb0
JH
608 set_capture_state(Running);
609
9c112671 610 // Create a new data snapshot
1b1ec774
JH
611 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
612 new data::LogicSnapshot(logic));
9c112671
JH
613 _logic_data->push_snapshot(_cur_logic_snapshot);
614 }
615 else
616 {
617 // Append to the existing data snapshot
618 _cur_logic_snapshot->append_payload(logic);
619 }
620
621 data_updated();
622}
623
aba1dd16
JH
624void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
625{
626 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
627
628 if(!_analog_data)
aba1dd16 629 {
79efbc53
JH
630 qDebug() << "Unexpected analog packet";
631 return; // This analog packet was not expected.
632 }
be73bdfa 633
79efbc53
JH
634 if (!_cur_analog_snapshot)
635 {
2b49eeb0
JH
636 set_capture_state(Running);
637
aba1dd16 638 // Create a new data snapshot
1b1ec774
JH
639 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
640 new data::AnalogSnapshot(analog));
aba1dd16
JH
641 _analog_data->push_snapshot(_cur_analog_snapshot);
642 }
643 else
644 {
645 // Append to the existing data snapshot
646 _cur_analog_snapshot->append_payload(analog);
647 }
648
649 data_updated();
650}
9c112671 651
b0e1d01d
JH
652void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
653 const struct sr_datafeed_packet *packet)
654{
655 assert(sdi);
656 assert(packet);
657
658 switch (packet->type) {
659 case SR_DF_HEADER:
eec446e1 660 feed_in_header(sdi);
b0e1d01d
JH
661 break;
662
be73bdfa 663 case SR_DF_META:
aba1dd16 664 assert(packet->payload);
be73bdfa
JH
665 feed_in_meta(sdi,
666 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
667 break;
668
2e2946fe 669 case SR_DF_LOGIC:
28a4c9c5 670 assert(packet->payload);
9c112671 671 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
672 break;
673
aba1dd16
JH
674 case SR_DF_ANALOG:
675 assert(packet->payload);
676 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
677 break;
678
2953961c 679 case SR_DF_END:
2e2946fe
JH
680 {
681 {
682 lock_guard<mutex> lock(_data_mutex);
683 _cur_logic_snapshot.reset();
aba1dd16 684 _cur_analog_snapshot.reset();
2e2946fe 685 }
04abfae9 686 data_updated();
2953961c
JH
687 break;
688 }
2e2946fe 689 }
2953961c
JH
690}
691
04abfae9 692void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 693 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 694{
5045f16d 695 (void) cb_data;
04abfae9
JH
696 assert(_session);
697 _session->data_feed_in(sdi, packet);
2953961c 698}
51e77110
JH
699
700} // namespace pv