]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decode/decoder.cpp
Allow more than 256 binary output classes
[pulseview.git] / pv / data / decode / decoder.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <cassert>
21
22#include <QDebug>
23
24#include <libsigrokcxx/libsigrokcxx.hpp>
25#include <libsigrokdecode/libsigrokdecode.h>
26
27#include "decoder.hpp"
28
29#include <pv/data/signalbase.hpp>
30#include <pv/data/decodesignal.hpp>
31
32using std::map;
33using std::string;
34
35namespace pv {
36namespace data {
37namespace decode {
38
39Decoder::Decoder(const srd_decoder *const dec) :
40 decoder_(dec),
41 shown_(true),
42 decoder_inst_(nullptr)
43{
44 // Query the decoder outputs
45 uint32_t i = 0;
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});
51 }
52}
53
54Decoder::~Decoder()
55{
56 for (auto& option : options_)
57 g_variant_unref(option.second);
58}
59
60const srd_decoder* Decoder::decoder() const
61{
62 return decoder_;
63}
64
65const char* Decoder::name() const
66{
67 return decoder_->name;
68}
69
70bool Decoder::shown() const
71{
72 return shown_;
73}
74
75void Decoder::show(bool show)
76{
77 shown_ = show;
78}
79
80const vector<DecodeChannel*>& Decoder::channels() const
81{
82 return channels_;
83}
84
85void Decoder::set_channels(vector<DecodeChannel*> channels)
86{
87 channels_ = channels;
88}
89
90const map<string, GVariant*>& Decoder::options() const
91{
92 return options_;
93}
94
95void Decoder::set_option(const char *id, GVariant *value)
96{
97 assert(value);
98 g_variant_ref(value);
99 options_[id] = value;
100
101 // If we have a decoder instance, apply option value immediately
102 apply_all_options();
103}
104
105void Decoder::apply_all_options()
106{
107 if (decoder_inst_) {
108 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
109 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
110
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);
116 }
117
118 srd_inst_option_set(decoder_inst_, opt_hash);
119 g_hash_table_destroy(opt_hash);
120 }
121}
122
123bool Decoder::have_required_channels() const
124{
125 for (DecodeChannel *ch : channels_)
126 if (!ch->assigned_signal && !ch->is_optional)
127 return false;
128
129 return true;
130}
131
132srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
133{
134 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
135 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
136
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);
142 }
143
144 if (decoder_inst_)
145 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
146
147 decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash);
148 g_hash_table_destroy(opt_hash);
149
150 if (!decoder_inst_)
151 return nullptr;
152
153 // Setup the channels
154 GArray *const init_pin_states = g_array_sized_new(false, true,
155 sizeof(uint8_t), channels_.size());
156
157 g_array_set_size(init_pin_states, channels_.size());
158
159 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
160 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
161
162 for (DecodeChannel *ch : channels_) {
163 if (!ch->assigned_signal)
164 continue;
165
166 init_pin_states->data[ch->id] = ch->initial_pin_state;
167
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);
172 }
173
174 srd_inst_channel_set_all(decoder_inst_, channels);
175
176 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
177 g_array_free(init_pin_states, true);
178
179 return decoder_inst_;
180}
181
182void Decoder::invalidate_decoder_inst()
183{
184 decoder_inst_ = nullptr;
185}
186
187uint32_t Decoder::get_binary_class_count() const
188{
189 return bin_classes_.size();
190}
191
192const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
193{
194 return &(bin_classes_.at(id));
195}
196
197} // namespace decode
198} // namespace data
199} // namespace pv