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