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