]>
Commit | Line | Data |
---|---|---|
2953961c | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
2953961c | 3 | * |
1bc6525b | 4 | * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk> |
2953961c JH |
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 | ||
269528f5 | 21 | #ifdef ENABLE_DECODE |
4e5a4405 | 22 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 23 | #endif |
4e5a4405 | 24 | |
2953961c JH |
25 | #include "sigsession.h" |
26 | ||
dc0867ff | 27 | #include "devicemanager.h" |
19adbc2c | 28 | #include "devinst.h" |
119aff65 | 29 | |
1b1ec774 JH |
30 | #include "data/analog.h" |
31 | #include "data/analogsnapshot.h" | |
6e89374a | 32 | #include "data/decoderstack.h" |
1b1ec774 JH |
33 | #include "data/logic.h" |
34 | #include "data/logicsnapshot.h" | |
7491a29f | 35 | #include "data/decode/decoder.h" |
82c7f640 | 36 | |
aba1dd16 | 37 | #include "view/analogsignal.h" |
b9329558 | 38 | #include "view/decodetrace.h" |
8d634081 | 39 | #include "view/logicsignal.h" |
28a4c9c5 | 40 | |
2953961c JH |
41 | #include <assert.h> |
42 | ||
e92cd4e4 JH |
43 | #include <stdexcept> |
44 | ||
4e5a4405 JH |
45 | #include <boost/foreach.hpp> |
46 | ||
728e5ef7 JH |
47 | #include <sys/stat.h> |
48 | ||
79efbc53 JH |
49 | #include <QDebug> |
50 | ||
819f4c25 JH |
51 | using boost::dynamic_pointer_cast; |
52 | using boost::function; | |
53 | using boost::lock_guard; | |
54 | using boost::mutex; | |
55 | using boost::shared_ptr; | |
56 | using std::map; | |
02412f0b | 57 | using std::set; |
819f4c25 JH |
58 | using std::string; |
59 | using std::vector; | |
28a4c9c5 | 60 | |
51e77110 JH |
61 | namespace pv { |
62 | ||
2953961c | 63 | // TODO: This should not be necessary |
04abfae9 | 64 | SigSession* SigSession::_session = NULL; |
2953961c | 65 | |
dc0867ff JH |
66 | SigSession::SigSession(DeviceManager &device_manager) : |
67 | _device_manager(device_manager), | |
5b7cf66c | 68 | _capture_state(Stopped) |
2953961c JH |
69 | { |
70 | // TODO: This should not be necessary | |
04abfae9 | 71 | _session = this; |
2953961c JH |
72 | } |
73 | ||
74 | SigSession::~SigSession() | |
75 | { | |
5b7cf66c JH |
76 | stop_capture(); |
77 | ||
6d483b8b MC |
78 | if (_sampling_thread.joinable()) |
79 | _sampling_thread.join(); | |
2e2946fe | 80 | |
19adbc2c JH |
81 | if (_dev_inst) |
82 | _device_manager.release_device(_dev_inst); | |
0f784939 | 83 | |
2953961c | 84 | // TODO: This should not be necessary |
04abfae9 | 85 | _session = NULL; |
2953961c JH |
86 | } |
87 | ||
19adbc2c | 88 | shared_ptr<DevInst> SigSession::get_device() const |
dc0867ff | 89 | { |
19adbc2c | 90 | return _dev_inst; |
dc0867ff JH |
91 | } |
92 | ||
19adbc2c | 93 | void SigSession::set_device(shared_ptr<DevInst> dev_inst) |
d64d1596 | 94 | { |
82596f46 JH |
95 | // Ensure we are not capturing before setting the device |
96 | stop_capture(); | |
97 | ||
19adbc2c JH |
98 | if (_dev_inst) |
99 | _device_manager.release_device(_dev_inst); | |
100 | if (dev_inst) | |
101 | _device_manager.use_device(dev_inst, this); | |
102 | _dev_inst = dev_inst; | |
103 | update_signals(dev_inst); | |
d64d1596 JH |
104 | } |
105 | ||
19adbc2c | 106 | void SigSession::release_device(shared_ptr<DevInst> dev_inst) |
dc0867ff | 107 | { |
19adbc2c | 108 | (void)dev_inst; |
dc0867ff JH |
109 | |
110 | assert(_capture_state == Stopped); | |
19adbc2c | 111 | _dev_inst = shared_ptr<DevInst>(); |
dc0867ff JH |
112 | } |
113 | ||
f2edb557 JH |
114 | void SigSession::load_file(const string &name, |
115 | function<void (const QString)> error_handler) | |
2953961c | 116 | { |
8a67bd9e | 117 | stop_capture(); |
e8c9f8a5 JH |
118 | |
119 | if (sr_session_load(name.c_str()) == SR_OK) { | |
120 | GSList *devlist = NULL; | |
121 | sr_session_dev_list(&devlist); | |
122 | ||
123 | if (!devlist || !devlist->data || | |
124 | sr_session_start() != SR_OK) { | |
125 | error_handler(tr("Failed to start session.")); | |
126 | return; | |
127 | } | |
128 | ||
19adbc2c JH |
129 | shared_ptr<DevInst> dev_inst( |
130 | new DevInst((sr_dev_inst*)devlist->data)); | |
e8c9f8a5 JH |
131 | g_slist_free(devlist); |
132 | ||
7d0c935c | 133 | _decode_traces.clear(); |
19adbc2c JH |
134 | update_signals(dev_inst); |
135 | read_sample_rate(dev_inst->dev_inst()); | |
deef291c | 136 | |
e042ad64 | 137 | _sampling_thread = boost::thread( |
e8c9f8a5 | 138 | &SigSession::load_session_thread_proc, this, |
e042ad64 | 139 | error_handler); |
e8c9f8a5 JH |
140 | |
141 | } else { | |
142 | sr_input *in = NULL; | |
143 | ||
144 | if (!(in = load_input_file_format(name.c_str(), | |
145 | error_handler))) | |
146 | return; | |
147 | ||
7d0c935c | 148 | _decode_traces.clear(); |
19adbc2c | 149 | update_signals(shared_ptr<DevInst>(new DevInst(in->sdi))); |
deef291c | 150 | read_sample_rate(in->sdi); |
e8c9f8a5 | 151 | |
e042ad64 | 152 | _sampling_thread = boost::thread( |
e8c9f8a5 | 153 | &SigSession::load_input_thread_proc, this, |
e042ad64 | 154 | name, in, error_handler); |
e8c9f8a5 | 155 | } |
2953961c JH |
156 | } |
157 | ||
5b7cf66c JH |
158 | SigSession::capture_state SigSession::get_capture_state() const |
159 | { | |
949f8050 | 160 | lock_guard<mutex> lock(_sampling_mutex); |
5b7cf66c JH |
161 | return _capture_state; |
162 | } | |
163 | ||
d99dc9f2 | 164 | void SigSession::start_capture(function<void (const QString)> error_handler) |
2e2946fe | 165 | { |
5b7cf66c JH |
166 | stop_capture(); |
167 | ||
d64d1596 | 168 | // Check that a device instance has been selected. |
19adbc2c | 169 | if (!_dev_inst) { |
d64d1596 JH |
170 | qDebug() << "No device selected"; |
171 | return; | |
172 | } | |
173 | ||
19adbc2c JH |
174 | assert(_dev_inst->dev_inst()); |
175 | ||
9c48fa57 JH |
176 | // Check that at least one probe is enabled |
177 | const GSList *l; | |
19adbc2c | 178 | for (l = _dev_inst->dev_inst()->probes; l; l = l->next) { |
9c48fa57 JH |
179 | sr_probe *const probe = (sr_probe*)l->data; |
180 | assert(probe); | |
181 | if (probe->enabled) | |
182 | break; | |
183 | } | |
184 | ||
185 | if (!l) { | |
186 | error_handler(tr("No probes enabled.")); | |
187 | return; | |
188 | } | |
189 | ||
190 | // Begin the session | |
e042ad64 | 191 | _sampling_thread = boost::thread( |
19adbc2c JH |
192 | &SigSession::sample_thread_proc, this, _dev_inst, |
193 | error_handler); | |
2e2946fe JH |
194 | } |
195 | ||
5b7cf66c JH |
196 | void SigSession::stop_capture() |
197 | { | |
333d5bbc | 198 | if (get_capture_state() == Stopped) |
5b7cf66c JH |
199 | return; |
200 | ||
201 | sr_session_stop(); | |
202 | ||
203 | // Check that sampling stopped | |
6d483b8b MC |
204 | if (_sampling_thread.joinable()) |
205 | _sampling_thread.join(); | |
5b7cf66c JH |
206 | } |
207 | ||
02412f0b JH |
208 | set< shared_ptr<data::SignalData> > SigSession::get_data() const |
209 | { | |
210 | lock_guard<mutex> lock(_signals_mutex); | |
211 | set< shared_ptr<data::SignalData> > data; | |
212 | BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) { | |
213 | assert(sig); | |
214 | data.insert(sig->data()); | |
215 | } | |
216 | ||
217 | return data; | |
218 | } | |
219 | ||
38eeddea | 220 | vector< shared_ptr<view::Signal> > SigSession::get_signals() const |
2e2946fe | 221 | { |
3868e5fa | 222 | lock_guard<mutex> lock(_signals_mutex); |
2e2946fe JH |
223 | return _signals; |
224 | } | |
225 | ||
269528f5 | 226 | #ifdef ENABLE_DECODE |
4e5a4405 | 227 | bool SigSession::add_decoder(srd_decoder *const dec) |
82c7f640 | 228 | { |
4e5a4405 | 229 | map<const srd_probe*, shared_ptr<view::LogicSignal> > probes; |
7491a29f | 230 | shared_ptr<data::DecoderStack> decoder_stack; |
4e5a4405 | 231 | |
e92cd4e4 | 232 | try |
82c7f640 | 233 | { |
119aff65 | 234 | lock_guard<mutex> lock(_signals_mutex); |
e0fc5810 | 235 | |
4e5a4405 | 236 | // Create the decoder |
7491a29f JH |
237 | decoder_stack = shared_ptr<data::DecoderStack>( |
238 | new data::DecoderStack(dec)); | |
4e5a4405 | 239 | |
d2f46d27 JH |
240 | // Make a list of all the probes |
241 | std::vector<const srd_probe*> all_probes; | |
4e5a4405 | 242 | for(const GSList *i = dec->probes; i; i = i->next) |
d2f46d27 JH |
243 | all_probes.push_back((const srd_probe*)i->data); |
244 | for(const GSList *i = dec->opt_probes; i; i = i->next) | |
245 | all_probes.push_back((const srd_probe*)i->data); | |
246 | ||
247 | // Auto select the initial probes | |
248 | BOOST_FOREACH(const srd_probe *probe, all_probes) | |
4e5a4405 JH |
249 | BOOST_FOREACH(shared_ptr<view::Signal> s, _signals) |
250 | { | |
251 | shared_ptr<view::LogicSignal> l = | |
252 | dynamic_pointer_cast<view::LogicSignal>(s); | |
27e8df22 JH |
253 | if (l && QString::fromUtf8(probe->name). |
254 | toLower().contains( | |
4e5a4405 JH |
255 | l->get_name().toLower())) |
256 | probes[probe] = l; | |
257 | } | |
4e5a4405 | 258 | |
7491a29f JH |
259 | assert(decoder_stack); |
260 | assert(!decoder_stack->stack().empty()); | |
261 | assert(decoder_stack->stack().front()); | |
262 | decoder_stack->stack().front()->set_probes(probes); | |
4e5a4405 JH |
263 | |
264 | // Create the decode signal | |
b9329558 | 265 | shared_ptr<view::DecodeTrace> d( |
7491a29f | 266 | new view::DecodeTrace(*this, decoder_stack, |
06bb4e6a | 267 | _decode_traces.size())); |
82c7f640 JH |
268 | _decode_traces.push_back(d); |
269 | } | |
e92cd4e4 JH |
270 | catch(std::runtime_error e) |
271 | { | |
272 | return false; | |
273 | } | |
274 | ||
82c7f640 | 275 | signals_changed(); |
e92cd4e4 | 276 | |
7491a29f JH |
277 | // Do an initial decode |
278 | decoder_stack->begin_decode(); | |
279 | ||
e92cd4e4 | 280 | return true; |
82c7f640 JH |
281 | } |
282 | ||
b9329558 | 283 | vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const |
38eeddea JH |
284 | { |
285 | lock_guard<mutex> lock(_signals_mutex); | |
286 | return _decode_traces; | |
287 | } | |
288 | ||
b9329558 | 289 | void SigSession::remove_decode_signal(view::DecodeTrace *signal) |
c51482b3 | 290 | { |
b9329558 | 291 | for (vector< shared_ptr<view::DecodeTrace> >::iterator i = |
c51482b3 JH |
292 | _decode_traces.begin(); |
293 | i != _decode_traces.end(); | |
294 | i++) | |
295 | if ((*i).get() == signal) | |
296 | { | |
297 | _decode_traces.erase(i); | |
298 | signals_changed(); | |
299 | return; | |
300 | } | |
301 | } | |
269528f5 | 302 | #endif |
c51482b3 | 303 | |
6ac96c2e JH |
304 | void SigSession::set_capture_state(capture_state state) |
305 | { | |
949f8050 | 306 | lock_guard<mutex> lock(_sampling_mutex); |
2b49eeb0 | 307 | const bool changed = _capture_state != state; |
6ac96c2e | 308 | _capture_state = state; |
2b49eeb0 JH |
309 | if(changed) |
310 | capture_state_changed(state); | |
6ac96c2e JH |
311 | } |
312 | ||
728e5ef7 JH |
313 | /** |
314 | * Attempts to autodetect the format. Failing that | |
315 | * @param filename The filename of the input file. | |
316 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
317 | * or NULL if no input format was selected or auto-detected. | |
318 | */ | |
319 | sr_input_format* SigSession::determine_input_file_format( | |
320 | const string &filename) | |
321 | { | |
322 | int i; | |
323 | ||
324 | /* If there are no input formats, return NULL right away. */ | |
325 | sr_input_format *const *const inputs = sr_input_list(); | |
326 | if (!inputs) { | |
327 | g_critical("No supported input formats available."); | |
328 | return NULL; | |
329 | } | |
330 | ||
331 | /* Otherwise, try to find an input module that can handle this file. */ | |
332 | for (i = 0; inputs[i]; i++) { | |
333 | if (inputs[i]->format_match(filename.c_str())) | |
334 | break; | |
335 | } | |
336 | ||
337 | /* Return NULL if no input module wanted to touch this. */ | |
338 | if (!inputs[i]) { | |
339 | g_critical("Error: no matching input module found."); | |
340 | return NULL; | |
341 | } | |
342 | ||
343 | return inputs[i]; | |
344 | } | |
345 | ||
346 | sr_input* SigSession::load_input_file_format(const string &filename, | |
347 | function<void (const QString)> error_handler, | |
348 | sr_input_format *format) | |
349 | { | |
350 | struct stat st; | |
351 | sr_input *in; | |
352 | ||
353 | if (!format && !(format = | |
354 | determine_input_file_format(filename.c_str()))) { | |
355 | /* The exact cause was already logged. */ | |
356 | return NULL; | |
357 | } | |
358 | ||
359 | if (stat(filename.c_str(), &st) == -1) { | |
360 | error_handler(tr("Failed to load file")); | |
361 | return NULL; | |
362 | } | |
363 | ||
364 | /* Initialize the input module. */ | |
365 | if (!(in = new sr_input)) { | |
366 | qDebug("Failed to allocate input module.\n"); | |
367 | return NULL; | |
368 | } | |
369 | ||
370 | in->format = format; | |
371 | in->param = NULL; | |
372 | if (in->format->init && | |
373 | in->format->init(in, filename.c_str()) != SR_OK) { | |
374 | qDebug("Input format init failed.\n"); | |
375 | return NULL; | |
376 | } | |
377 | ||
378 | sr_session_new(); | |
379 | ||
380 | if (sr_session_dev_add(in->sdi) != SR_OK) { | |
381 | qDebug("Failed to use device.\n"); | |
382 | sr_session_destroy(); | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | return in; | |
387 | } | |
388 | ||
19adbc2c | 389 | void SigSession::update_signals(shared_ptr<DevInst> dev_inst) |
b087ba7f | 390 | { |
19adbc2c | 391 | assert(dev_inst); |
b087ba7f JH |
392 | assert(_capture_state == Stopped); |
393 | ||
b087ba7f | 394 | unsigned int logic_probe_count = 0; |
b087ba7f | 395 | |
82c7f640 JH |
396 | // Clear the decode traces |
397 | _decode_traces.clear(); | |
398 | ||
b087ba7f | 399 | // Detect what data types we will receive |
19adbc2c JH |
400 | if(dev_inst) { |
401 | assert(dev_inst->dev_inst()); | |
402 | for (const GSList *l = dev_inst->dev_inst()->probes; | |
403 | l; l = l->next) { | |
b087ba7f JH |
404 | const sr_probe *const probe = (const sr_probe *)l->data; |
405 | if (!probe->enabled) | |
406 | continue; | |
407 | ||
408 | switch(probe->type) { | |
409 | case SR_PROBE_LOGIC: | |
410 | logic_probe_count++; | |
411 | break; | |
b087ba7f JH |
412 | } |
413 | } | |
414 | } | |
415 | ||
bb2cdfff | 416 | // Create data containers for the logic data snapshots |
b087ba7f JH |
417 | { |
418 | lock_guard<mutex> data_lock(_data_mutex); | |
419 | ||
420 | _logic_data.reset(); | |
421 | if (logic_probe_count != 0) { | |
422 | _logic_data.reset(new data::Logic( | |
423 | logic_probe_count)); | |
424 | assert(_logic_data); | |
425 | } | |
b087ba7f JH |
426 | } |
427 | ||
428 | // Make the Signals list | |
39e680c2 | 429 | do { |
b087ba7f JH |
430 | lock_guard<mutex> lock(_signals_mutex); |
431 | ||
432 | _signals.clear(); | |
433 | ||
19adbc2c | 434 | if(!dev_inst) |
39e680c2 JH |
435 | break; |
436 | ||
19adbc2c JH |
437 | assert(dev_inst->dev_inst()); |
438 | for (const GSList *l = dev_inst->dev_inst()->probes; | |
439 | l; l = l->next) { | |
39e680c2 JH |
440 | shared_ptr<view::Signal> signal; |
441 | sr_probe *const probe = (sr_probe *)l->data; | |
442 | assert(probe); | |
443 | ||
444 | switch(probe->type) { | |
445 | case SR_PROBE_LOGIC: | |
446 | signal = shared_ptr<view::Signal>( | |
83c23cc9 | 447 | new view::LogicSignal(dev_inst, |
8d3e0764 | 448 | probe, _logic_data)); |
39e680c2 JH |
449 | break; |
450 | ||
451 | case SR_PROBE_ANALOG: | |
bb2cdfff JH |
452 | { |
453 | shared_ptr<data::Analog> data( | |
454 | new data::Analog()); | |
39e680c2 | 455 | signal = shared_ptr<view::Signal>( |
83c23cc9 | 456 | new view::AnalogSignal(dev_inst, |
8d3e0764 | 457 | probe, data)); |
39e680c2 | 458 | break; |
bb2cdfff | 459 | } |
39e680c2 JH |
460 | |
461 | default: | |
462 | assert(0); | |
463 | break; | |
b087ba7f | 464 | } |
39e680c2 JH |
465 | |
466 | assert(signal); | |
467 | _signals.push_back(signal); | |
b087ba7f | 468 | } |
39e680c2 JH |
469 | |
470 | } while(0); | |
b087ba7f JH |
471 | |
472 | signals_changed(); | |
473 | } | |
474 | ||
2b49eeb0 JH |
475 | bool SigSession::is_trigger_enabled() const |
476 | { | |
19adbc2c JH |
477 | assert(_dev_inst); |
478 | assert(_dev_inst->dev_inst()); | |
479 | for (const GSList *l = _dev_inst->dev_inst()->probes; l; l = l->next) { | |
2b49eeb0 JH |
480 | const sr_probe *const p = (const sr_probe *)l->data; |
481 | assert(p); | |
482 | if (p->trigger && p->trigger[0] != '\0') | |
483 | return true; | |
484 | } | |
485 | ||
486 | return false; | |
487 | } | |
488 | ||
3ddcc083 JH |
489 | shared_ptr<view::Signal> SigSession::signal_from_probe( |
490 | const sr_probe *probe) const | |
491 | { | |
492 | lock_guard<mutex> lock(_signals_mutex); | |
493 | BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) { | |
494 | assert(sig); | |
495 | if (sig->probe() == probe) | |
496 | return sig; | |
497 | } | |
498 | return shared_ptr<view::Signal>(); | |
499 | } | |
500 | ||
deef291c JH |
501 | void SigSession::read_sample_rate(const sr_dev_inst *const sdi) |
502 | { | |
503 | GVariant *gvar; | |
504 | uint64_t sample_rate = 0; | |
505 | ||
506 | // Read out the sample rate | |
507 | if(sdi->driver) | |
508 | { | |
68162c29 BV |
509 | const int ret = sr_config_get(sdi->driver, sdi, NULL, |
510 | SR_CONF_SAMPLERATE, &gvar); | |
deef291c JH |
511 | if (ret != SR_OK) { |
512 | qDebug("Failed to get samplerate\n"); | |
513 | return; | |
514 | } | |
515 | ||
516 | sample_rate = g_variant_get_uint64(gvar); | |
517 | g_variant_unref(gvar); | |
518 | } | |
519 | ||
bb2cdfff JH |
520 | // Set the sample rate of all data |
521 | const set< shared_ptr<data::SignalData> > data_set = get_data(); | |
522 | BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) { | |
523 | assert(data); | |
524 | data->set_samplerate(sample_rate); | |
525 | } | |
deef291c JH |
526 | } |
527 | ||
e8c9f8a5 | 528 | void SigSession::load_session_thread_proc( |
f2edb557 | 529 | function<void (const QString)> error_handler) |
8a67bd9e | 530 | { |
e8c9f8a5 | 531 | (void)error_handler; |
728e5ef7 | 532 | |
e8c9f8a5 JH |
533 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
534 | ||
535 | set_capture_state(Running); | |
536 | ||
537 | sr_session_run(); | |
538 | ||
539 | sr_session_destroy(); | |
540 | set_capture_state(Stopped); | |
541 | ||
542 | // Confirm that SR_DF_END was received | |
543 | assert(!_cur_logic_snapshot); | |
bb2cdfff | 544 | assert(_cur_analog_snapshots.empty()); |
e8c9f8a5 JH |
545 | } |
546 | ||
547 | void SigSession::load_input_thread_proc(const string name, | |
548 | sr_input *in, function<void (const QString)> error_handler) | |
549 | { | |
550 | (void)error_handler; | |
551 | ||
552 | assert(in); | |
553 | assert(in->format); | |
8a67bd9e | 554 | |
5045f16d | 555 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
8a67bd9e | 556 | |
8a67bd9e JH |
557 | set_capture_state(Running); |
558 | ||
e8c9f8a5 | 559 | in->format->loadfile(in, name.c_str()); |
8a67bd9e | 560 | |
728e5ef7 | 561 | sr_session_destroy(); |
8a67bd9e | 562 | set_capture_state(Stopped); |
1a2fe44c JH |
563 | |
564 | // Confirm that SR_DF_END was received | |
565 | assert(!_cur_logic_snapshot); | |
bb2cdfff | 566 | assert(_cur_analog_snapshots.empty()); |
728e5ef7 JH |
567 | |
568 | delete in; | |
8a67bd9e JH |
569 | } |
570 | ||
19adbc2c | 571 | void SigSession::sample_thread_proc(shared_ptr<DevInst> dev_inst, |
f2edb557 | 572 | function<void (const QString)> error_handler) |
274d4f13 | 573 | { |
19adbc2c JH |
574 | assert(dev_inst); |
575 | assert(dev_inst->dev_inst()); | |
f2edb557 | 576 | assert(error_handler); |
be73bdfa | 577 | |
274d4f13 | 578 | sr_session_new(); |
5045f16d | 579 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
274d4f13 | 580 | |
19adbc2c | 581 | if (sr_session_dev_add(dev_inst->dev_inst()) != SR_OK) { |
f2edb557 | 582 | error_handler(tr("Failed to use device.")); |
274d4f13 JH |
583 | sr_session_destroy(); |
584 | return; | |
585 | } | |
586 | ||
eec446e1 | 587 | if (sr_session_start() != SR_OK) { |
f2edb557 | 588 | error_handler(tr("Failed to start session.")); |
eec446e1 JH |
589 | return; |
590 | } | |
591 | ||
2b49eeb0 | 592 | set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running); |
eec446e1 JH |
593 | |
594 | sr_session_run(); | |
595 | sr_session_destroy(); | |
596 | ||
597 | set_capture_state(Stopped); | |
1a2fe44c JH |
598 | |
599 | // Confirm that SR_DF_END was received | |
bb2cdfff JH |
600 | if (_cur_logic_snapshot) |
601 | { | |
602 | qDebug("SR_DF_END was not received."); | |
603 | assert(0); | |
604 | } | |
eec446e1 JH |
605 | } |
606 | ||
607 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
608 | { | |
deef291c | 609 | read_sample_rate(sdi); |
be73bdfa JH |
610 | } |
611 | ||
612 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
613 | const sr_datafeed_meta &meta) | |
614 | { | |
94fe58ef UH |
615 | (void)sdi; |
616 | ||
be73bdfa JH |
617 | for (const GSList *l = meta.config; l; l = l->next) { |
618 | const sr_config *const src = (const sr_config*)l->data; | |
619 | switch (src->key) { | |
620 | case SR_CONF_SAMPLERATE: | |
621 | /// @todo handle samplerate changes | |
622 | /// samplerate = (uint64_t *)src->value; | |
623 | break; | |
624 | default: | |
625 | // Unknown metadata is not an error. | |
626 | break; | |
627 | } | |
aba1dd16 | 628 | } |
74043039 JH |
629 | |
630 | signals_changed(); | |
aba1dd16 JH |
631 | } |
632 | ||
9c112671 JH |
633 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
634 | { | |
635 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
636 | |
637 | if (!_logic_data) | |
9c112671 | 638 | { |
79efbc53 JH |
639 | qDebug() << "Unexpected logic packet"; |
640 | return; | |
641 | } | |
be73bdfa | 642 | |
79efbc53 JH |
643 | if (!_cur_logic_snapshot) |
644 | { | |
bb2cdfff | 645 | // This could be the first packet after a trigger |
2b49eeb0 JH |
646 | set_capture_state(Running); |
647 | ||
9c112671 | 648 | // Create a new data snapshot |
1b1ec774 | 649 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
27d7c96b | 650 | new data::LogicSnapshot(logic, _dev_inst->get_sample_limit())); |
9c112671 JH |
651 | _logic_data->push_snapshot(_cur_logic_snapshot); |
652 | } | |
653 | else | |
654 | { | |
655 | // Append to the existing data snapshot | |
656 | _cur_logic_snapshot->append_payload(logic); | |
657 | } | |
658 | ||
659 | data_updated(); | |
660 | } | |
661 | ||
aba1dd16 JH |
662 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
663 | { | |
664 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 | 665 | |
bb2cdfff JH |
666 | const unsigned int probe_count = g_slist_length(analog.probes); |
667 | const size_t sample_count = analog.num_samples / probe_count; | |
668 | const float *data = analog.data; | |
669 | bool sweep_beginning = false; | |
be73bdfa | 670 | |
bb2cdfff | 671 | for (GSList *p = analog.probes; p; p = p->next) |
79efbc53 | 672 | { |
bb2cdfff | 673 | shared_ptr<data::AnalogSnapshot> snapshot; |
2b49eeb0 | 674 | |
bb2cdfff JH |
675 | sr_probe *const probe = (sr_probe*)p->data; |
676 | assert(probe); | |
677 | ||
678 | // Try to get the snapshot of the probe | |
679 | const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >:: | |
680 | iterator iter = _cur_analog_snapshots.find(probe); | |
681 | if (iter != _cur_analog_snapshots.end()) | |
682 | snapshot = (*iter).second; | |
683 | else | |
684 | { | |
685 | // If no snapshot was found, this means we havn't | |
686 | // created one yet. i.e. this is the first packet | |
687 | // in the sweep containing this snapshot. | |
688 | sweep_beginning = true; | |
689 | ||
690 | // Create a snapshot, keep it in the maps of probes | |
691 | snapshot = shared_ptr<data::AnalogSnapshot>( | |
27d7c96b | 692 | new data::AnalogSnapshot(_dev_inst->get_sample_limit())); |
bb2cdfff JH |
693 | _cur_analog_snapshots[probe] = snapshot; |
694 | ||
695 | // Find the annalog data associated with the probe | |
696 | shared_ptr<view::AnalogSignal> sig = | |
697 | dynamic_pointer_cast<view::AnalogSignal>( | |
698 | signal_from_probe(probe)); | |
699 | assert(sig); | |
700 | ||
701 | shared_ptr<data::Analog> data(sig->analog_data()); | |
702 | assert(data); | |
703 | ||
704 | // Push the snapshot into the analog data. | |
705 | data->push_snapshot(snapshot); | |
706 | } | |
707 | ||
708 | assert(snapshot); | |
709 | ||
710 | // Append the samples in the snapshot | |
711 | snapshot->append_interleaved_samples(data++, sample_count, | |
712 | probe_count); | |
aba1dd16 | 713 | } |
bb2cdfff JH |
714 | |
715 | if (sweep_beginning) { | |
716 | // This could be the first packet after a trigger | |
717 | set_capture_state(Running); | |
aba1dd16 JH |
718 | } |
719 | ||
720 | data_updated(); | |
721 | } | |
9c112671 | 722 | |
b0e1d01d JH |
723 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
724 | const struct sr_datafeed_packet *packet) | |
725 | { | |
726 | assert(sdi); | |
727 | assert(packet); | |
728 | ||
729 | switch (packet->type) { | |
730 | case SR_DF_HEADER: | |
eec446e1 | 731 | feed_in_header(sdi); |
b0e1d01d JH |
732 | break; |
733 | ||
be73bdfa | 734 | case SR_DF_META: |
aba1dd16 | 735 | assert(packet->payload); |
be73bdfa JH |
736 | feed_in_meta(sdi, |
737 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
738 | break; |
739 | ||
2e2946fe | 740 | case SR_DF_LOGIC: |
28a4c9c5 | 741 | assert(packet->payload); |
9c112671 | 742 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
743 | break; |
744 | ||
aba1dd16 JH |
745 | case SR_DF_ANALOG: |
746 | assert(packet->payload); | |
747 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
748 | break; | |
749 | ||
2953961c | 750 | case SR_DF_END: |
2e2946fe JH |
751 | { |
752 | { | |
753 | lock_guard<mutex> lock(_data_mutex); | |
754 | _cur_logic_snapshot.reset(); | |
bb2cdfff | 755 | _cur_analog_snapshots.clear(); |
2e2946fe | 756 | } |
04abfae9 | 757 | data_updated(); |
2953961c JH |
758 | break; |
759 | } | |
2e2946fe | 760 | } |
2953961c JH |
761 | } |
762 | ||
04abfae9 | 763 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 764 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 765 | { |
5045f16d | 766 | (void) cb_data; |
04abfae9 JH |
767 | assert(_session); |
768 | _session->data_feed_in(sdi, packet); | |
2953961c | 769 | } |
51e77110 JH |
770 | |
771 | } // namespace pv |