2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <libsigrokdecode/libsigrokdecode.h>
23 #include "sigsession.h"
25 #include "devicemanager.h"
27 #include "data/analog.h"
28 #include "data/analogsnapshot.h"
29 #include "data/decoderstack.h"
30 #include "data/logic.h"
31 #include "data/logicsnapshot.h"
32 #include "data/decode/decoder.h"
34 #include "view/analogsignal.h"
35 #include "view/decodetrace.h"
36 #include "view/logicsignal.h"
42 #include <boost/foreach.hpp>
48 using namespace boost;
53 // TODO: This should not be necessary
54 SigSession* SigSession::_session = NULL;
56 SigSession::SigSession(DeviceManager &device_manager) :
57 _device_manager(device_manager),
59 _capture_state(Stopped)
61 // TODO: This should not be necessary
65 SigSession::~SigSession()
69 _sampling_thread.join();
72 _device_manager.release_device(_sdi);
75 // TODO: This should not be necessary
79 struct sr_dev_inst* SigSession::get_device() const
84 void SigSession::set_device(struct sr_dev_inst *sdi)
87 _device_manager.release_device(_sdi);
89 _device_manager.use_device(sdi, this);
94 void SigSession::release_device(struct sr_dev_inst *sdi)
98 assert(_capture_state == Stopped);
100 update_signals(NULL);
103 void SigSession::load_file(const string &name,
104 function<void (const QString)> error_handler)
108 if (sr_session_load(name.c_str()) == SR_OK) {
109 GSList *devlist = NULL;
110 sr_session_dev_list(&devlist);
112 if (!devlist || !devlist->data ||
113 sr_session_start() != SR_OK) {
114 error_handler(tr("Failed to start session."));
118 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
119 g_slist_free(devlist);
121 _decode_traces.clear();
123 read_sample_rate(sdi);
125 _sampling_thread = boost::thread(
126 &SigSession::load_session_thread_proc, this,
132 if (!(in = load_input_file_format(name.c_str(),
136 _decode_traces.clear();
137 update_signals(in->sdi);
138 read_sample_rate(in->sdi);
140 _sampling_thread = boost::thread(
141 &SigSession::load_input_thread_proc, this,
142 name, in, error_handler);
146 SigSession::capture_state SigSession::get_capture_state() const
148 lock_guard<mutex> lock(_sampling_mutex);
149 return _capture_state;
152 void SigSession::start_capture(uint64_t record_length,
153 function<void (const QString)> error_handler)
157 // Check that a device instance has been selected.
159 qDebug() << "No device selected";
163 // Check that at least one probe is enabled
165 for (l = _sdi->probes; l; l = l->next) {
166 sr_probe *const probe = (sr_probe*)l->data;
173 error_handler(tr("No probes enabled."));
178 _sampling_thread = boost::thread(
179 &SigSession::sample_thread_proc, this, _sdi,
180 record_length, error_handler);
183 void SigSession::stop_capture()
185 if (get_capture_state() == Stopped)
190 // Check that sampling stopped
191 _sampling_thread.join();
194 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
196 lock_guard<mutex> lock(_signals_mutex);
200 boost::shared_ptr<data::Logic> SigSession::get_data()
205 bool SigSession::add_decoder(srd_decoder *const dec)
207 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
208 shared_ptr<data::DecoderStack> decoder_stack;
212 lock_guard<mutex> lock(_signals_mutex);
214 // Create the decoder
215 decoder_stack = shared_ptr<data::DecoderStack>(
216 new data::DecoderStack(dec));
218 // Auto select the initial probes
219 for(const GSList *i = dec->probes; i; i = i->next)
221 const srd_probe *const probe = (const srd_probe*)i->data;
222 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
224 shared_ptr<view::LogicSignal> l =
225 dynamic_pointer_cast<view::LogicSignal>(s);
226 if (l && QString(probe->name).toLower().contains(
227 l->get_name().toLower()))
232 assert(decoder_stack);
233 assert(!decoder_stack->stack().empty());
234 assert(decoder_stack->stack().front());
235 decoder_stack->stack().front()->set_probes(probes);
237 // Create the decode signal
238 shared_ptr<view::DecodeTrace> d(
239 new view::DecodeTrace(*this, decoder_stack,
240 _decode_traces.size()));
241 _decode_traces.push_back(d);
243 catch(std::runtime_error e)
250 // Do an initial decode
251 decoder_stack->begin_decode();
256 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
258 lock_guard<mutex> lock(_signals_mutex);
259 return _decode_traces;
262 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
264 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
265 _decode_traces.begin();
266 i != _decode_traces.end();
268 if ((*i).get() == signal)
270 _decode_traces.erase(i);
276 void SigSession::set_capture_state(capture_state state)
278 lock_guard<mutex> lock(_sampling_mutex);
279 const bool changed = _capture_state != state;
280 _capture_state = state;
282 capture_state_changed(state);
286 * Attempts to autodetect the format. Failing that
287 * @param filename The filename of the input file.
288 * @return A pointer to the 'struct sr_input_format' that should be used,
289 * or NULL if no input format was selected or auto-detected.
291 sr_input_format* SigSession::determine_input_file_format(
292 const string &filename)
296 /* If there are no input formats, return NULL right away. */
297 sr_input_format *const *const inputs = sr_input_list();
299 g_critical("No supported input formats available.");
303 /* Otherwise, try to find an input module that can handle this file. */
304 for (i = 0; inputs[i]; i++) {
305 if (inputs[i]->format_match(filename.c_str()))
309 /* Return NULL if no input module wanted to touch this. */
311 g_critical("Error: no matching input module found.");
318 sr_input* SigSession::load_input_file_format(const string &filename,
319 function<void (const QString)> error_handler,
320 sr_input_format *format)
325 if (!format && !(format =
326 determine_input_file_format(filename.c_str()))) {
327 /* The exact cause was already logged. */
331 if (stat(filename.c_str(), &st) == -1) {
332 error_handler(tr("Failed to load file"));
336 /* Initialize the input module. */
337 if (!(in = new sr_input)) {
338 qDebug("Failed to allocate input module.\n");
344 if (in->format->init &&
345 in->format->init(in, filename.c_str()) != SR_OK) {
346 qDebug("Input format init failed.\n");
352 if (sr_session_dev_add(in->sdi) != SR_OK) {
353 qDebug("Failed to use device.\n");
354 sr_session_destroy();
361 void SigSession::update_signals(const sr_dev_inst *const sdi)
363 assert(_capture_state == Stopped);
365 shared_ptr<view::Signal> signal;
366 unsigned int logic_probe_count = 0;
367 unsigned int analog_probe_count = 0;
369 // Clear the decode traces
370 _decode_traces.clear();
372 // Detect what data types we will receive
374 for (const GSList *l = sdi->probes; l; l = l->next) {
375 const sr_probe *const probe = (const sr_probe *)l->data;
379 switch(probe->type) {
384 case SR_PROBE_ANALOG:
385 analog_probe_count++;
391 // Create data containers for the data snapshots
393 lock_guard<mutex> data_lock(_data_mutex);
396 if (logic_probe_count != 0) {
397 _logic_data.reset(new data::Logic(
402 _analog_data.reset();
403 if (analog_probe_count != 0) {
404 _analog_data.reset(new data::Analog());
405 assert(_analog_data);
409 // Make the Signals list
411 lock_guard<mutex> lock(_signals_mutex);
416 for (const GSList *l = sdi->probes; l; l = l->next) {
417 sr_probe *const probe = (sr_probe *)l->data;
420 switch(probe->type) {
422 signal = shared_ptr<view::Signal>(
423 new view::LogicSignal(*this, probe,
427 case SR_PROBE_ANALOG:
428 signal = shared_ptr<view::Signal>(
429 new view::AnalogSignal(*this, probe,
434 _signals.push_back(signal);
442 bool SigSession::is_trigger_enabled() const
445 for (const GSList *l = _sdi->probes; l; l = l->next) {
446 const sr_probe *const p = (const sr_probe *)l->data;
448 if (p->trigger && p->trigger[0] != '\0')
455 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
458 uint64_t sample_rate = 0;
460 // Read out the sample rate
463 const int ret = sr_config_get(sdi->driver, sdi, NULL,
464 SR_CONF_SAMPLERATE, &gvar);
466 qDebug("Failed to get samplerate\n");
470 sample_rate = g_variant_get_uint64(gvar);
471 g_variant_unref(gvar);
475 _analog_data->set_samplerate(sample_rate);
477 _logic_data->set_samplerate(sample_rate);
480 void SigSession::load_session_thread_proc(
481 function<void (const QString)> error_handler)
485 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
487 set_capture_state(Running);
491 sr_session_destroy();
492 set_capture_state(Stopped);
494 // Confirm that SR_DF_END was received
495 assert(!_cur_logic_snapshot);
496 assert(!_cur_analog_snapshot);
499 void SigSession::load_input_thread_proc(const string name,
500 sr_input *in, function<void (const QString)> error_handler)
507 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
509 set_capture_state(Running);
511 in->format->loadfile(in, name.c_str());
513 sr_session_destroy();
514 set_capture_state(Stopped);
516 // Confirm that SR_DF_END was received
517 assert(!_cur_logic_snapshot);
518 assert(!_cur_analog_snapshot);
523 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
524 uint64_t record_length,
525 function<void (const QString)> error_handler)
528 assert(error_handler);
531 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
533 if (sr_session_dev_add(sdi) != SR_OK) {
534 error_handler(tr("Failed to use device."));
535 sr_session_destroy();
539 // Set the sample limit
540 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
541 g_variant_new_uint64(record_length)) != SR_OK) {
542 error_handler(tr("Failed to configure "
543 "time-based sample limit."));
544 sr_session_destroy();
548 if (sr_session_start() != SR_OK) {
549 error_handler(tr("Failed to start session."));
553 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
556 sr_session_destroy();
558 set_capture_state(Stopped);
560 // Confirm that SR_DF_END was received
561 assert(!_cur_logic_snapshot);
562 assert(!_cur_analog_snapshot);
565 void SigSession::feed_in_header(const sr_dev_inst *sdi)
567 read_sample_rate(sdi);
570 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
571 const sr_datafeed_meta &meta)
575 for (const GSList *l = meta.config; l; l = l->next) {
576 const sr_config *const src = (const sr_config*)l->data;
578 case SR_CONF_SAMPLERATE:
579 /// @todo handle samplerate changes
580 /// samplerate = (uint64_t *)src->value;
583 // Unknown metadata is not an error.
591 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
593 lock_guard<mutex> lock(_data_mutex);
597 qDebug() << "Unexpected logic packet";
601 if (!_cur_logic_snapshot)
603 set_capture_state(Running);
605 // Create a new data snapshot
606 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
607 new data::LogicSnapshot(logic));
608 _logic_data->push_snapshot(_cur_logic_snapshot);
612 // Append to the existing data snapshot
613 _cur_logic_snapshot->append_payload(logic);
619 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
621 lock_guard<mutex> lock(_data_mutex);
625 qDebug() << "Unexpected analog packet";
626 return; // This analog packet was not expected.
629 if (!_cur_analog_snapshot)
631 set_capture_state(Running);
633 // Create a new data snapshot
634 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
635 new data::AnalogSnapshot(analog));
636 _analog_data->push_snapshot(_cur_analog_snapshot);
640 // Append to the existing data snapshot
641 _cur_analog_snapshot->append_payload(analog);
647 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
648 const struct sr_datafeed_packet *packet)
653 switch (packet->type) {
659 assert(packet->payload);
661 *(const sr_datafeed_meta*)packet->payload);
665 assert(packet->payload);
666 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
670 assert(packet->payload);
671 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
677 lock_guard<mutex> lock(_data_mutex);
678 _cur_logic_snapshot.reset();
679 _cur_analog_snapshot.reset();
687 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
688 const struct sr_datafeed_packet *packet, void *cb_data)
692 _session->data_feed_in(sdi, packet);