]>
Commit | Line | Data |
---|---|---|
b2c19614 | 1 | /* |
50bd5d25 | 2 | * This file is part of the libsigrokdecode project. |
b2c19614 BV |
3 | * |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
bc5f5a43 | 5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
b2c19614 BV |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
c1f86f02 UH |
21 | #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
22 | #include "libsigrokdecode-internal.h" | |
7ce7775c | 23 | #include "config.h" |
b2c19614 | 24 | #include <glib.h> |
1aef2f93 | 25 | #include <inttypes.h> |
0bdadba2 | 26 | #include <stdlib.h> |
32cfb920 | 27 | #include <stdint.h> |
b2c19614 | 28 | |
32cfb920 | 29 | /** @cond PRIVATE */ |
d906d3f9 | 30 | |
fe9d91a8 | 31 | extern GSList *sessions; |
b2c19614 | 32 | |
c9bfccc6 | 33 | /* type_logic.c */ |
55c3c5f4 | 34 | extern SRD_PRIV PyTypeObject srd_logic_type; |
b2c19614 | 35 | |
57790bc8 UH |
36 | /** @endcond */ |
37 | ||
b2c19614 | 38 | /** |
190b71cf | 39 | * @file |
8c664ca2 | 40 | * |
190b71cf | 41 | * Decoder instance handling. |
b2c19614 | 42 | */ |
4895418c UH |
43 | |
44 | /** | |
45 | * @defgroup grp_instances Decoder instances | |
46 | * | |
47 | * Decoder instance handling. | |
48 | * | |
49 | * @{ | |
50 | */ | |
51 | ||
7ce7775c | 52 | /** |
b33b8aa5 | 53 | * Set one or more options in a decoder instance. |
0bdadba2 | 54 | * |
361fdcaa UH |
55 | * Handled options are removed from the hash. |
56 | * | |
0bdadba2 BV |
57 | * @param di Decoder instance. |
58 | * @param options A GHashTable of options to set. | |
7ce7775c | 59 | * |
0bdadba2 | 60 | * @return SRD_OK upon success, a (negative) error code otherwise. |
8c664ca2 UH |
61 | * |
62 | * @since 0.1.0 | |
0bdadba2 | 63 | */ |
b33b8aa5 | 64 | SRD_API int srd_inst_option_set(struct srd_decoder_inst *di, |
0ff2d191 | 65 | GHashTable *options) |
0bdadba2 BV |
66 | { |
67 | PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval; | |
68 | PyObject *py_optlist, *py_classval; | |
69 | Py_UNICODE *py_ustr; | |
2f395bff | 70 | GVariant *value; |
0bdadba2 | 71 | unsigned long long int val_ull; |
2f395bff | 72 | gint64 val_int; |
0bdadba2 | 73 | int num_optkeys, ret, size, i; |
2f395bff BV |
74 | const char *val_str; |
75 | char *dbg, *key; | |
0bdadba2 | 76 | |
3af0e345 UH |
77 | if (!di) { |
78 | srd_err("Invalid decoder instance."); | |
79 | return SRD_ERR_ARG; | |
80 | } | |
81 | ||
82 | if (!options) { | |
83 | srd_err("Invalid options GHashTable."); | |
84 | return SRD_ERR_ARG; | |
85 | } | |
86 | ||
c9bfccc6 | 87 | if (!PyObject_HasAttrString(di->decoder->py_dec, "options")) { |
0bdadba2 | 88 | /* Decoder has no options. */ |
e431d9cc BV |
89 | if (g_hash_table_size(options) == 0) { |
90 | /* No options provided. */ | |
91 | return SRD_OK; | |
92 | } else { | |
93 | srd_err("Protocol decoder has no options."); | |
94 | return SRD_ERR_ARG; | |
95 | } | |
0bdadba2 | 96 | return SRD_OK; |
e431d9cc | 97 | } |
0bdadba2 BV |
98 | |
99 | ret = SRD_ERR_PYTHON; | |
100 | key = NULL; | |
101 | py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL; | |
102 | py_optlist = py_classval = NULL; | |
103 | py_dec_options = PyObject_GetAttrString(di->decoder->py_dec, "options"); | |
0bdadba2 BV |
104 | |
105 | /* All of these are synthesized objects, so they're good. */ | |
106 | py_dec_optkeys = PyDict_Keys(py_dec_options); | |
107 | num_optkeys = PyList_Size(py_dec_optkeys); | |
119d6258 BV |
108 | |
109 | /* | |
110 | * The 'options' dictionary is a class variable, but we need to | |
111 | * change it. Changing it directly will affect the entire class, | |
112 | * so we need to create a new object for it, and populate that | |
113 | * instead. | |
114 | */ | |
a8b72b05 | 115 | if (!(py_di_options = PyObject_GetAttrString(di->py_inst, "options"))) |
0bdadba2 | 116 | goto err_out; |
119d6258 BV |
117 | Py_DECREF(py_di_options); |
118 | py_di_options = PyDict_New(); | |
119 | PyObject_SetAttrString(di->py_inst, "options", py_di_options); | |
0bdadba2 BV |
120 | for (i = 0; i < num_optkeys; i++) { |
121 | /* Get the default class value for this option. */ | |
122 | py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key); | |
123 | if (!(py_optlist = PyDict_GetItemString(py_dec_options, key))) | |
124 | goto err_out; | |
125 | if (!(py_classval = PyList_GetItem(py_optlist, 1))) | |
126 | goto err_out; | |
130ef08a | 127 | if (!PyUnicode_Check(py_classval) && !PyLong_Check(py_classval)) { |
c9bfccc6 UH |
128 | srd_err("Options of type %s are not yet supported.", |
129 | Py_TYPE(py_classval)->tp_name); | |
130ef08a BV |
130 | goto err_out; |
131 | } | |
0bdadba2 BV |
132 | |
133 | if ((value = g_hash_table_lookup(options, key))) { | |
2f395bff BV |
134 | dbg = g_variant_print(value, TRUE); |
135 | srd_dbg("got option '%s' = %s", key, dbg); | |
136 | g_free(dbg); | |
0bdadba2 BV |
137 | /* An override for this option was provided. */ |
138 | if (PyUnicode_Check(py_classval)) { | |
2f395bff BV |
139 | if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) { |
140 | srd_err("Option '%s' requires a string value.", key); | |
141 | goto err_out; | |
142 | } | |
143 | val_str = g_variant_get_string(value, NULL); | |
144 | if (!(py_optval = PyUnicode_FromString(val_str))) { | |
0bdadba2 BV |
145 | /* Some UTF-8 encoding error. */ |
146 | PyErr_Clear(); | |
2f395bff | 147 | srd_err("Option '%s' requires a UTF-8 string value.", key); |
0bdadba2 BV |
148 | goto err_out; |
149 | } | |
150 | } else if (PyLong_Check(py_classval)) { | |
2f395bff BV |
151 | if (!g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) { |
152 | srd_err("Option '%s' requires an integer value.", key); | |
153 | goto err_out; | |
154 | } | |
155 | val_int = g_variant_get_int64(value); | |
156 | if (!(py_optval = PyLong_FromLong(val_int))) { | |
0bdadba2 BV |
157 | /* ValueError Exception */ |
158 | PyErr_Clear(); | |
2f395bff | 159 | srd_err("Option '%s' has invalid integer value.", key); |
0bdadba2 BV |
160 | goto err_out; |
161 | } | |
162 | } | |
163 | g_hash_table_remove(options, key); | |
164 | } else { | |
165 | /* Use the class default for this option. */ | |
166 | if (PyUnicode_Check(py_classval)) { | |
167 | /* Make a brand new copy of the string. */ | |
168 | py_ustr = PyUnicode_AS_UNICODE(py_classval); | |
169 | size = PyUnicode_GET_SIZE(py_classval); | |
170 | py_optval = PyUnicode_FromUnicode(py_ustr, size); | |
171 | } else if (PyLong_Check(py_classval)) { | |
172 | /* Make a brand new copy of the integer. */ | |
173 | val_ull = PyLong_AsUnsignedLongLong(py_classval); | |
174 | if (val_ull == (unsigned long long)-1) { | |
175 | /* OverFlowError exception */ | |
176 | PyErr_Clear(); | |
c9bfccc6 UH |
177 | srd_err("Invalid integer value for %s: " |
178 | "expected integer.", key); | |
0bdadba2 BV |
179 | goto err_out; |
180 | } | |
181 | if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull))) | |
182 | goto err_out; | |
183 | } | |
184 | } | |
185 | ||
361fdcaa UH |
186 | /* |
187 | * If we got here, py_optval holds a known good new reference | |
0bdadba2 BV |
188 | * to the instance option to set. |
189 | */ | |
190 | if (PyDict_SetItemString(py_di_options, key, py_optval) == -1) | |
191 | goto err_out; | |
e592ac89 | 192 | g_free(key); |
b88e336b | 193 | key = NULL; |
0bdadba2 BV |
194 | } |
195 | ||
196 | ret = SRD_OK; | |
197 | ||
198 | err_out: | |
0bdadba2 BV |
199 | Py_XDECREF(py_di_options); |
200 | Py_XDECREF(py_dec_optkeys); | |
201 | Py_XDECREF(py_dec_options); | |
6f858319 | 202 | g_free(key); |
7fc7bde6 | 203 | if (PyErr_Occurred()) { |
aafeeaea | 204 | srd_exception_catch("Stray exception in srd_inst_option_set()."); |
7fc7bde6 BV |
205 | ret = SRD_ERR_PYTHON; |
206 | } | |
0bdadba2 BV |
207 | |
208 | return ret; | |
209 | } | |
210 | ||
b33b8aa5 | 211 | /* Helper GComparefunc for g_slist_find_custom() in srd_inst_probe_set_all() */ |
abeeed8b | 212 | static gint compare_probe_id(const struct srd_probe *a, const char *probe_id) |
f38ec285 | 213 | { |
f38ec285 BV |
214 | return strcmp(a->id, probe_id); |
215 | } | |
216 | ||
0bdadba2 | 217 | /** |
b33b8aa5 UH |
218 | * Set all probes in a decoder instance. |
219 | * | |
220 | * This function sets _all_ probes for the specified decoder instance, i.e., | |
221 | * it overwrites any probes that were already defined (if any). | |
0bdadba2 BV |
222 | * |
223 | * @param di Decoder instance. | |
38ff5046 UH |
224 | * @param new_probes A GHashTable of probes to set. Key is probe name, value is |
225 | * the probe number. Samples passed to this instance will be | |
226 | * arranged in this order. | |
9eec72cb DE |
227 | * @param unit_size Number of bytes per sample in the data stream to be passed |
228 | * to the decoder. The highest probe index specified in the | |
229 | * probe map must lie within a sample unit. | |
12243c22 | 230 | * |
0bdadba2 | 231 | * @return SRD_OK upon success, a (negative) error code otherwise. |
8c664ca2 UH |
232 | * |
233 | * @since 0.1.0 | |
0bdadba2 | 234 | */ |
b33b8aa5 | 235 | SRD_API int srd_inst_probe_set_all(struct srd_decoder_inst *di, |
9eec72cb | 236 | GHashTable *new_probes, int unit_size) |
0bdadba2 | 237 | { |
2f395bff | 238 | GVariant *probe_val; |
f38ec285 BV |
239 | GList *l; |
240 | GSList *sl; | |
241 | struct srd_probe *p; | |
9eec72cb | 242 | int *new_probemap, new_probenum, num_required_probes, i; |
2f395bff | 243 | char *probe_id; |
0bdadba2 | 244 | |
ce46beed | 245 | srd_dbg("set probes called for instance %s with list of %d probes", |
a8b72b05 | 246 | di->inst_id, g_hash_table_size(new_probes)); |
ce46beed | 247 | |
f38ec285 | 248 | if (g_hash_table_size(new_probes) == 0) |
0bdadba2 BV |
249 | /* No probes provided. */ |
250 | return SRD_OK; | |
251 | ||
c9bfccc6 | 252 | if (di->dec_num_probes == 0) { |
0bdadba2 | 253 | /* Decoder has no probes. */ |
f38ec285 | 254 | srd_err("Protocol decoder %s has no probes to define.", |
c9bfccc6 | 255 | di->decoder->name); |
f38ec285 BV |
256 | return SRD_ERR_ARG; |
257 | } | |
0bdadba2 | 258 | |
f38ec285 | 259 | new_probemap = NULL; |
0bdadba2 | 260 | |
f38ec285 | 261 | if (!(new_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) { |
a61ece20 | 262 | srd_err("Failed to g_malloc() new probe map."); |
f38ec285 | 263 | return SRD_ERR_MALLOC; |
0bdadba2 BV |
264 | } |
265 | ||
38ff5046 UH |
266 | /* |
267 | * For now, map all indexes to probe -1 (can be overridden later). | |
268 | * This -1 is interpreted as an unspecified probe later. | |
269 | */ | |
270 | for (i = 0; i < di->dec_num_probes; i++) | |
271 | new_probemap[i] = -1; | |
272 | ||
f38ec285 BV |
273 | for (l = g_hash_table_get_keys(new_probes); l; l = l->next) { |
274 | probe_id = l->data; | |
69075817 | 275 | probe_val = g_hash_table_lookup(new_probes, probe_id); |
2f395bff | 276 | if (!g_variant_is_of_type(probe_val, G_VARIANT_TYPE_INT32)) { |
be873260 | 277 | /* Probe name was specified without a value. */ |
c9bfccc6 | 278 | srd_err("No probe number was specified for %s.", |
2f395bff | 279 | probe_id); |
be873260 BV |
280 | g_free(new_probemap); |
281 | return SRD_ERR_ARG; | |
282 | } | |
2f395bff | 283 | new_probenum = g_variant_get_int32(probe_val); |
9eec72cb DE |
284 | if (new_probenum >= 8 * unit_size) { |
285 | srd_err("Probe index %d not within data unit (%d bit).", | |
286 | new_probenum, 8 * unit_size); | |
287 | g_free(new_probemap); | |
288 | return SRD_ERR_ARG; | |
289 | } | |
f38ec285 BV |
290 | if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id, |
291 | (GCompareFunc)compare_probe_id))) { | |
292 | /* Fall back on optional probes. */ | |
dcdf4883 | 293 | if (!(sl = g_slist_find_custom(di->decoder->opt_probes, |
c9bfccc6 UH |
294 | probe_id, (GCompareFunc) compare_probe_id))) { |
295 | srd_err("Protocol decoder %s has no probe " | |
2f395bff | 296 | "'%s'.", di->decoder->name, probe_id); |
f38ec285 BV |
297 | g_free(new_probemap); |
298 | return SRD_ERR_ARG; | |
299 | } | |
300 | } | |
301 | p = sl->data; | |
302 | new_probemap[p->order] = new_probenum; | |
38ff5046 UH |
303 | srd_dbg("Setting probe mapping: %s (index %d) = probe %d.", |
304 | p->id, p->order, new_probenum); | |
305 | } | |
9eec72cb | 306 | di->data_unitsize = unit_size; |
38ff5046 UH |
307 | |
308 | srd_dbg("Final probe map:"); | |
309 | num_required_probes = g_slist_length(di->decoder->probes); | |
310 | for (i = 0; i < di->dec_num_probes; i++) { | |
311 | srd_dbg(" - index %d = probe %d (%s)", i, new_probemap[i], | |
312 | (i < num_required_probes) ? "required" : "optional"); | |
f38ec285 | 313 | } |
38ff5046 | 314 | |
9bf7f71c UH |
315 | /* Report an error if not all required probes were specified. */ |
316 | for (i = 0; i < num_required_probes; i++) { | |
317 | if (new_probemap[i] != -1) | |
318 | continue; | |
319 | p = g_slist_nth(di->decoder->probes, i)->data; | |
320 | srd_err("Required probe '%s' (index %d) was not specified.", | |
321 | p->id, i); | |
322 | return SRD_ERR; | |
323 | } | |
324 | ||
f38ec285 BV |
325 | g_free(di->dec_probemap); |
326 | di->dec_probemap = new_probemap; | |
0bdadba2 | 327 | |
f38ec285 | 328 | return SRD_OK; |
0bdadba2 BV |
329 | } |
330 | ||
331 | /** | |
332 | * Create a new protocol decoder instance. | |
7ce7775c | 333 | * |
32cfb920 | 334 | * @param sess The session holding the protocol decoder instance. |
38ff5046 | 335 | * @param decoder_id Decoder 'id' field. |
0bdadba2 | 336 | * @param options GHashtable of options which override the defaults set in |
38ff5046 | 337 | * the decoder class. May be NULL. |
12243c22 | 338 | * |
a8b72b05 | 339 | * @return Pointer to a newly allocated struct srd_decoder_inst, or |
c9bfccc6 | 340 | * NULL in case of failure. |
8c664ca2 | 341 | * |
69075817 | 342 | * @since 0.3.0 |
7ce7775c | 343 | */ |
32cfb920 BV |
344 | SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess, |
345 | const char *decoder_id, GHashTable *options) | |
b2c19614 | 346 | { |
c9bfccc6 | 347 | int i; |
b2c19614 | 348 | struct srd_decoder *dec; |
a8b72b05 BV |
349 | struct srd_decoder_inst *di; |
350 | char *inst_id; | |
b2c19614 | 351 | |
7a1712c4 | 352 | srd_dbg("Creating new %s instance.", decoder_id); |
7ce7775c | 353 | |
32cfb920 BV |
354 | if (session_is_valid(sess) != SRD_OK) { |
355 | srd_err("Invalid session."); | |
356 | return NULL; | |
357 | } | |
358 | ||
9d122fd5 | 359 | if (!(dec = srd_decoder_get_by_id(decoder_id))) { |
0bdadba2 | 360 | srd_err("Protocol decoder %s not found.", decoder_id); |
b2c19614 | 361 | return NULL; |
0bdadba2 | 362 | } |
b2c19614 | 363 | |
a8b72b05 | 364 | if (!(di = g_try_malloc0(sizeof(struct srd_decoder_inst)))) { |
a61ece20 | 365 | srd_err("Failed to g_malloc() instance."); |
7ce7775c BV |
366 | return NULL; |
367 | } | |
0bdadba2 | 368 | |
b2c19614 | 369 | di->decoder = dec; |
32cfb920 | 370 | di->sess = sess; |
38ff5046 UH |
371 | if (options) { |
372 | inst_id = g_hash_table_lookup(options, "id"); | |
373 | di->inst_id = g_strdup(inst_id ? inst_id : decoder_id); | |
374 | g_hash_table_remove(options, "id"); | |
375 | } else | |
376 | di->inst_id = g_strdup(decoder_id); | |
b2c19614 | 377 | |
361fdcaa UH |
378 | /* |
379 | * Prepare a default probe map, where samples come in the | |
f38ec285 BV |
380 | * order in which the decoder class defined them. |
381 | */ | |
382 | di->dec_num_probes = g_slist_length(di->decoder->probes) + | |
69075817 | 383 | g_slist_length(di->decoder->opt_probes); |
19a90bab | 384 | if (di->dec_num_probes) { |
c9bfccc6 | 385 | if (!(di->dec_probemap = |
69075817 | 386 | g_try_malloc(sizeof(int) * di->dec_num_probes))) { |
a61ece20 | 387 | srd_err("Failed to g_malloc() probe map."); |
19a90bab BV |
388 | g_free(di); |
389 | return NULL; | |
390 | } | |
391 | for (i = 0; i < di->dec_num_probes; i++) | |
392 | di->dec_probemap[i] = i; | |
37b94c20 BV |
393 | di->data_unitsize = (di->dec_num_probes + 7) / 8; |
394 | /* | |
395 | * Will be used to prepare a sample at every iteration | |
396 | * of the instance's decode() method. | |
397 | */ | |
398 | if (!(di->probe_samples = g_try_malloc(di->dec_num_probes))) { | |
399 | srd_err("Failed to g_malloc() sample buffer."); | |
400 | g_free(di->dec_probemap); | |
401 | g_free(di); | |
402 | return NULL; | |
403 | } | |
f38ec285 | 404 | } |
f38ec285 | 405 | |
0bdadba2 | 406 | /* Create a new instance of this decoder class. */ |
a8b72b05 | 407 | if (!(di->py_inst = PyObject_CallObject(dec->py_dec, NULL))) { |
b2c19614 | 408 | if (PyErr_Occurred()) |
aafeeaea | 409 | srd_exception_catch("failed to create %s instance: ", |
69075817 | 410 | decoder_id); |
f38ec285 | 411 | g_free(di->dec_probemap); |
0bdadba2 | 412 | g_free(di); |
7ce7775c | 413 | return NULL; |
b2c19614 | 414 | } |
7ce7775c | 415 | |
38ff5046 | 416 | if (options && srd_inst_option_set(di, options) != SRD_OK) { |
f38ec285 | 417 | g_free(di->dec_probemap); |
0bdadba2 BV |
418 | g_free(di); |
419 | return NULL; | |
420 | } | |
b2c19614 | 421 | |
f38ec285 | 422 | /* Instance takes input from a frontend by default. */ |
32cfb920 | 423 | sess->di_list = g_slist_append(sess->di_list, di); |
f38ec285 | 424 | |
b2c19614 BV |
425 | return di; |
426 | } | |
427 | ||
582c8473 BV |
428 | /** |
429 | * Stack a decoder instance on top of another. | |
430 | * | |
32cfb920 | 431 | * @param sess The session holding the protocol decoder instances. |
4d2c7619 BV |
432 | * @param di_bottom The instance on top of which di_top will be stacked. |
433 | * @param di_top The instance to go on top. | |
582c8473 BV |
434 | * |
435 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 | 436 | * |
69075817 | 437 | * @since 0.3.0 |
582c8473 | 438 | */ |
32cfb920 | 439 | SRD_API int srd_inst_stack(struct srd_session *sess, |
4d2c7619 BV |
440 | struct srd_decoder_inst *di_bottom, |
441 | struct srd_decoder_inst *di_top) | |
7ce7775c | 442 | { |
32cfb920 BV |
443 | |
444 | if (session_is_valid(sess) != SRD_OK) { | |
445 | srd_err("Invalid session."); | |
446 | return SRD_ERR_ARG; | |
447 | } | |
448 | ||
4d2c7619 | 449 | if (!di_bottom || !di_top) { |
d906d3f9 | 450 | srd_err("Invalid from/to instance pair."); |
7ce7775c BV |
451 | return SRD_ERR_ARG; |
452 | } | |
453 | ||
4d2c7619 | 454 | if (g_slist_find(sess->di_list, di_top)) { |
2072ae0c | 455 | /* Remove from the unstacked list. */ |
4d2c7619 | 456 | sess->di_list = g_slist_remove(sess->di_list, di_top); |
7ce7775c BV |
457 | } |
458 | ||
7ce7775c | 459 | /* Stack on top of source di. */ |
4d2c7619 BV |
460 | di_bottom->next_di = g_slist_append(di_bottom->next_di, di_top); |
461 | ||
462 | srd_dbg("Stacked %s on top of %s.", di_top->inst_id, di_bottom->inst_id); | |
7ce7775c BV |
463 | |
464 | return SRD_OK; | |
465 | } | |
466 | ||
2072ae0c | 467 | /** |
361fdcaa UH |
468 | * Find a decoder instance by its instance ID. |
469 | * | |
470 | * Only the bottom level of instances are searched -- instances already stacked | |
471 | * on top of another one will not be found. | |
2072ae0c | 472 | * |
32cfb920 | 473 | * @param sess The session holding the protocol decoder instance. |
ed2306a6 | 474 | * @param inst_id The instance ID to be found. |
2072ae0c | 475 | * |
a8b72b05 | 476 | * @return Pointer to struct srd_decoder_inst, or NULL if not found. |
8c664ca2 | 477 | * |
69075817 | 478 | * @since 0.3.0 |
2072ae0c | 479 | */ |
32cfb920 BV |
480 | SRD_API struct srd_decoder_inst *srd_inst_find_by_id(struct srd_session *sess, |
481 | const char *inst_id) | |
7ce7775c BV |
482 | { |
483 | GSList *l; | |
a8b72b05 | 484 | struct srd_decoder_inst *tmp, *di; |
b2c19614 | 485 | |
32cfb920 BV |
486 | if (session_is_valid(sess) != SRD_OK) { |
487 | srd_err("Invalid session."); | |
488 | return NULL; | |
489 | } | |
490 | ||
7ce7775c | 491 | di = NULL; |
32cfb920 | 492 | for (l = sess->di_list; l; l = l->next) { |
7ce7775c | 493 | tmp = l->data; |
a8b72b05 | 494 | if (!strcmp(tmp->inst_id, inst_id)) { |
7ce7775c BV |
495 | di = tmp; |
496 | break; | |
497 | } | |
498 | } | |
499 | ||
500 | return di; | |
501 | } | |
502 | ||
32cfb920 BV |
503 | static struct srd_decoder_inst *srd_sess_inst_find_by_obj( |
504 | struct srd_session *sess, const GSList *stack, | |
505 | const PyObject *obj) | |
506 | { | |
507 | const GSList *l; | |
508 | struct srd_decoder_inst *tmp, *di; | |
509 | ||
510 | if (session_is_valid(sess) != SRD_OK) { | |
511 | srd_err("Invalid session."); | |
512 | return NULL; | |
513 | } | |
514 | ||
515 | di = NULL; | |
516 | for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) { | |
517 | tmp = l->data; | |
518 | if (tmp->py_inst == obj) | |
519 | di = tmp; | |
520 | else if (tmp->next_di) | |
521 | di = srd_sess_inst_find_by_obj(sess, tmp->next_di, obj); | |
522 | } | |
523 | ||
524 | return di; | |
525 | } | |
526 | ||
2072ae0c | 527 | /** |
361fdcaa UH |
528 | * Find a decoder instance by its Python object. |
529 | * | |
530 | * I.e. find that instance's instantiation of the sigrokdecode.Decoder class. | |
32cfb920 BV |
531 | * This will recurse to find the instance anywhere in the stack tree of all |
532 | * sessions. | |
2072ae0c | 533 | * |
361fdcaa UH |
534 | * @param stack Pointer to a GSList of struct srd_decoder_inst, indicating the |
535 | * stack to search. To start searching at the bottom level of | |
536 | * decoder instances, pass NULL. | |
511e2123 | 537 | * @param obj The Python class instantiation. |
2072ae0c | 538 | * |
a8b72b05 | 539 | * @return Pointer to struct srd_decoder_inst, or NULL if not found. |
57790bc8 UH |
540 | * |
541 | * @private | |
8c664ca2 UH |
542 | * |
543 | * @since 0.1.0 | |
2072ae0c | 544 | */ |
abeeed8b | 545 | SRD_PRIV struct srd_decoder_inst *srd_inst_find_by_obj(const GSList *stack, |
0ff2d191 | 546 | const PyObject *obj) |
2072ae0c | 547 | { |
32cfb920 BV |
548 | struct srd_decoder_inst *di; |
549 | struct srd_session *sess; | |
550 | GSList *l; | |
2072ae0c BV |
551 | |
552 | di = NULL; | |
32cfb920 BV |
553 | for (l = sessions; di == NULL && l != NULL; l = l->next) { |
554 | sess = l->data; | |
555 | di = srd_sess_inst_find_by_obj(sess, stack, obj); | |
2072ae0c BV |
556 | } |
557 | ||
558 | return di; | |
559 | } | |
560 | ||
57790bc8 | 561 | /** @private */ |
ed416497 | 562 | SRD_PRIV int srd_inst_start(struct srd_decoder_inst *di) |
b2c19614 | 563 | { |
ed416497 | 564 | PyObject *py_res; |
2072ae0c | 565 | GSList *l; |
a8b72b05 | 566 | struct srd_decoder_inst *next_di; |
ed416497 | 567 | int ret; |
b2c19614 | 568 | |
7a1712c4 | 569 | srd_dbg("Calling start() method on protocol decoder instance %s.", |
ed416497 | 570 | di->inst_id); |
b2c19614 | 571 | |
ed416497 | 572 | if (!(py_res = PyObject_CallMethod(di->py_inst, "start", NULL))) { |
aafeeaea | 573 | srd_exception_catch("Protocol decoder instance %s: ", |
ed416497 | 574 | di->inst_id); |
7ce7775c BV |
575 | return SRD_ERR_PYTHON; |
576 | } | |
f38ec285 | 577 | Py_DecRef(py_res); |
7ce7775c | 578 | |
ed416497 | 579 | /* Start all the PDs stacked on top of this one. */ |
2072ae0c BV |
580 | for (l = di->next_di; l; l = l->next) { |
581 | next_di = l->data; | |
ed416497 BV |
582 | if ((ret = srd_inst_start(next_di)) != SRD_OK) |
583 | return ret; | |
2072ae0c BV |
584 | } |
585 | ||
b2c19614 BV |
586 | return SRD_OK; |
587 | } | |
588 | ||
b2c19614 BV |
589 | /** |
590 | * Run the specified decoder function. | |
591 | * | |
ed416497 | 592 | * @param di The decoder instance to call. Must not be NULL. |
d906d3f9 | 593 | * @param start_samplenum The starting sample number for the buffer's sample |
7a1712c4 | 594 | * set, relative to the start of capture. |
ed416497 BV |
595 | * @param end_samplenum The ending sample number for the buffer's sample |
596 | * set, relative to the start of capture. | |
7a1712c4 UH |
597 | * @param inbuf The buffer to decode. Must not be NULL. |
598 | * @param inbuflen Length of the buffer. Must be > 0. | |
b2c19614 BV |
599 | * |
600 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
57790bc8 UH |
601 | * |
602 | * @private | |
8c664ca2 UH |
603 | * |
604 | * @since 0.1.0 | |
b2c19614 | 605 | */ |
ed416497 BV |
606 | SRD_PRIV int srd_inst_decode(const struct srd_decoder_inst *di, |
607 | uint64_t start_samplenum, uint64_t end_samplenum, | |
608 | const uint8_t *inbuf, uint64_t inbuflen) | |
b2c19614 | 609 | { |
d906d3f9 | 610 | PyObject *py_res; |
bc5f5a43 | 611 | srd_logic *logic; |
b2c19614 | 612 | |
74bb0af5 UH |
613 | srd_dbg("Calling decode() on instance %s with %" PRIu64 " bytes " |
614 | "starting at sample %" PRIu64 ".", di->inst_id, inbuflen, | |
615 | start_samplenum); | |
b2c19614 | 616 | |
d906d3f9 | 617 | /* Return an error upon unusable input. */ |
7a1712c4 UH |
618 | if (!di) { |
619 | srd_dbg("empty decoder instance"); | |
d906d3f9 BV |
620 | return SRD_ERR_ARG; |
621 | } | |
7a1712c4 UH |
622 | if (!inbuf) { |
623 | srd_dbg("NULL buffer pointer"); | |
d906d3f9 BV |
624 | return SRD_ERR_ARG; |
625 | } | |
626 | if (inbuflen == 0) { | |
7a1712c4 | 627 | srd_dbg("empty buffer"); |
d906d3f9 BV |
628 | return SRD_ERR_ARG; |
629 | } | |
b2c19614 | 630 | |
361fdcaa UH |
631 | /* |
632 | * Create new srd_logic object. Each iteration around the PD's loop | |
d906d3f9 BV |
633 | * will fill one sample into this object. |
634 | */ | |
bc5f5a43 BV |
635 | logic = PyObject_New(srd_logic, &srd_logic_type); |
636 | Py_INCREF(logic); | |
abeeed8b | 637 | logic->di = (struct srd_decoder_inst *)di; |
86528298 | 638 | logic->start_samplenum = start_samplenum; |
bc5f5a43 | 639 | logic->itercnt = 0; |
abeeed8b | 640 | logic->inbuf = (uint8_t *)inbuf; |
bc5f5a43 BV |
641 | logic->inbuflen = inbuflen; |
642 | logic->sample = PyList_New(2); | |
643 | Py_INCREF(logic->sample); | |
644 | ||
a8b72b05 | 645 | Py_IncRef(di->py_inst); |
a8b72b05 | 646 | if (!(py_res = PyObject_CallMethod(di->py_inst, "decode", |
ed416497 BV |
647 | "KKO", start_samplenum, end_samplenum, logic))) { |
648 | srd_exception_catch("Protocol decoder instance %s: ", di->inst_id); | |
0ff2d191 | 649 | return SRD_ERR_PYTHON; |
b2c19614 | 650 | } |
d906d3f9 | 651 | Py_DecRef(py_res); |
bc5f5a43 | 652 | |
b2c19614 BV |
653 | return SRD_OK; |
654 | } | |
655 | ||
57790bc8 | 656 | /** @private */ |
12243c22 | 657 | SRD_PRIV void srd_inst_free(struct srd_decoder_inst *di) |
fa12a21e BV |
658 | { |
659 | GSList *l; | |
660 | struct srd_pd_output *pdo; | |
661 | ||
a8b72b05 | 662 | srd_dbg("Freeing instance %s", di->inst_id); |
fa12a21e | 663 | |
a8b72b05 BV |
664 | Py_DecRef(di->py_inst); |
665 | g_free(di->inst_id); | |
fa12a21e BV |
666 | g_free(di->dec_probemap); |
667 | g_slist_free(di->next_di); | |
668 | for (l = di->pd_output; l; l = l->next) { | |
669 | pdo = l->data; | |
670 | g_free(pdo->proto_id); | |
671 | g_free(pdo); | |
672 | } | |
673 | g_slist_free(di->pd_output); | |
e592ac89 | 674 | g_free(di); |
fa12a21e BV |
675 | } |
676 | ||
57790bc8 | 677 | /** @private */ |
32cfb920 | 678 | SRD_PRIV void srd_inst_free_all(struct srd_session *sess, GSList *stack) |
fa12a21e BV |
679 | { |
680 | GSList *l; | |
a8b72b05 | 681 | struct srd_decoder_inst *di; |
fa12a21e | 682 | |
32cfb920 BV |
683 | if (session_is_valid(sess) != SRD_OK) { |
684 | srd_err("Invalid session."); | |
685 | return; | |
686 | } | |
687 | ||
fa12a21e | 688 | di = NULL; |
32cfb920 | 689 | for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) { |
fa12a21e BV |
690 | di = l->data; |
691 | if (di->next_di) | |
32cfb920 | 692 | srd_inst_free_all(sess, di->next_di); |
a8b72b05 | 693 | srd_inst_free(di); |
fa12a21e BV |
694 | } |
695 | if (!stack) { | |
32cfb920 BV |
696 | g_slist_free(sess->di_list); |
697 | sess->di_list = NULL; | |
fa12a21e | 698 | } |
fa12a21e | 699 | } |
b2c19614 | 700 | |
4895418c UH |
701 | /** @} */ |
702 |