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 _capture_state = state;
168 capture_state_changed(state);
172 * Attempts to autodetect the format. Failing that
173 * @param filename The filename of the input file.
174 * @return A pointer to the 'struct sr_input_format' that should be used,
175 * or NULL if no input format was selected or auto-detected.
177 sr_input_format* SigSession::determine_input_file_format(
178 const string &filename)
182 /* If there are no input formats, return NULL right away. */
183 sr_input_format *const *const inputs = sr_input_list();
185 g_critical("No supported input formats available.");
189 /* Otherwise, try to find an input module that can handle this file. */
190 for (i = 0; inputs[i]; i++) {
191 if (inputs[i]->format_match(filename.c_str()))
195 /* Return NULL if no input module wanted to touch this. */
197 g_critical("Error: no matching input module found.");
204 sr_input* SigSession::load_input_file_format(const string &filename,
205 function<void (const QString)> error_handler,
206 sr_input_format *format)
211 if (!format && !(format =
212 determine_input_file_format(filename.c_str()))) {
213 /* The exact cause was already logged. */
217 if (stat(filename.c_str(), &st) == -1) {
218 error_handler(tr("Failed to load file"));
222 /* Initialize the input module. */
223 if (!(in = new sr_input)) {
224 qDebug("Failed to allocate input module.\n");
230 if (in->format->init &&
231 in->format->init(in, filename.c_str()) != SR_OK) {
232 qDebug("Input format init failed.\n");
238 if (sr_session_dev_add(in->sdi) != SR_OK) {
239 qDebug("Failed to use device.\n");
240 sr_session_destroy();
247 void SigSession::update_signals()
249 assert(_capture_state == Stopped);
251 shared_ptr<view::Signal> signal;
252 unsigned int logic_probe_count = 0;
253 unsigned int analog_probe_count = 0;
255 // Detect what data types we will receive
257 for (const GSList *l = _sdi->probes; l; l = l->next) {
258 const sr_probe *const probe = (const sr_probe *)l->data;
262 switch(probe->type) {
267 case SR_PROBE_ANALOG:
268 analog_probe_count++;
274 // Create data containers for the data snapshots
276 lock_guard<mutex> data_lock(_data_mutex);
279 if (logic_probe_count != 0) {
280 _logic_data.reset(new data::Logic(
285 _analog_data.reset();
286 if (analog_probe_count != 0) {
287 _analog_data.reset(new data::Analog());
288 assert(_analog_data);
292 // Make the Signals list
294 lock_guard<mutex> lock(_signals_mutex);
299 for (const GSList *l = _sdi->probes; l; l = l->next) {
300 const sr_probe *const probe =
301 (const sr_probe *)l->data;
304 switch(probe->type) {
306 signal = shared_ptr<view::Signal>(
307 new view::LogicSignal(probe,
311 case SR_PROBE_ANALOG:
312 signal = shared_ptr<view::Signal>(
313 new view::AnalogSignal(probe,
318 _signals.push_back(signal);
326 void SigSession::load_thread_proc(const string name,
327 function<void (const QString)> error_handler)
331 if (sr_session_load(name.c_str()) == SR_OK) {
332 if (sr_session_start() != SR_OK) {
333 error_handler(tr("Failed to start session."));
337 else if(!(in = load_input_file_format(name.c_str(), error_handler)))
340 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
342 set_capture_state(Running);
346 in->format->loadfile(in, name.c_str());
350 sr_session_destroy();
351 set_capture_state(Stopped);
353 // Confirm that SR_DF_END was received
354 assert(!_cur_logic_snapshot);
355 assert(!_cur_analog_snapshot);
360 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
361 uint64_t record_length,
362 function<void (const QString)> error_handler)
365 assert(error_handler);
368 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
370 if (sr_session_dev_add(sdi) != SR_OK) {
371 error_handler(tr("Failed to use device."));
372 sr_session_destroy();
376 // Set the sample limit
377 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
378 g_variant_new_uint64(record_length)) != SR_OK) {
379 error_handler(tr("Failed to configure "
380 "time-based sample limit."));
381 sr_session_destroy();
385 if (sr_session_start() != SR_OK) {
386 error_handler(tr("Failed to start session."));
390 set_capture_state(Running);
393 sr_session_destroy();
395 set_capture_state(Stopped);
397 // Confirm that SR_DF_END was received
398 assert(!_cur_logic_snapshot);
399 assert(!_cur_analog_snapshot);
402 void SigSession::feed_in_header(const sr_dev_inst *sdi)
405 uint64_t sample_rate = 0;
407 // Read out the sample rate
410 const int ret = sr_config_get(sdi->driver,
411 SR_CONF_SAMPLERATE, &gvar, sdi);
413 qDebug("Failed to get samplerate\n");
417 sample_rate = g_variant_get_uint64(gvar);
418 g_variant_unref(gvar);
422 _analog_data->set_samplerate(sample_rate);
424 _logic_data->set_samplerate(sample_rate);
427 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
428 const sr_datafeed_meta &meta)
432 for (const GSList *l = meta.config; l; l = l->next) {
433 const sr_config *const src = (const sr_config*)l->data;
435 case SR_CONF_SAMPLERATE:
436 /// @todo handle samplerate changes
437 /// samplerate = (uint64_t *)src->value;
440 // Unknown metadata is not an error.
446 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
448 lock_guard<mutex> lock(_data_mutex);
452 qDebug() << "Unexpected logic packet";
456 if (!_cur_logic_snapshot)
458 // Create a new data snapshot
459 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
460 new data::LogicSnapshot(logic));
461 _logic_data->push_snapshot(_cur_logic_snapshot);
465 // Append to the existing data snapshot
466 _cur_logic_snapshot->append_payload(logic);
472 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
474 lock_guard<mutex> lock(_data_mutex);
478 qDebug() << "Unexpected analog packet";
479 return; // This analog packet was not expected.
482 if (!_cur_analog_snapshot)
484 // Create a new data snapshot
485 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
486 new data::AnalogSnapshot(analog));
487 _analog_data->push_snapshot(_cur_analog_snapshot);
491 // Append to the existing data snapshot
492 _cur_analog_snapshot->append_payload(analog);
498 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
499 const struct sr_datafeed_packet *packet)
504 switch (packet->type) {
510 assert(packet->payload);
512 *(const sr_datafeed_meta*)packet->payload);
516 assert(packet->payload);
517 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
521 assert(packet->payload);
522 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
528 lock_guard<mutex> lock(_data_mutex);
529 _cur_logic_snapshot.reset();
530 _cur_analog_snapshot.reset();
538 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
539 const struct sr_datafeed_packet *packet, void *cb_data)
543 _session->data_feed_in(sdi, packet);