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