2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 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
21 #include <libsigrokdecode/libsigrokdecode.h>
23 #include "sigsession.h"
25 #include "devicemanager.h"
27 #include "data/analog.h"
28 #include "data/analogsnapshot.h"
29 #include "data/decoderstack.h"
30 #include "data/logic.h"
31 #include "data/logicsnapshot.h"
32 #include "data/decode/decoder.h"
34 #include "view/analogsignal.h"
35 #include "view/decodetrace.h"
36 #include "view/logicsignal.h"
42 #include <boost/foreach.hpp>
48 using namespace boost;
53 // TODO: This should not be necessary
54 SigSession* SigSession::_session = NULL;
56 SigSession::SigSession(DeviceManager &device_manager) :
57 _device_manager(device_manager),
59 _capture_state(Stopped)
61 // TODO: This should not be necessary
65 SigSession::~SigSession()
69 _sampling_thread.join();
72 _device_manager.release_device(_sdi);
75 // TODO: This should not be necessary
79 struct sr_dev_inst* SigSession::get_device() const
84 void SigSession::set_device(struct sr_dev_inst *sdi)
87 _device_manager.release_device(_sdi);
89 _device_manager.use_device(sdi, this);
94 void SigSession::release_device(struct sr_dev_inst *sdi)
98 assert(_capture_state == Stopped);
100 update_signals(NULL);
103 void SigSession::load_file(const string &name,
104 function<void (const QString)> error_handler)
108 if (sr_session_load(name.c_str()) == SR_OK) {
109 GSList *devlist = NULL;
110 sr_session_dev_list(&devlist);
112 if (!devlist || !devlist->data ||
113 sr_session_start() != SR_OK) {
114 error_handler(tr("Failed to start session."));
118 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
119 g_slist_free(devlist);
122 read_sample_rate(sdi);
124 _sampling_thread = boost::thread(
125 &SigSession::load_session_thread_proc, this,
131 if (!(in = load_input_file_format(name.c_str(),
135 update_signals(in->sdi);
136 read_sample_rate(in->sdi);
138 _sampling_thread = boost::thread(
139 &SigSession::load_input_thread_proc, this,
140 name, in, error_handler);
144 SigSession::capture_state SigSession::get_capture_state() const
146 lock_guard<mutex> lock(_sampling_mutex);
147 return _capture_state;
150 void SigSession::start_capture(uint64_t record_length,
151 function<void (const QString)> error_handler)
155 // Check that a device instance has been selected.
157 qDebug() << "No device selected";
161 // Check that at least one probe is enabled
163 for (l = _sdi->probes; l; l = l->next) {
164 sr_probe *const probe = (sr_probe*)l->data;
171 error_handler(tr("No probes enabled."));
176 _sampling_thread = boost::thread(
177 &SigSession::sample_thread_proc, this, _sdi,
178 record_length, error_handler);
181 void SigSession::stop_capture()
183 if (get_capture_state() == Stopped)
188 // Check that sampling stopped
189 _sampling_thread.join();
192 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
194 lock_guard<mutex> lock(_signals_mutex);
198 boost::shared_ptr<data::Logic> SigSession::get_data()
203 bool SigSession::add_decoder(srd_decoder *const dec)
205 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
206 shared_ptr<data::DecoderStack> decoder_stack;
210 lock_guard<mutex> lock(_signals_mutex);
212 // Create the decoder
213 decoder_stack = shared_ptr<data::DecoderStack>(
214 new data::DecoderStack(dec));
216 // Auto select the initial probes
217 for(const GSList *i = dec->probes; i; i = i->next)
219 const srd_probe *const probe = (const srd_probe*)i->data;
220 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
222 shared_ptr<view::LogicSignal> l =
223 dynamic_pointer_cast<view::LogicSignal>(s);
224 if (l && QString(probe->name).toLower().contains(
225 l->get_name().toLower()))
230 assert(decoder_stack);
231 assert(!decoder_stack->stack().empty());
232 assert(decoder_stack->stack().front());
233 decoder_stack->stack().front()->set_probes(probes);
235 // Create the decode signal
236 shared_ptr<view::DecodeTrace> d(
237 new view::DecodeTrace(*this, decoder_stack,
238 _decode_traces.size()));
239 _decode_traces.push_back(d);
241 catch(std::runtime_error e)
248 // Do an initial decode
249 decoder_stack->begin_decode();
254 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
256 lock_guard<mutex> lock(_signals_mutex);
257 return _decode_traces;
260 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
262 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
263 _decode_traces.begin();
264 i != _decode_traces.end();
266 if ((*i).get() == signal)
268 _decode_traces.erase(i);
274 void SigSession::set_capture_state(capture_state state)
276 lock_guard<mutex> lock(_sampling_mutex);
277 const bool changed = _capture_state != state;
278 _capture_state = state;
280 capture_state_changed(state);
284 * Attempts to autodetect the format. Failing that
285 * @param filename The filename of the input file.
286 * @return A pointer to the 'struct sr_input_format' that should be used,
287 * or NULL if no input format was selected or auto-detected.
289 sr_input_format* SigSession::determine_input_file_format(
290 const string &filename)
294 /* If there are no input formats, return NULL right away. */
295 sr_input_format *const *const inputs = sr_input_list();
297 g_critical("No supported input formats available.");
301 /* Otherwise, try to find an input module that can handle this file. */
302 for (i = 0; inputs[i]; i++) {
303 if (inputs[i]->format_match(filename.c_str()))
307 /* Return NULL if no input module wanted to touch this. */
309 g_critical("Error: no matching input module found.");
316 sr_input* SigSession::load_input_file_format(const string &filename,
317 function<void (const QString)> error_handler,
318 sr_input_format *format)
323 if (!format && !(format =
324 determine_input_file_format(filename.c_str()))) {
325 /* The exact cause was already logged. */
329 if (stat(filename.c_str(), &st) == -1) {
330 error_handler(tr("Failed to load file"));
334 /* Initialize the input module. */
335 if (!(in = new sr_input)) {
336 qDebug("Failed to allocate input module.\n");
342 if (in->format->init &&
343 in->format->init(in, filename.c_str()) != SR_OK) {
344 qDebug("Input format init failed.\n");
350 if (sr_session_dev_add(in->sdi) != SR_OK) {
351 qDebug("Failed to use device.\n");
352 sr_session_destroy();
359 void SigSession::update_signals(const sr_dev_inst *const sdi)
361 assert(_capture_state == Stopped);
363 shared_ptr<view::Signal> signal;
364 unsigned int logic_probe_count = 0;
365 unsigned int analog_probe_count = 0;
367 // Clear the decode traces
368 _decode_traces.clear();
370 // Detect what data types we will receive
372 for (const GSList *l = sdi->probes; l; l = l->next) {
373 const sr_probe *const probe = (const sr_probe *)l->data;
377 switch(probe->type) {
382 case SR_PROBE_ANALOG:
383 analog_probe_count++;
389 // Create data containers for the data snapshots
391 lock_guard<mutex> data_lock(_data_mutex);
394 if (logic_probe_count != 0) {
395 _logic_data.reset(new data::Logic(
400 _analog_data.reset();
401 if (analog_probe_count != 0) {
402 _analog_data.reset(new data::Analog());
403 assert(_analog_data);
407 // Make the Signals list
409 lock_guard<mutex> lock(_signals_mutex);
414 for (const GSList *l = sdi->probes; l; l = l->next) {
415 sr_probe *const probe = (sr_probe *)l->data;
418 switch(probe->type) {
420 signal = shared_ptr<view::Signal>(
421 new view::LogicSignal(*this, probe,
425 case SR_PROBE_ANALOG:
426 signal = shared_ptr<view::Signal>(
427 new view::AnalogSignal(*this, probe,
432 _signals.push_back(signal);
440 bool SigSession::is_trigger_enabled() const
443 for (const GSList *l = _sdi->probes; l; l = l->next) {
444 const sr_probe *const p = (const sr_probe *)l->data;
446 if (p->trigger && p->trigger[0] != '\0')
453 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
456 uint64_t sample_rate = 0;
458 // Read out the sample rate
461 const int ret = sr_config_get(sdi->driver, sdi, NULL,
462 SR_CONF_SAMPLERATE, &gvar);
464 qDebug("Failed to get samplerate\n");
468 sample_rate = g_variant_get_uint64(gvar);
469 g_variant_unref(gvar);
473 _analog_data->set_samplerate(sample_rate);
475 _logic_data->set_samplerate(sample_rate);
478 void SigSession::load_session_thread_proc(
479 function<void (const QString)> error_handler)
483 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
485 set_capture_state(Running);
489 sr_session_destroy();
490 set_capture_state(Stopped);
492 // Confirm that SR_DF_END was received
493 assert(!_cur_logic_snapshot);
494 assert(!_cur_analog_snapshot);
497 void SigSession::load_input_thread_proc(const string name,
498 sr_input *in, function<void (const QString)> error_handler)
505 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
507 set_capture_state(Running);
509 in->format->loadfile(in, name.c_str());
511 sr_session_destroy();
512 set_capture_state(Stopped);
514 // Confirm that SR_DF_END was received
515 assert(!_cur_logic_snapshot);
516 assert(!_cur_analog_snapshot);
521 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
522 uint64_t record_length,
523 function<void (const QString)> error_handler)
526 assert(error_handler);
529 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
531 if (sr_session_dev_add(sdi) != SR_OK) {
532 error_handler(tr("Failed to use device."));
533 sr_session_destroy();
537 // Set the sample limit
538 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
539 g_variant_new_uint64(record_length)) != SR_OK) {
540 error_handler(tr("Failed to configure "
541 "time-based sample limit."));
542 sr_session_destroy();
546 if (sr_session_start() != SR_OK) {
547 error_handler(tr("Failed to start session."));
551 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
554 sr_session_destroy();
556 set_capture_state(Stopped);
558 // Confirm that SR_DF_END was received
559 assert(!_cur_logic_snapshot);
560 assert(!_cur_analog_snapshot);
563 void SigSession::feed_in_header(const sr_dev_inst *sdi)
565 read_sample_rate(sdi);
568 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
569 const sr_datafeed_meta &meta)
573 for (const GSList *l = meta.config; l; l = l->next) {
574 const sr_config *const src = (const sr_config*)l->data;
576 case SR_CONF_SAMPLERATE:
577 /// @todo handle samplerate changes
578 /// samplerate = (uint64_t *)src->value;
581 // Unknown metadata is not an error.
589 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
591 lock_guard<mutex> lock(_data_mutex);
595 qDebug() << "Unexpected logic packet";
599 if (!_cur_logic_snapshot)
601 set_capture_state(Running);
603 // Create a new data snapshot
604 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
605 new data::LogicSnapshot(logic));
606 _logic_data->push_snapshot(_cur_logic_snapshot);
610 // Append to the existing data snapshot
611 _cur_logic_snapshot->append_payload(logic);
617 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
619 lock_guard<mutex> lock(_data_mutex);
623 qDebug() << "Unexpected analog packet";
624 return; // This analog packet was not expected.
627 if (!_cur_analog_snapshot)
629 set_capture_state(Running);
631 // Create a new data snapshot
632 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
633 new data::AnalogSnapshot(analog));
634 _analog_data->push_snapshot(_cur_analog_snapshot);
638 // Append to the existing data snapshot
639 _cur_analog_snapshot->append_payload(analog);
645 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
646 const struct sr_datafeed_packet *packet)
651 switch (packet->type) {
657 assert(packet->payload);
659 *(const sr_datafeed_meta*)packet->payload);
663 assert(packet->payload);
664 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
668 assert(packet->payload);
669 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
675 lock_guard<mutex> lock(_data_mutex);
676 _cur_logic_snapshot.reset();
677 _cur_analog_snapshot.reset();
685 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
686 const struct sr_datafeed_packet *packet, void *cb_data)
690 _session->data_feed_in(sdi, packet);