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"
35 using namespace boost;
40 // TODO: This should not be necessary
41 SigSession* SigSession::_session = NULL;
43 SigSession::SigSession(DeviceManager &device_manager) :
44 _device_manager(device_manager),
46 _capture_state(Stopped)
48 // TODO: This should not be necessary
52 SigSession::~SigSession()
56 if (_sampling_thread.get())
57 _sampling_thread->join();
58 _sampling_thread.reset();
60 // TODO: This should not be necessary
64 struct sr_dev_inst* SigSession::get_device() const
69 void SigSession::set_device(struct sr_dev_inst *sdi)
72 _device_manager.release_device(_sdi);
74 _device_manager.use_device(sdi, this);
78 void SigSession::release_device(struct sr_dev_inst *sdi)
82 assert(_capture_state == Stopped);
86 void SigSession::load_file(const string &name,
87 function<void (const QString)> error_handler)
90 _sampling_thread.reset(new boost::thread(
91 &SigSession::load_thread_proc, this, name,
95 SigSession::capture_state SigSession::get_capture_state() const
97 lock_guard<mutex> lock(_sampling_mutex);
98 return _capture_state;
101 void SigSession::start_capture(uint64_t record_length,
102 function<void (const QString)> error_handler)
106 // Check that a device instance has been selected.
108 qDebug() << "No device selected";
112 // Check that at least one probe is enabled
114 for (l = _sdi->probes; l; l = l->next) {
115 sr_probe *const probe = (sr_probe*)l->data;
122 error_handler(tr("No probes enabled."));
127 _sampling_thread.reset(new boost::thread(
128 &SigSession::sample_thread_proc, this, _sdi,
129 record_length, error_handler));
132 void SigSession::stop_capture()
134 if (get_capture_state() == Stopped)
139 // Check that sampling stopped
140 if (_sampling_thread.get())
141 _sampling_thread->join();
142 _sampling_thread.reset();
145 vector< shared_ptr<view::Signal> > SigSession::get_signals()
147 lock_guard<mutex> lock(_signals_mutex);
151 boost::shared_ptr<data::Logic> SigSession::get_data()
156 void SigSession::set_capture_state(capture_state state)
158 lock_guard<mutex> lock(_sampling_mutex);
159 _capture_state = state;
160 capture_state_changed(state);
163 void SigSession::load_thread_proc(const string name,
164 function<void (const QString)> error_handler)
166 if (sr_session_load(name.c_str()) != SR_OK) {
167 error_handler(tr("Failed to load file."));
171 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
173 if (sr_session_start() != SR_OK) {
174 error_handler(tr("Failed to start session."));
178 set_capture_state(Running);
181 sr_session_destroy();
183 set_capture_state(Stopped);
185 // Confirm that SR_DF_END was received
186 assert(!_cur_logic_snapshot);
187 assert(!_cur_analog_snapshot);
190 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
191 uint64_t record_length,
192 function<void (const QString)> error_handler)
195 assert(error_handler);
198 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
200 if (sr_session_dev_add(sdi) != SR_OK) {
201 error_handler(tr("Failed to use device."));
202 sr_session_destroy();
206 // Set the sample limit
207 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
208 g_variant_new_uint64(record_length)) != SR_OK) {
209 error_handler(tr("Failed to configure "
210 "time-based sample limit."));
211 sr_session_destroy();
215 if (sr_session_start() != SR_OK) {
216 error_handler(tr("Failed to start session."));
220 set_capture_state(Running);
223 sr_session_destroy();
225 set_capture_state(Stopped);
227 // Confirm that SR_DF_END was received
228 assert(!_cur_logic_snapshot);
229 assert(!_cur_analog_snapshot);
232 void SigSession::feed_in_header(const sr_dev_inst *sdi)
234 shared_ptr<view::Signal> signal;
236 uint64_t sample_rate = 0;
237 unsigned int logic_probe_count = 0;
238 unsigned int analog_probe_count = 0;
240 // Detect what data types we will receive
241 for (const GSList *l = sdi->probes; l; l = l->next) {
242 const sr_probe *const probe = (const sr_probe *)l->data;
246 switch(probe->type) {
251 case SR_PROBE_ANALOG:
252 analog_probe_count++;
257 // Read out the sample rate
260 const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
263 qDebug("Failed to get samplerate\n");
267 sample_rate = g_variant_get_uint64(gvar);
268 g_variant_unref(gvar);
270 // Create data containers for the coming data snapshots
272 lock_guard<mutex> data_lock(_data_mutex);
274 if (logic_probe_count != 0) {
275 _logic_data.reset(new data::Logic(
276 logic_probe_count, sample_rate));
280 if (analog_probe_count != 0) {
281 _analog_data.reset(new data::Analog(sample_rate));
282 assert(_analog_data);
286 // Make the logic probe list
288 lock_guard<mutex> lock(_signals_mutex);
292 for (const GSList *l = sdi->probes; l; l = l->next) {
293 const sr_probe *const probe =
294 (const sr_probe *)l->data;
299 switch(probe->type) {
301 signal = shared_ptr<view::Signal>(
302 new view::LogicSignal(probe->name,
303 _logic_data, probe->index));
306 case SR_PROBE_ANALOG:
307 signal = shared_ptr<view::Signal>(
308 new view::AnalogSignal(probe->name,
309 _analog_data, probe->index));
313 _signals.push_back(signal);
320 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
321 const sr_datafeed_meta &meta)
325 for (const GSList *l = meta.config; l; l = l->next) {
326 const sr_config *const src = (const sr_config*)l->data;
328 case SR_CONF_SAMPLERATE:
329 /// @todo handle samplerate changes
330 /// samplerate = (uint64_t *)src->value;
333 // Unknown metadata is not an error.
339 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
341 lock_guard<mutex> lock(_data_mutex);
345 qDebug() << "Unexpected logic packet";
349 if (!_cur_logic_snapshot)
351 // Create a new data snapshot
352 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
353 new data::LogicSnapshot(logic));
354 _logic_data->push_snapshot(_cur_logic_snapshot);
358 // Append to the existing data snapshot
359 _cur_logic_snapshot->append_payload(logic);
365 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
367 lock_guard<mutex> lock(_data_mutex);
371 qDebug() << "Unexpected analog packet";
372 return; // This analog packet was not expected.
375 if (!_cur_analog_snapshot)
377 // Create a new data snapshot
378 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
379 new data::AnalogSnapshot(analog));
380 _analog_data->push_snapshot(_cur_analog_snapshot);
384 // Append to the existing data snapshot
385 _cur_analog_snapshot->append_payload(analog);
391 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
392 const struct sr_datafeed_packet *packet)
397 switch (packet->type) {
403 assert(packet->payload);
405 *(const sr_datafeed_meta*)packet->payload);
409 assert(packet->payload);
410 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
414 assert(packet->payload);
415 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
421 lock_guard<mutex> lock(_data_mutex);
422 _cur_logic_snapshot.reset();
423 _cur_analog_snapshot.reset();
431 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
432 const struct sr_datafeed_packet *packet, void *cb_data)
436 _session->data_feed_in(sdi, packet);