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