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 boost::dynamic_pointer_cast;
51 using boost::function;
52 using boost::lock_guard;
54 using boost::shared_ptr;
61 // TODO: This should not be necessary
62 SigSession* SigSession::_session = NULL;
64 SigSession::SigSession(DeviceManager &device_manager) :
65 _device_manager(device_manager),
67 _capture_state(Stopped)
69 // TODO: This should not be necessary
73 SigSession::~SigSession()
77 _sampling_thread.join();
80 _device_manager.release_device(_sdi);
83 // TODO: This should not be necessary
87 struct sr_dev_inst* SigSession::get_device() const
92 void SigSession::set_device(struct sr_dev_inst *sdi)
94 // Ensure we are not capturing before setting the device
98 _device_manager.release_device(_sdi);
100 _device_manager.use_device(sdi, this);
105 void SigSession::release_device(struct sr_dev_inst *sdi)
109 assert(_capture_state == Stopped);
111 update_signals(NULL);
114 void SigSession::load_file(const string &name,
115 function<void (const QString)> error_handler)
119 if (sr_session_load(name.c_str()) == SR_OK) {
120 GSList *devlist = NULL;
121 sr_session_dev_list(&devlist);
123 if (!devlist || !devlist->data ||
124 sr_session_start() != SR_OK) {
125 error_handler(tr("Failed to start session."));
129 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
130 g_slist_free(devlist);
132 _decode_traces.clear();
134 read_sample_rate(sdi);
136 _sampling_thread = boost::thread(
137 &SigSession::load_session_thread_proc, this,
143 if (!(in = load_input_file_format(name.c_str(),
147 _decode_traces.clear();
148 update_signals(in->sdi);
149 read_sample_rate(in->sdi);
151 _sampling_thread = boost::thread(
152 &SigSession::load_input_thread_proc, this,
153 name, in, error_handler);
157 SigSession::capture_state SigSession::get_capture_state() const
159 lock_guard<mutex> lock(_sampling_mutex);
160 return _capture_state;
163 void SigSession::start_capture(uint64_t record_length,
164 function<void (const QString)> error_handler)
168 // Check that a device instance has been selected.
170 qDebug() << "No device selected";
174 // Check that at least one probe is enabled
176 for (l = _sdi->probes; l; l = l->next) {
177 sr_probe *const probe = (sr_probe*)l->data;
184 error_handler(tr("No probes enabled."));
189 _sampling_thread = boost::thread(
190 &SigSession::sample_thread_proc, this, _sdi,
191 record_length, error_handler);
194 void SigSession::stop_capture()
196 if (get_capture_state() == Stopped)
201 // Check that sampling stopped
202 _sampling_thread.join();
205 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
207 lock_guard<mutex> lock(_signals_mutex);
211 boost::shared_ptr<data::Logic> SigSession::get_data()
217 bool SigSession::add_decoder(srd_decoder *const dec)
219 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
220 shared_ptr<data::DecoderStack> decoder_stack;
224 lock_guard<mutex> lock(_signals_mutex);
226 // Create the decoder
227 decoder_stack = shared_ptr<data::DecoderStack>(
228 new data::DecoderStack(dec));
230 // Auto select the initial probes
231 for(const GSList *i = dec->probes; i; i = i->next)
233 const srd_probe *const probe = (const srd_probe*)i->data;
234 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
236 shared_ptr<view::LogicSignal> l =
237 dynamic_pointer_cast<view::LogicSignal>(s);
238 if (l && QString::fromUtf8(probe->name).
240 l->get_name().toLower()))
245 assert(decoder_stack);
246 assert(!decoder_stack->stack().empty());
247 assert(decoder_stack->stack().front());
248 decoder_stack->stack().front()->set_probes(probes);
250 // Create the decode signal
251 shared_ptr<view::DecodeTrace> d(
252 new view::DecodeTrace(*this, decoder_stack,
253 _decode_traces.size()));
254 _decode_traces.push_back(d);
256 catch(std::runtime_error e)
263 // Do an initial decode
264 decoder_stack->begin_decode();
269 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
271 lock_guard<mutex> lock(_signals_mutex);
272 return _decode_traces;
275 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
277 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
278 _decode_traces.begin();
279 i != _decode_traces.end();
281 if ((*i).get() == signal)
283 _decode_traces.erase(i);
290 void SigSession::set_capture_state(capture_state state)
292 lock_guard<mutex> lock(_sampling_mutex);
293 const bool changed = _capture_state != state;
294 _capture_state = state;
296 capture_state_changed(state);
300 * Attempts to autodetect the format. Failing that
301 * @param filename The filename of the input file.
302 * @return A pointer to the 'struct sr_input_format' that should be used,
303 * or NULL if no input format was selected or auto-detected.
305 sr_input_format* SigSession::determine_input_file_format(
306 const string &filename)
310 /* If there are no input formats, return NULL right away. */
311 sr_input_format *const *const inputs = sr_input_list();
313 g_critical("No supported input formats available.");
317 /* Otherwise, try to find an input module that can handle this file. */
318 for (i = 0; inputs[i]; i++) {
319 if (inputs[i]->format_match(filename.c_str()))
323 /* Return NULL if no input module wanted to touch this. */
325 g_critical("Error: no matching input module found.");
332 sr_input* SigSession::load_input_file_format(const string &filename,
333 function<void (const QString)> error_handler,
334 sr_input_format *format)
339 if (!format && !(format =
340 determine_input_file_format(filename.c_str()))) {
341 /* The exact cause was already logged. */
345 if (stat(filename.c_str(), &st) == -1) {
346 error_handler(tr("Failed to load file"));
350 /* Initialize the input module. */
351 if (!(in = new sr_input)) {
352 qDebug("Failed to allocate input module.\n");
358 if (in->format->init &&
359 in->format->init(in, filename.c_str()) != SR_OK) {
360 qDebug("Input format init failed.\n");
366 if (sr_session_dev_add(in->sdi) != SR_OK) {
367 qDebug("Failed to use device.\n");
368 sr_session_destroy();
375 void SigSession::update_signals(const sr_dev_inst *const sdi)
377 assert(_capture_state == Stopped);
379 shared_ptr<view::Signal> signal;
380 unsigned int logic_probe_count = 0;
381 unsigned int analog_probe_count = 0;
383 // Clear the decode traces
384 _decode_traces.clear();
386 // Detect what data types we will receive
388 for (const GSList *l = sdi->probes; l; l = l->next) {
389 const sr_probe *const probe = (const sr_probe *)l->data;
393 switch(probe->type) {
398 case SR_PROBE_ANALOG:
399 analog_probe_count++;
405 // Create data containers for the data snapshots
407 lock_guard<mutex> data_lock(_data_mutex);
410 if (logic_probe_count != 0) {
411 _logic_data.reset(new data::Logic(
416 _analog_data.reset();
417 if (analog_probe_count != 0) {
418 _analog_data.reset(new data::Analog());
419 assert(_analog_data);
423 // Make the Signals list
425 lock_guard<mutex> lock(_signals_mutex);
430 for (const GSList *l = sdi->probes; l; l = l->next) {
431 sr_probe *const probe = (sr_probe *)l->data;
434 switch(probe->type) {
436 signal = shared_ptr<view::Signal>(
437 new view::LogicSignal(*this, probe,
441 case SR_PROBE_ANALOG:
442 signal = shared_ptr<view::Signal>(
443 new view::AnalogSignal(*this, probe,
448 _signals.push_back(signal);
456 bool SigSession::is_trigger_enabled() const
459 for (const GSList *l = _sdi->probes; l; l = l->next) {
460 const sr_probe *const p = (const sr_probe *)l->data;
462 if (p->trigger && p->trigger[0] != '\0')
469 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
472 uint64_t sample_rate = 0;
474 // Read out the sample rate
477 const int ret = sr_config_get(sdi->driver, sdi, NULL,
478 SR_CONF_SAMPLERATE, &gvar);
480 qDebug("Failed to get samplerate\n");
484 sample_rate = g_variant_get_uint64(gvar);
485 g_variant_unref(gvar);
489 _analog_data->set_samplerate(sample_rate);
491 _logic_data->set_samplerate(sample_rate);
494 void SigSession::load_session_thread_proc(
495 function<void (const QString)> error_handler)
499 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
501 set_capture_state(Running);
505 sr_session_destroy();
506 set_capture_state(Stopped);
508 // Confirm that SR_DF_END was received
509 assert(!_cur_logic_snapshot);
510 assert(!_cur_analog_snapshot);
513 void SigSession::load_input_thread_proc(const string name,
514 sr_input *in, function<void (const QString)> error_handler)
521 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
523 set_capture_state(Running);
525 in->format->loadfile(in, name.c_str());
527 sr_session_destroy();
528 set_capture_state(Stopped);
530 // Confirm that SR_DF_END was received
531 assert(!_cur_logic_snapshot);
532 assert(!_cur_analog_snapshot);
537 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
538 uint64_t record_length,
539 function<void (const QString)> error_handler)
542 assert(error_handler);
545 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
547 if (sr_session_dev_add(sdi) != SR_OK) {
548 error_handler(tr("Failed to use device."));
549 sr_session_destroy();
553 // Set the sample limit
554 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
555 g_variant_new_uint64(record_length)) != SR_OK) {
556 error_handler(tr("Failed to configure "
557 "time-based sample limit."));
558 sr_session_destroy();
562 if (sr_session_start() != SR_OK) {
563 error_handler(tr("Failed to start session."));
567 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
570 sr_session_destroy();
572 set_capture_state(Stopped);
574 // Confirm that SR_DF_END was received
575 assert(!_cur_logic_snapshot);
576 assert(!_cur_analog_snapshot);
579 void SigSession::feed_in_header(const sr_dev_inst *sdi)
581 read_sample_rate(sdi);
584 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
585 const sr_datafeed_meta &meta)
589 for (const GSList *l = meta.config; l; l = l->next) {
590 const sr_config *const src = (const sr_config*)l->data;
592 case SR_CONF_SAMPLERATE:
593 /// @todo handle samplerate changes
594 /// samplerate = (uint64_t *)src->value;
597 // Unknown metadata is not an error.
605 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
607 lock_guard<mutex> lock(_data_mutex);
611 qDebug() << "Unexpected logic packet";
615 if (!_cur_logic_snapshot)
617 set_capture_state(Running);
619 // Create a new data snapshot
620 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
621 new data::LogicSnapshot(logic));
622 _logic_data->push_snapshot(_cur_logic_snapshot);
626 // Append to the existing data snapshot
627 _cur_logic_snapshot->append_payload(logic);
633 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
635 lock_guard<mutex> lock(_data_mutex);
639 qDebug() << "Unexpected analog packet";
640 return; // This analog packet was not expected.
643 if (!_cur_analog_snapshot)
645 set_capture_state(Running);
647 // Create a new data snapshot
648 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
649 new data::AnalogSnapshot(analog));
650 _analog_data->push_snapshot(_cur_analog_snapshot);
654 // Append to the existing data snapshot
655 _cur_analog_snapshot->append_payload(analog);
661 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
662 const struct sr_datafeed_packet *packet)
667 switch (packet->type) {
673 assert(packet->payload);
675 *(const sr_datafeed_meta*)packet->payload);
679 assert(packet->payload);
680 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
684 assert(packet->payload);
685 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
691 lock_guard<mutex> lock(_data_mutex);
692 _cur_logic_snapshot.reset();
693 _cur_analog_snapshot.reset();
701 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
702 const struct sr_datafeed_packet *packet, void *cb_data)
706 _session->data_feed_in(sdi, packet);