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 "sigsession.h"
23 #include "devicemanager.h"
25 #include "data/analog.h"
26 #include "data/analogsnapshot.h"
27 #include "data/decoder.h"
28 #include "data/logic.h"
29 #include "data/logicsnapshot.h"
31 #include "view/analogsignal.h"
32 #include "view/decodesignal.h"
33 #include "view/logicsignal.h"
41 using namespace boost;
46 // TODO: This should not be necessary
47 SigSession* SigSession::_session = NULL;
49 SigSession::SigSession(DeviceManager &device_manager) :
50 _device_manager(device_manager),
52 _capture_state(Stopped)
54 // TODO: This should not be necessary
58 SigSession::~SigSession()
62 _sampling_thread.join();
65 _device_manager.release_device(_sdi);
68 // TODO: This should not be necessary
72 struct sr_dev_inst* SigSession::get_device() const
77 void SigSession::set_device(struct sr_dev_inst *sdi)
80 _device_manager.release_device(_sdi);
82 _device_manager.use_device(sdi, this);
87 void SigSession::release_device(struct sr_dev_inst *sdi)
91 assert(_capture_state == Stopped);
96 void SigSession::load_file(const string &name,
97 function<void (const QString)> error_handler)
101 if (sr_session_load(name.c_str()) == SR_OK) {
102 GSList *devlist = NULL;
103 sr_session_dev_list(&devlist);
105 if (!devlist || !devlist->data ||
106 sr_session_start() != SR_OK) {
107 error_handler(tr("Failed to start session."));
111 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
112 g_slist_free(devlist);
115 read_sample_rate(sdi);
117 _sampling_thread = boost::thread(
118 &SigSession::load_session_thread_proc, this,
124 if (!(in = load_input_file_format(name.c_str(),
128 update_signals(in->sdi);
129 read_sample_rate(in->sdi);
131 _sampling_thread = boost::thread(
132 &SigSession::load_input_thread_proc, this,
133 name, in, error_handler);
137 SigSession::capture_state SigSession::get_capture_state() const
139 lock_guard<mutex> lock(_sampling_mutex);
140 return _capture_state;
143 void SigSession::start_capture(uint64_t record_length,
144 function<void (const QString)> error_handler)
148 // Check that a device instance has been selected.
150 qDebug() << "No device selected";
154 // Check that at least one probe is enabled
156 for (l = _sdi->probes; l; l = l->next) {
157 sr_probe *const probe = (sr_probe*)l->data;
164 error_handler(tr("No probes enabled."));
169 _sampling_thread = boost::thread(
170 &SigSession::sample_thread_proc, this, _sdi,
171 record_length, error_handler);
174 void SigSession::stop_capture()
176 if (get_capture_state() == Stopped)
181 // Check that sampling stopped
182 _sampling_thread.join();
185 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
187 lock_guard<mutex> lock(_signals_mutex);
191 boost::shared_ptr<data::Logic> SigSession::get_data()
196 void SigSession::add_decoder(srd_decoder *const dec,
197 std::map<const srd_probe*,
198 boost::shared_ptr<view::Signal> > probes)
201 lock_guard<mutex> lock(_signals_mutex);
203 if (!_decode_traces.empty()) {
204 qDebug("Currently only one decode trace can be added "
206 _decode_traces.clear();
209 shared_ptr<data::Decoder> decoder(
210 new data::Decoder(dec, probes));
211 shared_ptr<view::DecodeSignal> d(
212 new view::DecodeSignal(*this, decoder,
213 _decode_traces.size()));
214 _decode_traces.push_back(d);
219 vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
221 lock_guard<mutex> lock(_signals_mutex);
222 return _decode_traces;
225 void SigSession::set_capture_state(capture_state state)
227 lock_guard<mutex> lock(_sampling_mutex);
228 const bool changed = _capture_state != state;
229 _capture_state = state;
231 capture_state_changed(state);
235 * Attempts to autodetect the format. Failing that
236 * @param filename The filename of the input file.
237 * @return A pointer to the 'struct sr_input_format' that should be used,
238 * or NULL if no input format was selected or auto-detected.
240 sr_input_format* SigSession::determine_input_file_format(
241 const string &filename)
245 /* If there are no input formats, return NULL right away. */
246 sr_input_format *const *const inputs = sr_input_list();
248 g_critical("No supported input formats available.");
252 /* Otherwise, try to find an input module that can handle this file. */
253 for (i = 0; inputs[i]; i++) {
254 if (inputs[i]->format_match(filename.c_str()))
258 /* Return NULL if no input module wanted to touch this. */
260 g_critical("Error: no matching input module found.");
267 sr_input* SigSession::load_input_file_format(const string &filename,
268 function<void (const QString)> error_handler,
269 sr_input_format *format)
274 if (!format && !(format =
275 determine_input_file_format(filename.c_str()))) {
276 /* The exact cause was already logged. */
280 if (stat(filename.c_str(), &st) == -1) {
281 error_handler(tr("Failed to load file"));
285 /* Initialize the input module. */
286 if (!(in = new sr_input)) {
287 qDebug("Failed to allocate input module.\n");
293 if (in->format->init &&
294 in->format->init(in, filename.c_str()) != SR_OK) {
295 qDebug("Input format init failed.\n");
301 if (sr_session_dev_add(in->sdi) != SR_OK) {
302 qDebug("Failed to use device.\n");
303 sr_session_destroy();
310 void SigSession::update_signals(const sr_dev_inst *const sdi)
312 assert(_capture_state == Stopped);
314 shared_ptr<view::Signal> signal;
315 unsigned int logic_probe_count = 0;
316 unsigned int analog_probe_count = 0;
318 // Clear the decode traces
319 _decode_traces.clear();
321 // Detect what data types we will receive
323 for (const GSList *l = sdi->probes; l; l = l->next) {
324 const sr_probe *const probe = (const sr_probe *)l->data;
328 switch(probe->type) {
333 case SR_PROBE_ANALOG:
334 analog_probe_count++;
340 // Create data containers for the data snapshots
342 lock_guard<mutex> data_lock(_data_mutex);
345 if (logic_probe_count != 0) {
346 _logic_data.reset(new data::Logic(
351 _analog_data.reset();
352 if (analog_probe_count != 0) {
353 _analog_data.reset(new data::Analog());
354 assert(_analog_data);
358 // Make the Signals list
360 lock_guard<mutex> lock(_signals_mutex);
365 for (const GSList *l = sdi->probes; l; l = l->next) {
366 const sr_probe *const probe =
367 (const sr_probe *)l->data;
370 switch(probe->type) {
372 signal = shared_ptr<view::Signal>(
373 new view::LogicSignal(*this, probe,
377 case SR_PROBE_ANALOG:
378 signal = shared_ptr<view::Signal>(
379 new view::AnalogSignal(*this, probe,
384 _signals.push_back(signal);
392 bool SigSession::is_trigger_enabled() const
395 for (const GSList *l = _sdi->probes; l; l = l->next) {
396 const sr_probe *const p = (const sr_probe *)l->data;
398 if (p->trigger && p->trigger[0] != '\0')
405 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
408 uint64_t sample_rate = 0;
410 // Read out the sample rate
413 const int ret = sr_config_get(sdi->driver,
414 SR_CONF_SAMPLERATE, &gvar, sdi);
416 qDebug("Failed to get samplerate\n");
420 sample_rate = g_variant_get_uint64(gvar);
421 g_variant_unref(gvar);
425 _analog_data->set_samplerate(sample_rate);
427 _logic_data->set_samplerate(sample_rate);
430 void SigSession::load_session_thread_proc(
431 function<void (const QString)> error_handler)
435 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
437 set_capture_state(Running);
441 sr_session_destroy();
442 set_capture_state(Stopped);
444 // Confirm that SR_DF_END was received
445 assert(!_cur_logic_snapshot);
446 assert(!_cur_analog_snapshot);
449 void SigSession::load_input_thread_proc(const string name,
450 sr_input *in, function<void (const QString)> error_handler)
457 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
459 set_capture_state(Running);
461 in->format->loadfile(in, name.c_str());
463 sr_session_destroy();
464 set_capture_state(Stopped);
466 // Confirm that SR_DF_END was received
467 assert(!_cur_logic_snapshot);
468 assert(!_cur_analog_snapshot);
473 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
474 uint64_t record_length,
475 function<void (const QString)> error_handler)
478 assert(error_handler);
481 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
483 if (sr_session_dev_add(sdi) != SR_OK) {
484 error_handler(tr("Failed to use device."));
485 sr_session_destroy();
489 // Set the sample limit
490 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
491 g_variant_new_uint64(record_length)) != SR_OK) {
492 error_handler(tr("Failed to configure "
493 "time-based sample limit."));
494 sr_session_destroy();
498 if (sr_session_start() != SR_OK) {
499 error_handler(tr("Failed to start session."));
503 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
506 sr_session_destroy();
508 set_capture_state(Stopped);
510 // Confirm that SR_DF_END was received
511 assert(!_cur_logic_snapshot);
512 assert(!_cur_analog_snapshot);
515 void SigSession::feed_in_header(const sr_dev_inst *sdi)
517 read_sample_rate(sdi);
520 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
521 const sr_datafeed_meta &meta)
525 for (const GSList *l = meta.config; l; l = l->next) {
526 const sr_config *const src = (const sr_config*)l->data;
528 case SR_CONF_SAMPLERATE:
529 /// @todo handle samplerate changes
530 /// samplerate = (uint64_t *)src->value;
533 // Unknown metadata is not an error.
541 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
543 lock_guard<mutex> lock(_data_mutex);
547 qDebug() << "Unexpected logic packet";
551 if (!_cur_logic_snapshot)
553 set_capture_state(Running);
555 // Create a new data snapshot
556 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
557 new data::LogicSnapshot(logic));
558 _logic_data->push_snapshot(_cur_logic_snapshot);
562 // Append to the existing data snapshot
563 _cur_logic_snapshot->append_payload(logic);
569 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
571 lock_guard<mutex> lock(_data_mutex);
575 qDebug() << "Unexpected analog packet";
576 return; // This analog packet was not expected.
579 if (!_cur_analog_snapshot)
581 set_capture_state(Running);
583 // Create a new data snapshot
584 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
585 new data::AnalogSnapshot(analog));
586 _analog_data->push_snapshot(_cur_analog_snapshot);
590 // Append to the existing data snapshot
591 _cur_analog_snapshot->append_payload(analog);
597 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
598 const struct sr_datafeed_packet *packet)
603 switch (packet->type) {
609 assert(packet->payload);
611 *(const sr_datafeed_meta*)packet->payload);
615 assert(packet->payload);
616 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
620 assert(packet->payload);
621 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
627 lock_guard<mutex> lock(_data_mutex);
628 _cur_logic_snapshot.reset();
629 _cur_analog_snapshot.reset();
637 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
638 const struct sr_datafeed_packet *packet, void *cb_data)
642 _session->data_feed_in(sdi, packet);