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::fromUtf8(probe->name).
228 l->get_name().toLower()))
233 assert(decoder_stack);
234 assert(!decoder_stack->stack().empty());
235 assert(decoder_stack->stack().front());
236 decoder_stack->stack().front()->set_probes(probes);
238 // Create the decode signal
239 shared_ptr<view::DecodeTrace> d(
240 new view::DecodeTrace(*this, decoder_stack,
241 _decode_traces.size()));
242 _decode_traces.push_back(d);
244 catch(std::runtime_error e)
251 // Do an initial decode
252 decoder_stack->begin_decode();
257 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
259 lock_guard<mutex> lock(_signals_mutex);
260 return _decode_traces;
263 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
265 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
266 _decode_traces.begin();
267 i != _decode_traces.end();
269 if ((*i).get() == signal)
271 _decode_traces.erase(i);
277 void SigSession::set_capture_state(capture_state state)
279 lock_guard<mutex> lock(_sampling_mutex);
280 const bool changed = _capture_state != state;
281 _capture_state = state;
283 capture_state_changed(state);
287 * Attempts to autodetect the format. Failing that
288 * @param filename The filename of the input file.
289 * @return A pointer to the 'struct sr_input_format' that should be used,
290 * or NULL if no input format was selected or auto-detected.
292 sr_input_format* SigSession::determine_input_file_format(
293 const string &filename)
297 /* If there are no input formats, return NULL right away. */
298 sr_input_format *const *const inputs = sr_input_list();
300 g_critical("No supported input formats available.");
304 /* Otherwise, try to find an input module that can handle this file. */
305 for (i = 0; inputs[i]; i++) {
306 if (inputs[i]->format_match(filename.c_str()))
310 /* Return NULL if no input module wanted to touch this. */
312 g_critical("Error: no matching input module found.");
319 sr_input* SigSession::load_input_file_format(const string &filename,
320 function<void (const QString)> error_handler,
321 sr_input_format *format)
326 if (!format && !(format =
327 determine_input_file_format(filename.c_str()))) {
328 /* The exact cause was already logged. */
332 if (stat(filename.c_str(), &st) == -1) {
333 error_handler(tr("Failed to load file"));
337 /* Initialize the input module. */
338 if (!(in = new sr_input)) {
339 qDebug("Failed to allocate input module.\n");
345 if (in->format->init &&
346 in->format->init(in, filename.c_str()) != SR_OK) {
347 qDebug("Input format init failed.\n");
353 if (sr_session_dev_add(in->sdi) != SR_OK) {
354 qDebug("Failed to use device.\n");
355 sr_session_destroy();
362 void SigSession::update_signals(const sr_dev_inst *const sdi)
364 assert(_capture_state == Stopped);
366 shared_ptr<view::Signal> signal;
367 unsigned int logic_probe_count = 0;
368 unsigned int analog_probe_count = 0;
370 // Clear the decode traces
371 _decode_traces.clear();
373 // Detect what data types we will receive
375 for (const GSList *l = sdi->probes; l; l = l->next) {
376 const sr_probe *const probe = (const sr_probe *)l->data;
380 switch(probe->type) {
385 case SR_PROBE_ANALOG:
386 analog_probe_count++;
392 // Create data containers for the data snapshots
394 lock_guard<mutex> data_lock(_data_mutex);
397 if (logic_probe_count != 0) {
398 _logic_data.reset(new data::Logic(
403 _analog_data.reset();
404 if (analog_probe_count != 0) {
405 _analog_data.reset(new data::Analog());
406 assert(_analog_data);
410 // Make the Signals list
412 lock_guard<mutex> lock(_signals_mutex);
417 for (const GSList *l = sdi->probes; l; l = l->next) {
418 sr_probe *const probe = (sr_probe *)l->data;
421 switch(probe->type) {
423 signal = shared_ptr<view::Signal>(
424 new view::LogicSignal(*this, probe,
428 case SR_PROBE_ANALOG:
429 signal = shared_ptr<view::Signal>(
430 new view::AnalogSignal(*this, probe,
435 _signals.push_back(signal);
443 bool SigSession::is_trigger_enabled() const
446 for (const GSList *l = _sdi->probes; l; l = l->next) {
447 const sr_probe *const p = (const sr_probe *)l->data;
449 if (p->trigger && p->trigger[0] != '\0')
456 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
459 uint64_t sample_rate = 0;
461 // Read out the sample rate
464 const int ret = sr_config_get(sdi->driver, sdi, NULL,
465 SR_CONF_SAMPLERATE, &gvar);
467 qDebug("Failed to get samplerate\n");
471 sample_rate = g_variant_get_uint64(gvar);
472 g_variant_unref(gvar);
476 _analog_data->set_samplerate(sample_rate);
478 _logic_data->set_samplerate(sample_rate);
481 void SigSession::load_session_thread_proc(
482 function<void (const QString)> error_handler)
486 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
488 set_capture_state(Running);
492 sr_session_destroy();
493 set_capture_state(Stopped);
495 // Confirm that SR_DF_END was received
496 assert(!_cur_logic_snapshot);
497 assert(!_cur_analog_snapshot);
500 void SigSession::load_input_thread_proc(const string name,
501 sr_input *in, function<void (const QString)> error_handler)
508 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
510 set_capture_state(Running);
512 in->format->loadfile(in, name.c_str());
514 sr_session_destroy();
515 set_capture_state(Stopped);
517 // Confirm that SR_DF_END was received
518 assert(!_cur_logic_snapshot);
519 assert(!_cur_analog_snapshot);
524 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
525 uint64_t record_length,
526 function<void (const QString)> error_handler)
529 assert(error_handler);
532 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
534 if (sr_session_dev_add(sdi) != SR_OK) {
535 error_handler(tr("Failed to use device."));
536 sr_session_destroy();
540 // Set the sample limit
541 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
542 g_variant_new_uint64(record_length)) != SR_OK) {
543 error_handler(tr("Failed to configure "
544 "time-based sample limit."));
545 sr_session_destroy();
549 if (sr_session_start() != SR_OK) {
550 error_handler(tr("Failed to start session."));
554 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
557 sr_session_destroy();
559 set_capture_state(Stopped);
561 // Confirm that SR_DF_END was received
562 assert(!_cur_logic_snapshot);
563 assert(!_cur_analog_snapshot);
566 void SigSession::feed_in_header(const sr_dev_inst *sdi)
568 read_sample_rate(sdi);
571 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
572 const sr_datafeed_meta &meta)
576 for (const GSList *l = meta.config; l; l = l->next) {
577 const sr_config *const src = (const sr_config*)l->data;
579 case SR_CONF_SAMPLERATE:
580 /// @todo handle samplerate changes
581 /// samplerate = (uint64_t *)src->value;
584 // Unknown metadata is not an error.
592 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
594 lock_guard<mutex> lock(_data_mutex);
598 qDebug() << "Unexpected logic packet";
602 if (!_cur_logic_snapshot)
604 set_capture_state(Running);
606 // Create a new data snapshot
607 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
608 new data::LogicSnapshot(logic));
609 _logic_data->push_snapshot(_cur_logic_snapshot);
613 // Append to the existing data snapshot
614 _cur_logic_snapshot->append_payload(logic);
620 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
622 lock_guard<mutex> lock(_data_mutex);
626 qDebug() << "Unexpected analog packet";
627 return; // This analog packet was not expected.
630 if (!_cur_analog_snapshot)
632 set_capture_state(Running);
634 // Create a new data snapshot
635 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
636 new data::AnalogSnapshot(analog));
637 _analog_data->push_snapshot(_cur_analog_snapshot);
641 // Append to the existing data snapshot
642 _cur_analog_snapshot->append_payload(analog);
648 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
649 const struct sr_datafeed_packet *packet)
654 switch (packet->type) {
660 assert(packet->payload);
662 *(const sr_datafeed_meta*)packet->payload);
666 assert(packet->payload);
667 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
671 assert(packet->payload);
672 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
678 lock_guard<mutex> lock(_data_mutex);
679 _cur_logic_snapshot.reset();
680 _cur_analog_snapshot.reset();
688 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
689 const struct sr_datafeed_packet *packet, void *cb_data)
693 _session->data_feed_in(sdi, packet);