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