2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
21 #include <forward_list>
27 #include "logicsegment.hpp"
28 #include "decodesignal.hpp"
29 #include "signaldata.hpp"
31 #include <pv/data/decode/decoder.hpp>
32 #include <pv/data/decode/row.hpp>
33 #include <pv/globalsettings.hpp>
34 #include <pv/session.hpp>
36 using std::dynamic_pointer_cast;
37 using std::lock_guard;
38 using std::make_shared;
40 using std::out_of_range;
41 using std::shared_ptr;
42 using std::unique_lock;
43 using pv::data::decode::AnnotationClass;
44 using pv::data::decode::DecodeChannel;
49 const double DecodeSignal::DecodeMargin = 1.0;
50 const double DecodeSignal::DecodeThreshold = 0.2;
51 const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
54 DecodeSignal::DecodeSignal(pv::Session &session) :
55 SignalBase(nullptr, SignalBase::DecodeChannel),
57 srd_session_(nullptr),
58 logic_mux_data_invalid_(false),
59 stack_config_changed_(true),
60 current_segment_id_(0)
62 connect(&session_, SIGNAL(capture_state_changed(int)),
63 this, SLOT(on_capture_state_changed(int)));
66 DecodeSignal::~DecodeSignal()
71 void DecodeSignal::set_name(QString name)
73 SignalBase::set_name(name);
75 update_output_signals();
78 void DecodeSignal::set_color(QColor color)
80 SignalBase::set_color(color);
82 update_output_signals();
85 const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
90 void DecodeSignal::stack_decoder(const srd_decoder *decoder, bool restart_decode)
94 // Set name if this decoder is the first in the list or the name is unchanged
95 const srd_decoder* prev_dec = stack_.empty() ? nullptr : stack_.back()->get_srd_decoder();
96 const QString prev_dec_name = prev_dec ? QString::fromUtf8(prev_dec->name) : QString();
98 if ((stack_.empty()) || ((stack_.size() > 0) && (name() == prev_dec_name)))
99 set_name(QString::fromUtf8(decoder->name));
101 const shared_ptr<Decoder> dec = make_shared<Decoder>(decoder, stack_.size());
102 stack_.push_back(dec);
104 connect(dec.get(), SIGNAL(annotation_visibility_changed()),
105 this, SLOT(on_annotation_visibility_changed()));
107 // Include the newly created decode channels in the channel lists
108 update_channel_list();
110 stack_config_changed_ = true;
111 auto_assign_signals(dec);
112 commit_decoder_channels();
113 update_output_signals();
115 decoder_stacked((void*)dec.get());
121 void DecodeSignal::remove_decoder(int index)
124 assert(index < (int)stack_.size());
126 // Find the decoder in the stack
127 auto iter = stack_.begin() + index;
128 assert(iter != stack_.end());
130 shared_ptr<Decoder> dec = *iter;
132 decoder_removed(dec.get());
134 // Delete the element
137 // Update channels and decoded data
138 stack_config_changed_ = true;
139 update_channel_list();
143 bool DecodeSignal::toggle_decoder_visibility(int index)
145 auto iter = stack_.cbegin();
146 for (int i = 0; i < index; i++, iter++)
147 assert(iter != stack_.end());
149 shared_ptr<Decoder> dec = *iter;
151 // Toggle decoder visibility
154 state = !dec->visible();
155 dec->set_visible(state);
161 void DecodeSignal::reset_decode(bool shutting_down)
163 resume_decode(); // Make sure the decode thread isn't blocked by pausing
165 if (stack_config_changed_ || shutting_down)
168 terminate_srd_session();
170 if (decode_thread_.joinable()) {
171 decode_interrupt_ = true;
172 decode_input_cond_.notify_one();
173 decode_thread_.join();
176 if (logic_mux_thread_.joinable()) {
177 logic_mux_interrupt_ = true;
178 logic_mux_cond_.notify_one();
179 logic_mux_thread_.join();
182 current_segment_id_ = 0;
185 for (const shared_ptr<decode::Decoder>& dec : stack_)
186 if (dec->has_logic_output())
187 output_logic_[dec->get_srd_decoder()]->clear();
189 logic_mux_data_.reset();
190 logic_mux_data_invalid_ = true;
192 if (!error_message_.isEmpty()) {
193 error_message_.clear();
194 // TODO Emulate noquote()
195 qDebug().nospace() << name() << ": Error cleared";
201 void DecodeSignal::begin_decode()
203 if (decode_thread_.joinable()) {
204 decode_interrupt_ = true;
205 decode_input_cond_.notify_one();
206 decode_thread_.join();
209 if (logic_mux_thread_.joinable()) {
210 logic_mux_interrupt_ = true;
211 logic_mux_cond_.notify_one();
212 logic_mux_thread_.join();
217 if (stack_.size() == 0) {
218 set_error_message(tr("No decoders"));
222 assert(channels_.size() > 0);
224 if (get_assigned_signal_count() == 0) {
225 set_error_message(tr("There are no channels assigned to this decoder"));
229 // Make sure that all assigned channels still provide logic data
230 // (can happen when a converted signal was assigned but the
231 // conversion removed in the meanwhile)
232 for (decode::DecodeChannel& ch : channels_)
233 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
234 ch.assigned_signal = nullptr;
236 // Check that all decoders have the required channels
237 for (const shared_ptr<Decoder>& dec : stack_)
238 if (!dec->have_required_channels()) {
239 set_error_message(tr("One or more required channels "
240 "have not been specified"));
244 // Free the logic data and its segment(s) if it needs to be updated
245 if (logic_mux_data_invalid_)
246 logic_mux_data_.reset();
248 if (!logic_mux_data_) {
249 const uint32_t ch_count = get_assigned_signal_count();
250 logic_mux_unit_size_ = (ch_count + 7) / 8;
251 logic_mux_data_ = make_shared<Logic>(ch_count);
254 if (get_input_segment_count() == 0)
255 set_error_message(tr("No input data"));
257 // Make sure the logic output data is complete and up-to-date
258 logic_mux_interrupt_ = false;
259 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
261 // Decode the muxed logic data
262 decode_interrupt_ = false;
263 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
266 void DecodeSignal::pause_decode()
268 decode_paused_ = true;
271 void DecodeSignal::resume_decode()
273 // Manual unlocking is done before notifying, to avoid waking up the
274 // waiting thread only to block again (see notify_one for details)
275 decode_pause_mutex_.unlock();
276 decode_pause_cond_.notify_one();
277 decode_paused_ = false;
280 bool DecodeSignal::is_paused() const
282 return decode_paused_;
285 const vector<decode::DecodeChannel> DecodeSignal::get_channels() const
290 void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
292 bool new_assignment = false;
294 // Disconnect all input signal notifications so we don't have duplicate connections
295 disconnect_input_notifiers();
297 // Try to auto-select channels that don't have signals assigned yet
298 for (decode::DecodeChannel& ch : channels_) {
299 // If a decoder is given, auto-assign only its channels
300 if (dec && (ch.decoder_ != dec))
303 if (ch.assigned_signal)
306 QString ch_name = ch.name.toLower();
307 ch_name = ch_name.replace(QRegExp("[-_.]"), " ");
309 shared_ptr<data::SignalBase> match;
310 for (const shared_ptr<data::SignalBase>& s : session_.signalbases()) {
314 QString s_name = s->name().toLower();
315 s_name = s_name.replace(QRegExp("[-_.]"), " ");
317 if (s->logic_data() &&
318 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
322 // Only replace an existing match if it matches more characters
323 int old_unmatched = ch_name.length() - match->name().length();
324 int new_unmatched = ch_name.length() - s->name().length();
325 if (abs(new_unmatched) < abs(old_unmatched))
331 // Prevent using a signal more than once as D1 would match e.g. D1 and D10
332 bool signal_not_already_used = true;
333 for (decode::DecodeChannel& ch : channels_)
334 if (ch.assigned_signal && (ch.assigned_signal == match))
335 signal_not_already_used = false;
337 if (match && signal_not_already_used) {
338 ch.assigned_signal = match;
339 new_assignment = true;
343 if (new_assignment) {
344 // Receive notifications when new sample data is available
345 connect_input_notifiers();
347 logic_mux_data_invalid_ = true;
348 stack_config_changed_ = true;
349 commit_decoder_channels();
354 void DecodeSignal::assign_signal(const uint16_t channel_id, shared_ptr<const SignalBase> signal)
356 // Disconnect all input signal notifications so we don't have duplicate connections
357 disconnect_input_notifiers();
359 for (decode::DecodeChannel& ch : channels_)
360 if (ch.id == channel_id) {
361 ch.assigned_signal = signal;
362 logic_mux_data_invalid_ = true;
365 // Receive notifications when new sample data is available
366 connect_input_notifiers();
368 stack_config_changed_ = true;
369 commit_decoder_channels();
374 int DecodeSignal::get_assigned_signal_count() const
376 // Count all channels that have a signal assigned to them
377 return count_if(channels_.begin(), channels_.end(),
378 [](decode::DecodeChannel ch) { return ch.assigned_signal.get(); });
381 void DecodeSignal::update_output_signals()
383 for (const shared_ptr<decode::Decoder>& dec : stack_) {
386 if (dec->has_logic_output()) {
387 const vector<decode::DecoderLogicOutputChannel> logic_channels =
388 dec->logic_output_channels();
390 // All signals of a decoder share the same LogicSegment, so it's
391 // sufficient to check for only the first channel
392 const decode::DecoderLogicOutputChannel& first_ch = logic_channels[0];
394 bool ch_exists = false;
395 for (const shared_ptr<SignalBase>& signal : output_signals_)
396 if (signal->internal_name() == first_ch.id)
400 shared_ptr<Logic> logic_data = make_shared<Logic>(logic_channels.size());
401 logic_data->set_samplerate(get_samplerate());
402 output_logic_[dec->get_srd_decoder()] = logic_data;
403 output_logic_muxed_data_[dec->get_srd_decoder()] = vector<uint8_t>();
405 shared_ptr<LogicSegment> logic_segment = make_shared<data::LogicSegment>(
406 *logic_data, 0, (logic_data->num_channels() + 7) / 8, get_samplerate());
407 logic_data->push_segment(logic_segment);
410 for (const decode::DecoderLogicOutputChannel& logic_ch : logic_channels) {
411 shared_ptr<data::SignalBase> signal =
412 make_shared<data::SignalBase>(nullptr, LogicChannel);
413 signal->set_internal_name(logic_ch.id);
414 signal->set_index(index);
415 signal->set_data(logic_data);
416 output_signals_.push_back(signal);
417 session_.add_generated_signal(signal);
421 shared_ptr<Logic> logic_data = output_logic_[dec->get_srd_decoder()];
422 logic_data->set_samplerate(get_samplerate());
423 for (shared_ptr<LogicSegment>& segment : logic_data->logic_segments())
424 segment->set_samplerate(get_samplerate());
429 for (shared_ptr<SignalBase> s : output_signals_) {
430 s->set_name(s->internal_name() + " (" + name() + ")");
431 s->set_color(color());
434 // TODO Assert that all sample rates are the same as the session's
437 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
439 for (decode::DecodeChannel& ch : channels_)
440 if (ch.id == channel_id)
441 ch.initial_pin_state = init_state;
443 stack_config_changed_ = true;
448 double DecodeSignal::get_samplerate() const
452 // TODO For now, we simply return the first samplerate that we have
453 if (segments_.size() > 0)
454 result = segments_.front().samplerate;
459 const pv::util::Timestamp DecodeSignal::start_time() const
461 pv::util::Timestamp result;
463 // TODO For now, we simply return the first start time that we have
464 if (segments_.size() > 0)
465 result = segments_.front().start_time;
470 int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const
472 // The working sample count is the highest sample number for
473 // which all used signals have data available, so go through all
474 // channels and use the lowest overall sample count of the segment
476 int64_t count = std::numeric_limits<int64_t>::max();
477 bool no_signals_assigned = true;
479 for (const decode::DecodeChannel& ch : channels_)
480 if (ch.assigned_signal) {
481 if (!ch.assigned_signal->logic_data())
484 no_signals_assigned = false;
486 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
487 if (logic_data->logic_segments().empty())
490 if (segment_id >= logic_data->logic_segments().size())
493 const shared_ptr<const LogicSegment> segment = logic_data->logic_segments()[segment_id]->get_shared_ptr();
495 count = min(count, (int64_t)segment->get_sample_count());
498 return (no_signals_assigned ? 0 : count);
501 int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id,
502 bool include_processing) const
504 lock_guard<mutex> decode_lock(output_mutex_);
508 if (segment_id >= segments_.size())
511 if (include_processing)
512 result = segments_[segment_id].samples_decoded_incl;
514 result = segments_[segment_id].samples_decoded_excl;
519 vector<Row*> DecodeSignal::get_rows(bool visible_only)
523 for (const shared_ptr<Decoder>& dec : stack_) {
525 if (visible_only && !dec->visible())
528 for (Row* row : dec->get_rows())
535 vector<const Row*> DecodeSignal::get_rows(bool visible_only) const
537 vector<const Row*> rows;
539 for (const shared_ptr<Decoder>& dec : stack_) {
541 if (visible_only && !dec->visible())
544 for (const Row* row : dec->get_rows())
551 uint64_t DecodeSignal::get_annotation_count(const Row* row, uint32_t segment_id) const
553 if (segment_id >= segments_.size())
556 const DecodeSegment* segment = &(segments_.at(segment_id));
558 auto row_it = segment->annotation_rows.find(row);
561 if (row_it == segment->annotation_rows.end())
564 rd = &(row_it->second);
566 return rd->get_annotation_count();
569 void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
570 const Row* row, uint32_t segment_id, uint64_t start_sample,
571 uint64_t end_sample) const
573 lock_guard<mutex> lock(output_mutex_);
575 if (segment_id >= segments_.size())
578 const DecodeSegment* segment = &(segments_.at(segment_id));
580 auto row_it = segment->annotation_rows.find(row);
583 if (row_it == segment->annotation_rows.end())
586 rd = &(row_it->second);
588 rd->get_annotation_subset(dest, start_sample, end_sample);
591 void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
592 uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const
594 for (const Row* row : get_rows())
595 get_annotation_subset(dest, row, segment_id, start_sample, end_sample);
598 uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id,
599 const Decoder* dec, uint32_t bin_class_id) const
601 if ((segments_.size() == 0) || (segment_id >= segments_.size()))
604 const DecodeSegment *segment = &(segments_[segment_id]);
606 for (const DecodeBinaryClass& bc : segment->binary_classes)
607 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
608 return bc.chunks.size();
613 void DecodeSignal::get_binary_data_chunk(uint32_t segment_id,
614 const Decoder* dec, uint32_t bin_class_id, uint32_t chunk_id,
615 const vector<uint8_t> **dest, uint64_t *size)
617 if (segment_id >= segments_.size())
620 const DecodeSegment *segment = &(segments_[segment_id]);
622 for (const DecodeBinaryClass& bc : segment->binary_classes)
623 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) {
624 if (dest) *dest = &(bc.chunks.at(chunk_id).data);
625 if (size) *size = bc.chunks.at(chunk_id).data.size();
630 void DecodeSignal::get_merged_binary_data_chunks_by_sample(uint32_t segment_id,
631 const Decoder* dec, uint32_t bin_class_id, uint64_t start_sample,
632 uint64_t end_sample, vector<uint8_t> *dest) const
634 assert(dest != nullptr);
636 if (segment_id >= segments_.size())
639 const DecodeSegment *segment = &(segments_[segment_id]);
641 const DecodeBinaryClass* bin_class = nullptr;
642 for (const DecodeBinaryClass& bc : segment->binary_classes)
643 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
646 // Determine overall size before copying to resize dest vector only once
648 uint64_t matches = 0;
649 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
650 if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
651 size += chunk.data.size();
657 uint64_t matches2 = 0;
658 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
659 if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
660 memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size());
661 offset += chunk.data.size();
664 // Make sure we don't overwrite memory if the array grew in the meanwhile
665 if (matches2 == matches)
670 void DecodeSignal::get_merged_binary_data_chunks_by_offset(uint32_t segment_id,
671 const Decoder* dec, uint32_t bin_class_id, uint64_t start, uint64_t end,
672 vector<uint8_t> *dest) const
674 assert(dest != nullptr);
676 if (segment_id >= segments_.size())
679 const DecodeSegment *segment = &(segments_[segment_id]);
681 const DecodeBinaryClass* bin_class = nullptr;
682 for (const DecodeBinaryClass& bc : segment->binary_classes)
683 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
686 // Determine overall size before copying to resize dest vector only once
689 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
691 size += chunk.data.size();
692 offset += chunk.data.size();
699 uint64_t dest_offset = 0;
700 for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
701 if (offset >= start) {
702 memcpy(dest->data() + dest_offset, chunk.data.data(), chunk.data.size());
703 dest_offset += chunk.data.size();
705 offset += chunk.data.size();
711 const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id,
712 const Decoder* dec, uint32_t bin_class_id) const
714 if (segment_id >= segments_.size())
717 const DecodeSegment *segment = &(segments_[segment_id]);
719 for (const DecodeBinaryClass& bc : segment->binary_classes)
720 if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
726 const deque<const Annotation*>* DecodeSignal::get_all_annotations_by_segment(
727 uint32_t segment_id) const
729 if (segment_id >= segments_.size())
732 const DecodeSegment *segment = &(segments_[segment_id]);
734 return &(segment->all_annotations);
737 void DecodeSignal::save_settings(QSettings &settings) const
739 SignalBase::save_settings(settings);
741 settings.setValue("decoders", (int)(stack_.size()));
743 // Save decoder stack
745 for (const shared_ptr<Decoder>& decoder : stack_) {
746 settings.beginGroup("decoder" + QString::number(decoder_idx++));
748 settings.setValue("id", decoder->get_srd_decoder()->id);
749 settings.setValue("visible", decoder->visible());
751 // Save decoder options
752 const map<string, GVariant*>& options = decoder->options();
754 settings.setValue("options", (int)options.size());
756 // Note: Decoder::options() returns only the options
757 // that differ from the default. See binding::Decoder::getter()
759 for (auto& option : options) {
760 settings.beginGroup("option" + QString::number(i));
761 settings.setValue("name", QString::fromStdString(option.first));
762 GlobalSettings::store_gvariant(settings, option.second);
767 // Save row properties
769 for (const Row* row : decoder->get_rows()) {
770 settings.beginGroup("row" + QString::number(i));
771 settings.setValue("visible", row->visible());
776 // Save class properties
778 for (const AnnotationClass* ann_class : decoder->ann_classes()) {
779 settings.beginGroup("ann_class" + QString::number(i));
780 settings.setValue("visible", ann_class->visible());
788 // Save channel mapping
789 settings.setValue("channels", (int)channels_.size());
791 for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
792 auto channel = find_if(channels_.begin(), channels_.end(),
793 [&](decode::DecodeChannel ch) { return ch.id == channel_id; });
795 if (channel == channels_.end()) {
796 qDebug() << "ERROR: Gap in channel index:" << channel_id;
800 settings.beginGroup("channel" + QString::number(channel_id));
802 settings.setValue("name", channel->name); // Useful for debugging
803 settings.setValue("initial_pin_state", channel->initial_pin_state);
805 if (channel->assigned_signal)
806 settings.setValue("assigned_signal_name", channel->assigned_signal->name());
811 // TODO Save logic output signal settings
814 void DecodeSignal::restore_settings(QSettings &settings)
816 SignalBase::restore_settings(settings);
818 // Restore decoder stack
819 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
821 int decoders = settings.value("decoders").toInt();
823 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
824 settings.beginGroup("decoder" + QString::number(decoder_idx));
826 QString id = settings.value("id").toString();
828 for (GSList *entry = dec_list; entry; entry = entry->next) {
829 const srd_decoder *dec = (srd_decoder*)entry->data;
833 if (QString::fromUtf8(dec->id) == id) {
834 shared_ptr<Decoder> decoder = make_shared<Decoder>(dec, stack_.size());
836 connect(decoder.get(), SIGNAL(annotation_visibility_changed()),
837 this, SLOT(on_annotation_visibility_changed()));
839 stack_.push_back(decoder);
840 decoder->set_visible(settings.value("visible", true).toBool());
842 // Restore decoder options that differ from their default
843 int options = settings.value("options").toInt();
845 for (int i = 0; i < options; i++) {
846 settings.beginGroup("option" + QString::number(i));
847 QString name = settings.value("name").toString();
848 GVariant *value = GlobalSettings::restore_gvariant(settings);
849 decoder->set_option(name.toUtf8(), value);
853 // Include the newly created decode channels in the channel lists
854 update_channel_list();
856 // Restore row properties
858 for (Row* row : decoder->get_rows()) {
859 settings.beginGroup("row" + QString::number(i));
860 row->set_visible(settings.value("visible", true).toBool());
865 // Restore class properties
867 for (AnnotationClass* ann_class : decoder->ann_classes()) {
868 settings.beginGroup("ann_class" + QString::number(i));
869 ann_class->set_visible(settings.value("visible", true).toBool());
882 // Restore channel mapping
883 unsigned int channels = settings.value("channels").toInt();
885 const vector< shared_ptr<data::SignalBase> > signalbases =
886 session_.signalbases();
888 for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
889 auto channel = find_if(channels_.begin(), channels_.end(),
890 [&](decode::DecodeChannel ch) { return ch.id == channel_id; });
892 if (channel == channels_.end()) {
893 qDebug() << "ERROR: Non-existant channel index:" << channel_id;
897 settings.beginGroup("channel" + QString::number(channel_id));
899 QString assigned_signal_name = settings.value("assigned_signal_name").toString();
901 for (const shared_ptr<data::SignalBase>& signal : signalbases)
902 if ((signal->name() == assigned_signal_name) && (signal->type() != SignalBase::DecodeChannel))
903 channel->assigned_signal = signal;
905 channel->initial_pin_state = settings.value("initial_pin_state").toInt();
910 connect_input_notifiers();
912 // Update the internal structures
913 stack_config_changed_ = true;
914 update_channel_list();
915 commit_decoder_channels();
916 update_output_signals();
918 // TODO Restore logic output signal settings
923 bool DecodeSignal::all_input_segments_complete(uint32_t segment_id) const
925 bool all_complete = true;
927 for (const decode::DecodeChannel& ch : channels_)
928 if (ch.assigned_signal) {
929 if (!ch.assigned_signal->logic_data())
932 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
933 if (logic_data->logic_segments().empty())
936 if (segment_id >= logic_data->logic_segments().size())
939 const shared_ptr<const LogicSegment> segment = logic_data->logic_segments()[segment_id]->get_shared_ptr();
940 if (segment && !segment->is_complete())
941 all_complete = false;
947 uint32_t DecodeSignal::get_input_segment_count() const
949 uint64_t count = std::numeric_limits<uint64_t>::max();
950 bool no_signals_assigned = true;
952 for (const decode::DecodeChannel& ch : channels_)
953 if (ch.assigned_signal) {
954 no_signals_assigned = false;
956 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
957 if (!logic_data || logic_data->logic_segments().empty())
960 // Find the min value of all segment counts
961 if ((uint64_t)(logic_data->logic_segments().size()) < count)
962 count = logic_data->logic_segments().size();
965 return (no_signals_assigned ? 0 : count);
968 double DecodeSignal::get_input_samplerate(uint32_t segment_id) const
970 double samplerate = 0;
972 for (const decode::DecodeChannel& ch : channels_)
973 if (ch.assigned_signal) {
974 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
975 if (!logic_data || logic_data->logic_segments().empty())
979 const shared_ptr<const LogicSegment> segment =
980 logic_data->logic_segments().at(segment_id)->get_shared_ptr();
982 samplerate = segment->samplerate();
983 } catch (out_of_range&) {
992 Decoder* DecodeSignal::get_decoder_by_instance(const srd_decoder *const srd_dec)
994 for (shared_ptr<Decoder>& d : stack_)
995 if (d->get_srd_decoder() == srd_dec)
1001 void DecodeSignal::update_channel_list()
1003 vector<decode::DecodeChannel> prev_channels = channels_;
1008 // Copy existing entries, create new as needed
1009 for (shared_ptr<Decoder>& decoder : stack_) {
1010 const srd_decoder* srd_dec = decoder->get_srd_decoder();
1013 // Mandatory channels
1014 for (l = srd_dec->channels; l; l = l->next) {
1015 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
1016 bool ch_added = false;
1018 // Copy but update ID if this channel was in the list before
1019 for (decode::DecodeChannel& ch : prev_channels)
1020 if (ch.pdch_ == pdch) {
1022 channels_.push_back(ch);
1028 // Create new entry without a mapped signal
1029 decode::DecodeChannel ch = {id++, 0, false, nullptr,
1030 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
1031 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
1032 channels_.push_back(ch);
1036 // Optional channels
1037 for (l = srd_dec->opt_channels; l; l = l->next) {
1038 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
1039 bool ch_added = false;
1041 // Copy but update ID if this channel was in the list before
1042 for (decode::DecodeChannel& ch : prev_channels)
1043 if (ch.pdch_ == pdch) {
1045 channels_.push_back(ch);
1051 // Create new entry without a mapped signal
1052 decode::DecodeChannel ch = {id++, 0, true, nullptr,
1053 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
1054 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
1055 channels_.push_back(ch);
1060 // Invalidate the logic output data if the channel assignment changed
1061 if (prev_channels.size() != channels_.size()) {
1062 // The number of channels changed, there's definitely a difference
1063 logic_mux_data_invalid_ = true;
1065 // Same number but assignment may still differ, so compare all channels
1066 for (size_t i = 0; i < channels_.size(); i++) {
1067 const decode::DecodeChannel& p_ch = prev_channels[i];
1068 const decode::DecodeChannel& ch = channels_[i];
1070 if ((p_ch.pdch_ != ch.pdch_) ||
1071 (p_ch.assigned_signal != ch.assigned_signal)) {
1072 logic_mux_data_invalid_ = true;
1082 void DecodeSignal::commit_decoder_channels()
1084 // Submit channel list to every decoder, containing only the relevant channels
1085 for (shared_ptr<Decoder> dec : stack_) {
1086 vector<decode::DecodeChannel*> channel_list;
1088 for (decode::DecodeChannel& ch : channels_)
1089 if (ch.decoder_ == dec)
1090 channel_list.push_back(&ch);
1092 dec->set_channels(channel_list);
1095 // Channel bit IDs must be in sync with the channel's apperance in channels_
1097 for (decode::DecodeChannel& ch : channels_)
1098 if (ch.assigned_signal)
1102 void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end)
1104 // Enforce end to be greater than start
1108 // Fetch the channel segments and their data
1109 vector<shared_ptr<const LogicSegment> > segments;
1110 vector<const uint8_t*> signal_data;
1111 vector<uint8_t> signal_in_bytepos;
1112 vector<uint8_t> signal_in_bitpos;
1114 for (decode::DecodeChannel& ch : channels_)
1115 if (ch.assigned_signal) {
1116 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
1118 shared_ptr<const LogicSegment> segment;
1119 if (segment_id < logic_data->logic_segments().size()) {
1120 segment = logic_data->logic_segments().at(segment_id)->get_shared_ptr();
1122 qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \
1123 << "has no logic segment" << segment_id;
1124 logic_mux_interrupt_ = true;
1131 segments.push_back(segment);
1133 uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
1134 segment->get_samples(start, end, data);
1135 signal_data.push_back(data);
1137 const int bitpos = ch.assigned_signal->logic_bit_index();
1138 signal_in_bytepos.push_back(bitpos / 8);
1139 signal_in_bitpos.push_back(bitpos % 8);
1142 shared_ptr<LogicSegment> output_segment;
1144 output_segment = logic_mux_data_->logic_segments().at(segment_id);
1145 } catch (out_of_range&) {
1146 qDebug() << "Muxer error for" << name() << ": no logic mux segment" \
1147 << segment_id << "in mux_logic_samples(), mux segments size is" \
1148 << logic_mux_data_->logic_segments().size();
1149 logic_mux_interrupt_ = true;
1153 // Perform the muxing of signal data into the output data
1154 uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()];
1155 unsigned int signal_count = signal_data.size();
1157 for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start));
1161 uint8_t bytepos = 0;
1163 const int out_sample_pos = sample_cnt * output_segment->unit_size();
1164 for (unsigned int i = 0; i < output_segment->unit_size(); i++)
1165 output[out_sample_pos + i] = 0;
1167 for (unsigned int i = 0; i < signal_count; i++) {
1168 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
1169 const uint8_t in_sample = 1 &
1170 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
1172 const uint8_t out_sample = output[out_sample_pos + bytepos];
1174 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
1184 output_segment->append_payload(output, (end - start) * output_segment->unit_size());
1187 for (const uint8_t* data : signal_data)
1191 void DecodeSignal::logic_mux_proc()
1193 uint32_t input_segment_count;
1195 input_segment_count = get_input_segment_count();
1196 if (input_segment_count == 0) {
1197 // Wait for input data
1198 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
1199 logic_mux_cond_.wait(logic_mux_lock);
1201 } while ((!logic_mux_interrupt_) && (input_segment_count == 0));
1203 if (logic_mux_interrupt_)
1206 assert(logic_mux_data_);
1208 uint32_t segment_id = 0;
1210 // Create initial logic mux segment
1211 shared_ptr<LogicSegment> output_segment =
1212 make_shared<LogicSegment>(*logic_mux_data_, segment_id, logic_mux_unit_size_, 0);
1213 logic_mux_data_->push_segment(output_segment);
1215 output_segment->set_samplerate(get_input_samplerate(0));
1217 // Logic mux data is being updated
1218 logic_mux_data_invalid_ = false;
1220 uint64_t samples_to_process;
1223 const uint64_t input_sample_count = get_working_sample_count(segment_id);
1224 const uint64_t output_sample_count = output_segment->get_sample_count();
1226 samples_to_process =
1227 (input_sample_count > output_sample_count) ?
1228 (input_sample_count - output_sample_count) : 0;
1230 if (samples_to_process > 0) {
1231 const uint64_t unit_size = output_segment->unit_size();
1232 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
1234 uint64_t processed_samples = 0;
1236 const uint64_t start_sample = output_sample_count + processed_samples;
1237 const uint64_t sample_count =
1238 min(samples_to_process - processed_samples, chunk_sample_count);
1240 mux_logic_samples(segment_id, start_sample, start_sample + sample_count);
1241 processed_samples += sample_count;
1243 // ...and process the newly muxed logic data
1244 decode_input_cond_.notify_one();
1245 } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process));
1247 } while (!logic_mux_interrupt_ && (samples_to_process > 0));
1249 if (!logic_mux_interrupt_) {
1250 // samples_to_process is now 0, we've exhausted the currently available input data
1252 // If the input segments are complete, we've completed this segment
1253 if (all_input_segments_complete(segment_id)) {
1254 if (!output_segment->is_complete())
1255 output_segment->set_complete();
1257 if (segment_id < get_input_segment_count() - 1) {
1259 // Process next segment
1263 make_shared<LogicSegment>(*logic_mux_data_, segment_id,
1264 logic_mux_unit_size_, 0);
1265 logic_mux_data_->push_segment(output_segment);
1267 output_segment->set_samplerate(get_input_samplerate(segment_id));
1269 // Wait for more input data if we're processing the currently last segment
1270 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
1271 logic_mux_cond_.wait(logic_mux_lock);
1274 // Input segments aren't all complete yet but samples_to_process is 0, wait for more input data
1275 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
1276 logic_mux_cond_.wait(logic_mux_lock);
1279 } while (!logic_mux_interrupt_);
1282 void DecodeSignal::decode_data(
1283 const int64_t abs_start_samplenum, const int64_t sample_count,
1284 const shared_ptr<const LogicSegment> input_segment)
1286 const int64_t unit_size = input_segment->unit_size();
1287 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
1289 for (int64_t i = abs_start_samplenum;
1290 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
1291 i += chunk_sample_count) {
1293 const int64_t chunk_end = min(i + chunk_sample_count,
1294 abs_start_samplenum + sample_count);
1297 lock_guard<mutex> lock(output_mutex_);
1298 // Update the sample count showing the samples including currently processed ones
1299 segments_.at(current_segment_id_).samples_decoded_incl = chunk_end;
1302 int64_t data_size = (chunk_end - i) * unit_size;
1303 uint8_t* chunk = new uint8_t[data_size];
1304 input_segment->get_samples(i, chunk_end, chunk);
1306 if (srd_session_send(srd_session_, i, chunk_end, chunk,
1307 data_size, unit_size) != SRD_OK) {
1308 set_error_message(tr("Decoder reported an error"));
1309 decode_interrupt_ = true;
1315 lock_guard<mutex> lock(output_mutex_);
1316 // Now that all samples are processed, the exclusive sample count catches up
1317 segments_.at(current_segment_id_).samples_decoded_excl = chunk_end;
1320 // Notify the frontend that we processed some data and
1321 // possibly have new annotations as well
1324 if (decode_paused_) {
1325 unique_lock<mutex> pause_wait_lock(decode_pause_mutex_);
1326 decode_pause_cond_.wait(pause_wait_lock);
1331 void DecodeSignal::decode_proc()
1333 current_segment_id_ = 0;
1335 // If there is no input data available yet, wait until it is or we're interrupted
1337 if (logic_mux_data_->logic_segments().size() == 0) {
1338 // Wait for input data
1339 unique_lock<mutex> input_wait_lock(input_mutex_);
1340 decode_input_cond_.wait(input_wait_lock);
1342 } while ((!decode_interrupt_) && (logic_mux_data_->logic_segments().size() == 0));
1344 if (decode_interrupt_)
1347 shared_ptr<const LogicSegment> input_segment = logic_mux_data_->logic_segments().front()->get_shared_ptr();
1351 // Create the initial segment and set its sample rate so that we can pass it to SRD
1352 create_decode_segment();
1353 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
1354 segments_.at(current_segment_id_).start_time = input_segment->start_time();
1356 start_srd_session();
1358 uint64_t samples_to_process = 0;
1359 uint64_t abs_start_samplenum = 0;
1361 // Keep processing new samples until we exhaust the input data
1363 samples_to_process = input_segment->get_sample_count() - abs_start_samplenum;
1365 if (samples_to_process > 0) {
1366 decode_data(abs_start_samplenum, samples_to_process, input_segment);
1367 abs_start_samplenum += samples_to_process;
1369 } while (!decode_interrupt_ && (samples_to_process > 0));
1371 if (!decode_interrupt_) {
1372 // samples_to_process is now 0, we've exhausted the currently available input data
1374 // If the input segment is complete, we've exhausted this segment
1375 if (input_segment->is_complete()) {
1376 if (current_segment_id_ < (logic_mux_data_->logic_segments().size() - 1)) {
1377 // Process next segment
1378 current_segment_id_++;
1381 input_segment = logic_mux_data_->logic_segments().at(current_segment_id_);
1382 } catch (out_of_range&) {
1383 qDebug() << "Decode error for" << name() << ": no logic mux segment" \
1384 << current_segment_id_ << "in decode_proc(), mux segments size is" \
1385 << logic_mux_data_->logic_segments().size();
1386 decode_interrupt_ = true;
1389 abs_start_samplenum = 0;
1391 // Create the next segment and set its metadata
1392 create_decode_segment();
1393 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
1394 segments_.at(current_segment_id_).start_time = input_segment->start_time();
1396 // Reset decoder state but keep the decoder stack intact
1397 terminate_srd_session();
1399 // All segments have been processed
1400 if (!decode_interrupt_)
1403 // Wait for more input data
1404 unique_lock<mutex> input_wait_lock(input_mutex_);
1405 decode_input_cond_.wait(input_wait_lock);
1408 // Input segment isn't complete yet but samples_to_process is 0, wait for more input data
1409 unique_lock<mutex> input_wait_lock(input_mutex_);
1410 decode_input_cond_.wait(input_wait_lock);
1414 } while (!decode_interrupt_);
1417 void DecodeSignal::start_srd_session()
1419 // If there were stack changes, the session has been destroyed by now, so if
1420 // it hasn't been destroyed, we can just reset and re-use it
1422 // When a decoder stack was created before, re-use it
1423 // for the next stream of input data, after terminating
1424 // potentially still executing operations, and resetting
1425 // internal state. Skip the rather expensive (teardown
1426 // and) construction of another decoder stack.
1428 // TODO Reduce redundancy, use a common code path for
1429 // the meta/start sequence?
1430 terminate_srd_session();
1432 // Metadata is cleared also, so re-set it
1433 uint64_t samplerate = 0;
1434 if (segments_.size() > 0)
1435 samplerate = segments_.at(current_segment_id_).samplerate;
1437 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1438 g_variant_new_uint64(samplerate));
1439 for (const shared_ptr<Decoder>& dec : stack_)
1440 dec->apply_all_options();
1441 srd_session_start(srd_session_);
1446 // Update the samplerates for the output logic channels
1447 update_output_signals();
1449 // Create the session
1450 srd_session_new(&srd_session_);
1451 assert(srd_session_);
1453 // Create the decoders
1454 srd_decoder_inst *prev_di = nullptr;
1455 for (const shared_ptr<Decoder>& dec : stack_) {
1456 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
1459 set_error_message(tr("Failed to create decoder instance"));
1460 srd_session_destroy(srd_session_);
1461 srd_session_ = nullptr;
1466 srd_inst_stack(srd_session_, prev_di, di);
1471 // Start the session
1472 if (segments_.size() > 0)
1473 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1474 g_variant_new_uint64(segments_.at(current_segment_id_).samplerate));
1476 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
1477 DecodeSignal::annotation_callback, this);
1479 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_BINARY,
1480 DecodeSignal::binary_callback, this);
1482 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_LOGIC,
1483 DecodeSignal::logic_output_callback, this);
1485 srd_session_start(srd_session_);
1487 // We just recreated the srd session, so all stack changes are applied now
1488 stack_config_changed_ = false;
1491 void DecodeSignal::terminate_srd_session()
1493 // Call the "terminate and reset" routine for the decoder stack
1494 // (if available). This does not harm those stacks which already
1495 // have completed their operation, and reduces response time for
1496 // those stacks which still are processing data while the
1497 // application no longer wants them to.
1499 srd_session_terminate_reset(srd_session_);
1501 // Metadata is cleared also, so re-set it
1502 uint64_t samplerate = 0;
1503 if (segments_.size() > 0)
1504 samplerate = segments_.at(current_segment_id_).samplerate;
1506 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1507 g_variant_new_uint64(samplerate));
1508 for (const shared_ptr<Decoder>& dec : stack_)
1509 dec->apply_all_options();
1513 void DecodeSignal::stop_srd_session()
1516 // Destroy the session
1517 srd_session_destroy(srd_session_);
1518 srd_session_ = nullptr;
1520 // Mark the decoder instances as non-existant since they were deleted
1521 for (const shared_ptr<Decoder>& dec : stack_)
1522 dec->invalidate_decoder_inst();
1526 void DecodeSignal::connect_input_notifiers()
1528 // Connect the currently used signals to our slot
1529 for (decode::DecodeChannel& ch : channels_) {
1530 if (!ch.assigned_signal)
1532 const data::SignalBase *signal = ch.assigned_signal.get();
1534 connect(signal, SIGNAL(samples_cleared()),
1535 this, SLOT(on_data_cleared()), Qt::UniqueConnection);
1536 connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)),
1537 this, SLOT(on_data_received()), Qt::UniqueConnection);
1539 if (signal->logic_data())
1540 connect(signal->logic_data().get(), SIGNAL(segment_completed()),
1541 this, SLOT(on_input_segment_completed()), Qt::UniqueConnection);
1545 void DecodeSignal::disconnect_input_notifiers()
1547 // Disconnect the notification slot from the previous set of signals
1548 for (decode::DecodeChannel& ch : channels_) {
1549 if (!ch.assigned_signal)
1551 const data::SignalBase *signal = ch.assigned_signal.get();
1552 disconnect(signal, nullptr, this, SLOT(on_data_cleared()));
1553 disconnect(signal, nullptr, this, SLOT(on_data_received()));
1555 if (signal->logic_data())
1556 disconnect(signal->logic_data().get(), nullptr, this, SLOT(on_input_segment_completed()));
1560 void DecodeSignal::create_decode_segment()
1562 // Create annotation segment
1563 segments_.emplace_back();
1565 // Add annotation classes
1566 for (const shared_ptr<Decoder>& dec : stack_)
1567 for (Row* row : dec->get_rows())
1568 segments_.back().annotation_rows.emplace(row, RowData(row));
1570 // Prepare our binary output classes
1571 for (const shared_ptr<Decoder>& dec : stack_) {
1572 uint32_t n = dec->get_binary_class_count();
1574 for (uint32_t i = 0; i < n; i++)
1575 segments_.back().binary_classes.push_back(
1576 {dec.get(), dec->get_binary_class(i), deque<DecodeBinaryDataChunk>()});
1580 void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
1583 assert(decode_signal);
1585 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1588 if (ds->decode_interrupt_)
1591 if (ds->segments_.empty())
1594 lock_guard<mutex> lock(ds->output_mutex_);
1596 // Get the decoder and the annotation data
1598 assert(pdata->pdo->di);
1599 const srd_decoder *const srd_dec = pdata->pdo->di->decoder;
1602 const srd_proto_data_annotation *const pda = (const srd_proto_data_annotation*)pdata->data;
1606 Decoder* dec = ds->get_decoder_by_instance(srd_dec);
1609 AnnotationClass* ann_class = dec->get_ann_class_by_id(pda->ann_class);
1611 qWarning() << "Decoder" << ds->display_name() << "wanted to add annotation" <<
1612 "with class ID" << pda->ann_class << "but there are only" <<
1613 dec->ann_classes().size() << "known classes";
1617 const Row* row = ann_class->row;
1620 row = dec->get_row_by_id(0);
1622 RowData& row_data = ds->segments_[ds->current_segment_id_].annotation_rows.at(row);
1624 // Add the annotation to the row
1625 const Annotation* ann = row_data.emplace_annotation(pdata);
1627 // We insert the annotation into the global annotation list in a way so that
1628 // the annotation list is sorted by start sample and length. Otherwise, we'd
1629 // have to sort the model, which is expensive
1630 deque<const Annotation*>& all_annotations =
1631 ds->segments_[ds->current_segment_id_].all_annotations;
1633 if (all_annotations.empty()) {
1634 all_annotations.emplace_back(ann);
1636 const uint64_t new_ann_len = (pdata->end_sample - pdata->start_sample);
1637 bool ann_has_earlier_start = (pdata->start_sample < all_annotations.back()->start_sample());
1638 bool ann_is_longer = (new_ann_len >
1639 (all_annotations.back()->end_sample() - all_annotations.back()->start_sample()));
1641 if (ann_has_earlier_start && ann_is_longer) {
1642 bool ann_has_same_start;
1643 auto it = all_annotations.end();
1647 ann_has_earlier_start = (pdata->start_sample < (*it)->start_sample());
1648 ann_has_same_start = (pdata->start_sample == (*it)->start_sample());
1649 ann_is_longer = (new_ann_len > (*it)->length());
1650 } while ((ann_has_earlier_start || (ann_has_same_start && ann_is_longer)) && (it != all_annotations.begin()));
1652 // Allow inserting at the front
1653 if (it != all_annotations.begin())
1656 all_annotations.emplace(it, ann);
1658 all_annotations.emplace_back(ann);
1661 // When emplace_annotation() inserts instead of appends an annotation,
1662 // the pointers in all_annotations that follow the inserted annotation and
1663 // point to annotations for this row are off by one and must be updated
1664 if (&(row_data.annotations().back()) != ann) {
1665 // Search backwards until we find the annotation we just added
1666 auto row_it = row_data.annotations().end();
1667 auto all_it = all_annotations.end();
1670 if ((*all_it)->row_data() == &row_data)
1672 } while (&(*row_it) != ann);
1674 // Update the annotation addresses for this row's annotations until the end
1676 if ((*all_it)->row_data() == &row_data) {
1677 *all_it = &(*row_it);
1681 } while (all_it != all_annotations.end());
1685 void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal)
1688 assert(decode_signal);
1690 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1693 if (ds->decode_interrupt_)
1696 // Get the decoder and the binary data
1698 assert(pdata->pdo->di);
1699 const srd_decoder *const srd_dec = pdata->pdo->di->decoder;
1702 const srd_proto_data_binary *const pdb = (const srd_proto_data_binary*)pdata->data;
1705 // Find the matching DecodeBinaryClass
1706 DecodeSegment* segment = &(ds->segments_.at(ds->current_segment_id_));
1708 DecodeBinaryClass* bin_class = nullptr;
1709 for (DecodeBinaryClass& bc : segment->binary_classes)
1710 if ((bc.decoder->get_srd_decoder() == srd_dec) &&
1711 (bc.info->bin_class_id == (uint32_t)pdb->bin_class))
1715 qWarning() << "Could not find valid DecodeBinaryClass in segment" <<
1716 ds->current_segment_id_ << "for binary class ID" << pdb->bin_class <<
1717 ", segment only knows" << segment->binary_classes.size() << "classes";
1721 // Add the data chunk
1722 bin_class->chunks.emplace_back();
1723 DecodeBinaryDataChunk* chunk = &(bin_class->chunks.back());
1725 chunk->sample = pdata->start_sample;
1726 chunk->data.resize(pdb->size);
1727 memcpy(chunk->data.data(), pdb->data, pdb->size);
1729 Decoder* dec = ds->get_decoder_by_instance(srd_dec);
1731 ds->new_binary_data(ds->current_segment_id_, (void*)dec, pdb->bin_class);
1734 void DecodeSignal::logic_output_callback(srd_proto_data *pdata, void *decode_signal)
1737 assert(decode_signal);
1739 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1742 if (ds->decode_interrupt_)
1745 lock_guard<mutex> lock(ds->output_mutex_);
1748 assert(pdata->pdo->di);
1749 const srd_decoder *const decc = pdata->pdo->di->decoder;
1752 const srd_proto_data_logic *const pdl = (const srd_proto_data_logic*)pdata->data;
1755 assert(pdl->logic_group == 0); // FIXME Only one group supported for now
1757 shared_ptr<Logic> output_logic = ds->output_logic_.at(decc);
1759 vector< shared_ptr<Segment> > segments = output_logic->segments();
1761 shared_ptr<LogicSegment> last_segment;
1763 if (!segments.empty())
1764 last_segment = dynamic_pointer_cast<LogicSegment>(segments.back());
1766 // Happens when the data was cleared - all segments are gone then
1767 // segment_id is always 0 as it's the first segment
1768 last_segment = make_shared<data::LogicSegment>(
1769 *output_logic, 0, (output_logic->num_channels() + 7) / 8, output_logic->get_samplerate());
1770 output_logic->push_segment(last_segment);
1773 if (pdata->start_sample < pdata->end_sample) {
1774 vector<uint8_t> data;
1775 data.reserve(pdl->repeat_count + 1); // FIXME Include unit size in calculation
1776 for (unsigned int i = 0; i <= pdl->repeat_count; i++)
1777 // FIXME Currently only supports 8 channels as we ignore the unit size
1778 data.emplace_back(*((uint8_t*)pdl->data));
1780 last_segment->append_payload(data.data(), data.size());
1782 qInfo() << "Received logic output state change for group" << pdl->logic_group << "from decoder" \
1783 << QString::fromUtf8(decc->name) << "from" << pdata->start_sample << "to" << pdata->end_sample << ":" << *((uint8_t*)pdl->data);
1786 qWarning() << "Ignoring malformed logic output state change for group" << pdl->logic_group << "from decoder" \
1787 << QString::fromUtf8(decc->name) << "from" << pdata->start_sample << "to" << pdata->end_sample;
1790 void DecodeSignal::on_capture_state_changed(int state)
1792 // If a new acquisition was started, we need to start decoding from scratch
1793 if (state == Session::Running) {
1794 logic_mux_data_invalid_ = true;
1799 void DecodeSignal::on_data_cleared()
1804 void DecodeSignal::on_data_received()
1806 // If we detected a lack of input data when trying to start decoding,
1807 // we have set an error message. Bail out if we still don't have data
1809 if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0))
1812 if (!error_message_.isEmpty()) {
1813 error_message_.clear();
1814 // TODO Emulate noquote()
1815 qDebug().nospace() << name() << ": Input data available, error cleared";
1818 if (!logic_mux_thread_.joinable())
1821 logic_mux_cond_.notify_one();
1824 void DecodeSignal::on_input_segment_completed()
1826 if (!logic_mux_thread_.joinable())
1827 logic_mux_cond_.notify_one();
1830 void DecodeSignal::on_annotation_visibility_changed()
1832 annotation_visibility_changed();