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 _sampling_thread.reset(new boost::thread(
81 &SigSession::sample_thread_proc, this, sdi,
82 record_length, error_handler));
85 void SigSession::stop_capture()
87 if (get_capture_state() == Stopped)
92 // Check that sampling stopped
93 if (_sampling_thread.get())
94 _sampling_thread->join();
95 _sampling_thread.reset();
98 vector< shared_ptr<view::Signal> > SigSession::get_signals()
100 lock_guard<mutex> lock(_signals_mutex);
104 boost::shared_ptr<data::Logic> SigSession::get_data()
109 void SigSession::set_capture_state(capture_state state)
111 lock_guard<mutex> lock(_sampling_mutex);
112 _capture_state = state;
113 capture_state_changed(state);
116 void SigSession::load_thread_proc(const string name,
117 function<void (const QString)> error_handler)
119 if (sr_session_load(name.c_str()) != SR_OK) {
120 error_handler(tr("Failed to load file."));
124 sr_session_datafeed_callback_add(data_feed_in_proc);
126 if (sr_session_start() != SR_OK) {
127 error_handler(tr("Failed to start session."));
131 set_capture_state(Running);
136 set_capture_state(Stopped);
139 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
140 uint64_t record_length,
141 function<void (const QString)> error_handler)
144 assert(error_handler);
147 sr_session_datafeed_callback_add(data_feed_in_proc);
149 if (sr_session_dev_add(sdi) != SR_OK) {
150 error_handler(tr("Failed to use device."));
151 sr_session_destroy();
155 // Set the sample limit
156 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
157 &record_length) != SR_OK) {
158 error_handler(tr("Failed to configure "
159 "time-based sample limit."));
160 sr_session_destroy();
164 if (sr_session_start() != SR_OK) {
165 error_handler(tr("Failed to start session."));
169 set_capture_state(Running);
172 sr_session_destroy();
174 set_capture_state(Stopped);
177 void SigSession::feed_in_header(const sr_dev_inst *sdi)
179 shared_ptr<view::Signal> signal;
180 uint64_t *sample_rate = NULL;
181 unsigned int logic_probe_count = 0;
182 unsigned int analog_probe_count = 0;
184 // Detect what data types we will receive
185 for (const GSList *l = sdi->probes; l; l = l->next) {
186 const sr_probe *const probe = (const sr_probe *)l->data;
190 switch(probe->type) {
195 case SR_PROBE_ANALOG:
196 analog_probe_count++;
201 // Read out the sample rate
204 const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
205 (const void**)&sample_rate, sdi);
206 assert(ret == SR_OK);
208 // Create data containers for the coming data snapshots
210 lock_guard<mutex> data_lock(_data_mutex);
212 if (logic_probe_count != 0) {
213 _logic_data.reset(new data::Logic(
214 logic_probe_count, *sample_rate));
218 if (analog_probe_count != 0) {
219 _analog_data.reset(new data::Analog(*sample_rate));
220 assert(_analog_data);
224 // Make the logic probe list
226 lock_guard<mutex> lock(_signals_mutex);
230 for (const GSList *l = sdi->probes; l; l = l->next) {
231 const sr_probe *const probe =
232 (const sr_probe *)l->data;
237 switch(probe->type) {
239 signal = shared_ptr<view::Signal>(
240 new view::LogicSignal(probe->name,
241 _logic_data, probe->index));
244 case SR_PROBE_ANALOG:
245 signal = shared_ptr<view::Signal>(
246 new view::AnalogSignal(probe->name,
251 _signals.push_back(signal);
258 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
259 const sr_datafeed_meta &meta)
263 for (const GSList *l = meta.config; l; l = l->next) {
264 const sr_config *const src = (const sr_config*)l->data;
266 case SR_CONF_SAMPLERATE:
267 /// @todo handle samplerate changes
268 /// samplerate = (uint64_t *)src->value;
271 // Unknown metadata is not an error.
277 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
279 lock_guard<mutex> lock(_data_mutex);
280 if (!_cur_logic_snapshot)
284 // Create a new data snapshot
285 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
286 new data::LogicSnapshot(logic));
287 _logic_data->push_snapshot(_cur_logic_snapshot);
291 // Append to the existing data snapshot
292 _cur_logic_snapshot->append_payload(logic);
298 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
300 lock_guard<mutex> lock(_data_mutex);
301 if (!_cur_analog_snapshot)
303 assert(_analog_data);
305 // Create a new data snapshot
306 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
307 new data::AnalogSnapshot(analog));
308 _analog_data->push_snapshot(_cur_analog_snapshot);
312 // Append to the existing data snapshot
313 _cur_analog_snapshot->append_payload(analog);
319 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
320 const struct sr_datafeed_packet *packet)
325 switch (packet->type) {
331 assert(packet->payload);
333 *(const sr_datafeed_meta*)packet->payload);
337 assert(packet->payload);
338 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
342 assert(packet->payload);
343 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
349 lock_guard<mutex> lock(_data_mutex);
350 _cur_logic_snapshot.reset();
351 _cur_analog_snapshot.reset();
359 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
360 const struct sr_datafeed_packet *packet)
363 _session->data_feed_in(sdi, packet);