]>
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) : | |
40 | srd_decoder_(dec), | |
41 | shown_(true), | |
42 | decoder_inst_(nullptr) | |
43 | { | |
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_.push_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 | ||
84 | Decoder::~Decoder() | |
85 | { | |
86 | for (auto& option : options_) | |
87 | g_variant_unref(option.second); | |
88 | } | |
89 | ||
90 | const srd_decoder* Decoder::get_srd_decoder() const | |
91 | { | |
92 | return srd_decoder_; | |
93 | } | |
94 | ||
95 | const char* Decoder::name() const | |
96 | { | |
97 | return srd_decoder_->name; | |
98 | } | |
99 | ||
100 | bool Decoder::shown() const | |
101 | { | |
102 | return shown_; | |
103 | } | |
104 | ||
105 | void Decoder::show(bool show) | |
106 | { | |
107 | shown_ = show; | |
108 | } | |
109 | ||
110 | const vector<DecodeChannel*>& Decoder::channels() const | |
111 | { | |
112 | return channels_; | |
113 | } | |
114 | ||
115 | void Decoder::set_channels(vector<DecodeChannel*> channels) | |
116 | { | |
117 | channels_ = channels; | |
118 | } | |
119 | ||
120 | const map<string, GVariant*>& Decoder::options() const | |
121 | { | |
122 | return options_; | |
123 | } | |
124 | ||
125 | void Decoder::set_option(const char *id, GVariant *value) | |
126 | { | |
127 | assert(value); | |
128 | g_variant_ref(value); | |
129 | options_[id] = value; | |
130 | ||
131 | // If we have a decoder instance, apply option value immediately | |
132 | apply_all_options(); | |
133 | } | |
134 | ||
135 | void Decoder::apply_all_options() | |
136 | { | |
137 | if (decoder_inst_) { | |
138 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
139 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
140 | ||
141 | for (const auto& option : options_) { | |
142 | GVariant *const value = option.second; | |
143 | g_variant_ref(value); | |
144 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
145 | option.first.c_str()), value); | |
146 | } | |
147 | ||
148 | srd_inst_option_set(decoder_inst_, opt_hash); | |
149 | g_hash_table_destroy(opt_hash); | |
150 | } | |
151 | } | |
152 | ||
153 | bool Decoder::have_required_channels() const | |
154 | { | |
155 | for (DecodeChannel *ch : channels_) | |
156 | if (!ch->assigned_signal && !ch->is_optional) | |
157 | return false; | |
158 | ||
159 | return true; | |
160 | } | |
161 | ||
162 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) | |
163 | { | |
164 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
165 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
166 | ||
167 | for (const auto& option : options_) { | |
168 | GVariant *const value = option.second; | |
169 | g_variant_ref(value); | |
170 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
171 | option.first.c_str()), value); | |
172 | } | |
173 | ||
174 | if (decoder_inst_) | |
175 | qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists"; | |
176 | ||
177 | decoder_inst_ = srd_inst_new(session, srd_decoder_->id, opt_hash); | |
178 | g_hash_table_destroy(opt_hash); | |
179 | ||
180 | if (!decoder_inst_) | |
181 | return nullptr; | |
182 | ||
183 | // Setup the channels | |
184 | GArray *const init_pin_states = g_array_sized_new(false, true, | |
185 | sizeof(uint8_t), channels_.size()); | |
186 | ||
187 | g_array_set_size(init_pin_states, channels_.size()); | |
188 | ||
189 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, | |
190 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
191 | ||
192 | for (DecodeChannel *ch : channels_) { | |
193 | if (!ch->assigned_signal) | |
194 | continue; | |
195 | ||
196 | init_pin_states->data[ch->id] = ch->initial_pin_state; | |
197 | ||
198 | GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position | |
199 | g_variant_ref_sink(gvar); | |
200 | // key is channel name (pdch->id), value is bit position in each sample (gvar) | |
201 | g_hash_table_insert(channels, ch->pdch_->id, gvar); | |
202 | } | |
203 | ||
204 | srd_inst_channel_set_all(decoder_inst_, channels); | |
205 | ||
206 | srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states); | |
207 | g_array_free(init_pin_states, true); | |
208 | ||
209 | return decoder_inst_; | |
210 | } | |
211 | ||
212 | void Decoder::invalidate_decoder_inst() | |
213 | { | |
214 | decoder_inst_ = nullptr; | |
215 | } | |
216 | ||
217 | vector<Row*> Decoder::get_rows() | |
218 | { | |
219 | vector<Row*> result; | |
220 | ||
221 | for (Row& row : rows_) | |
222 | result.push_back(&row); | |
223 | ||
224 | return result; | |
225 | } | |
226 | ||
227 | Row* Decoder::get_row_by_id(size_t id) | |
228 | { | |
229 | if (id > rows_.size()) | |
230 | return nullptr; | |
231 | ||
232 | return &(rows_[id]); | |
233 | } | |
234 | ||
235 | vector<const AnnotationClass*> Decoder::ann_classes() const | |
236 | { | |
237 | vector<const AnnotationClass*> result; | |
238 | ||
239 | for (const AnnotationClass& c : ann_classes_) | |
240 | result.push_back(&c); | |
241 | ||
242 | return result; | |
243 | } | |
244 | ||
245 | AnnotationClass* Decoder::get_ann_class_by_id(size_t id) | |
246 | { | |
247 | if (id >= ann_classes_.size()) | |
248 | return nullptr; | |
249 | ||
250 | return &(ann_classes_[id]); | |
251 | } | |
252 | ||
253 | uint32_t Decoder::get_binary_class_count() const | |
254 | { | |
255 | return bin_classes_.size(); | |
256 | } | |
257 | ||
258 | const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const | |
259 | { | |
260 | return &(bin_classes_.at(id)); | |
261 | } | |
262 | ||
263 | } // namespace decode | |
264 | } // namespace data | |
265 | } // namespace pv |