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"
30 #include "data/analog.h"
31 #include "data/analogsnapshot.h"
32 #include "data/decoderstack.h"
33 #include "data/logic.h"
34 #include "data/logicsnapshot.h"
35 #include "data/decode/decoder.h"
37 #include "view/analogsignal.h"
38 #include "view/decodetrace.h"
39 #include "view/logicsignal.h"
45 #include <boost/foreach.hpp>
51 using boost::dynamic_pointer_cast;
52 using boost::function;
53 using boost::lock_guard;
55 using boost::shared_ptr;
63 // TODO: This should not be necessary
64 SigSession* SigSession::_session = NULL;
66 SigSession::SigSession(DeviceManager &device_manager) :
67 _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(_dev_inst);
83 // TODO: This should not be necessary
87 shared_ptr<DevInst> SigSession::get_device() const
92 void SigSession::set_device(shared_ptr<DevInst> dev_inst)
94 // Ensure we are not capturing before setting the device
98 _device_manager.release_device(_dev_inst);
100 _device_manager.use_device(dev_inst, this);
101 _dev_inst = dev_inst;
102 update_signals(dev_inst);
105 void SigSession::release_device(shared_ptr<DevInst> dev_inst)
109 assert(_capture_state == Stopped);
110 _dev_inst = shared_ptr<DevInst>();
113 void SigSession::load_file(const string &name,
114 function<void (const QString)> error_handler)
118 if (sr_session_load(name.c_str()) == SR_OK) {
119 GSList *devlist = NULL;
120 sr_session_dev_list(&devlist);
122 if (!devlist || !devlist->data ||
123 sr_session_start() != SR_OK) {
124 error_handler(tr("Failed to start session."));
128 shared_ptr<DevInst> dev_inst(
129 new DevInst((sr_dev_inst*)devlist->data));
130 g_slist_free(devlist);
132 _decode_traces.clear();
133 update_signals(dev_inst);
134 read_sample_rate(dev_inst->dev_inst());
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(shared_ptr<DevInst>(new DevInst(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(function<void (const QString)> error_handler)
167 // Check that a device instance has been selected.
169 qDebug() << "No device selected";
173 assert(_dev_inst->dev_inst());
175 // Check that at least one probe is enabled
177 for (l = _dev_inst->dev_inst()->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, _dev_inst,
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 // Make a list of all the probes
239 std::vector<const srd_probe*> all_probes;
240 for(const GSList *i = dec->probes; i; i = i->next)
241 all_probes.push_back((const srd_probe*)i->data);
242 for(const GSList *i = dec->opt_probes; i; i = i->next)
243 all_probes.push_back((const srd_probe*)i->data);
245 // Auto select the initial probes
246 BOOST_FOREACH(const srd_probe *probe, all_probes)
247 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
249 shared_ptr<view::LogicSignal> l =
250 dynamic_pointer_cast<view::LogicSignal>(s);
251 if (l && QString::fromUtf8(probe->name).
253 l->get_name().toLower()))
257 assert(decoder_stack);
258 assert(!decoder_stack->stack().empty());
259 assert(decoder_stack->stack().front());
260 decoder_stack->stack().front()->set_probes(probes);
262 // Create the decode signal
263 shared_ptr<view::DecodeTrace> d(
264 new view::DecodeTrace(*this, decoder_stack,
265 _decode_traces.size()));
266 _decode_traces.push_back(d);
268 catch(std::runtime_error e)
275 // Do an initial decode
276 decoder_stack->begin_decode();
281 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
283 lock_guard<mutex> lock(_signals_mutex);
284 return _decode_traces;
287 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
289 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
290 _decode_traces.begin();
291 i != _decode_traces.end();
293 if ((*i).get() == signal)
295 _decode_traces.erase(i);
302 void SigSession::set_capture_state(capture_state state)
304 lock_guard<mutex> lock(_sampling_mutex);
305 const bool changed = _capture_state != state;
306 _capture_state = state;
308 capture_state_changed(state);
312 * Attempts to autodetect the format. Failing that
313 * @param filename The filename of the input file.
314 * @return A pointer to the 'struct sr_input_format' that should be used,
315 * or NULL if no input format was selected or auto-detected.
317 sr_input_format* SigSession::determine_input_file_format(
318 const string &filename)
322 /* If there are no input formats, return NULL right away. */
323 sr_input_format *const *const inputs = sr_input_list();
325 g_critical("No supported input formats available.");
329 /* Otherwise, try to find an input module that can handle this file. */
330 for (i = 0; inputs[i]; i++) {
331 if (inputs[i]->format_match(filename.c_str()))
335 /* Return NULL if no input module wanted to touch this. */
337 g_critical("Error: no matching input module found.");
344 sr_input* SigSession::load_input_file_format(const string &filename,
345 function<void (const QString)> error_handler,
346 sr_input_format *format)
351 if (!format && !(format =
352 determine_input_file_format(filename.c_str()))) {
353 /* The exact cause was already logged. */
357 if (stat(filename.c_str(), &st) == -1) {
358 error_handler(tr("Failed to load file"));
362 /* Initialize the input module. */
363 if (!(in = new sr_input)) {
364 qDebug("Failed to allocate input module.\n");
370 if (in->format->init &&
371 in->format->init(in, filename.c_str()) != SR_OK) {
372 qDebug("Input format init failed.\n");
378 if (sr_session_dev_add(in->sdi) != SR_OK) {
379 qDebug("Failed to use device.\n");
380 sr_session_destroy();
387 void SigSession::update_signals(shared_ptr<DevInst> dev_inst)
390 assert(_capture_state == Stopped);
392 unsigned int logic_probe_count = 0;
394 // Clear the decode traces
395 _decode_traces.clear();
397 // Detect what data types we will receive
399 assert(dev_inst->dev_inst());
400 for (const GSList *l = dev_inst->dev_inst()->probes;
402 const sr_probe *const probe = (const sr_probe *)l->data;
406 switch(probe->type) {
414 // Create data containers for the logic data snapshots
416 lock_guard<mutex> data_lock(_data_mutex);
419 if (logic_probe_count != 0) {
420 _logic_data.reset(new data::Logic(
426 // Make the Signals list
428 lock_guard<mutex> lock(_signals_mutex);
435 assert(dev_inst->dev_inst());
436 for (const GSList *l = dev_inst->dev_inst()->probes;
438 shared_ptr<view::Signal> signal;
439 sr_probe *const probe = (sr_probe *)l->data;
442 switch(probe->type) {
444 signal = shared_ptr<view::Signal>(
445 new view::LogicSignal(*this, probe,
449 case SR_PROBE_ANALOG:
451 shared_ptr<data::Analog> data(
453 signal = shared_ptr<view::Signal>(
454 new view::AnalogSignal(*this, probe,
465 _signals.push_back(signal);
473 bool SigSession::is_trigger_enabled() const
476 assert(_dev_inst->dev_inst());
477 for (const GSList *l = _dev_inst->dev_inst()->probes; l; l = l->next) {
478 const sr_probe *const p = (const sr_probe *)l->data;
480 if (p->trigger && p->trigger[0] != '\0')
487 shared_ptr<view::Signal> SigSession::signal_from_probe(
488 const sr_probe *probe) const
490 lock_guard<mutex> lock(_signals_mutex);
491 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
493 if (sig->probe() == probe)
496 return shared_ptr<view::Signal>();
499 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
502 uint64_t sample_rate = 0;
504 // Read out the sample rate
507 const int ret = sr_config_get(sdi->driver, sdi, NULL,
508 SR_CONF_SAMPLERATE, &gvar);
510 qDebug("Failed to get samplerate\n");
514 sample_rate = g_variant_get_uint64(gvar);
515 g_variant_unref(gvar);
518 // Set the sample rate of all data
519 const set< shared_ptr<data::SignalData> > data_set = get_data();
520 BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) {
522 data->set_samplerate(sample_rate);
526 void SigSession::load_session_thread_proc(
527 function<void (const QString)> error_handler)
531 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
533 set_capture_state(Running);
537 sr_session_destroy();
538 set_capture_state(Stopped);
540 // Confirm that SR_DF_END was received
541 assert(!_cur_logic_snapshot);
542 assert(_cur_analog_snapshots.empty());
545 void SigSession::load_input_thread_proc(const string name,
546 sr_input *in, function<void (const QString)> error_handler)
553 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
555 set_capture_state(Running);
557 in->format->loadfile(in, name.c_str());
559 sr_session_destroy();
560 set_capture_state(Stopped);
562 // Confirm that SR_DF_END was received
563 assert(!_cur_logic_snapshot);
564 assert(_cur_analog_snapshots.empty());
569 void SigSession::sample_thread_proc(shared_ptr<DevInst> dev_inst,
570 function<void (const QString)> error_handler)
573 assert(dev_inst->dev_inst());
574 assert(error_handler);
577 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
579 if (sr_session_dev_add(dev_inst->dev_inst()) != SR_OK) {
580 error_handler(tr("Failed to use device."));
581 sr_session_destroy();
585 if (sr_session_start() != SR_OK) {
586 error_handler(tr("Failed to start session."));
590 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
593 sr_session_destroy();
595 set_capture_state(Stopped);
597 // Confirm that SR_DF_END was received
598 if (_cur_logic_snapshot)
600 qDebug("SR_DF_END was not received.");
605 void SigSession::feed_in_header(const sr_dev_inst *sdi)
607 read_sample_rate(sdi);
610 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
611 const sr_datafeed_meta &meta)
615 for (const GSList *l = meta.config; l; l = l->next) {
616 const sr_config *const src = (const sr_config*)l->data;
618 case SR_CONF_SAMPLERATE:
619 /// @todo handle samplerate changes
620 /// samplerate = (uint64_t *)src->value;
623 // Unknown metadata is not an error.
631 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
633 lock_guard<mutex> lock(_data_mutex);
637 qDebug() << "Unexpected logic packet";
641 if (!_cur_logic_snapshot)
643 // This could be the first packet after a trigger
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);
664 const unsigned int probe_count = g_slist_length(analog.probes);
665 const size_t sample_count = analog.num_samples / probe_count;
666 const float *data = analog.data;
667 bool sweep_beginning = false;
669 for (GSList *p = analog.probes; p; p = p->next)
671 shared_ptr<data::AnalogSnapshot> snapshot;
673 sr_probe *const probe = (sr_probe*)p->data;
676 // Try to get the snapshot of the probe
677 const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >::
678 iterator iter = _cur_analog_snapshots.find(probe);
679 if (iter != _cur_analog_snapshots.end())
680 snapshot = (*iter).second;
683 // If no snapshot was found, this means we havn't
684 // created one yet. i.e. this is the first packet
685 // in the sweep containing this snapshot.
686 sweep_beginning = true;
688 // Create a snapshot, keep it in the maps of probes
689 snapshot = shared_ptr<data::AnalogSnapshot>(
690 new data::AnalogSnapshot());
691 _cur_analog_snapshots[probe] = snapshot;
693 // Find the annalog data associated with the probe
694 shared_ptr<view::AnalogSignal> sig =
695 dynamic_pointer_cast<view::AnalogSignal>(
696 signal_from_probe(probe));
699 shared_ptr<data::Analog> data(sig->analog_data());
702 // Push the snapshot into the analog data.
703 data->push_snapshot(snapshot);
708 // Append the samples in the snapshot
709 snapshot->append_interleaved_samples(data++, sample_count,
713 if (sweep_beginning) {
714 // This could be the first packet after a trigger
715 set_capture_state(Running);
721 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
722 const struct sr_datafeed_packet *packet)
727 switch (packet->type) {
733 assert(packet->payload);
735 *(const sr_datafeed_meta*)packet->payload);
739 assert(packet->payload);
740 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
744 assert(packet->payload);
745 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
751 lock_guard<mutex> lock(_data_mutex);
752 _cur_logic_snapshot.reset();
753 _cur_analog_snapshots.clear();
761 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
762 const struct sr_datafeed_packet *packet, void *cb_data)
766 _session->data_feed_in(sdi, packet);