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) :
42 decoder_inst_(nullptr)
44 // Query the decoder outputs
46 for (GSList *l = dec->binary; l; l = l->next) {
47 char **bin_class = (char**)l->data;
48 char *name = bin_class[0];
49 char *desc = bin_class[1];
50 bin_classes_.push_back({i++, name, desc});
56 for (auto& option : options_)
57 g_variant_unref(option.second);
60 const srd_decoder* Decoder::decoder() const
65 const char* Decoder::name() const
67 return decoder_->name;
70 bool Decoder::shown() const
75 void Decoder::show(bool show)
80 const vector<DecodeChannel*>& Decoder::channels() const
85 void Decoder::set_channels(vector<DecodeChannel*> channels)
90 const map<string, GVariant*>& Decoder::options() const
95 void Decoder::set_option(const char *id, GVariant *value)
101 // If we have a decoder instance, apply option value immediately
105 void Decoder::apply_all_options()
108 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
109 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
111 for (const auto& option : options_) {
112 GVariant *const value = option.second;
113 g_variant_ref(value);
114 g_hash_table_replace(opt_hash, (void*)g_strdup(
115 option.first.c_str()), value);
118 srd_inst_option_set(decoder_inst_, opt_hash);
119 g_hash_table_destroy(opt_hash);
123 bool Decoder::have_required_channels() const
125 for (DecodeChannel *ch : channels_)
126 if (!ch->assigned_signal && !ch->is_optional)
132 srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
134 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
135 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
137 for (const auto& option : options_) {
138 GVariant *const value = option.second;
139 g_variant_ref(value);
140 g_hash_table_replace(opt_hash, (void*)g_strdup(
141 option.first.c_str()), value);
145 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
147 decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash);
148 g_hash_table_destroy(opt_hash);
153 // Setup the channels
154 GArray *const init_pin_states = g_array_sized_new(false, true,
155 sizeof(uint8_t), channels_.size());
157 g_array_set_size(init_pin_states, channels_.size());
159 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
160 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
162 for (DecodeChannel *ch : channels_) {
163 if (!ch->assigned_signal)
166 init_pin_states->data[ch->id] = ch->initial_pin_state;
168 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
169 g_variant_ref_sink(gvar);
170 // key is channel name (pdch->id), value is bit position in each sample (gvar)
171 g_hash_table_insert(channels, ch->pdch_->id, gvar);
174 srd_inst_channel_set_all(decoder_inst_, channels);
176 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
177 g_array_free(init_pin_states, true);
179 return decoder_inst_;
182 void Decoder::invalidate_decoder_inst()
184 decoder_inst_ = nullptr;
187 uint32_t Decoder::get_binary_class_count() const
189 return bin_classes_.size();
192 const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
194 return &(bin_classes_.at(id));
197 } // namespace decode