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