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
22 #include <libsigrokdecode/libsigrokdecode.h>
25 #include "sigsession.h"
27 #include "devicemanager.h"
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"
36 #include "view/analogsignal.h"
37 #include "view/decodetrace.h"
38 #include "view/logicsignal.h"
44 #include <boost/foreach.hpp>
50 using namespace boost;
55 // TODO: This should not be necessary
56 SigSession* SigSession::_session = NULL;
58 SigSession::SigSession(DeviceManager &device_manager) :
59 _device_manager(device_manager),
61 _capture_state(Stopped)
63 // TODO: This should not be necessary
67 SigSession::~SigSession()
71 _sampling_thread.join();
74 _device_manager.release_device(_sdi);
77 // TODO: This should not be necessary
81 struct sr_dev_inst* SigSession::get_device() const
86 void SigSession::set_device(struct sr_dev_inst *sdi)
89 _device_manager.release_device(_sdi);
91 _device_manager.use_device(sdi, this);
96 void SigSession::release_device(struct sr_dev_inst *sdi)
100 assert(_capture_state == Stopped);
102 update_signals(NULL);
105 void SigSession::load_file(const string &name,
106 function<void (const QString)> error_handler)
110 if (sr_session_load(name.c_str()) == SR_OK) {
111 GSList *devlist = NULL;
112 sr_session_dev_list(&devlist);
114 if (!devlist || !devlist->data ||
115 sr_session_start() != SR_OK) {
116 error_handler(tr("Failed to start session."));
120 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
121 g_slist_free(devlist);
123 _decode_traces.clear();
125 read_sample_rate(sdi);
127 _sampling_thread = boost::thread(
128 &SigSession::load_session_thread_proc, this,
134 if (!(in = load_input_file_format(name.c_str(),
138 _decode_traces.clear();
139 update_signals(in->sdi);
140 read_sample_rate(in->sdi);
142 _sampling_thread = boost::thread(
143 &SigSession::load_input_thread_proc, this,
144 name, in, error_handler);
148 SigSession::capture_state SigSession::get_capture_state() const
150 lock_guard<mutex> lock(_sampling_mutex);
151 return _capture_state;
154 void SigSession::start_capture(uint64_t record_length,
155 function<void (const QString)> error_handler)
159 // Check that a device instance has been selected.
161 qDebug() << "No device selected";
165 // Check that at least one probe is enabled
167 for (l = _sdi->probes; l; l = l->next) {
168 sr_probe *const probe = (sr_probe*)l->data;
175 error_handler(tr("No probes enabled."));
180 _sampling_thread = boost::thread(
181 &SigSession::sample_thread_proc, this, _sdi,
182 record_length, error_handler);
185 void SigSession::stop_capture()
187 if (get_capture_state() == Stopped)
192 // Check that sampling stopped
193 _sampling_thread.join();
196 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
198 lock_guard<mutex> lock(_signals_mutex);
202 boost::shared_ptr<data::Logic> SigSession::get_data()
208 bool SigSession::add_decoder(srd_decoder *const dec)
210 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
211 shared_ptr<data::DecoderStack> decoder_stack;
215 lock_guard<mutex> lock(_signals_mutex);
217 // Create the decoder
218 decoder_stack = shared_ptr<data::DecoderStack>(
219 new data::DecoderStack(dec));
221 // Auto select the initial probes
222 for(const GSList *i = dec->probes; i; i = i->next)
224 const srd_probe *const probe = (const srd_probe*)i->data;
225 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
227 shared_ptr<view::LogicSignal> l =
228 dynamic_pointer_cast<view::LogicSignal>(s);
229 if (l && QString::fromUtf8(probe->name).
231 l->get_name().toLower()))
236 assert(decoder_stack);
237 assert(!decoder_stack->stack().empty());
238 assert(decoder_stack->stack().front());
239 decoder_stack->stack().front()->set_probes(probes);
241 // Create the decode signal
242 shared_ptr<view::DecodeTrace> d(
243 new view::DecodeTrace(*this, decoder_stack,
244 _decode_traces.size()));
245 _decode_traces.push_back(d);
247 catch(std::runtime_error e)
254 // Do an initial decode
255 decoder_stack->begin_decode();
260 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
262 lock_guard<mutex> lock(_signals_mutex);
263 return _decode_traces;
266 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
268 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
269 _decode_traces.begin();
270 i != _decode_traces.end();
272 if ((*i).get() == signal)
274 _decode_traces.erase(i);
281 void SigSession::set_capture_state(capture_state state)
283 lock_guard<mutex> lock(_sampling_mutex);
284 const bool changed = _capture_state != state;
285 _capture_state = state;
287 capture_state_changed(state);
291 * Attempts to autodetect the format. Failing that
292 * @param filename The filename of the input file.
293 * @return A pointer to the 'struct sr_input_format' that should be used,
294 * or NULL if no input format was selected or auto-detected.
296 sr_input_format* SigSession::determine_input_file_format(
297 const string &filename)
301 /* If there are no input formats, return NULL right away. */
302 sr_input_format *const *const inputs = sr_input_list();
304 g_critical("No supported input formats available.");
308 /* Otherwise, try to find an input module that can handle this file. */
309 for (i = 0; inputs[i]; i++) {
310 if (inputs[i]->format_match(filename.c_str()))
314 /* Return NULL if no input module wanted to touch this. */
316 g_critical("Error: no matching input module found.");
323 sr_input* SigSession::load_input_file_format(const string &filename,
324 function<void (const QString)> error_handler,
325 sr_input_format *format)
330 if (!format && !(format =
331 determine_input_file_format(filename.c_str()))) {
332 /* The exact cause was already logged. */
336 if (stat(filename.c_str(), &st) == -1) {
337 error_handler(tr("Failed to load file"));
341 /* Initialize the input module. */
342 if (!(in = new sr_input)) {
343 qDebug("Failed to allocate input module.\n");
349 if (in->format->init &&
350 in->format->init(in, filename.c_str()) != SR_OK) {
351 qDebug("Input format init failed.\n");
357 if (sr_session_dev_add(in->sdi) != SR_OK) {
358 qDebug("Failed to use device.\n");
359 sr_session_destroy();
366 void SigSession::update_signals(const sr_dev_inst *const sdi)
368 assert(_capture_state == Stopped);
370 shared_ptr<view::Signal> signal;
371 unsigned int logic_probe_count = 0;
372 unsigned int analog_probe_count = 0;
374 // Clear the decode traces
375 _decode_traces.clear();
377 // Detect what data types we will receive
379 for (const GSList *l = sdi->probes; l; l = l->next) {
380 const sr_probe *const probe = (const sr_probe *)l->data;
384 switch(probe->type) {
389 case SR_PROBE_ANALOG:
390 analog_probe_count++;
396 // Create data containers for the data snapshots
398 lock_guard<mutex> data_lock(_data_mutex);
401 if (logic_probe_count != 0) {
402 _logic_data.reset(new data::Logic(
407 _analog_data.reset();
408 if (analog_probe_count != 0) {
409 _analog_data.reset(new data::Analog());
410 assert(_analog_data);
414 // Make the Signals list
416 lock_guard<mutex> lock(_signals_mutex);
421 for (const GSList *l = sdi->probes; l; l = l->next) {
422 sr_probe *const probe = (sr_probe *)l->data;
425 switch(probe->type) {
427 signal = shared_ptr<view::Signal>(
428 new view::LogicSignal(*this, probe,
432 case SR_PROBE_ANALOG:
433 signal = shared_ptr<view::Signal>(
434 new view::AnalogSignal(*this, probe,
439 _signals.push_back(signal);
447 bool SigSession::is_trigger_enabled() const
450 for (const GSList *l = _sdi->probes; l; l = l->next) {
451 const sr_probe *const p = (const sr_probe *)l->data;
453 if (p->trigger && p->trigger[0] != '\0')
460 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
463 uint64_t sample_rate = 0;
465 // Read out the sample rate
468 const int ret = sr_config_get(sdi->driver, sdi, NULL,
469 SR_CONF_SAMPLERATE, &gvar);
471 qDebug("Failed to get samplerate\n");
475 sample_rate = g_variant_get_uint64(gvar);
476 g_variant_unref(gvar);
480 _analog_data->set_samplerate(sample_rate);
482 _logic_data->set_samplerate(sample_rate);
485 void SigSession::load_session_thread_proc(
486 function<void (const QString)> error_handler)
490 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
492 set_capture_state(Running);
496 sr_session_destroy();
497 set_capture_state(Stopped);
499 // Confirm that SR_DF_END was received
500 assert(!_cur_logic_snapshot);
501 assert(!_cur_analog_snapshot);
504 void SigSession::load_input_thread_proc(const string name,
505 sr_input *in, function<void (const QString)> error_handler)
512 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
514 set_capture_state(Running);
516 in->format->loadfile(in, name.c_str());
518 sr_session_destroy();
519 set_capture_state(Stopped);
521 // Confirm that SR_DF_END was received
522 assert(!_cur_logic_snapshot);
523 assert(!_cur_analog_snapshot);
528 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
529 uint64_t record_length,
530 function<void (const QString)> error_handler)
533 assert(error_handler);
536 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
538 if (sr_session_dev_add(sdi) != SR_OK) {
539 error_handler(tr("Failed to use device."));
540 sr_session_destroy();
544 // Set the sample limit
545 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
546 g_variant_new_uint64(record_length)) != SR_OK) {
547 error_handler(tr("Failed to configure "
548 "time-based sample limit."));
549 sr_session_destroy();
553 if (sr_session_start() != SR_OK) {
554 error_handler(tr("Failed to start session."));
558 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
561 sr_session_destroy();
563 set_capture_state(Stopped);
565 // Confirm that SR_DF_END was received
566 assert(!_cur_logic_snapshot);
567 assert(!_cur_analog_snapshot);
570 void SigSession::feed_in_header(const sr_dev_inst *sdi)
572 read_sample_rate(sdi);
575 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
576 const sr_datafeed_meta &meta)
580 for (const GSList *l = meta.config; l; l = l->next) {
581 const sr_config *const src = (const sr_config*)l->data;
583 case SR_CONF_SAMPLERATE:
584 /// @todo handle samplerate changes
585 /// samplerate = (uint64_t *)src->value;
588 // Unknown metadata is not an error.
596 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
598 lock_guard<mutex> lock(_data_mutex);
602 qDebug() << "Unexpected logic packet";
606 if (!_cur_logic_snapshot)
608 set_capture_state(Running);
610 // Create a new data snapshot
611 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
612 new data::LogicSnapshot(logic));
613 _logic_data->push_snapshot(_cur_logic_snapshot);
617 // Append to the existing data snapshot
618 _cur_logic_snapshot->append_payload(logic);
624 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
626 lock_guard<mutex> lock(_data_mutex);
630 qDebug() << "Unexpected analog packet";
631 return; // This analog packet was not expected.
634 if (!_cur_analog_snapshot)
636 set_capture_state(Running);
638 // Create a new data snapshot
639 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
640 new data::AnalogSnapshot(analog));
641 _analog_data->push_snapshot(_cur_analog_snapshot);
645 // Append to the existing data snapshot
646 _cur_analog_snapshot->append_payload(analog);
652 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
653 const struct sr_datafeed_packet *packet)
658 switch (packet->type) {
664 assert(packet->payload);
666 *(const sr_datafeed_meta*)packet->payload);
670 assert(packet->payload);
671 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
675 assert(packet->payload);
676 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
682 lock_guard<mutex> lock(_data_mutex);
683 _cur_logic_snapshot.reset();
684 _cur_analog_snapshot.reset();
692 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
693 const struct sr_datafeed_packet *packet, void *cb_data)
697 _session->data_feed_in(sdi, packet);