]>
Commit | Line | Data |
---|---|---|
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 | ||
3782d860 UH |
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> |
3782d860 | 30 | #include <pv/data/decodesignal.hpp> |
7491a29f | 31 | |
819f4c25 JH |
32 | using std::map; |
33 | using std::string; | |
7491a29f JH |
34 | |
35 | namespace pv { | |
36 | namespace data { | |
37 | namespace decode { | |
38 | ||
39 | Decoder::Decoder(const srd_decoder *const dec) : | |
10f5e6f5 UH |
40 | srd_decoder_(dec), |
41 | visible_(true), | |
3782d860 | 42 | decoder_inst_(nullptr) |
7491a29f | 43 | { |
10f5e6f5 UH |
44 | // Query the annotation output classes |
45 | uint32_t i = 0; | |
46 | for (GSList *l = dec->annotations; l; l = l->next) { | |
47 | char **ann_class = (char**)l->data; | |
48 | char *name = ann_class[0]; | |
49 | char *desc = ann_class[1]; | |
50 | ann_classes_.push_back({i++, name, desc, nullptr, true}); // Visible by default | |
51 | } | |
52 | ||
53 | // Query the binary output classes | |
54 | i = 0; | |
55 | for (GSList *l = dec->binary; l; l = l->next) { | |
56 | char **bin_class = (char**)l->data; | |
57 | char *name = bin_class[0]; | |
58 | char *desc = bin_class[1]; | |
59 | bin_classes_.push_back({i++, name, desc}); | |
60 | } | |
61 | ||
62 | // Query the annotation rows and reference them by the classes that use them | |
63 | uint32_t row_count = 0; | |
64 | for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next) | |
65 | row_count++; | |
66 | rows_.reserve(row_count); | |
67 | ||
68 | i = 0; | |
69 | for (const GSList *rl = srd_decoder_->annotation_rows; rl; rl = rl->next) { | |
70 | const srd_decoder_annotation_row *const srd_row = (srd_decoder_annotation_row *)rl->data; | |
71 | assert(srd_row); | |
72 | rows_.emplace_back(i++, this, srd_row); | |
73 | ||
74 | // FIXME PV can crash from .at() if a PD's ann classes are defined incorrectly | |
75 | for (const GSList *cl = srd_row->ann_classes; cl; cl = cl->next) | |
76 | ann_classes_.at((size_t)cl->data).row = &(rows_.back()); | |
77 | } | |
78 | ||
79 | if (rows_.empty()) { | |
80 | // Make sure there is a row for PDs without row declarations | |
81 | rows_.emplace_back(0, this); | |
82 | ||
83 | for (AnnotationClass& c : ann_classes_) | |
84 | c.row = &(rows_.back()); | |
85 | } | |
7491a29f JH |
86 | } |
87 | ||
88 | Decoder::~Decoder() | |
89 | { | |
27c05210 | 90 | for (auto& option : options_) |
da50281d | 91 | g_variant_unref(option.second); |
7491a29f JH |
92 | } |
93 | ||
10f5e6f5 UH |
94 | const srd_decoder* Decoder::get_srd_decoder() const |
95 | { | |
96 | return srd_decoder_; | |
97 | } | |
98 | ||
99 | const char* Decoder::name() const | |
7491a29f | 100 | { |
10f5e6f5 | 101 | return srd_decoder_->name; |
7491a29f JH |
102 | } |
103 | ||
10f5e6f5 | 104 | bool Decoder::visible() const |
dd048a7e | 105 | { |
10f5e6f5 | 106 | return visible_; |
dd048a7e JH |
107 | } |
108 | ||
10f5e6f5 | 109 | void Decoder::set_visible(bool visible) |
dd048a7e | 110 | { |
10f5e6f5 | 111 | visible_ = visible; |
dd048a7e JH |
112 | } |
113 | ||
3782d860 | 114 | const vector<DecodeChannel*>& Decoder::channels() const |
7491a29f | 115 | { |
8dbbc7f0 | 116 | return channels_; |
7491a29f JH |
117 | } |
118 | ||
3782d860 | 119 | void Decoder::set_channels(vector<DecodeChannel*> channels) |
7491a29f | 120 | { |
8dbbc7f0 | 121 | channels_ = channels; |
7491a29f JH |
122 | } |
123 | ||
6f925ba9 | 124 | const map<string, GVariant*>& Decoder::options() const |
7491a29f | 125 | { |
8dbbc7f0 | 126 | return options_; |
7491a29f JH |
127 | } |
128 | ||
129 | void Decoder::set_option(const char *id, GVariant *value) | |
130 | { | |
615f6d25 | 131 | assert(value); |
7491a29f | 132 | g_variant_ref(value); |
8dbbc7f0 | 133 | options_[id] = value; |
3782d860 UH |
134 | |
135 | // If we have a decoder instance, apply option value immediately | |
136 | apply_all_options(); | |
7491a29f JH |
137 | } |
138 | ||
3782d860 | 139 | void Decoder::apply_all_options() |
a2d4b551 | 140 | { |
3782d860 UH |
141 | if (decoder_inst_) { |
142 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
143 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
144 | ||
145 | for (const auto& option : options_) { | |
146 | GVariant *const value = option.second; | |
147 | g_variant_ref(value); | |
148 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
149 | option.first.c_str()), value); | |
150 | } | |
151 | ||
152 | srd_inst_option_set(decoder_inst_, opt_hash); | |
153 | g_hash_table_destroy(opt_hash); | |
a2d4b551 | 154 | } |
a2d4b551 JH |
155 | } |
156 | ||
3782d860 | 157 | bool Decoder::have_required_channels() const |
ddee4cf8 | 158 | { |
3782d860 UH |
159 | for (DecodeChannel *ch : channels_) |
160 | if (!ch->assigned_signal && !ch->is_optional) | |
161 | return false; | |
ddee4cf8 | 162 | |
3782d860 | 163 | return true; |
ddee4cf8 JH |
164 | } |
165 | ||
3782d860 | 166 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) |
7491a29f | 167 | { |
615f6d25 JH |
168 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, |
169 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
170 | ||
27c05210 | 171 | for (const auto& option : options_) { |
da50281d | 172 | GVariant *const value = option.second; |
615f6d25 JH |
173 | g_variant_ref(value); |
174 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
da50281d | 175 | option.first.c_str()), value); |
615f6d25 JH |
176 | } |
177 | ||
3782d860 UH |
178 | if (decoder_inst_) |
179 | qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists"; | |
180 | ||
10f5e6f5 | 181 | decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash); |
615f6d25 JH |
182 | g_hash_table_destroy(opt_hash); |
183 | ||
3782d860 | 184 | if (!decoder_inst_) |
4c60462b | 185 | return nullptr; |
7491a29f | 186 | |
6ac6242b | 187 | // Setup the channels |
3782d860 UH |
188 | GArray *const init_pin_states = g_array_sized_new(false, true, |
189 | sizeof(uint8_t), channels_.size()); | |
190 | ||
191 | g_array_set_size(init_pin_states, channels_.size()); | |
192 | ||
6ac6242b | 193 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, |
7491a29f JH |
194 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); |
195 | ||
3782d860 UH |
196 | for (DecodeChannel *ch : channels_) { |
197 | if (!ch->assigned_signal) | |
198 | continue; | |
199 | ||
200 | init_pin_states->data[ch->id] = ch->initial_pin_state; | |
201 | ||
202 | GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position | |
7491a29f | 203 | g_variant_ref_sink(gvar); |
3782d860 UH |
204 | // key is channel name (pdch->id), value is bit position in each sample (gvar) |
205 | g_hash_table_insert(channels, ch->pdch_->id, gvar); | |
7491a29f JH |
206 | } |
207 | ||
3782d860 | 208 | srd_inst_channel_set_all(decoder_inst_, channels); |
7491a29f | 209 | |
3782d860 UH |
210 | srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states); |
211 | g_array_free(init_pin_states, true); | |
407c9ebe | 212 | |
3782d860 UH |
213 | return decoder_inst_; |
214 | } | |
215 | ||
216 | void Decoder::invalidate_decoder_inst() | |
217 | { | |
218 | decoder_inst_ = nullptr; | |
7491a29f JH |
219 | } |
220 | ||
10f5e6f5 UH |
221 | vector<Row*> Decoder::get_rows() |
222 | { | |
223 | vector<Row*> result; | |
224 | ||
225 | for (Row& row : rows_) | |
226 | result.push_back(&row); | |
227 | ||
228 | return result; | |
229 | } | |
230 | ||
231 | Row* Decoder::get_row_by_id(size_t id) | |
232 | { | |
233 | if (id > rows_.size()) | |
234 | return nullptr; | |
235 | ||
236 | return &(rows_[id]); | |
237 | } | |
238 | ||
239 | vector<const AnnotationClass*> Decoder::ann_classes() const | |
240 | { | |
241 | vector<const AnnotationClass*> result; | |
242 | ||
243 | for (const AnnotationClass& c : ann_classes_) | |
244 | result.push_back(&c); | |
245 | ||
246 | return result; | |
247 | } | |
248 | ||
249 | vector<AnnotationClass*> Decoder::ann_classes() | |
250 | { | |
251 | vector<AnnotationClass*> result; | |
252 | ||
253 | for (AnnotationClass& c : ann_classes_) | |
254 | result.push_back(&c); | |
255 | ||
256 | return result; | |
257 | } | |
258 | ||
259 | AnnotationClass* Decoder::get_ann_class_by_id(size_t id) | |
260 | { | |
261 | if (id >= ann_classes_.size()) | |
262 | return nullptr; | |
263 | ||
264 | return &(ann_classes_[id]); | |
265 | } | |
266 | ||
267 | const AnnotationClass* Decoder::get_ann_class_by_id(size_t id) const | |
268 | { | |
269 | if (id >= ann_classes_.size()) | |
270 | return nullptr; | |
271 | ||
272 | return &(ann_classes_[id]); | |
273 | } | |
274 | ||
275 | uint32_t Decoder::get_binary_class_count() const | |
276 | { | |
277 | return bin_classes_.size(); | |
278 | } | |
279 | ||
280 | const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const | |
281 | { | |
282 | return &(bin_classes_.at(id)); | |
283 | } | |
284 | ||
870ea3db UH |
285 | } // namespace decode |
286 | } // namespace data | |
287 | } // namespace pv |