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