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