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