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