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