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)
79 _sampling_thread.reset(new boost::thread(
80 &SigSession::sample_thread_proc, this, sdi,
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)
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 if (sr_session_start() != SR_OK) {
160 qDebug() << "Failed to start session.";
164 set_capture_state(Running);
167 sr_session_destroy();
169 set_capture_state(Stopped);
172 void SigSession::feed_in_header(const sr_dev_inst *sdi)
174 shared_ptr<view::Signal> signal;
175 uint64_t *sample_rate = NULL;
176 unsigned int logic_probe_count = 0;
177 unsigned int analog_probe_count = 0;
179 // Detect what data types we will receive
180 for (const GSList *l = sdi->probes; l; l = l->next) {
181 const sr_probe *const probe = (const sr_probe *)l->data;
185 switch(probe->type) {
190 case SR_PROBE_ANALOG:
191 analog_probe_count++;
196 // Read out the sample rate
198 assert(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
199 (const void**)&sample_rate, sdi) == SR_OK);
201 // Create data containers for the coming data snapshots
203 lock_guard<mutex> data_lock(_data_mutex);
205 if (logic_probe_count != 0) {
206 _logic_data.reset(new data::Logic(
207 logic_probe_count, *sample_rate));
211 if (analog_probe_count != 0) {
212 _analog_data.reset(new data::Analog(*sample_rate));
213 assert(_analog_data);
217 // Make the logic probe list
219 lock_guard<mutex> lock(_signals_mutex);
223 for (const GSList *l = sdi->probes; l; l = l->next) {
224 const sr_probe *const probe =
225 (const sr_probe *)l->data;
230 switch(probe->type) {
232 signal = shared_ptr<view::Signal>(
233 new view::LogicSignal(probe->name,
234 _logic_data, probe->index));
237 case SR_PROBE_ANALOG:
238 signal = shared_ptr<view::Signal>(
239 new view::AnalogSignal(probe->name,
244 _signals.push_back(signal);
251 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
252 const sr_datafeed_meta &meta)
256 for (const GSList *l = meta.config; l; l = l->next) {
257 const sr_config *const src = (const sr_config*)l->data;
259 case SR_CONF_SAMPLERATE:
260 /// @todo handle samplerate changes
261 /// samplerate = (uint64_t *)src->value;
264 // Unknown metadata is not an error.
270 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
272 lock_guard<mutex> lock(_data_mutex);
273 if (!_cur_logic_snapshot)
277 // Create a new data snapshot
278 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
279 new data::LogicSnapshot(logic));
280 _logic_data->push_snapshot(_cur_logic_snapshot);
284 // Append to the existing data snapshot
285 _cur_logic_snapshot->append_payload(logic);
291 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
293 lock_guard<mutex> lock(_data_mutex);
294 if (!_cur_analog_snapshot)
296 assert(_analog_data);
298 // Create a new data snapshot
299 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
300 new data::AnalogSnapshot(analog));
301 _analog_data->push_snapshot(_cur_analog_snapshot);
305 // Append to the existing data snapshot
306 _cur_analog_snapshot->append_payload(analog);
312 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
313 const struct sr_datafeed_packet *packet)
318 switch (packet->type) {
324 assert(packet->payload);
326 *(const sr_datafeed_meta*)packet->payload);
330 assert(packet->payload);
331 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
335 assert(packet->payload);
336 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
342 lock_guard<mutex> lock(_data_mutex);
343 _cur_logic_snapshot.reset();
344 _cur_analog_snapshot.reset();
352 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
353 const struct sr_datafeed_packet *packet)
356 _session->data_feed_in(sdi, packet);