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)
48 for (auto& option : options_)
49 g_variant_unref(option.second);
52 const srd_decoder* Decoder::decoder() const
57 bool Decoder::shown() const
62 void Decoder::show(bool show)
67 const vector<DecodeChannel*>& Decoder::channels() const
72 void Decoder::set_channels(vector<DecodeChannel*> channels)
77 const map<string, GVariant*>& Decoder::options() const
82 void Decoder::set_option(const char *id, GVariant *value)
88 // If we have a decoder instance, apply option value immediately
92 void Decoder::apply_all_options()
95 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
96 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
98 for (const auto& option : options_) {
99 GVariant *const value = option.second;
100 g_variant_ref(value);
101 g_hash_table_replace(opt_hash, (void*)g_strdup(
102 option.first.c_str()), value);
105 srd_inst_option_set(decoder_inst_, opt_hash);
106 g_hash_table_destroy(opt_hash);
110 bool Decoder::have_required_channels() const
112 for (DecodeChannel *ch : channels_)
113 if (!ch->assigned_signal && !ch->is_optional)
119 srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
121 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
122 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
124 for (const auto& option : options_) {
125 GVariant *const value = option.second;
126 g_variant_ref(value);
127 g_hash_table_replace(opt_hash, (void*)g_strdup(
128 option.first.c_str()), value);
132 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
134 decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash);
135 g_hash_table_destroy(opt_hash);
140 // Setup the channels
141 GArray *const init_pin_states = g_array_sized_new(false, true,
142 sizeof(uint8_t), channels_.size());
144 g_array_set_size(init_pin_states, channels_.size());
146 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
147 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
149 for (DecodeChannel *ch : channels_) {
150 if (!ch->assigned_signal)
153 init_pin_states->data[ch->id] = ch->initial_pin_state;
155 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
156 g_variant_ref_sink(gvar);
157 // key is channel name (pdch->id), value is bit position in each sample (gvar)
158 g_hash_table_insert(channels, ch->pdch_->id, gvar);
161 srd_inst_channel_set_all(decoder_inst_, channels);
163 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
164 g_array_free(init_pin_states, true);
166 return decoder_inst_;
169 void Decoder::invalidate_decoder_inst()
171 decoder_inst_ = nullptr;
174 } // namespace decode