]> sigrok.org Git - pulseview.git/blame - pv/data/decode/decoder.cpp
TabularDecView: Implement "hide hidden rows/classes" checkbox
[pulseview.git] / pv / data / decode / decoder.cpp
CommitLineData
7491a29f
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7491a29f
JH
18 */
19
943edd76
MC
20#include <cassert>
21
8ce0e732
SA
22#include <QDebug>
23
fe3a1c21 24#include <libsigrokcxx/libsigrokcxx.hpp>
7491a29f
JH
25#include <libsigrokdecode/libsigrokdecode.h>
26
2acdb232 27#include "decoder.hpp"
7491a29f 28
04394ded 29#include <pv/data/signalbase.hpp>
27a3f09b 30#include <pv/data/decodesignal.hpp>
7491a29f 31
819f4c25
JH
32using std::map;
33using std::string;
7491a29f
JH
34
35namespace pv {
36namespace data {
37namespace decode {
38
f54e68b0 39Decoder::Decoder(const srd_decoder *const dec, uint8_t stack_level) :
cbb2e4da 40 srd_decoder_(dec),
f54e68b0 41 stack_level_(stack_level),
5d3ca591 42 visible_(true),
8ce0e732 43 decoder_inst_(nullptr)
7491a29f 44{
6a26fc44 45 // Query the annotation output classes
ac9494ef 46 uint32_t i = 0;
6a26fc44
SA
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
52 }
53
54 // Query the binary output classes
55 i = 0;
e77de61f
SA
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});
61 }
6a26fc44
SA
62
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)
66 row_count++;
67 rows_.reserve(row_count);
68
69 i = 0;
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;
72 assert(srd_row);
feda6c6b 73 rows_.emplace_back(i++, this, srd_row);
6a26fc44
SA
74
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());
78 }
79
41293691 80 if (rows_.empty()) {
6a26fc44 81 // Make sure there is a row for PDs without row declarations
feda6c6b 82 rows_.emplace_back(0, this);
41293691
SA
83
84 for (AnnotationClass& c : ann_classes_)
85 c.row = &(rows_.back());
86 }
7491a29f
JH
87}
88
89Decoder::~Decoder()
90{
27c05210 91 for (auto& option : options_)
da50281d 92 g_variant_unref(option.second);
7491a29f
JH
93}
94
6a26fc44 95const srd_decoder* Decoder::get_srd_decoder() const
7491a29f 96{
cbb2e4da 97 return srd_decoder_;
7491a29f
JH
98}
99
f54e68b0
SA
100uint8_t Decoder::get_stack_level() const
101{
102 return stack_level_;
103}
104
e77de61f
SA
105const char* Decoder::name() const
106{
cbb2e4da 107 return srd_decoder_->name;
e77de61f
SA
108}
109
5d3ca591 110bool Decoder::visible() const
dd048a7e 111{
5d3ca591 112 return visible_;
dd048a7e
JH
113}
114
5d3ca591 115void Decoder::set_visible(bool visible)
dd048a7e 116{
5d3ca591 117 visible_ = visible;
dd048a7e
JH
118}
119
27a3f09b 120const vector<DecodeChannel*>& Decoder::channels() const
7491a29f 121{
8dbbc7f0 122 return channels_;
7491a29f
JH
123}
124
27a3f09b 125void Decoder::set_channels(vector<DecodeChannel*> channels)
7491a29f 126{
8dbbc7f0 127 channels_ = channels;
7491a29f
JH
128}
129
6f925ba9 130const map<string, GVariant*>& Decoder::options() const
7491a29f 131{
8dbbc7f0 132 return options_;
7491a29f
JH
133}
134
135void Decoder::set_option(const char *id, GVariant *value)
136{
615f6d25 137 assert(value);
7491a29f 138 g_variant_ref(value);
8dbbc7f0 139 options_[id] = value;
8ce0e732
SA
140
141 // If we have a decoder instance, apply option value immediately
72486b78
SA
142 apply_all_options();
143}
144
145void Decoder::apply_all_options()
146{
8ce0e732
SA
147 if (decoder_inst_) {
148 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
149 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
150
c5f47334
SA
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);
156 }
8ce0e732
SA
157
158 srd_inst_option_set(decoder_inst_, opt_hash);
159 g_hash_table_destroy(opt_hash);
160 }
7491a29f
JH
161}
162
6ac6242b 163bool Decoder::have_required_channels() const
a2d4b551 164{
27a3f09b
SA
165 for (DecodeChannel *ch : channels_)
166 if (!ch->assigned_signal && !ch->is_optional)
a2d4b551 167 return false;
a2d4b551
JH
168
169 return true;
170}
171
8ce0e732 172srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
7491a29f 173{
615f6d25
JH
174 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
175 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
176
27c05210 177 for (const auto& option : options_) {
da50281d 178 GVariant *const value = option.second;
615f6d25
JH
179 g_variant_ref(value);
180 g_hash_table_replace(opt_hash, (void*)g_strdup(
da50281d 181 option.first.c_str()), value);
615f6d25
JH
182 }
183
8ce0e732
SA
184 if (decoder_inst_)
185 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
186
cbb2e4da 187 decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash);
615f6d25
JH
188 g_hash_table_destroy(opt_hash);
189
8ce0e732 190 if (!decoder_inst_)
4c60462b 191 return nullptr;
7491a29f 192
6ac6242b 193 // Setup the channels
f5295834 194 GArray *const init_pin_states = g_array_sized_new(false, true,
9f97b357
SA
195 sizeof(uint8_t), channels_.size());
196
197 g_array_set_size(init_pin_states, channels_.size());
198
6ac6242b 199 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
7491a29f
JH
200 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
201
27a3f09b 202 for (DecodeChannel *ch : channels_) {
6e7a4a00
SA
203 if (!ch->assigned_signal)
204 continue;
205
27a3f09b 206 init_pin_states->data[ch->id] = ch->initial_pin_state;
9f97b357 207
6e7a4a00 208 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
7491a29f 209 g_variant_ref_sink(gvar);
6e7a4a00 210 // key is channel name (pdch->id), value is bit position in each sample (gvar)
27a3f09b 211 g_hash_table_insert(channels, ch->pdch_->id, gvar);
7491a29f
JH
212 }
213
8ce0e732 214 srd_inst_channel_set_all(decoder_inst_, channels);
7491a29f 215
8ce0e732 216 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
f5295834 217 g_array_free(init_pin_states, true);
407c9ebe 218
8ce0e732
SA
219 return decoder_inst_;
220}
221
222void Decoder::invalidate_decoder_inst()
223{
224 decoder_inst_ = nullptr;
7491a29f
JH
225}
226
6a26fc44
SA
227vector<Row*> Decoder::get_rows()
228{
229 vector<Row*> result;
230
231 for (Row& row : rows_)
232 result.push_back(&row);
233
234 return result;
235}
236
237Row* Decoder::get_row_by_id(size_t id)
238{
239 if (id > rows_.size())
240 return nullptr;
241
242 return &(rows_[id]);
243}
244
245vector<const AnnotationClass*> Decoder::ann_classes() const
246{
247 vector<const AnnotationClass*> result;
248
249 for (const AnnotationClass& c : ann_classes_)
250 result.push_back(&c);
251
252 return result;
253}
254
41293691
SA
255vector<AnnotationClass*> Decoder::ann_classes()
256{
257 vector<AnnotationClass*> result;
258
259 for (AnnotationClass& c : ann_classes_)
260 result.push_back(&c);
261
262 return result;
263}
264
6a26fc44
SA
265AnnotationClass* Decoder::get_ann_class_by_id(size_t id)
266{
267 if (id >= ann_classes_.size())
268 return nullptr;
269
270 return &(ann_classes_[id]);
271}
272
761f8302
SA
273const AnnotationClass* Decoder::get_ann_class_by_id(size_t id) const
274{
275 if (id >= ann_classes_.size())
276 return nullptr;
277
278 return &(ann_classes_[id]);
279}
280
ac9494ef 281uint32_t Decoder::get_binary_class_count() const
e77de61f
SA
282{
283 return bin_classes_.size();
284}
285
ac9494ef 286const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
e77de61f
SA
287{
288 return &(bin_classes_.at(id));
289}
290
870ea3db
UH
291} // namespace decode
292} // namespace data
293} // namespace pv