]>
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 | ||
2b49eeb0 JH |
482 | bool SigSession::is_trigger_enabled() const |
483 | { | |
19adbc2c JH |
484 | assert(_dev_inst); |
485 | assert(_dev_inst->dev_inst()); | |
486 | for (const GSList *l = _dev_inst->dev_inst()->probes; l; l = l->next) { | |
2b49eeb0 JH |
487 | const sr_probe *const p = (const sr_probe *)l->data; |
488 | assert(p); | |
489 | if (p->trigger && p->trigger[0] != '\0') | |
490 | return true; | |
491 | } | |
492 | ||
493 | return false; | |
494 | } | |
495 | ||
3ddcc083 JH |
496 | shared_ptr<view::Signal> SigSession::signal_from_probe( |
497 | const sr_probe *probe) const | |
498 | { | |
499 | lock_guard<mutex> lock(_signals_mutex); | |
500 | BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) { | |
501 | assert(sig); | |
502 | if (sig->probe() == probe) | |
503 | return sig; | |
504 | } | |
505 | return shared_ptr<view::Signal>(); | |
506 | } | |
507 | ||
deef291c JH |
508 | void SigSession::read_sample_rate(const sr_dev_inst *const sdi) |
509 | { | |
510 | GVariant *gvar; | |
511 | uint64_t sample_rate = 0; | |
512 | ||
513 | // Read out the sample rate | |
514 | if(sdi->driver) | |
515 | { | |
68162c29 BV |
516 | const int ret = sr_config_get(sdi->driver, sdi, NULL, |
517 | SR_CONF_SAMPLERATE, &gvar); | |
deef291c JH |
518 | if (ret != SR_OK) { |
519 | qDebug("Failed to get samplerate\n"); | |
520 | return; | |
521 | } | |
522 | ||
523 | sample_rate = g_variant_get_uint64(gvar); | |
524 | g_variant_unref(gvar); | |
525 | } | |
526 | ||
bb2cdfff JH |
527 | // Set the sample rate of all data |
528 | const set< shared_ptr<data::SignalData> > data_set = get_data(); | |
529 | BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) { | |
530 | assert(data); | |
531 | data->set_samplerate(sample_rate); | |
532 | } | |
deef291c JH |
533 | } |
534 | ||
e8c9f8a5 | 535 | void SigSession::load_session_thread_proc( |
f2edb557 | 536 | function<void (const QString)> error_handler) |
8a67bd9e | 537 | { |
e8c9f8a5 | 538 | (void)error_handler; |
728e5ef7 | 539 | |
e8c9f8a5 JH |
540 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
541 | ||
542 | set_capture_state(Running); | |
543 | ||
544 | sr_session_run(); | |
545 | ||
546 | sr_session_destroy(); | |
547 | set_capture_state(Stopped); | |
548 | ||
549 | // Confirm that SR_DF_END was received | |
550 | assert(!_cur_logic_snapshot); | |
bb2cdfff | 551 | assert(_cur_analog_snapshots.empty()); |
e8c9f8a5 JH |
552 | } |
553 | ||
554 | void SigSession::load_input_thread_proc(const string name, | |
555 | sr_input *in, function<void (const QString)> error_handler) | |
556 | { | |
557 | (void)error_handler; | |
558 | ||
559 | assert(in); | |
560 | assert(in->format); | |
8a67bd9e | 561 | |
5045f16d | 562 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
8a67bd9e | 563 | |
8a67bd9e JH |
564 | set_capture_state(Running); |
565 | ||
e8c9f8a5 | 566 | in->format->loadfile(in, name.c_str()); |
8a67bd9e | 567 | |
728e5ef7 | 568 | sr_session_destroy(); |
8a67bd9e | 569 | set_capture_state(Stopped); |
1a2fe44c JH |
570 | |
571 | // Confirm that SR_DF_END was received | |
572 | assert(!_cur_logic_snapshot); | |
bb2cdfff | 573 | assert(_cur_analog_snapshots.empty()); |
728e5ef7 JH |
574 | |
575 | delete in; | |
8a67bd9e JH |
576 | } |
577 | ||
94574501 | 578 | void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst, |
f2edb557 | 579 | function<void (const QString)> error_handler) |
274d4f13 | 580 | { |
19adbc2c JH |
581 | assert(dev_inst); |
582 | assert(dev_inst->dev_inst()); | |
f2edb557 | 583 | assert(error_handler); |
be73bdfa | 584 | |
274d4f13 | 585 | sr_session_new(); |
5045f16d | 586 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); |
274d4f13 | 587 | |
19adbc2c | 588 | if (sr_session_dev_add(dev_inst->dev_inst()) != SR_OK) { |
f2edb557 | 589 | error_handler(tr("Failed to use device.")); |
274d4f13 JH |
590 | sr_session_destroy(); |
591 | return; | |
592 | } | |
593 | ||
eec446e1 | 594 | if (sr_session_start() != SR_OK) { |
f2edb557 | 595 | error_handler(tr("Failed to start session.")); |
eec446e1 JH |
596 | return; |
597 | } | |
598 | ||
2b49eeb0 | 599 | set_capture_state(is_trigger_enabled() ? AwaitingTrigger : Running); |
eec446e1 JH |
600 | |
601 | sr_session_run(); | |
602 | sr_session_destroy(); | |
603 | ||
604 | set_capture_state(Stopped); | |
1a2fe44c JH |
605 | |
606 | // Confirm that SR_DF_END was received | |
bb2cdfff JH |
607 | if (_cur_logic_snapshot) |
608 | { | |
609 | qDebug("SR_DF_END was not received."); | |
610 | assert(0); | |
611 | } | |
eec446e1 JH |
612 | } |
613 | ||
614 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
615 | { | |
deef291c | 616 | read_sample_rate(sdi); |
be73bdfa JH |
617 | } |
618 | ||
619 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
620 | const sr_datafeed_meta &meta) | |
621 | { | |
94fe58ef UH |
622 | (void)sdi; |
623 | ||
be73bdfa JH |
624 | for (const GSList *l = meta.config; l; l = l->next) { |
625 | const sr_config *const src = (const sr_config*)l->data; | |
626 | switch (src->key) { | |
627 | case SR_CONF_SAMPLERATE: | |
628 | /// @todo handle samplerate changes | |
629 | /// samplerate = (uint64_t *)src->value; | |
630 | break; | |
631 | default: | |
632 | // Unknown metadata is not an error. | |
633 | break; | |
634 | } | |
aba1dd16 | 635 | } |
74043039 JH |
636 | |
637 | signals_changed(); | |
aba1dd16 JH |
638 | } |
639 | ||
9c112671 JH |
640 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
641 | { | |
642 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
643 | |
644 | if (!_logic_data) | |
9c112671 | 645 | { |
79efbc53 JH |
646 | qDebug() << "Unexpected logic packet"; |
647 | return; | |
648 | } | |
be73bdfa | 649 | |
79efbc53 JH |
650 | if (!_cur_logic_snapshot) |
651 | { | |
bb2cdfff | 652 | // This could be the first packet after a trigger |
2b49eeb0 JH |
653 | set_capture_state(Running); |
654 | ||
9c112671 | 655 | // Create a new data snapshot |
1b1ec774 | 656 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
27d7c96b | 657 | new data::LogicSnapshot(logic, _dev_inst->get_sample_limit())); |
9c112671 JH |
658 | _logic_data->push_snapshot(_cur_logic_snapshot); |
659 | } | |
660 | else | |
661 | { | |
662 | // Append to the existing data snapshot | |
663 | _cur_logic_snapshot->append_payload(logic); | |
664 | } | |
665 | ||
666 | data_updated(); | |
667 | } | |
668 | ||
aba1dd16 JH |
669 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
670 | { | |
671 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 | 672 | |
bb2cdfff JH |
673 | const unsigned int probe_count = g_slist_length(analog.probes); |
674 | const size_t sample_count = analog.num_samples / probe_count; | |
675 | const float *data = analog.data; | |
676 | bool sweep_beginning = false; | |
be73bdfa | 677 | |
bb2cdfff | 678 | for (GSList *p = analog.probes; p; p = p->next) |
79efbc53 | 679 | { |
bb2cdfff | 680 | shared_ptr<data::AnalogSnapshot> snapshot; |
2b49eeb0 | 681 | |
bb2cdfff JH |
682 | sr_probe *const probe = (sr_probe*)p->data; |
683 | assert(probe); | |
684 | ||
685 | // Try to get the snapshot of the probe | |
686 | const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >:: | |
687 | iterator iter = _cur_analog_snapshots.find(probe); | |
688 | if (iter != _cur_analog_snapshots.end()) | |
689 | snapshot = (*iter).second; | |
690 | else | |
691 | { | |
692 | // If no snapshot was found, this means we havn't | |
693 | // created one yet. i.e. this is the first packet | |
694 | // in the sweep containing this snapshot. | |
695 | sweep_beginning = true; | |
696 | ||
697 | // Create a snapshot, keep it in the maps of probes | |
698 | snapshot = shared_ptr<data::AnalogSnapshot>( | |
27d7c96b | 699 | new data::AnalogSnapshot(_dev_inst->get_sample_limit())); |
bb2cdfff JH |
700 | _cur_analog_snapshots[probe] = snapshot; |
701 | ||
702 | // Find the annalog data associated with the probe | |
703 | shared_ptr<view::AnalogSignal> sig = | |
704 | dynamic_pointer_cast<view::AnalogSignal>( | |
705 | signal_from_probe(probe)); | |
706 | assert(sig); | |
707 | ||
708 | shared_ptr<data::Analog> data(sig->analog_data()); | |
709 | assert(data); | |
710 | ||
711 | // Push the snapshot into the analog data. | |
712 | data->push_snapshot(snapshot); | |
713 | } | |
714 | ||
715 | assert(snapshot); | |
716 | ||
717 | // Append the samples in the snapshot | |
718 | snapshot->append_interleaved_samples(data++, sample_count, | |
719 | probe_count); | |
aba1dd16 | 720 | } |
bb2cdfff JH |
721 | |
722 | if (sweep_beginning) { | |
723 | // This could be the first packet after a trigger | |
724 | set_capture_state(Running); | |
aba1dd16 JH |
725 | } |
726 | ||
727 | data_updated(); | |
728 | } | |
9c112671 | 729 | |
b0e1d01d JH |
730 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
731 | const struct sr_datafeed_packet *packet) | |
732 | { | |
733 | assert(sdi); | |
734 | assert(packet); | |
735 | ||
736 | switch (packet->type) { | |
737 | case SR_DF_HEADER: | |
eec446e1 | 738 | feed_in_header(sdi); |
b0e1d01d JH |
739 | break; |
740 | ||
be73bdfa | 741 | case SR_DF_META: |
aba1dd16 | 742 | assert(packet->payload); |
be73bdfa JH |
743 | feed_in_meta(sdi, |
744 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
745 | break; |
746 | ||
2e2946fe | 747 | case SR_DF_LOGIC: |
28a4c9c5 | 748 | assert(packet->payload); |
9c112671 | 749 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
750 | break; |
751 | ||
aba1dd16 JH |
752 | case SR_DF_ANALOG: |
753 | assert(packet->payload); | |
754 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
755 | break; | |
756 | ||
2953961c | 757 | case SR_DF_END: |
2e2946fe JH |
758 | { |
759 | { | |
760 | lock_guard<mutex> lock(_data_mutex); | |
761 | _cur_logic_snapshot.reset(); | |
bb2cdfff | 762 | _cur_analog_snapshots.clear(); |
2e2946fe | 763 | } |
04abfae9 | 764 | data_updated(); |
2953961c JH |
765 | break; |
766 | } | |
2e2946fe | 767 | } |
2953961c JH |
768 | } |
769 | ||
04abfae9 | 770 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 771 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 772 | { |
5045f16d | 773 | (void) cb_data; |
04abfae9 JH |
774 | assert(_session); |
775 | _session->data_feed_in(sdi, packet); | |
2953961c | 776 | } |
51e77110 JH |
777 | |
778 | } // namespace pv |