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"
28 #include "view/analogsignal.h"
29 #include "view/logicsignal.h"
37 using namespace boost;
42 // TODO: This should not be necessary
43 SigSession* SigSession::_session = NULL;
45 SigSession::SigSession(DeviceManager &device_manager) :
46 _device_manager(device_manager),
48 _capture_state(Stopped)
50 // TODO: This should not be necessary
54 SigSession::~SigSession()
58 if (_sampling_thread.get())
59 _sampling_thread->join();
60 _sampling_thread.reset();
63 _device_manager.release_device(_sdi);
66 // TODO: This should not be necessary
70 struct sr_dev_inst* SigSession::get_device() const
75 void SigSession::set_device(struct sr_dev_inst *sdi)
78 _device_manager.release_device(_sdi);
80 _device_manager.use_device(sdi, this);
85 void SigSession::release_device(struct sr_dev_inst *sdi)
89 assert(_capture_state == Stopped);
94 void SigSession::load_file(const string &name,
95 function<void (const QString)> error_handler)
99 if (sr_session_load(name.c_str()) == SR_OK) {
100 GSList *devlist = NULL;
101 sr_session_dev_list(&devlist);
103 if (!devlist || !devlist->data ||
104 sr_session_start() != SR_OK) {
105 error_handler(tr("Failed to start session."));
109 sr_dev_inst *const sdi = (sr_dev_inst*)devlist->data;
110 g_slist_free(devlist);
113 read_sample_rate(sdi);
115 _sampling_thread.reset(new boost::thread(
116 &SigSession::load_session_thread_proc, this,
122 if (!(in = load_input_file_format(name.c_str(),
126 update_signals(in->sdi);
127 read_sample_rate(in->sdi);
129 _sampling_thread.reset(new boost::thread(
130 &SigSession::load_input_thread_proc, this,
131 name, in, error_handler));
136 SigSession::capture_state SigSession::get_capture_state() const
138 lock_guard<mutex> lock(_sampling_mutex);
139 return _capture_state;
142 void SigSession::start_capture(uint64_t record_length,
143 function<void (const QString)> error_handler)
147 // Check that a device instance has been selected.
149 qDebug() << "No device selected";
153 // Check that at least one probe is enabled
155 for (l = _sdi->probes; l; l = l->next) {
156 sr_probe *const probe = (sr_probe*)l->data;
163 error_handler(tr("No probes enabled."));
168 _sampling_thread.reset(new boost::thread(
169 &SigSession::sample_thread_proc, this, _sdi,
170 record_length, error_handler));
173 void SigSession::stop_capture()
175 if (get_capture_state() == Stopped)
180 // Check that sampling stopped
181 if (_sampling_thread.get())
182 _sampling_thread->join();
183 _sampling_thread.reset();
186 vector< shared_ptr<view::Signal> > SigSession::get_signals()
188 lock_guard<mutex> lock(_signals_mutex);
192 boost::shared_ptr<data::Logic> SigSession::get_data()
197 void SigSession::set_capture_state(capture_state state)
199 lock_guard<mutex> lock(_sampling_mutex);
200 const bool changed = _capture_state != state;
201 _capture_state = state;
203 capture_state_changed(state);
207 * Attempts to autodetect the format. Failing that
208 * @param filename The filename of the input file.
209 * @return A pointer to the 'struct sr_input_format' that should be used,
210 * or NULL if no input format was selected or auto-detected.
212 sr_input_format* SigSession::determine_input_file_format(
213 const string &filename)
217 /* If there are no input formats, return NULL right away. */
218 sr_input_format *const *const inputs = sr_input_list();
220 g_critical("No supported input formats available.");
224 /* Otherwise, try to find an input module that can handle this file. */
225 for (i = 0; inputs[i]; i++) {
226 if (inputs[i]->format_match(filename.c_str()))
230 /* Return NULL if no input module wanted to touch this. */
232 g_critical("Error: no matching input module found.");
239 sr_input* SigSession::load_input_file_format(const string &filename,
240 function<void (const QString)> error_handler,
241 sr_input_format *format)
246 if (!format && !(format =
247 determine_input_file_format(filename.c_str()))) {
248 /* The exact cause was already logged. */
252 if (stat(filename.c_str(), &st) == -1) {
253 error_handler(tr("Failed to load file"));
257 /* Initialize the input module. */
258 if (!(in = new sr_input)) {
259 qDebug("Failed to allocate input module.\n");
265 if (in->format->init &&
266 in->format->init(in, filename.c_str()) != SR_OK) {
267 qDebug("Input format init failed.\n");
273 if (sr_session_dev_add(in->sdi) != SR_OK) {
274 qDebug("Failed to use device.\n");
275 sr_session_destroy();
282 void SigSession::update_signals(const sr_dev_inst *const sdi)
284 assert(_capture_state == Stopped);
286 shared_ptr<view::Signal> signal;
287 unsigned int logic_probe_count = 0;
288 unsigned int analog_probe_count = 0;
290 // Detect what data types we will receive
292 for (const GSList *l = sdi->probes; l; l = l->next) {
293 const sr_probe *const probe = (const sr_probe *)l->data;
297 switch(probe->type) {
302 case SR_PROBE_ANALOG:
303 analog_probe_count++;
309 // Create data containers for the data snapshots
311 lock_guard<mutex> data_lock(_data_mutex);
314 if (logic_probe_count != 0) {
315 _logic_data.reset(new data::Logic(
320 _analog_data.reset();
321 if (analog_probe_count != 0) {
322 _analog_data.reset(new data::Analog());
323 assert(_analog_data);
327 // Make the Signals list
329 lock_guard<mutex> lock(_signals_mutex);
334 for (const GSList *l = sdi->probes; l; l = l->next) {
335 const sr_probe *const probe =
336 (const sr_probe *)l->data;
339 switch(probe->type) {
341 signal = shared_ptr<view::Signal>(
342 new view::LogicSignal(*this, probe,
346 case SR_PROBE_ANALOG:
347 signal = shared_ptr<view::Signal>(
348 new view::AnalogSignal(*this, probe,
353 _signals.push_back(signal);
361 bool SigSession::is_trigger_enabled() const
364 for (const GSList *l = _sdi->probes; l; l = l->next) {
365 const sr_probe *const p = (const sr_probe *)l->data;
367 if (p->trigger && p->trigger[0] != '\0')
374 void SigSession::read_sample_rate(const sr_dev_inst *const sdi)
377 uint64_t sample_rate = 0;
379 // Read out the sample rate
382 const int ret = sr_config_get(sdi->driver,
383 SR_CONF_SAMPLERATE, &gvar, sdi);
385 qDebug("Failed to get samplerate\n");
389 sample_rate = g_variant_get_uint64(gvar);
390 g_variant_unref(gvar);
394 _analog_data->set_samplerate(sample_rate);
396 _logic_data->set_samplerate(sample_rate);
399 void SigSession::load_session_thread_proc(
400 function<void (const QString)> error_handler)
404 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
406 set_capture_state(Running);
410 sr_session_destroy();
411 set_capture_state(Stopped);
413 // Confirm that SR_DF_END was received
414 assert(!_cur_logic_snapshot);
415 assert(!_cur_analog_snapshot);
418 void SigSession::load_input_thread_proc(const string name,
419 sr_input *in, function<void (const QString)> error_handler)
426 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
428 set_capture_state(Running);
430 in->format->loadfile(in, name.c_str());
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);
442 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
443 uint64_t record_length,
444 function<void (const QString)> error_handler)
447 assert(error_handler);
450 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
452 if (sr_session_dev_add(sdi) != SR_OK) {
453 error_handler(tr("Failed to use device."));
454 sr_session_destroy();
458 // Set the sample limit
459 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
460 g_variant_new_uint64(record_length)) != SR_OK) {
461 error_handler(tr("Failed to configure "
462 "time-based sample limit."));
463 sr_session_destroy();
467 if (sr_session_start() != SR_OK) {
468 error_handler(tr("Failed to start session."));
472 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
475 sr_session_destroy();
477 set_capture_state(Stopped);
479 // Confirm that SR_DF_END was received
480 assert(!_cur_logic_snapshot);
481 assert(!_cur_analog_snapshot);
484 void SigSession::feed_in_header(const sr_dev_inst *sdi)
486 read_sample_rate(sdi);
489 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
490 const sr_datafeed_meta &meta)
494 for (const GSList *l = meta.config; l; l = l->next) {
495 const sr_config *const src = (const sr_config*)l->data;
497 case SR_CONF_SAMPLERATE:
498 /// @todo handle samplerate changes
499 /// samplerate = (uint64_t *)src->value;
502 // Unknown metadata is not an error.
508 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
510 lock_guard<mutex> lock(_data_mutex);
514 qDebug() << "Unexpected logic packet";
518 if (!_cur_logic_snapshot)
520 set_capture_state(Running);
522 // Create a new data snapshot
523 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
524 new data::LogicSnapshot(logic));
525 _logic_data->push_snapshot(_cur_logic_snapshot);
529 // Append to the existing data snapshot
530 _cur_logic_snapshot->append_payload(logic);
536 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
538 lock_guard<mutex> lock(_data_mutex);
542 qDebug() << "Unexpected analog packet";
543 return; // This analog packet was not expected.
546 if (!_cur_analog_snapshot)
548 set_capture_state(Running);
550 // Create a new data snapshot
551 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
552 new data::AnalogSnapshot(analog));
553 _analog_data->push_snapshot(_cur_analog_snapshot);
557 // Append to the existing data snapshot
558 _cur_analog_snapshot->append_payload(analog);
564 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
565 const struct sr_datafeed_packet *packet)
570 switch (packet->type) {
576 assert(packet->payload);
578 *(const sr_datafeed_meta*)packet->payload);
582 assert(packet->payload);
583 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
587 assert(packet->payload);
588 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
594 lock_guard<mutex> lock(_data_mutex);
595 _cur_logic_snapshot.reset();
596 _cur_analog_snapshot.reset();
604 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
605 const struct sr_datafeed_packet *packet, void *cb_data)
609 _session->data_feed_in(sdi, packet);