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