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/>.
25 #include "logicsegment.hpp"
26 #include "decodesignal.hpp"
27 #include "signaldata.hpp"
29 #include <pv/binding/decoder.hpp>
30 #include <pv/data/decode/decoder.hpp>
31 #include <pv/data/decode/row.hpp>
32 #include <pv/globalsettings.hpp>
33 #include <pv/session.hpp>
35 using std::lock_guard;
37 using std::make_shared;
39 using std::shared_ptr;
40 using std::unique_lock;
41 using pv::data::decode::Annotation;
42 using pv::data::decode::Decoder;
43 using pv::data::decode::Row;
48 const double DecodeSignal::DecodeMargin = 1.0;
49 const double DecodeSignal::DecodeThreshold = 0.2;
50 const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
53 DecodeSignal::DecodeSignal(pv::Session &session) :
54 SignalBase(nullptr, SignalBase::DecodeChannel),
56 srd_session_(nullptr),
57 logic_mux_data_invalid_(false),
61 frame_complete_(false)
63 connect(&session_, SIGNAL(capture_state_changed(int)),
64 this, SLOT(on_capture_state_changed(int)));
67 DecodeSignal::~DecodeSignal()
72 const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
77 void DecodeSignal::stack_decoder(const srd_decoder *decoder)
80 const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder);
82 stack_.push_back(dec);
84 // Set name if this decoder is the first in the list
85 if (stack_.size() == 1)
86 set_name(QString::fromUtf8(decoder->name));
88 // Include the newly created decode channels in the channel lists
89 update_channel_list();
91 auto_assign_signals(dec);
92 commit_decoder_channels();
96 void DecodeSignal::remove_decoder(int index)
99 assert(index < (int)stack_.size());
101 // Find the decoder in the stack
102 auto iter = stack_.begin();
103 for (int i = 0; i < index; i++, iter++)
104 assert(iter != stack_.end());
106 // Delete the element
109 // Update channels and decoded data
110 update_channel_list();
114 bool DecodeSignal::toggle_decoder_visibility(int index)
116 auto iter = stack_.cbegin();
117 for (int i = 0; i < index; i++, iter++)
118 assert(iter != stack_.end());
120 shared_ptr<Decoder> dec = *iter;
122 // Toggle decoder visibility
125 state = !dec->shown();
132 void DecodeSignal::reset_decode()
134 if (decode_thread_.joinable()) {
135 decode_interrupt_ = true;
136 decode_input_cond_.notify_one();
137 decode_thread_.join();
140 if (logic_mux_thread_.joinable()) {
141 logic_mux_interrupt_ = true;
142 logic_mux_cond_.notify_one();
143 logic_mux_thread_.join();
148 frame_complete_ = false;
149 samples_decoded_ = 0;
150 error_message_ = QString();
153 current_rows_= nullptr;
156 logic_mux_data_.reset();
157 logic_mux_data_invalid_ = true;
162 void DecodeSignal::begin_decode()
164 if (decode_thread_.joinable()) {
165 decode_interrupt_ = true;
166 decode_input_cond_.notify_one();
167 decode_thread_.join();
170 if (logic_mux_thread_.joinable()) {
171 logic_mux_interrupt_ = true;
172 logic_mux_cond_.notify_one();
173 logic_mux_thread_.join();
178 if (stack_.size() == 0) {
179 error_message_ = tr("No decoders");
183 assert(channels_.size() > 0);
185 if (get_assigned_signal_count() == 0) {
186 error_message_ = tr("There are no channels assigned to this decoder");
190 // Make sure that all assigned channels still provide logic data
191 // (can happen when a converted signal was assigned but the
192 // conversion removed in the meanwhile)
193 for (data::DecodeChannel &ch : channels_)
194 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
195 ch.assigned_signal = nullptr;
197 // Check that all decoders have the required channels
198 for (const shared_ptr<decode::Decoder> &dec : stack_)
199 if (!dec->have_required_channels()) {
200 error_message_ = tr("One or more required channels "
201 "have not been specified");
205 // Map out all the annotation classes
206 for (const shared_ptr<decode::Decoder> &dec : stack_) {
208 const srd_decoder *const decc = dec->decoder();
209 assert(dec->decoder());
211 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
212 const srd_decoder_annotation_row *const ann_row =
213 (srd_decoder_annotation_row *)l->data;
216 const Row row(decc, ann_row);
218 for (const GSList *ll = ann_row->ann_classes;
220 class_rows_[make_pair(decc,
221 GPOINTER_TO_INT(ll->data))] = row;
225 prepare_annotation_segment();
227 // Free the logic data and its segment(s) if it needs to be updated
228 if (logic_mux_data_invalid_)
229 logic_mux_data_.reset();
231 if (!logic_mux_data_) {
232 const int64_t ch_count = get_assigned_signal_count();
233 const int64_t unit_size = (ch_count + 7) / 8;
234 logic_mux_data_ = make_shared<Logic>(ch_count);
235 logic_mux_segment_ = make_shared<LogicSegment>(*logic_mux_data_, unit_size, samplerate_);
236 logic_mux_data_->push_segment(logic_mux_segment_);
239 // Make sure the logic output data is complete and up-to-date
240 logic_mux_interrupt_ = false;
241 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
243 // Decode the muxed logic data
244 decode_interrupt_ = false;
245 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
247 // Receive notifications when new sample data is available
248 connect_input_notifiers();
251 QString DecodeSignal::error_message() const
253 lock_guard<mutex> lock(output_mutex_);
254 return error_message_;
257 const vector<data::DecodeChannel> DecodeSignal::get_channels() const
262 void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
264 bool new_assignment = false;
266 // Try to auto-select channels that don't have signals assigned yet
267 for (data::DecodeChannel &ch : channels_) {
268 // If a decoder is given, auto-assign only its channels
269 if (dec && (ch.decoder_ != dec))
272 if (ch.assigned_signal)
275 for (shared_ptr<data::SignalBase> s : session_.signalbases()) {
276 const QString ch_name = ch.name.toLower();
277 const QString s_name = s->name().toLower();
279 if (s->logic_data() &&
280 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
281 ch.assigned_signal = s.get();
282 new_assignment = true;
287 if (new_assignment) {
288 logic_mux_data_invalid_ = true;
289 commit_decoder_channels();
294 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
296 for (data::DecodeChannel &ch : channels_)
297 if (ch.id == channel_id) {
298 ch.assigned_signal = signal;
299 logic_mux_data_invalid_ = true;
302 commit_decoder_channels();
307 int DecodeSignal::get_assigned_signal_count() const
309 // Count all channels that have a signal assigned to them
310 return count_if(channels_.begin(), channels_.end(),
311 [](data::DecodeChannel ch) { return ch.assigned_signal; });
314 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
316 for (data::DecodeChannel &ch : channels_)
317 if (ch.id == channel_id)
318 ch.initial_pin_state = init_state;
325 double DecodeSignal::samplerate() const
330 const pv::util::Timestamp& DecodeSignal::start_time() const
335 int64_t DecodeSignal::get_working_sample_count() const
337 // The working sample count is the highest sample number for
338 // which all used signals have data available, so go through
339 // all channels and use the lowest overall sample count of the
342 // TODO Currently, we assume only a single segment exists
344 int64_t count = std::numeric_limits<int64_t>::max();
345 bool no_signals_assigned = true;
347 for (const data::DecodeChannel &ch : channels_)
348 if (ch.assigned_signal) {
349 no_signals_assigned = false;
351 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
352 if (!logic_data || logic_data->logic_segments().empty())
355 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
356 count = min(count, (int64_t)segment->get_sample_count());
359 return (no_signals_assigned ? 0 : count);
362 int64_t DecodeSignal::get_decoded_sample_count() const
364 lock_guard<mutex> decode_lock(output_mutex_);
365 return samples_decoded_;
368 vector<Row> DecodeSignal::visible_rows() const
370 lock_guard<mutex> lock(output_mutex_);
374 for (const shared_ptr<decode::Decoder> &dec : stack_) {
379 const srd_decoder *const decc = dec->decoder();
380 assert(dec->decoder());
382 // Add a row for the decoder if it doesn't have a row list
383 if (!decc->annotation_rows)
384 rows.emplace_back(decc);
386 // Add the decoder rows
387 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
388 const srd_decoder_annotation_row *const ann_row =
389 (srd_decoder_annotation_row *)l->data;
391 rows.emplace_back(decc, ann_row);
398 void DecodeSignal::get_annotation_subset(
399 vector<pv::data::decode::Annotation> &dest,
400 const decode::Row &row, uint64_t start_sample,
401 uint64_t end_sample) const
403 lock_guard<mutex> lock(output_mutex_);
408 // TODO Instead of current_rows_, use rows_ and the ID of the segment
410 const auto iter = current_rows_->find(row);
411 if (iter != current_rows_->end())
412 (*iter).second.get_annotation_subset(dest,
413 start_sample, end_sample);
416 void DecodeSignal::save_settings(QSettings &settings) const
418 SignalBase::save_settings(settings);
420 settings.setValue("decoders", (int)(stack_.size()));
422 // Save decoder stack
424 for (shared_ptr<decode::Decoder> decoder : stack_) {
425 settings.beginGroup("decoder" + QString::number(decoder_idx++));
427 settings.setValue("id", decoder->decoder()->id);
429 // Save decoder options
430 const map<string, GVariant*>& options = decoder->options();
432 settings.setValue("options", (int)options.size());
434 // Note: decode::Decoder::options() returns only the options
435 // that differ from the default. See binding::Decoder::getter()
437 for (auto option : options) {
438 settings.beginGroup("option" + QString::number(i));
439 settings.setValue("name", QString::fromStdString(option.first));
440 GlobalSettings::store_gvariant(settings, option.second);
448 // Save channel mapping
449 settings.setValue("channels", (int)channels_.size());
451 for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
452 auto channel = find_if(channels_.begin(), channels_.end(),
453 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
455 if (channel == channels_.end()) {
456 qDebug() << "ERROR: Gap in channel index:" << channel_id;
460 settings.beginGroup("channel" + QString::number(channel_id));
462 settings.setValue("name", channel->name); // Useful for debugging
463 settings.setValue("initial_pin_state", channel->initial_pin_state);
465 if (channel->assigned_signal)
466 settings.setValue("assigned_signal_name", channel->assigned_signal->name());
472 void DecodeSignal::restore_settings(QSettings &settings)
474 SignalBase::restore_settings(settings);
476 // Restore decoder stack
477 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
479 int decoders = settings.value("decoders").toInt();
481 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
482 settings.beginGroup("decoder" + QString::number(decoder_idx));
484 QString id = settings.value("id").toString();
486 for (GSList *entry = dec_list; entry; entry = entry->next) {
487 const srd_decoder *dec = (srd_decoder*)entry->data;
491 if (QString::fromUtf8(dec->id) == id) {
492 shared_ptr<decode::Decoder> decoder =
493 make_shared<decode::Decoder>(dec);
495 stack_.push_back(decoder);
497 // Restore decoder options that differ from their default
498 int options = settings.value("options").toInt();
500 for (int i = 0; i < options; i++) {
501 settings.beginGroup("option" + QString::number(i));
502 QString name = settings.value("name").toString();
503 GVariant *value = GlobalSettings::restore_gvariant(settings);
504 decoder->set_option(name.toUtf8(), value);
508 // Include the newly created decode channels in the channel lists
509 update_channel_list();
510 commit_decoder_channels();
519 // Restore channel mapping
520 unsigned int channels = settings.value("channels").toInt();
522 const unordered_set< shared_ptr<data::SignalBase> > signalbases =
523 session_.signalbases();
525 for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
526 auto channel = find_if(channels_.begin(), channels_.end(),
527 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
529 if (channel == channels_.end()) {
530 qDebug() << "ERROR: Non-existant channel index:" << channel_id;
534 settings.beginGroup("channel" + QString::number(channel_id));
536 QString assigned_signal_name = settings.value("assigned_signal_name").toString();
538 for (shared_ptr<data::SignalBase> signal : signalbases)
539 if (signal->name() == assigned_signal_name)
540 channel->assigned_signal = signal.get();
542 channel->initial_pin_state = settings.value("initial_pin_state").toInt();
550 void DecodeSignal::update_channel_list()
552 vector<data::DecodeChannel> prev_channels = channels_;
557 // Copy existing entries, create new as needed
558 for (shared_ptr<Decoder> decoder : stack_) {
559 const srd_decoder* srd_d = decoder->decoder();
562 // Mandatory channels
563 for (l = srd_d->channels; l; l = l->next) {
564 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
565 bool ch_added = false;
567 // Copy but update ID if this channel was in the list before
568 for (data::DecodeChannel &ch : prev_channels)
569 if (ch.pdch_ == pdch) {
571 channels_.push_back(ch);
577 // Create new entry without a mapped signal
578 data::DecodeChannel ch = {id++, 0, false, nullptr,
579 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
580 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
581 channels_.push_back(ch);
586 for (l = srd_d->opt_channels; l; l = l->next) {
587 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
588 bool ch_added = false;
590 // Copy but update ID if this channel was in the list before
591 for (data::DecodeChannel &ch : prev_channels)
592 if (ch.pdch_ == pdch) {
594 channels_.push_back(ch);
600 // Create new entry without a mapped signal
601 data::DecodeChannel ch = {id++, 0, true, nullptr,
602 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
603 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
604 channels_.push_back(ch);
609 // Invalidate the logic output data if the channel assignment changed
610 if (prev_channels.size() != channels_.size()) {
611 // The number of channels changed, there's definitely a difference
612 logic_mux_data_invalid_ = true;
614 // Same number but assignment may still differ, so compare all channels
615 for (size_t i = 0; i < channels_.size(); i++) {
616 const data::DecodeChannel &p_ch = prev_channels[i];
617 const data::DecodeChannel &ch = channels_[i];
619 if ((p_ch.pdch_ != ch.pdch_) ||
620 (p_ch.assigned_signal != ch.assigned_signal)) {
621 logic_mux_data_invalid_ = true;
631 void DecodeSignal::commit_decoder_channels()
633 // Submit channel list to every decoder, containing only the relevant channels
634 for (shared_ptr<decode::Decoder> dec : stack_) {
635 vector<data::DecodeChannel*> channel_list;
637 for (data::DecodeChannel &ch : channels_)
638 if (ch.decoder_ == dec)
639 channel_list.push_back(&ch);
641 dec->set_channels(channel_list);
645 void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end)
647 // Enforce end to be greater than start
651 // Fetch all segments and their data
652 // TODO Currently, we assume only a single segment exists
653 vector<shared_ptr<LogicSegment> > segments;
654 vector<const uint8_t*> signal_data;
655 vector<uint8_t> signal_in_bytepos;
656 vector<uint8_t> signal_in_bitpos;
659 for (data::DecodeChannel &ch : channels_)
660 if (ch.assigned_signal) {
663 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
664 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
665 segments.push_back(segment);
667 uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
668 segment->get_samples(start, end, data);
669 signal_data.push_back(data);
671 const int bitpos = ch.assigned_signal->logic_bit_index();
672 signal_in_bytepos.push_back(bitpos / 8);
673 signal_in_bitpos.push_back(bitpos % 8);
676 // Perform the muxing of signal data into the output data
677 uint8_t* output = new uint8_t[(end - start) * logic_mux_segment_->unit_size()];
678 unsigned int signal_count = signal_data.size();
680 for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) {
684 const int out_sample_pos = sample_cnt * logic_mux_segment_->unit_size();
685 for (unsigned int i = 0; i < logic_mux_segment_->unit_size(); i++)
686 output[out_sample_pos + i] = 0;
688 for (unsigned int i = 0; i < signal_count; i++) {
689 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
690 const uint8_t in_sample = 1 &
691 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
693 const uint8_t out_sample = output[out_sample_pos + bytepos];
695 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
705 logic_mux_segment_->append_payload(output, (end - start) * logic_mux_segment_->unit_size());
708 for (const uint8_t* data : signal_data)
712 void DecodeSignal::logic_mux_proc()
715 const uint64_t input_sample_count = get_working_sample_count();
716 const uint64_t output_sample_count = logic_mux_segment_->get_sample_count();
718 const uint64_t samples_to_process =
719 (input_sample_count > output_sample_count) ?
720 (input_sample_count - output_sample_count) : 0;
722 // Process the samples if necessary...
723 if (samples_to_process > 0) {
724 const uint64_t unit_size = logic_mux_segment_->unit_size();
725 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
727 uint64_t processed_samples = 0;
729 const uint64_t start_sample = output_sample_count + processed_samples;
730 const uint64_t sample_count =
731 min(samples_to_process - processed_samples, chunk_sample_count);
733 mux_logic_samples(start_sample, start_sample + sample_count);
734 processed_samples += sample_count;
736 // ...and process the newly muxed logic data
737 decode_input_cond_.notify_one();
738 } while (processed_samples < samples_to_process);
741 if (samples_to_process == 0) {
742 // Wait for more input
743 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
744 logic_mux_cond_.wait(logic_mux_lock);
746 } while (!logic_mux_interrupt_);
748 // No more input data and session is stopped, let the decode thread
749 // process any pending data, terminate and release the global SRD mutex
750 // in order to let other decoders run
751 decode_input_cond_.notify_one();
754 void DecodeSignal::query_input_metadata()
756 // Update the samplerate and start time because we cannot start
757 // the libsrd session without the current samplerate
759 // TODO Currently we assume all channels have the same sample rate
761 bool samplerate_valid = false;
762 data::DecodeChannel *any_channel;
763 shared_ptr<Logic> logic_data;
766 any_channel = &(*find_if(channels_.begin(), channels_.end(),
767 [](data::DecodeChannel ch) { return ch.assigned_signal; }));
769 logic_data = any_channel->assigned_signal->logic_data();
772 // Wait until input data is available or an interrupt was requested
773 unique_lock<mutex> input_wait_lock(input_mutex_);
774 decode_input_cond_.wait(input_wait_lock);
776 } while (!logic_data && !decode_interrupt_);
778 if (decode_interrupt_)
782 if (!logic_data->logic_segments().empty()) {
783 shared_ptr<LogicSegment> first_segment =
784 any_channel->assigned_signal->logic_data()->logic_segments().front();
785 start_time_ = first_segment->start_time();
786 samplerate_ = first_segment->samplerate();
788 samplerate_valid = true;
791 if (!samplerate_valid) {
792 // Wait until input data is available or an interrupt was requested
793 unique_lock<mutex> input_wait_lock(input_mutex_);
794 decode_input_cond_.wait(input_wait_lock);
796 } while (!samplerate_valid && !decode_interrupt_);
799 void DecodeSignal::decode_data(
800 const int64_t abs_start_samplenum, const int64_t sample_count)
802 const int64_t unit_size = logic_mux_segment_->unit_size();
803 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
805 for (int64_t i = abs_start_samplenum;
806 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
807 i += chunk_sample_count) {
809 const int64_t chunk_end = min(i + chunk_sample_count,
810 abs_start_samplenum + sample_count);
812 int64_t data_size = (chunk_end - i) * unit_size;
813 uint8_t* chunk = new uint8_t[data_size];
814 logic_mux_segment_->get_samples(i, chunk_end, chunk);
816 if (srd_session_send(srd_session_, i, chunk_end, chunk,
817 data_size, unit_size) != SRD_OK) {
818 error_message_ = tr("Decoder reported an error");
826 lock_guard<mutex> lock(output_mutex_);
827 samples_decoded_ = chunk_end;
830 // Notify the frontend that we processed some data and
831 // possibly have new annotations as well
836 void DecodeSignal::decode_proc()
838 query_input_metadata();
840 if (decode_interrupt_)
845 uint64_t sample_count;
846 uint64_t abs_start_samplenum = 0;
848 // Keep processing new samples until we exhaust the input data
850 lock_guard<mutex> input_lock(input_mutex_);
851 sample_count = logic_mux_segment_->get_sample_count() - abs_start_samplenum;
853 if (sample_count > 0) {
854 decode_data(abs_start_samplenum, sample_count);
855 abs_start_samplenum += sample_count;
857 } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_);
859 if (error_message_.isEmpty() && !decode_interrupt_) {
860 if (sample_count == 0)
863 // Wait for new input data or an interrupt was requested
864 unique_lock<mutex> input_wait_lock(input_mutex_);
865 decode_input_cond_.wait(input_wait_lock);
867 } while (error_message_.isEmpty() && !decode_interrupt_);
870 void DecodeSignal::start_srd_session()
875 // Create the session
876 srd_session_new(&srd_session_);
877 assert(srd_session_);
879 // Create the decoders
880 srd_decoder_inst *prev_di = nullptr;
881 for (const shared_ptr<decode::Decoder> &dec : stack_) {
882 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
885 error_message_ = tr("Failed to create decoder instance");
886 srd_session_destroy(srd_session_);
891 srd_inst_stack(srd_session_, prev_di, di);
897 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
898 g_variant_new_uint64(samplerate_));
900 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
901 DecodeSignal::annotation_callback, this);
903 srd_session_start(srd_session_);
906 void DecodeSignal::stop_srd_session()
909 // Destroy the session
910 srd_session_destroy(srd_session_);
911 srd_session_ = nullptr;
915 void DecodeSignal::connect_input_notifiers()
917 // Disconnect the notification slot from the previous set of signals
918 disconnect(this, SLOT(on_data_cleared()));
919 disconnect(this, SLOT(on_data_received()));
921 // Connect the currently used signals to our slot
922 for (data::DecodeChannel &ch : channels_) {
923 if (!ch.assigned_signal)
926 const data::SignalBase *signal = ch.assigned_signal;
927 connect(signal, SIGNAL(samples_cleared()),
928 this, SLOT(on_data_cleared()));
929 connect(signal, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
930 this, SLOT(on_data_received()));
934 void DecodeSignal::prepare_annotation_segment()
936 // TODO Won't work for multiple segments
937 rows_.emplace_back(map<const decode::Row, decode::RowData>());
938 current_rows_ = &(rows_.back());
940 // Add annotation classes
941 for (const shared_ptr<decode::Decoder> &dec : stack_) {
943 const srd_decoder *const decc = dec->decoder();
944 assert(dec->decoder());
946 // Add a row for the decoder if it doesn't have a row list
947 if (!decc->annotation_rows)
948 (*current_rows_)[Row(decc)] = decode::RowData();
950 // Add the decoder rows
951 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
952 const srd_decoder_annotation_row *const ann_row =
953 (srd_decoder_annotation_row *)l->data;
956 const Row row(decc, ann_row);
958 // Add a new empty row data object
959 (*current_rows_)[row] = decode::RowData();
964 void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
967 assert(decode_signal);
969 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
972 lock_guard<mutex> lock(ds->output_mutex_);
976 assert(pdata->pdo->di);
977 const srd_decoder *const decc = pdata->pdo->di->decoder;
979 assert(ds->current_rows_);
981 const srd_proto_data_annotation *const pda =
982 (const srd_proto_data_annotation*)pdata->data;
985 auto row_iter = ds->current_rows_->end();
987 // Try looking up the sub-row of this class
988 const auto format = pda->ann_class;
989 const auto r = ds->class_rows_.find(make_pair(decc, format));
990 if (r != ds->class_rows_.end())
991 row_iter = ds->current_rows_->find((*r).second);
993 // Failing that, use the decoder as a key
994 row_iter = ds->current_rows_->find(Row(decc));
997 if (row_iter == ds->current_rows_->end()) {
998 qDebug() << "Unexpected annotation: decoder = " << decc <<
999 ", format = " << format;
1004 // Add the annotation
1005 (*row_iter).second.emplace_annotation(pdata);
1008 void DecodeSignal::on_capture_state_changed(int state)
1010 // If a new acquisition was started, we need to start decoding from scratch
1011 if (state == Session::Running)
1015 void DecodeSignal::on_data_cleared()
1020 void DecodeSignal::on_data_received()
1022 if (!logic_mux_thread_.joinable())
1025 logic_mux_cond_.notify_one();