]>
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" |
ae2d1bc5 | 29 | #include "device/file.h" |
119aff65 | 30 | |
1b1ec774 JH |
31 | #include "data/analog.h" |
32 | #include "data/analogsnapshot.h" | |
6e89374a | 33 | #include "data/decoderstack.h" |
1b1ec774 JH |
34 | #include "data/logic.h" |
35 | #include "data/logicsnapshot.h" | |
7491a29f | 36 | #include "data/decode/decoder.h" |
82c7f640 | 37 | |
aba1dd16 | 38 | #include "view/analogsignal.h" |
b9329558 | 39 | #include "view/decodetrace.h" |
8d634081 | 40 | #include "view/logicsignal.h" |
28a4c9c5 | 41 | |
3b68d03d JH |
42 | #include <cassert> |
43 | #include <mutex> | |
e92cd4e4 JH |
44 | #include <stdexcept> |
45 | ||
728e5ef7 JH |
46 | #include <sys/stat.h> |
47 | ||
79efbc53 JH |
48 | #include <QDebug> |
49 | ||
f9abf97e | 50 | using std::dynamic_pointer_cast; |
d2344534 | 51 | using std::function; |
3b68d03d JH |
52 | using std::lock_guard; |
53 | using std::mutex; | |
d873f4d6 | 54 | using std::list; |
819f4c25 | 55 | using std::map; |
02412f0b | 56 | using std::set; |
f9abf97e | 57 | using std::shared_ptr; |
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 | |
bb3030b3 ML |
66 | // TODO: This should not be necessary |
67 | struct sr_session *SigSession::_sr_session = NULL; | |
68 | ||
dc0867ff JH |
69 | SigSession::SigSession(DeviceManager &device_manager) : |
70 | _device_manager(device_manager), | |
5b7cf66c | 71 | _capture_state(Stopped) |
2953961c JH |
72 | { |
73 | // TODO: This should not be necessary | |
04abfae9 | 74 | _session = this; |
d873f4d6 JH |
75 | |
76 | set_default_device(); | |
2953961c JH |
77 | } |
78 | ||
79 | SigSession::~SigSession() | |
80 | { | |
85843b14 JH |
81 | using pv::device::Device; |
82 | ||
04463625 | 83 | // Stop and join to the thread |
5b7cf66c JH |
84 | stop_capture(); |
85 | ||
7f255cd6 BV |
86 | if (_dev_inst) |
87 | _dev_inst->release(); | |
0f784939 | 88 | |
2953961c | 89 | // TODO: This should not be necessary |
04abfae9 | 90 | _session = NULL; |
2953961c JH |
91 | } |
92 | ||
94574501 | 93 | shared_ptr<device::DevInst> SigSession::get_device() const |
dc0867ff | 94 | { |
19adbc2c | 95 | return _dev_inst; |
dc0867ff JH |
96 | } |
97 | ||
ae2d1bc5 JH |
98 | void SigSession::set_device( |
99 | shared_ptr<device::DevInst> dev_inst) throw(QString) | |
d64d1596 | 100 | { |
85843b14 JH |
101 | using pv::device::Device; |
102 | ||
ab973f47 BV |
103 | if (!dev_inst) |
104 | return; | |
105 | ||
82596f46 JH |
106 | // Ensure we are not capturing before setting the device |
107 | stop_capture(); | |
108 | ||
ae2d1bc5 | 109 | if (_dev_inst) { |
bb3030b3 | 110 | sr_session_datafeed_callback_remove_all(_sr_session); |
996b7c9d | 111 | _dev_inst->release(); |
ae2d1bc5 JH |
112 | } |
113 | ||
114 | _dev_inst = dev_inst; | |
115 | _decode_traces.clear(); | |
85843b14 | 116 | |
ae2d1bc5 | 117 | if (dev_inst) { |
996b7c9d | 118 | dev_inst->use(this); |
bb3030b3 | 119 | sr_session_datafeed_callback_add(_sr_session, data_feed_in_proc, NULL); |
ae2d1bc5 JH |
120 | update_signals(dev_inst); |
121 | } | |
122 | } | |
85843b14 | 123 | |
ae2d1bc5 JH |
124 | void SigSession::set_file(const string &name) throw(QString) |
125 | { | |
5ab6596d JS |
126 | // Deselect the old device, because file type detection in File::create |
127 | // destroys the old session inside libsigrok. | |
ae2d1bc5 JH |
128 | set_device(shared_ptr<device::DevInst>()); |
129 | set_device(shared_ptr<device::DevInst>(device::File::create(name))); | |
d64d1596 JH |
130 | } |
131 | ||
6fd0b639 JH |
132 | void SigSession::set_default_device() |
133 | { | |
134 | shared_ptr<pv::device::DevInst> default_device; | |
135 | const list< shared_ptr<device::Device> > &devices = | |
136 | _device_manager.devices(); | |
137 | ||
138 | if (!devices.empty()) { | |
139 | // Fall back to the first device in the list. | |
140 | default_device = devices.front(); | |
141 | ||
142 | // Try and find the demo device and select that by default | |
d9aecf1f | 143 | for (shared_ptr<pv::device::Device> dev : devices) |
6fd0b639 JH |
144 | if (strcmp(dev->dev_inst()->driver->name, |
145 | "demo") == 0) { | |
146 | default_device = dev; | |
147 | break; | |
148 | } | |
149 | } | |
150 | ||
151 | set_device(default_device); | |
152 | } | |
153 | ||
996b7c9d | 154 | void SigSession::release_device(device::DevInst *dev_inst) |
dc0867ff | 155 | { |
19adbc2c | 156 | (void)dev_inst; |
996b7c9d | 157 | assert(_dev_inst.get() == dev_inst); |
dc0867ff JH |
158 | |
159 | assert(_capture_state == Stopped); | |
94574501 | 160 | _dev_inst = shared_ptr<device::DevInst>(); |
dc0867ff JH |
161 | } |
162 | ||
5b7cf66c JH |
163 | SigSession::capture_state SigSession::get_capture_state() const |
164 | { | |
949f8050 | 165 | lock_guard<mutex> lock(_sampling_mutex); |
5b7cf66c JH |
166 | return _capture_state; |
167 | } | |
168 | ||
d99dc9f2 | 169 | void SigSession::start_capture(function<void (const QString)> error_handler) |
2e2946fe | 170 | { |
5b7cf66c JH |
171 | stop_capture(); |
172 | ||
d64d1596 | 173 | // Check that a device instance has been selected. |
19adbc2c | 174 | if (!_dev_inst) { |
d64d1596 JH |
175 | qDebug() << "No device selected"; |
176 | return; | |
177 | } | |
178 | ||
19adbc2c JH |
179 | assert(_dev_inst->dev_inst()); |
180 | ||
6ac6242b | 181 | // Check that at least one channel is enabled |
9c48fa57 | 182 | const GSList *l; |
4871ed92 | 183 | for (l = _dev_inst->dev_inst()->channels; l; l = l->next) { |
6ac6242b ML |
184 | sr_channel *const channel = (sr_channel*)l->data; |
185 | assert(channel); | |
186 | if (channel->enabled) | |
9c48fa57 JH |
187 | break; |
188 | } | |
189 | ||
190 | if (!l) { | |
4871ed92 | 191 | error_handler(tr("No channels enabled.")); |
9c48fa57 JH |
192 | return; |
193 | } | |
194 | ||
195 | // Begin the session | |
3b68d03d | 196 | _sampling_thread = std::thread( |
19adbc2c JH |
197 | &SigSession::sample_thread_proc, this, _dev_inst, |
198 | error_handler); | |
2e2946fe JH |
199 | } |
200 | ||
5b7cf66c JH |
201 | void SigSession::stop_capture() |
202 | { | |
04463625 | 203 | if (get_capture_state() != Stopped) |
bb3030b3 | 204 | sr_session_stop(_sr_session); |
5b7cf66c JH |
205 | |
206 | // Check that sampling stopped | |
6d483b8b MC |
207 | if (_sampling_thread.joinable()) |
208 | _sampling_thread.join(); | |
5b7cf66c JH |
209 | } |
210 | ||
02412f0b JH |
211 | set< shared_ptr<data::SignalData> > SigSession::get_data() const |
212 | { | |
213 | lock_guard<mutex> lock(_signals_mutex); | |
214 | set< shared_ptr<data::SignalData> > data; | |
d9aecf1f | 215 | for (const shared_ptr<view::Signal> sig : _signals) { |
02412f0b JH |
216 | assert(sig); |
217 | data.insert(sig->data()); | |
218 | } | |
219 | ||
220 | return data; | |
221 | } | |
222 | ||
38eeddea | 223 | vector< shared_ptr<view::Signal> > SigSession::get_signals() const |
2e2946fe | 224 | { |
3868e5fa | 225 | lock_guard<mutex> lock(_signals_mutex); |
2e2946fe JH |
226 | return _signals; |
227 | } | |
228 | ||
269528f5 | 229 | #ifdef ENABLE_DECODE |
4e5a4405 | 230 | bool SigSession::add_decoder(srd_decoder *const dec) |
82c7f640 | 231 | { |
6ac6242b | 232 | map<const srd_channel*, shared_ptr<view::LogicSignal> > channels; |
7491a29f | 233 | shared_ptr<data::DecoderStack> decoder_stack; |
4e5a4405 | 234 | |
e92cd4e4 | 235 | try |
82c7f640 | 236 | { |
119aff65 | 237 | lock_guard<mutex> lock(_signals_mutex); |
e0fc5810 | 238 | |
4e5a4405 | 239 | // Create the decoder |
7491a29f | 240 | decoder_stack = shared_ptr<data::DecoderStack>( |
bdc5c3b0 | 241 | new data::DecoderStack(*this, dec)); |
4e5a4405 | 242 | |
6ac6242b ML |
243 | // Make a list of all the channels |
244 | std::vector<const srd_channel*> all_channels; | |
8bd26d8b | 245 | for(const GSList *i = dec->channels; i; i = i->next) |
6ac6242b | 246 | all_channels.push_back((const srd_channel*)i->data); |
8bd26d8b | 247 | for(const GSList *i = dec->opt_channels; i; i = i->next) |
6ac6242b | 248 | all_channels.push_back((const srd_channel*)i->data); |
d2f46d27 | 249 | |
6ac6242b ML |
250 | // Auto select the initial channels |
251 | for (const srd_channel *pdch : all_channels) | |
d9aecf1f | 252 | for (shared_ptr<view::Signal> s : _signals) |
4e5a4405 JH |
253 | { |
254 | shared_ptr<view::LogicSignal> l = | |
255 | dynamic_pointer_cast<view::LogicSignal>(s); | |
8bd26d8b | 256 | if (l && QString::fromUtf8(pdch->name). |
27e8df22 | 257 | toLower().contains( |
4e5a4405 | 258 | l->get_name().toLower())) |
6ac6242b | 259 | channels[pdch] = l; |
4e5a4405 | 260 | } |
4e5a4405 | 261 | |
7491a29f JH |
262 | assert(decoder_stack); |
263 | assert(!decoder_stack->stack().empty()); | |
264 | assert(decoder_stack->stack().front()); | |
6ac6242b | 265 | decoder_stack->stack().front()->set_channels(channels); |
4e5a4405 JH |
266 | |
267 | // Create the decode signal | |
b9329558 | 268 | shared_ptr<view::DecodeTrace> d( |
7491a29f | 269 | new view::DecodeTrace(*this, decoder_stack, |
06bb4e6a | 270 | _decode_traces.size())); |
82c7f640 JH |
271 | _decode_traces.push_back(d); |
272 | } | |
e92cd4e4 JH |
273 | catch(std::runtime_error e) |
274 | { | |
275 | return false; | |
276 | } | |
277 | ||
82c7f640 | 278 | signals_changed(); |
e92cd4e4 | 279 | |
7491a29f JH |
280 | // Do an initial decode |
281 | decoder_stack->begin_decode(); | |
282 | ||
e92cd4e4 | 283 | return true; |
82c7f640 JH |
284 | } |
285 | ||
b9329558 | 286 | vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const |
38eeddea JH |
287 | { |
288 | lock_guard<mutex> lock(_signals_mutex); | |
289 | return _decode_traces; | |
290 | } | |
291 | ||
b9329558 | 292 | void SigSession::remove_decode_signal(view::DecodeTrace *signal) |
c51482b3 | 293 | { |
f46e495e | 294 | for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++) |
c51482b3 JH |
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 | ||
94574501 | 313 | void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst) |
b087ba7f | 314 | { |
19adbc2c | 315 | assert(dev_inst); |
b087ba7f JH |
316 | assert(_capture_state == Stopped); |
317 | ||
6ac6242b | 318 | unsigned int logic_channel_count = 0; |
b087ba7f | 319 | |
82c7f640 JH |
320 | // Clear the decode traces |
321 | _decode_traces.clear(); | |
322 | ||
b087ba7f | 323 | // Detect what data types we will receive |
19adbc2c JH |
324 | if(dev_inst) { |
325 | assert(dev_inst->dev_inst()); | |
4871ed92 | 326 | for (const GSList *l = dev_inst->dev_inst()->channels; |
19adbc2c | 327 | l; l = l->next) { |
6ac6242b ML |
328 | const sr_channel *const channel = (const sr_channel *)l->data; |
329 | if (!channel->enabled) | |
b087ba7f JH |
330 | continue; |
331 | ||
6ac6242b | 332 | switch(channel->type) { |
4871ed92 | 333 | case SR_CHANNEL_LOGIC: |
6ac6242b | 334 | logic_channel_count++; |
b087ba7f | 335 | break; |
b087ba7f JH |
336 | } |
337 | } | |
338 | } | |
339 | ||
bb2cdfff | 340 | // Create data containers for the logic data snapshots |
b087ba7f JH |
341 | { |
342 | lock_guard<mutex> data_lock(_data_mutex); | |
343 | ||
344 | _logic_data.reset(); | |
6ac6242b | 345 | if (logic_channel_count != 0) { |
b087ba7f | 346 | _logic_data.reset(new data::Logic( |
6ac6242b | 347 | logic_channel_count)); |
b087ba7f JH |
348 | assert(_logic_data); |
349 | } | |
b087ba7f JH |
350 | } |
351 | ||
352 | // Make the Signals list | |
39e680c2 | 353 | do { |
b087ba7f JH |
354 | lock_guard<mutex> lock(_signals_mutex); |
355 | ||
356 | _signals.clear(); | |
357 | ||
19adbc2c | 358 | if(!dev_inst) |
39e680c2 JH |
359 | break; |
360 | ||
19adbc2c | 361 | assert(dev_inst->dev_inst()); |
4871ed92 | 362 | for (const GSList *l = dev_inst->dev_inst()->channels; |
19adbc2c | 363 | l; l = l->next) { |
39e680c2 | 364 | shared_ptr<view::Signal> signal; |
6ac6242b ML |
365 | sr_channel *const channel = (sr_channel *)l->data; |
366 | assert(channel); | |
39e680c2 | 367 | |
6ac6242b | 368 | switch(channel->type) { |
4871ed92 | 369 | case SR_CHANNEL_LOGIC: |
39e680c2 | 370 | signal = shared_ptr<view::Signal>( |
83c23cc9 | 371 | new view::LogicSignal(dev_inst, |
6ac6242b | 372 | channel, _logic_data)); |
39e680c2 JH |
373 | break; |
374 | ||
4871ed92 | 375 | case SR_CHANNEL_ANALOG: |
bb2cdfff JH |
376 | { |
377 | shared_ptr<data::Analog> data( | |
378 | new data::Analog()); | |
39e680c2 | 379 | signal = shared_ptr<view::Signal>( |
83c23cc9 | 380 | new view::AnalogSignal(dev_inst, |
6ac6242b | 381 | channel, data)); |
39e680c2 | 382 | break; |
bb2cdfff | 383 | } |
39e680c2 JH |
384 | |
385 | default: | |
386 | assert(0); | |
387 | break; | |
b087ba7f | 388 | } |
39e680c2 JH |
389 | |
390 | assert(signal); | |
391 | _signals.push_back(signal); | |
b087ba7f | 392 | } |
39e680c2 JH |
393 | |
394 | } while(0); | |
b087ba7f JH |
395 | |
396 | signals_changed(); | |
397 | } | |
398 | ||
6ac6242b ML |
399 | shared_ptr<view::Signal> SigSession::signal_from_channel( |
400 | const sr_channel *channel) const | |
3ddcc083 JH |
401 | { |
402 | lock_guard<mutex> lock(_signals_mutex); | |
d9aecf1f | 403 | for (shared_ptr<view::Signal> sig : _signals) { |
3ddcc083 | 404 | assert(sig); |
6ac6242b | 405 | if (sig->channel() == channel) |
3ddcc083 JH |
406 | return sig; |
407 | } | |
408 | return shared_ptr<view::Signal>(); | |
409 | } | |
410 | ||
deef291c JH |
411 | void SigSession::read_sample_rate(const sr_dev_inst *const sdi) |
412 | { | |
413 | GVariant *gvar; | |
414 | uint64_t sample_rate = 0; | |
415 | ||
416 | // Read out the sample rate | |
417 | if(sdi->driver) | |
418 | { | |
68162c29 BV |
419 | const int ret = sr_config_get(sdi->driver, sdi, NULL, |
420 | SR_CONF_SAMPLERATE, &gvar); | |
deef291c JH |
421 | if (ret != SR_OK) { |
422 | qDebug("Failed to get samplerate\n"); | |
423 | return; | |
424 | } | |
425 | ||
426 | sample_rate = g_variant_get_uint64(gvar); | |
427 | g_variant_unref(gvar); | |
428 | } | |
429 | ||
bb2cdfff JH |
430 | // Set the sample rate of all data |
431 | const set< shared_ptr<data::SignalData> > data_set = get_data(); | |
d9aecf1f | 432 | for (shared_ptr<data::SignalData> data : data_set) { |
bb2cdfff JH |
433 | assert(data); |
434 | data->set_samplerate(sample_rate); | |
435 | } | |
deef291c JH |
436 | } |
437 | ||
94574501 | 438 | void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst, |
f2edb557 | 439 | function<void (const QString)> error_handler) |
274d4f13 | 440 | { |
19adbc2c JH |
441 | assert(dev_inst); |
442 | assert(dev_inst->dev_inst()); | |
f2edb557 | 443 | assert(error_handler); |
be73bdfa | 444 | |
ae2d1bc5 | 445 | read_sample_rate(dev_inst->dev_inst()); |
274d4f13 | 446 | |
ae2d1bc5 JH |
447 | try { |
448 | dev_inst->start(); | |
449 | } catch(const QString e) { | |
450 | error_handler(e); | |
eec446e1 JH |
451 | return; |
452 | } | |
453 | ||
bb3030b3 | 454 | set_capture_state(sr_session_trigger_get(_sr_session) ? |
07dcf561 | 455 | AwaitingTrigger : Running); |
eec446e1 | 456 | |
ae2d1bc5 | 457 | dev_inst->run(); |
eec446e1 | 458 | set_capture_state(Stopped); |
1a2fe44c JH |
459 | |
460 | // Confirm that SR_DF_END was received | |
bb2cdfff JH |
461 | if (_cur_logic_snapshot) |
462 | { | |
463 | qDebug("SR_DF_END was not received."); | |
464 | assert(0); | |
465 | } | |
eec446e1 JH |
466 | } |
467 | ||
468 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
469 | { | |
deef291c | 470 | read_sample_rate(sdi); |
be73bdfa JH |
471 | } |
472 | ||
473 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
474 | const sr_datafeed_meta &meta) | |
475 | { | |
94fe58ef UH |
476 | (void)sdi; |
477 | ||
be73bdfa JH |
478 | for (const GSList *l = meta.config; l; l = l->next) { |
479 | const sr_config *const src = (const sr_config*)l->data; | |
480 | switch (src->key) { | |
481 | case SR_CONF_SAMPLERATE: | |
482 | /// @todo handle samplerate changes | |
483 | /// samplerate = (uint64_t *)src->value; | |
484 | break; | |
485 | default: | |
486 | // Unknown metadata is not an error. | |
487 | break; | |
488 | } | |
aba1dd16 | 489 | } |
74043039 JH |
490 | |
491 | signals_changed(); | |
aba1dd16 JH |
492 | } |
493 | ||
82f50b10 JH |
494 | void SigSession::feed_in_frame_begin() |
495 | { | |
496 | if (_cur_logic_snapshot || !_cur_analog_snapshots.empty()) | |
497 | frame_began(); | |
498 | } | |
499 | ||
9c112671 JH |
500 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) |
501 | { | |
502 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 JH |
503 | |
504 | if (!_logic_data) | |
9c112671 | 505 | { |
79efbc53 JH |
506 | qDebug() << "Unexpected logic packet"; |
507 | return; | |
508 | } | |
be73bdfa | 509 | |
79efbc53 JH |
510 | if (!_cur_logic_snapshot) |
511 | { | |
bb2cdfff | 512 | // This could be the first packet after a trigger |
2b49eeb0 JH |
513 | set_capture_state(Running); |
514 | ||
9c112671 | 515 | // Create a new data snapshot |
1b1ec774 | 516 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( |
27d7c96b | 517 | new data::LogicSnapshot(logic, _dev_inst->get_sample_limit())); |
9c112671 | 518 | _logic_data->push_snapshot(_cur_logic_snapshot); |
82f50b10 JH |
519 | |
520 | // @todo Putting this here means that only listeners querying | |
521 | // for logic will be notified. Currently the only user of | |
522 | // frame_began is DecoderStack, but in future we need to signal | |
523 | // this after both analog and logic sweeps have begun. | |
524 | frame_began(); | |
9c112671 JH |
525 | } |
526 | else | |
527 | { | |
528 | // Append to the existing data snapshot | |
529 | _cur_logic_snapshot->append_payload(logic); | |
530 | } | |
531 | ||
1f374035 | 532 | data_received(); |
9c112671 JH |
533 | } |
534 | ||
aba1dd16 JH |
535 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) |
536 | { | |
537 | lock_guard<mutex> lock(_data_mutex); | |
79efbc53 | 538 | |
6ac6242b ML |
539 | const unsigned int channel_count = g_slist_length(analog.channels); |
540 | const size_t sample_count = analog.num_samples / channel_count; | |
bb2cdfff JH |
541 | const float *data = analog.data; |
542 | bool sweep_beginning = false; | |
be73bdfa | 543 | |
4871ed92 | 544 | for (GSList *p = analog.channels; p; p = p->next) |
79efbc53 | 545 | { |
bb2cdfff | 546 | shared_ptr<data::AnalogSnapshot> snapshot; |
2b49eeb0 | 547 | |
6ac6242b ML |
548 | sr_channel *const channel = (sr_channel*)p->data; |
549 | assert(channel); | |
bb2cdfff | 550 | |
6ac6242b | 551 | // Try to get the snapshot of the channel |
4871ed92 | 552 | const map< const sr_channel*, shared_ptr<data::AnalogSnapshot> >:: |
6ac6242b | 553 | iterator iter = _cur_analog_snapshots.find(channel); |
bb2cdfff JH |
554 | if (iter != _cur_analog_snapshots.end()) |
555 | snapshot = (*iter).second; | |
556 | else | |
557 | { | |
558 | // If no snapshot was found, this means we havn't | |
559 | // created one yet. i.e. this is the first packet | |
560 | // in the sweep containing this snapshot. | |
561 | sweep_beginning = true; | |
562 | ||
6ac6242b | 563 | // Create a snapshot, keep it in the maps of channels |
bb2cdfff | 564 | snapshot = shared_ptr<data::AnalogSnapshot>( |
27d7c96b | 565 | new data::AnalogSnapshot(_dev_inst->get_sample_limit())); |
6ac6242b | 566 | _cur_analog_snapshots[channel] = snapshot; |
bb2cdfff | 567 | |
6ac6242b | 568 | // Find the annalog data associated with the channel |
bb2cdfff JH |
569 | shared_ptr<view::AnalogSignal> sig = |
570 | dynamic_pointer_cast<view::AnalogSignal>( | |
6ac6242b | 571 | signal_from_channel(channel)); |
bb2cdfff JH |
572 | assert(sig); |
573 | ||
574 | shared_ptr<data::Analog> data(sig->analog_data()); | |
575 | assert(data); | |
576 | ||
577 | // Push the snapshot into the analog data. | |
578 | data->push_snapshot(snapshot); | |
579 | } | |
580 | ||
581 | assert(snapshot); | |
582 | ||
583 | // Append the samples in the snapshot | |
584 | snapshot->append_interleaved_samples(data++, sample_count, | |
6ac6242b | 585 | channel_count); |
aba1dd16 | 586 | } |
bb2cdfff JH |
587 | |
588 | if (sweep_beginning) { | |
589 | // This could be the first packet after a trigger | |
590 | set_capture_state(Running); | |
aba1dd16 JH |
591 | } |
592 | ||
1f374035 | 593 | data_received(); |
aba1dd16 | 594 | } |
9c112671 | 595 | |
b0e1d01d JH |
596 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, |
597 | const struct sr_datafeed_packet *packet) | |
598 | { | |
599 | assert(sdi); | |
600 | assert(packet); | |
601 | ||
602 | switch (packet->type) { | |
603 | case SR_DF_HEADER: | |
eec446e1 | 604 | feed_in_header(sdi); |
b0e1d01d JH |
605 | break; |
606 | ||
be73bdfa | 607 | case SR_DF_META: |
aba1dd16 | 608 | assert(packet->payload); |
be73bdfa JH |
609 | feed_in_meta(sdi, |
610 | *(const sr_datafeed_meta*)packet->payload); | |
aba1dd16 JH |
611 | break; |
612 | ||
82f50b10 JH |
613 | case SR_DF_FRAME_BEGIN: |
614 | feed_in_frame_begin(); | |
615 | break; | |
616 | ||
2e2946fe | 617 | case SR_DF_LOGIC: |
28a4c9c5 | 618 | assert(packet->payload); |
9c112671 | 619 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); |
2953961c JH |
620 | break; |
621 | ||
aba1dd16 JH |
622 | case SR_DF_ANALOG: |
623 | assert(packet->payload); | |
624 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
625 | break; | |
626 | ||
2953961c | 627 | case SR_DF_END: |
2e2946fe JH |
628 | { |
629 | { | |
630 | lock_guard<mutex> lock(_data_mutex); | |
631 | _cur_logic_snapshot.reset(); | |
bb2cdfff | 632 | _cur_analog_snapshots.clear(); |
2e2946fe | 633 | } |
1f374035 | 634 | frame_ended(); |
2953961c JH |
635 | break; |
636 | } | |
adb0a983 ML |
637 | default: |
638 | break; | |
2e2946fe | 639 | } |
2953961c JH |
640 | } |
641 | ||
04abfae9 | 642 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, |
5045f16d | 643 | const struct sr_datafeed_packet *packet, void *cb_data) |
2953961c | 644 | { |
5045f16d | 645 | (void) cb_data; |
04abfae9 JH |
646 | assert(_session); |
647 | _session->data_feed_in(sdi, packet); | |
2953961c | 648 | } |
51e77110 JH |
649 | |
650 | } // namespace pv |