]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decode/decoder.cpp
Initial support for SRD_OUTPUT_LOGIC
[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
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
61Decoder::Decoder(const srd_decoder *const dec, uint8_t stack_level) :
62 srd_decoder_(dec),
63 stack_level_(stack_level),
64 visible_(true),
65 decoder_inst_(nullptr)
66{
67 // Query the annotation output classes
68 uint32_t i = 0;
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];
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()));
77 }
78
79 // Query the binary output classes
80 i = 0;
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 }
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++;
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);
97 rows_.emplace_back(i++, this, srd_row);
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());
102
103 connect(&(rows_.back()), SIGNAL(visibility_changed()),
104 this, SLOT(on_row_visibility_changed()));
105 }
106
107 if (rows_.empty()) {
108 // Make sure there is a row for PDs without row declarations
109 rows_.emplace_back(0, this);
110
111 for (AnnotationClass& c : ann_classes_)
112 c.row = &(rows_.back());
113 }
114}
115
116Decoder::~Decoder()
117{
118 for (auto& option : options_)
119 g_variant_unref(option.second);
120}
121
122const srd_decoder* Decoder::get_srd_decoder() const
123{
124 return srd_decoder_;
125}
126
127uint8_t Decoder::get_stack_level() const
128{
129 return stack_level_;
130}
131
132const char* Decoder::name() const
133{
134 return srd_decoder_->name;
135}
136
137bool Decoder::visible() const
138{
139 return visible_;
140}
141
142void Decoder::set_visible(bool visible)
143{
144 visible_ = visible;
145
146 annotation_visibility_changed();
147}
148
149const vector<DecodeChannel*>& Decoder::channels() const
150{
151 return channels_;
152}
153
154void Decoder::set_channels(vector<DecodeChannel*> channels)
155{
156 channels_ = channels;
157}
158
159const map<string, GVariant*>& Decoder::options() const
160{
161 return options_;
162}
163
164void Decoder::set_option(const char *id, GVariant *value)
165{
166 assert(value);
167 g_variant_ref(value);
168 options_[id] = value;
169
170 // If we have a decoder instance, apply option value immediately
171 apply_all_options();
172}
173
174void Decoder::apply_all_options()
175{
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
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 }
186
187 srd_inst_option_set(decoder_inst_, opt_hash);
188 g_hash_table_destroy(opt_hash);
189 }
190}
191
192bool Decoder::have_required_channels() const
193{
194 for (DecodeChannel *ch : channels_)
195 if (!ch->assigned_signal && !ch->is_optional)
196 return false;
197
198 return true;
199}
200
201srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
202{
203 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
204 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
205
206 for (const auto& option : options_) {
207 GVariant *const value = option.second;
208 g_variant_ref(value);
209 g_hash_table_replace(opt_hash, (void*)g_strdup(
210 option.first.c_str()), value);
211 }
212
213 if (decoder_inst_)
214 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
215
216 decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash);
217 g_hash_table_destroy(opt_hash);
218
219 if (!decoder_inst_)
220 return nullptr;
221
222 // Setup the channels
223 GArray *const init_pin_states = g_array_sized_new(false, true,
224 sizeof(uint8_t), channels_.size());
225
226 g_array_set_size(init_pin_states, channels_.size());
227
228 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
229 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
230
231 for (DecodeChannel *ch : channels_) {
232 if (!ch->assigned_signal)
233 continue;
234
235 init_pin_states->data[ch->id] = ch->initial_pin_state;
236
237 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
238 g_variant_ref_sink(gvar);
239 // key is channel name (pdch->id), value is bit position in each sample (gvar)
240 g_hash_table_insert(channels, ch->pdch_->id, gvar);
241 }
242
243 srd_inst_channel_set_all(decoder_inst_, channels);
244
245 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
246 g_array_free(init_pin_states, true);
247
248 return decoder_inst_;
249}
250
251void Decoder::invalidate_decoder_inst()
252{
253 decoder_inst_ = nullptr;
254}
255
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
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
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
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
310uint32_t Decoder::get_binary_class_count() const
311{
312 return bin_classes_.size();
313}
314
315const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
316{
317 return &(bin_classes_.at(id));
318}
319
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
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
350} // namespace decode
351} // namespace data
352} // namespace pv