]>
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 <assert.h> | |
43 | ||
44 | #include <stdexcept> | |
45 | ||
46 | #include <boost/foreach.hpp> | |
47 | ||
48 | #include <sys/stat.h> | |
49 | ||
50 | #include <QDebug> | |
51 | ||
52 | using boost::dynamic_pointer_cast; | |
53 | using boost::function; | |
54 | using boost::lock_guard; | |
55 | using boost::mutex; | |
56 | using boost::shared_ptr; | |
57 | using std::list; | |
58 | using std::map; | |
59 | using std::set; | |
60 | using std::string; | |
61 | using std::vector; | |
62 | ||
63 | namespace pv { | |
64 | ||
65 | // TODO: This should not be necessary | |
66 | SigSession* SigSession::_session = NULL; | |
67 | ||
68 | SigSession::SigSession(DeviceManager &device_manager) : | |
69 | _device_manager(device_manager), | |
70 | _capture_state(Stopped) | |
71 | { | |
72 | // TODO: This should not be necessary | |
73 | _session = this; | |
74 | ||
75 | set_default_device(); | |
76 | } | |
77 | ||
78 | SigSession::~SigSession() | |
79 | { | |
80 | using pv::device::Device; | |
81 | ||
82 | stop_capture(); | |
83 | ||
84 | if (_sampling_thread.joinable()) | |
85 | _sampling_thread.join(); | |
86 | ||
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 | // Ensure we are not capturing before setting the device | |
104 | stop_capture(); | |
105 | ||
106 | if (_dev_inst) { | |
107 | sr_session_datafeed_callback_remove_all(); | |
108 | _dev_inst->release(); | |
109 | } | |
110 | ||
111 | _dev_inst = dev_inst; | |
112 | _decode_traces.clear(); | |
113 | ||
114 | if (dev_inst) { | |
115 | dev_inst->use(this); | |
116 | sr_session_datafeed_callback_add(data_feed_in_proc, NULL); | |
117 | update_signals(dev_inst); | |
118 | } | |
119 | } | |
120 | ||
121 | void SigSession::set_file(const string &name) throw(QString) | |
122 | { | |
123 | // Deslect the old device, because file type detection in File::create | |
124 | // destorys the old session inside libsigrok. | |
125 | set_device(shared_ptr<device::DevInst>()); | |
126 | set_device(shared_ptr<device::DevInst>(device::File::create(name))); | |
127 | } | |
128 | ||
129 | void SigSession::release_device(device::DevInst *dev_inst) | |
130 | { | |
131 | (void)dev_inst; | |
132 | assert(_dev_inst.get() == dev_inst); | |
133 | ||
134 | assert(_capture_state == Stopped); | |
135 | _dev_inst = shared_ptr<device::DevInst>(); | |
136 | } | |
137 | ||
138 | SigSession::capture_state SigSession::get_capture_state() const | |
139 | { | |
140 | lock_guard<mutex> lock(_sampling_mutex); | |
141 | return _capture_state; | |
142 | } | |
143 | ||
144 | void SigSession::start_capture(function<void (const QString)> error_handler) | |
145 | { | |
146 | stop_capture(); | |
147 | ||
148 | // Check that a device instance has been selected. | |
149 | if (!_dev_inst) { | |
150 | qDebug() << "No device selected"; | |
151 | return; | |
152 | } | |
153 | ||
154 | assert(_dev_inst->dev_inst()); | |
155 | ||
156 | // Check that at least one probe is enabled | |
157 | const GSList *l; | |
158 | for (l = _dev_inst->dev_inst()->probes; l; l = l->next) { | |
159 | sr_probe *const probe = (sr_probe*)l->data; | |
160 | assert(probe); | |
161 | if (probe->enabled) | |
162 | break; | |
163 | } | |
164 | ||
165 | if (!l) { | |
166 | error_handler(tr("No probes enabled.")); | |
167 | return; | |
168 | } | |
169 | ||
170 | // Begin the session | |
171 | _sampling_thread = boost::thread( | |
172 | &SigSession::sample_thread_proc, this, _dev_inst, | |
173 | error_handler); | |
174 | } | |
175 | ||
176 | void SigSession::stop_capture() | |
177 | { | |
178 | if (get_capture_state() == Stopped) | |
179 | return; | |
180 | ||
181 | sr_session_stop(); | |
182 | ||
183 | // Check that sampling stopped | |
184 | if (_sampling_thread.joinable()) | |
185 | _sampling_thread.join(); | |
186 | } | |
187 | ||
188 | set< shared_ptr<data::SignalData> > SigSession::get_data() const | |
189 | { | |
190 | lock_guard<mutex> lock(_signals_mutex); | |
191 | set< shared_ptr<data::SignalData> > data; | |
192 | BOOST_FOREACH(const shared_ptr<view::Signal> sig, _signals) { | |
193 | assert(sig); | |
194 | data.insert(sig->data()); | |
195 | } | |
196 | ||
197 | return data; | |
198 | } | |
199 | ||
200 | vector< shared_ptr<view::Signal> > SigSession::get_signals() const | |
201 | { | |
202 | lock_guard<mutex> lock(_signals_mutex); | |
203 | return _signals; | |
204 | } | |
205 | ||
206 | #ifdef ENABLE_DECODE | |
207 | bool SigSession::add_decoder(srd_decoder *const dec) | |
208 | { | |
209 | map<const srd_probe*, shared_ptr<view::LogicSignal> > probes; | |
210 | shared_ptr<data::DecoderStack> decoder_stack; | |
211 | ||
212 | try | |
213 | { | |
214 | lock_guard<mutex> lock(_signals_mutex); | |
215 | ||
216 | // Create the decoder | |
217 | decoder_stack = shared_ptr<data::DecoderStack>( | |
218 | new data::DecoderStack(*this, dec)); | |
219 | ||
220 | // Make a list of all the probes | |
221 | std::vector<const srd_probe*> all_probes; | |
222 | for(const GSList *i = dec->probes; i; i = i->next) | |
223 | all_probes.push_back((const srd_probe*)i->data); | |
224 | for(const GSList *i = dec->opt_probes; i; i = i->next) | |
225 | all_probes.push_back((const srd_probe*)i->data); | |
226 | ||
227 | // Auto select the initial probes | |
228 | BOOST_FOREACH(const srd_probe *probe, all_probes) | |
229 | BOOST_FOREACH(shared_ptr<view::Signal> s, _signals) | |
230 | { | |
231 | shared_ptr<view::LogicSignal> l = | |
232 | dynamic_pointer_cast<view::LogicSignal>(s); | |
233 | if (l && QString::fromUtf8(probe->name). | |
234 | toLower().contains( | |
235 | l->get_name().toLower())) | |
236 | probes[probe] = l; | |
237 | } | |
238 | ||
239 | assert(decoder_stack); | |
240 | assert(!decoder_stack->stack().empty()); | |
241 | assert(decoder_stack->stack().front()); | |
242 | decoder_stack->stack().front()->set_probes(probes); | |
243 | ||
244 | // Create the decode signal | |
245 | shared_ptr<view::DecodeTrace> d( | |
246 | new view::DecodeTrace(*this, decoder_stack, | |
247 | _decode_traces.size())); | |
248 | _decode_traces.push_back(d); | |
249 | } | |
250 | catch(std::runtime_error e) | |
251 | { | |
252 | return false; | |
253 | } | |
254 | ||
255 | signals_changed(); | |
256 | ||
257 | // Do an initial decode | |
258 | decoder_stack->begin_decode(); | |
259 | ||
260 | return true; | |
261 | } | |
262 | ||
263 | vector< shared_ptr<view::DecodeTrace> > SigSession::get_decode_signals() const | |
264 | { | |
265 | lock_guard<mutex> lock(_signals_mutex); | |
266 | return _decode_traces; | |
267 | } | |
268 | ||
269 | void SigSession::remove_decode_signal(view::DecodeTrace *signal) | |
270 | { | |
271 | for (vector< shared_ptr<view::DecodeTrace> >::iterator i = | |
272 | _decode_traces.begin(); | |
273 | i != _decode_traces.end(); | |
274 | i++) | |
275 | if ((*i).get() == signal) | |
276 | { | |
277 | _decode_traces.erase(i); | |
278 | signals_changed(); | |
279 | return; | |
280 | } | |
281 | } | |
282 | #endif | |
283 | ||
284 | void SigSession::set_capture_state(capture_state state) | |
285 | { | |
286 | lock_guard<mutex> lock(_sampling_mutex); | |
287 | const bool changed = _capture_state != state; | |
288 | _capture_state = state; | |
289 | if(changed) | |
290 | capture_state_changed(state); | |
291 | } | |
292 | ||
293 | void SigSession::set_default_device() | |
294 | { | |
295 | shared_ptr<pv::device::DevInst> default_device; | |
296 | const list< shared_ptr<device::Device> > &devices = | |
297 | _device_manager.devices(); | |
298 | ||
299 | if (!devices.empty()) { | |
300 | // Fall back to the first device in the list. | |
301 | default_device = devices.front(); | |
302 | ||
303 | // Try and find the demo device and select that by default | |
304 | BOOST_FOREACH (shared_ptr<pv::device::Device> dev, devices) | |
305 | if (strcmp(dev->dev_inst()->driver->name, | |
306 | "demo") == 0) { | |
307 | default_device = dev; | |
308 | break; | |
309 | } | |
310 | } | |
311 | ||
312 | set_device(default_device); | |
313 | } | |
314 | ||
315 | void SigSession::update_signals(shared_ptr<device::DevInst> dev_inst) | |
316 | { | |
317 | assert(dev_inst); | |
318 | assert(_capture_state == Stopped); | |
319 | ||
320 | unsigned int logic_probe_count = 0; | |
321 | ||
322 | // Clear the decode traces | |
323 | _decode_traces.clear(); | |
324 | ||
325 | // Detect what data types we will receive | |
326 | if(dev_inst) { | |
327 | assert(dev_inst->dev_inst()); | |
328 | for (const GSList *l = dev_inst->dev_inst()->probes; | |
329 | l; l = l->next) { | |
330 | const sr_probe *const probe = (const sr_probe *)l->data; | |
331 | if (!probe->enabled) | |
332 | continue; | |
333 | ||
334 | switch(probe->type) { | |
335 | case SR_PROBE_LOGIC: | |
336 | logic_probe_count++; | |
337 | break; | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
342 | // Create data containers for the logic data snapshots | |
343 | { | |
344 | lock_guard<mutex> data_lock(_data_mutex); | |
345 | ||
346 | _logic_data.reset(); | |
347 | if (logic_probe_count != 0) { | |
348 | _logic_data.reset(new data::Logic( | |
349 | logic_probe_count)); | |
350 | assert(_logic_data); | |
351 | } | |
352 | } | |
353 | ||
354 | // Make the Signals list | |
355 | do { | |
356 | lock_guard<mutex> lock(_signals_mutex); | |
357 | ||
358 | _signals.clear(); | |
359 | ||
360 | if(!dev_inst) | |
361 | break; | |
362 | ||
363 | assert(dev_inst->dev_inst()); | |
364 | for (const GSList *l = dev_inst->dev_inst()->probes; | |
365 | l; l = l->next) { | |
366 | shared_ptr<view::Signal> signal; | |
367 | sr_probe *const probe = (sr_probe *)l->data; | |
368 | assert(probe); | |
369 | ||
370 | switch(probe->type) { | |
371 | case SR_PROBE_LOGIC: | |
372 | signal = shared_ptr<view::Signal>( | |
373 | new view::LogicSignal(dev_inst, | |
374 | probe, _logic_data)); | |
375 | break; | |
376 | ||
377 | case SR_PROBE_ANALOG: | |
378 | { | |
379 | shared_ptr<data::Analog> data( | |
380 | new data::Analog()); | |
381 | signal = shared_ptr<view::Signal>( | |
382 | new view::AnalogSignal(dev_inst, | |
383 | probe, data)); | |
384 | break; | |
385 | } | |
386 | ||
387 | default: | |
388 | assert(0); | |
389 | break; | |
390 | } | |
391 | ||
392 | assert(signal); | |
393 | _signals.push_back(signal); | |
394 | } | |
395 | ||
396 | } while(0); | |
397 | ||
398 | signals_changed(); | |
399 | } | |
400 | ||
401 | shared_ptr<view::Signal> SigSession::signal_from_probe( | |
402 | const sr_probe *probe) const | |
403 | { | |
404 | lock_guard<mutex> lock(_signals_mutex); | |
405 | BOOST_FOREACH(shared_ptr<view::Signal> sig, _signals) { | |
406 | assert(sig); | |
407 | if (sig->probe() == probe) | |
408 | return sig; | |
409 | } | |
410 | return shared_ptr<view::Signal>(); | |
411 | } | |
412 | ||
413 | void SigSession::read_sample_rate(const sr_dev_inst *const sdi) | |
414 | { | |
415 | GVariant *gvar; | |
416 | uint64_t sample_rate = 0; | |
417 | ||
418 | // Read out the sample rate | |
419 | if(sdi->driver) | |
420 | { | |
421 | const int ret = sr_config_get(sdi->driver, sdi, NULL, | |
422 | SR_CONF_SAMPLERATE, &gvar); | |
423 | if (ret != SR_OK) { | |
424 | qDebug("Failed to get samplerate\n"); | |
425 | return; | |
426 | } | |
427 | ||
428 | sample_rate = g_variant_get_uint64(gvar); | |
429 | g_variant_unref(gvar); | |
430 | } | |
431 | ||
432 | // Set the sample rate of all data | |
433 | const set< shared_ptr<data::SignalData> > data_set = get_data(); | |
434 | BOOST_FOREACH(shared_ptr<data::SignalData> data, data_set) { | |
435 | assert(data); | |
436 | data->set_samplerate(sample_rate); | |
437 | } | |
438 | } | |
439 | ||
440 | void SigSession::sample_thread_proc(shared_ptr<device::DevInst> dev_inst, | |
441 | function<void (const QString)> error_handler) | |
442 | { | |
443 | assert(dev_inst); | |
444 | assert(dev_inst->dev_inst()); | |
445 | assert(error_handler); | |
446 | ||
447 | read_sample_rate(dev_inst->dev_inst()); | |
448 | ||
449 | try { | |
450 | dev_inst->start(); | |
451 | } catch(const QString e) { | |
452 | error_handler(e); | |
453 | return; | |
454 | } | |
455 | ||
456 | set_capture_state(dev_inst->is_trigger_enabled() ? | |
457 | AwaitingTrigger : Running); | |
458 | ||
459 | dev_inst->run(); | |
460 | set_capture_state(Stopped); | |
461 | ||
462 | // Confirm that SR_DF_END was received | |
463 | if (_cur_logic_snapshot) | |
464 | { | |
465 | qDebug("SR_DF_END was not received."); | |
466 | assert(0); | |
467 | } | |
468 | } | |
469 | ||
470 | void SigSession::feed_in_header(const sr_dev_inst *sdi) | |
471 | { | |
472 | read_sample_rate(sdi); | |
473 | } | |
474 | ||
475 | void SigSession::feed_in_meta(const sr_dev_inst *sdi, | |
476 | const sr_datafeed_meta &meta) | |
477 | { | |
478 | (void)sdi; | |
479 | ||
480 | for (const GSList *l = meta.config; l; l = l->next) { | |
481 | const sr_config *const src = (const sr_config*)l->data; | |
482 | switch (src->key) { | |
483 | case SR_CONF_SAMPLERATE: | |
484 | /// @todo handle samplerate changes | |
485 | /// samplerate = (uint64_t *)src->value; | |
486 | break; | |
487 | default: | |
488 | // Unknown metadata is not an error. | |
489 | break; | |
490 | } | |
491 | } | |
492 | ||
493 | signals_changed(); | |
494 | } | |
495 | ||
496 | void SigSession::feed_in_frame_begin() | |
497 | { | |
498 | if (_cur_logic_snapshot || !_cur_analog_snapshots.empty()) | |
499 | frame_began(); | |
500 | } | |
501 | ||
502 | void SigSession::feed_in_logic(const sr_datafeed_logic &logic) | |
503 | { | |
504 | lock_guard<mutex> lock(_data_mutex); | |
505 | ||
506 | if (!_logic_data) | |
507 | { | |
508 | qDebug() << "Unexpected logic packet"; | |
509 | return; | |
510 | } | |
511 | ||
512 | if (!_cur_logic_snapshot) | |
513 | { | |
514 | // This could be the first packet after a trigger | |
515 | set_capture_state(Running); | |
516 | ||
517 | // Create a new data snapshot | |
518 | _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>( | |
519 | new data::LogicSnapshot(logic, _dev_inst->get_sample_limit())); | |
520 | _logic_data->push_snapshot(_cur_logic_snapshot); | |
521 | ||
522 | // @todo Putting this here means that only listeners querying | |
523 | // for logic will be notified. Currently the only user of | |
524 | // frame_began is DecoderStack, but in future we need to signal | |
525 | // this after both analog and logic sweeps have begun. | |
526 | frame_began(); | |
527 | } | |
528 | else | |
529 | { | |
530 | // Append to the existing data snapshot | |
531 | _cur_logic_snapshot->append_payload(logic); | |
532 | } | |
533 | ||
534 | data_received(); | |
535 | } | |
536 | ||
537 | void SigSession::feed_in_analog(const sr_datafeed_analog &analog) | |
538 | { | |
539 | lock_guard<mutex> lock(_data_mutex); | |
540 | ||
541 | const unsigned int probe_count = g_slist_length(analog.probes); | |
542 | const size_t sample_count = analog.num_samples / probe_count; | |
543 | const float *data = analog.data; | |
544 | bool sweep_beginning = false; | |
545 | ||
546 | for (GSList *p = analog.probes; p; p = p->next) | |
547 | { | |
548 | shared_ptr<data::AnalogSnapshot> snapshot; | |
549 | ||
550 | sr_probe *const probe = (sr_probe*)p->data; | |
551 | assert(probe); | |
552 | ||
553 | // Try to get the snapshot of the probe | |
554 | const map< const sr_probe*, shared_ptr<data::AnalogSnapshot> >:: | |
555 | iterator iter = _cur_analog_snapshots.find(probe); | |
556 | if (iter != _cur_analog_snapshots.end()) | |
557 | snapshot = (*iter).second; | |
558 | else | |
559 | { | |
560 | // If no snapshot was found, this means we havn't | |
561 | // created one yet. i.e. this is the first packet | |
562 | // in the sweep containing this snapshot. | |
563 | sweep_beginning = true; | |
564 | ||
565 | // Create a snapshot, keep it in the maps of probes | |
566 | snapshot = shared_ptr<data::AnalogSnapshot>( | |
567 | new data::AnalogSnapshot(_dev_inst->get_sample_limit())); | |
568 | _cur_analog_snapshots[probe] = snapshot; | |
569 | ||
570 | // Find the annalog data associated with the probe | |
571 | shared_ptr<view::AnalogSignal> sig = | |
572 | dynamic_pointer_cast<view::AnalogSignal>( | |
573 | signal_from_probe(probe)); | |
574 | assert(sig); | |
575 | ||
576 | shared_ptr<data::Analog> data(sig->analog_data()); | |
577 | assert(data); | |
578 | ||
579 | // Push the snapshot into the analog data. | |
580 | data->push_snapshot(snapshot); | |
581 | } | |
582 | ||
583 | assert(snapshot); | |
584 | ||
585 | // Append the samples in the snapshot | |
586 | snapshot->append_interleaved_samples(data++, sample_count, | |
587 | probe_count); | |
588 | } | |
589 | ||
590 | if (sweep_beginning) { | |
591 | // This could be the first packet after a trigger | |
592 | set_capture_state(Running); | |
593 | } | |
594 | ||
595 | data_received(); | |
596 | } | |
597 | ||
598 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, | |
599 | const struct sr_datafeed_packet *packet) | |
600 | { | |
601 | assert(sdi); | |
602 | assert(packet); | |
603 | ||
604 | switch (packet->type) { | |
605 | case SR_DF_HEADER: | |
606 | feed_in_header(sdi); | |
607 | break; | |
608 | ||
609 | case SR_DF_META: | |
610 | assert(packet->payload); | |
611 | feed_in_meta(sdi, | |
612 | *(const sr_datafeed_meta*)packet->payload); | |
613 | break; | |
614 | ||
615 | case SR_DF_FRAME_BEGIN: | |
616 | feed_in_frame_begin(); | |
617 | break; | |
618 | ||
619 | case SR_DF_LOGIC: | |
620 | assert(packet->payload); | |
621 | feed_in_logic(*(const sr_datafeed_logic*)packet->payload); | |
622 | break; | |
623 | ||
624 | case SR_DF_ANALOG: | |
625 | assert(packet->payload); | |
626 | feed_in_analog(*(const sr_datafeed_analog*)packet->payload); | |
627 | break; | |
628 | ||
629 | case SR_DF_END: | |
630 | { | |
631 | { | |
632 | lock_guard<mutex> lock(_data_mutex); | |
633 | _cur_logic_snapshot.reset(); | |
634 | _cur_analog_snapshots.clear(); | |
635 | } | |
636 | frame_ended(); | |
637 | break; | |
638 | } | |
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 |