2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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/>.
24 #include <libsigrokcxx/libsigrokcxx.hpp>
25 #include <libsigrokdecode/libsigrokdecode.h>
27 #include "decoder.hpp"
29 #include <pv/data/signalbase.hpp>
30 #include <pv/data/decodesignal.hpp>
39 AnnotationClass::AnnotationClass(size_t _id, char* _name, char* _description, Row* _row) :
42 description(_description),
48 bool AnnotationClass::visible() const
53 void AnnotationClass::set_visible(bool visible)
61 Decoder::Decoder(const srd_decoder *const dec, uint8_t stack_level) :
63 stack_level_(stack_level),
65 decoder_inst_(nullptr)
67 // Query the annotation output classes
69 for (GSList *l = dec->annotations; l; l = l->next) {
70 char **ann_class = (char**)l->data;
71 char *name = ann_class[0];
72 char *desc = ann_class[1];
73 ann_classes_.emplace_back(i++, name, desc, nullptr);
75 connect(&(ann_classes_.back()), SIGNAL(visibility_changed()),
76 this, SLOT(on_class_visibility_changed()));
79 // Query the binary output classes
81 for (GSList *l = dec->binary; l; l = l->next) {
82 char **bin_class = (char**)l->data;
83 char *name = bin_class[0];
84 char *desc = bin_class[1];
85 bin_classes_.push_back({i++, name, desc});
88 // Query the annotation rows and reference them by the classes that use them
89 uint32_t row_count = 0;
90 for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next)
94 for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next) {
95 const srd_decoder_annotation_row *const srd_row = (srd_decoder_annotation_row *)rl->data;
97 rows_.emplace_back(i++, this, srd_row);
99 // FIXME PV can crash from .at() if a PD's ann classes are defined incorrectly
100 for (const GSList *cl = srd_row->ann_classes; cl; cl = cl->next)
101 ann_classes_.at((size_t)cl->data).row = &(rows_.back());
103 connect(&(rows_.back()), SIGNAL(visibility_changed()),
104 this, SLOT(on_row_visibility_changed()));
108 // Make sure there is a row for PDs without row declarations
109 rows_.emplace_back(0, this);
111 for (AnnotationClass& c : ann_classes_)
112 c.row = &(rows_.back());
118 for (auto& option : options_)
119 g_variant_unref(option.second);
122 const srd_decoder* Decoder::get_srd_decoder() const
127 uint8_t Decoder::get_stack_level() const
132 const char* Decoder::name() const
134 return srd_decoder_->name;
137 bool Decoder::visible() const
142 void Decoder::set_visible(bool visible)
146 annotation_visibility_changed();
149 const vector<DecodeChannel*>& Decoder::channels() const
154 void Decoder::set_channels(vector<DecodeChannel*> channels)
156 channels_ = channels;
159 const map<string, GVariant*>& Decoder::options() const
164 void Decoder::set_option(const char *id, GVariant *value)
167 g_variant_ref(value);
168 options_[id] = value;
170 // If we have a decoder instance, apply option value immediately
174 void Decoder::apply_all_options()
177 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
178 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
180 for (const auto& option : options_) {
181 GVariant *const value = option.second;
182 g_variant_ref(value);
183 g_hash_table_replace(opt_hash, (void*)g_strdup(
184 option.first.c_str()), value);
187 srd_inst_option_set(decoder_inst_, opt_hash);
188 g_hash_table_destroy(opt_hash);
192 bool Decoder::have_required_channels() const
194 for (DecodeChannel *ch : channels_)
195 if (!ch->assigned_signal && !ch->is_optional)
201 srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
203 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
204 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
206 for (const auto& option : options_) {
207 GVariant *const value = option.second;
208 g_variant_ref(value);
209 g_hash_table_replace(opt_hash, (void*)g_strdup(
210 option.first.c_str()), value);
214 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
216 decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash);
217 g_hash_table_destroy(opt_hash);
222 // Setup the channels
223 GArray *const init_pin_states = g_array_sized_new(false, true,
224 sizeof(uint8_t), channels_.size());
226 g_array_set_size(init_pin_states, channels_.size());
228 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
229 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
231 for (DecodeChannel *ch : channels_) {
232 if (!ch->assigned_signal)
235 init_pin_states->data[ch->id] = ch->initial_pin_state;
237 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
238 g_variant_ref_sink(gvar);
239 // key is channel name (pdch->id), value is bit position in each sample (gvar)
240 g_hash_table_insert(channels, ch->pdch_->id, gvar);
243 srd_inst_channel_set_all(decoder_inst_, channels);
245 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
246 g_array_free(init_pin_states, true);
248 return decoder_inst_;
251 void Decoder::invalidate_decoder_inst()
253 decoder_inst_ = nullptr;
256 vector<Row*> Decoder::get_rows()
260 for (Row& row : rows_)
261 result.push_back(&row);
266 Row* Decoder::get_row_by_id(size_t id)
268 if (id > rows_.size())
274 vector<const AnnotationClass*> Decoder::ann_classes() const
276 vector<const AnnotationClass*> result;
278 for (const AnnotationClass& c : ann_classes_)
279 result.push_back(&c);
284 vector<AnnotationClass*> Decoder::ann_classes()
286 vector<AnnotationClass*> result;
288 for (AnnotationClass& c : ann_classes_)
289 result.push_back(&c);
294 AnnotationClass* Decoder::get_ann_class_by_id(size_t id)
296 if (id >= ann_classes_.size())
299 return &(ann_classes_[id]);
302 const AnnotationClass* Decoder::get_ann_class_by_id(size_t id) const
304 if (id >= ann_classes_.size())
307 return &(ann_classes_[id]);
310 uint32_t Decoder::get_binary_class_count() const
312 return bin_classes_.size();
315 const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
317 return &(bin_classes_.at(id));
320 void Decoder::on_row_visibility_changed()
322 annotation_visibility_changed();
325 void Decoder::on_class_visibility_changed()
327 annotation_visibility_changed();
330 bool Decoder::has_logic_output() const
332 return (srd_decoder_->logic_output_channels != nullptr);
335 const vector<DecoderLogicOutputChannel> Decoder::logic_output_channels() const
337 vector<DecoderLogicOutputChannel> result;
339 for (GSList *l = srd_decoder_->logic_output_channels; l; l = l->next) {
340 const srd_decoder_logic_output_channel* ch_data =
341 (srd_decoder_logic_output_channel*)l->data;
343 result.emplace_back(QString::fromUtf8(ch_data->id),
344 QString::fromUtf8(ch_data->desc));
350 } // namespace decode