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 Decoder::Decoder(const srd_decoder *const dec, uint8_t stack_level) :
41 stack_level_(stack_level),
43 decoder_inst_(nullptr)
45 // Query the annotation output classes
47 for (GSList *l = dec->annotations; l; l = l->next) {
48 char **ann_class = (char**)l->data;
49 char *name = ann_class[0];
50 char *desc = ann_class[1];
51 ann_classes_.push_back({i++, name, desc, nullptr, true}); // Visible by default
54 // Query the binary output classes
56 for (GSList *l = dec->binary; l; l = l->next) {
57 char **bin_class = (char**)l->data;
58 char *name = bin_class[0];
59 char *desc = bin_class[1];
60 bin_classes_.push_back({i++, name, desc});
63 // Query the annotation rows and reference them by the classes that use them
64 uint32_t row_count = 0;
65 for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next)
67 rows_.reserve(row_count);
70 for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next) {
71 const srd_decoder_annotation_row *const srd_row = (srd_decoder_annotation_row *)rl->data;
73 rows_.emplace_back(i++, this, srd_row);
75 // FIXME PV can crash from .at() if a PD's ann classes are defined incorrectly
76 for (const GSList *cl = srd_row->ann_classes; cl; cl = cl->next)
77 ann_classes_.at((size_t)cl->data).row = &(rows_.back());
81 // Make sure there is a row for PDs without row declarations
82 rows_.emplace_back(0, this);
84 for (AnnotationClass& c : ann_classes_)
85 c.row = &(rows_.back());
91 for (auto& option : options_)
92 g_variant_unref(option.second);
95 const srd_decoder* Decoder::get_srd_decoder() const
100 uint8_t Decoder::get_stack_level() const
105 const char* Decoder::name() const
107 return srd_decoder_->name;
110 bool Decoder::visible() const
115 void Decoder::set_visible(bool visible)
120 const vector<DecodeChannel*>& Decoder::channels() const
125 void Decoder::set_channels(vector<DecodeChannel*> channels)
127 channels_ = channels;
130 const map<string, GVariant*>& Decoder::options() const
135 void Decoder::set_option(const char *id, GVariant *value)
138 g_variant_ref(value);
139 options_[id] = value;
141 // If we have a decoder instance, apply option value immediately
145 void Decoder::apply_all_options()
148 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
149 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
151 for (const auto& option : options_) {
152 GVariant *const value = option.second;
153 g_variant_ref(value);
154 g_hash_table_replace(opt_hash, (void*)g_strdup(
155 option.first.c_str()), value);
158 srd_inst_option_set(decoder_inst_, opt_hash);
159 g_hash_table_destroy(opt_hash);
163 bool Decoder::have_required_channels() const
165 for (DecodeChannel *ch : channels_)
166 if (!ch->assigned_signal && !ch->is_optional)
172 srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
174 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
175 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
177 for (const auto& option : options_) {
178 GVariant *const value = option.second;
179 g_variant_ref(value);
180 g_hash_table_replace(opt_hash, (void*)g_strdup(
181 option.first.c_str()), value);
185 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
187 decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash);
188 g_hash_table_destroy(opt_hash);
193 // Setup the channels
194 GArray *const init_pin_states = g_array_sized_new(false, true,
195 sizeof(uint8_t), channels_.size());
197 g_array_set_size(init_pin_states, channels_.size());
199 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
200 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
202 for (DecodeChannel *ch : channels_) {
203 if (!ch->assigned_signal)
206 init_pin_states->data[ch->id] = ch->initial_pin_state;
208 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
209 g_variant_ref_sink(gvar);
210 // key is channel name (pdch->id), value is bit position in each sample (gvar)
211 g_hash_table_insert(channels, ch->pdch_->id, gvar);
214 srd_inst_channel_set_all(decoder_inst_, channels);
216 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
217 g_array_free(init_pin_states, true);
219 return decoder_inst_;
222 void Decoder::invalidate_decoder_inst()
224 decoder_inst_ = nullptr;
227 vector<Row*> Decoder::get_rows()
231 for (Row& row : rows_)
232 result.push_back(&row);
237 Row* Decoder::get_row_by_id(size_t id)
239 if (id > rows_.size())
245 vector<const AnnotationClass*> Decoder::ann_classes() const
247 vector<const AnnotationClass*> result;
249 for (const AnnotationClass& c : ann_classes_)
250 result.push_back(&c);
255 vector<AnnotationClass*> Decoder::ann_classes()
257 vector<AnnotationClass*> result;
259 for (AnnotationClass& c : ann_classes_)
260 result.push_back(&c);
265 AnnotationClass* Decoder::get_ann_class_by_id(size_t id)
267 if (id >= ann_classes_.size())
270 return &(ann_classes_[id]);
273 const AnnotationClass* Decoder::get_ann_class_by_id(size_t id) const
275 if (id >= ann_classes_.size())
278 return &(ann_classes_[id]);
281 uint32_t Decoder::get_binary_class_count() const
283 return bin_classes_.size();
286 const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
288 return &(bin_classes_.at(id));
291 } // namespace decode