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 update_signals((sr_dev_inst*)devlist->data);
110 g_slist_free(devlist);
112 _sampling_thread.reset(new boost::thread(
113 &SigSession::load_session_thread_proc, this,
119 if (!(in = load_input_file_format(name.c_str(),
123 update_signals(in->sdi);
125 _sampling_thread.reset(new boost::thread(
126 &SigSession::load_input_thread_proc, this,
127 name, in, error_handler));
132 SigSession::capture_state SigSession::get_capture_state() const
134 lock_guard<mutex> lock(_sampling_mutex);
135 return _capture_state;
138 void SigSession::start_capture(uint64_t record_length,
139 function<void (const QString)> error_handler)
143 // Check that a device instance has been selected.
145 qDebug() << "No device selected";
149 // Check that at least one probe is enabled
151 for (l = _sdi->probes; l; l = l->next) {
152 sr_probe *const probe = (sr_probe*)l->data;
159 error_handler(tr("No probes enabled."));
164 _sampling_thread.reset(new boost::thread(
165 &SigSession::sample_thread_proc, this, _sdi,
166 record_length, error_handler));
169 void SigSession::stop_capture()
171 if (get_capture_state() == Stopped)
176 // Check that sampling stopped
177 if (_sampling_thread.get())
178 _sampling_thread->join();
179 _sampling_thread.reset();
182 vector< shared_ptr<view::Signal> > SigSession::get_signals()
184 lock_guard<mutex> lock(_signals_mutex);
188 boost::shared_ptr<data::Logic> SigSession::get_data()
193 void SigSession::set_capture_state(capture_state state)
195 lock_guard<mutex> lock(_sampling_mutex);
196 const bool changed = _capture_state != state;
197 _capture_state = state;
199 capture_state_changed(state);
203 * Attempts to autodetect the format. Failing that
204 * @param filename The filename of the input file.
205 * @return A pointer to the 'struct sr_input_format' that should be used,
206 * or NULL if no input format was selected or auto-detected.
208 sr_input_format* SigSession::determine_input_file_format(
209 const string &filename)
213 /* If there are no input formats, return NULL right away. */
214 sr_input_format *const *const inputs = sr_input_list();
216 g_critical("No supported input formats available.");
220 /* Otherwise, try to find an input module that can handle this file. */
221 for (i = 0; inputs[i]; i++) {
222 if (inputs[i]->format_match(filename.c_str()))
226 /* Return NULL if no input module wanted to touch this. */
228 g_critical("Error: no matching input module found.");
235 sr_input* SigSession::load_input_file_format(const string &filename,
236 function<void (const QString)> error_handler,
237 sr_input_format *format)
242 if (!format && !(format =
243 determine_input_file_format(filename.c_str()))) {
244 /* The exact cause was already logged. */
248 if (stat(filename.c_str(), &st) == -1) {
249 error_handler(tr("Failed to load file"));
253 /* Initialize the input module. */
254 if (!(in = new sr_input)) {
255 qDebug("Failed to allocate input module.\n");
261 if (in->format->init &&
262 in->format->init(in, filename.c_str()) != SR_OK) {
263 qDebug("Input format init failed.\n");
269 if (sr_session_dev_add(in->sdi) != SR_OK) {
270 qDebug("Failed to use device.\n");
271 sr_session_destroy();
278 void SigSession::update_signals(const sr_dev_inst *const sdi)
280 assert(_capture_state == Stopped);
282 shared_ptr<view::Signal> signal;
283 unsigned int logic_probe_count = 0;
284 unsigned int analog_probe_count = 0;
286 // Detect what data types we will receive
288 for (const GSList *l = sdi->probes; l; l = l->next) {
289 const sr_probe *const probe = (const sr_probe *)l->data;
293 switch(probe->type) {
298 case SR_PROBE_ANALOG:
299 analog_probe_count++;
305 // Create data containers for the data snapshots
307 lock_guard<mutex> data_lock(_data_mutex);
310 if (logic_probe_count != 0) {
311 _logic_data.reset(new data::Logic(
316 _analog_data.reset();
317 if (analog_probe_count != 0) {
318 _analog_data.reset(new data::Analog());
319 assert(_analog_data);
323 // Make the Signals list
325 lock_guard<mutex> lock(_signals_mutex);
330 for (const GSList *l = sdi->probes; l; l = l->next) {
331 const sr_probe *const probe =
332 (const sr_probe *)l->data;
335 switch(probe->type) {
337 signal = shared_ptr<view::Signal>(
338 new view::LogicSignal(*this, probe,
342 case SR_PROBE_ANALOG:
343 signal = shared_ptr<view::Signal>(
344 new view::AnalogSignal(*this, probe,
349 _signals.push_back(signal);
357 bool SigSession::is_trigger_enabled() const
360 for (const GSList *l = _sdi->probes; l; l = l->next) {
361 const sr_probe *const p = (const sr_probe *)l->data;
363 if (p->trigger && p->trigger[0] != '\0')
370 void SigSession::load_session_thread_proc(
371 function<void (const QString)> error_handler)
375 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
377 set_capture_state(Running);
381 sr_session_destroy();
382 set_capture_state(Stopped);
384 // Confirm that SR_DF_END was received
385 assert(!_cur_logic_snapshot);
386 assert(!_cur_analog_snapshot);
389 void SigSession::load_input_thread_proc(const string name,
390 sr_input *in, function<void (const QString)> error_handler)
397 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
399 set_capture_state(Running);
401 in->format->loadfile(in, name.c_str());
403 sr_session_destroy();
404 set_capture_state(Stopped);
406 // Confirm that SR_DF_END was received
407 assert(!_cur_logic_snapshot);
408 assert(!_cur_analog_snapshot);
413 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
414 uint64_t record_length,
415 function<void (const QString)> error_handler)
418 assert(error_handler);
421 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
423 if (sr_session_dev_add(sdi) != SR_OK) {
424 error_handler(tr("Failed to use device."));
425 sr_session_destroy();
429 // Set the sample limit
430 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
431 g_variant_new_uint64(record_length)) != SR_OK) {
432 error_handler(tr("Failed to configure "
433 "time-based sample limit."));
434 sr_session_destroy();
438 if (sr_session_start() != SR_OK) {
439 error_handler(tr("Failed to start session."));
443 set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running);
446 sr_session_destroy();
448 set_capture_state(Stopped);
450 // Confirm that SR_DF_END was received
451 assert(!_cur_logic_snapshot);
452 assert(!_cur_analog_snapshot);
455 void SigSession::feed_in_header(const sr_dev_inst *sdi)
458 uint64_t sample_rate = 0;
460 // Read out the sample rate
463 const int ret = sr_config_get(sdi->driver,
464 SR_CONF_SAMPLERATE, &gvar, sdi);
466 qDebug("Failed to get samplerate\n");
470 sample_rate = g_variant_get_uint64(gvar);
471 g_variant_unref(gvar);
475 _analog_data->set_samplerate(sample_rate);
477 _logic_data->set_samplerate(sample_rate);
480 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
481 const sr_datafeed_meta &meta)
485 for (const GSList *l = meta.config; l; l = l->next) {
486 const sr_config *const src = (const sr_config*)l->data;
488 case SR_CONF_SAMPLERATE:
489 /// @todo handle samplerate changes
490 /// samplerate = (uint64_t *)src->value;
493 // Unknown metadata is not an error.
499 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
501 lock_guard<mutex> lock(_data_mutex);
505 qDebug() << "Unexpected logic packet";
509 if (!_cur_logic_snapshot)
511 set_capture_state(Running);
513 // Create a new data snapshot
514 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
515 new data::LogicSnapshot(logic));
516 _logic_data->push_snapshot(_cur_logic_snapshot);
520 // Append to the existing data snapshot
521 _cur_logic_snapshot->append_payload(logic);
527 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
529 lock_guard<mutex> lock(_data_mutex);
533 qDebug() << "Unexpected analog packet";
534 return; // This analog packet was not expected.
537 if (!_cur_analog_snapshot)
539 set_capture_state(Running);
541 // Create a new data snapshot
542 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
543 new data::AnalogSnapshot(analog));
544 _analog_data->push_snapshot(_cur_analog_snapshot);
548 // Append to the existing data snapshot
549 _cur_analog_snapshot->append_payload(analog);
555 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
556 const struct sr_datafeed_packet *packet)
561 switch (packet->type) {
567 assert(packet->payload);
569 *(const sr_datafeed_meta*)packet->payload);
573 assert(packet->payload);
574 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
578 assert(packet->payload);
579 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
585 lock_guard<mutex> lock(_data_mutex);
586 _cur_logic_snapshot.reset();
587 _cur_analog_snapshot.reset();
595 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
596 const struct sr_datafeed_packet *packet, void *cb_data)
600 _session->data_feed_in(sdi, packet);