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::out_of_range;
40 using std::shared_ptr;
41 using std::unique_lock;
42 using pv::data::decode::Annotation;
43 using pv::data::decode::Decoder;
44 using pv::data::decode::Row;
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),
62 frame_complete_(false)
64 connect(&session_, SIGNAL(capture_state_changed(int)),
65 this, SLOT(on_capture_state_changed(int)));
68 DecodeSignal::~DecodeSignal()
73 const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
78 void DecodeSignal::stack_decoder(const srd_decoder *decoder)
81 const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder);
83 stack_.push_back(dec);
85 // Set name if this decoder is the first in the list
86 if (stack_.size() == 1)
87 set_name(QString::fromUtf8(decoder->name));
89 // Include the newly created decode channels in the channel lists
90 update_channel_list();
92 auto_assign_signals(dec);
93 commit_decoder_channels();
97 void DecodeSignal::remove_decoder(int index)
100 assert(index < (int)stack_.size());
102 // Find the decoder in the stack
103 auto iter = stack_.begin();
104 for (int i = 0; i < index; i++, iter++)
105 assert(iter != stack_.end());
107 // Delete the element
110 // Update channels and decoded data
111 update_channel_list();
115 bool DecodeSignal::toggle_decoder_visibility(int index)
117 auto iter = stack_.cbegin();
118 for (int i = 0; i < index; i++, iter++)
119 assert(iter != stack_.end());
121 shared_ptr<Decoder> dec = *iter;
123 // Toggle decoder visibility
126 state = !dec->shown();
133 void DecodeSignal::reset_decode()
135 if (decode_thread_.joinable()) {
136 decode_interrupt_ = true;
137 decode_input_cond_.notify_one();
138 decode_thread_.join();
141 if (logic_mux_thread_.joinable()) {
142 logic_mux_interrupt_ = true;
143 logic_mux_cond_.notify_one();
144 logic_mux_thread_.join();
149 frame_complete_ = false;
150 samples_decoded_ = 0;
151 currently_processed_segment_ = 0;
152 error_message_ = QString();
154 segmented_rows_.clear();
155 current_rows_= nullptr;
158 logic_mux_data_.reset();
159 logic_mux_data_invalid_ = true;
164 void DecodeSignal::begin_decode()
166 if (decode_thread_.joinable()) {
167 decode_interrupt_ = true;
168 decode_input_cond_.notify_one();
169 decode_thread_.join();
172 if (logic_mux_thread_.joinable()) {
173 logic_mux_interrupt_ = true;
174 logic_mux_cond_.notify_one();
175 logic_mux_thread_.join();
180 if (stack_.size() == 0) {
181 error_message_ = tr("No decoders");
185 assert(channels_.size() > 0);
187 if (get_assigned_signal_count() == 0) {
188 error_message_ = tr("There are no channels assigned to this decoder");
192 // Make sure that all assigned channels still provide logic data
193 // (can happen when a converted signal was assigned but the
194 // conversion removed in the meanwhile)
195 for (data::DecodeChannel &ch : channels_)
196 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
197 ch.assigned_signal = nullptr;
199 // Check that all decoders have the required channels
200 for (const shared_ptr<decode::Decoder> &dec : stack_)
201 if (!dec->have_required_channels()) {
202 error_message_ = tr("One or more required channels "
203 "have not been specified");
207 // Map out all the annotation classes
208 for (const shared_ptr<decode::Decoder> &dec : stack_) {
210 const srd_decoder *const decc = dec->decoder();
211 assert(dec->decoder());
213 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
214 const srd_decoder_annotation_row *const ann_row =
215 (srd_decoder_annotation_row *)l->data;
218 const Row row(decc, ann_row);
220 for (const GSList *ll = ann_row->ann_classes;
222 class_rows_[make_pair(decc,
223 GPOINTER_TO_INT(ll->data))] = row;
227 create_new_annotation_segment();
229 // TODO Allow logic_mux_data and logic_mux_segment to work with multiple segments
231 // Free the logic data and its segment(s) if it needs to be updated
232 if (logic_mux_data_invalid_)
233 logic_mux_data_.reset();
235 if (!logic_mux_data_) {
236 const int64_t ch_count = get_assigned_signal_count();
237 const int64_t unit_size = (ch_count + 7) / 8;
238 logic_mux_data_ = make_shared<Logic>(ch_count);
239 logic_mux_segment_ = make_shared<LogicSegment>(*logic_mux_data_, unit_size, samplerate_);
240 logic_mux_data_->push_segment(logic_mux_segment_);
243 // Make sure the logic output data is complete and up-to-date
244 logic_mux_interrupt_ = false;
245 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
247 // Decode the muxed logic data
248 decode_interrupt_ = false;
249 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
251 // Receive notifications when new sample data is available
252 connect_input_notifiers();
255 QString DecodeSignal::error_message() const
257 lock_guard<mutex> lock(output_mutex_);
258 return error_message_;
261 const vector<data::DecodeChannel> DecodeSignal::get_channels() const
266 void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
268 bool new_assignment = false;
270 // Try to auto-select channels that don't have signals assigned yet
271 for (data::DecodeChannel &ch : channels_) {
272 // If a decoder is given, auto-assign only its channels
273 if (dec && (ch.decoder_ != dec))
276 if (ch.assigned_signal)
279 for (shared_ptr<data::SignalBase> s : session_.signalbases()) {
280 const QString ch_name = ch.name.toLower();
281 const QString s_name = s->name().toLower();
283 if (s->logic_data() &&
284 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
285 ch.assigned_signal = s.get();
286 new_assignment = true;
291 if (new_assignment) {
292 logic_mux_data_invalid_ = true;
293 commit_decoder_channels();
298 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
300 for (data::DecodeChannel &ch : channels_)
301 if (ch.id == channel_id) {
302 ch.assigned_signal = signal;
303 logic_mux_data_invalid_ = true;
306 commit_decoder_channels();
311 int DecodeSignal::get_assigned_signal_count() const
313 // Count all channels that have a signal assigned to them
314 return count_if(channels_.begin(), channels_.end(),
315 [](data::DecodeChannel ch) { return ch.assigned_signal; });
318 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
320 for (data::DecodeChannel &ch : channels_)
321 if (ch.id == channel_id)
322 ch.initial_pin_state = init_state;
329 double DecodeSignal::samplerate() const
334 const pv::util::Timestamp& DecodeSignal::start_time() const
339 int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const
341 // The working sample count is the highest sample number for
342 // which all used signals have data available, so go through
343 // all channels and use the lowest overall sample count of the
346 int64_t count = std::numeric_limits<int64_t>::max();
347 bool no_signals_assigned = true;
349 for (const data::DecodeChannel &ch : channels_)
350 if (ch.assigned_signal) {
351 no_signals_assigned = false;
353 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
354 if (!logic_data || logic_data->logic_segments().empty())
358 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id);
359 count = min(count, (int64_t)segment->get_sample_count());
360 } catch (out_of_range) {
365 return (no_signals_assigned ? 0 : count);
368 int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id) const
370 lock_guard<mutex> decode_lock(output_mutex_);
374 if (segment_id == currently_processed_segment_)
375 result = samples_decoded_;
377 if (segment_id < currently_processed_segment_)
378 // Segment was already decoded fully
379 result = get_working_sample_count(segment_id);
381 // Segment wasn't decoded at all yet
387 vector<Row> DecodeSignal::visible_rows() const
389 lock_guard<mutex> lock(output_mutex_);
393 for (const shared_ptr<decode::Decoder> &dec : stack_) {
398 const srd_decoder *const decc = dec->decoder();
399 assert(dec->decoder());
401 // Add a row for the decoder if it doesn't have a row list
402 if (!decc->annotation_rows)
403 rows.emplace_back(decc);
405 // Add the decoder rows
406 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
407 const srd_decoder_annotation_row *const ann_row =
408 (srd_decoder_annotation_row *)l->data;
410 rows.emplace_back(decc, ann_row);
417 void DecodeSignal::get_annotation_subset(
418 vector<pv::data::decode::Annotation> &dest,
419 const decode::Row &row, uint64_t start_sample,
420 uint64_t end_sample) const
422 lock_guard<mutex> lock(output_mutex_);
427 // TODO Instead of current_rows_, use segmented_rows_ and the ID of the segment
429 const auto iter = current_rows_->find(row);
430 if (iter != current_rows_->end())
431 (*iter).second.get_annotation_subset(dest,
432 start_sample, end_sample);
435 void DecodeSignal::save_settings(QSettings &settings) const
437 SignalBase::save_settings(settings);
439 settings.setValue("decoders", (int)(stack_.size()));
441 // Save decoder stack
443 for (shared_ptr<decode::Decoder> decoder : stack_) {
444 settings.beginGroup("decoder" + QString::number(decoder_idx++));
446 settings.setValue("id", decoder->decoder()->id);
448 // Save decoder options
449 const map<string, GVariant*>& options = decoder->options();
451 settings.setValue("options", (int)options.size());
453 // Note: decode::Decoder::options() returns only the options
454 // that differ from the default. See binding::Decoder::getter()
456 for (auto option : options) {
457 settings.beginGroup("option" + QString::number(i));
458 settings.setValue("name", QString::fromStdString(option.first));
459 GlobalSettings::store_gvariant(settings, option.second);
467 // Save channel mapping
468 settings.setValue("channels", (int)channels_.size());
470 for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
471 auto channel = find_if(channels_.begin(), channels_.end(),
472 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
474 if (channel == channels_.end()) {
475 qDebug() << "ERROR: Gap in channel index:" << channel_id;
479 settings.beginGroup("channel" + QString::number(channel_id));
481 settings.setValue("name", channel->name); // Useful for debugging
482 settings.setValue("initial_pin_state", channel->initial_pin_state);
484 if (channel->assigned_signal)
485 settings.setValue("assigned_signal_name", channel->assigned_signal->name());
491 void DecodeSignal::restore_settings(QSettings &settings)
493 SignalBase::restore_settings(settings);
495 // Restore decoder stack
496 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
498 int decoders = settings.value("decoders").toInt();
500 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
501 settings.beginGroup("decoder" + QString::number(decoder_idx));
503 QString id = settings.value("id").toString();
505 for (GSList *entry = dec_list; entry; entry = entry->next) {
506 const srd_decoder *dec = (srd_decoder*)entry->data;
510 if (QString::fromUtf8(dec->id) == id) {
511 shared_ptr<decode::Decoder> decoder =
512 make_shared<decode::Decoder>(dec);
514 stack_.push_back(decoder);
516 // Restore decoder options that differ from their default
517 int options = settings.value("options").toInt();
519 for (int i = 0; i < options; i++) {
520 settings.beginGroup("option" + QString::number(i));
521 QString name = settings.value("name").toString();
522 GVariant *value = GlobalSettings::restore_gvariant(settings);
523 decoder->set_option(name.toUtf8(), value);
527 // Include the newly created decode channels in the channel lists
528 update_channel_list();
537 // Restore channel mapping
538 unsigned int channels = settings.value("channels").toInt();
540 const unordered_set< shared_ptr<data::SignalBase> > signalbases =
541 session_.signalbases();
543 for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
544 auto channel = find_if(channels_.begin(), channels_.end(),
545 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
547 if (channel == channels_.end()) {
548 qDebug() << "ERROR: Non-existant channel index:" << channel_id;
552 settings.beginGroup("channel" + QString::number(channel_id));
554 QString assigned_signal_name = settings.value("assigned_signal_name").toString();
556 for (shared_ptr<data::SignalBase> signal : signalbases)
557 if (signal->name() == assigned_signal_name)
558 channel->assigned_signal = signal.get();
560 channel->initial_pin_state = settings.value("initial_pin_state").toInt();
565 // Update the internal structures
566 update_channel_list();
567 commit_decoder_channels();
572 void DecodeSignal::update_channel_list()
574 vector<data::DecodeChannel> prev_channels = channels_;
579 // Copy existing entries, create new as needed
580 for (shared_ptr<Decoder> decoder : stack_) {
581 const srd_decoder* srd_d = decoder->decoder();
584 // Mandatory channels
585 for (l = srd_d->channels; l; l = l->next) {
586 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
587 bool ch_added = false;
589 // Copy but update ID if this channel was in the list before
590 for (data::DecodeChannel &ch : prev_channels)
591 if (ch.pdch_ == pdch) {
593 channels_.push_back(ch);
599 // Create new entry without a mapped signal
600 data::DecodeChannel ch = {id++, 0, false, nullptr,
601 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
602 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
603 channels_.push_back(ch);
608 for (l = srd_d->opt_channels; l; l = l->next) {
609 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
610 bool ch_added = false;
612 // Copy but update ID if this channel was in the list before
613 for (data::DecodeChannel &ch : prev_channels)
614 if (ch.pdch_ == pdch) {
616 channels_.push_back(ch);
622 // Create new entry without a mapped signal
623 data::DecodeChannel ch = {id++, 0, true, nullptr,
624 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
625 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
626 channels_.push_back(ch);
631 // Invalidate the logic output data if the channel assignment changed
632 if (prev_channels.size() != channels_.size()) {
633 // The number of channels changed, there's definitely a difference
634 logic_mux_data_invalid_ = true;
636 // Same number but assignment may still differ, so compare all channels
637 for (size_t i = 0; i < channels_.size(); i++) {
638 const data::DecodeChannel &p_ch = prev_channels[i];
639 const data::DecodeChannel &ch = channels_[i];
641 if ((p_ch.pdch_ != ch.pdch_) ||
642 (p_ch.assigned_signal != ch.assigned_signal)) {
643 logic_mux_data_invalid_ = true;
653 void DecodeSignal::commit_decoder_channels()
655 // Submit channel list to every decoder, containing only the relevant channels
656 for (shared_ptr<decode::Decoder> dec : stack_) {
657 vector<data::DecodeChannel*> channel_list;
659 for (data::DecodeChannel &ch : channels_)
660 if (ch.decoder_ == dec)
661 channel_list.push_back(&ch);
663 dec->set_channels(channel_list);
666 // Channel bit IDs must be in sync with the channel's apperance in channels_
668 for (data::DecodeChannel &ch : channels_)
669 if (ch.assigned_signal)
673 void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end)
675 // Enforce end to be greater than start
679 // Fetch all segments and their data
680 // TODO Currently, we assume only a single segment exists
681 vector<shared_ptr<LogicSegment> > segments;
682 vector<const uint8_t*> signal_data;
683 vector<uint8_t> signal_in_bytepos;
684 vector<uint8_t> signal_in_bitpos;
686 for (data::DecodeChannel &ch : channels_)
687 if (ch.assigned_signal) {
688 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
689 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
690 segments.push_back(segment);
692 uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
693 segment->get_samples(start, end, data);
694 signal_data.push_back(data);
696 const int bitpos = ch.assigned_signal->logic_bit_index();
697 signal_in_bytepos.push_back(bitpos / 8);
698 signal_in_bitpos.push_back(bitpos % 8);
701 // Perform the muxing of signal data into the output data
702 uint8_t* output = new uint8_t[(end - start) * logic_mux_segment_->unit_size()];
703 unsigned int signal_count = signal_data.size();
705 for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) {
709 const int out_sample_pos = sample_cnt * logic_mux_segment_->unit_size();
710 for (unsigned int i = 0; i < logic_mux_segment_->unit_size(); i++)
711 output[out_sample_pos + i] = 0;
713 for (unsigned int i = 0; i < signal_count; i++) {
714 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
715 const uint8_t in_sample = 1 &
716 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
718 const uint8_t out_sample = output[out_sample_pos + bytepos];
720 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
730 logic_mux_segment_->append_payload(output, (end - start) * logic_mux_segment_->unit_size());
733 for (const uint8_t* data : signal_data)
737 void DecodeSignal::logic_mux_proc()
740 const uint64_t input_sample_count = get_working_sample_count(currently_processed_segment_);
741 const uint64_t output_sample_count = logic_mux_segment_->get_sample_count();
743 const uint64_t samples_to_process =
744 (input_sample_count > output_sample_count) ?
745 (input_sample_count - output_sample_count) : 0;
747 // Process the samples if necessary...
748 if (samples_to_process > 0) {
749 const uint64_t unit_size = logic_mux_segment_->unit_size();
750 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
752 uint64_t processed_samples = 0;
754 const uint64_t start_sample = output_sample_count + processed_samples;
755 const uint64_t sample_count =
756 min(samples_to_process - processed_samples, chunk_sample_count);
758 mux_logic_samples(start_sample, start_sample + sample_count);
759 processed_samples += sample_count;
761 // ...and process the newly muxed logic data
762 decode_input_cond_.notify_one();
763 } while (processed_samples < samples_to_process);
766 if (samples_to_process == 0) {
767 // Wait for more input
768 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
769 logic_mux_cond_.wait(logic_mux_lock);
771 } while (!logic_mux_interrupt_);
774 void DecodeSignal::query_input_metadata()
776 // Update the samplerate and start time because we cannot start
777 // the libsrd session without the current samplerate
779 // TODO Currently we assume all channels have the same sample rate
781 bool samplerate_valid = false;
782 data::DecodeChannel *any_channel;
783 shared_ptr<Logic> logic_data;
786 any_channel = &(*find_if(channels_.begin(), channels_.end(),
787 [](data::DecodeChannel ch) { return ch.assigned_signal; }));
789 logic_data = any_channel->assigned_signal->logic_data();
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 (!logic_data && !decode_interrupt_);
798 if (decode_interrupt_)
802 if (!logic_data->logic_segments().empty()) {
803 shared_ptr<LogicSegment> first_segment =
804 any_channel->assigned_signal->logic_data()->logic_segments().front();
805 start_time_ = first_segment->start_time();
806 samplerate_ = first_segment->samplerate();
808 samplerate_valid = true;
811 if (!samplerate_valid) {
812 // Wait until input data is available or an interrupt was requested
813 unique_lock<mutex> input_wait_lock(input_mutex_);
814 decode_input_cond_.wait(input_wait_lock);
816 } while (!samplerate_valid && !decode_interrupt_);
819 void DecodeSignal::decode_data(
820 const int64_t abs_start_samplenum, const int64_t sample_count)
822 const int64_t unit_size = logic_mux_segment_->unit_size();
823 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
825 for (int64_t i = abs_start_samplenum;
826 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
827 i += chunk_sample_count) {
829 const int64_t chunk_end = min(i + chunk_sample_count,
830 abs_start_samplenum + sample_count);
832 int64_t data_size = (chunk_end - i) * unit_size;
833 uint8_t* chunk = new uint8_t[data_size];
834 logic_mux_segment_->get_samples(i, chunk_end, chunk);
836 if (srd_session_send(srd_session_, i, chunk_end, chunk,
837 data_size, unit_size) != SRD_OK) {
838 error_message_ = tr("Decoder reported an error");
846 lock_guard<mutex> lock(output_mutex_);
847 samples_decoded_ = chunk_end;
850 // Notify the frontend that we processed some data and
851 // possibly have new annotations as well
856 void DecodeSignal::decode_proc()
858 query_input_metadata();
860 if (decode_interrupt_)
865 uint64_t sample_count;
866 uint64_t abs_start_samplenum = 0;
868 // Keep processing new samples until we exhaust the input data
870 lock_guard<mutex> input_lock(input_mutex_);
871 sample_count = logic_mux_segment_->get_sample_count() - abs_start_samplenum;
873 if (sample_count > 0) {
874 decode_data(abs_start_samplenum, sample_count);
875 abs_start_samplenum += sample_count;
877 } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_);
879 if (error_message_.isEmpty() && !decode_interrupt_) {
880 if (sample_count == 0)
883 // Wait for new input data or an interrupt was requested
884 unique_lock<mutex> input_wait_lock(input_mutex_);
885 decode_input_cond_.wait(input_wait_lock);
887 } while (error_message_.isEmpty() && !decode_interrupt_);
890 void DecodeSignal::start_srd_session()
895 // Create the session
896 srd_session_new(&srd_session_);
897 assert(srd_session_);
899 // Create the decoders
900 srd_decoder_inst *prev_di = nullptr;
901 for (const shared_ptr<decode::Decoder> &dec : stack_) {
902 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
905 error_message_ = tr("Failed to create decoder instance");
906 srd_session_destroy(srd_session_);
911 srd_inst_stack(srd_session_, prev_di, di);
917 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
918 g_variant_new_uint64(samplerate_));
920 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
921 DecodeSignal::annotation_callback, this);
923 srd_session_start(srd_session_);
926 void DecodeSignal::stop_srd_session()
929 // Destroy the session
930 srd_session_destroy(srd_session_);
931 srd_session_ = nullptr;
935 void DecodeSignal::connect_input_notifiers()
937 // Disconnect the notification slot from the previous set of signals
938 disconnect(this, SLOT(on_data_cleared()));
939 disconnect(this, SLOT(on_data_received()));
941 // Connect the currently used signals to our slot
942 for (data::DecodeChannel &ch : channels_) {
943 if (!ch.assigned_signal)
946 const data::SignalBase *signal = ch.assigned_signal;
947 connect(signal, SIGNAL(samples_cleared()),
948 this, SLOT(on_data_cleared()));
949 connect(signal, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
950 this, SLOT(on_data_received()));
954 void DecodeSignal::create_new_annotation_segment()
956 segmented_rows_.emplace_back(map<const decode::Row, decode::RowData>());
957 current_rows_ = &(segmented_rows_.back());
959 // Add annotation classes
960 for (const shared_ptr<decode::Decoder> &dec : stack_) {
962 const srd_decoder *const decc = dec->decoder();
963 assert(dec->decoder());
965 // Add a row for the decoder if it doesn't have a row list
966 if (!decc->annotation_rows)
967 (*current_rows_)[Row(decc)] = decode::RowData();
969 // Add the decoder rows
970 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
971 const srd_decoder_annotation_row *const ann_row =
972 (srd_decoder_annotation_row *)l->data;
975 const Row row(decc, ann_row);
977 // Add a new empty row data object
978 (*current_rows_)[row] = decode::RowData();
983 void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
986 assert(decode_signal);
988 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
991 lock_guard<mutex> lock(ds->output_mutex_);
995 assert(pdata->pdo->di);
996 const srd_decoder *const decc = pdata->pdo->di->decoder;
998 assert(ds->current_rows_);
1000 const srd_proto_data_annotation *const pda =
1001 (const srd_proto_data_annotation*)pdata->data;
1004 auto row_iter = ds->current_rows_->end();
1006 // Try looking up the sub-row of this class
1007 const auto format = pda->ann_class;
1008 const auto r = ds->class_rows_.find(make_pair(decc, format));
1009 if (r != ds->class_rows_.end())
1010 row_iter = ds->current_rows_->find((*r).second);
1012 // Failing that, use the decoder as a key
1013 row_iter = ds->current_rows_->find(Row(decc));
1016 if (row_iter == ds->current_rows_->end()) {
1017 qDebug() << "Unexpected annotation: decoder = " << decc <<
1018 ", format = " << format;
1023 // Add the annotation
1024 (*row_iter).second.emplace_annotation(pdata);
1027 void DecodeSignal::on_capture_state_changed(int state)
1029 // If a new acquisition was started, we need to start decoding from scratch
1030 if (state == Session::Running)
1034 void DecodeSignal::on_data_cleared()
1039 void DecodeSignal::on_data_received()
1041 if (!logic_mux_thread_.joinable())
1044 logic_mux_cond_.notify_one();