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,
202 lock_guard<mutex> lock(_signals_mutex);
204 if (!_decode_traces.empty()) {
205 qDebug("Currently only one decode trace can be added "
207 _decode_traces.clear();
210 shared_ptr<data::Decoder> decoder(
211 new data::Decoder(dec, probes, options));
212 shared_ptr<view::DecodeSignal> d(
213 new view::DecodeSignal(*this, decoder,
214 _decode_traces.size()));
215 _decode_traces.push_back(d);
220 vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
222 lock_guard<mutex> lock(_signals_mutex);
223 return _decode_traces;
226 void SigSession::remove_decode_signal(view::DecodeSignal *signal)
228 for (vector< shared_ptr<view::DecodeSignal> >::iterator i =
229 _decode_traces.begin();
230 i != _decode_traces.end();
232 if ((*i).get() == signal)
234 _decode_traces.erase(i);
240 void SigSession::set_capture_state(capture_state state)
242 lock_guard<mutex> lock(_sampling_mutex);
243 const bool changed = _capture_state != state;
244 _capture_state = state;
246 capture_state_changed(state);
250 * Attempts to autodetect the format. Failing that
251 * @param filename The filename of the input file.
252 * @return A pointer to the 'struct sr_input_format' that should be used,
253 * or NULL if no input format was selected or auto-detected.
255 sr_input_format* SigSession::determine_input_file_format(
256 const string &filename)
260 /* If there are no input formats, return NULL right away. */
261 sr_input_format *const *const inputs = sr_input_list();
263 g_critical("No supported input formats available.");
267 /* Otherwise, try to find an input module that can handle this file. */
268 for (i = 0; inputs[i]; i++) {
269 if (inputs[i]->format_match(filename.c_str()))
273 /* Return NULL if no input module wanted to touch this. */
275 g_critical("Error: no matching input module found.");
282 sr_input* SigSession::load_input_file_format(const string &filename,
283 function<void (const QString)> error_handler,
284 sr_input_format *format)
289 if (!format && !(format =
290 determine_input_file_format(filename.c_str()))) {
291 /* The exact cause was already logged. */
295 if (stat(filename.c_str(), &st) == -1) {
296 error_handler(tr("Failed to load file"));
300 /* Initialize the input module. */
301 if (!(in = new sr_input)) {
302 qDebug("Failed to allocate input module.\n");
308 if (in->format->init &&
309 in->format->init(in, filename.c_str()) != SR_OK) {
310 qDebug("Input format init failed.\n");
316 if (sr_session_dev_add(in->sdi) != SR_OK) {
317 qDebug("Failed to use device.\n");
318 sr_session_destroy();
325 void SigSession::update_signals(const sr_dev_inst *const sdi)
327 assert(_capture_state == Stopped);
329 shared_ptr<view::Signal> signal;
330 unsigned int logic_probe_count = 0;
331 unsigned int analog_probe_count = 0;
333 // Clear the decode traces
334 _decode_traces.clear();
336 // Detect what data types we will receive
338 for (const GSList *l = sdi->probes; l; l = l->next) {
339 const sr_probe *const probe = (const sr_probe *)l->data;
343 switch(probe->type) {
348 case SR_PROBE_ANALOG:
349 analog_probe_count++;
355 // Create data containers for the data snapshots
357 lock_guard<mutex> data_lock(_data_mutex);
360 if (logic_probe_count != 0) {
361 _logic_data.reset(new data::Logic(
366 _analog_data.reset();
367 if (analog_probe_count != 0) {
368 _analog_data.reset(new data::Analog());
369 assert(_analog_data);
373 // Make the Signals list
375 lock_guard<mutex> lock(_signals_mutex);
380 for (const GSList *l = sdi->probes; l; l = l->next) {
381 const sr_probe *const probe =
382 (const sr_probe *)l->data;
385 switch(probe->type) {
387 signal = shared_ptr<view::Signal>(
388 new view::LogicSignal(*this, probe,
392 case SR_PROBE_ANALOG:
393 signal = shared_ptr<view::Signal>(
394 new view::AnalogSignal(*this, probe,
399 _signals.push_back(signal);
407 bool SigSession::is_trigger_enabled() const
410 for (const GSList *l = _sdi->probes; l; l = l->next) {
411 const sr_probe *const p = (const sr_probe *)l->data;
413 if (p->trigger && p->trigger[0] != '\0')
420 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
423 uint64_t sample_rate = 0;
425 // Read out the sample rate
428 const int ret = sr_config_get(sdi->driver,
429 SR_CONF_SAMPLERATE, &gvar, sdi);
431 qDebug("Failed to get samplerate\n");
435 sample_rate = g_variant_get_uint64(gvar);
436 g_variant_unref(gvar);
440 _analog_data->set_samplerate(sample_rate);
442 _logic_data->set_samplerate(sample_rate);
445 void SigSession::load_session_thread_proc(
446 function<void (const QString)> error_handler)
450 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
452 set_capture_state(Running);
456 sr_session_destroy();
457 set_capture_state(Stopped);
459 // Confirm that SR_DF_END was received
460 assert(!_cur_logic_snapshot);
461 assert(!_cur_analog_snapshot);
464 void SigSession::load_input_thread_proc(const string name,
465 sr_input *in, function<void (const QString)> error_handler)
472 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
474 set_capture_state(Running);
476 in->format->loadfile(in, name.c_str());
478 sr_session_destroy();
479 set_capture_state(Stopped);
481 // Confirm that SR_DF_END was received
482 assert(!_cur_logic_snapshot);
483 assert(!_cur_analog_snapshot);
488 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
489 uint64_t record_length,
490 function<void (const QString)> error_handler)
493 assert(error_handler);
496 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
498 if (sr_session_dev_add(sdi) != SR_OK) {
499 error_handler(tr("Failed to use device."));
500 sr_session_destroy();
504 // Set the sample limit
505 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
506 g_variant_new_uint64(record_length)) != SR_OK) {
507 error_handler(tr("Failed to configure "
508 "time-based sample limit."));
509 sr_session_destroy();
513 if (sr_session_start() != SR_OK) {
514 error_handler(tr("Failed to start session."));
518 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
521 sr_session_destroy();
523 set_capture_state(Stopped);
525 // Confirm that SR_DF_END was received
526 assert(!_cur_logic_snapshot);
527 assert(!_cur_analog_snapshot);
530 void SigSession::feed_in_header(const sr_dev_inst *sdi)
532 read_sample_rate(sdi);
535 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
536 const sr_datafeed_meta &meta)
540 for (const GSList *l = meta.config; l; l = l->next) {
541 const sr_config *const src = (const sr_config*)l->data;
543 case SR_CONF_SAMPLERATE:
544 /// @todo handle samplerate changes
545 /// samplerate = (uint64_t *)src->value;
548 // Unknown metadata is not an error.
556 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
558 lock_guard<mutex> lock(_data_mutex);
562 qDebug() << "Unexpected logic packet";
566 if (!_cur_logic_snapshot)
568 set_capture_state(Running);
570 // Create a new data snapshot
571 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
572 new data::LogicSnapshot(logic));
573 _logic_data->push_snapshot(_cur_logic_snapshot);
577 // Append to the existing data snapshot
578 _cur_logic_snapshot->append_payload(logic);
584 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
586 lock_guard<mutex> lock(_data_mutex);
590 qDebug() << "Unexpected analog packet";
591 return; // This analog packet was not expected.
594 if (!_cur_analog_snapshot)
596 set_capture_state(Running);
598 // Create a new data snapshot
599 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
600 new data::AnalogSnapshot(analog));
601 _analog_data->push_snapshot(_cur_analog_snapshot);
605 // Append to the existing data snapshot
606 _cur_analog_snapshot->append_payload(analog);
612 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
613 const struct sr_datafeed_packet *packet)
618 switch (packet->type) {
624 assert(packet->payload);
626 *(const sr_datafeed_meta*)packet->payload);
630 assert(packet->payload);
631 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
635 assert(packet->payload);
636 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
642 lock_guard<mutex> lock(_data_mutex);
643 _cur_logic_snapshot.reset();
644 _cur_analog_snapshot.reset();
652 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
653 const struct sr_datafeed_packet *packet, void *cb_data)
657 _session->data_feed_in(sdi, packet);