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