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;
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);
212 bool SigSession::add_decoder(srd_decoder *const dec)
214 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
215 shared_ptr<data::DecoderStack> decoder_stack;
219 lock_guard<mutex> lock(_signals_mutex);
221 // Create the decoder
222 decoder_stack = shared_ptr<data::DecoderStack>(
223 new data::DecoderStack(dec));
225 // Auto select the initial probes
226 for(const GSList *i = dec->probes; i; i = i->next)
228 const srd_probe *const probe = (const srd_probe*)i->data;
229 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
231 shared_ptr<view::LogicSignal> l =
232 dynamic_pointer_cast<view::LogicSignal>(s);
233 if (l && QString::fromUtf8(probe->name).
235 l->get_name().toLower()))
240 assert(decoder_stack);
241 assert(!decoder_stack->stack().empty());
242 assert(decoder_stack->stack().front());
243 decoder_stack->stack().front()->set_probes(probes);
245 // Create the decode signal
246 shared_ptr<view::DecodeTrace> d(
247 new view::DecodeTrace(*this, decoder_stack,
248 _decode_traces.size()));
249 _decode_traces.push_back(d);
251 catch(std::runtime_error e)
258 // Do an initial decode
259 decoder_stack->begin_decode();
264 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
266 lock_guard<mutex> lock(_signals_mutex);
267 return _decode_traces;
270 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
272 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
273 _decode_traces.begin();
274 i != _decode_traces.end();
276 if ((*i).get() == signal)
278 _decode_traces.erase(i);
285 void SigSession::set_capture_state(capture_state state)
287 lock_guard<mutex> lock(_sampling_mutex);
288 const bool changed = _capture_state != state;
289 _capture_state = state;
291 capture_state_changed(state);
295 * Attempts to autodetect the format. Failing that
296 * @param filename The filename of the input file.
297 * @return A pointer to the 'struct sr_input_format' that should be used,
298 * or NULL if no input format was selected or auto-detected.
300 sr_input_format* SigSession::determine_input_file_format(
301 const string &filename)
305 /* If there are no input formats, return NULL right away. */
306 sr_input_format *const *const inputs = sr_input_list();
308 g_critical("No supported input formats available.");
312 /* Otherwise, try to find an input module that can handle this file. */
313 for (i = 0; inputs[i]; i++) {
314 if (inputs[i]->format_match(filename.c_str()))
318 /* Return NULL if no input module wanted to touch this. */
320 g_critical("Error: no matching input module found.");
327 sr_input* SigSession::load_input_file_format(const string &filename,
328 function<void (const QString)> error_handler,
329 sr_input_format *format)
334 if (!format && !(format =
335 determine_input_file_format(filename.c_str()))) {
336 /* The exact cause was already logged. */
340 if (stat(filename.c_str(), &st) == -1) {
341 error_handler(tr("Failed to load file"));
345 /* Initialize the input module. */
346 if (!(in = new sr_input)) {
347 qDebug("Failed to allocate input module.\n");
353 if (in->format->init &&
354 in->format->init(in, filename.c_str()) != SR_OK) {
355 qDebug("Input format init failed.\n");
361 if (sr_session_dev_add(in->sdi) != SR_OK) {
362 qDebug("Failed to use device.\n");
363 sr_session_destroy();
370 void SigSession::update_signals(const sr_dev_inst *const sdi)
372 assert(_capture_state == Stopped);
374 shared_ptr<view::Signal> signal;
375 unsigned int logic_probe_count = 0;
376 unsigned int analog_probe_count = 0;
378 // Clear the decode traces
379 _decode_traces.clear();
381 // Detect what data types we will receive
383 for (const GSList *l = sdi->probes; l; l = l->next) {
384 const sr_probe *const probe = (const sr_probe *)l->data;
388 switch(probe->type) {
393 case SR_PROBE_ANALOG:
394 analog_probe_count++;
400 // Create data containers for the data snapshots
402 lock_guard<mutex> data_lock(_data_mutex);
405 if (logic_probe_count != 0) {
406 _logic_data.reset(new data::Logic(
411 _analog_data.reset();
412 if (analog_probe_count != 0) {
413 _analog_data.reset(new data::Analog());
414 assert(_analog_data);
418 // Make the Signals list
420 lock_guard<mutex> lock(_signals_mutex);
425 for (const GSList *l = sdi->probes; l; l = l->next) {
426 sr_probe *const probe = (sr_probe *)l->data;
429 switch(probe->type) {
431 signal = shared_ptr<view::Signal>(
432 new view::LogicSignal(*this, probe,
436 case SR_PROBE_ANALOG:
437 signal = shared_ptr<view::Signal>(
438 new view::AnalogSignal(*this, probe,
443 _signals.push_back(signal);
451 bool SigSession::is_trigger_enabled() const
454 for (const GSList *l = _sdi->probes; l; l = l->next) {
455 const sr_probe *const p = (const sr_probe *)l->data;
457 if (p->trigger && p->trigger[0] != '\0')
464 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
467 uint64_t sample_rate = 0;
469 // Read out the sample rate
472 const int ret = sr_config_get(sdi->driver, sdi, NULL,
473 SR_CONF_SAMPLERATE, &gvar);
475 qDebug("Failed to get samplerate\n");
479 sample_rate = g_variant_get_uint64(gvar);
480 g_variant_unref(gvar);
484 _analog_data->set_samplerate(sample_rate);
486 _logic_data->set_samplerate(sample_rate);
489 void SigSession::load_session_thread_proc(
490 function<void (const QString)> error_handler)
494 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
496 set_capture_state(Running);
500 sr_session_destroy();
501 set_capture_state(Stopped);
503 // Confirm that SR_DF_END was received
504 assert(!_cur_logic_snapshot);
505 assert(!_cur_analog_snapshot);
508 void SigSession::load_input_thread_proc(const string name,
509 sr_input *in, function<void (const QString)> error_handler)
516 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
518 set_capture_state(Running);
520 in->format->loadfile(in, name.c_str());
522 sr_session_destroy();
523 set_capture_state(Stopped);
525 // Confirm that SR_DF_END was received
526 assert(!_cur_logic_snapshot);
527 assert(!_cur_analog_snapshot);
532 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
533 uint64_t record_length,
534 function<void (const QString)> error_handler)
537 assert(error_handler);
540 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
542 if (sr_session_dev_add(sdi) != SR_OK) {
543 error_handler(tr("Failed to use device."));
544 sr_session_destroy();
548 // Set the sample limit
549 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
550 g_variant_new_uint64(record_length)) != SR_OK) {
551 error_handler(tr("Failed to configure "
552 "time-based sample limit."));
553 sr_session_destroy();
557 if (sr_session_start() != SR_OK) {
558 error_handler(tr("Failed to start session."));
562 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
565 sr_session_destroy();
567 set_capture_state(Stopped);
569 // Confirm that SR_DF_END was received
570 assert(!_cur_logic_snapshot);
571 assert(!_cur_analog_snapshot);
574 void SigSession::feed_in_header(const sr_dev_inst *sdi)
576 read_sample_rate(sdi);
579 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
580 const sr_datafeed_meta &meta)
584 for (const GSList *l = meta.config; l; l = l->next) {
585 const sr_config *const src = (const sr_config*)l->data;
587 case SR_CONF_SAMPLERATE:
588 /// @todo handle samplerate changes
589 /// samplerate = (uint64_t *)src->value;
592 // Unknown metadata is not an error.
600 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
602 lock_guard<mutex> lock(_data_mutex);
606 qDebug() << "Unexpected logic packet";
610 if (!_cur_logic_snapshot)
612 set_capture_state(Running);
614 // Create a new data snapshot
615 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
616 new data::LogicSnapshot(logic));
617 _logic_data->push_snapshot(_cur_logic_snapshot);
621 // Append to the existing data snapshot
622 _cur_logic_snapshot->append_payload(logic);
628 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
630 lock_guard<mutex> lock(_data_mutex);
634 qDebug() << "Unexpected analog packet";
635 return; // This analog packet was not expected.
638 if (!_cur_analog_snapshot)
640 set_capture_state(Running);
642 // Create a new data snapshot
643 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
644 new data::AnalogSnapshot(analog));
645 _analog_data->push_snapshot(_cur_analog_snapshot);
649 // Append to the existing data snapshot
650 _cur_analog_snapshot->append_payload(analog);
656 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
657 const struct sr_datafeed_packet *packet)
662 switch (packet->type) {
668 assert(packet->payload);
670 *(const sr_datafeed_meta*)packet->payload);
674 assert(packet->payload);
675 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
679 assert(packet->payload);
680 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
686 lock_guard<mutex> lock(_data_mutex);
687 _cur_logic_snapshot.reset();
688 _cur_analog_snapshot.reset();
696 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
697 const struct sr_datafeed_packet *packet, void *cb_data)
701 _session->data_feed_in(sdi, packet);