]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <cstring> | |
21 | #include <forward_list> | |
22 | #include <limits> | |
23 | ||
24 | #include <QDebug> | |
25 | ||
26 | #include "logic.hpp" | |
27 | #include "logicsegment.hpp" | |
28 | #include "decodesignal.hpp" | |
29 | #include "signaldata.hpp" | |
30 | ||
31 | #include <pv/data/decode/decoder.hpp> | |
32 | #include <pv/data/decode/row.hpp> | |
33 | #include <pv/globalsettings.hpp> | |
34 | #include <pv/session.hpp> | |
35 | ||
36 | using std::forward_list; | |
37 | using std::lock_guard; | |
38 | using std::make_shared; | |
39 | using std::min; | |
40 | using std::out_of_range; | |
41 | using std::shared_ptr; | |
42 | using std::unique_lock; | |
43 | using pv::data::decode::AnnotationClass; | |
44 | using pv::data::decode::DecodeChannel; | |
45 | ||
46 | namespace pv { | |
47 | namespace data { | |
48 | ||
49 | const double DecodeSignal::DecodeMargin = 1.0; | |
50 | const double DecodeSignal::DecodeThreshold = 0.2; | |
51 | const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024; | |
52 | ||
53 | ||
54 | DecodeSignal::DecodeSignal(pv::Session &session) : | |
55 | SignalBase(nullptr, SignalBase::DecodeChannel), | |
56 | session_(session), | |
57 | srd_session_(nullptr), | |
58 | logic_mux_data_invalid_(false), | |
59 | stack_config_changed_(true), | |
60 | current_segment_id_(0) | |
61 | { | |
62 | connect(&session_, SIGNAL(capture_state_changed(int)), | |
63 | this, SLOT(on_capture_state_changed(int))); | |
64 | } | |
65 | ||
66 | DecodeSignal::~DecodeSignal() | |
67 | { | |
68 | reset_decode(true); | |
69 | } | |
70 | ||
71 | const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const | |
72 | { | |
73 | return stack_; | |
74 | } | |
75 | ||
76 | void DecodeSignal::stack_decoder(const srd_decoder *decoder, bool restart_decode) | |
77 | { | |
78 | assert(decoder); | |
79 | ||
80 | // Set name if this decoder is the first in the list or the name is unchanged | |
81 | const srd_decoder* prev_dec = stack_.empty() ? nullptr : stack_.back()->get_srd_decoder(); | |
82 | const QString prev_dec_name = prev_dec ? QString::fromUtf8(prev_dec->name) : QString(); | |
83 | ||
84 | if ((stack_.empty()) || ((stack_.size() > 0) && (name() == prev_dec_name))) | |
85 | set_name(QString::fromUtf8(decoder->name)); | |
86 | ||
87 | const shared_ptr<Decoder> dec = make_shared<Decoder>(decoder); | |
88 | stack_.push_back(dec); | |
89 | ||
90 | // Include the newly created decode channels in the channel lists | |
91 | update_channel_list(); | |
92 | ||
93 | stack_config_changed_ = true; | |
94 | auto_assign_signals(dec); | |
95 | commit_decoder_channels(); | |
96 | ||
97 | decoder_stacked((void*)dec.get()); | |
98 | ||
99 | if (restart_decode) | |
100 | begin_decode(); | |
101 | } | |
102 | ||
103 | void DecodeSignal::remove_decoder(int index) | |
104 | { | |
105 | assert(index >= 0); | |
106 | assert(index < (int)stack_.size()); | |
107 | ||
108 | // Find the decoder in the stack | |
109 | auto iter = stack_.begin(); | |
110 | for (int i = 0; i < index; i++, iter++) | |
111 | assert(iter != stack_.end()); | |
112 | ||
113 | decoder_removed(iter->get()); | |
114 | ||
115 | // Delete the element | |
116 | stack_.erase(iter); | |
117 | ||
118 | // Update channels and decoded data | |
119 | stack_config_changed_ = true; | |
120 | update_channel_list(); | |
121 | begin_decode(); | |
122 | } | |
123 | ||
124 | bool DecodeSignal::toggle_decoder_visibility(int index) | |
125 | { | |
126 | auto iter = stack_.cbegin(); | |
127 | for (int i = 0; i < index; i++, iter++) | |
128 | assert(iter != stack_.end()); | |
129 | ||
130 | shared_ptr<Decoder> dec = *iter; | |
131 | ||
132 | // Toggle decoder visibility | |
133 | bool state = false; | |
134 | if (dec) { | |
135 | state = !dec->visible(); | |
136 | dec->set_visible(state); | |
137 | } | |
138 | ||
139 | return state; | |
140 | } | |
141 | ||
142 | void DecodeSignal::reset_decode(bool shutting_down) | |
143 | { | |
144 | if (stack_config_changed_ || shutting_down) | |
145 | stop_srd_session(); | |
146 | else | |
147 | terminate_srd_session(); | |
148 | ||
149 | if (decode_thread_.joinable()) { | |
150 | decode_interrupt_ = true; | |
151 | decode_input_cond_.notify_one(); | |
152 | decode_thread_.join(); | |
153 | } | |
154 | ||
155 | if (logic_mux_thread_.joinable()) { | |
156 | logic_mux_interrupt_ = true; | |
157 | logic_mux_cond_.notify_one(); | |
158 | logic_mux_thread_.join(); | |
159 | } | |
160 | ||
161 | resume_decode(); // Make sure the decode thread isn't blocked by pausing | |
162 | ||
163 | current_segment_id_ = 0; | |
164 | segments_.clear(); | |
165 | ||
166 | logic_mux_data_.reset(); | |
167 | logic_mux_data_invalid_ = true; | |
168 | ||
169 | if (!error_message_.isEmpty()) { | |
170 | error_message_ = QString(); | |
171 | // TODO Emulate noquote() | |
172 | qDebug().nospace() << name() << ": Error cleared"; | |
173 | } | |
174 | ||
175 | decode_reset(); | |
176 | } | |
177 | ||
178 | void DecodeSignal::begin_decode() | |
179 | { | |
180 | if (decode_thread_.joinable()) { | |
181 | decode_interrupt_ = true; | |
182 | decode_input_cond_.notify_one(); | |
183 | decode_thread_.join(); | |
184 | } | |
185 | ||
186 | if (logic_mux_thread_.joinable()) { | |
187 | logic_mux_interrupt_ = true; | |
188 | logic_mux_cond_.notify_one(); | |
189 | logic_mux_thread_.join(); | |
190 | } | |
191 | ||
192 | reset_decode(); | |
193 | ||
194 | if (stack_.size() == 0) { | |
195 | set_error_message(tr("No decoders")); | |
196 | return; | |
197 | } | |
198 | ||
199 | assert(channels_.size() > 0); | |
200 | ||
201 | if (get_assigned_signal_count() == 0) { | |
202 | set_error_message(tr("There are no channels assigned to this decoder")); | |
203 | return; | |
204 | } | |
205 | ||
206 | // Make sure that all assigned channels still provide logic data | |
207 | // (can happen when a converted signal was assigned but the | |
208 | // conversion removed in the meanwhile) | |
209 | for (decode::DecodeChannel& ch : channels_) | |
210 | if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr)) | |
211 | ch.assigned_signal = nullptr; | |
212 | ||
213 | // Check that all decoders have the required channels | |
214 | for (const shared_ptr<Decoder>& dec : stack_) | |
215 | if (!dec->have_required_channels()) { | |
216 | set_error_message(tr("One or more required channels " | |
217 | "have not been specified")); | |
218 | return; | |
219 | } | |
220 | ||
221 | // Free the logic data and its segment(s) if it needs to be updated | |
222 | if (logic_mux_data_invalid_) | |
223 | logic_mux_data_.reset(); | |
224 | ||
225 | if (!logic_mux_data_) { | |
226 | const uint32_t ch_count = get_assigned_signal_count(); | |
227 | logic_mux_unit_size_ = (ch_count + 7) / 8; | |
228 | logic_mux_data_ = make_shared<Logic>(ch_count); | |
229 | } | |
230 | ||
231 | // Receive notifications when new sample data is available | |
232 | connect_input_notifiers(); | |
233 | ||
234 | if (get_input_segment_count() == 0) { | |
235 | set_error_message(tr("No input data")); | |
236 | return; | |
237 | } | |
238 | ||
239 | // Make sure the logic output data is complete and up-to-date | |
240 | logic_mux_interrupt_ = false; | |
241 | logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this); | |
242 | ||
243 | // Decode the muxed logic data | |
244 | decode_interrupt_ = false; | |
245 | decode_thread_ = std::thread(&DecodeSignal::decode_proc, this); | |
246 | } | |
247 | ||
248 | void DecodeSignal::pause_decode() | |
249 | { | |
250 | decode_paused_ = true; | |
251 | } | |
252 | ||
253 | void DecodeSignal::resume_decode() | |
254 | { | |
255 | // Manual unlocking is done before notifying, to avoid waking up the | |
256 | // waiting thread only to block again (see notify_one for details) | |
257 | decode_pause_mutex_.unlock(); | |
258 | decode_pause_cond_.notify_one(); | |
259 | decode_paused_ = false; | |
260 | } | |
261 | ||
262 | bool DecodeSignal::is_paused() const | |
263 | { | |
264 | return decode_paused_; | |
265 | } | |
266 | ||
267 | QString DecodeSignal::error_message() const | |
268 | { | |
269 | lock_guard<mutex> lock(output_mutex_); | |
270 | return error_message_; | |
271 | } | |
272 | ||
273 | const vector<decode::DecodeChannel> DecodeSignal::get_channels() const | |
274 | { | |
275 | return channels_; | |
276 | } | |
277 | ||
278 | void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec) | |
279 | { | |
280 | bool new_assignment = false; | |
281 | ||
282 | // Try to auto-select channels that don't have signals assigned yet | |
283 | for (decode::DecodeChannel& ch : channels_) { | |
284 | // If a decoder is given, auto-assign only its channels | |
285 | if (dec && (ch.decoder_ != dec)) | |
286 | continue; | |
287 | ||
288 | if (ch.assigned_signal) | |
289 | continue; | |
290 | ||
291 | QString ch_name = ch.name.toLower(); | |
292 | ch_name = ch_name.replace(QRegExp("[-_.]"), " "); | |
293 | ||
294 | shared_ptr<data::SignalBase> match; | |
295 | for (const shared_ptr<data::SignalBase>& s : session_.signalbases()) { | |
296 | if (!s->enabled()) | |
297 | continue; | |
298 | ||
299 | QString s_name = s->name().toLower(); | |
300 | s_name = s_name.replace(QRegExp("[-_.]"), " "); | |
301 | ||
302 | if (s->logic_data() && | |
303 | ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) { | |
304 | if (!match) | |
305 | match = s; | |
306 | else { | |
307 | // Only replace an existing match if it matches more characters | |
308 | int old_unmatched = ch_name.length() - match->name().length(); | |
309 | int new_unmatched = ch_name.length() - s->name().length(); | |
310 | if (abs(new_unmatched) < abs(old_unmatched)) | |
311 | match = s; | |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | if (match) { | |
317 | ch.assigned_signal = match.get(); | |
318 | new_assignment = true; | |
319 | } | |
320 | } | |
321 | ||
322 | if (new_assignment) { | |
323 | logic_mux_data_invalid_ = true; | |
324 | stack_config_changed_ = true; | |
325 | commit_decoder_channels(); | |
326 | channels_updated(); | |
327 | } | |
328 | } | |
329 | ||
330 | void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal) | |
331 | { | |
332 | for (decode::DecodeChannel& ch : channels_) | |
333 | if (ch.id == channel_id) { | |
334 | ch.assigned_signal = signal; | |
335 | logic_mux_data_invalid_ = true; | |
336 | } | |
337 | ||
338 | stack_config_changed_ = true; | |
339 | commit_decoder_channels(); | |
340 | channels_updated(); | |
341 | begin_decode(); | |
342 | } | |
343 | ||
344 | int DecodeSignal::get_assigned_signal_count() const | |
345 | { | |
346 | // Count all channels that have a signal assigned to them | |
347 | return count_if(channels_.begin(), channels_.end(), | |
348 | [](decode::DecodeChannel ch) { return ch.assigned_signal; }); | |
349 | } | |
350 | ||
351 | void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state) | |
352 | { | |
353 | for (decode::DecodeChannel& ch : channels_) | |
354 | if (ch.id == channel_id) | |
355 | ch.initial_pin_state = init_state; | |
356 | ||
357 | stack_config_changed_ = true; | |
358 | channels_updated(); | |
359 | begin_decode(); | |
360 | } | |
361 | ||
362 | double DecodeSignal::samplerate() const | |
363 | { | |
364 | double result = 0; | |
365 | ||
366 | // TODO For now, we simply return the first samplerate that we have | |
367 | if (segments_.size() > 0) | |
368 | result = segments_.front().samplerate; | |
369 | ||
370 | return result; | |
371 | } | |
372 | ||
373 | const pv::util::Timestamp DecodeSignal::start_time() const | |
374 | { | |
375 | pv::util::Timestamp result; | |
376 | ||
377 | // TODO For now, we simply return the first start time that we have | |
378 | if (segments_.size() > 0) | |
379 | result = segments_.front().start_time; | |
380 | ||
381 | return result; | |
382 | } | |
383 | ||
384 | int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const | |
385 | { | |
386 | // The working sample count is the highest sample number for | |
387 | // which all used signals have data available, so go through all | |
388 | // channels and use the lowest overall sample count of the segment | |
389 | ||
390 | int64_t count = std::numeric_limits<int64_t>::max(); | |
391 | bool no_signals_assigned = true; | |
392 | ||
393 | for (const decode::DecodeChannel& ch : channels_) | |
394 | if (ch.assigned_signal) { | |
395 | no_signals_assigned = false; | |
396 | ||
397 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
398 | if (!logic_data || logic_data->logic_segments().empty()) | |
399 | return 0; | |
400 | ||
401 | try { | |
402 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
403 | count = min(count, (int64_t)segment->get_sample_count()); | |
404 | } catch (out_of_range&) { | |
405 | return 0; | |
406 | } | |
407 | } | |
408 | ||
409 | return (no_signals_assigned ? 0 : count); | |
410 | } | |
411 | ||
412 | int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id, | |
413 | bool include_processing) const | |
414 | { | |
415 | lock_guard<mutex> decode_lock(output_mutex_); | |
416 | ||
417 | int64_t result = 0; | |
418 | ||
419 | if (segment_id >= segments_.size()) | |
420 | return result; | |
421 | ||
422 | if (include_processing) | |
423 | result = segments_[segment_id].samples_decoded_incl; | |
424 | else | |
425 | result = segments_[segment_id].samples_decoded_excl; | |
426 | ||
427 | return result; | |
428 | } | |
429 | ||
430 | vector<Row*> DecodeSignal::get_rows(bool visible_only) | |
431 | { | |
432 | vector<Row*> rows; | |
433 | ||
434 | for (const shared_ptr<Decoder>& dec : stack_) { | |
435 | assert(dec); | |
436 | if (visible_only && !dec->visible()) | |
437 | continue; | |
438 | ||
439 | for (Row* row : dec->get_rows()) | |
440 | rows.push_back(row); | |
441 | } | |
442 | ||
443 | return rows; | |
444 | } | |
445 | ||
446 | vector<const Row*> DecodeSignal::get_rows(bool visible_only) const | |
447 | { | |
448 | vector<const Row*> rows; | |
449 | ||
450 | for (const shared_ptr<Decoder>& dec : stack_) { | |
451 | assert(dec); | |
452 | if (visible_only && !dec->visible()) | |
453 | continue; | |
454 | ||
455 | for (const Row* row : dec->get_rows()) | |
456 | rows.push_back(row); | |
457 | } | |
458 | ||
459 | return rows; | |
460 | } | |
461 | ||
462 | ||
463 | uint64_t DecodeSignal::get_annotation_count(const Row* row, uint32_t segment_id) const | |
464 | { | |
465 | if (segment_id >= segments_.size()) | |
466 | return 0; | |
467 | ||
468 | const DecodeSegment* segment = &(segments_.at(segment_id)); | |
469 | ||
470 | auto row_it = segment->annotation_rows.find(row); | |
471 | ||
472 | const RowData* rd; | |
473 | if (row_it == segment->annotation_rows.end()) | |
474 | return 0; | |
475 | else | |
476 | rd = &(row_it->second); | |
477 | ||
478 | return rd->get_annotation_count(); | |
479 | } | |
480 | ||
481 | void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest, | |
482 | const Row* row, uint32_t segment_id, uint64_t start_sample, | |
483 | uint64_t end_sample) const | |
484 | { | |
485 | lock_guard<mutex> lock(output_mutex_); | |
486 | ||
487 | if (segment_id >= segments_.size()) | |
488 | return; | |
489 | ||
490 | const DecodeSegment* segment = &(segments_.at(segment_id)); | |
491 | ||
492 | auto row_it = segment->annotation_rows.find(row); | |
493 | ||
494 | const RowData* rd; | |
495 | if (row_it == segment->annotation_rows.end()) | |
496 | return; | |
497 | else | |
498 | rd = &(row_it->second); | |
499 | ||
500 | rd->get_annotation_subset(dest, start_sample, end_sample); | |
501 | } | |
502 | ||
503 | void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest, | |
504 | uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const | |
505 | { | |
506 | for (const Row* row : get_rows()) | |
507 | get_annotation_subset(dest, row, segment_id, start_sample, end_sample); | |
508 | } | |
509 | ||
510 | uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id, | |
511 | const Decoder* dec, uint32_t bin_class_id) const | |
512 | { | |
513 | if (segments_.size() == 0) | |
514 | return 0; | |
515 | ||
516 | try { | |
517 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
518 | ||
519 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
520 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
521 | return bc.chunks.size(); | |
522 | } catch (out_of_range&) { | |
523 | // Do nothing | |
524 | } | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
529 | void DecodeSignal::get_binary_data_chunk(uint32_t segment_id, | |
530 | const Decoder* dec, uint32_t bin_class_id, uint32_t chunk_id, | |
531 | const vector<uint8_t> **dest, uint64_t *size) | |
532 | { | |
533 | try { | |
534 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
535 | ||
536 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
537 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) { | |
538 | if (dest) *dest = &(bc.chunks.at(chunk_id).data); | |
539 | if (size) *size = bc.chunks.at(chunk_id).data.size(); | |
540 | return; | |
541 | } | |
542 | } catch (out_of_range&) { | |
543 | // Do nothing | |
544 | } | |
545 | } | |
546 | ||
547 | void DecodeSignal::get_merged_binary_data_chunks_by_sample(uint32_t segment_id, | |
548 | const Decoder* dec, uint32_t bin_class_id, uint64_t start_sample, | |
549 | uint64_t end_sample, vector<uint8_t> *dest) const | |
550 | { | |
551 | assert(dest != nullptr); | |
552 | ||
553 | try { | |
554 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
555 | ||
556 | const DecodeBinaryClass* bin_class = nullptr; | |
557 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
558 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
559 | bin_class = &bc; | |
560 | ||
561 | // Determine overall size before copying to resize dest vector only once | |
562 | uint64_t size = 0; | |
563 | uint64_t matches = 0; | |
564 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) | |
565 | if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) { | |
566 | size += chunk.data.size(); | |
567 | matches++; | |
568 | } | |
569 | dest->resize(size); | |
570 | ||
571 | uint64_t offset = 0; | |
572 | uint64_t matches2 = 0; | |
573 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) | |
574 | if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) { | |
575 | memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size()); | |
576 | offset += chunk.data.size(); | |
577 | matches2++; | |
578 | ||
579 | // Make sure we don't overwrite memory if the array grew in the meanwhile | |
580 | if (matches2 == matches) | |
581 | break; | |
582 | } | |
583 | } catch (out_of_range&) { | |
584 | // Do nothing | |
585 | } | |
586 | } | |
587 | ||
588 | void DecodeSignal::get_merged_binary_data_chunks_by_offset(uint32_t segment_id, | |
589 | const Decoder* dec, uint32_t bin_class_id, uint64_t start, uint64_t end, | |
590 | vector<uint8_t> *dest) const | |
591 | { | |
592 | assert(dest != nullptr); | |
593 | ||
594 | try { | |
595 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
596 | ||
597 | const DecodeBinaryClass* bin_class = nullptr; | |
598 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
599 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
600 | bin_class = &bc; | |
601 | ||
602 | // Determine overall size before copying to resize dest vector only once | |
603 | uint64_t size = 0; | |
604 | uint64_t offset = 0; | |
605 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) { | |
606 | if (offset >= start) | |
607 | size += chunk.data.size(); | |
608 | offset += chunk.data.size(); | |
609 | if (offset >= end) | |
610 | break; | |
611 | } | |
612 | dest->resize(size); | |
613 | ||
614 | offset = 0; | |
615 | uint64_t dest_offset = 0; | |
616 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) { | |
617 | if (offset >= start) { | |
618 | memcpy(dest->data() + dest_offset, chunk.data.data(), chunk.data.size()); | |
619 | dest_offset += chunk.data.size(); | |
620 | } | |
621 | offset += chunk.data.size(); | |
622 | if (offset >= end) | |
623 | break; | |
624 | } | |
625 | } catch (out_of_range&) { | |
626 | // Do nothing | |
627 | } | |
628 | } | |
629 | ||
630 | const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id, | |
631 | const Decoder* dec, uint32_t bin_class_id) const | |
632 | { | |
633 | try { | |
634 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
635 | ||
636 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
637 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
638 | return &bc; | |
639 | } catch (out_of_range&) { | |
640 | // Do nothing | |
641 | } | |
642 | ||
643 | return nullptr; | |
644 | } | |
645 | ||
646 | void DecodeSignal::save_settings(QSettings &settings) const | |
647 | { | |
648 | SignalBase::save_settings(settings); | |
649 | ||
650 | settings.setValue("decoders", (int)(stack_.size())); | |
651 | ||
652 | // Save decoder stack | |
653 | int decoder_idx = 0; | |
654 | for (const shared_ptr<Decoder>& decoder : stack_) { | |
655 | settings.beginGroup("decoder" + QString::number(decoder_idx++)); | |
656 | ||
657 | settings.setValue("id", decoder->get_srd_decoder()->id); | |
658 | settings.setValue("visible", decoder->visible()); | |
659 | ||
660 | // Save decoder options | |
661 | const map<string, GVariant*>& options = decoder->options(); | |
662 | ||
663 | settings.setValue("options", (int)options.size()); | |
664 | ||
665 | // Note: Decoder::options() returns only the options | |
666 | // that differ from the default. See binding::Decoder::getter() | |
667 | int i = 0; | |
668 | for (auto& option : options) { | |
669 | settings.beginGroup("option" + QString::number(i)); | |
670 | settings.setValue("name", QString::fromStdString(option.first)); | |
671 | GlobalSettings::store_gvariant(settings, option.second); | |
672 | settings.endGroup(); | |
673 | i++; | |
674 | } | |
675 | ||
676 | // Save row properties | |
677 | i = 0; | |
678 | for (const Row* row : decoder->get_rows()) { | |
679 | settings.beginGroup("row" + QString::number(i)); | |
680 | settings.setValue("visible", row->visible()); | |
681 | settings.endGroup(); | |
682 | i++; | |
683 | } | |
684 | ||
685 | // Save class properties | |
686 | i = 0; | |
687 | for (const AnnotationClass* ann_class : decoder->ann_classes()) { | |
688 | settings.beginGroup("ann_class" + QString::number(i)); | |
689 | settings.setValue("visible", ann_class->visible); | |
690 | settings.endGroup(); | |
691 | i++; | |
692 | } | |
693 | ||
694 | settings.endGroup(); | |
695 | } | |
696 | ||
697 | // Save channel mapping | |
698 | settings.setValue("channels", (int)channels_.size()); | |
699 | ||
700 | for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) { | |
701 | auto channel = find_if(channels_.begin(), channels_.end(), | |
702 | [&](decode::DecodeChannel ch) { return ch.id == channel_id; }); | |
703 | ||
704 | if (channel == channels_.end()) { | |
705 | qDebug() << "ERROR: Gap in channel index:" << channel_id; | |
706 | continue; | |
707 | } | |
708 | ||
709 | settings.beginGroup("channel" + QString::number(channel_id)); | |
710 | ||
711 | settings.setValue("name", channel->name); // Useful for debugging | |
712 | settings.setValue("initial_pin_state", channel->initial_pin_state); | |
713 | ||
714 | if (channel->assigned_signal) | |
715 | settings.setValue("assigned_signal_name", channel->assigned_signal->name()); | |
716 | ||
717 | settings.endGroup(); | |
718 | } | |
719 | } | |
720 | ||
721 | void DecodeSignal::restore_settings(QSettings &settings) | |
722 | { | |
723 | SignalBase::restore_settings(settings); | |
724 | ||
725 | // Restore decoder stack | |
726 | GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list()); | |
727 | ||
728 | int decoders = settings.value("decoders").toInt(); | |
729 | ||
730 | for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) { | |
731 | settings.beginGroup("decoder" + QString::number(decoder_idx)); | |
732 | ||
733 | QString id = settings.value("id").toString(); | |
734 | ||
735 | for (GSList *entry = dec_list; entry; entry = entry->next) { | |
736 | const srd_decoder *dec = (srd_decoder*)entry->data; | |
737 | if (!dec) | |
738 | continue; | |
739 | ||
740 | if (QString::fromUtf8(dec->id) == id) { | |
741 | shared_ptr<Decoder> decoder = make_shared<Decoder>(dec); | |
742 | ||
743 | stack_.push_back(decoder); | |
744 | decoder->set_visible(settings.value("visible", true).toBool()); | |
745 | ||
746 | // Restore decoder options that differ from their default | |
747 | int options = settings.value("options").toInt(); | |
748 | ||
749 | for (int i = 0; i < options; i++) { | |
750 | settings.beginGroup("option" + QString::number(i)); | |
751 | QString name = settings.value("name").toString(); | |
752 | GVariant *value = GlobalSettings::restore_gvariant(settings); | |
753 | decoder->set_option(name.toUtf8(), value); | |
754 | settings.endGroup(); | |
755 | } | |
756 | ||
757 | // Include the newly created decode channels in the channel lists | |
758 | update_channel_list(); | |
759 | ||
760 | // Restore row properties | |
761 | int i = 0; | |
762 | for (Row* row : decoder->get_rows()) { | |
763 | settings.beginGroup("row" + QString::number(i)); | |
764 | row->set_visible(settings.value("visible", true).toBool()); | |
765 | settings.endGroup(); | |
766 | i++; | |
767 | } | |
768 | ||
769 | // Restore class properties | |
770 | i = 0; | |
771 | for (AnnotationClass* ann_class : decoder->ann_classes()) { | |
772 | settings.beginGroup("ann_class" + QString::number(i)); | |
773 | ann_class->visible = settings.value("visible", true).toBool(); | |
774 | settings.endGroup(); | |
775 | i++; | |
776 | } | |
777 | ||
778 | break; | |
779 | } | |
780 | } | |
781 | ||
782 | settings.endGroup(); | |
783 | channels_updated(); | |
784 | } | |
785 | ||
786 | // Restore channel mapping | |
787 | unsigned int channels = settings.value("channels").toInt(); | |
788 | ||
789 | const vector< shared_ptr<data::SignalBase> > signalbases = | |
790 | session_.signalbases(); | |
791 | ||
792 | for (unsigned int channel_id = 0; channel_id < channels; channel_id++) { | |
793 | auto channel = find_if(channels_.begin(), channels_.end(), | |
794 | [&](decode::DecodeChannel ch) { return ch.id == channel_id; }); | |
795 | ||
796 | if (channel == channels_.end()) { | |
797 | qDebug() << "ERROR: Non-existant channel index:" << channel_id; | |
798 | continue; | |
799 | } | |
800 | ||
801 | settings.beginGroup("channel" + QString::number(channel_id)); | |
802 | ||
803 | QString assigned_signal_name = settings.value("assigned_signal_name").toString(); | |
804 | ||
805 | for (const shared_ptr<data::SignalBase>& signal : signalbases) | |
806 | if (signal->name() == assigned_signal_name) | |
807 | channel->assigned_signal = signal.get(); | |
808 | ||
809 | channel->initial_pin_state = settings.value("initial_pin_state").toInt(); | |
810 | ||
811 | settings.endGroup(); | |
812 | } | |
813 | ||
814 | // Update the internal structures | |
815 | stack_config_changed_ = true; | |
816 | update_channel_list(); | |
817 | commit_decoder_channels(); | |
818 | ||
819 | begin_decode(); | |
820 | } | |
821 | ||
822 | void DecodeSignal::set_error_message(QString msg) | |
823 | { | |
824 | error_message_ = msg; | |
825 | // TODO Emulate noquote() | |
826 | qDebug().nospace() << name() << ": " << msg; | |
827 | } | |
828 | ||
829 | uint32_t DecodeSignal::get_input_segment_count() const | |
830 | { | |
831 | uint64_t count = std::numeric_limits<uint64_t>::max(); | |
832 | bool no_signals_assigned = true; | |
833 | ||
834 | for (const decode::DecodeChannel& ch : channels_) | |
835 | if (ch.assigned_signal) { | |
836 | no_signals_assigned = false; | |
837 | ||
838 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
839 | if (!logic_data || logic_data->logic_segments().empty()) | |
840 | return 0; | |
841 | ||
842 | // Find the min value of all segment counts | |
843 | if ((uint64_t)(logic_data->logic_segments().size()) < count) | |
844 | count = logic_data->logic_segments().size(); | |
845 | } | |
846 | ||
847 | return (no_signals_assigned ? 0 : count); | |
848 | } | |
849 | ||
850 | uint32_t DecodeSignal::get_input_samplerate(uint32_t segment_id) const | |
851 | { | |
852 | double samplerate = 0; | |
853 | ||
854 | for (const decode::DecodeChannel& ch : channels_) | |
855 | if (ch.assigned_signal) { | |
856 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
857 | if (!logic_data || logic_data->logic_segments().empty()) | |
858 | continue; | |
859 | ||
860 | try { | |
861 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
862 | samplerate = segment->samplerate(); | |
863 | } catch (out_of_range&) { | |
864 | // Do nothing | |
865 | } | |
866 | break; | |
867 | } | |
868 | ||
869 | return samplerate; | |
870 | } | |
871 | ||
872 | Decoder* DecodeSignal::get_decoder_by_instance(const srd_decoder *const srd_dec) | |
873 | { | |
874 | for (shared_ptr<Decoder>& d : stack_) | |
875 | if (d->get_srd_decoder() == srd_dec) | |
876 | return d.get(); | |
877 | ||
878 | return nullptr; | |
879 | } | |
880 | ||
881 | void DecodeSignal::update_channel_list() | |
882 | { | |
883 | vector<decode::DecodeChannel> prev_channels = channels_; | |
884 | channels_.clear(); | |
885 | ||
886 | uint16_t id = 0; | |
887 | ||
888 | // Copy existing entries, create new as needed | |
889 | for (shared_ptr<Decoder>& decoder : stack_) { | |
890 | const srd_decoder* srd_dec = decoder->get_srd_decoder(); | |
891 | const GSList *l; | |
892 | ||
893 | // Mandatory channels | |
894 | for (l = srd_dec->channels; l; l = l->next) { | |
895 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
896 | bool ch_added = false; | |
897 | ||
898 | // Copy but update ID if this channel was in the list before | |
899 | for (decode::DecodeChannel& ch : prev_channels) | |
900 | if (ch.pdch_ == pdch) { | |
901 | ch.id = id++; | |
902 | channels_.push_back(ch); | |
903 | ch_added = true; | |
904 | break; | |
905 | } | |
906 | ||
907 | if (!ch_added) { | |
908 | // Create new entry without a mapped signal | |
909 | decode::DecodeChannel ch = {id++, 0, false, nullptr, | |
910 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), | |
911 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
912 | channels_.push_back(ch); | |
913 | } | |
914 | } | |
915 | ||
916 | // Optional channels | |
917 | for (l = srd_dec->opt_channels; l; l = l->next) { | |
918 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
919 | bool ch_added = false; | |
920 | ||
921 | // Copy but update ID if this channel was in the list before | |
922 | for (decode::DecodeChannel& ch : prev_channels) | |
923 | if (ch.pdch_ == pdch) { | |
924 | ch.id = id++; | |
925 | channels_.push_back(ch); | |
926 | ch_added = true; | |
927 | break; | |
928 | } | |
929 | ||
930 | if (!ch_added) { | |
931 | // Create new entry without a mapped signal | |
932 | decode::DecodeChannel ch = {id++, 0, true, nullptr, | |
933 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), | |
934 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
935 | channels_.push_back(ch); | |
936 | } | |
937 | } | |
938 | } | |
939 | ||
940 | // Invalidate the logic output data if the channel assignment changed | |
941 | if (prev_channels.size() != channels_.size()) { | |
942 | // The number of channels changed, there's definitely a difference | |
943 | logic_mux_data_invalid_ = true; | |
944 | } else { | |
945 | // Same number but assignment may still differ, so compare all channels | |
946 | for (size_t i = 0; i < channels_.size(); i++) { | |
947 | const decode::DecodeChannel& p_ch = prev_channels[i]; | |
948 | const decode::DecodeChannel& ch = channels_[i]; | |
949 | ||
950 | if ((p_ch.pdch_ != ch.pdch_) || | |
951 | (p_ch.assigned_signal != ch.assigned_signal)) { | |
952 | logic_mux_data_invalid_ = true; | |
953 | break; | |
954 | } | |
955 | } | |
956 | ||
957 | } | |
958 | ||
959 | channels_updated(); | |
960 | } | |
961 | ||
962 | void DecodeSignal::commit_decoder_channels() | |
963 | { | |
964 | // Submit channel list to every decoder, containing only the relevant channels | |
965 | for (shared_ptr<Decoder> dec : stack_) { | |
966 | vector<decode::DecodeChannel*> channel_list; | |
967 | ||
968 | for (decode::DecodeChannel& ch : channels_) | |
969 | if (ch.decoder_ == dec) | |
970 | channel_list.push_back(&ch); | |
971 | ||
972 | dec->set_channels(channel_list); | |
973 | } | |
974 | ||
975 | // Channel bit IDs must be in sync with the channel's apperance in channels_ | |
976 | int id = 0; | |
977 | for (decode::DecodeChannel& ch : channels_) | |
978 | if (ch.assigned_signal) | |
979 | ch.bit_id = id++; | |
980 | } | |
981 | ||
982 | void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end) | |
983 | { | |
984 | // Enforce end to be greater than start | |
985 | if (end <= start) | |
986 | return; | |
987 | ||
988 | // Fetch the channel segments and their data | |
989 | vector<shared_ptr<LogicSegment> > segments; | |
990 | vector<const uint8_t*> signal_data; | |
991 | vector<uint8_t> signal_in_bytepos; | |
992 | vector<uint8_t> signal_in_bitpos; | |
993 | ||
994 | for (decode::DecodeChannel& ch : channels_) | |
995 | if (ch.assigned_signal) { | |
996 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
997 | ||
998 | shared_ptr<LogicSegment> segment; | |
999 | try { | |
1000 | segment = logic_data->logic_segments().at(segment_id); | |
1001 | } catch (out_of_range&) { | |
1002 | qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \ | |
1003 | << "has no logic segment" << segment_id; | |
1004 | return; | |
1005 | } | |
1006 | segments.push_back(segment); | |
1007 | ||
1008 | uint8_t* data = new uint8_t[(end - start) * segment->unit_size()]; | |
1009 | segment->get_samples(start, end, data); | |
1010 | signal_data.push_back(data); | |
1011 | ||
1012 | const int bitpos = ch.assigned_signal->logic_bit_index(); | |
1013 | signal_in_bytepos.push_back(bitpos / 8); | |
1014 | signal_in_bitpos.push_back(bitpos % 8); | |
1015 | } | |
1016 | ||
1017 | ||
1018 | shared_ptr<LogicSegment> output_segment; | |
1019 | try { | |
1020 | output_segment = logic_mux_data_->logic_segments().at(segment_id); | |
1021 | } catch (out_of_range&) { | |
1022 | qDebug() << "Muxer error for" << name() << ": no logic mux segment" \ | |
1023 | << segment_id << "in mux_logic_samples(), mux segments size is" \ | |
1024 | << logic_mux_data_->logic_segments().size(); | |
1025 | return; | |
1026 | } | |
1027 | ||
1028 | // Perform the muxing of signal data into the output data | |
1029 | uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()]; | |
1030 | unsigned int signal_count = signal_data.size(); | |
1031 | ||
1032 | for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start)); | |
1033 | sample_cnt++) { | |
1034 | ||
1035 | int bitpos = 0; | |
1036 | uint8_t bytepos = 0; | |
1037 | ||
1038 | const int out_sample_pos = sample_cnt * output_segment->unit_size(); | |
1039 | for (unsigned int i = 0; i < output_segment->unit_size(); i++) | |
1040 | output[out_sample_pos + i] = 0; | |
1041 | ||
1042 | for (unsigned int i = 0; i < signal_count; i++) { | |
1043 | const int in_sample_pos = sample_cnt * segments[i]->unit_size(); | |
1044 | const uint8_t in_sample = 1 & | |
1045 | ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i])); | |
1046 | ||
1047 | const uint8_t out_sample = output[out_sample_pos + bytepos]; | |
1048 | ||
1049 | output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos); | |
1050 | ||
1051 | bitpos++; | |
1052 | if (bitpos > 7) { | |
1053 | bitpos = 0; | |
1054 | bytepos++; | |
1055 | } | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | output_segment->append_payload(output, (end - start) * output_segment->unit_size()); | |
1060 | delete[] output; | |
1061 | ||
1062 | for (const uint8_t* data : signal_data) | |
1063 | delete[] data; | |
1064 | } | |
1065 | ||
1066 | void DecodeSignal::logic_mux_proc() | |
1067 | { | |
1068 | uint32_t segment_id = 0; | |
1069 | ||
1070 | assert(logic_mux_data_); | |
1071 | ||
1072 | // Create initial logic mux segment | |
1073 | shared_ptr<LogicSegment> output_segment = | |
1074 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, | |
1075 | logic_mux_unit_size_, 0); | |
1076 | logic_mux_data_->push_segment(output_segment); | |
1077 | ||
1078 | output_segment->set_samplerate(get_input_samplerate(0)); | |
1079 | ||
1080 | do { | |
1081 | const uint64_t input_sample_count = get_working_sample_count(segment_id); | |
1082 | const uint64_t output_sample_count = output_segment->get_sample_count(); | |
1083 | ||
1084 | const uint64_t samples_to_process = | |
1085 | (input_sample_count > output_sample_count) ? | |
1086 | (input_sample_count - output_sample_count) : 0; | |
1087 | ||
1088 | // Process the samples if necessary... | |
1089 | if (samples_to_process > 0) { | |
1090 | const uint64_t unit_size = output_segment->unit_size(); | |
1091 | const uint64_t chunk_sample_count = DecodeChunkLength / unit_size; | |
1092 | ||
1093 | uint64_t processed_samples = 0; | |
1094 | do { | |
1095 | const uint64_t start_sample = output_sample_count + processed_samples; | |
1096 | const uint64_t sample_count = | |
1097 | min(samples_to_process - processed_samples, chunk_sample_count); | |
1098 | ||
1099 | mux_logic_samples(segment_id, start_sample, start_sample + sample_count); | |
1100 | processed_samples += sample_count; | |
1101 | ||
1102 | // ...and process the newly muxed logic data | |
1103 | decode_input_cond_.notify_one(); | |
1104 | } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process)); | |
1105 | } | |
1106 | ||
1107 | if (samples_to_process == 0) { | |
1108 | // TODO Optimize this by caching the input segment count and only | |
1109 | // querying it when the cached value was reached | |
1110 | if (segment_id < get_input_segment_count() - 1) { | |
1111 | // Process next segment | |
1112 | segment_id++; | |
1113 | ||
1114 | output_segment = | |
1115 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, | |
1116 | logic_mux_unit_size_, 0); | |
1117 | logic_mux_data_->push_segment(output_segment); | |
1118 | ||
1119 | output_segment->set_samplerate(get_input_samplerate(segment_id)); | |
1120 | ||
1121 | } else { | |
1122 | // All segments have been processed | |
1123 | logic_mux_data_invalid_ = false; | |
1124 | ||
1125 | // Wait for more input | |
1126 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
1127 | logic_mux_cond_.wait(logic_mux_lock); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | } while (!logic_mux_interrupt_); | |
1132 | } | |
1133 | ||
1134 | void DecodeSignal::decode_data( | |
1135 | const int64_t abs_start_samplenum, const int64_t sample_count, | |
1136 | const shared_ptr<LogicSegment> input_segment) | |
1137 | { | |
1138 | const int64_t unit_size = input_segment->unit_size(); | |
1139 | const int64_t chunk_sample_count = DecodeChunkLength / unit_size; | |
1140 | ||
1141 | for (int64_t i = abs_start_samplenum; | |
1142 | error_message_.isEmpty() && !decode_interrupt_ && | |
1143 | (i < (abs_start_samplenum + sample_count)); | |
1144 | i += chunk_sample_count) { | |
1145 | ||
1146 | const int64_t chunk_end = min(i + chunk_sample_count, | |
1147 | abs_start_samplenum + sample_count); | |
1148 | ||
1149 | { | |
1150 | lock_guard<mutex> lock(output_mutex_); | |
1151 | // Update the sample count showing the samples including currently processed ones | |
1152 | segments_.at(current_segment_id_).samples_decoded_incl = chunk_end; | |
1153 | } | |
1154 | ||
1155 | int64_t data_size = (chunk_end - i) * unit_size; | |
1156 | uint8_t* chunk = new uint8_t[data_size]; | |
1157 | input_segment->get_samples(i, chunk_end, chunk); | |
1158 | ||
1159 | if (srd_session_send(srd_session_, i, chunk_end, chunk, | |
1160 | data_size, unit_size) != SRD_OK) | |
1161 | set_error_message(tr("Decoder reported an error")); | |
1162 | ||
1163 | delete[] chunk; | |
1164 | ||
1165 | { | |
1166 | lock_guard<mutex> lock(output_mutex_); | |
1167 | // Now that all samples are processed, the exclusive sample count catches up | |
1168 | segments_.at(current_segment_id_).samples_decoded_excl = chunk_end; | |
1169 | } | |
1170 | ||
1171 | // Notify the frontend that we processed some data and | |
1172 | // possibly have new annotations as well | |
1173 | new_annotations(); | |
1174 | ||
1175 | if (decode_paused_) { | |
1176 | unique_lock<mutex> pause_wait_lock(decode_pause_mutex_); | |
1177 | decode_pause_cond_.wait(pause_wait_lock); | |
1178 | } | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | void DecodeSignal::decode_proc() | |
1183 | { | |
1184 | current_segment_id_ = 0; | |
1185 | ||
1186 | // If there is no input data available yet, wait until it is or we're interrupted | |
1187 | if (logic_mux_data_->logic_segments().size() == 0) { | |
1188 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1189 | decode_input_cond_.wait(input_wait_lock); | |
1190 | } | |
1191 | ||
1192 | if (decode_interrupt_) | |
1193 | return; | |
1194 | ||
1195 | shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front(); | |
1196 | assert(input_segment); | |
1197 | ||
1198 | // Create the initial segment and set its sample rate so that we can pass it to SRD | |
1199 | create_decode_segment(); | |
1200 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); | |
1201 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); | |
1202 | ||
1203 | start_srd_session(); | |
1204 | ||
1205 | uint64_t sample_count = 0; | |
1206 | uint64_t abs_start_samplenum = 0; | |
1207 | do { | |
1208 | // Keep processing new samples until we exhaust the input data | |
1209 | do { | |
1210 | lock_guard<mutex> input_lock(input_mutex_); | |
1211 | sample_count = input_segment->get_sample_count() - abs_start_samplenum; | |
1212 | ||
1213 | if (sample_count > 0) { | |
1214 | decode_data(abs_start_samplenum, sample_count, input_segment); | |
1215 | abs_start_samplenum += sample_count; | |
1216 | } | |
1217 | } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_); | |
1218 | ||
1219 | if (error_message_.isEmpty() && !decode_interrupt_ && sample_count == 0) { | |
1220 | if (current_segment_id_ < logic_mux_data_->logic_segments().size() - 1) { | |
1221 | // Process next segment | |
1222 | current_segment_id_++; | |
1223 | ||
1224 | try { | |
1225 | input_segment = logic_mux_data_->logic_segments().at(current_segment_id_); | |
1226 | } catch (out_of_range&) { | |
1227 | qDebug() << "Decode error for" << name() << ": no logic mux segment" \ | |
1228 | << current_segment_id_ << "in decode_proc(), mux segments size is" \ | |
1229 | << logic_mux_data_->logic_segments().size(); | |
1230 | return; | |
1231 | } | |
1232 | abs_start_samplenum = 0; | |
1233 | ||
1234 | // Create the next segment and set its metadata | |
1235 | create_decode_segment(); | |
1236 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); | |
1237 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); | |
1238 | ||
1239 | // Reset decoder state but keep the decoder stack intact | |
1240 | terminate_srd_session(); | |
1241 | } else { | |
1242 | // All segments have been processed | |
1243 | decode_finished(); | |
1244 | ||
1245 | // Wait for new input data or an interrupt was requested | |
1246 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1247 | decode_input_cond_.wait(input_wait_lock); | |
1248 | } | |
1249 | } | |
1250 | } while (error_message_.isEmpty() && !decode_interrupt_); | |
1251 | ||
1252 | // Potentially reap decoders when the application no longer is | |
1253 | // interested in their (pending) results. | |
1254 | if (decode_interrupt_) | |
1255 | terminate_srd_session(); | |
1256 | } | |
1257 | ||
1258 | void DecodeSignal::start_srd_session() | |
1259 | { | |
1260 | // If there were stack changes, the session has been destroyed by now, so if | |
1261 | // it hasn't been destroyed, we can just reset and re-use it | |
1262 | if (srd_session_) { | |
1263 | // When a decoder stack was created before, re-use it | |
1264 | // for the next stream of input data, after terminating | |
1265 | // potentially still executing operations, and resetting | |
1266 | // internal state. Skip the rather expensive (teardown | |
1267 | // and) construction of another decoder stack. | |
1268 | ||
1269 | // TODO Reduce redundancy, use a common code path for | |
1270 | // the meta/start sequence? | |
1271 | terminate_srd_session(); | |
1272 | ||
1273 | // Metadata is cleared also, so re-set it | |
1274 | uint64_t samplerate = 0; | |
1275 | if (segments_.size() > 0) | |
1276 | samplerate = segments_.at(current_segment_id_).samplerate; | |
1277 | if (samplerate) | |
1278 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
1279 | g_variant_new_uint64(samplerate)); | |
1280 | for (const shared_ptr<Decoder>& dec : stack_) | |
1281 | dec->apply_all_options(); | |
1282 | srd_session_start(srd_session_); | |
1283 | ||
1284 | return; | |
1285 | } | |
1286 | ||
1287 | // Create the session | |
1288 | srd_session_new(&srd_session_); | |
1289 | assert(srd_session_); | |
1290 | ||
1291 | // Create the decoders | |
1292 | srd_decoder_inst *prev_di = nullptr; | |
1293 | for (const shared_ptr<Decoder>& dec : stack_) { | |
1294 | srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_); | |
1295 | ||
1296 | if (!di) { | |
1297 | set_error_message(tr("Failed to create decoder instance")); | |
1298 | srd_session_destroy(srd_session_); | |
1299 | srd_session_ = nullptr; | |
1300 | return; | |
1301 | } | |
1302 | ||
1303 | if (prev_di) | |
1304 | srd_inst_stack(srd_session_, prev_di, di); | |
1305 | ||
1306 | prev_di = di; | |
1307 | } | |
1308 | ||
1309 | // Start the session | |
1310 | if (segments_.size() > 0) | |
1311 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
1312 | g_variant_new_uint64(segments_.at(current_segment_id_).samplerate)); | |
1313 | ||
1314 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN, | |
1315 | DecodeSignal::annotation_callback, this); | |
1316 | ||
1317 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_BINARY, | |
1318 | DecodeSignal::binary_callback, this); | |
1319 | ||
1320 | srd_session_start(srd_session_); | |
1321 | ||
1322 | // We just recreated the srd session, so all stack changes are applied now | |
1323 | stack_config_changed_ = false; | |
1324 | } | |
1325 | ||
1326 | void DecodeSignal::terminate_srd_session() | |
1327 | { | |
1328 | // Call the "terminate and reset" routine for the decoder stack | |
1329 | // (if available). This does not harm those stacks which already | |
1330 | // have completed their operation, and reduces response time for | |
1331 | // those stacks which still are processing data while the | |
1332 | // application no longer wants them to. | |
1333 | if (srd_session_) { | |
1334 | srd_session_terminate_reset(srd_session_); | |
1335 | ||
1336 | // Metadata is cleared also, so re-set it | |
1337 | uint64_t samplerate = 0; | |
1338 | if (segments_.size() > 0) | |
1339 | samplerate = segments_.at(current_segment_id_).samplerate; | |
1340 | if (samplerate) | |
1341 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
1342 | g_variant_new_uint64(samplerate)); | |
1343 | for (const shared_ptr<Decoder>& dec : stack_) | |
1344 | dec->apply_all_options(); | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | void DecodeSignal::stop_srd_session() | |
1349 | { | |
1350 | if (srd_session_) { | |
1351 | // Destroy the session | |
1352 | srd_session_destroy(srd_session_); | |
1353 | srd_session_ = nullptr; | |
1354 | ||
1355 | // Mark the decoder instances as non-existant since they were deleted | |
1356 | for (const shared_ptr<Decoder>& dec : stack_) | |
1357 | dec->invalidate_decoder_inst(); | |
1358 | } | |
1359 | } | |
1360 | ||
1361 | void DecodeSignal::connect_input_notifiers() | |
1362 | { | |
1363 | // Disconnect the notification slot from the previous set of signals | |
1364 | disconnect(this, SLOT(on_data_cleared())); | |
1365 | disconnect(this, SLOT(on_data_received())); | |
1366 | ||
1367 | // Connect the currently used signals to our slot | |
1368 | for (decode::DecodeChannel& ch : channels_) { | |
1369 | if (!ch.assigned_signal) | |
1370 | continue; | |
1371 | ||
1372 | const data::SignalBase *signal = ch.assigned_signal; | |
1373 | connect(signal, SIGNAL(samples_cleared()), | |
1374 | this, SLOT(on_data_cleared())); | |
1375 | connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)), | |
1376 | this, SLOT(on_data_received())); | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | void DecodeSignal::create_decode_segment() | |
1381 | { | |
1382 | // Create annotation segment | |
1383 | segments_.emplace_back(DecodeSegment()); | |
1384 | ||
1385 | // Add annotation classes | |
1386 | for (const shared_ptr<Decoder>& dec : stack_) | |
1387 | for (Row* row : dec->get_rows()) | |
1388 | segments_.back().annotation_rows.emplace(row, RowData(row)); | |
1389 | ||
1390 | // Prepare our binary output classes | |
1391 | for (const shared_ptr<Decoder>& dec : stack_) { | |
1392 | uint32_t n = dec->get_binary_class_count(); | |
1393 | ||
1394 | for (uint32_t i = 0; i < n; i++) | |
1395 | segments_.back().binary_classes.push_back( | |
1396 | {dec.get(), dec->get_binary_class(i), deque<DecodeBinaryDataChunk>()}); | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal) | |
1401 | { | |
1402 | assert(pdata); | |
1403 | assert(decode_signal); | |
1404 | ||
1405 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1406 | assert(ds); | |
1407 | ||
1408 | if (ds->decode_interrupt_) | |
1409 | return; | |
1410 | ||
1411 | lock_guard<mutex> lock(ds->output_mutex_); | |
1412 | ||
1413 | // Get the decoder and the annotation data | |
1414 | assert(pdata->pdo); | |
1415 | assert(pdata->pdo->di); | |
1416 | const srd_decoder *const srd_dec = pdata->pdo->di->decoder; | |
1417 | assert(srd_dec); | |
1418 | ||
1419 | const srd_proto_data_annotation *const pda = (const srd_proto_data_annotation*)pdata->data; | |
1420 | assert(pda); | |
1421 | ||
1422 | // Find the row | |
1423 | Decoder* dec = ds->get_decoder_by_instance(srd_dec); | |
1424 | assert(dec); | |
1425 | ||
1426 | AnnotationClass* ann_class = dec->get_ann_class_by_id(pda->ann_class); | |
1427 | if (!ann_class) { | |
1428 | qWarning() << "Decoder" << ds->display_name() << "wanted to add annotation" << | |
1429 | "with class ID" << pda->ann_class << "but there are only" << | |
1430 | dec->ann_classes().size() << "known classes"; | |
1431 | return; | |
1432 | } | |
1433 | ||
1434 | const Row* row = ann_class->row; | |
1435 | ||
1436 | if (!row) | |
1437 | row = dec->get_row_by_id(0); | |
1438 | ||
1439 | // Add the annotation | |
1440 | ds->segments_[ds->current_segment_id_].annotation_rows.at(row).emplace_annotation(pdata); | |
1441 | } | |
1442 | ||
1443 | void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal) | |
1444 | { | |
1445 | assert(pdata); | |
1446 | assert(decode_signal); | |
1447 | ||
1448 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1449 | assert(ds); | |
1450 | ||
1451 | if (ds->decode_interrupt_) | |
1452 | return; | |
1453 | ||
1454 | // Get the decoder and the binary data | |
1455 | assert(pdata->pdo); | |
1456 | assert(pdata->pdo->di); | |
1457 | const srd_decoder *const srd_dec = pdata->pdo->di->decoder; | |
1458 | assert(srd_dec); | |
1459 | ||
1460 | const srd_proto_data_binary *const pdb = (const srd_proto_data_binary*)pdata->data; | |
1461 | assert(pdb); | |
1462 | ||
1463 | // Find the matching DecodeBinaryClass | |
1464 | DecodeSegment* segment = &(ds->segments_.at(ds->current_segment_id_)); | |
1465 | ||
1466 | DecodeBinaryClass* bin_class = nullptr; | |
1467 | for (DecodeBinaryClass& bc : segment->binary_classes) | |
1468 | if ((bc.decoder->get_srd_decoder() == srd_dec) && | |
1469 | (bc.info->bin_class_id == (uint32_t)pdb->bin_class)) | |
1470 | bin_class = &bc; | |
1471 | ||
1472 | if (!bin_class) { | |
1473 | qWarning() << "Could not find valid DecodeBinaryClass in segment" << | |
1474 | ds->current_segment_id_ << "for binary class ID" << pdb->bin_class << | |
1475 | ", segment only knows" << segment->binary_classes.size() << "classes"; | |
1476 | return; | |
1477 | } | |
1478 | ||
1479 | // Add the data chunk | |
1480 | bin_class->chunks.emplace_back(); | |
1481 | DecodeBinaryDataChunk* chunk = &(bin_class->chunks.back()); | |
1482 | ||
1483 | chunk->sample = pdata->start_sample; | |
1484 | chunk->data.resize(pdb->size); | |
1485 | memcpy(chunk->data.data(), pdb->data, pdb->size); | |
1486 | ||
1487 | Decoder* dec = ds->get_decoder_by_instance(srd_dec); | |
1488 | ||
1489 | ds->new_binary_data(ds->current_segment_id_, (void*)dec, pdb->bin_class); | |
1490 | } | |
1491 | ||
1492 | void DecodeSignal::on_capture_state_changed(int state) | |
1493 | { | |
1494 | // If a new acquisition was started, we need to start decoding from scratch | |
1495 | if (state == Session::Running) { | |
1496 | logic_mux_data_invalid_ = true; | |
1497 | begin_decode(); | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | void DecodeSignal::on_data_cleared() | |
1502 | { | |
1503 | reset_decode(); | |
1504 | } | |
1505 | ||
1506 | void DecodeSignal::on_data_received() | |
1507 | { | |
1508 | // If we detected a lack of input data when trying to start decoding, | |
1509 | // we have set an error message. Only try again if we now have data | |
1510 | // to work with | |
1511 | if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0)) | |
1512 | return; | |
1513 | ||
1514 | if (!logic_mux_thread_.joinable()) | |
1515 | begin_decode(); | |
1516 | else | |
1517 | logic_mux_cond_.notify_one(); | |
1518 | } | |
1519 | ||
1520 | } // namespace data | |
1521 | } // namespace pv |