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 "data/analog.h"
24 #include "data/analogsnapshot.h"
25 #include "data/logic.h"
26 #include "data/logicsnapshot.h"
27 #include "view/analogsignal.h"
28 #include "view/logicsignal.h"
32 using namespace boost;
37 // TODO: This should not be necessary
38 SigSession* SigSession::_session = NULL;
40 SigSession::SigSession() :
41 _capture_state(Stopped)
43 // TODO: This should not be necessary
47 SigSession::~SigSession()
51 if (_sampling_thread.get())
52 _sampling_thread->join();
53 _sampling_thread.reset();
55 // TODO: This should not be necessary
59 void SigSession::load_file(const string &name,
60 function<void (const QString)> error_handler)
63 _sampling_thread.reset(new boost::thread(
64 &SigSession::load_thread_proc, this, name,
68 SigSession::capture_state SigSession::get_capture_state() const
70 lock_guard<mutex> lock(_sampling_mutex);
71 return _capture_state;
74 void SigSession::start_capture(struct sr_dev_inst *sdi,
75 uint64_t record_length,
76 function<void (const QString)> error_handler)
80 // Check that at least one probe is enabled
82 for (l = sdi->probes; l; l = l->next) {
83 sr_probe *const probe = (sr_probe*)l->data;
90 error_handler(tr("No probes enabled."));
95 _sampling_thread.reset(new boost::thread(
96 &SigSession::sample_thread_proc, this, sdi,
97 record_length, error_handler));
100 void SigSession::stop_capture()
102 if (get_capture_state() == Stopped)
107 // Check that sampling stopped
108 if (_sampling_thread.get())
109 _sampling_thread->join();
110 _sampling_thread.reset();
113 vector< shared_ptr<view::Signal> > SigSession::get_signals()
115 lock_guard<mutex> lock(_signals_mutex);
119 boost::shared_ptr<data::Logic> SigSession::get_data()
124 void SigSession::set_capture_state(capture_state state)
126 lock_guard<mutex> lock(_sampling_mutex);
127 _capture_state = state;
128 capture_state_changed(state);
131 void SigSession::load_thread_proc(const string name,
132 function<void (const QString)> error_handler)
134 if (sr_session_load(name.c_str()) != SR_OK) {
135 error_handler(tr("Failed to load file."));
139 sr_session_datafeed_callback_add(data_feed_in_proc);
141 if (sr_session_start() != SR_OK) {
142 error_handler(tr("Failed to start session."));
146 set_capture_state(Running);
151 set_capture_state(Stopped);
154 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
155 uint64_t record_length,
156 function<void (const QString)> error_handler)
159 assert(error_handler);
162 sr_session_datafeed_callback_add(data_feed_in_proc);
164 if (sr_session_dev_add(sdi) != SR_OK) {
165 error_handler(tr("Failed to use device."));
166 sr_session_destroy();
170 // Set the sample limit
171 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
172 &record_length) != SR_OK) {
173 error_handler(tr("Failed to configure "
174 "time-based sample limit."));
175 sr_session_destroy();
179 if (sr_session_start() != SR_OK) {
180 error_handler(tr("Failed to start session."));
184 set_capture_state(Running);
187 sr_session_destroy();
189 set_capture_state(Stopped);
192 void SigSession::feed_in_header(const sr_dev_inst *sdi)
194 shared_ptr<view::Signal> signal;
195 uint64_t *sample_rate = NULL;
196 unsigned int logic_probe_count = 0;
197 unsigned int analog_probe_count = 0;
199 // Detect what data types we will receive
200 for (const GSList *l = sdi->probes; l; l = l->next) {
201 const sr_probe *const probe = (const sr_probe *)l->data;
205 switch(probe->type) {
210 case SR_PROBE_ANALOG:
211 analog_probe_count++;
216 // Read out the sample rate
219 const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
220 (const void**)&sample_rate, sdi);
221 assert(ret == SR_OK);
223 // Create data containers for the coming data snapshots
225 lock_guard<mutex> data_lock(_data_mutex);
227 if (logic_probe_count != 0) {
228 _logic_data.reset(new data::Logic(
229 logic_probe_count, *sample_rate));
233 if (analog_probe_count != 0) {
234 _analog_data.reset(new data::Analog(*sample_rate));
235 assert(_analog_data);
239 // Make the logic probe list
241 lock_guard<mutex> lock(_signals_mutex);
245 for (const GSList *l = sdi->probes; l; l = l->next) {
246 const sr_probe *const probe =
247 (const sr_probe *)l->data;
252 switch(probe->type) {
254 signal = shared_ptr<view::Signal>(
255 new view::LogicSignal(probe->name,
256 _logic_data, probe->index));
259 case SR_PROBE_ANALOG:
260 signal = shared_ptr<view::Signal>(
261 new view::AnalogSignal(probe->name,
266 _signals.push_back(signal);
273 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
274 const sr_datafeed_meta &meta)
278 for (const GSList *l = meta.config; l; l = l->next) {
279 const sr_config *const src = (const sr_config*)l->data;
281 case SR_CONF_SAMPLERATE:
282 /// @todo handle samplerate changes
283 /// samplerate = (uint64_t *)src->value;
286 // Unknown metadata is not an error.
292 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
294 lock_guard<mutex> lock(_data_mutex);
295 if (!_cur_logic_snapshot)
299 // Create a new data snapshot
300 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
301 new data::LogicSnapshot(logic));
302 _logic_data->push_snapshot(_cur_logic_snapshot);
306 // Append to the existing data snapshot
307 _cur_logic_snapshot->append_payload(logic);
313 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
315 lock_guard<mutex> lock(_data_mutex);
316 if (!_cur_analog_snapshot)
318 assert(_analog_data);
320 // Create a new data snapshot
321 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
322 new data::AnalogSnapshot(analog));
323 _analog_data->push_snapshot(_cur_analog_snapshot);
327 // Append to the existing data snapshot
328 _cur_analog_snapshot->append_payload(analog);
334 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
335 const struct sr_datafeed_packet *packet)
340 switch (packet->type) {
346 assert(packet->payload);
348 *(const sr_datafeed_meta*)packet->payload);
352 assert(packet->payload);
353 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
357 assert(packet->payload);
358 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
364 lock_guard<mutex> lock(_data_mutex);
365 _cur_logic_snapshot.reset();
366 _cur_analog_snapshot.reset();
374 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
375 const struct sr_datafeed_packet *packet)
378 _session->data_feed_in(sdi, packet);