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/decoder.h"
30 #include "data/logic.h"
31 #include "data/logicsnapshot.h"
33 #include "view/analogsignal.h"
34 #include "view/decodesignal.h"
35 #include "view/logicsignal.h"
41 #include <boost/foreach.hpp>
47 using namespace boost;
52 // TODO: This should not be necessary
53 SigSession* SigSession::_session = NULL;
55 SigSession::SigSession(DeviceManager &device_manager) :
56 _device_manager(device_manager),
58 _capture_state(Stopped)
60 // TODO: This should not be necessary
64 SigSession::~SigSession()
68 _sampling_thread.join();
71 _device_manager.release_device(_sdi);
74 // TODO: This should not be necessary
78 struct sr_dev_inst* SigSession::get_device() const
83 void SigSession::set_device(struct sr_dev_inst *sdi)
86 _device_manager.release_device(_sdi);
88 _device_manager.use_device(sdi, this);
93 void SigSession::release_device(struct sr_dev_inst *sdi)
97 assert(_capture_state == Stopped);
102 void SigSession::load_file(const string &name,
103 function<void (const QString)> error_handler)
107 if (sr_session_load(name.c_str()) == SR_OK) {
108 GSList *devlist = NULL;
109 sr_session_dev_list(&devlist);
111 if (!devlist || !devlist->data ||
112 sr_session_start() != SR_OK) {
113 error_handler(tr("Failed to start session."));
117 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
118 g_slist_free(devlist);
121 read_sample_rate(sdi);
123 _sampling_thread = boost::thread(
124 &SigSession::load_session_thread_proc, this,
130 if (!(in = load_input_file_format(name.c_str(),
134 update_signals(in->sdi);
135 read_sample_rate(in->sdi);
137 _sampling_thread = boost::thread(
138 &SigSession::load_input_thread_proc, this,
139 name, in, error_handler);
143 SigSession::capture_state SigSession::get_capture_state() const
145 lock_guard<mutex> lock(_sampling_mutex);
146 return _capture_state;
149 void SigSession::start_capture(uint64_t record_length,
150 function<void (const QString)> error_handler)
154 // Check that a device instance has been selected.
156 qDebug() << "No device selected";
160 // Check that at least one probe is enabled
162 for (l = _sdi->probes; l; l = l->next) {
163 sr_probe *const probe = (sr_probe*)l->data;
170 error_handler(tr("No probes enabled."));
175 _sampling_thread = boost::thread(
176 &SigSession::sample_thread_proc, this, _sdi,
177 record_length, error_handler);
180 void SigSession::stop_capture()
182 if (get_capture_state() == Stopped)
187 // Check that sampling stopped
188 _sampling_thread.join();
191 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
193 lock_guard<mutex> lock(_signals_mutex);
197 boost::shared_ptr<data::Logic> SigSession::get_data()
202 bool SigSession::add_decoder(srd_decoder *const dec)
204 map<const srd_probe*, shared_ptr<view::LogicSignal> > probes;
208 lock_guard<mutex> lock(_signals_mutex);
210 // Create the decoder
211 shared_ptr<data::Decoder> decoder(new data::Decoder(dec));
213 // Auto select the initial probes
214 for(const GSList *i = dec->probes; i; i = i->next)
216 const srd_probe *const probe = (const srd_probe*)i->data;
217 BOOST_FOREACH(shared_ptr<view::Signal> s, _signals)
219 shared_ptr<view::LogicSignal> l =
220 dynamic_pointer_cast<view::LogicSignal>(s);
221 if (l && QString(probe->name).toLower().contains(
222 l->get_name().toLower()))
227 decoder->set_probes(probes);
229 // Create the decode signal
230 shared_ptr<view::DecodeSignal> d(
231 new view::DecodeSignal(*this, decoder,
232 _decode_traces.size()));
233 _decode_traces.push_back(d);
235 catch(std::runtime_error e)
245 vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
247 lock_guard<mutex> lock(_signals_mutex);
248 return _decode_traces;
251 void SigSession::remove_decode_signal(view::DecodeSignal *signal)
253 for (vector< shared_ptr<view::DecodeSignal> >::iterator i =
254 _decode_traces.begin();
255 i != _decode_traces.end();
257 if ((*i).get() == signal)
259 _decode_traces.erase(i);
265 void SigSession::set_capture_state(capture_state state)
267 lock_guard<mutex> lock(_sampling_mutex);
268 const bool changed = _capture_state != state;
269 _capture_state = state;
271 capture_state_changed(state);
275 * Attempts to autodetect the format. Failing that
276 * @param filename The filename of the input file.
277 * @return A pointer to the 'struct sr_input_format' that should be used,
278 * or NULL if no input format was selected or auto-detected.
280 sr_input_format* SigSession::determine_input_file_format(
281 const string &filename)
285 /* If there are no input formats, return NULL right away. */
286 sr_input_format *const *const inputs = sr_input_list();
288 g_critical("No supported input formats available.");
292 /* Otherwise, try to find an input module that can handle this file. */
293 for (i = 0; inputs[i]; i++) {
294 if (inputs[i]->format_match(filename.c_str()))
298 /* Return NULL if no input module wanted to touch this. */
300 g_critical("Error: no matching input module found.");
307 sr_input* SigSession::load_input_file_format(const string &filename,
308 function<void (const QString)> error_handler,
309 sr_input_format *format)
314 if (!format && !(format =
315 determine_input_file_format(filename.c_str()))) {
316 /* The exact cause was already logged. */
320 if (stat(filename.c_str(), &st) == -1) {
321 error_handler(tr("Failed to load file"));
325 /* Initialize the input module. */
326 if (!(in = new sr_input)) {
327 qDebug("Failed to allocate input module.\n");
333 if (in->format->init &&
334 in->format->init(in, filename.c_str()) != SR_OK) {
335 qDebug("Input format init failed.\n");
341 if (sr_session_dev_add(in->sdi) != SR_OK) {
342 qDebug("Failed to use device.\n");
343 sr_session_destroy();
350 void SigSession::update_signals(const sr_dev_inst *const sdi)
352 assert(_capture_state == Stopped);
354 shared_ptr<view::Signal> signal;
355 unsigned int logic_probe_count = 0;
356 unsigned int analog_probe_count = 0;
358 // Clear the decode traces
359 _decode_traces.clear();
361 // Detect what data types we will receive
363 for (const GSList *l = sdi->probes; l; l = l->next) {
364 const sr_probe *const probe = (const sr_probe *)l->data;
368 switch(probe->type) {
373 case SR_PROBE_ANALOG:
374 analog_probe_count++;
380 // Create data containers for the data snapshots
382 lock_guard<mutex> data_lock(_data_mutex);
385 if (logic_probe_count != 0) {
386 _logic_data.reset(new data::Logic(
391 _analog_data.reset();
392 if (analog_probe_count != 0) {
393 _analog_data.reset(new data::Analog());
394 assert(_analog_data);
398 // Make the Signals list
400 lock_guard<mutex> lock(_signals_mutex);
405 for (const GSList *l = sdi->probes; l; l = l->next) {
406 sr_probe *const probe = (sr_probe *)l->data;
409 switch(probe->type) {
411 signal = shared_ptr<view::Signal>(
412 new view::LogicSignal(*this, probe,
416 case SR_PROBE_ANALOG:
417 signal = shared_ptr<view::Signal>(
418 new view::AnalogSignal(*this, probe,
423 _signals.push_back(signal);
431 bool SigSession::is_trigger_enabled() const
434 for (const GSList *l = _sdi->probes; l; l = l->next) {
435 const sr_probe *const p = (const sr_probe *)l->data;
437 if (p->trigger && p->trigger[0] != '\0')
444 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
447 uint64_t sample_rate = 0;
449 // Read out the sample rate
452 const int ret = sr_config_get(sdi->driver, sdi, NULL,
453 SR_CONF_SAMPLERATE, &gvar);
455 qDebug("Failed to get samplerate\n");
459 sample_rate = g_variant_get_uint64(gvar);
460 g_variant_unref(gvar);
464 _analog_data->set_samplerate(sample_rate);
466 _logic_data->set_samplerate(sample_rate);
469 void SigSession::load_session_thread_proc(
470 function<void (const QString)> error_handler)
474 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
476 set_capture_state(Running);
480 sr_session_destroy();
481 set_capture_state(Stopped);
483 // Confirm that SR_DF_END was received
484 assert(!_cur_logic_snapshot);
485 assert(!_cur_analog_snapshot);
488 void SigSession::load_input_thread_proc(const string name,
489 sr_input *in, function<void (const QString)> error_handler)
496 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
498 set_capture_state(Running);
500 in->format->loadfile(in, name.c_str());
502 sr_session_destroy();
503 set_capture_state(Stopped);
505 // Confirm that SR_DF_END was received
506 assert(!_cur_logic_snapshot);
507 assert(!_cur_analog_snapshot);
512 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
513 uint64_t record_length,
514 function<void (const QString)> error_handler)
517 assert(error_handler);
520 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
522 if (sr_session_dev_add(sdi) != SR_OK) {
523 error_handler(tr("Failed to use device."));
524 sr_session_destroy();
528 // Set the sample limit
529 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
530 g_variant_new_uint64(record_length)) != SR_OK) {
531 error_handler(tr("Failed to configure "
532 "time-based sample limit."));
533 sr_session_destroy();
537 if (sr_session_start() != SR_OK) {
538 error_handler(tr("Failed to start session."));
542 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
545 sr_session_destroy();
547 set_capture_state(Stopped);
549 // Confirm that SR_DF_END was received
550 assert(!_cur_logic_snapshot);
551 assert(!_cur_analog_snapshot);
554 void SigSession::feed_in_header(const sr_dev_inst *sdi)
556 read_sample_rate(sdi);
559 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
560 const sr_datafeed_meta &meta)
564 for (const GSList *l = meta.config; l; l = l->next) {
565 const sr_config *const src = (const sr_config*)l->data;
567 case SR_CONF_SAMPLERATE:
568 /// @todo handle samplerate changes
569 /// samplerate = (uint64_t *)src->value;
572 // Unknown metadata is not an error.
580 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
582 lock_guard<mutex> lock(_data_mutex);
586 qDebug() << "Unexpected logic packet";
590 if (!_cur_logic_snapshot)
592 set_capture_state(Running);
594 // Create a new data snapshot
595 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
596 new data::LogicSnapshot(logic));
597 _logic_data->push_snapshot(_cur_logic_snapshot);
601 // Append to the existing data snapshot
602 _cur_logic_snapshot->append_payload(logic);
608 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
610 lock_guard<mutex> lock(_data_mutex);
614 qDebug() << "Unexpected analog packet";
615 return; // This analog packet was not expected.
618 if (!_cur_analog_snapshot)
620 set_capture_state(Running);
622 // Create a new data snapshot
623 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
624 new data::AnalogSnapshot(analog));
625 _analog_data->push_snapshot(_cur_analog_snapshot);
629 // Append to the existing data snapshot
630 _cur_analog_snapshot->append_payload(analog);
636 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
637 const struct sr_datafeed_packet *packet)
642 switch (packet->type) {
648 assert(packet->payload);
650 *(const sr_datafeed_meta*)packet->payload);
654 assert(packet->payload);
655 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
659 assert(packet->payload);
660 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
666 lock_guard<mutex> lock(_data_mutex);
667 _cur_logic_snapshot.reset();
668 _cur_analog_snapshot.reset();
676 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
677 const struct sr_datafeed_packet *packet, void *cb_data)
681 _session->data_feed_in(sdi, packet);