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() const
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 vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
218 lock_guard<mutex> lock(_signals_mutex);
219 return _decode_traces;
222 void SigSession::set_capture_state(capture_state state)
224 lock_guard<mutex> lock(_sampling_mutex);
225 const bool changed = _capture_state != state;
226 _capture_state = state;
228 capture_state_changed(state);
232 * Attempts to autodetect the format. Failing that
233 * @param filename The filename of the input file.
234 * @return A pointer to the 'struct sr_input_format' that should be used,
235 * or NULL if no input format was selected or auto-detected.
237 sr_input_format* SigSession::determine_input_file_format(
238 const string &filename)
242 /* If there are no input formats, return NULL right away. */
243 sr_input_format *const *const inputs = sr_input_list();
245 g_critical("No supported input formats available.");
249 /* Otherwise, try to find an input module that can handle this file. */
250 for (i = 0; inputs[i]; i++) {
251 if (inputs[i]->format_match(filename.c_str()))
255 /* Return NULL if no input module wanted to touch this. */
257 g_critical("Error: no matching input module found.");
264 sr_input* SigSession::load_input_file_format(const string &filename,
265 function<void (const QString)> error_handler,
266 sr_input_format *format)
271 if (!format && !(format =
272 determine_input_file_format(filename.c_str()))) {
273 /* The exact cause was already logged. */
277 if (stat(filename.c_str(), &st) == -1) {
278 error_handler(tr("Failed to load file"));
282 /* Initialize the input module. */
283 if (!(in = new sr_input)) {
284 qDebug("Failed to allocate input module.\n");
290 if (in->format->init &&
291 in->format->init(in, filename.c_str()) != SR_OK) {
292 qDebug("Input format init failed.\n");
298 if (sr_session_dev_add(in->sdi) != SR_OK) {
299 qDebug("Failed to use device.\n");
300 sr_session_destroy();
307 void SigSession::update_signals(const sr_dev_inst *const sdi)
309 assert(_capture_state == Stopped);
311 shared_ptr<view::Signal> signal;
312 unsigned int logic_probe_count = 0;
313 unsigned int analog_probe_count = 0;
315 // Clear the decode traces
316 _decode_traces.clear();
318 // Detect what data types we will receive
320 for (const GSList *l = sdi->probes; l; l = l->next) {
321 const sr_probe *const probe = (const sr_probe *)l->data;
325 switch(probe->type) {
330 case SR_PROBE_ANALOG:
331 analog_probe_count++;
337 // Create data containers for the data snapshots
339 lock_guard<mutex> data_lock(_data_mutex);
342 if (logic_probe_count != 0) {
343 _logic_data.reset(new data::Logic(
348 _analog_data.reset();
349 if (analog_probe_count != 0) {
350 _analog_data.reset(new data::Analog());
351 assert(_analog_data);
355 // Make the Signals list
357 lock_guard<mutex> lock(_signals_mutex);
362 for (const GSList *l = sdi->probes; l; l = l->next) {
363 const sr_probe *const probe =
364 (const sr_probe *)l->data;
367 switch(probe->type) {
369 signal = shared_ptr<view::Signal>(
370 new view::LogicSignal(*this, probe,
374 case SR_PROBE_ANALOG:
375 signal = shared_ptr<view::Signal>(
376 new view::AnalogSignal(*this, probe,
381 _signals.push_back(signal);
389 bool SigSession::is_trigger_enabled() const
392 for (const GSList *l = _sdi->probes; l; l = l->next) {
393 const sr_probe *const p = (const sr_probe *)l->data;
395 if (p->trigger && p->trigger[0] != '\0')
402 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
405 uint64_t sample_rate = 0;
407 // Read out the sample rate
410 const int ret = sr_config_get(sdi->driver,
411 SR_CONF_SAMPLERATE, &gvar, sdi);
413 qDebug("Failed to get samplerate\n");
417 sample_rate = g_variant_get_uint64(gvar);
418 g_variant_unref(gvar);
422 _analog_data->set_samplerate(sample_rate);
424 _logic_data->set_samplerate(sample_rate);
427 void SigSession::load_session_thread_proc(
428 function<void (const QString)> error_handler)
432 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
434 set_capture_state(Running);
438 sr_session_destroy();
439 set_capture_state(Stopped);
441 // Confirm that SR_DF_END was received
442 assert(!_cur_logic_snapshot);
443 assert(!_cur_analog_snapshot);
446 void SigSession::load_input_thread_proc(const string name,
447 sr_input *in, function<void (const QString)> error_handler)
454 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
456 set_capture_state(Running);
458 in->format->loadfile(in, name.c_str());
460 sr_session_destroy();
461 set_capture_state(Stopped);
463 // Confirm that SR_DF_END was received
464 assert(!_cur_logic_snapshot);
465 assert(!_cur_analog_snapshot);
470 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
471 uint64_t record_length,
472 function<void (const QString)> error_handler)
475 assert(error_handler);
478 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
480 if (sr_session_dev_add(sdi) != SR_OK) {
481 error_handler(tr("Failed to use device."));
482 sr_session_destroy();
486 // Set the sample limit
487 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
488 g_variant_new_uint64(record_length)) != SR_OK) {
489 error_handler(tr("Failed to configure "
490 "time-based sample limit."));
491 sr_session_destroy();
495 if (sr_session_start() != SR_OK) {
496 error_handler(tr("Failed to start session."));
500 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
503 sr_session_destroy();
505 set_capture_state(Stopped);
507 // Confirm that SR_DF_END was received
508 assert(!_cur_logic_snapshot);
509 assert(!_cur_analog_snapshot);
512 void SigSession::feed_in_header(const sr_dev_inst *sdi)
514 read_sample_rate(sdi);
517 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
518 const sr_datafeed_meta &meta)
522 for (const GSList *l = meta.config; l; l = l->next) {
523 const sr_config *const src = (const sr_config*)l->data;
525 case SR_CONF_SAMPLERATE:
526 /// @todo handle samplerate changes
527 /// samplerate = (uint64_t *)src->value;
530 // Unknown metadata is not an error.
538 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
540 lock_guard<mutex> lock(_data_mutex);
544 qDebug() << "Unexpected logic packet";
548 if (!_cur_logic_snapshot)
550 set_capture_state(Running);
552 // Create a new data snapshot
553 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
554 new data::LogicSnapshot(logic));
555 _logic_data->push_snapshot(_cur_logic_snapshot);
559 // Append to the existing data snapshot
560 _cur_logic_snapshot->append_payload(logic);
566 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
568 lock_guard<mutex> lock(_data_mutex);
572 qDebug() << "Unexpected analog packet";
573 return; // This analog packet was not expected.
576 if (!_cur_analog_snapshot)
578 set_capture_state(Running);
580 // Create a new data snapshot
581 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
582 new data::AnalogSnapshot(analog));
583 _analog_data->push_snapshot(_cur_analog_snapshot);
587 // Append to the existing data snapshot
588 _cur_analog_snapshot->append_payload(analog);
594 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
595 const struct sr_datafeed_packet *packet)
600 switch (packet->type) {
606 assert(packet->payload);
608 *(const sr_datafeed_meta*)packet->payload);
612 assert(packet->payload);
613 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
617 assert(packet->payload);
618 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
624 lock_guard<mutex> lock(_data_mutex);
625 _cur_logic_snapshot.reset();
626 _cur_analog_snapshot.reset();
634 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
635 const struct sr_datafeed_packet *packet, void *cb_data)
639 _session->data_feed_in(sdi, packet);