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