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