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