]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Added assertions to confirm that the session was terminated cleanly
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c
JH
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
1b1ec774
JH
23#include "data/analog.h"
24#include "data/analogsnapshot.h"
25#include "data/logic.h"
26#include "data/logicsnapshot.h"
aba1dd16 27#include "view/analogsignal.h"
8d634081 28#include "view/logicsignal.h"
28a4c9c5 29
2953961c
JH
30#include <assert.h>
31
79efbc53
JH
32#include <QDebug>
33
28a4c9c5 34using namespace boost;
e3f65ace 35using namespace std;
28a4c9c5 36
51e77110
JH
37namespace pv {
38
2953961c 39// TODO: This should not be necessary
04abfae9 40SigSession* SigSession::_session = NULL;
2953961c 41
5b7cf66c 42SigSession::SigSession() :
d64d1596 43 _sdi(NULL),
5b7cf66c 44 _capture_state(Stopped)
2953961c
JH
45{
46 // TODO: This should not be necessary
04abfae9 47 _session = this;
2953961c
JH
48}
49
50SigSession::~SigSession()
51{
5b7cf66c
JH
52 stop_capture();
53
333d5bbc 54 if (_sampling_thread.get())
2e2946fe
JH
55 _sampling_thread->join();
56 _sampling_thread.reset();
57
2953961c 58 // TODO: This should not be necessary
04abfae9 59 _session = NULL;
2953961c
JH
60}
61
d64d1596
JH
62void SigSession::set_device(struct sr_dev_inst *sdi)
63{
88497156
BV
64 if (_sdi)
65 sr_dev_close(_sdi);
66 if (sdi)
67 sr_dev_open(sdi);
d64d1596
JH
68 _sdi = sdi;
69}
70
f2edb557
JH
71void SigSession::load_file(const string &name,
72 function<void (const QString)> error_handler)
2953961c 73{
8a67bd9e
JH
74 stop_capture();
75 _sampling_thread.reset(new boost::thread(
f2edb557
JH
76 &SigSession::load_thread_proc, this, name,
77 error_handler));
2953961c
JH
78}
79
5b7cf66c
JH
80SigSession::capture_state SigSession::get_capture_state() const
81{
949f8050 82 lock_guard<mutex> lock(_sampling_mutex);
5b7cf66c
JH
83 return _capture_state;
84}
85
d64d1596 86void SigSession::start_capture(uint64_t record_length,
f2edb557 87 function<void (const QString)> error_handler)
2e2946fe 88{
5b7cf66c
JH
89 stop_capture();
90
d64d1596
JH
91 // Check that a device instance has been selected.
92 if (!_sdi) {
93 qDebug() << "No device selected";
94 return;
95 }
96
9c48fa57
JH
97 // Check that at least one probe is enabled
98 const GSList *l;
d64d1596 99 for (l = _sdi->probes; l; l = l->next) {
9c48fa57
JH
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
2e2946fe 112 _sampling_thread.reset(new boost::thread(
d64d1596 113 &SigSession::sample_thread_proc, this, _sdi,
f2edb557 114 record_length, error_handler));
2e2946fe
JH
115}
116
5b7cf66c
JH
117void SigSession::stop_capture()
118{
333d5bbc 119 if (get_capture_state() == Stopped)
5b7cf66c
JH
120 return;
121
122 sr_session_stop();
123
124 // Check that sampling stopped
333d5bbc 125 if (_sampling_thread.get())
5b7cf66c
JH
126 _sampling_thread->join();
127 _sampling_thread.reset();
5b7cf66c
JH
128}
129
3868e5fa 130vector< shared_ptr<view::Signal> > SigSession::get_signals()
2e2946fe 131{
3868e5fa 132 lock_guard<mutex> lock(_signals_mutex);
2e2946fe
JH
133 return _signals;
134}
135
1b1ec774 136boost::shared_ptr<data::Logic> SigSession::get_data()
2e2946fe
JH
137{
138 return _logic_data;
139}
140
6ac96c2e
JH
141void SigSession::set_capture_state(capture_state state)
142{
949f8050 143 lock_guard<mutex> lock(_sampling_mutex);
6ac96c2e
JH
144 _capture_state = state;
145 capture_state_changed(state);
146}
147
f2edb557
JH
148void SigSession::load_thread_proc(const string name,
149 function<void (const QString)> error_handler)
8a67bd9e
JH
150{
151 if (sr_session_load(name.c_str()) != SR_OK) {
f2edb557 152 error_handler(tr("Failed to load file."));
8a67bd9e
JH
153 return;
154 }
155
5045f16d 156 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
8a67bd9e
JH
157
158 if (sr_session_start() != SR_OK) {
f2edb557 159 error_handler(tr("Failed to start session."));
8a67bd9e
JH
160 return;
161 }
162
163 set_capture_state(Running);
164
165 sr_session_run();
d5bf2c6c 166 sr_session_destroy();
8a67bd9e
JH
167
168 set_capture_state(Stopped);
1a2fe44c
JH
169
170 // Confirm that SR_DF_END was received
171 assert(!_cur_logic_snapshot);
172 assert(!_cur_analog_snapshot);
8a67bd9e
JH
173}
174
2e2946fe 175void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
f2edb557
JH
176 uint64_t record_length,
177 function<void (const QString)> error_handler)
274d4f13 178{
be73bdfa 179 assert(sdi);
f2edb557 180 assert(error_handler);
be73bdfa 181
274d4f13 182 sr_session_new();
5045f16d 183 sr_session_datafeed_callback_add(data_feed_in_proc, NULL);
274d4f13
JH
184
185 if (sr_session_dev_add(sdi) != SR_OK) {
f2edb557 186 error_handler(tr("Failed to use device."));
274d4f13
JH
187 sr_session_destroy();
188 return;
189 }
190
be73bdfa
JH
191 // Set the sample limit
192 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
8eb9d311 193 g_variant_new_uint64(record_length)) != SR_OK) {
f2edb557
JH
194 error_handler(tr("Failed to configure "
195 "time-based sample limit."));
274d4f13
JH
196 sr_session_destroy();
197 return;
198 }
199
eec446e1 200 if (sr_session_start() != SR_OK) {
f2edb557 201 error_handler(tr("Failed to start session."));
eec446e1
JH
202 return;
203 }
204
205 set_capture_state(Running);
206
207 sr_session_run();
208 sr_session_destroy();
209
210 set_capture_state(Stopped);
1a2fe44c
JH
211
212 // Confirm that SR_DF_END was received
213 assert(!_cur_logic_snapshot);
214 assert(!_cur_analog_snapshot);
eec446e1
JH
215}
216
217void SigSession::feed_in_header(const sr_dev_inst *sdi)
218{
219 shared_ptr<view::Signal> signal;
8eb9d311
BV
220 GVariant *gvar;
221 uint64_t sample_rate = 0;
eec446e1
JH
222 unsigned int logic_probe_count = 0;
223 unsigned int analog_probe_count = 0;
224
be73bdfa
JH
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;
274d4f13 230
be73bdfa
JH
231 switch(probe->type) {
232 case SR_PROBE_LOGIC:
233 logic_probe_count++;
234 break;
5b7cf66c 235
be73bdfa
JH
236 case SR_PROBE_ANALOG:
237 analog_probe_count++;
238 break;
239 }
240 }
8d634081 241
7297c76e
JH
242 // Read out the sample rate
243 assert(sdi->driver);
c0d6d479
JH
244
245 const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
8eb9d311 246 &gvar, sdi);
174e27a1
JH
247 if (ret != SR_OK) {
248 qDebug("Failed to get samplerate\n");
249 return;
250 }
251
8eb9d311
BV
252 sample_rate = g_variant_get_uint64(gvar);
253 g_variant_unref(gvar);
7297c76e 254
be73bdfa 255 // Create data containers for the coming data snapshots
3868e5fa 256 {
aba1dd16 257 lock_guard<mutex> data_lock(_data_mutex);
3868e5fa 258
be73bdfa
JH
259 if (logic_probe_count != 0) {
260 _logic_data.reset(new data::Logic(
8eb9d311 261 logic_probe_count, sample_rate));
be73bdfa
JH
262 assert(_logic_data);
263 }
264
265 if (analog_probe_count != 0) {
8eb9d311 266 _analog_data.reset(new data::Analog(sample_rate));
be73bdfa
JH
267 assert(_analog_data);
268 }
3868e5fa
JH
269 }
270
be73bdfa 271 // Make the logic probe list
3868e5fa
JH
272 {
273 lock_guard<mutex> lock(_signals_mutex);
2e2946fe 274
be73bdfa
JH
275 _signals.clear();
276
277 for (const GSList *l = sdi->probes; l; l = l->next) {
2e2946fe 278 const sr_probe *const probe =
be73bdfa
JH
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,
b0e1d01d 288 _logic_data, probe->index));
be73bdfa
JH
289 break;
290
291 case SR_PROBE_ANALOG:
292 signal = shared_ptr<view::Signal>(
293 new view::AnalogSignal(probe->name,
568b90d4 294 _analog_data, probe->index));
be73bdfa 295 break;
e3f65ace 296 }
be73bdfa
JH
297
298 _signals.push_back(signal);
2953961c 299 }
28a4c9c5 300
69dd2b03 301 signals_changed();
2e2946fe 302 }
be73bdfa
JH
303}
304
305void SigSession::feed_in_meta(const sr_dev_inst *sdi,
306 const sr_datafeed_meta &meta)
307{
94fe58ef
UH
308 (void)sdi;
309
be73bdfa
JH
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 }
aba1dd16
JH
321 }
322}
323
9c112671
JH
324void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
325{
326 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
327
328 if (!_logic_data)
9c112671 329 {
79efbc53
JH
330 qDebug() << "Unexpected logic packet";
331 return;
332 }
be73bdfa 333
79efbc53
JH
334 if (!_cur_logic_snapshot)
335 {
9c112671 336 // Create a new data snapshot
1b1ec774
JH
337 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
338 new data::LogicSnapshot(logic));
9c112671
JH
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
aba1dd16
JH
350void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
351{
352 lock_guard<mutex> lock(_data_mutex);
79efbc53
JH
353
354 if(!_analog_data)
aba1dd16 355 {
79efbc53
JH
356 qDebug() << "Unexpected analog packet";
357 return; // This analog packet was not expected.
358 }
be73bdfa 359
79efbc53
JH
360 if (!_cur_analog_snapshot)
361 {
aba1dd16 362 // Create a new data snapshot
1b1ec774
JH
363 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
364 new data::AnalogSnapshot(analog));
aba1dd16
JH
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}
9c112671 375
b0e1d01d
JH
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:
eec446e1 384 feed_in_header(sdi);
b0e1d01d
JH
385 break;
386
be73bdfa 387 case SR_DF_META:
aba1dd16 388 assert(packet->payload);
be73bdfa
JH
389 feed_in_meta(sdi,
390 *(const sr_datafeed_meta*)packet->payload);
aba1dd16
JH
391 break;
392
2e2946fe 393 case SR_DF_LOGIC:
28a4c9c5 394 assert(packet->payload);
9c112671 395 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
2953961c
JH
396 break;
397
aba1dd16
JH
398 case SR_DF_ANALOG:
399 assert(packet->payload);
400 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
401 break;
402
2953961c 403 case SR_DF_END:
2e2946fe
JH
404 {
405 {
406 lock_guard<mutex> lock(_data_mutex);
407 _cur_logic_snapshot.reset();
aba1dd16 408 _cur_analog_snapshot.reset();
2e2946fe 409 }
04abfae9 410 data_updated();
2953961c
JH
411 break;
412 }
2e2946fe 413 }
2953961c
JH
414}
415
04abfae9 416void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
5045f16d 417 const struct sr_datafeed_packet *packet, void *cb_data)
2953961c 418{
5045f16d 419 (void) cb_data;
04abfae9
JH
420 assert(_session);
421 _session->data_feed_in(sdi, packet);
2953961c 422}
51e77110
JH
423
424} // namespace pv