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(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, error_handler);
193 void SigSession::stop_capture()
195 if (get_capture_state() == Stopped)
200 // Check that sampling stopped
201 _sampling_thread.join();
204 set< shared_ptr<data::SignalData> > SigSession::get_data() const
206 lock_guard<mutex> lock(_signals_mutex);
207 set< shared_ptr<data::SignalData> > data;
208 BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) {
210 data.insert(sig->data());
216 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
218 lock_guard<mutex> lock(_signals_mutex);
223 bool SigSession::add_decoder(srd_decoder *const dec)
225 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
226 shared_ptr<data::DecoderStack> decoder_stack;
230 lock_guard<mutex> lock(_signals_mutex);
232 // Create the decoder
233 decoder_stack = shared_ptr<data::DecoderStack>(
234 new data::DecoderStack(dec));
236 // Make a list of all the probes
237 std::vector<const srd_probe*> all_probes;
238 for(const GSList *i = dec->probes; i; i = i->next)
239 all_probes.push_back((const srd_probe*)i->data);
240 for(const GSList *i = dec->opt_probes; i; i = i->next)
241 all_probes.push_back((const srd_probe*)i->data);
243 // Auto select the initial probes
244 BOOST_FOREACH(const srd_probe *probe, all_probes)
245 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
247 shared_ptr<view::LogicSignal> l =
248 dynamic_pointer_cast<view::LogicSignal>(s);
249 if (l && QString::fromUtf8(probe->name).
251 l->get_name().toLower()))
255 assert(decoder_stack);
256 assert(!decoder_stack->stack().empty());
257 assert(decoder_stack->stack().front());
258 decoder_stack->stack().front()->set_probes(probes);
260 // Create the decode signal
261 shared_ptr<view::DecodeTrace> d(
262 new view::DecodeTrace(*this, decoder_stack,
263 _decode_traces.size()));
264 _decode_traces.push_back(d);
266 catch(std::runtime_error e)
273 // Do an initial decode
274 decoder_stack->begin_decode();
279 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
281 lock_guard<mutex> lock(_signals_mutex);
282 return _decode_traces;
285 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
287 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
288 _decode_traces.begin();
289 i != _decode_traces.end();
291 if ((*i).get() == signal)
293 _decode_traces.erase(i);
300 void SigSession::set_capture_state(capture_state state)
302 lock_guard<mutex> lock(_sampling_mutex);
303 const bool changed = _capture_state != state;
304 _capture_state = state;
306 capture_state_changed(state);
310 * Attempts to autodetect the format. Failing that
311 * @param filename The filename of the input file.
312 * @return A pointer to the 'struct sr_input_format' that should be used,
313 * or NULL if no input format was selected or auto-detected.
315 sr_input_format* SigSession::determine_input_file_format(
316 const string &filename)
320 /* If there are no input formats, return NULL right away. */
321 sr_input_format *const *const inputs = sr_input_list();
323 g_critical("No supported input formats available.");
327 /* Otherwise, try to find an input module that can handle this file. */
328 for (i = 0; inputs[i]; i++) {
329 if (inputs[i]->format_match(filename.c_str()))
333 /* Return NULL if no input module wanted to touch this. */
335 g_critical("Error: no matching input module found.");
342 sr_input* SigSession::load_input_file_format(const string &filename,
343 function<void (const QString)> error_handler,
344 sr_input_format *format)
349 if (!format && !(format =
350 determine_input_file_format(filename.c_str()))) {
351 /* The exact cause was already logged. */
355 if (stat(filename.c_str(), &st) == -1) {
356 error_handler(tr("Failed to load file"));
360 /* Initialize the input module. */
361 if (!(in = new sr_input)) {
362 qDebug("Failed to allocate input module.\n");
368 if (in->format->init &&
369 in->format->init(in, filename.c_str()) != SR_OK) {
370 qDebug("Input format init failed.\n");
376 if (sr_session_dev_add(in->sdi) != SR_OK) {
377 qDebug("Failed to use device.\n");
378 sr_session_destroy();
385 void SigSession::update_signals(const sr_dev_inst *const sdi)
387 assert(_capture_state == Stopped);
389 unsigned int logic_probe_count = 0;
391 // Clear the decode traces
392 _decode_traces.clear();
394 // Detect what data types we will receive
396 for (const GSList *l = sdi->probes; l; l = l->next) {
397 const sr_probe *const probe = (const sr_probe *)l->data;
401 switch(probe->type) {
409 // Create data containers for the logic data snapshots
411 lock_guard<mutex> data_lock(_data_mutex);
414 if (logic_probe_count != 0) {
415 _logic_data.reset(new data::Logic(
421 // Make the Signals list
423 lock_guard<mutex> lock(_signals_mutex);
430 for (const GSList *l = sdi->probes; l; l = l->next) {
431 shared_ptr<view::Signal> signal;
432 sr_probe *const probe = (sr_probe *)l->data;
435 switch(probe->type) {
437 signal = shared_ptr<view::Signal>(
438 new view::LogicSignal(*this, probe,
442 case SR_PROBE_ANALOG:
444 shared_ptr<data::Analog> data(
446 signal = shared_ptr<view::Signal>(
447 new view::AnalogSignal(*this, probe,
458 _signals.push_back(signal);
466 bool SigSession::is_trigger_enabled() const
469 for (const GSList *l = _sdi->probes; l; l = l->next) {
470 const sr_probe *const p = (const sr_probe *)l->data;
472 if (p->trigger && p->trigger[0] != '\0')
479 shared_ptr<view::Signal> SigSession::signal_from_probe(
480 const sr_probe *probe) const
482 lock_guard<mutex> lock(_signals_mutex);
483 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
485 if (sig->probe() == probe)
488 return shared_ptr<view::Signal>();
491 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
494 uint64_t sample_rate = 0;
496 // Read out the sample rate
499 const int ret = sr_config_get(sdi->driver, sdi, NULL,
500 SR_CONF_SAMPLERATE, &gvar);
502 qDebug("Failed to get samplerate\n");
506 sample_rate = g_variant_get_uint64(gvar);
507 g_variant_unref(gvar);
510 // Set the sample rate of all data
511 const set< shared_ptr<data::SignalData> > data_set = get_data();
512 BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) {
514 data->set_samplerate(sample_rate);
518 void SigSession::load_session_thread_proc(
519 function<void (const QString)> error_handler)
523 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
525 set_capture_state(Running);
529 sr_session_destroy();
530 set_capture_state(Stopped);
532 // Confirm that SR_DF_END was received
533 assert(!_cur_logic_snapshot);
534 assert(_cur_analog_snapshots.empty());
537 void SigSession::load_input_thread_proc(const string name,
538 sr_input *in, function<void (const QString)> error_handler)
545 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
547 set_capture_state(Running);
549 in->format->loadfile(in, name.c_str());
551 sr_session_destroy();
552 set_capture_state(Stopped);
554 // Confirm that SR_DF_END was received
555 assert(!_cur_logic_snapshot);
556 assert(_cur_analog_snapshots.empty());
561 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
562 function<void (const QString)> error_handler)
565 assert(error_handler);
568 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
570 if (sr_session_dev_add(sdi) != SR_OK) {
571 error_handler(tr("Failed to use device."));
572 sr_session_destroy();
576 if (sr_session_start() != SR_OK) {
577 error_handler(tr("Failed to start session."));
581 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
584 sr_session_destroy();
586 set_capture_state(Stopped);
588 // Confirm that SR_DF_END was received
589 if (_cur_logic_snapshot)
591 qDebug("SR_DF_END was not received.");
596 void SigSession::feed_in_header(const sr_dev_inst *sdi)
598 read_sample_rate(sdi);
601 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
602 const sr_datafeed_meta &meta)
606 for (const GSList *l = meta.config; l; l = l->next) {
607 const sr_config *const src = (const sr_config*)l->data;
609 case SR_CONF_SAMPLERATE:
610 /// @todo handle samplerate changes
611 /// samplerate = (uint64_t *)src->value;
614 // Unknown metadata is not an error.
622 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
624 lock_guard<mutex> lock(_data_mutex);
628 qDebug() << "Unexpected logic packet";
632 if (!_cur_logic_snapshot)
634 // This could be the first packet after a trigger
635 set_capture_state(Running);
637 // Create a new data snapshot
638 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
639 new data::LogicSnapshot(logic));
640 _logic_data->push_snapshot(_cur_logic_snapshot);
644 // Append to the existing data snapshot
645 _cur_logic_snapshot->append_payload(logic);
651 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
653 lock_guard<mutex> lock(_data_mutex);
655 const unsigned int probe_count = g_slist_length(analog.probes);
656 const size_t sample_count = analog.num_samples / probe_count;
657 const float *data = analog.data;
658 bool sweep_beginning = false;
660 for (GSList *p = analog.probes; p; p = p->next)
662 shared_ptr<data::AnalogSnapshot> snapshot;
664 sr_probe *const probe = (sr_probe*)p->data;
667 // Try to get the snapshot of the probe
668 const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >::
669 iterator iter = _cur_analog_snapshots.find(probe);
670 if (iter != _cur_analog_snapshots.end())
671 snapshot = (*iter).second;
674 // If no snapshot was found, this means we havn't
675 // created one yet. i.e. this is the first packet
676 // in the sweep containing this snapshot.
677 sweep_beginning = true;
679 // Create a snapshot, keep it in the maps of probes
680 snapshot = shared_ptr<data::AnalogSnapshot>(
681 new data::AnalogSnapshot());
682 _cur_analog_snapshots[probe] = snapshot;
684 // Find the annalog data associated with the probe
685 shared_ptr<view::AnalogSignal> sig =
686 dynamic_pointer_cast<view::AnalogSignal>(
687 signal_from_probe(probe));
690 shared_ptr<data::Analog> data(sig->analog_data());
693 // Push the snapshot into the analog data.
694 data->push_snapshot(snapshot);
699 // Append the samples in the snapshot
700 snapshot->append_interleaved_samples(data++, sample_count,
704 if (sweep_beginning) {
705 // This could be the first packet after a trigger
706 set_capture_state(Running);
712 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
713 const struct sr_datafeed_packet *packet)
718 switch (packet->type) {
724 assert(packet->payload);
726 *(const sr_datafeed_meta*)packet->payload);
730 assert(packet->payload);
731 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
735 assert(packet->payload);
736 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
742 lock_guard<mutex> lock(_data_mutex);
743 _cur_logic_snapshot.reset();
744 _cur_analog_snapshots.clear();
752 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
753 const struct sr_datafeed_packet *packet, void *cb_data)
757 _session->data_feed_in(sdi, packet);