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();
62 // TODO: This should not be necessary
66 struct sr_dev_inst* SigSession::get_device() const
71 void SigSession::set_device(struct sr_dev_inst *sdi)
74 _device_manager.release_device(_sdi);
76 _device_manager.use_device(sdi, this);
80 void SigSession::release_device(struct sr_dev_inst *sdi)
84 assert(_capture_state == Stopped);
88 void SigSession::load_file(const string &name,
89 function<void (const QString)> error_handler)
92 _sampling_thread.reset(new boost::thread(
93 &SigSession::load_thread_proc, this, name,
97 SigSession::capture_state SigSession::get_capture_state() const
99 lock_guard<mutex> lock(_sampling_mutex);
100 return _capture_state;
103 void SigSession::start_capture(uint64_t record_length,
104 function<void (const QString)> error_handler)
108 // Check that a device instance has been selected.
110 qDebug() << "No device selected";
114 // Check that at least one probe is enabled
116 for (l = _sdi->probes; l; l = l->next) {
117 sr_probe *const probe = (sr_probe*)l->data;
124 error_handler(tr("No probes enabled."));
129 _sampling_thread.reset(new boost::thread(
130 &SigSession::sample_thread_proc, this, _sdi,
131 record_length, error_handler));
134 void SigSession::stop_capture()
136 if (get_capture_state() == Stopped)
141 // Check that sampling stopped
142 if (_sampling_thread.get())
143 _sampling_thread->join();
144 _sampling_thread.reset();
147 vector< shared_ptr<view::Signal> > SigSession::get_signals()
149 lock_guard<mutex> lock(_signals_mutex);
153 boost::shared_ptr<data::Logic> SigSession::get_data()
158 void SigSession::set_capture_state(capture_state state)
160 lock_guard<mutex> lock(_sampling_mutex);
161 _capture_state = state;
162 capture_state_changed(state);
166 * Attempts to autodetect the format. Failing that
167 * @param filename The filename of the input file.
168 * @return A pointer to the 'struct sr_input_format' that should be used,
169 * or NULL if no input format was selected or auto-detected.
171 sr_input_format* SigSession::determine_input_file_format(
172 const string &filename)
176 /* If there are no input formats, return NULL right away. */
177 sr_input_format *const *const inputs = sr_input_list();
179 g_critical("No supported input formats available.");
183 /* Otherwise, try to find an input module that can handle this file. */
184 for (i = 0; inputs[i]; i++) {
185 if (inputs[i]->format_match(filename.c_str()))
189 /* Return NULL if no input module wanted to touch this. */
191 g_critical("Error: no matching input module found.");
198 sr_input* SigSession::load_input_file_format(const string &filename,
199 function<void (const QString)> error_handler,
200 sr_input_format *format)
205 if (!format && !(format =
206 determine_input_file_format(filename.c_str()))) {
207 /* The exact cause was already logged. */
211 if (stat(filename.c_str(), &st) == -1) {
212 error_handler(tr("Failed to load file"));
216 /* Initialize the input module. */
217 if (!(in = new sr_input)) {
218 qDebug("Failed to allocate input module.\n");
224 if (in->format->init &&
225 in->format->init(in, filename.c_str()) != SR_OK) {
226 qDebug("Input format init failed.\n");
232 if (sr_session_dev_add(in->sdi) != SR_OK) {
233 qDebug("Failed to use device.\n");
234 sr_session_destroy();
241 void SigSession::load_thread_proc(const string name,
242 function<void (const QString)> error_handler)
246 if (sr_session_load(name.c_str()) == SR_OK) {
247 if (sr_session_start() != SR_OK) {
248 error_handler(tr("Failed to start session."));
252 else if(!(in = load_input_file_format(name.c_str(), error_handler)))
255 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
257 set_capture_state(Running);
261 in->format->loadfile(in, name.c_str());
265 sr_session_destroy();
266 set_capture_state(Stopped);
268 // Confirm that SR_DF_END was received
269 assert(!_cur_logic_snapshot);
270 assert(!_cur_analog_snapshot);
275 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
276 uint64_t record_length,
277 function<void (const QString)> error_handler)
280 assert(error_handler);
283 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
285 if (sr_session_dev_add(sdi) != SR_OK) {
286 error_handler(tr("Failed to use device."));
287 sr_session_destroy();
291 // Set the sample limit
292 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
293 g_variant_new_uint64(record_length)) != SR_OK) {
294 error_handler(tr("Failed to configure "
295 "time-based sample limit."));
296 sr_session_destroy();
300 if (sr_session_start() != SR_OK) {
301 error_handler(tr("Failed to start session."));
305 set_capture_state(Running);
308 sr_session_destroy();
310 set_capture_state(Stopped);
312 // Confirm that SR_DF_END was received
313 assert(!_cur_logic_snapshot);
314 assert(!_cur_analog_snapshot);
317 void SigSession::feed_in_header(const sr_dev_inst *sdi)
319 shared_ptr<view::Signal> signal;
321 uint64_t sample_rate = 0;
322 unsigned int logic_probe_count = 0;
323 unsigned int analog_probe_count = 0;
325 // Detect what data types we will receive
326 for (const GSList *l = sdi->probes; l; l = l->next) {
327 const sr_probe *const probe = (const sr_probe *)l->data;
331 switch(probe->type) {
336 case SR_PROBE_ANALOG:
337 analog_probe_count++;
342 // Read out the sample rate
345 const int ret = sr_config_get(sdi->driver,
346 SR_CONF_SAMPLERATE, &gvar, sdi);
348 qDebug("Failed to get samplerate\n");
352 sample_rate = g_variant_get_uint64(gvar);
353 g_variant_unref(gvar);
356 // Create data containers for the coming data snapshots
358 lock_guard<mutex> data_lock(_data_mutex);
360 if (logic_probe_count != 0) {
361 _logic_data.reset(new data::Logic(
362 logic_probe_count, sample_rate));
366 if (analog_probe_count != 0) {
367 _analog_data.reset(new data::Analog(sample_rate));
368 assert(_analog_data);
372 // Make the logic probe list
374 lock_guard<mutex> lock(_signals_mutex);
378 for (const GSList *l = sdi->probes; l; l = l->next) {
379 const sr_probe *const probe =
380 (const sr_probe *)l->data;
385 switch(probe->type) {
387 signal = shared_ptr<view::Signal>(
388 new view::LogicSignal(probe->name,
389 _logic_data, probe->index));
392 case SR_PROBE_ANALOG:
393 signal = shared_ptr<view::Signal>(
394 new view::AnalogSignal(probe->name,
395 _analog_data, probe->index));
399 _signals.push_back(signal);
406 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
407 const sr_datafeed_meta &meta)
411 for (const GSList *l = meta.config; l; l = l->next) {
412 const sr_config *const src = (const sr_config*)l->data;
414 case SR_CONF_SAMPLERATE:
415 /// @todo handle samplerate changes
416 /// samplerate = (uint64_t *)src->value;
419 // Unknown metadata is not an error.
425 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
427 lock_guard<mutex> lock(_data_mutex);
431 qDebug() << "Unexpected logic packet";
435 if (!_cur_logic_snapshot)
437 // Create a new data snapshot
438 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
439 new data::LogicSnapshot(logic));
440 _logic_data->push_snapshot(_cur_logic_snapshot);
444 // Append to the existing data snapshot
445 _cur_logic_snapshot->append_payload(logic);
451 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
453 lock_guard<mutex> lock(_data_mutex);
457 qDebug() << "Unexpected analog packet";
458 return; // This analog packet was not expected.
461 if (!_cur_analog_snapshot)
463 // Create a new data snapshot
464 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
465 new data::AnalogSnapshot(analog));
466 _analog_data->push_snapshot(_cur_analog_snapshot);
470 // Append to the existing data snapshot
471 _cur_analog_snapshot->append_payload(analog);
477 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
478 const struct sr_datafeed_packet *packet)
483 switch (packet->type) {
489 assert(packet->payload);
491 *(const sr_datafeed_meta*)packet->payload);
495 assert(packet->payload);
496 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
500 assert(packet->payload);
501 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
507 lock_guard<mutex> lock(_data_mutex);
508 _cur_logic_snapshot.reset();
509 _cur_analog_snapshot.reset();
517 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
518 const struct sr_datafeed_packet *packet, void *cb_data)
522 _session->data_feed_in(sdi, packet);