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