2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-14 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;
62 // TODO: This should not be necessary
63 SigSession* SigSession::_session = NULL;
65 SigSession::SigSession(DeviceManager &device_manager) :
66 _device_manager(device_manager),
68 _capture_state(Stopped)
70 // TODO: This should not be necessary
74 SigSession::~SigSession()
78 _sampling_thread.join();
81 _device_manager.release_device(_sdi);
84 // TODO: This should not be necessary
88 struct sr_dev_inst* SigSession::get_device() const
93 void SigSession::set_device(struct sr_dev_inst *sdi)
95 // Ensure we are not capturing before setting the device
99 _device_manager.release_device(_sdi);
101 _device_manager.use_device(sdi, this);
106 void SigSession::release_device(struct sr_dev_inst *sdi)
110 assert(_capture_state == Stopped);
112 update_signals(NULL);
115 void SigSession::load_file(const string &name,
116 function<void (const QString)> error_handler)
120 if (sr_session_load(name.c_str()) == SR_OK) {
121 GSList *devlist = NULL;
122 sr_session_dev_list(&devlist);
124 if (!devlist || !devlist->data ||
125 sr_session_start() != SR_OK) {
126 error_handler(tr("Failed to start session."));
130 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
131 g_slist_free(devlist);
133 _decode_traces.clear();
135 read_sample_rate(sdi);
137 _sampling_thread = boost::thread(
138 &SigSession::load_session_thread_proc, this,
144 if (!(in = load_input_file_format(name.c_str(),
148 _decode_traces.clear();
149 update_signals(in->sdi);
150 read_sample_rate(in->sdi);
152 _sampling_thread = boost::thread(
153 &SigSession::load_input_thread_proc, this,
154 name, in, error_handler);
158 SigSession::capture_state SigSession::get_capture_state() const
160 lock_guard<mutex> lock(_sampling_mutex);
161 return _capture_state;
164 void SigSession::start_capture(uint64_t record_length,
165 function<void (const QString)> error_handler)
169 // Check that a device instance has been selected.
171 qDebug() << "No device selected";
175 // Check that at least one probe is enabled
177 for (l = _sdi->probes; l; l = l->next) {
178 sr_probe *const probe = (sr_probe*)l->data;
185 error_handler(tr("No probes enabled."));
190 _sampling_thread = boost::thread(
191 &SigSession::sample_thread_proc, this, _sdi,
192 record_length, error_handler);
195 void SigSession::stop_capture()
197 if (get_capture_state() == Stopped)
202 // Check that sampling stopped
203 _sampling_thread.join();
206 set< shared_ptr<data::SignalData> > SigSession::get_data() const
208 lock_guard<mutex> lock(_signals_mutex);
209 set< shared_ptr<data::SignalData> > data;
210 BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) {
212 data.insert(sig->data());
218 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
220 lock_guard<mutex> lock(_signals_mutex);
225 bool SigSession::add_decoder(srd_decoder *const dec)
227 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
228 shared_ptr<data::DecoderStack> decoder_stack;
232 lock_guard<mutex> lock(_signals_mutex);
234 // Create the decoder
235 decoder_stack = shared_ptr<data::DecoderStack>(
236 new data::DecoderStack(dec));
238 // Auto select the initial probes
239 for(const GSList *i = dec->probes; i; i = i->next)
241 const srd_probe *const probe = (const srd_probe*)i->data;
242 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
244 shared_ptr<view::LogicSignal> l =
245 dynamic_pointer_cast<view::LogicSignal>(s);
246 if (l && QString::fromUtf8(probe->name).
248 l->get_name().toLower()))
253 assert(decoder_stack);
254 assert(!decoder_stack->stack().empty());
255 assert(decoder_stack->stack().front());
256 decoder_stack->stack().front()->set_probes(probes);
258 // Create the decode signal
259 shared_ptr<view::DecodeTrace> d(
260 new view::DecodeTrace(*this, decoder_stack,
261 _decode_traces.size()));
262 _decode_traces.push_back(d);
264 catch(std::runtime_error e)
271 // Do an initial decode
272 decoder_stack->begin_decode();
277 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
279 lock_guard<mutex> lock(_signals_mutex);
280 return _decode_traces;
283 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
285 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
286 _decode_traces.begin();
287 i != _decode_traces.end();
289 if ((*i).get() == signal)
291 _decode_traces.erase(i);
298 void SigSession::set_capture_state(capture_state state)
300 lock_guard<mutex> lock(_sampling_mutex);
301 const bool changed = _capture_state != state;
302 _capture_state = state;
304 capture_state_changed(state);
308 * Attempts to autodetect the format. Failing that
309 * @param filename The filename of the input file.
310 * @return A pointer to the 'struct sr_input_format' that should be used,
311 * or NULL if no input format was selected or auto-detected.
313 sr_input_format* SigSession::determine_input_file_format(
314 const string &filename)
318 /* If there are no input formats, return NULL right away. */
319 sr_input_format *const *const inputs = sr_input_list();
321 g_critical("No supported input formats available.");
325 /* Otherwise, try to find an input module that can handle this file. */
326 for (i = 0; inputs[i]; i++) {
327 if (inputs[i]->format_match(filename.c_str()))
331 /* Return NULL if no input module wanted to touch this. */
333 g_critical("Error: no matching input module found.");
340 sr_input* SigSession::load_input_file_format(const string &filename,
341 function<void (const QString)> error_handler,
342 sr_input_format *format)
347 if (!format && !(format =
348 determine_input_file_format(filename.c_str()))) {
349 /* The exact cause was already logged. */
353 if (stat(filename.c_str(), &st) == -1) {
354 error_handler(tr("Failed to load file"));
358 /* Initialize the input module. */
359 if (!(in = new sr_input)) {
360 qDebug("Failed to allocate input module.\n");
366 if (in->format->init &&
367 in->format->init(in, filename.c_str()) != SR_OK) {
368 qDebug("Input format init failed.\n");
374 if (sr_session_dev_add(in->sdi) != SR_OK) {
375 qDebug("Failed to use device.\n");
376 sr_session_destroy();
383 void SigSession::update_signals(const sr_dev_inst *const sdi)
385 assert(_capture_state == Stopped);
387 unsigned int logic_probe_count = 0;
388 unsigned int analog_probe_count = 0;
390 // Clear the decode traces
391 _decode_traces.clear();
393 // Detect what data types we will receive
395 for (const GSList *l = sdi->probes; l; l = l->next) {
396 const sr_probe *const probe = (const sr_probe *)l->data;
400 switch(probe->type) {
405 case SR_PROBE_ANALOG:
406 analog_probe_count++;
412 // Create data containers for the data snapshots
414 lock_guard<mutex> data_lock(_data_mutex);
417 if (logic_probe_count != 0) {
418 _logic_data.reset(new data::Logic(
423 _analog_data.reset();
424 if (analog_probe_count != 0) {
425 _analog_data.reset(new data::Analog());
426 assert(_analog_data);
430 // Make the Signals list
432 lock_guard<mutex> lock(_signals_mutex);
439 for (const GSList *l = sdi->probes; l; l = l->next) {
440 shared_ptr<view::Signal> signal;
441 sr_probe *const probe = (sr_probe *)l->data;
444 switch(probe->type) {
446 signal = shared_ptr<view::Signal>(
447 new view::LogicSignal(*this, probe,
451 case SR_PROBE_ANALOG:
452 signal = shared_ptr<view::Signal>(
453 new view::AnalogSignal(*this, probe,
463 _signals.push_back(signal);
471 bool SigSession::is_trigger_enabled() const
474 for (const GSList *l = _sdi->probes; l; l = l->next) {
475 const sr_probe *const p = (const sr_probe *)l->data;
477 if (p->trigger && p->trigger[0] != '\0')
484 shared_ptr<view::Signal> SigSession::signal_from_probe(
485 const sr_probe *probe) const
487 lock_guard<mutex> lock(_signals_mutex);
488 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
490 if (sig->probe() == probe)
493 return shared_ptr<view::Signal>();
496 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
499 uint64_t sample_rate = 0;
501 // Read out the sample rate
504 const int ret = sr_config_get(sdi->driver, sdi, NULL,
505 SR_CONF_SAMPLERATE, &gvar);
507 qDebug("Failed to get samplerate\n");
511 sample_rate = g_variant_get_uint64(gvar);
512 g_variant_unref(gvar);
516 _analog_data->set_samplerate(sample_rate);
518 _logic_data->set_samplerate(sample_rate);
521 void SigSession::load_session_thread_proc(
522 function<void (const QString)> error_handler)
526 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
528 set_capture_state(Running);
532 sr_session_destroy();
533 set_capture_state(Stopped);
535 // Confirm that SR_DF_END was received
536 assert(!_cur_logic_snapshot);
537 assert(!_cur_analog_snapshot);
540 void SigSession::load_input_thread_proc(const string name,
541 sr_input *in, function<void (const QString)> error_handler)
548 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
550 set_capture_state(Running);
552 in->format->loadfile(in, name.c_str());
554 sr_session_destroy();
555 set_capture_state(Stopped);
557 // Confirm that SR_DF_END was received
558 assert(!_cur_logic_snapshot);
559 assert(!_cur_analog_snapshot);
564 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
565 uint64_t record_length,
566 function<void (const QString)> error_handler)
569 assert(error_handler);
572 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
574 if (sr_session_dev_add(sdi) != SR_OK) {
575 error_handler(tr("Failed to use device."));
576 sr_session_destroy();
580 // Set the sample limit
581 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
582 g_variant_new_uint64(record_length)) != SR_OK) {
583 error_handler(tr("Failed to configure "
584 "time-based sample limit."));
585 sr_session_destroy();
589 if (sr_session_start() != SR_OK) {
590 error_handler(tr("Failed to start session."));
594 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
597 sr_session_destroy();
599 set_capture_state(Stopped);
601 // Confirm that SR_DF_END was received
602 assert(!_cur_logic_snapshot);
603 assert(!_cur_analog_snapshot);
606 void SigSession::feed_in_header(const sr_dev_inst *sdi)
608 read_sample_rate(sdi);
611 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
612 const sr_datafeed_meta &meta)
616 for (const GSList *l = meta.config; l; l = l->next) {
617 const sr_config *const src = (const sr_config*)l->data;
619 case SR_CONF_SAMPLERATE:
620 /// @todo handle samplerate changes
621 /// samplerate = (uint64_t *)src->value;
624 // Unknown metadata is not an error.
632 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
634 lock_guard<mutex> lock(_data_mutex);
638 qDebug() << "Unexpected logic packet";
642 if (!_cur_logic_snapshot)
644 set_capture_state(Running);
646 // Create a new data snapshot
647 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
648 new data::LogicSnapshot(logic));
649 _logic_data->push_snapshot(_cur_logic_snapshot);
653 // Append to the existing data snapshot
654 _cur_logic_snapshot->append_payload(logic);
660 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
662 lock_guard<mutex> lock(_data_mutex);
666 qDebug() << "Unexpected analog packet";
667 return; // This analog packet was not expected.
670 if (!_cur_analog_snapshot)
672 set_capture_state(Running);
674 // Create a new data snapshot
675 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
676 new data::AnalogSnapshot(analog));
677 _analog_data->push_snapshot(_cur_analog_snapshot);
681 // Append to the existing data snapshot
682 _cur_analog_snapshot->append_payload(analog);
688 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
689 const struct sr_datafeed_packet *packet)
694 switch (packet->type) {
700 assert(packet->payload);
702 *(const sr_datafeed_meta*)packet->payload);
706 assert(packet->payload);
707 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
711 assert(packet->payload);
712 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
718 lock_guard<mutex> lock(_data_mutex);
719 _cur_logic_snapshot.reset();
720 _cur_analog_snapshot.reset();
728 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
729 const struct sr_datafeed_packet *packet, void *cb_data)
733 _session->data_feed_in(sdi, packet);