]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-14 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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 | ||
21 | #ifdef ENABLE_DECODE | |
22 | #include <libsigrokdecode/libsigrokdecode.h> | |
23 | #endif | |
24 | ||
25 | #include "sigsession.h" | |
26 | ||
27 | #include "devicemanager.h" | |
28 | #include "device/device.h" | |
29 | #include "device/file.h" | |
30 | ||
31 | #include "data/analog.h" | |
32 | #include "data/analogsnapshot.h" | |
33 | #include "data/decoderstack.h" | |
34 | #include "data/logic.h" | |
35 | #include "data/logicsnapshot.h" | |
36 | #include "data/decode/decoder.h" | |
37 | ||
38 | #include "view/analogsignal.h" | |
39 | #include "view/decodetrace.h" | |
40 | #include "view/logicsignal.h" | |
41 | ||
42 | #include <cassert> | |
43 | #include <mutex> | |
44 | #include <stdexcept> | |
45 | ||
46 | #include <sys/stat.h> | |
47 | ||
48 | #include <QDebug> | |
49 | ||
50 | using std::dynamic_pointer_cast; | |
51 | using std::function; | |
52 | using std::lock_guard; | |
53 | using std::mutex; | |
54 | using std::list; | |
55 | using std::map; | |
56 | using std::set; | |
57 | using std::shared_ptr; | |
58 | using std::string; | |
59 | using std::vector; | |
60 | ||
61 | namespace pv { | |
62 | ||
63 | // TODO: This should not be necessary | |
64 | SigSession* SigSession::_session = NULL; | |
65 | ||
66 | // TODO: This should not be necessary | |
67 | struct sr_session *SigSession::_sr_session = NULL; | |
68 | ||
69 | SigSession::SigSession(DeviceManager &device_manager) : | |
70 | _device_manager(device_manager), | |
71 | _capture_state(Stopped) | |
72 | { | |
73 | // TODO: This should not be necessary | |
74 | _session = this; | |
75 | ||
76 | set_default_device(); | |
77 | } | |
78 | ||
79 | SigSession::~SigSession() | |
80 | { | |
81 | using pv::device::Device; | |
82 | ||
83 | // Stop and join to the thread | |
84 | stop_capture(); | |
85 | ||
86 | if (_dev_inst) | |
87 | _dev_inst->release(); | |
88 | ||
89 | // TODO: This should not be necessary | |
90 | _session = NULL; | |
91 | } | |
92 | ||
93 | shared_ptr<device::DevInst> SigSession::get_device() const | |
94 | { | |
95 | return _dev_inst; | |
96 | } | |
97 | ||
98 | void SigSession::set_device( | |
99 | shared_ptr<device::DevInst> dev_inst) throw(QString) | |
100 | { | |
101 | using pv::device::Device; | |
102 | ||
103 | if (!dev_inst) | |
104 | return; | |
105 | ||
106 | // Ensure we are not capturing before setting the device | |
107 | stop_capture(); | |
108 | ||
109 | if (_dev_inst) { | |
110 | sr_session_datafeed_callback_remove_all(_sr_session); | |
111 | _dev_inst->release(); | |
112 | } | |
113 | ||
114 | _dev_inst = dev_inst; | |
115 | _decode_traces.clear(); | |
116 | ||
117 | if (dev_inst) { | |
118 | dev_inst->use(this); | |
119 | sr_session_datafeed_callback_add(_sr_session, data_feed_in_proc, NULL); | |
120 | update_signals(dev_inst); | |
121 | } | |
122 | } | |
123 | ||
124 | void SigSession::set_file(const string &name) throw(QString) | |
125 | { | |
126 | // Deselect the old device, because file type detection in File::create | |
127 | // destroys the old session inside libsigrok. | |
128 | set_device(shared_ptr<device::DevInst>()); | |
129 | set_device(shared_ptr<device::DevInst>(device::File::create(name))); | |
130 | } | |
131 | ||
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 | |
143 | for (shared_ptr<pv::device::Device> dev : devices) | |
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 | ||
154 | void SigSession::release_device(device::DevInst *dev_inst) | |
155 | { | |
156 | (void)dev_inst; | |
157 | assert(_dev_inst.get() == dev_inst); | |
158 | ||
159 | assert(_capture_state == Stopped); | |
160 | _dev_inst = shared_ptr<device::DevInst>(); | |
161 | } | |
162 | ||
163 | SigSession::capture_state SigSession::get_capture_state() const | |
164 | { | |
165 | lock_guard<mutex> lock(_sampling_mutex); | |
166 | return _capture_state; | |
167 | } | |
168 | ||
169 | void SigSession::start_capture(function<void (const QString)> error_handler) | |
170 | { | |
171 | stop_capture(); | |
172 | ||
173 | // Check that a device instance has been selected. | |
174 | if (!_dev_inst) { | |
175 | qDebug() << "No device selected"; | |
176 | return; | |
177 | } | |
178 | ||
179 | assert(_dev_inst->dev_inst()); | |
180 | ||
181 | // Check that at least one channel is enabled | |
182 | const GSList *l; | |
183 | for (l = _dev_inst->dev_inst()->channels; l; l = l->next) { | |
184 | sr_channel *const channel = (sr_channel*)l->data; | |
185 | assert(channel); | |
186 | if (channel->enabled) | |
187 | break; | |
188 | } | |
189 | ||
190 | if (!l) { | |
191 | error_handler(tr("No channels enabled.")); | |
192 | return; | |
193 | } | |
194 | ||
195 | // Begin the session | |
196 | _sampling_thread = std::thread( | |
197 | &SigSession::sample_thread_proc, this, _dev_inst, | |
198 | error_handler); | |
199 | } | |
200 | ||
201 | void SigSession::stop_capture() | |
202 | { | |
203 | if (get_capture_state() != Stopped) | |
204 | sr_session_stop(_sr_session); | |
205 | ||
206 | // Check that sampling stopped | |
207 | if (_sampling_thread.joinable()) | |
208 | _sampling_thread.join(); | |
209 | } | |
210 | ||
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; | |
215 | for (const shared_ptr<view::Signal> sig : _signals) { | |
216 | assert(sig); | |
217 | data.insert(sig->data()); | |
218 | } | |
219 | ||
220 | return data; | |
221 | } | |
222 | ||
223 | vector< shared_ptr<view::Signal> > SigSession::get_signals() const | |
224 | { | |
225 | lock_guard<mutex> lock(_signals_mutex); | |
226 | return _signals; | |
227 | } | |
228 | ||
229 | #ifdef ENABLE_DECODE | |
230 | bool SigSession::add_decoder(srd_decoder *const dec) | |
231 | { | |
232 | map<const srd_channel*, shared_ptr<view::LogicSignal> > channels; | |
233 | shared_ptr<data::DecoderStack> decoder_stack; | |
234 | ||
235 | try | |
236 | { | |
237 | lock_guard<mutex> lock(_signals_mutex); | |
238 | ||
239 | // Create the decoder | |
240 | decoder_stack = shared_ptr<data::DecoderStack>( | |
241 | new data::DecoderStack(*this, dec)); | |
242 | ||
243 | // Make a list of all the channels | |
244 | std::vector<const srd_channel*> all_channels; | |
245 | for(const GSList *i = dec->channels; i; i = i->next) | |
246 | all_channels.push_back((const srd_channel*)i->data); | |
247 | for(const GSList *i = dec->opt_channels; i; i = i->next) | |
248 | all_channels.push_back((const srd_channel*)i->data); | |
249 | ||
250 | // Auto select the initial channels | |
251 | for (const srd_channel *pdch : all_channels) | |
252 | for (shared_ptr<view::Signal> s : _signals) | |
253 | { | |
254 | shared_ptr<view::LogicSignal> l = | |
255 | dynamic_pointer_cast<view::LogicSignal>(s); | |
256 | if (l && QString::fromUtf8(pdch->name). | |
257 | toLower().contains( | |
258 | l->get_name().toLower())) | |
259 | channels[pdch] = l; | |
260 | } | |
261 | ||
262 | assert(decoder_stack); | |
263 | assert(!decoder_stack->stack().empty()); | |
264 | assert(decoder_stack->stack().front()); | |
265 | decoder_stack->stack().front()->set_channels(channels); | |
266 | ||
267 | // Create the decode signal | |
268 | shared_ptr<view::DecodeTrace> d( | |
269 | new view::DecodeTrace(*this, decoder_stack, | |
270 | _decode_traces.size())); | |
271 | _decode_traces.push_back(d); | |
272 | } | |
273 | catch(std::runtime_error e) | |
274 | { | |
275 | return false; | |
276 | } | |
277 | ||
278 | signals_changed(); | |
279 | ||
280 | // Do an initial decode | |
281 | decoder_stack->begin_decode(); | |
282 | ||
283 | return true; | |
284 | } | |
285 | ||
286 | vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const | |
287 | { | |
288 | lock_guard<mutex> lock(_signals_mutex); | |
289 | return _decode_traces; | |
290 | } | |
291 | ||
292 | void SigSession::remove_decode_signal(view::DecodeTrace *signal) | |
293 | { | |
294 | for (auto i = _decode_traces.begin(); i != _decode_traces.end(); i++) | |
295 | if ((*i).get() == signal) | |
296 | { | |
297 | _decode_traces.erase(i); | |
298 | signals_changed(); | |
299 | return; | |
300 | } | |
301 | } | |
302 | #endif | |
303 | ||
304 | void SigSession::set_capture_state(capture_state state) | |
305 | { | |
306 | lock_guard<mutex> lock(_sampling_mutex); | |
307 | const bool changed = _capture_state != state; | |
308 | _capture_state = state; | |
309 | if(changed) | |
310 | capture_state_changed(state); | |
311 | } | |
312 | ||
313 | void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst) | |
314 | { | |
315 | assert(dev_inst); | |
316 | assert(_capture_state == Stopped); | |
317 | ||
318 | unsigned int logic_channel_count = 0; | |
319 | ||
320 | // Clear the decode traces | |
321 | _decode_traces.clear(); | |
322 | ||
323 | // Detect what data types we will receive | |
324 | if(dev_inst) { | |
325 | assert(dev_inst->dev_inst()); | |
326 | for (const GSList *l = dev_inst->dev_inst()->channels; | |
327 | l; l = l->next) { | |
328 | const sr_channel *const channel = (const sr_channel *)l->data; | |
329 | if (!channel->enabled) | |
330 | continue; | |
331 | ||
332 | switch(channel->type) { | |
333 | case SR_CHANNEL_LOGIC: | |
334 | logic_channel_count++; | |
335 | break; | |
336 | } | |
337 | } | |
338 | } | |
339 | ||
340 | // Create data containers for the logic data snapshots | |
341 | { | |
342 | lock_guard<mutex> data_lock(_data_mutex); | |
343 | ||
344 | _logic_data.reset(); | |
345 | if (logic_channel_count != 0) { | |
346 | _logic_data.reset(new data::Logic( | |
347 | logic_channel_count)); | |
348 | assert(_logic_data); | |
349 | } | |
350 | } | |
351 | ||
352 | // Make the Signals list | |
353 | do { | |
354 | lock_guard<mutex> lock(_signals_mutex); | |
355 | ||
356 | _signals.clear(); | |
357 | ||
358 | if(!dev_inst) | |
359 | break; | |
360 | ||
361 | assert(dev_inst->dev_inst()); | |
362 | for (const GSList *l = dev_inst->dev_inst()->channels; | |
363 | l; l = l->next) { | |
364 | shared_ptr<view::Signal> signal; | |
365 | sr_channel *const channel = (sr_channel *)l->data; | |
366 | assert(channel); | |
367 | ||
368 | switch(channel->type) { | |
369 | case SR_CHANNEL_LOGIC: | |
370 | signal = shared_ptr<view::Signal>( | |
371 | new view::LogicSignal(dev_inst, | |
372 | channel, _logic_data)); | |
373 | break; | |
374 | ||
375 | case SR_CHANNEL_ANALOG: | |
376 | { | |
377 | shared_ptr<data::Analog> data( | |
378 | new data::Analog()); | |
379 | signal = shared_ptr<view::Signal>( | |
380 | new view::AnalogSignal(dev_inst, | |
381 | channel, data)); | |
382 | break; | |
383 | } | |
384 | ||
385 | default: | |
386 | assert(0); | |
387 | break; | |
388 | } | |
389 | ||
390 | assert(signal); | |
391 | _signals.push_back(signal); | |
392 | } | |
393 | ||
394 | } while(0); | |
395 | ||
396 | signals_changed(); | |
397 | } | |
398 | ||
399 | shared_ptr<view::Signal> SigSession::signal_from_channel( | |
400 | const sr_channel *channel) const | |
401 | { | |
402 | lock_guard<mutex> lock(_signals_mutex); | |
403 | for (shared_ptr<view::Signal> sig : _signals) { | |
404 | assert(sig); | |
405 | if (sig->channel() == channel) | |
406 | return sig; | |
407 | } | |
408 | return shared_ptr<view::Signal>(); | |
409 | } | |
410 | ||
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 | { | |
419 | const int ret = sr_config_get(sdi->driver, sdi, NULL, | |
420 | SR_CONF_SAMPLERATE, &gvar); | |
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 | ||
430 | // Set the sample rate of all data | |
431 | const set< shared_ptr<data::SignalData> > data_set = get_data(); | |
432 | for (shared_ptr<data::SignalData> data : data_set) { | |
433 | assert(data); | |
434 | data->set_samplerate(sample_rate); | |
435 | } | |
436 | } | |
437 | ||
438 | void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst, | |
439 | function<void (const QString)> error_handler) | |
440 | { | |
441 | assert(dev_inst); | |
442 | assert(dev_inst->dev_inst()); | |
443 | assert(error_handler); | |
444 | ||
445 | read_sample_rate(dev_inst->dev_inst()); | |
446 | ||
447 | try { | |
448 | dev_inst->start(); | |
449 | } catch(const QString e) { | |
450 | error_handler(e); | |
451 | return; | |
452 | } | |
453 | ||
454 | set_capture_state(sr_session_trigger_get(_sr_session) ? | |
455 | AwaitingTrigger : Running); | |
456 | ||
457 | dev_inst->run(); | |
458 | set_capture_state(Stopped); | |
459 | ||
460 | // Confirm that SR_DF_END was received | |
461 | if (_cur_logic_snapshot) | |
462 | { | |
463 | qDebug("SR_DF_END was not received."); | |
464 | assert(0); | |
465 | } | |
466 | } | |
467 | ||
468 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
469 | { | |
470 | read_sample_rate(sdi); | |
471 | } | |
472 | ||
473 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
474 | const sr_datafeed_meta &meta) | |
475 | { | |
476 | (void)sdi; | |
477 | ||
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 | } | |
489 | } | |
490 | ||
491 | signals_changed(); | |
492 | } | |
493 | ||
494 | void SigSession::feed_in_frame_begin() | |
495 | { | |
496 | if (_cur_logic_snapshot || !_cur_analog_snapshots.empty()) | |
497 | frame_began(); | |
498 | } | |
499 | ||
500 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) | |
501 | { | |
502 | lock_guard<mutex> lock(_data_mutex); | |
503 | ||
504 | if (!_logic_data) | |
505 | { | |
506 | qDebug() << "Unexpected logic packet"; | |
507 | return; | |
508 | } | |
509 | ||
510 | if (!_cur_logic_snapshot) | |
511 | { | |
512 | // This could be the first packet after a trigger | |
513 | set_capture_state(Running); | |
514 | ||
515 | // Create a new data snapshot | |
516 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( | |
517 | new data::LogicSnapshot(logic, _dev_inst->get_sample_limit())); | |
518 | _logic_data->push_snapshot(_cur_logic_snapshot); | |
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(); | |
525 | } | |
526 | else | |
527 | { | |
528 | // Append to the existing data snapshot | |
529 | _cur_logic_snapshot->append_payload(logic); | |
530 | } | |
531 | ||
532 | data_received(); | |
533 | } | |
534 | ||
535 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) | |
536 | { | |
537 | lock_guard<mutex> lock(_data_mutex); | |
538 | ||
539 | const unsigned int channel_count = g_slist_length(analog.channels); | |
540 | const size_t sample_count = analog.num_samples / channel_count; | |
541 | const float *data = analog.data; | |
542 | bool sweep_beginning = false; | |
543 | ||
544 | for (GSList *p = analog.channels; p; p = p->next) | |
545 | { | |
546 | shared_ptr<data::AnalogSnapshot> snapshot; | |
547 | ||
548 | sr_channel *const channel = (sr_channel*)p->data; | |
549 | assert(channel); | |
550 | ||
551 | // Try to get the snapshot of the channel | |
552 | const map< const sr_channel*, shared_ptr<data::AnalogSnapshot> >:: | |
553 | iterator iter = _cur_analog_snapshots.find(channel); | |
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 | ||
563 | // Create a snapshot, keep it in the maps of channels | |
564 | snapshot = shared_ptr<data::AnalogSnapshot>( | |
565 | new data::AnalogSnapshot(_dev_inst->get_sample_limit())); | |
566 | _cur_analog_snapshots[channel] = snapshot; | |
567 | ||
568 | // Find the annalog data associated with the channel | |
569 | shared_ptr<view::AnalogSignal> sig = | |
570 | dynamic_pointer_cast<view::AnalogSignal>( | |
571 | signal_from_channel(channel)); | |
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, | |
585 | channel_count); | |
586 | } | |
587 | ||
588 | if (sweep_beginning) { | |
589 | // This could be the first packet after a trigger | |
590 | set_capture_state(Running); | |
591 | } | |
592 | ||
593 | data_received(); | |
594 | } | |
595 | ||
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: | |
604 | feed_in_header(sdi); | |
605 | break; | |
606 | ||
607 | case SR_DF_META: | |
608 | assert(packet->payload); | |
609 | feed_in_meta(sdi, | |
610 | *(const sr_datafeed_meta*)packet->payload); | |
611 | break; | |
612 | ||
613 | case SR_DF_FRAME_BEGIN: | |
614 | feed_in_frame_begin(); | |
615 | break; | |
616 | ||
617 | case SR_DF_LOGIC: | |
618 | assert(packet->payload); | |
619 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); | |
620 | break; | |
621 | ||
622 | case SR_DF_ANALOG: | |
623 | assert(packet->payload); | |
624 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
625 | break; | |
626 | ||
627 | case SR_DF_END: | |
628 | { | |
629 | { | |
630 | lock_guard<mutex> lock(_data_mutex); | |
631 | _cur_logic_snapshot.reset(); | |
632 | _cur_analog_snapshots.clear(); | |
633 | } | |
634 | frame_ended(); | |
635 | break; | |
636 | } | |
637 | default: | |
638 | break; | |
639 | } | |
640 | } | |
641 | ||
642 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, | |
643 | const struct sr_datafeed_packet *packet, void *cb_data) | |
644 | { | |
645 | (void) cb_data; | |
646 | assert(_session); | |
647 | _session->data_feed_in(sdi, packet); | |
648 | } | |
649 | ||
650 | } // namespace pv |