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,
202 std::map<const srd_probe*,
203 boost::shared_ptr<view::Signal> > probes)
206 lock_guard<mutex> lock(_signals_mutex);
207 shared_ptr<data::Decoder> decoder(
208 new data::Decoder(dec, probes));
209 shared_ptr<view::DecodeSignal> d(
210 new view::DecodeSignal(*this, decoder));
211 _decode_traces.push_back(d);
216 void SigSession::set_capture_state(capture_state state)
218 lock_guard<mutex> lock(_sampling_mutex);
219 const bool changed = _capture_state != state;
220 _capture_state = state;
222 capture_state_changed(state);
226 * Attempts to autodetect the format. Failing that
227 * @param filename The filename of the input file.
228 * @return A pointer to the 'struct sr_input_format' that should be used,
229 * or NULL if no input format was selected or auto-detected.
231 sr_input_format* SigSession::determine_input_file_format(
232 const string &filename)
236 /* If there are no input formats, return NULL right away. */
237 sr_input_format *const *const inputs = sr_input_list();
239 g_critical("No supported input formats available.");
243 /* Otherwise, try to find an input module that can handle this file. */
244 for (i = 0; inputs[i]; i++) {
245 if (inputs[i]->format_match(filename.c_str()))
249 /* Return NULL if no input module wanted to touch this. */
251 g_critical("Error: no matching input module found.");
258 sr_input* SigSession::load_input_file_format(const string &filename,
259 function<void (const QString)> error_handler,
260 sr_input_format *format)
265 if (!format && !(format =
266 determine_input_file_format(filename.c_str()))) {
267 /* The exact cause was already logged. */
271 if (stat(filename.c_str(), &st) == -1) {
272 error_handler(tr("Failed to load file"));
276 /* Initialize the input module. */
277 if (!(in = new sr_input)) {
278 qDebug("Failed to allocate input module.\n");
284 if (in->format->init &&
285 in->format->init(in, filename.c_str()) != SR_OK) {
286 qDebug("Input format init failed.\n");
292 if (sr_session_dev_add(in->sdi) != SR_OK) {
293 qDebug("Failed to use device.\n");
294 sr_session_destroy();
301 void SigSession::update_signals(const sr_dev_inst *const sdi)
303 assert(_capture_state == Stopped);
305 shared_ptr<view::Signal> signal;
306 unsigned int logic_probe_count = 0;
307 unsigned int analog_probe_count = 0;
309 // Clear the decode traces
310 _decode_traces.clear();
312 // Detect what data types we will receive
314 for (const GSList *l = sdi->probes; l; l = l->next) {
315 const sr_probe *const probe = (const sr_probe *)l->data;
319 switch(probe->type) {
324 case SR_PROBE_ANALOG:
325 analog_probe_count++;
331 // Create data containers for the data snapshots
333 lock_guard<mutex> data_lock(_data_mutex);
336 if (logic_probe_count != 0) {
337 _logic_data.reset(new data::Logic(
342 _analog_data.reset();
343 if (analog_probe_count != 0) {
344 _analog_data.reset(new data::Analog());
345 assert(_analog_data);
349 // Make the Signals list
351 lock_guard<mutex> lock(_signals_mutex);
356 for (const GSList *l = sdi->probes; l; l = l->next) {
357 const sr_probe *const probe =
358 (const sr_probe *)l->data;
361 switch(probe->type) {
363 signal = shared_ptr<view::Signal>(
364 new view::LogicSignal(*this, probe,
368 case SR_PROBE_ANALOG:
369 signal = shared_ptr<view::Signal>(
370 new view::AnalogSignal(*this, probe,
375 _signals.push_back(signal);
383 bool SigSession::is_trigger_enabled() const
386 for (const GSList *l = _sdi->probes; l; l = l->next) {
387 const sr_probe *const p = (const sr_probe *)l->data;
389 if (p->trigger && p->trigger[0] != '\0')
396 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
399 uint64_t sample_rate = 0;
401 // Read out the sample rate
404 const int ret = sr_config_get(sdi->driver,
405 SR_CONF_SAMPLERATE, &gvar, sdi);
407 qDebug("Failed to get samplerate\n");
411 sample_rate = g_variant_get_uint64(gvar);
412 g_variant_unref(gvar);
416 _analog_data->set_samplerate(sample_rate);
418 _logic_data->set_samplerate(sample_rate);
421 void SigSession::load_session_thread_proc(
422 function<void (const QString)> error_handler)
426 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
428 set_capture_state(Running);
432 sr_session_destroy();
433 set_capture_state(Stopped);
435 // Confirm that SR_DF_END was received
436 assert(!_cur_logic_snapshot);
437 assert(!_cur_analog_snapshot);
440 void SigSession::load_input_thread_proc(const string name,
441 sr_input *in, function<void (const QString)> error_handler)
448 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
450 set_capture_state(Running);
452 in->format->loadfile(in, name.c_str());
454 sr_session_destroy();
455 set_capture_state(Stopped);
457 // Confirm that SR_DF_END was received
458 assert(!_cur_logic_snapshot);
459 assert(!_cur_analog_snapshot);
464 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
465 uint64_t record_length,
466 function<void (const QString)> error_handler)
469 assert(error_handler);
472 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
474 if (sr_session_dev_add(sdi) != SR_OK) {
475 error_handler(tr("Failed to use device."));
476 sr_session_destroy();
480 // Set the sample limit
481 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
482 g_variant_new_uint64(record_length)) != SR_OK) {
483 error_handler(tr("Failed to configure "
484 "time-based sample limit."));
485 sr_session_destroy();
489 if (sr_session_start() != SR_OK) {
490 error_handler(tr("Failed to start session."));
494 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
497 sr_session_destroy();
499 set_capture_state(Stopped);
501 // Confirm that SR_DF_END was received
502 assert(!_cur_logic_snapshot);
503 assert(!_cur_analog_snapshot);
506 void SigSession::feed_in_header(const sr_dev_inst *sdi)
508 read_sample_rate(sdi);
511 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
512 const sr_datafeed_meta &meta)
516 for (const GSList *l = meta.config; l; l = l->next) {
517 const sr_config *const src = (const sr_config*)l->data;
519 case SR_CONF_SAMPLERATE:
520 /// @todo handle samplerate changes
521 /// samplerate = (uint64_t *)src->value;
524 // Unknown metadata is not an error.
532 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
534 lock_guard<mutex> lock(_data_mutex);
538 qDebug() << "Unexpected logic packet";
542 if (!_cur_logic_snapshot)
544 set_capture_state(Running);
546 // Create a new data snapshot
547 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
548 new data::LogicSnapshot(logic));
549 _logic_data->push_snapshot(_cur_logic_snapshot);
553 // Append to the existing data snapshot
554 _cur_logic_snapshot->append_payload(logic);
560 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
562 lock_guard<mutex> lock(_data_mutex);
566 qDebug() << "Unexpected analog packet";
567 return; // This analog packet was not expected.
570 if (!_cur_analog_snapshot)
572 set_capture_state(Running);
574 // Create a new data snapshot
575 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
576 new data::AnalogSnapshot(analog));
577 _analog_data->push_snapshot(_cur_analog_snapshot);
581 // Append to the existing data snapshot
582 _cur_analog_snapshot->append_payload(analog);
588 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
589 const struct sr_datafeed_packet *packet)
594 switch (packet->type) {
600 assert(packet->payload);
602 *(const sr_datafeed_meta*)packet->payload);
606 assert(packet->payload);
607 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
611 assert(packet->payload);
612 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
618 lock_guard<mutex> lock(_data_mutex);
619 _cur_logic_snapshot.reset();
620 _cur_analog_snapshot.reset();
628 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
629 const struct sr_datafeed_packet *packet, void *cb_data)
633 _session->data_feed_in(sdi, packet);