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