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
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 namespace boost;
55 // TODO: This should not be necessary
56 SigSession* SigSession::_session = NULL;
58 SigSession::SigSession(DeviceManager &device_manager) :
59 _device_manager(device_manager),
61 _capture_state(Stopped)
63 // TODO: This should not be necessary
67 SigSession::~SigSession()
71 _sampling_thread.join();
74 _device_manager.release_device(_sdi);
77 // TODO: This should not be necessary
81 struct sr_dev_inst* SigSession::get_device() const
86 void SigSession::set_device(struct sr_dev_inst *sdi)
88 // Ensure we are not capturing before setting the device
92 _device_manager.release_device(_sdi);
94 _device_manager.use_device(sdi, this);
99 void SigSession::release_device(struct sr_dev_inst *sdi)
103 assert(_capture_state == Stopped);
105 update_signals(NULL);
108 void SigSession::load_file(const string &name,
109 function<void (const QString)> error_handler)
113 if (sr_session_load(name.c_str()) == SR_OK) {
114 GSList *devlist = NULL;
115 sr_session_dev_list(&devlist);
117 if (!devlist || !devlist->data ||
118 sr_session_start() != SR_OK) {
119 error_handler(tr("Failed to start session."));
123 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
124 g_slist_free(devlist);
126 _decode_traces.clear();
128 read_sample_rate(sdi);
130 _sampling_thread = boost::thread(
131 &SigSession::load_session_thread_proc, this,
137 if (!(in = load_input_file_format(name.c_str(),
141 _decode_traces.clear();
142 update_signals(in->sdi);
143 read_sample_rate(in->sdi);
145 _sampling_thread = boost::thread(
146 &SigSession::load_input_thread_proc, this,
147 name, in, error_handler);
151 SigSession::capture_state SigSession::get_capture_state() const
153 lock_guard<mutex> lock(_sampling_mutex);
154 return _capture_state;
157 void SigSession::start_capture(uint64_t record_length,
158 function<void (const QString)> error_handler)
162 // Check that a device instance has been selected.
164 qDebug() << "No device selected";
168 // Check that at least one probe is enabled
170 for (l = _sdi->probes; l; l = l->next) {
171 sr_probe *const probe = (sr_probe*)l->data;
178 error_handler(tr("No probes enabled."));
183 _sampling_thread = boost::thread(
184 &SigSession::sample_thread_proc, this, _sdi,
185 record_length, error_handler);
188 void SigSession::stop_capture()
190 if (get_capture_state() == Stopped)
195 // Check that sampling stopped
196 _sampling_thread.join();
199 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
201 lock_guard<mutex> lock(_signals_mutex);
205 boost::shared_ptr<data::Logic> SigSession::get_data()
211 bool SigSession::add_decoder(srd_decoder *const dec)
213 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
214 shared_ptr<data::DecoderStack> decoder_stack;
218 lock_guard<mutex> lock(_signals_mutex);
220 // Create the decoder
221 decoder_stack = shared_ptr<data::DecoderStack>(
222 new data::DecoderStack(dec));
224 // Auto select the initial probes
225 for(const GSList *i = dec->probes; i; i = i->next)
227 const srd_probe *const probe = (const srd_probe*)i->data;
228 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
230 shared_ptr<view::LogicSignal> l =
231 dynamic_pointer_cast<view::LogicSignal>(s);
232 if (l && QString::fromUtf8(probe->name).
234 l->get_name().toLower()))
239 assert(decoder_stack);
240 assert(!decoder_stack->stack().empty());
241 assert(decoder_stack->stack().front());
242 decoder_stack->stack().front()->set_probes(probes);
244 // Create the decode signal
245 shared_ptr<view::DecodeTrace> d(
246 new view::DecodeTrace(*this, decoder_stack,
247 _decode_traces.size()));
248 _decode_traces.push_back(d);
250 catch(std::runtime_error e)
257 // Do an initial decode
258 decoder_stack->begin_decode();
263 vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const
265 lock_guard<mutex> lock(_signals_mutex);
266 return _decode_traces;
269 void SigSession::remove_decode_signal(view::DecodeTrace *signal)
271 for (vector< shared_ptr<view::DecodeTrace> >::iterator i =
272 _decode_traces.begin();
273 i != _decode_traces.end();
275 if ((*i).get() == signal)
277 _decode_traces.erase(i);
284 void SigSession::set_capture_state(capture_state state)
286 lock_guard<mutex> lock(_sampling_mutex);
287 const bool changed = _capture_state != state;
288 _capture_state = state;
290 capture_state_changed(state);
294 * Attempts to autodetect the format. Failing that
295 * @param filename The filename of the input file.
296 * @return A pointer to the 'struct sr_input_format' that should be used,
297 * or NULL if no input format was selected or auto-detected.
299 sr_input_format* SigSession::determine_input_file_format(
300 const string &filename)
304 /* If there are no input formats, return NULL right away. */
305 sr_input_format *const *const inputs = sr_input_list();
307 g_critical("No supported input formats available.");
311 /* Otherwise, try to find an input module that can handle this file. */
312 for (i = 0; inputs[i]; i++) {
313 if (inputs[i]->format_match(filename.c_str()))
317 /* Return NULL if no input module wanted to touch this. */
319 g_critical("Error: no matching input module found.");
326 sr_input* SigSession::load_input_file_format(const string &filename,
327 function<void (const QString)> error_handler,
328 sr_input_format *format)
333 if (!format && !(format =
334 determine_input_file_format(filename.c_str()))) {
335 /* The exact cause was already logged. */
339 if (stat(filename.c_str(), &st) == -1) {
340 error_handler(tr("Failed to load file"));
344 /* Initialize the input module. */
345 if (!(in = new sr_input)) {
346 qDebug("Failed to allocate input module.\n");
352 if (in->format->init &&
353 in->format->init(in, filename.c_str()) != SR_OK) {
354 qDebug("Input format init failed.\n");
360 if (sr_session_dev_add(in->sdi) != SR_OK) {
361 qDebug("Failed to use device.\n");
362 sr_session_destroy();
369 void SigSession::update_signals(const sr_dev_inst *const sdi)
371 assert(_capture_state == Stopped);
373 shared_ptr<view::Signal> signal;
374 unsigned int logic_probe_count = 0;
375 unsigned int analog_probe_count = 0;
377 // Clear the decode traces
378 _decode_traces.clear();
380 // Detect what data types we will receive
382 for (const GSList *l = sdi->probes; l; l = l->next) {
383 const sr_probe *const probe = (const sr_probe *)l->data;
387 switch(probe->type) {
392 case SR_PROBE_ANALOG:
393 analog_probe_count++;
399 // Create data containers for the data snapshots
401 lock_guard<mutex> data_lock(_data_mutex);
404 if (logic_probe_count != 0) {
405 _logic_data.reset(new data::Logic(
410 _analog_data.reset();
411 if (analog_probe_count != 0) {
412 _analog_data.reset(new data::Analog());
413 assert(_analog_data);
417 // Make the Signals list
419 lock_guard<mutex> lock(_signals_mutex);
424 for (const GSList *l = sdi->probes; l; l = l->next) {
425 sr_probe *const probe = (sr_probe *)l->data;
428 switch(probe->type) {
430 signal = shared_ptr<view::Signal>(
431 new view::LogicSignal(*this, probe,
435 case SR_PROBE_ANALOG:
436 signal = shared_ptr<view::Signal>(
437 new view::AnalogSignal(*this, probe,
442 _signals.push_back(signal);
450 bool SigSession::is_trigger_enabled() const
453 for (const GSList *l = _sdi->probes; l; l = l->next) {
454 const sr_probe *const p = (const sr_probe *)l->data;
456 if (p->trigger && p->trigger[0] != '\0')
463 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
466 uint64_t sample_rate = 0;
468 // Read out the sample rate
471 const int ret = sr_config_get(sdi->driver, sdi, NULL,
472 SR_CONF_SAMPLERATE, &gvar);
474 qDebug("Failed to get samplerate\n");
478 sample_rate = g_variant_get_uint64(gvar);
479 g_variant_unref(gvar);
483 _analog_data->set_samplerate(sample_rate);
485 _logic_data->set_samplerate(sample_rate);
488 void SigSession::load_session_thread_proc(
489 function<void (const QString)> error_handler)
493 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
495 set_capture_state(Running);
499 sr_session_destroy();
500 set_capture_state(Stopped);
502 // Confirm that SR_DF_END was received
503 assert(!_cur_logic_snapshot);
504 assert(!_cur_analog_snapshot);
507 void SigSession::load_input_thread_proc(const string name,
508 sr_input *in, function<void (const QString)> error_handler)
515 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
517 set_capture_state(Running);
519 in->format->loadfile(in, name.c_str());
521 sr_session_destroy();
522 set_capture_state(Stopped);
524 // Confirm that SR_DF_END was received
525 assert(!_cur_logic_snapshot);
526 assert(!_cur_analog_snapshot);
531 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
532 uint64_t record_length,
533 function<void (const QString)> error_handler)
536 assert(error_handler);
539 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
541 if (sr_session_dev_add(sdi) != SR_OK) {
542 error_handler(tr("Failed to use device."));
543 sr_session_destroy();
547 // Set the sample limit
548 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
549 g_variant_new_uint64(record_length)) != SR_OK) {
550 error_handler(tr("Failed to configure "
551 "time-based sample limit."));
552 sr_session_destroy();
556 if (sr_session_start() != SR_OK) {
557 error_handler(tr("Failed to start session."));
561 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
564 sr_session_destroy();
566 set_capture_state(Stopped);
568 // Confirm that SR_DF_END was received
569 assert(!_cur_logic_snapshot);
570 assert(!_cur_analog_snapshot);
573 void SigSession::feed_in_header(const sr_dev_inst *sdi)
575 read_sample_rate(sdi);
578 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
579 const sr_datafeed_meta &meta)
583 for (const GSList *l = meta.config; l; l = l->next) {
584 const sr_config *const src = (const sr_config*)l->data;
586 case SR_CONF_SAMPLERATE:
587 /// @todo handle samplerate changes
588 /// samplerate = (uint64_t *)src->value;
591 // Unknown metadata is not an error.
599 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
601 lock_guard<mutex> lock(_data_mutex);
605 qDebug() << "Unexpected logic packet";
609 if (!_cur_logic_snapshot)
611 set_capture_state(Running);
613 // Create a new data snapshot
614 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
615 new data::LogicSnapshot(logic));
616 _logic_data->push_snapshot(_cur_logic_snapshot);
620 // Append to the existing data snapshot
621 _cur_logic_snapshot->append_payload(logic);
627 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
629 lock_guard<mutex> lock(_data_mutex);
633 qDebug() << "Unexpected analog packet";
634 return; // This analog packet was not expected.
637 if (!_cur_analog_snapshot)
639 set_capture_state(Running);
641 // Create a new data snapshot
642 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
643 new data::AnalogSnapshot(analog));
644 _analog_data->push_snapshot(_cur_analog_snapshot);
648 // Append to the existing data snapshot
649 _cur_analog_snapshot->append_payload(analog);
655 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
656 const struct sr_datafeed_packet *packet)
661 switch (packet->type) {
667 assert(packet->payload);
669 *(const sr_datafeed_meta*)packet->payload);
673 assert(packet->payload);
674 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
678 assert(packet->payload);
679 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
685 lock_guard<mutex> lock(_data_mutex);
686 _cur_logic_snapshot.reset();
687 _cur_analog_snapshot.reset();
695 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
696 const struct sr_datafeed_packet *packet, void *cb_data)
700 _session->data_feed_in(sdi, packet);