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