]>
Commit | Line | Data |
---|---|---|
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 | ||
32 | using std::map; | |
33 | using std::string; | |
34 | ||
35 | namespace pv { | |
36 | namespace data { | |
37 | namespace decode { | |
38 | ||
39 | Decoder::Decoder(const srd_decoder *const dec, uint8_t stack_level) : | |
40 | srd_decoder_(dec), | |
41 | stack_level_(stack_level), | |
42 | visible_(true), | |
43 | decoder_inst_(nullptr) | |
44 | { | |
45 | // Query the annotation output classes | |
46 | uint32_t i = 0; | |
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; | |
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 | } | |
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); | |
73 | rows_.emplace_back(i++, this, srd_row); | |
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 | ||
80 | if (rows_.empty()) { | |
81 | // Make sure there is a row for PDs without row declarations | |
82 | rows_.emplace_back(0, this); | |
83 | ||
84 | for (AnnotationClass& c : ann_classes_) | |
85 | c.row = &(rows_.back()); | |
86 | } | |
87 | } | |
88 | ||
89 | Decoder::~Decoder() | |
90 | { | |
91 | for (auto& option : options_) | |
92 | g_variant_unref(option.second); | |
93 | } | |
94 | ||
95 | const srd_decoder* Decoder::get_srd_decoder() const | |
96 | { | |
97 | return srd_decoder_; | |
98 | } | |
99 | ||
100 | uint8_t Decoder::get_stack_level() const | |
101 | { | |
102 | return stack_level_; | |
103 | } | |
104 | ||
105 | const char* Decoder::name() const | |
106 | { | |
107 | return srd_decoder_->name; | |
108 | } | |
109 | ||
110 | bool Decoder::visible() const | |
111 | { | |
112 | return visible_; | |
113 | } | |
114 | ||
115 | void Decoder::set_visible(bool visible) | |
116 | { | |
117 | visible_ = visible; | |
118 | } | |
119 | ||
120 | const vector<DecodeChannel*>& Decoder::channels() const | |
121 | { | |
122 | return channels_; | |
123 | } | |
124 | ||
125 | void Decoder::set_channels(vector<DecodeChannel*> channels) | |
126 | { | |
127 | channels_ = channels; | |
128 | } | |
129 | ||
130 | const map<string, GVariant*>& Decoder::options() const | |
131 | { | |
132 | return options_; | |
133 | } | |
134 | ||
135 | void Decoder::set_option(const char *id, GVariant *value) | |
136 | { | |
137 | assert(value); | |
138 | g_variant_ref(value); | |
139 | options_[id] = value; | |
140 | ||
141 | // If we have a decoder instance, apply option value immediately | |
142 | apply_all_options(); | |
143 | } | |
144 | ||
145 | void Decoder::apply_all_options() | |
146 | { | |
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 | ||
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 | } | |
157 | ||
158 | srd_inst_option_set(decoder_inst_, opt_hash); | |
159 | g_hash_table_destroy(opt_hash); | |
160 | } | |
161 | } | |
162 | ||
163 | bool Decoder::have_required_channels() const | |
164 | { | |
165 | for (DecodeChannel *ch : channels_) | |
166 | if (!ch->assigned_signal && !ch->is_optional) | |
167 | return false; | |
168 | ||
169 | return true; | |
170 | } | |
171 | ||
172 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) | |
173 | { | |
174 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
175 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
176 | ||
177 | for (const auto& option : options_) { | |
178 | GVariant *const value = option.second; | |
179 | g_variant_ref(value); | |
180 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
181 | option.first.c_str()), value); | |
182 | } | |
183 | ||
184 | if (decoder_inst_) | |
185 | qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists"; | |
186 | ||
187 | decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash); | |
188 | g_hash_table_destroy(opt_hash); | |
189 | ||
190 | if (!decoder_inst_) | |
191 | return nullptr; | |
192 | ||
193 | // Setup the channels | |
194 | GArray *const init_pin_states = g_array_sized_new(false, true, | |
195 | sizeof(uint8_t), channels_.size()); | |
196 | ||
197 | g_array_set_size(init_pin_states, channels_.size()); | |
198 | ||
199 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, | |
200 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
201 | ||
202 | for (DecodeChannel *ch : channels_) { | |
203 | if (!ch->assigned_signal) | |
204 | continue; | |
205 | ||
206 | init_pin_states->data[ch->id] = ch->initial_pin_state; | |
207 | ||
208 | GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position | |
209 | g_variant_ref_sink(gvar); | |
210 | // key is channel name (pdch->id), value is bit position in each sample (gvar) | |
211 | g_hash_table_insert(channels, ch->pdch_->id, gvar); | |
212 | } | |
213 | ||
214 | srd_inst_channel_set_all(decoder_inst_, channels); | |
215 | ||
216 | srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states); | |
217 | g_array_free(init_pin_states, true); | |
218 | ||
219 | return decoder_inst_; | |
220 | } | |
221 | ||
222 | void Decoder::invalidate_decoder_inst() | |
223 | { | |
224 | decoder_inst_ = nullptr; | |
225 | } | |
226 | ||
227 | vector<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 | ||
237 | Row* Decoder::get_row_by_id(size_t id) | |
238 | { | |
239 | if (id > rows_.size()) | |
240 | return nullptr; | |
241 | ||
242 | return &(rows_[id]); | |
243 | } | |
244 | ||
245 | vector<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 | ||
255 | vector<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 | ||
265 | AnnotationClass* 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 | ||
273 | const 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 | ||
281 | uint32_t Decoder::get_binary_class_count() const | |
282 | { | |
283 | return bin_classes_.size(); | |
284 | } | |
285 | ||
286 | const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const | |
287 | { | |
288 | return &(bin_classes_.at(id)); | |
289 | } | |
290 | ||
291 | } // namespace decode | |
292 | } // namespace data | |
293 | } // namespace pv |