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"
43 using namespace boost;
48 // TODO: This should not be necessary
49 SigSession* SigSession::_session = NULL;
51 SigSession::SigSession(DeviceManager &device_manager) :
52 _device_manager(device_manager),
54 _capture_state(Stopped)
56 // TODO: This should not be necessary
60 SigSession::~SigSession()
64 _sampling_thread.join();
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 = 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 = boost::thread(
134 &SigSession::load_input_thread_proc, this,
135 name, in, error_handler);
139 SigSession::capture_state SigSession::get_capture_state() const
141 lock_guard<mutex> lock(_sampling_mutex);
142 return _capture_state;
145 void SigSession::start_capture(uint64_t record_length,
146 function<void (const QString)> error_handler)
150 // Check that a device instance has been selected.
152 qDebug() << "No device selected";
156 // Check that at least one probe is enabled
158 for (l = _sdi->probes; l; l = l->next) {
159 sr_probe *const probe = (sr_probe*)l->data;
166 error_handler(tr("No probes enabled."));
171 _sampling_thread = boost::thread(
172 &SigSession::sample_thread_proc, this, _sdi,
173 record_length, error_handler);
176 void SigSession::stop_capture()
178 if (get_capture_state() == Stopped)
183 // Check that sampling stopped
184 _sampling_thread.join();
187 vector< shared_ptr<view::Signal> > SigSession::get_signals() const
189 lock_guard<mutex> lock(_signals_mutex);
193 boost::shared_ptr<data::Logic> SigSession::get_data()
198 bool SigSession::add_decoder(srd_decoder *const dec,
199 std::map<const srd_probe*,
200 boost::shared_ptr<view::Signal> > probes,
205 lock_guard<mutex> lock(_signals_mutex);
207 shared_ptr<data::Decoder> decoder(
208 new data::Decoder(dec, probes, options));
209 shared_ptr<view::DecodeSignal> d(
210 new view::DecodeSignal(*this, decoder,
211 _decode_traces.size()));
212 _decode_traces.push_back(d);
214 catch(std::runtime_error e)
224 vector< shared_ptr<view::DecodeSignal> > SigSession::get_decode_signals() const
226 lock_guard<mutex> lock(_signals_mutex);
227 return _decode_traces;
230 void SigSession::remove_decode_signal(view::DecodeSignal *signal)
232 for (vector< shared_ptr<view::DecodeSignal> >::iterator i =
233 _decode_traces.begin();
234 i != _decode_traces.end();
236 if ((*i).get() == signal)
238 _decode_traces.erase(i);
244 void SigSession::set_capture_state(capture_state state)
246 lock_guard<mutex> lock(_sampling_mutex);
247 const bool changed = _capture_state != state;
248 _capture_state = state;
250 capture_state_changed(state);
254 * Attempts to autodetect the format. Failing that
255 * @param filename The filename of the input file.
256 * @return A pointer to the 'struct sr_input_format' that should be used,
257 * or NULL if no input format was selected or auto-detected.
259 sr_input_format* SigSession::determine_input_file_format(
260 const string &filename)
264 /* If there are no input formats, return NULL right away. */
265 sr_input_format *const *const inputs = sr_input_list();
267 g_critical("No supported input formats available.");
271 /* Otherwise, try to find an input module that can handle this file. */
272 for (i = 0; inputs[i]; i++) {
273 if (inputs[i]->format_match(filename.c_str()))
277 /* Return NULL if no input module wanted to touch this. */
279 g_critical("Error: no matching input module found.");
286 sr_input* SigSession::load_input_file_format(const string &filename,
287 function<void (const QString)> error_handler,
288 sr_input_format *format)
293 if (!format && !(format =
294 determine_input_file_format(filename.c_str()))) {
295 /* The exact cause was already logged. */
299 if (stat(filename.c_str(), &st) == -1) {
300 error_handler(tr("Failed to load file"));
304 /* Initialize the input module. */
305 if (!(in = new sr_input)) {
306 qDebug("Failed to allocate input module.\n");
312 if (in->format->init &&
313 in->format->init(in, filename.c_str()) != SR_OK) {
314 qDebug("Input format init failed.\n");
320 if (sr_session_dev_add(in->sdi) != SR_OK) {
321 qDebug("Failed to use device.\n");
322 sr_session_destroy();
329 void SigSession::update_signals(const sr_dev_inst *const sdi)
331 assert(_capture_state == Stopped);
333 shared_ptr<view::Signal> signal;
334 unsigned int logic_probe_count = 0;
335 unsigned int analog_probe_count = 0;
337 // Clear the decode traces
338 _decode_traces.clear();
340 // Detect what data types we will receive
342 for (const GSList *l = sdi->probes; l; l = l->next) {
343 const sr_probe *const probe = (const sr_probe *)l->data;
347 switch(probe->type) {
352 case SR_PROBE_ANALOG:
353 analog_probe_count++;
359 // Create data containers for the data snapshots
361 lock_guard<mutex> data_lock(_data_mutex);
364 if (logic_probe_count != 0) {
365 _logic_data.reset(new data::Logic(
370 _analog_data.reset();
371 if (analog_probe_count != 0) {
372 _analog_data.reset(new data::Analog());
373 assert(_analog_data);
377 // Make the Signals list
379 lock_guard<mutex> lock(_signals_mutex);
384 for (const GSList *l = sdi->probes; l; l = l->next) {
385 sr_probe *const probe = (sr_probe *)l->data;
388 switch(probe->type) {
390 signal = shared_ptr<view::Signal>(
391 new view::LogicSignal(*this, probe,
395 case SR_PROBE_ANALOG:
396 signal = shared_ptr<view::Signal>(
397 new view::AnalogSignal(*this, probe,
402 _signals.push_back(signal);
410 bool SigSession::is_trigger_enabled() const
413 for (const GSList *l = _sdi->probes; l; l = l->next) {
414 const sr_probe *const p = (const sr_probe *)l->data;
416 if (p->trigger && p->trigger[0] != '\0')
423 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
426 uint64_t sample_rate = 0;
428 // Read out the sample rate
431 const int ret = sr_config_get(sdi->driver,
432 SR_CONF_SAMPLERATE, &gvar, sdi);
434 qDebug("Failed to get samplerate\n");
438 sample_rate = g_variant_get_uint64(gvar);
439 g_variant_unref(gvar);
443 _analog_data->set_samplerate(sample_rate);
445 _logic_data->set_samplerate(sample_rate);
448 void SigSession::load_session_thread_proc(
449 function<void (const QString)> error_handler)
453 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
455 set_capture_state(Running);
459 sr_session_destroy();
460 set_capture_state(Stopped);
462 // Confirm that SR_DF_END was received
463 assert(!_cur_logic_snapshot);
464 assert(!_cur_analog_snapshot);
467 void SigSession::load_input_thread_proc(const string name,
468 sr_input *in, function<void (const QString)> error_handler)
475 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
477 set_capture_state(Running);
479 in->format->loadfile(in, name.c_str());
481 sr_session_destroy();
482 set_capture_state(Stopped);
484 // Confirm that SR_DF_END was received
485 assert(!_cur_logic_snapshot);
486 assert(!_cur_analog_snapshot);
491 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
492 uint64_t record_length,
493 function<void (const QString)> error_handler)
496 assert(error_handler);
499 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
501 if (sr_session_dev_add(sdi) != SR_OK) {
502 error_handler(tr("Failed to use device."));
503 sr_session_destroy();
507 // Set the sample limit
508 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
509 g_variant_new_uint64(record_length)) != SR_OK) {
510 error_handler(tr("Failed to configure "
511 "time-based sample limit."));
512 sr_session_destroy();
516 if (sr_session_start() != SR_OK) {
517 error_handler(tr("Failed to start session."));
521 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
524 sr_session_destroy();
526 set_capture_state(Stopped);
528 // Confirm that SR_DF_END was received
529 assert(!_cur_logic_snapshot);
530 assert(!_cur_analog_snapshot);
533 void SigSession::feed_in_header(const sr_dev_inst *sdi)
535 read_sample_rate(sdi);
538 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
539 const sr_datafeed_meta &meta)
543 for (const GSList *l = meta.config; l; l = l->next) {
544 const sr_config *const src = (const sr_config*)l->data;
546 case SR_CONF_SAMPLERATE:
547 /// @todo handle samplerate changes
548 /// samplerate = (uint64_t *)src->value;
551 // Unknown metadata is not an error.
559 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
561 lock_guard<mutex> lock(_data_mutex);
565 qDebug() << "Unexpected logic packet";
569 if (!_cur_logic_snapshot)
571 set_capture_state(Running);
573 // Create a new data snapshot
574 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
575 new data::LogicSnapshot(logic));
576 _logic_data->push_snapshot(_cur_logic_snapshot);
580 // Append to the existing data snapshot
581 _cur_logic_snapshot->append_payload(logic);
587 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
589 lock_guard<mutex> lock(_data_mutex);
593 qDebug() << "Unexpected analog packet";
594 return; // This analog packet was not expected.
597 if (!_cur_analog_snapshot)
599 set_capture_state(Running);
601 // Create a new data snapshot
602 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
603 new data::AnalogSnapshot(analog));
604 _analog_data->push_snapshot(_cur_analog_snapshot);
608 // Append to the existing data snapshot
609 _cur_analog_snapshot->append_payload(analog);
615 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
616 const struct sr_datafeed_packet *packet)
621 switch (packet->type) {
627 assert(packet->payload);
629 *(const sr_datafeed_meta*)packet->payload);
633 assert(packet->payload);
634 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
638 assert(packet->payload);
639 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
645 lock_guard<mutex> lock(_data_mutex);
646 _cur_logic_snapshot.reset();
647 _cur_analog_snapshot.reset();
655 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
656 const struct sr_datafeed_packet *packet, void *cb_data)
660 _session->data_feed_in(sdi, packet);