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