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