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"
24 #include "data/analog.h"
25 #include "data/analogsnapshot.h"
26 #include "data/logic.h"
27 #include "data/logicsnapshot.h"
29 #include "view/analogsignal.h"
30 #include "view/decodesignal.h"
31 #include "view/logicsignal.h"
39 using namespace boost;
44 // TODO: This should not be necessary
45 SigSession* SigSession::_session = NULL;
47 SigSession::SigSession(DeviceManager &device_manager) :
48 _device_manager(device_manager),
50 _capture_state(Stopped)
52 // TODO: This should not be necessary
56 SigSession::~SigSession()
60 if (_sampling_thread.get())
61 _sampling_thread->join();
62 _sampling_thread.reset();
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.reset(new 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.reset(new boost::thread(
132 &SigSession::load_input_thread_proc, this,
133 name, in, error_handler));
138 SigSession::capture_state SigSession::get_capture_state() const
140 lock_guard<mutex> lock(_sampling_mutex);
141 return _capture_state;
144 void SigSession::start_capture(uint64_t record_length,
145 function<void (const QString)> error_handler)
149 // Check that a device instance has been selected.
151 qDebug() << "No device selected";
155 // Check that at least one probe is enabled
157 for (l = _sdi->probes; l; l = l->next) {
158 sr_probe *const probe = (sr_probe*)l->data;
165 error_handler(tr("No probes enabled."));
170 _sampling_thread.reset(new boost::thread(
171 &SigSession::sample_thread_proc, this, _sdi,
172 record_length, error_handler));
175 void SigSession::stop_capture()
177 if (get_capture_state() == Stopped)
182 // Check that sampling stopped
183 if (_sampling_thread.get())
184 _sampling_thread->join();
185 _sampling_thread.reset();
188 vector< shared_ptr<view::Signal> > SigSession::get_signals()
190 lock_guard<mutex> lock(_signals_mutex);
194 boost::shared_ptr<data::Logic> SigSession::get_data()
199 void SigSession::add_decoder(srd_decoder *const dec)
202 shared_ptr<view::DecodeSignal> d(new view::DecodeSignal(*this, dec));
203 _decode_traces.push_back(d);
208 void SigSession::set_capture_state(capture_state state)
210 lock_guard<mutex> lock(_sampling_mutex);
211 const bool changed = _capture_state != state;
212 _capture_state = state;
214 capture_state_changed(state);
218 * Attempts to autodetect the format. Failing that
219 * @param filename The filename of the input file.
220 * @return A pointer to the 'struct sr_input_format' that should be used,
221 * or NULL if no input format was selected or auto-detected.
223 sr_input_format* SigSession::determine_input_file_format(
224 const string &filename)
228 /* If there are no input formats, return NULL right away. */
229 sr_input_format *const *const inputs = sr_input_list();
231 g_critical("No supported input formats available.");
235 /* Otherwise, try to find an input module that can handle this file. */
236 for (i = 0; inputs[i]; i++) {
237 if (inputs[i]->format_match(filename.c_str()))
241 /* Return NULL if no input module wanted to touch this. */
243 g_critical("Error: no matching input module found.");
250 sr_input* SigSession::load_input_file_format(const string &filename,
251 function<void (const QString)> error_handler,
252 sr_input_format *format)
257 if (!format && !(format =
258 determine_input_file_format(filename.c_str()))) {
259 /* The exact cause was already logged. */
263 if (stat(filename.c_str(), &st) == -1) {
264 error_handler(tr("Failed to load file"));
268 /* Initialize the input module. */
269 if (!(in = new sr_input)) {
270 qDebug("Failed to allocate input module.\n");
276 if (in->format->init &&
277 in->format->init(in, filename.c_str()) != SR_OK) {
278 qDebug("Input format init failed.\n");
284 if (sr_session_dev_add(in->sdi) != SR_OK) {
285 qDebug("Failed to use device.\n");
286 sr_session_destroy();
293 void SigSession::update_signals(const sr_dev_inst *const sdi)
295 assert(_capture_state == Stopped);
297 shared_ptr<view::Signal> signal;
298 unsigned int logic_probe_count = 0;
299 unsigned int analog_probe_count = 0;
301 // Clear the decode traces
302 _decode_traces.clear();
304 // Detect what data types we will receive
306 for (const GSList *l = sdi->probes; l; l = l->next) {
307 const sr_probe *const probe = (const sr_probe *)l->data;
311 switch(probe->type) {
316 case SR_PROBE_ANALOG:
317 analog_probe_count++;
323 // Create data containers for the data snapshots
325 lock_guard<mutex> data_lock(_data_mutex);
328 if (logic_probe_count != 0) {
329 _logic_data.reset(new data::Logic(
334 _analog_data.reset();
335 if (analog_probe_count != 0) {
336 _analog_data.reset(new data::Analog());
337 assert(_analog_data);
341 // Make the Signals list
343 lock_guard<mutex> lock(_signals_mutex);
348 for (const GSList *l = sdi->probes; l; l = l->next) {
349 const sr_probe *const probe =
350 (const sr_probe *)l->data;
353 switch(probe->type) {
355 signal = shared_ptr<view::Signal>(
356 new view::LogicSignal(*this, probe,
360 case SR_PROBE_ANALOG:
361 signal = shared_ptr<view::Signal>(
362 new view::AnalogSignal(*this, probe,
367 _signals.push_back(signal);
375 bool SigSession::is_trigger_enabled() const
378 for (const GSList *l = _sdi->probes; l; l = l->next) {
379 const sr_probe *const p = (const sr_probe *)l->data;
381 if (p->trigger && p->trigger[0] != '\0')
388 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
391 uint64_t sample_rate = 0;
393 // Read out the sample rate
396 const int ret = sr_config_get(sdi->driver,
397 SR_CONF_SAMPLERATE, &gvar, sdi);
399 qDebug("Failed to get samplerate\n");
403 sample_rate = g_variant_get_uint64(gvar);
404 g_variant_unref(gvar);
408 _analog_data->set_samplerate(sample_rate);
410 _logic_data->set_samplerate(sample_rate);
413 void SigSession::load_session_thread_proc(
414 function<void (const QString)> error_handler)
418 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
420 set_capture_state(Running);
424 sr_session_destroy();
425 set_capture_state(Stopped);
427 // Confirm that SR_DF_END was received
428 assert(!_cur_logic_snapshot);
429 assert(!_cur_analog_snapshot);
432 void SigSession::load_input_thread_proc(const string name,
433 sr_input *in, function<void (const QString)> error_handler)
440 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
442 set_capture_state(Running);
444 in->format->loadfile(in, name.c_str());
446 sr_session_destroy();
447 set_capture_state(Stopped);
449 // Confirm that SR_DF_END was received
450 assert(!_cur_logic_snapshot);
451 assert(!_cur_analog_snapshot);
456 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
457 uint64_t record_length,
458 function<void (const QString)> error_handler)
461 assert(error_handler);
464 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
466 if (sr_session_dev_add(sdi) != SR_OK) {
467 error_handler(tr("Failed to use device."));
468 sr_session_destroy();
472 // Set the sample limit
473 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
474 g_variant_new_uint64(record_length)) != SR_OK) {
475 error_handler(tr("Failed to configure "
476 "time-based sample limit."));
477 sr_session_destroy();
481 if (sr_session_start() != SR_OK) {
482 error_handler(tr("Failed to start session."));
486 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
489 sr_session_destroy();
491 set_capture_state(Stopped);
493 // Confirm that SR_DF_END was received
494 assert(!_cur_logic_snapshot);
495 assert(!_cur_analog_snapshot);
498 void SigSession::feed_in_header(const sr_dev_inst *sdi)
500 read_sample_rate(sdi);
503 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
504 const sr_datafeed_meta &meta)
508 for (const GSList *l = meta.config; l; l = l->next) {
509 const sr_config *const src = (const sr_config*)l->data;
511 case SR_CONF_SAMPLERATE:
512 /// @todo handle samplerate changes
513 /// samplerate = (uint64_t *)src->value;
516 // Unknown metadata is not an error.
522 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
524 lock_guard<mutex> lock(_data_mutex);
528 qDebug() << "Unexpected logic packet";
532 if (!_cur_logic_snapshot)
534 set_capture_state(Running);
536 // Create a new data snapshot
537 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
538 new data::LogicSnapshot(logic));
539 _logic_data->push_snapshot(_cur_logic_snapshot);
543 // Append to the existing data snapshot
544 _cur_logic_snapshot->append_payload(logic);
550 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
552 lock_guard<mutex> lock(_data_mutex);
556 qDebug() << "Unexpected analog packet";
557 return; // This analog packet was not expected.
560 if (!_cur_analog_snapshot)
562 set_capture_state(Running);
564 // Create a new data snapshot
565 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
566 new data::AnalogSnapshot(analog));
567 _analog_data->push_snapshot(_cur_analog_snapshot);
571 // Append to the existing data snapshot
572 _cur_analog_snapshot->append_payload(analog);
578 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
579 const struct sr_datafeed_packet *packet)
584 switch (packet->type) {
590 assert(packet->payload);
592 *(const sr_datafeed_meta*)packet->payload);
596 assert(packet->payload);
597 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
601 assert(packet->payload);
602 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
608 lock_guard<mutex> lock(_data_mutex);
609 _cur_logic_snapshot.reset();
610 _cur_analog_snapshot.reset();
618 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
619 const struct sr_datafeed_packet *packet, void *cb_data)
623 _session->data_feed_in(sdi, packet);