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