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