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 // Auto select the initial probes
237 for(const GSList *i = dec->probes; i; i = i->next)
239 const srd_probe *const probe = (const srd_probe*)i->data;
240 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
242 shared_ptr<view::LogicSignal> l =
243 dynamic_pointer_cast<view::LogicSignal>(s);
244 if (l && QString::fromUtf8(probe->name).
246 l->get_name().toLower()))
251 assert(decoder_stack);
252 assert(!decoder_stack->stack().empty());
253 assert(decoder_stack->stack().front());
254 decoder_stack->stack().front()->set_probes(probes);
256 // Create the decode signal
257 shared_ptr<view::DecodeTrace> d(
258 new view::DecodeTrace(*this, decoder_stack,
259 _decode_traces.size()));
260 _decode_traces.push_back(d);
262 catch(std::runtime_error e)
269 // Do an initial decode
270 decoder_stack->begin_decode();
275 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
277 lock_guard<mutex> lock(_signals_mutex);
278 return _decode_traces;
281 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
283 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
284 _decode_traces.begin();
285 i != _decode_traces.end();
287 if ((*i).get() == signal)
289 _decode_traces.erase(i);
296 void SigSession::set_capture_state(capture_state state)
298 lock_guard<mutex> lock(_sampling_mutex);
299 const bool changed = _capture_state != state;
300 _capture_state = state;
302 capture_state_changed(state);
306 * Attempts to autodetect the format. Failing that
307 * @param filename The filename of the input file.
308 * @return A pointer to the 'struct sr_input_format' that should be used,
309 * or NULL if no input format was selected or auto-detected.
311 sr_input_format* SigSession::determine_input_file_format(
312 const string &filename)
316 /* If there are no input formats, return NULL right away. */
317 sr_input_format *const *const inputs = sr_input_list();
319 g_critical("No supported input formats available.");
323 /* Otherwise, try to find an input module that can handle this file. */
324 for (i = 0; inputs[i]; i++) {
325 if (inputs[i]->format_match(filename.c_str()))
329 /* Return NULL if no input module wanted to touch this. */
331 g_critical("Error: no matching input module found.");
338 sr_input* SigSession::load_input_file_format(const string &filename,
339 function<void (const QString)> error_handler,
340 sr_input_format *format)
345 if (!format && !(format =
346 determine_input_file_format(filename.c_str()))) {
347 /* The exact cause was already logged. */
351 if (stat(filename.c_str(), &st) == -1) {
352 error_handler(tr("Failed to load file"));
356 /* Initialize the input module. */
357 if (!(in = new sr_input)) {
358 qDebug("Failed to allocate input module.\n");
364 if (in->format->init &&
365 in->format->init(in, filename.c_str()) != SR_OK) {
366 qDebug("Input format init failed.\n");
372 if (sr_session_dev_add(in->sdi) != SR_OK) {
373 qDebug("Failed to use device.\n");
374 sr_session_destroy();
381 void SigSession::update_signals(const sr_dev_inst *const sdi)
383 assert(_capture_state == Stopped);
385 unsigned int logic_probe_count = 0;
387 // Clear the decode traces
388 _decode_traces.clear();
390 // Detect what data types we will receive
392 for (const GSList *l = sdi->probes; l; l = l->next) {
393 const sr_probe *const probe = (const sr_probe *)l->data;
397 switch(probe->type) {
405 // Create data containers for the logic data snapshots
407 lock_guard<mutex> data_lock(_data_mutex);
410 if (logic_probe_count != 0) {
411 _logic_data.reset(new data::Logic(
417 // Make the Signals list
419 lock_guard<mutex> lock(_signals_mutex);
426 for (const GSList *l = sdi->probes; l; l = l->next) {
427 shared_ptr<view::Signal> signal;
428 sr_probe *const probe = (sr_probe *)l->data;
431 switch(probe->type) {
433 signal = shared_ptr<view::Signal>(
434 new view::LogicSignal(*this, probe,
438 case SR_PROBE_ANALOG:
440 shared_ptr<data::Analog> data(
442 signal = shared_ptr<view::Signal>(
443 new view::AnalogSignal(*this, probe,
454 _signals.push_back(signal);
462 bool SigSession::is_trigger_enabled() const
465 for (const GSList *l = _sdi->probes; l; l = l->next) {
466 const sr_probe *const p = (const sr_probe *)l->data;
468 if (p->trigger && p->trigger[0] != '\0')
475 shared_ptr<view::Signal> SigSession::signal_from_probe(
476 const sr_probe *probe) const
478 lock_guard<mutex> lock(_signals_mutex);
479 BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) {
481 if (sig->probe() == probe)
484 return shared_ptr<view::Signal>();
487 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
490 uint64_t sample_rate = 0;
492 // Read out the sample rate
495 const int ret = sr_config_get(sdi->driver, sdi, NULL,
496 SR_CONF_SAMPLERATE, &gvar);
498 qDebug("Failed to get samplerate\n");
502 sample_rate = g_variant_get_uint64(gvar);
503 g_variant_unref(gvar);
506 // Set the sample rate of all data
507 const set< shared_ptr<data::SignalData> > data_set = get_data();
508 BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) {
510 data->set_samplerate(sample_rate);
514 void SigSession::load_session_thread_proc(
515 function<void (const QString)> error_handler)
519 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
521 set_capture_state(Running);
525 sr_session_destroy();
526 set_capture_state(Stopped);
528 // Confirm that SR_DF_END was received
529 assert(!_cur_logic_snapshot);
530 assert(_cur_analog_snapshots.empty());
533 void SigSession::load_input_thread_proc(const string name,
534 sr_input *in, function<void (const QString)> error_handler)
541 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
543 set_capture_state(Running);
545 in->format->loadfile(in, name.c_str());
547 sr_session_destroy();
548 set_capture_state(Stopped);
550 // Confirm that SR_DF_END was received
551 assert(!_cur_logic_snapshot);
552 assert(_cur_analog_snapshots.empty());
557 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
558 function<void (const QString)> error_handler)
561 assert(error_handler);
564 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
566 if (sr_session_dev_add(sdi) != SR_OK) {
567 error_handler(tr("Failed to use device."));
568 sr_session_destroy();
572 if (sr_session_start() != SR_OK) {
573 error_handler(tr("Failed to start session."));
577 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
580 sr_session_destroy();
582 set_capture_state(Stopped);
584 // Confirm that SR_DF_END was received
585 if (_cur_logic_snapshot)
587 qDebug("SR_DF_END was not received.");
592 void SigSession::feed_in_header(const sr_dev_inst *sdi)
594 read_sample_rate(sdi);
597 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
598 const sr_datafeed_meta &meta)
602 for (const GSList *l = meta.config; l; l = l->next) {
603 const sr_config *const src = (const sr_config*)l->data;
605 case SR_CONF_SAMPLERATE:
606 /// @todo handle samplerate changes
607 /// samplerate = (uint64_t *)src->value;
610 // Unknown metadata is not an error.
618 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
620 lock_guard<mutex> lock(_data_mutex);
624 qDebug() << "Unexpected logic packet";
628 if (!_cur_logic_snapshot)
630 // This could be the first packet after a trigger
631 set_capture_state(Running);
633 // Create a new data snapshot
634 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
635 new data::LogicSnapshot(logic));
636 _logic_data->push_snapshot(_cur_logic_snapshot);
640 // Append to the existing data snapshot
641 _cur_logic_snapshot->append_payload(logic);
647 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
649 lock_guard<mutex> lock(_data_mutex);
651 const unsigned int probe_count = g_slist_length(analog.probes);
652 const size_t sample_count = analog.num_samples / probe_count;
653 const float *data = analog.data;
654 bool sweep_beginning = false;
656 for (GSList *p = analog.probes; p; p = p->next)
658 shared_ptr<data::AnalogSnapshot> snapshot;
660 sr_probe *const probe = (sr_probe*)p->data;
663 // Try to get the snapshot of the probe
664 const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >::
665 iterator iter = _cur_analog_snapshots.find(probe);
666 if (iter != _cur_analog_snapshots.end())
667 snapshot = (*iter).second;
670 // If no snapshot was found, this means we havn't
671 // created one yet. i.e. this is the first packet
672 // in the sweep containing this snapshot.
673 sweep_beginning = true;
675 // Create a snapshot, keep it in the maps of probes
676 snapshot = shared_ptr<data::AnalogSnapshot>(
677 new data::AnalogSnapshot());
678 _cur_analog_snapshots[probe] = snapshot;
680 // Find the annalog data associated with the probe
681 shared_ptr<view::AnalogSignal> sig =
682 dynamic_pointer_cast<view::AnalogSignal>(
683 signal_from_probe(probe));
686 shared_ptr<data::Analog> data(sig->analog_data());
689 // Push the snapshot into the analog data.
690 data->push_snapshot(snapshot);
695 // Append the samples in the snapshot
696 snapshot->append_interleaved_samples(data++, sample_count,
700 if (sweep_beginning) {
701 // This could be the first packet after a trigger
702 set_capture_state(Running);
708 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
709 const struct sr_datafeed_packet *packet)
714 switch (packet->type) {
720 assert(packet->payload);
722 *(const sr_datafeed_meta*)packet->payload);
726 assert(packet->payload);
727 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
731 assert(packet->payload);
732 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
738 lock_guard<mutex> lock(_data_mutex);
739 _cur_logic_snapshot.reset();
740 _cur_analog_snapshots.clear();
748 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
749 const struct sr_datafeed_packet *packet, void *cb_data)
753 _session->data_feed_in(sdi, packet);