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"
34 using namespace boost;
39 // TODO: This should not be necessary
40 SigSession* SigSession::_session = NULL;
42 SigSession::SigSession() :
43 _capture_state(Stopped)
45 // TODO: This should not be necessary
49 SigSession::~SigSession()
53 if (_sampling_thread.get())
54 _sampling_thread->join();
55 _sampling_thread.reset();
57 // TODO: This should not be necessary
61 void SigSession::load_file(const string &name)
64 _sampling_thread.reset(new boost::thread(
65 &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, uint64_t sample_rate)
79 _sampling_thread.reset(new boost::thread(
80 &SigSession::sample_thread_proc, this, sdi,
81 record_length, sample_rate));
84 void SigSession::stop_capture()
86 if (get_capture_state() == Stopped)
91 // Check that sampling stopped
92 if (_sampling_thread.get())
93 _sampling_thread->join();
94 _sampling_thread.reset();
97 vector< shared_ptr<view::Signal> > SigSession::get_signals()
99 lock_guard<mutex> lock(_signals_mutex);
103 boost::shared_ptr<data::Logic> SigSession::get_data()
108 void SigSession::set_capture_state(capture_state state)
110 lock_guard<mutex> lock(_sampling_mutex);
111 _capture_state = state;
112 capture_state_changed(state);
115 void SigSession::load_thread_proc(const string name)
117 if (sr_session_load(name.c_str()) != SR_OK) {
118 qDebug() << "Failed to load file.";
122 sr_session_datafeed_callback_add(data_feed_in_proc);
124 if (sr_session_start() != SR_OK) {
125 qDebug() << "Failed to start session.";
129 set_capture_state(Running);
134 set_capture_state(Stopped);
137 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
138 uint64_t record_length, uint64_t sample_rate)
143 sr_session_datafeed_callback_add(data_feed_in_proc);
145 if (sr_session_dev_add(sdi) != SR_OK) {
146 qDebug() << "Failed to use device.";
147 sr_session_destroy();
151 // Set the sample limit
152 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
153 &record_length) != SR_OK) {
154 qDebug() << "Failed to configure time-based sample limit.";
155 sr_session_destroy();
159 // Set the samplerate
160 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
161 &sample_rate) != SR_OK) {
162 qDebug() << "Failed to configure samplerate.";
163 sr_session_destroy();
167 if (sr_session_start() != SR_OK) {
168 qDebug() << "Failed to start session.";
172 set_capture_state(Running);
175 sr_session_destroy();
177 set_capture_state(Stopped);
180 void SigSession::feed_in_header(const sr_dev_inst *sdi)
182 shared_ptr<view::Signal> signal;
183 uint64_t *sample_rate;
184 unsigned int logic_probe_count = 0;
185 unsigned int analog_probe_count = 0;
187 // Detect what data types we will receive
188 for (const GSList *l = sdi->probes; l; l = l->next) {
189 const sr_probe *const probe = (const sr_probe *)l->data;
193 switch(probe->type) {
198 case SR_PROBE_ANALOG:
199 analog_probe_count++;
204 // Read out the sample rate
206 assert(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
207 (const void**)&sample_rate, sdi) == SR_OK);
209 // Create data containers for the coming data snapshots
211 lock_guard<mutex> data_lock(_data_mutex);
213 if (logic_probe_count != 0) {
214 _logic_data.reset(new data::Logic(
215 logic_probe_count, *sample_rate));
219 if (analog_probe_count != 0) {
220 _analog_data.reset(new data::Analog(*sample_rate));
221 assert(_analog_data);
225 // Make the logic probe list
227 lock_guard<mutex> lock(_signals_mutex);
231 for (const GSList *l = sdi->probes; l; l = l->next) {
232 const sr_probe *const probe =
233 (const sr_probe *)l->data;
238 switch(probe->type) {
240 signal = shared_ptr<view::Signal>(
241 new view::LogicSignal(probe->name,
242 _logic_data, probe->index));
245 case SR_PROBE_ANALOG:
246 signal = shared_ptr<view::Signal>(
247 new view::AnalogSignal(probe->name,
252 _signals.push_back(signal);
259 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
260 const sr_datafeed_meta &meta)
264 for (const GSList *l = meta.config; l; l = l->next) {
265 const sr_config *const src = (const sr_config*)l->data;
267 case SR_CONF_SAMPLERATE:
268 /// @todo handle samplerate changes
269 /// samplerate = (uint64_t *)src->value;
272 // Unknown metadata is not an error.
278 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
280 lock_guard<mutex> lock(_data_mutex);
281 if (!_cur_logic_snapshot)
285 // Create a new data snapshot
286 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
287 new data::LogicSnapshot(logic));
288 _logic_data->push_snapshot(_cur_logic_snapshot);
292 // Append to the existing data snapshot
293 _cur_logic_snapshot->append_payload(logic);
299 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
301 lock_guard<mutex> lock(_data_mutex);
302 if (!_cur_analog_snapshot)
304 assert(_analog_data);
306 // Create a new data snapshot
307 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
308 new data::AnalogSnapshot(analog));
309 _analog_data->push_snapshot(_cur_analog_snapshot);
313 // Append to the existing data snapshot
314 _cur_analog_snapshot->append_payload(analog);
320 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
321 const struct sr_datafeed_packet *packet)
326 switch (packet->type) {
332 assert(packet->payload);
334 *(const sr_datafeed_meta*)packet->payload);
338 assert(packet->payload);
339 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
343 assert(packet->payload);
344 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
350 lock_guard<mutex> lock(_data_mutex);
351 _cur_logic_snapshot.reset();
352 _cur_analog_snapshot.reset();
360 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
361 const struct sr_datafeed_packet *packet)
364 _session->data_feed_in(sdi, packet);