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 if (_sampling_thread.get())
63 _sampling_thread->join();
64 _sampling_thread.reset();
67 _device_manager.release_device(_sdi);
70 // TODO: This should not be necessary
74 struct sr_dev_inst* SigSession::get_device() const
79 void SigSession::set_device(struct sr_dev_inst *sdi)
82 _device_manager.release_device(_sdi);
84 _device_manager.use_device(sdi, this);
89 void SigSession::release_device(struct sr_dev_inst *sdi)
93 assert(_capture_state == Stopped);
98 void SigSession::load_file(const string &name,
99 function<void (const QString)> error_handler)
103 if (sr_session_load(name.c_str()) == SR_OK) {
104 GSList *devlist = NULL;
105 sr_session_dev_list(&devlist);
107 if (!devlist || !devlist->data ||
108 sr_session_start() != SR_OK) {
109 error_handler(tr("Failed to start session."));
113 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
114 g_slist_free(devlist);
117 read_sample_rate(sdi);
119 _sampling_thread.reset(new boost::thread(
120 &SigSession::load_session_thread_proc, this,
126 if (!(in = load_input_file_format(name.c_str(),
130 update_signals(in->sdi);
131 read_sample_rate(in->sdi);
133 _sampling_thread.reset(new boost::thread(
134 &SigSession::load_input_thread_proc, this,
135 name, in, error_handler));
140 SigSession::capture_state SigSession::get_capture_state() const
142 lock_guard<mutex> lock(_sampling_mutex);
143 return _capture_state;
146 void SigSession::start_capture(uint64_t record_length,
147 function<void (const QString)> error_handler)
151 // Check that a device instance has been selected.
153 qDebug() << "No device selected";
157 // Check that at least one probe is enabled
159 for (l = _sdi->probes; l; l = l->next) {
160 sr_probe *const probe = (sr_probe*)l->data;
167 error_handler(tr("No probes enabled."));
172 _sampling_thread.reset(new boost::thread(
173 &SigSession::sample_thread_proc, this, _sdi,
174 record_length, error_handler));
177 void SigSession::stop_capture()
179 if (get_capture_state() == Stopped)
184 // Check that sampling stopped
185 if (_sampling_thread.get())
186 _sampling_thread->join();
187 _sampling_thread.reset();
190 vector< shared_ptr<view::Signal> > SigSession::get_signals()
192 lock_guard<mutex> lock(_signals_mutex);
196 boost::shared_ptr<data::Logic> SigSession::get_data()
201 void SigSession::add_decoder(srd_decoder *const dec)
204 lock_guard<mutex> lock(_signals_mutex);
205 shared_ptr<data::Decoder> decoder(
206 new data::Decoder(dec));
207 shared_ptr<view::DecodeSignal> d(
208 new view::DecodeSignal(*this, decoder));
209 _decode_traces.push_back(d);
214 void SigSession::set_capture_state(capture_state state)
216 lock_guard<mutex> lock(_sampling_mutex);
217 const bool changed = _capture_state != state;
218 _capture_state = state;
220 capture_state_changed(state);
224 * Attempts to autodetect the format. Failing that
225 * @param filename The filename of the input file.
226 * @return A pointer to the 'struct sr_input_format' that should be used,
227 * or NULL if no input format was selected or auto-detected.
229 sr_input_format* SigSession::determine_input_file_format(
230 const string &filename)
234 /* If there are no input formats, return NULL right away. */
235 sr_input_format *const *const inputs = sr_input_list();
237 g_critical("No supported input formats available.");
241 /* Otherwise, try to find an input module that can handle this file. */
242 for (i = 0; inputs[i]; i++) {
243 if (inputs[i]->format_match(filename.c_str()))
247 /* Return NULL if no input module wanted to touch this. */
249 g_critical("Error: no matching input module found.");
256 sr_input* SigSession::load_input_file_format(const string &filename,
257 function<void (const QString)> error_handler,
258 sr_input_format *format)
263 if (!format && !(format =
264 determine_input_file_format(filename.c_str()))) {
265 /* The exact cause was already logged. */
269 if (stat(filename.c_str(), &st) == -1) {
270 error_handler(tr("Failed to load file"));
274 /* Initialize the input module. */
275 if (!(in = new sr_input)) {
276 qDebug("Failed to allocate input module.\n");
282 if (in->format->init &&
283 in->format->init(in, filename.c_str()) != SR_OK) {
284 qDebug("Input format init failed.\n");
290 if (sr_session_dev_add(in->sdi) != SR_OK) {
291 qDebug("Failed to use device.\n");
292 sr_session_destroy();
299 void SigSession::update_signals(const sr_dev_inst *const sdi)
301 assert(_capture_state == Stopped);
303 shared_ptr<view::Signal> signal;
304 unsigned int logic_probe_count = 0;
305 unsigned int analog_probe_count = 0;
307 // Clear the decode traces
308 _decode_traces.clear();
310 // Detect what data types we will receive
312 for (const GSList *l = sdi->probes; l; l = l->next) {
313 const sr_probe *const probe = (const sr_probe *)l->data;
317 switch(probe->type) {
322 case SR_PROBE_ANALOG:
323 analog_probe_count++;
329 // Create data containers for the data snapshots
331 lock_guard<mutex> data_lock(_data_mutex);
334 if (logic_probe_count != 0) {
335 _logic_data.reset(new data::Logic(
340 _analog_data.reset();
341 if (analog_probe_count != 0) {
342 _analog_data.reset(new data::Analog());
343 assert(_analog_data);
347 // Make the Signals list
349 lock_guard<mutex> lock(_signals_mutex);
354 for (const GSList *l = sdi->probes; l; l = l->next) {
355 const sr_probe *const probe =
356 (const sr_probe *)l->data;
359 switch(probe->type) {
361 signal = shared_ptr<view::Signal>(
362 new view::LogicSignal(*this, probe,
366 case SR_PROBE_ANALOG:
367 signal = shared_ptr<view::Signal>(
368 new view::AnalogSignal(*this, probe,
373 _signals.push_back(signal);
381 bool SigSession::is_trigger_enabled() const
384 for (const GSList *l = _sdi->probes; l; l = l->next) {
385 const sr_probe *const p = (const sr_probe *)l->data;
387 if (p->trigger && p->trigger[0] != '\0')
394 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
397 uint64_t sample_rate = 0;
399 // Read out the sample rate
402 const int ret = sr_config_get(sdi->driver,
403 SR_CONF_SAMPLERATE, &gvar, sdi);
405 qDebug("Failed to get samplerate\n");
409 sample_rate = g_variant_get_uint64(gvar);
410 g_variant_unref(gvar);
414 _analog_data->set_samplerate(sample_rate);
416 _logic_data->set_samplerate(sample_rate);
419 void SigSession::load_session_thread_proc(
420 function<void (const QString)> error_handler)
424 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
426 set_capture_state(Running);
430 sr_session_destroy();
431 set_capture_state(Stopped);
433 // Confirm that SR_DF_END was received
434 assert(!_cur_logic_snapshot);
435 assert(!_cur_analog_snapshot);
438 void SigSession::load_input_thread_proc(const string name,
439 sr_input *in, function<void (const QString)> error_handler)
446 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
448 set_capture_state(Running);
450 in->format->loadfile(in, name.c_str());
452 sr_session_destroy();
453 set_capture_state(Stopped);
455 // Confirm that SR_DF_END was received
456 assert(!_cur_logic_snapshot);
457 assert(!_cur_analog_snapshot);
462 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
463 uint64_t record_length,
464 function<void (const QString)> error_handler)
467 assert(error_handler);
470 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
472 if (sr_session_dev_add(sdi) != SR_OK) {
473 error_handler(tr("Failed to use device."));
474 sr_session_destroy();
478 // Set the sample limit
479 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
480 g_variant_new_uint64(record_length)) != SR_OK) {
481 error_handler(tr("Failed to configure "
482 "time-based sample limit."));
483 sr_session_destroy();
487 if (sr_session_start() != SR_OK) {
488 error_handler(tr("Failed to start session."));
492 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
495 sr_session_destroy();
497 set_capture_state(Stopped);
499 // Confirm that SR_DF_END was received
500 assert(!_cur_logic_snapshot);
501 assert(!_cur_analog_snapshot);
504 void SigSession::feed_in_header(const sr_dev_inst *sdi)
506 read_sample_rate(sdi);
509 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
510 const sr_datafeed_meta &meta)
514 for (const GSList *l = meta.config; l; l = l->next) {
515 const sr_config *const src = (const sr_config*)l->data;
517 case SR_CONF_SAMPLERATE:
518 /// @todo handle samplerate changes
519 /// samplerate = (uint64_t *)src->value;
522 // Unknown metadata is not an error.
530 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
532 lock_guard<mutex> lock(_data_mutex);
536 qDebug() << "Unexpected logic packet";
540 if (!_cur_logic_snapshot)
542 set_capture_state(Running);
544 // Create a new data snapshot
545 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
546 new data::LogicSnapshot(logic));
547 _logic_data->push_snapshot(_cur_logic_snapshot);
551 // Append to the existing data snapshot
552 _cur_logic_snapshot->append_payload(logic);
558 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
560 lock_guard<mutex> lock(_data_mutex);
564 qDebug() << "Unexpected analog packet";
565 return; // This analog packet was not expected.
568 if (!_cur_analog_snapshot)
570 set_capture_state(Running);
572 // Create a new data snapshot
573 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
574 new data::AnalogSnapshot(analog));
575 _analog_data->push_snapshot(_cur_analog_snapshot);
579 // Append to the existing data snapshot
580 _cur_analog_snapshot->append_payload(analog);
586 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
587 const struct sr_datafeed_packet *packet)
592 switch (packet->type) {
598 assert(packet->payload);
600 *(const sr_datafeed_meta*)packet->payload);
604 assert(packet->payload);
605 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
609 assert(packet->payload);
610 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
616 lock_guard<mutex> lock(_data_mutex);
617 _cur_logic_snapshot.reset();
618 _cur_analog_snapshot.reset();
626 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
627 const struct sr_datafeed_packet *packet, void *cb_data)
631 _session->data_feed_in(sdi, packet);