]> sigrok.org Git - pulseview.git/blame - pv/data/decode/decoder.cpp
Initial support for SRD_OUTPUT_LOGIC
[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
02078aa1
SA
39AnnotationClass::AnnotationClass(size_t _id, char* _name, char* _description, Row* _row) :
40 id(_id),
41 name(_name),
42 description(_description),
43 row(_row),
44 visible_(true)
45{
46}
47
48bool AnnotationClass::visible() const
49{
50 return visible_;
51}
52
53void AnnotationClass::set_visible(bool visible)
54{
55 visible_ = visible;
56
57 visibility_changed();
58}
59
60
f54e68b0 61Decoder::Decoder(const srd_decoder *const dec, uint8_t stack_level) :
cbb2e4da 62 srd_decoder_(dec),
f54e68b0 63 stack_level_(stack_level),
5d3ca591 64 visible_(true),
8ce0e732 65 decoder_inst_(nullptr)
7491a29f 66{
6a26fc44 67 // Query the annotation output classes
ac9494ef 68 uint32_t i = 0;
6a26fc44
SA
69 for (GSList *l = dec->annotations; l; l = l->next) {
70 char **ann_class = (char**)l->data;
71 char *name = ann_class[0];
72 char *desc = ann_class[1];
02078aa1
SA
73 ann_classes_.emplace_back(i++, name, desc, nullptr);
74
75 connect(&(ann_classes_.back()), SIGNAL(visibility_changed()),
76 this, SLOT(on_class_visibility_changed()));
6a26fc44
SA
77 }
78
79 // Query the binary output classes
80 i = 0;
e77de61f
SA
81 for (GSList *l = dec->binary; l; l = l->next) {
82 char **bin_class = (char**)l->data;
83 char *name = bin_class[0];
84 char *desc = bin_class[1];
85 bin_classes_.push_back({i++, name, desc});
86 }
6a26fc44
SA
87
88 // Query the annotation rows and reference them by the classes that use them
89 uint32_t row_count = 0;
90 for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next)
91 row_count++;
6a26fc44
SA
92
93 i = 0;
94 for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next) {
95 const srd_decoder_annotation_row *const srd_row = (srd_decoder_annotation_row *)rl->data;
96 assert(srd_row);
feda6c6b 97 rows_.emplace_back(i++, this, srd_row);
6a26fc44
SA
98
99 // FIXME PV can crash from .at() if a PD's ann classes are defined incorrectly
100 for (const GSList *cl = srd_row->ann_classes; cl; cl = cl->next)
101 ann_classes_.at((size_t)cl->data).row = &(rows_.back());
02078aa1
SA
102
103 connect(&(rows_.back()), SIGNAL(visibility_changed()),
104 this, SLOT(on_row_visibility_changed()));
6a26fc44
SA
105 }
106
41293691 107 if (rows_.empty()) {
6a26fc44 108 // Make sure there is a row for PDs without row declarations
feda6c6b 109 rows_.emplace_back(0, this);
41293691
SA
110
111 for (AnnotationClass& c : ann_classes_)
112 c.row = &(rows_.back());
113 }
7491a29f
JH
114}
115
116Decoder::~Decoder()
117{
27c05210 118 for (auto& option : options_)
da50281d 119 g_variant_unref(option.second);
7491a29f
JH
120}
121
6a26fc44 122const srd_decoder* Decoder::get_srd_decoder() const
7491a29f 123{
cbb2e4da 124 return srd_decoder_;
7491a29f
JH
125}
126
f54e68b0
SA
127uint8_t Decoder::get_stack_level() const
128{
129 return stack_level_;
130}
131
e77de61f
SA
132const char* Decoder::name() const
133{
cbb2e4da 134 return srd_decoder_->name;
e77de61f
SA
135}
136
5d3ca591 137bool Decoder::visible() const
dd048a7e 138{
5d3ca591 139 return visible_;
dd048a7e
JH
140}
141
5d3ca591 142void Decoder::set_visible(bool visible)
dd048a7e 143{
5d3ca591 144 visible_ = visible;
02078aa1
SA
145
146 annotation_visibility_changed();
dd048a7e
JH
147}
148
27a3f09b 149const vector<DecodeChannel*>& Decoder::channels() const
7491a29f 150{
8dbbc7f0 151 return channels_;
7491a29f
JH
152}
153
27a3f09b 154void Decoder::set_channels(vector<DecodeChannel*> channels)
7491a29f 155{
8dbbc7f0 156 channels_ = channels;
7491a29f
JH
157}
158
6f925ba9 159const map<string, GVariant*>& Decoder::options() const
7491a29f 160{
8dbbc7f0 161 return options_;
7491a29f
JH
162}
163
164void Decoder::set_option(const char *id, GVariant *value)
165{
615f6d25 166 assert(value);
7491a29f 167 g_variant_ref(value);
8dbbc7f0 168 options_[id] = value;
8ce0e732
SA
169
170 // If we have a decoder instance, apply option value immediately
72486b78
SA
171 apply_all_options();
172}
173
174void Decoder::apply_all_options()
175{
8ce0e732
SA
176 if (decoder_inst_) {
177 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
178 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
179
c5f47334
SA
180 for (const auto& option : options_) {
181 GVariant *const value = option.second;
182 g_variant_ref(value);
183 g_hash_table_replace(opt_hash, (void*)g_strdup(
184 option.first.c_str()), value);
185 }
8ce0e732
SA
186
187 srd_inst_option_set(decoder_inst_, opt_hash);
188 g_hash_table_destroy(opt_hash);
189 }
7491a29f
JH
190}
191
6ac6242b 192bool Decoder::have_required_channels() const
a2d4b551 193{
27a3f09b
SA
194 for (DecodeChannel *ch : channels_)
195 if (!ch->assigned_signal && !ch->is_optional)
a2d4b551 196 return false;
a2d4b551
JH
197
198 return true;
199}
200
8ce0e732 201srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
7491a29f 202{
615f6d25
JH
203 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
204 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
205
27c05210 206 for (const auto& option : options_) {
da50281d 207 GVariant *const value = option.second;
615f6d25
JH
208 g_variant_ref(value);
209 g_hash_table_replace(opt_hash, (void*)g_strdup(
da50281d 210 option.first.c_str()), value);
615f6d25
JH
211 }
212
8ce0e732
SA
213 if (decoder_inst_)
214 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
215
cbb2e4da 216 decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash);
615f6d25
JH
217 g_hash_table_destroy(opt_hash);
218
8ce0e732 219 if (!decoder_inst_)
4c60462b 220 return nullptr;
7491a29f 221
6ac6242b 222 // Setup the channels
f5295834 223 GArray *const init_pin_states = g_array_sized_new(false, true,
9f97b357
SA
224 sizeof(uint8_t), channels_.size());
225
226 g_array_set_size(init_pin_states, channels_.size());
227
6ac6242b 228 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
7491a29f
JH
229 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
230
27a3f09b 231 for (DecodeChannel *ch : channels_) {
6e7a4a00
SA
232 if (!ch->assigned_signal)
233 continue;
234
27a3f09b 235 init_pin_states->data[ch->id] = ch->initial_pin_state;
9f97b357 236
6e7a4a00 237 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
7491a29f 238 g_variant_ref_sink(gvar);
6e7a4a00 239 // key is channel name (pdch->id), value is bit position in each sample (gvar)
27a3f09b 240 g_hash_table_insert(channels, ch->pdch_->id, gvar);
7491a29f
JH
241 }
242
8ce0e732 243 srd_inst_channel_set_all(decoder_inst_, channels);
7491a29f 244
8ce0e732 245 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
f5295834 246 g_array_free(init_pin_states, true);
407c9ebe 247
8ce0e732
SA
248 return decoder_inst_;
249}
250
251void Decoder::invalidate_decoder_inst()
252{
253 decoder_inst_ = nullptr;
7491a29f
JH
254}
255
6a26fc44
SA
256vector<Row*> Decoder::get_rows()
257{
258 vector<Row*> result;
259
260 for (Row& row : rows_)
261 result.push_back(&row);
262
263 return result;
264}
265
266Row* Decoder::get_row_by_id(size_t id)
267{
268 if (id > rows_.size())
269 return nullptr;
270
271 return &(rows_[id]);
272}
273
274vector<const AnnotationClass*> Decoder::ann_classes() const
275{
276 vector<const AnnotationClass*> result;
277
278 for (const AnnotationClass& c : ann_classes_)
279 result.push_back(&c);
280
281 return result;
282}
283
41293691
SA
284vector<AnnotationClass*> Decoder::ann_classes()
285{
286 vector<AnnotationClass*> result;
287
288 for (AnnotationClass& c : ann_classes_)
289 result.push_back(&c);
290
291 return result;
292}
293
6a26fc44
SA
294AnnotationClass* Decoder::get_ann_class_by_id(size_t id)
295{
296 if (id >= ann_classes_.size())
297 return nullptr;
298
299 return &(ann_classes_[id]);
300}
301
761f8302
SA
302const AnnotationClass* Decoder::get_ann_class_by_id(size_t id) const
303{
304 if (id >= ann_classes_.size())
305 return nullptr;
306
307 return &(ann_classes_[id]);
308}
309
ac9494ef 310uint32_t Decoder::get_binary_class_count() const
e77de61f
SA
311{
312 return bin_classes_.size();
313}
314
ac9494ef 315const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
e77de61f
SA
316{
317 return &(bin_classes_.at(id));
318}
319
02078aa1
SA
320void Decoder::on_row_visibility_changed()
321{
322 annotation_visibility_changed();
323}
324
325void Decoder::on_class_visibility_changed()
326{
327 annotation_visibility_changed();
328}
329
04b04675
SA
330bool Decoder::has_logic_output() const
331{
332 return (srd_decoder_->logic_output_channels != nullptr);
333}
334
335const vector<DecoderLogicOutputChannel> Decoder::logic_output_channels() const
336{
337 vector<DecoderLogicOutputChannel> result;
338
339 for (GSList *l = srd_decoder_->logic_output_channels; l; l = l->next) {
340 const srd_decoder_logic_output_channel* ch_data =
341 (srd_decoder_logic_output_channel*)l->data;
342
343 result.emplace_back(QString::fromUtf8(ch_data->id),
344 QString::fromUtf8(ch_data->desc), ch_data->samplerate);
345 }
346
347 return result;
348}
349
870ea3db
UH
350} // namespace decode
351} // namespace data
352} // namespace pv