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