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