]> sigrok.org Git - pulseview.git/blame_incremental - pv/sigsession.cpp
Made decode an optional feature disabled by default.
[pulseview.git] / pv / sigsession.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#ifdef ENABLE_DECODE
22#include <libsigrokdecode/libsigrokdecode.h>
23#endif
24
25#include "sigsession.h"
26
27#include "devicemanager.h"
28
29#include "data/analog.h"
30#include "data/analogsnapshot.h"
31#include "data/decoderstack.h"
32#include "data/logic.h"
33#include "data/logicsnapshot.h"
34#include "data/decode/decoder.h"
35
36#include "view/analogsignal.h"
37#include "view/decodetrace.h"
38#include "view/logicsignal.h"
39
40#include <assert.h>
41
42#include <stdexcept>
43
44#include <boost/foreach.hpp>
45
46#include <sys/stat.h>
47
48#include <QDebug>
49
50using namespace boost;
51using namespace std;
52
53namespace pv {
54
55// TODO: This should not be necessary
56SigSession* SigSession::_session = NULL;
57
58SigSession::SigSession(DeviceManager &device_manager) :
59 _device_manager(device_manager),
60 _sdi(NULL),
61 _capture_state(Stopped)
62{
63 // TODO: This should not be necessary
64 _session = this;
65}
66
67SigSession::~SigSession()
68{
69 stop_capture();
70
71 _sampling_thread.join();
72
73 if (_sdi)
74 _device_manager.release_device(_sdi);
75 _sdi = NULL;
76
77 // TODO: This should not be necessary
78 _session = NULL;
79}
80
81struct sr_dev_inst* SigSession::get_device() const
82{
83 return _sdi;
84}
85
86void SigSession::set_device(struct sr_dev_inst *sdi)
87{
88 if (_sdi)
89 _device_manager.release_device(_sdi);
90 if (sdi)
91 _device_manager.use_device(sdi, this);
92 _sdi = sdi;
93 update_signals(sdi);
94}
95
96void SigSession::release_device(struct sr_dev_inst *sdi)
97{
98 (void)sdi;
99
100 assert(_capture_state == Stopped);
101 _sdi = NULL;
102 update_signals(NULL);
103}
104
105void SigSession::load_file(const string &name,
106 function<void (const QString)> error_handler)
107{
108 stop_capture();
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
120 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
121 g_slist_free(devlist);
122
123 _decode_traces.clear();
124 update_signals(sdi);
125 read_sample_rate(sdi);
126
127 _sampling_thread = boost::thread(
128 &SigSession::load_session_thread_proc, this,
129 error_handler);
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
138 _decode_traces.clear();
139 update_signals(in->sdi);
140 read_sample_rate(in->sdi);
141
142 _sampling_thread = boost::thread(
143 &SigSession::load_input_thread_proc, this,
144 name, in, error_handler);
145 }
146}
147
148SigSession::capture_state SigSession::get_capture_state() const
149{
150 lock_guard<mutex> lock(_sampling_mutex);
151 return _capture_state;
152}
153
154void SigSession::start_capture(uint64_t record_length,
155 function<void (const QString)> error_handler)
156{
157 stop_capture();
158
159 // Check that a device instance has been selected.
160 if (!_sdi) {
161 qDebug() << "No device selected";
162 return;
163 }
164
165 // Check that at least one probe is enabled
166 const GSList *l;
167 for (l = _sdi->probes; l; l = l->next) {
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
180 _sampling_thread = boost::thread(
181 &SigSession::sample_thread_proc, this, _sdi,
182 record_length, error_handler);
183}
184
185void SigSession::stop_capture()
186{
187 if (get_capture_state() == Stopped)
188 return;
189
190 sr_session_stop();
191
192 // Check that sampling stopped
193 _sampling_thread.join();
194}
195
196vector< shared_ptr<view::Signal> > SigSession::get_signals() const
197{
198 lock_guard<mutex> lock(_signals_mutex);
199 return _signals;
200}
201
202boost::shared_ptr<data::Logic> SigSession::get_data()
203{
204 return _logic_data;
205}
206
207#ifdef ENABLE_DECODE
208bool SigSession::add_decoder(srd_decoder *const dec)
209{
210 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
211 shared_ptr<data::DecoderStack> decoder_stack;
212
213 try
214 {
215 lock_guard<mutex> lock(_signals_mutex);
216
217 // Create the decoder
218 decoder_stack = shared_ptr<data::DecoderStack>(
219 new data::DecoderStack(dec));
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);
229 if (l && QString::fromUtf8(probe->name).
230 toLower().contains(
231 l->get_name().toLower()))
232 probes[probe] = l;
233 }
234 }
235
236 assert(decoder_stack);
237 assert(!decoder_stack->stack().empty());
238 assert(decoder_stack->stack().front());
239 decoder_stack->stack().front()->set_probes(probes);
240
241 // Create the decode signal
242 shared_ptr<view::DecodeTrace> d(
243 new view::DecodeTrace(*this, decoder_stack,
244 _decode_traces.size()));
245 _decode_traces.push_back(d);
246 }
247 catch(std::runtime_error e)
248 {
249 return false;
250 }
251
252 signals_changed();
253
254 // Do an initial decode
255 decoder_stack->begin_decode();
256
257 return true;
258}
259
260vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
261{
262 lock_guard<mutex> lock(_signals_mutex);
263 return _decode_traces;
264}
265
266void SigSession::remove_decode_signal(view::DecodeTrace *signal)
267{
268 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
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}
279#endif
280
281void SigSession::set_capture_state(capture_state state)
282{
283 lock_guard<mutex> lock(_sampling_mutex);
284 const bool changed = _capture_state != state;
285 _capture_state = state;
286 if(changed)
287 capture_state_changed(state);
288}
289
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
366void SigSession::update_signals(const sr_dev_inst *const sdi)
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
374 // Clear the decode traces
375 _decode_traces.clear();
376
377 // Detect what data types we will receive
378 if(sdi) {
379 for (const GSList *l = sdi->probes; l; l = l->next) {
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
420 if(sdi) {
421 for (const GSList *l = sdi->probes; l; l = l->next) {
422 sr_probe *const probe = (sr_probe *)l->data;
423 assert(probe);
424
425 switch(probe->type) {
426 case SR_PROBE_LOGIC:
427 signal = shared_ptr<view::Signal>(
428 new view::LogicSignal(*this, probe,
429 _logic_data));
430 break;
431
432 case SR_PROBE_ANALOG:
433 signal = shared_ptr<view::Signal>(
434 new view::AnalogSignal(*this, probe,
435 _analog_data));
436 break;
437 }
438
439 _signals.push_back(signal);
440 }
441 }
442 }
443
444 signals_changed();
445}
446
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
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 {
468 const int ret = sr_config_get(sdi->driver, sdi, NULL,
469 SR_CONF_SAMPLERATE, &gvar);
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
485void SigSession::load_session_thread_proc(
486 function<void (const QString)> error_handler)
487{
488 (void)error_handler;
489
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);
511
512 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
513
514 set_capture_state(Running);
515
516 in->format->loadfile(in, name.c_str());
517
518 sr_session_destroy();
519 set_capture_state(Stopped);
520
521 // Confirm that SR_DF_END was received
522 assert(!_cur_logic_snapshot);
523 assert(!_cur_analog_snapshot);
524
525 delete in;
526}
527
528void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
529 uint64_t record_length,
530 function<void (const QString)> error_handler)
531{
532 assert(sdi);
533 assert(error_handler);
534
535 sr_session_new();
536 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
537
538 if (sr_session_dev_add(sdi) != SR_OK) {
539 error_handler(tr("Failed to use device."));
540 sr_session_destroy();
541 return;
542 }
543
544 // Set the sample limit
545 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
546 g_variant_new_uint64(record_length)) != SR_OK) {
547 error_handler(tr("Failed to configure "
548 "time-based sample limit."));
549 sr_session_destroy();
550 return;
551 }
552
553 if (sr_session_start() != SR_OK) {
554 error_handler(tr("Failed to start session."));
555 return;
556 }
557
558 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
559
560 sr_session_run();
561 sr_session_destroy();
562
563 set_capture_state(Stopped);
564
565 // Confirm that SR_DF_END was received
566 assert(!_cur_logic_snapshot);
567 assert(!_cur_analog_snapshot);
568}
569
570void SigSession::feed_in_header(const sr_dev_inst *sdi)
571{
572 read_sample_rate(sdi);
573}
574
575void SigSession::feed_in_meta(const sr_dev_inst *sdi,
576 const sr_datafeed_meta &meta)
577{
578 (void)sdi;
579
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 }
591 }
592
593 signals_changed();
594}
595
596void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
597{
598 lock_guard<mutex> lock(_data_mutex);
599
600 if (!_logic_data)
601 {
602 qDebug() << "Unexpected logic packet";
603 return;
604 }
605
606 if (!_cur_logic_snapshot)
607 {
608 set_capture_state(Running);
609
610 // Create a new data snapshot
611 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
612 new data::LogicSnapshot(logic));
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
624void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
625{
626 lock_guard<mutex> lock(_data_mutex);
627
628 if(!_analog_data)
629 {
630 qDebug() << "Unexpected analog packet";
631 return; // This analog packet was not expected.
632 }
633
634 if (!_cur_analog_snapshot)
635 {
636 set_capture_state(Running);
637
638 // Create a new data snapshot
639 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
640 new data::AnalogSnapshot(analog));
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}
651
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:
660 feed_in_header(sdi);
661 break;
662
663 case SR_DF_META:
664 assert(packet->payload);
665 feed_in_meta(sdi,
666 *(const sr_datafeed_meta*)packet->payload);
667 break;
668
669 case SR_DF_LOGIC:
670 assert(packet->payload);
671 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
672 break;
673
674 case SR_DF_ANALOG:
675 assert(packet->payload);
676 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
677 break;
678
679 case SR_DF_END:
680 {
681 {
682 lock_guard<mutex> lock(_data_mutex);
683 _cur_logic_snapshot.reset();
684 _cur_analog_snapshot.reset();
685 }
686 data_updated();
687 break;
688 }
689 }
690}
691
692void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
693 const struct sr_datafeed_packet *packet, void *cb_data)
694{
695 (void) cb_data;
696 assert(_session);
697 _session->data_feed_in(sdi, packet);
698}
699
700} // namespace pv