]> sigrok.org Git - pulseview.git/blame_incremental - pv/sigsession.cpp
Made the sdi a persisent property of SigSession
[pulseview.git] / pv / sigsession.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "sigsession.h"
22
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"
29
30#include <assert.h>
31
32#include <QDebug>
33
34using namespace boost;
35using namespace std;
36
37namespace pv {
38
39// TODO: This should not be necessary
40SigSession* SigSession::_session = NULL;
41
42SigSession::SigSession() :
43 _sdi(NULL),
44 _capture_state(Stopped)
45{
46 // TODO: This should not be necessary
47 _session = this;
48}
49
50SigSession::~SigSession()
51{
52 stop_capture();
53
54 if (_sampling_thread.get())
55 _sampling_thread->join();
56 _sampling_thread.reset();
57
58 // TODO: This should not be necessary
59 _session = NULL;
60}
61
62struct sr_dev_inst* SigSession::get_device() const
63{
64 return _sdi;
65}
66
67void SigSession::set_device(struct sr_dev_inst *sdi)
68{
69 _sdi = sdi;
70}
71
72void SigSession::load_file(const string &name,
73 function<void (const QString)> error_handler)
74{
75 stop_capture();
76 _sampling_thread.reset(new boost::thread(
77 &SigSession::load_thread_proc, this, name,
78 error_handler));
79}
80
81SigSession::capture_state SigSession::get_capture_state() const
82{
83 lock_guard<mutex> lock(_sampling_mutex);
84 return _capture_state;
85}
86
87void SigSession::start_capture(uint64_t record_length,
88 function<void (const QString)> error_handler)
89{
90 stop_capture();
91
92 // Check that a device instance has been selected.
93 if (!_sdi) {
94 qDebug() << "No device selected";
95 return;
96 }
97
98 // Check that at least one probe is enabled
99 const GSList *l;
100 for (l = _sdi->probes; l; l = l->next) {
101 sr_probe *const probe = (sr_probe*)l->data;
102 assert(probe);
103 if (probe->enabled)
104 break;
105 }
106
107 if (!l) {
108 error_handler(tr("No probes enabled."));
109 return;
110 }
111
112 // Begin the session
113 _sampling_thread.reset(new boost::thread(
114 &SigSession::sample_thread_proc, this, _sdi,
115 record_length, error_handler));
116}
117
118void SigSession::stop_capture()
119{
120 if (get_capture_state() == Stopped)
121 return;
122
123 sr_session_stop();
124
125 // Check that sampling stopped
126 if (_sampling_thread.get())
127 _sampling_thread->join();
128 _sampling_thread.reset();
129}
130
131vector< shared_ptr<view::Signal> > SigSession::get_signals()
132{
133 lock_guard<mutex> lock(_signals_mutex);
134 return _signals;
135}
136
137boost::shared_ptr<data::Logic> SigSession::get_data()
138{
139 return _logic_data;
140}
141
142void SigSession::set_capture_state(capture_state state)
143{
144 lock_guard<mutex> lock(_sampling_mutex);
145 _capture_state = state;
146 capture_state_changed(state);
147}
148
149void SigSession::load_thread_proc(const string name,
150 function<void (const QString)> error_handler)
151{
152 if (sr_session_load(name.c_str()) != SR_OK) {
153 error_handler(tr("Failed to load file."));
154 return;
155 }
156
157 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
158
159 if (sr_session_start() != SR_OK) {
160 error_handler(tr("Failed to start session."));
161 return;
162 }
163
164 set_capture_state(Running);
165
166 sr_session_run();
167 sr_session_destroy();
168
169 set_capture_state(Stopped);
170}
171
172void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
173 uint64_t record_length,
174 function<void (const QString)> error_handler)
175{
176 assert(sdi);
177 assert(error_handler);
178
179 sr_session_new();
180 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
181
182 if (sr_session_dev_add(sdi) != SR_OK) {
183 error_handler(tr("Failed to use device."));
184 sr_session_destroy();
185 return;
186 }
187
188 // Set the sample limit
189 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
190 g_variant_new_uint64(record_length)) != SR_OK) {
191 error_handler(tr("Failed to configure "
192 "time-based sample limit."));
193 sr_session_destroy();
194 return;
195 }
196
197 if (sr_session_start() != SR_OK) {
198 error_handler(tr("Failed to start session."));
199 return;
200 }
201
202 set_capture_state(Running);
203
204 sr_session_run();
205 sr_session_destroy();
206
207 set_capture_state(Stopped);
208}
209
210void SigSession::feed_in_header(const sr_dev_inst *sdi)
211{
212 shared_ptr<view::Signal> signal;
213 GVariant *gvar;
214 uint64_t sample_rate = 0;
215 unsigned int logic_probe_count = 0;
216 unsigned int analog_probe_count = 0;
217
218 // Detect what data types we will receive
219 for (const GSList *l = sdi->probes; l; l = l->next) {
220 const sr_probe *const probe = (const sr_probe *)l->data;
221 if (!probe->enabled)
222 continue;
223
224 switch(probe->type) {
225 case SR_PROBE_LOGIC:
226 logic_probe_count++;
227 break;
228
229 case SR_PROBE_ANALOG:
230 analog_probe_count++;
231 break;
232 }
233 }
234
235 // Read out the sample rate
236 assert(sdi->driver);
237
238 const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
239 &gvar, sdi);
240 if (ret != SR_OK) {
241 qDebug("Failed to get samplerate\n");
242 return;
243 }
244
245 sample_rate = g_variant_get_uint64(gvar);
246 g_variant_unref(gvar);
247
248 // Create data containers for the coming data snapshots
249 {
250 lock_guard<mutex> data_lock(_data_mutex);
251
252 if (logic_probe_count != 0) {
253 _logic_data.reset(new data::Logic(
254 logic_probe_count, sample_rate));
255 assert(_logic_data);
256 }
257
258 if (analog_probe_count != 0) {
259 _analog_data.reset(new data::Analog(sample_rate));
260 assert(_analog_data);
261 }
262 }
263
264 // Make the logic probe list
265 {
266 lock_guard<mutex> lock(_signals_mutex);
267
268 _signals.clear();
269
270 for (const GSList *l = sdi->probes; l; l = l->next) {
271 const sr_probe *const probe =
272 (const sr_probe *)l->data;
273 assert(probe);
274 if (!probe->enabled)
275 continue;
276
277 switch(probe->type) {
278 case SR_PROBE_LOGIC:
279 signal = shared_ptr<view::Signal>(
280 new view::LogicSignal(probe->name,
281 _logic_data, probe->index));
282 break;
283
284 case SR_PROBE_ANALOG:
285 signal = shared_ptr<view::Signal>(
286 new view::AnalogSignal(probe->name,
287 _analog_data, probe->index));
288 break;
289 }
290
291 _signals.push_back(signal);
292 }
293
294 signals_changed();
295 }
296}
297
298void SigSession::feed_in_meta(const sr_dev_inst *sdi,
299 const sr_datafeed_meta &meta)
300{
301 (void)sdi;
302
303 for (const GSList *l = meta.config; l; l = l->next) {
304 const sr_config *const src = (const sr_config*)l->data;
305 switch (src->key) {
306 case SR_CONF_SAMPLERATE:
307 /// @todo handle samplerate changes
308 /// samplerate = (uint64_t *)src->value;
309 break;
310 default:
311 // Unknown metadata is not an error.
312 break;
313 }
314 }
315}
316
317void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
318{
319 lock_guard<mutex> lock(_data_mutex);
320
321 if (!_logic_data)
322 {
323 qDebug() << "Unexpected logic packet";
324 return;
325 }
326
327 if (!_cur_logic_snapshot)
328 {
329 // Create a new data snapshot
330 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
331 new data::LogicSnapshot(logic));
332 _logic_data->push_snapshot(_cur_logic_snapshot);
333 }
334 else
335 {
336 // Append to the existing data snapshot
337 _cur_logic_snapshot->append_payload(logic);
338 }
339
340 data_updated();
341}
342
343void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
344{
345 lock_guard<mutex> lock(_data_mutex);
346
347 if(!_analog_data)
348 {
349 qDebug() << "Unexpected analog packet";
350 return; // This analog packet was not expected.
351 }
352
353 if (!_cur_analog_snapshot)
354 {
355 // Create a new data snapshot
356 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
357 new data::AnalogSnapshot(analog));
358 _analog_data->push_snapshot(_cur_analog_snapshot);
359 }
360 else
361 {
362 // Append to the existing data snapshot
363 _cur_analog_snapshot->append_payload(analog);
364 }
365
366 data_updated();
367}
368
369void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
370 const struct sr_datafeed_packet *packet)
371{
372 assert(sdi);
373 assert(packet);
374
375 switch (packet->type) {
376 case SR_DF_HEADER:
377 feed_in_header(sdi);
378 break;
379
380 case SR_DF_META:
381 assert(packet->payload);
382 feed_in_meta(sdi,
383 *(const sr_datafeed_meta*)packet->payload);
384 break;
385
386 case SR_DF_LOGIC:
387 assert(packet->payload);
388 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
389 break;
390
391 case SR_DF_ANALOG:
392 assert(packet->payload);
393 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
394 break;
395
396 case SR_DF_END:
397 {
398 {
399 lock_guard<mutex> lock(_data_mutex);
400 _cur_logic_snapshot.reset();
401 _cur_analog_snapshot.reset();
402 }
403 data_updated();
404 break;
405 }
406 }
407}
408
409void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
410 const struct sr_datafeed_packet *packet, void *cb_data)
411{
412 (void) cb_data;
413 assert(_session);
414 _session->data_feed_in(sdi, packet);
415}
416
417} // namespace pv