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)
98 _sampling_thread.reset(new boost::thread(
99 &SigSession::load_thread_proc, this, name,
103 SigSession::capture_state SigSession::get_capture_state() const
105 lock_guard<mutex> lock(_sampling_mutex);
106 return _capture_state;
109 void SigSession::start_capture(uint64_t record_length,
110 function<void (const QString)> error_handler)
114 // Check that a device instance has been selected.
116 qDebug() << "No device selected";
120 // Check that at least one probe is enabled
122 for (l = _sdi->probes; l; l = l->next) {
123 sr_probe *const probe = (sr_probe*)l->data;
130 error_handler(tr("No probes enabled."));
135 _sampling_thread.reset(new boost::thread(
136 &SigSession::sample_thread_proc, this, _sdi,
137 record_length, error_handler));
140 void SigSession::stop_capture()
142 if (get_capture_state() == Stopped)
147 // Check that sampling stopped
148 if (_sampling_thread.get())
149 _sampling_thread->join();
150 _sampling_thread.reset();
153 vector< shared_ptr<view::Signal> > SigSession::get_signals()
155 lock_guard<mutex> lock(_signals_mutex);
159 boost::shared_ptr<data::Logic> SigSession::get_data()
164 void SigSession::set_capture_state(capture_state state)
166 lock_guard<mutex> lock(_sampling_mutex);
167 const bool changed = _capture_state != state;
168 _capture_state = state;
170 capture_state_changed(state);
174 * Attempts to autodetect the format. Failing that
175 * @param filename The filename of the input file.
176 * @return A pointer to the 'struct sr_input_format' that should be used,
177 * or NULL if no input format was selected or auto-detected.
179 sr_input_format* SigSession::determine_input_file_format(
180 const string &filename)
184 /* If there are no input formats, return NULL right away. */
185 sr_input_format *const *const inputs = sr_input_list();
187 g_critical("No supported input formats available.");
191 /* Otherwise, try to find an input module that can handle this file. */
192 for (i = 0; inputs[i]; i++) {
193 if (inputs[i]->format_match(filename.c_str()))
197 /* Return NULL if no input module wanted to touch this. */
199 g_critical("Error: no matching input module found.");
206 sr_input* SigSession::load_input_file_format(const string &filename,
207 function<void (const QString)> error_handler,
208 sr_input_format *format)
213 if (!format && !(format =
214 determine_input_file_format(filename.c_str()))) {
215 /* The exact cause was already logged. */
219 if (stat(filename.c_str(), &st) == -1) {
220 error_handler(tr("Failed to load file"));
224 /* Initialize the input module. */
225 if (!(in = new sr_input)) {
226 qDebug("Failed to allocate input module.\n");
232 if (in->format->init &&
233 in->format->init(in, filename.c_str()) != SR_OK) {
234 qDebug("Input format init failed.\n");
240 if (sr_session_dev_add(in->sdi) != SR_OK) {
241 qDebug("Failed to use device.\n");
242 sr_session_destroy();
249 void SigSession::update_signals()
251 assert(_capture_state == Stopped);
253 shared_ptr<view::Signal> signal;
254 unsigned int logic_probe_count = 0;
255 unsigned int analog_probe_count = 0;
257 // Detect what data types we will receive
259 for (const GSList *l = _sdi->probes; l; l = l->next) {
260 const sr_probe *const probe = (const sr_probe *)l->data;
264 switch(probe->type) {
269 case SR_PROBE_ANALOG:
270 analog_probe_count++;
276 // Create data containers for the data snapshots
278 lock_guard<mutex> data_lock(_data_mutex);
281 if (logic_probe_count != 0) {
282 _logic_data.reset(new data::Logic(
287 _analog_data.reset();
288 if (analog_probe_count != 0) {
289 _analog_data.reset(new data::Analog());
290 assert(_analog_data);
294 // Make the Signals list
296 lock_guard<mutex> lock(_signals_mutex);
301 for (const GSList *l = _sdi->probes; l; l = l->next) {
302 const sr_probe *const probe =
303 (const sr_probe *)l->data;
306 switch(probe->type) {
308 signal = shared_ptr<view::Signal>(
309 new view::LogicSignal(*this, probe,
313 case SR_PROBE_ANALOG:
314 signal = shared_ptr<view::Signal>(
315 new view::AnalogSignal(*this, probe,
320 _signals.push_back(signal);
328 bool SigSession::is_trigger_enabled() const
331 for (const GSList *l = _sdi->probes; l; l = l->next) {
332 const sr_probe *const p = (const sr_probe *)l->data;
334 if (p->trigger && p->trigger[0] != '\0')
341 void SigSession::load_thread_proc(const string name,
342 function<void (const QString)> error_handler)
346 if (sr_session_load(name.c_str()) == SR_OK) {
347 if (sr_session_start() != SR_OK) {
348 error_handler(tr("Failed to start session."));
352 else if(!(in = load_input_file_format(name.c_str(), error_handler)))
355 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
357 set_capture_state(Running);
361 in->format->loadfile(in, name.c_str());
365 sr_session_destroy();
366 set_capture_state(Stopped);
368 // Confirm that SR_DF_END was received
369 assert(!_cur_logic_snapshot);
370 assert(!_cur_analog_snapshot);
375 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
376 uint64_t record_length,
377 function<void (const QString)> error_handler)
380 assert(error_handler);
383 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
385 if (sr_session_dev_add(sdi) != SR_OK) {
386 error_handler(tr("Failed to use device."));
387 sr_session_destroy();
391 // Set the sample limit
392 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
393 g_variant_new_uint64(record_length)) != SR_OK) {
394 error_handler(tr("Failed to configure "
395 "time-based sample limit."));
396 sr_session_destroy();
400 if (sr_session_start() != SR_OK) {
401 error_handler(tr("Failed to start session."));
405 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
408 sr_session_destroy();
410 set_capture_state(Stopped);
412 // Confirm that SR_DF_END was received
413 assert(!_cur_logic_snapshot);
414 assert(!_cur_analog_snapshot);
417 void SigSession::feed_in_header(const sr_dev_inst *sdi)
420 uint64_t sample_rate = 0;
422 // Read out the sample rate
425 const int ret = sr_config_get(sdi->driver,
426 SR_CONF_SAMPLERATE, &gvar, sdi);
428 qDebug("Failed to get samplerate\n");
432 sample_rate = g_variant_get_uint64(gvar);
433 g_variant_unref(gvar);
437 _analog_data->set_samplerate(sample_rate);
439 _logic_data->set_samplerate(sample_rate);
442 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
443 const sr_datafeed_meta &meta)
447 for (const GSList *l = meta.config; l; l = l->next) {
448 const sr_config *const src = (const sr_config*)l->data;
450 case SR_CONF_SAMPLERATE:
451 /// @todo handle samplerate changes
452 /// samplerate = (uint64_t *)src->value;
455 // Unknown metadata is not an error.
461 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
463 lock_guard<mutex> lock(_data_mutex);
467 qDebug() << "Unexpected logic packet";
471 if (!_cur_logic_snapshot)
473 set_capture_state(Running);
475 // Create a new data snapshot
476 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
477 new data::LogicSnapshot(logic));
478 _logic_data->push_snapshot(_cur_logic_snapshot);
482 // Append to the existing data snapshot
483 _cur_logic_snapshot->append_payload(logic);
489 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
491 lock_guard<mutex> lock(_data_mutex);
495 qDebug() << "Unexpected analog packet";
496 return; // This analog packet was not expected.
499 if (!_cur_analog_snapshot)
501 set_capture_state(Running);
503 // Create a new data snapshot
504 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
505 new data::AnalogSnapshot(analog));
506 _analog_data->push_snapshot(_cur_analog_snapshot);
510 // Append to the existing data snapshot
511 _cur_analog_snapshot->append_payload(analog);
517 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
518 const struct sr_datafeed_packet *packet)
523 switch (packet->type) {
529 assert(packet->payload);
531 *(const sr_datafeed_meta*)packet->payload);
535 assert(packet->payload);
536 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
540 assert(packet->payload);
541 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
547 lock_guard<mutex> lock(_data_mutex);
548 _cur_logic_snapshot.reset();
549 _cur_analog_snapshot.reset();
557 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
558 const struct sr_datafeed_packet *packet, void *cb_data)
562 _session->data_feed_in(sdi, packet);