]>
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 | ||
36784362 | 21 | #include <config.h> |
f6c7eade MC |
22 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
23 | #include "libsigrokdecode.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 | |
04383ea8 GS |
33 | static void srd_inst_join_decode_thread(struct srd_decoder_inst *di); |
34 | static void srd_inst_reset_state(struct srd_decoder_inst *di); | |
63bbdb44 | 35 | SRD_PRIV void oldpins_array_seed(struct srd_decoder_inst *di); |
04383ea8 GS |
36 | SRD_PRIV void oldpins_array_free(struct srd_decoder_inst *di); |
37 | ||
57790bc8 UH |
38 | /** @endcond */ |
39 | ||
b2c19614 | 40 | /** |
190b71cf | 41 | * @file |
8c664ca2 | 42 | * |
190b71cf | 43 | * Decoder instance handling. |
b2c19614 | 44 | */ |
4895418c UH |
45 | |
46 | /** | |
47 | * @defgroup grp_instances Decoder instances | |
48 | * | |
49 | * Decoder instance handling. | |
50 | * | |
51 | * @{ | |
52 | */ | |
53 | ||
7ce7775c | 54 | /** |
b33b8aa5 | 55 | * Set one or more options in a decoder instance. |
0bdadba2 | 56 | * |
361fdcaa UH |
57 | * Handled options are removed from the hash. |
58 | * | |
0bdadba2 BV |
59 | * @param di Decoder instance. |
60 | * @param options A GHashTable of options to set. | |
7ce7775c | 61 | * |
0bdadba2 | 62 | * @return SRD_OK upon success, a (negative) error code otherwise. |
8c664ca2 UH |
63 | * |
64 | * @since 0.1.0 | |
0bdadba2 | 65 | */ |
b33b8aa5 | 66 | SRD_API int srd_inst_option_set(struct srd_decoder_inst *di, |
0ff2d191 | 67 | GHashTable *options) |
0bdadba2 | 68 | { |
d841d5b2 | 69 | struct srd_decoder_option *sdo; |
84c1c0b5 | 70 | PyObject *py_di_options, *py_optval; |
2f395bff | 71 | GVariant *value; |
d841d5b2 UH |
72 | GSList *l; |
73 | double val_double; | |
2f395bff | 74 | gint64 val_int; |
84c1c0b5 | 75 | int ret; |
2f395bff | 76 | const char *val_str; |
514b2edc | 77 | PyGILState_STATE gstate; |
0bdadba2 | 78 | |
3af0e345 UH |
79 | if (!di) { |
80 | srd_err("Invalid decoder instance."); | |
81 | return SRD_ERR_ARG; | |
82 | } | |
83 | ||
84 | if (!options) { | |
85 | srd_err("Invalid options GHashTable."); | |
86 | return SRD_ERR_ARG; | |
87 | } | |
88 | ||
514b2edc UH |
89 | gstate = PyGILState_Ensure(); |
90 | ||
c9bfccc6 | 91 | if (!PyObject_HasAttrString(di->decoder->py_dec, "options")) { |
0bdadba2 | 92 | /* Decoder has no options. */ |
514b2edc | 93 | PyGILState_Release(gstate); |
e431d9cc BV |
94 | if (g_hash_table_size(options) == 0) { |
95 | /* No options provided. */ | |
96 | return SRD_OK; | |
97 | } else { | |
98 | srd_err("Protocol decoder has no options."); | |
99 | return SRD_ERR_ARG; | |
100 | } | |
0bdadba2 | 101 | return SRD_OK; |
e431d9cc | 102 | } |
0bdadba2 BV |
103 | |
104 | ret = SRD_ERR_PYTHON; | |
d841d5b2 | 105 | py_optval = NULL; |
119d6258 BV |
106 | |
107 | /* | |
84c1c0b5 | 108 | * The 'options' tuple is a class variable, but we need to |
119d6258 BV |
109 | * change it. Changing it directly will affect the entire class, |
110 | * so we need to create a new object for it, and populate that | |
111 | * instead. | |
112 | */ | |
a8b72b05 | 113 | if (!(py_di_options = PyObject_GetAttrString(di->py_inst, "options"))) |
0bdadba2 | 114 | goto err_out; |
119d6258 BV |
115 | Py_DECREF(py_di_options); |
116 | py_di_options = PyDict_New(); | |
117 | PyObject_SetAttrString(di->py_inst, "options", py_di_options); | |
0bdadba2 | 118 | |
d841d5b2 UH |
119 | for (l = di->decoder->options; l; l = l->next) { |
120 | sdo = l->data; | |
121 | if ((value = g_hash_table_lookup(options, sdo->id))) { | |
122 | /* A value was supplied for this option. */ | |
123 | if (!g_variant_type_equal(g_variant_get_type(value), | |
bd6594c0 | 124 | g_variant_get_type(sdo->def))) { |
d841d5b2 UH |
125 | srd_err("Option '%s' should have the same type " |
126 | "as the default value.", sdo->id); | |
127 | goto err_out; | |
128 | } | |
129 | } else { | |
130 | /* Use default for this option. */ | |
131 | value = sdo->def; | |
132 | } | |
133 | if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) { | |
134 | val_str = g_variant_get_string(value, NULL); | |
135 | if (!(py_optval = PyUnicode_FromString(val_str))) { | |
136 | /* Some UTF-8 encoding error. */ | |
137 | PyErr_Clear(); | |
138 | srd_err("Option '%s' requires a UTF-8 string value.", sdo->id); | |
139 | goto err_out; | |
140 | } | |
141 | } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) { | |
142 | val_int = g_variant_get_int64(value); | |
143 | if (!(py_optval = PyLong_FromLong(val_int))) { | |
144 | /* ValueError Exception */ | |
145 | PyErr_Clear(); | |
146 | srd_err("Option '%s' has invalid integer value.", sdo->id); | |
147 | goto err_out; | |
148 | } | |
149 | } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) { | |
150 | val_double = g_variant_get_double(value); | |
151 | if (!(py_optval = PyFloat_FromDouble(val_double))) { | |
152 | /* ValueError Exception */ | |
153 | PyErr_Clear(); | |
154 | srd_err("Option '%s' has invalid float value.", | |
155 | sdo->id); | |
156 | goto err_out; | |
157 | } | |
158 | } | |
84c1c0b5 | 159 | if (PyDict_SetItemString(py_di_options, sdo->id, py_optval) == -1) |
0bdadba2 | 160 | goto err_out; |
d841d5b2 UH |
161 | /* Not harmful even if we used the default. */ |
162 | g_hash_table_remove(options, sdo->id); | |
163 | } | |
164 | if (g_hash_table_size(options) != 0) | |
165 | srd_warn("Unknown options specified for '%s'", di->inst_id); | |
0bdadba2 BV |
166 | |
167 | ret = SRD_OK; | |
168 | ||
169 | err_out: | |
84c1c0b5 | 170 | Py_XDECREF(py_optval); |
7fc7bde6 | 171 | if (PyErr_Occurred()) { |
201a85a8 | 172 | srd_exception_catch("Stray exception in srd_inst_option_set()"); |
7fc7bde6 BV |
173 | ret = SRD_ERR_PYTHON; |
174 | } | |
514b2edc | 175 | PyGILState_Release(gstate); |
0bdadba2 BV |
176 | |
177 | return ret; | |
178 | } | |
179 | ||
7969d803 | 180 | /* Helper GComparefunc for g_slist_find_custom() in srd_inst_channel_set_all(). */ |
6a15597a UH |
181 | static gint compare_channel_id(const struct srd_channel *pdch, |
182 | const char *channel_id) | |
f38ec285 | 183 | { |
6a15597a | 184 | return strcmp(pdch->id, channel_id); |
f38ec285 BV |
185 | } |
186 | ||
0bdadba2 | 187 | /** |
6a15597a | 188 | * Set all channels in a decoder instance. |
b33b8aa5 | 189 | * |
6a15597a UH |
190 | * This function sets _all_ channels for the specified decoder instance, i.e., |
191 | * it overwrites any channels that were already defined (if any). | |
0bdadba2 BV |
192 | * |
193 | * @param di Decoder instance. | |
6a15597a UH |
194 | * @param new_channels A GHashTable of channels to set. Key is channel name, |
195 | * value is the channel number. Samples passed to this | |
196 | * instance will be arranged in this order. | |
12243c22 | 197 | * |
0bdadba2 | 198 | * @return SRD_OK upon success, a (negative) error code otherwise. |
8c664ca2 | 199 | * |
cda2d36c | 200 | * @since 0.4.0 |
0bdadba2 | 201 | */ |
6a15597a | 202 | SRD_API int srd_inst_channel_set_all(struct srd_decoder_inst *di, |
cda2d36c | 203 | GHashTable *new_channels) |
0bdadba2 | 204 | { |
6a15597a | 205 | GVariant *channel_val; |
f38ec285 BV |
206 | GList *l; |
207 | GSList *sl; | |
6a15597a UH |
208 | struct srd_channel *pdch; |
209 | int *new_channelmap, new_channelnum, num_required_channels, i; | |
210 | char *channel_id; | |
0bdadba2 | 211 | |
cda2d36c UH |
212 | srd_dbg("Setting channels for instance %s with list of %d channels.", |
213 | di->inst_id, g_hash_table_size(new_channels)); | |
ce46beed | 214 | |
6a15597a UH |
215 | if (g_hash_table_size(new_channels) == 0) |
216 | /* No channels provided. */ | |
0bdadba2 BV |
217 | return SRD_OK; |
218 | ||
6a15597a UH |
219 | if (di->dec_num_channels == 0) { |
220 | /* Decoder has no channels. */ | |
221 | srd_err("Protocol decoder %s has no channels to define.", | |
c9bfccc6 | 222 | di->decoder->name); |
f38ec285 BV |
223 | return SRD_ERR_ARG; |
224 | } | |
0bdadba2 | 225 | |
8f14effc | 226 | new_channelmap = g_malloc0(sizeof(int) * di->dec_num_channels); |
0bdadba2 | 227 | |
38ff5046 | 228 | /* |
6a15597a UH |
229 | * For now, map all indexes to channel -1 (can be overridden later). |
230 | * This -1 is interpreted as an unspecified channel later. | |
38ff5046 | 231 | */ |
6a15597a UH |
232 | for (i = 0; i < di->dec_num_channels; i++) |
233 | new_channelmap[i] = -1; | |
234 | ||
235 | for (l = g_hash_table_get_keys(new_channels); l; l = l->next) { | |
236 | channel_id = l->data; | |
237 | channel_val = g_hash_table_lookup(new_channels, channel_id); | |
238 | if (!g_variant_is_of_type(channel_val, G_VARIANT_TYPE_INT32)) { | |
239 | /* Channel name was specified without a value. */ | |
240 | srd_err("No channel number was specified for %s.", | |
241 | channel_id); | |
242 | g_free(new_channelmap); | |
be873260 BV |
243 | return SRD_ERR_ARG; |
244 | } | |
6a15597a | 245 | new_channelnum = g_variant_get_int32(channel_val); |
6a15597a UH |
246 | if (!(sl = g_slist_find_custom(di->decoder->channels, channel_id, |
247 | (GCompareFunc)compare_channel_id))) { | |
248 | /* Fall back on optional channels. */ | |
249 | if (!(sl = g_slist_find_custom(di->decoder->opt_channels, | |
d841d5b2 | 250 | channel_id, (GCompareFunc)compare_channel_id))) { |
6a15597a | 251 | srd_err("Protocol decoder %s has no channel " |
d841d5b2 | 252 | "'%s'.", di->decoder->name, channel_id); |
6a15597a | 253 | g_free(new_channelmap); |
f38ec285 BV |
254 | return SRD_ERR_ARG; |
255 | } | |
256 | } | |
6a15597a UH |
257 | pdch = sl->data; |
258 | new_channelmap[pdch->order] = new_channelnum; | |
5b2595b5 | 259 | srd_dbg("Setting channel mapping: %s (PD ch idx %d) = input data ch idx %d.", |
6a15597a | 260 | pdch->id, pdch->order, new_channelnum); |
38ff5046 UH |
261 | } |
262 | ||
6a15597a UH |
263 | srd_dbg("Final channel map:"); |
264 | num_required_channels = g_slist_length(di->decoder->channels); | |
265 | for (i = 0; i < di->dec_num_channels; i++) { | |
3389df7d UH |
266 | GSList *ll = g_slist_nth(di->decoder->channels, i); |
267 | if (!ll) | |
268 | ll = g_slist_nth(di->decoder->opt_channels, | |
5b2595b5 | 269 | i - num_required_channels); |
3389df7d | 270 | pdch = ll->data; |
5b2595b5 UH |
271 | srd_dbg(" - PD ch idx %d (%s) = input data ch idx %d (%s)", i, |
272 | pdch->id, new_channelmap[i], | |
d841d5b2 | 273 | (i < num_required_channels) ? "required" : "optional"); |
f38ec285 | 274 | } |
38ff5046 | 275 | |
6a15597a UH |
276 | /* Report an error if not all required channels were specified. */ |
277 | for (i = 0; i < num_required_channels; i++) { | |
278 | if (new_channelmap[i] != -1) | |
9bf7f71c | 279 | continue; |
6a15597a UH |
280 | pdch = g_slist_nth(di->decoder->channels, i)->data; |
281 | srd_err("Required channel '%s' (index %d) was not specified.", | |
282 | pdch->id, i); | |
25d35761 | 283 | g_free(new_channelmap); |
9bf7f71c UH |
284 | return SRD_ERR; |
285 | } | |
286 | ||
6a15597a UH |
287 | g_free(di->dec_channelmap); |
288 | di->dec_channelmap = new_channelmap; | |
0bdadba2 | 289 | |
f38ec285 | 290 | return SRD_OK; |
0bdadba2 BV |
291 | } |
292 | ||
293 | /** | |
294 | * Create a new protocol decoder instance. | |
7ce7775c | 295 | * |
32cfb920 | 296 | * @param sess The session holding the protocol decoder instance. |
38ff5046 | 297 | * @param decoder_id Decoder 'id' field. |
0bdadba2 | 298 | * @param options GHashtable of options which override the defaults set in |
38ff5046 | 299 | * the decoder class. May be NULL. |
12243c22 | 300 | * |
a8b72b05 | 301 | * @return Pointer to a newly allocated struct srd_decoder_inst, or |
c9bfccc6 | 302 | * NULL in case of failure. |
8c664ca2 | 303 | * |
69075817 | 304 | * @since 0.3.0 |
7ce7775c | 305 | */ |
32cfb920 BV |
306 | SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess, |
307 | const char *decoder_id, GHashTable *options) | |
b2c19614 | 308 | { |
c9bfccc6 | 309 | int i; |
b2c19614 | 310 | struct srd_decoder *dec; |
a8b72b05 BV |
311 | struct srd_decoder_inst *di; |
312 | char *inst_id; | |
514b2edc | 313 | PyGILState_STATE gstate; |
b2c19614 | 314 | |
3262ef02 | 315 | i = 1; |
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 | |
077fa8ac | 328 | di = g_malloc0(sizeof(struct srd_decoder_inst)); |
0bdadba2 | 329 | |
b2c19614 | 330 | di->decoder = dec; |
32cfb920 | 331 | di->sess = sess; |
3262ef02 | 332 | |
38ff5046 UH |
333 | if (options) { |
334 | inst_id = g_hash_table_lookup(options, "id"); | |
3262ef02 KP |
335 | if (inst_id) |
336 | di->inst_id = g_strdup(inst_id); | |
38ff5046 | 337 | g_hash_table_remove(options, "id"); |
3262ef02 KP |
338 | } |
339 | ||
340 | /* Create a unique instance ID (as none was provided). */ | |
341 | if (!di->inst_id) { | |
342 | di->inst_id = g_strdup_printf("%s-%d", decoder_id, i++); | |
343 | while (srd_inst_find_by_id(sess, di->inst_id)) { | |
344 | g_free(di->inst_id); | |
345 | di->inst_id = g_strdup_printf("%s-%d", decoder_id, i++); | |
346 | } | |
347 | } | |
b2c19614 | 348 | |
361fdcaa | 349 | /* |
6a15597a | 350 | * Prepare a default channel map, where samples come in the |
f38ec285 BV |
351 | * order in which the decoder class defined them. |
352 | */ | |
6a15597a UH |
353 | di->dec_num_channels = g_slist_length(di->decoder->channels) + |
354 | g_slist_length(di->decoder->opt_channels); | |
355 | if (di->dec_num_channels) { | |
077fa8ac UH |
356 | di->dec_channelmap = |
357 | g_malloc(sizeof(int) * di->dec_num_channels); | |
6a15597a UH |
358 | for (i = 0; i < di->dec_num_channels; i++) |
359 | di->dec_channelmap[i] = i; | |
37b94c20 BV |
360 | /* |
361 | * Will be used to prepare a sample at every iteration | |
362 | * of the instance's decode() method. | |
363 | */ | |
077fa8ac | 364 | di->channel_samples = g_malloc(di->dec_num_channels); |
f38ec285 | 365 | } |
f38ec285 | 366 | |
97b874bd | 367 | /* Default to the initial pins being the same as in sample 0. */ |
63bbdb44 | 368 | oldpins_array_seed(di); |
97b874bd | 369 | |
514b2edc UH |
370 | gstate = PyGILState_Ensure(); |
371 | ||
0bdadba2 | 372 | /* Create a new instance of this decoder class. */ |
a8b72b05 | 373 | if (!(di->py_inst = PyObject_CallObject(dec->py_dec, NULL))) { |
b2c19614 | 374 | if (PyErr_Occurred()) |
201a85a8 | 375 | srd_exception_catch("Failed to create %s instance", |
69075817 | 376 | decoder_id); |
514b2edc | 377 | PyGILState_Release(gstate); |
6a15597a | 378 | g_free(di->dec_channelmap); |
0bdadba2 | 379 | g_free(di); |
7ce7775c | 380 | return NULL; |
b2c19614 | 381 | } |
7ce7775c | 382 | |
514b2edc UH |
383 | PyGILState_Release(gstate); |
384 | ||
38ff5046 | 385 | if (options && srd_inst_option_set(di, options) != SRD_OK) { |
6a15597a | 386 | g_free(di->dec_channelmap); |
0bdadba2 BV |
387 | g_free(di); |
388 | return NULL; | |
389 | } | |
b2c19614 | 390 | |
21dfd91d UH |
391 | di->condition_list = NULL; |
392 | di->match_array = NULL; | |
4564e8e5 UH |
393 | di->abs_start_samplenum = 0; |
394 | di->abs_end_samplenum = 0; | |
21dfd91d UH |
395 | di->inbuf = NULL; |
396 | di->inbuflen = 0; | |
4564e8e5 | 397 | di->abs_cur_samplenum = 0; |
21dfd91d UH |
398 | di->thread_handle = NULL; |
399 | di->got_new_samples = FALSE; | |
400 | di->handled_all_samples = FALSE; | |
04383ea8 GS |
401 | di->want_wait_terminate = FALSE; |
402 | ||
403 | /* | |
404 | * Strictly speaking initialization of statically allocated | |
405 | * condition and mutex variables (or variables allocated on the | |
406 | * stack) is not required, but won't harm either. Explicitly | |
407 | * running init() will better match subsequent clear() calls. | |
408 | */ | |
409 | g_cond_init(&di->got_new_samples_cond); | |
410 | g_cond_init(&di->handled_all_samples_cond); | |
411 | g_mutex_init(&di->data_mutex); | |
21dfd91d | 412 | |
f38ec285 | 413 | /* Instance takes input from a frontend by default. */ |
32cfb920 | 414 | sess->di_list = g_slist_append(sess->di_list, di); |
3262ef02 | 415 | srd_dbg("Created new %s instance with ID %s.", decoder_id, di->inst_id); |
f38ec285 | 416 | |
b2c19614 BV |
417 | return di; |
418 | } | |
419 | ||
04383ea8 GS |
420 | static void srd_inst_join_decode_thread(struct srd_decoder_inst *di) |
421 | { | |
422 | if (!di) | |
423 | return; | |
424 | if (!di->thread_handle) | |
425 | return; | |
426 | ||
427 | srd_dbg("%s: Joining decoder thread.", di->inst_id); | |
428 | ||
429 | /* | |
430 | * Terminate potentially running threads which still | |
431 | * execute the decoder instance's decode() method. | |
432 | */ | |
433 | srd_dbg("%s: Raising want_term, sending got_new.", di->inst_id); | |
434 | g_mutex_lock(&di->data_mutex); | |
435 | di->want_wait_terminate = TRUE; | |
436 | g_cond_signal(&di->got_new_samples_cond); | |
437 | g_mutex_unlock(&di->data_mutex); | |
438 | ||
439 | srd_dbg("%s: Running join().", di->inst_id); | |
440 | (void)g_thread_join(di->thread_handle); | |
441 | srd_dbg("%s: Call to join() done.", di->inst_id); | |
442 | di->thread_handle = NULL; | |
443 | ||
444 | /* | |
445 | * Reset condition and mutex variables, such that next | |
446 | * operations on them will find them in a clean state. | |
447 | */ | |
448 | g_cond_clear(&di->got_new_samples_cond); | |
449 | g_cond_init(&di->got_new_samples_cond); | |
450 | g_cond_clear(&di->handled_all_samples_cond); | |
451 | g_cond_init(&di->handled_all_samples_cond); | |
452 | g_mutex_clear(&di->data_mutex); | |
453 | g_mutex_init(&di->data_mutex); | |
454 | } | |
455 | ||
456 | static void srd_inst_reset_state(struct srd_decoder_inst *di) | |
457 | { | |
458 | if (!di) | |
459 | return; | |
460 | ||
461 | srd_dbg("%s: Resetting decoder state.", di->inst_id); | |
462 | ||
7969d803 | 463 | /* Reset internal state of the decoder. */ |
04383ea8 GS |
464 | condition_list_free(di); |
465 | match_array_free(di); | |
466 | di->abs_start_samplenum = 0; | |
467 | di->abs_end_samplenum = 0; | |
468 | di->inbuf = NULL; | |
469 | di->inbuflen = 0; | |
470 | di->abs_cur_samplenum = 0; | |
471 | oldpins_array_free(di); | |
472 | di->got_new_samples = FALSE; | |
473 | di->handled_all_samples = FALSE; | |
474 | di->want_wait_terminate = FALSE; | |
475 | /* Conditions and mutex got reset after joining the thread. */ | |
476 | } | |
477 | ||
582c8473 BV |
478 | /** |
479 | * Stack a decoder instance on top of another. | |
480 | * | |
32cfb920 | 481 | * @param sess The session holding the protocol decoder instances. |
4d2c7619 BV |
482 | * @param di_bottom The instance on top of which di_top will be stacked. |
483 | * @param di_top The instance to go on top. | |
582c8473 BV |
484 | * |
485 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 | 486 | * |
69075817 | 487 | * @since 0.3.0 |
582c8473 | 488 | */ |
32cfb920 | 489 | SRD_API int srd_inst_stack(struct srd_session *sess, |
4d2c7619 BV |
490 | struct srd_decoder_inst *di_bottom, |
491 | struct srd_decoder_inst *di_top) | |
7ce7775c | 492 | { |
32cfb920 BV |
493 | if (session_is_valid(sess) != SRD_OK) { |
494 | srd_err("Invalid session."); | |
495 | return SRD_ERR_ARG; | |
496 | } | |
497 | ||
4d2c7619 | 498 | if (!di_bottom || !di_top) { |
d906d3f9 | 499 | srd_err("Invalid from/to instance pair."); |
7ce7775c BV |
500 | return SRD_ERR_ARG; |
501 | } | |
502 | ||
4d2c7619 | 503 | if (g_slist_find(sess->di_list, di_top)) { |
2072ae0c | 504 | /* Remove from the unstacked list. */ |
4d2c7619 | 505 | sess->di_list = g_slist_remove(sess->di_list, di_top); |
7ce7775c BV |
506 | } |
507 | ||
7ce7775c | 508 | /* Stack on top of source di. */ |
4d2c7619 BV |
509 | di_bottom->next_di = g_slist_append(di_bottom->next_di, di_top); |
510 | ||
d841d5b2 | 511 | srd_dbg("Stacked %s onto %s.", di_top->inst_id, di_bottom->inst_id); |
7ce7775c BV |
512 | |
513 | return SRD_OK; | |
514 | } | |
515 | ||
79d0013e KP |
516 | /** |
517 | * Search a decoder instance and its stack for instance ID. | |
518 | * | |
519 | * @param[in] inst_id ID to search for. | |
520 | * @param[in] stack A decoder instance, potentially with stacked instances. | |
521 | * | |
522 | * @return The matching instance, or NULL. | |
523 | */ | |
524 | static struct srd_decoder_inst *srd_inst_find_by_id_stack(const char *inst_id, | |
525 | struct srd_decoder_inst *stack) | |
526 | { | |
527 | const GSList *l; | |
528 | struct srd_decoder_inst *tmp, *di; | |
529 | ||
530 | if (!strcmp(stack->inst_id, inst_id)) | |
531 | return stack; | |
532 | ||
533 | /* Otherwise, look recursively in our stack. */ | |
534 | di = NULL; | |
535 | if (stack->next_di) { | |
536 | for (l = stack->next_di; l; l = l->next) { | |
537 | tmp = l->data; | |
538 | if (!strcmp(tmp->inst_id, inst_id)) { | |
539 | di = tmp; | |
540 | break; | |
541 | } | |
542 | } | |
543 | } | |
544 | ||
545 | return di; | |
546 | } | |
547 | ||
2072ae0c | 548 | /** |
361fdcaa UH |
549 | * Find a decoder instance by its instance ID. |
550 | * | |
79d0013e KP |
551 | * This will recurse to find the instance anywhere in the stack tree of the |
552 | * given session. | |
2072ae0c | 553 | * |
32cfb920 | 554 | * @param sess The session holding the protocol decoder instance. |
ed2306a6 | 555 | * @param inst_id The instance ID to be found. |
2072ae0c | 556 | * |
a8b72b05 | 557 | * @return Pointer to struct srd_decoder_inst, or NULL if not found. |
8c664ca2 | 558 | * |
69075817 | 559 | * @since 0.3.0 |
2072ae0c | 560 | */ |
32cfb920 BV |
561 | SRD_API struct srd_decoder_inst *srd_inst_find_by_id(struct srd_session *sess, |
562 | const char *inst_id) | |
7ce7775c BV |
563 | { |
564 | GSList *l; | |
a8b72b05 | 565 | struct srd_decoder_inst *tmp, *di; |
b2c19614 | 566 | |
32cfb920 BV |
567 | if (session_is_valid(sess) != SRD_OK) { |
568 | srd_err("Invalid session."); | |
569 | return NULL; | |
570 | } | |
571 | ||
7ce7775c | 572 | di = NULL; |
32cfb920 | 573 | for (l = sess->di_list; l; l = l->next) { |
7ce7775c | 574 | tmp = l->data; |
9bac00e0 | 575 | if ((di = srd_inst_find_by_id_stack(inst_id, tmp)) != NULL) |
7ce7775c | 576 | break; |
7ce7775c BV |
577 | } |
578 | ||
579 | return di; | |
580 | } | |
581 | ||
32cfb920 BV |
582 | static struct srd_decoder_inst *srd_sess_inst_find_by_obj( |
583 | struct srd_session *sess, const GSList *stack, | |
584 | const PyObject *obj) | |
585 | { | |
586 | const GSList *l; | |
587 | struct srd_decoder_inst *tmp, *di; | |
588 | ||
589 | if (session_is_valid(sess) != SRD_OK) { | |
590 | srd_err("Invalid session."); | |
591 | return NULL; | |
592 | } | |
593 | ||
594 | di = NULL; | |
595 | for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) { | |
596 | tmp = l->data; | |
597 | if (tmp->py_inst == obj) | |
598 | di = tmp; | |
599 | else if (tmp->next_di) | |
600 | di = srd_sess_inst_find_by_obj(sess, tmp->next_di, obj); | |
601 | } | |
602 | ||
603 | return di; | |
604 | } | |
605 | ||
2072ae0c | 606 | /** |
361fdcaa UH |
607 | * Find a decoder instance by its Python object. |
608 | * | |
609 | * I.e. find that instance's instantiation of the sigrokdecode.Decoder class. | |
32cfb920 BV |
610 | * This will recurse to find the instance anywhere in the stack tree of all |
611 | * sessions. | |
2072ae0c | 612 | * |
361fdcaa UH |
613 | * @param stack Pointer to a GSList of struct srd_decoder_inst, indicating the |
614 | * stack to search. To start searching at the bottom level of | |
615 | * decoder instances, pass NULL. | |
511e2123 | 616 | * @param obj The Python class instantiation. |
2072ae0c | 617 | * |
a8b72b05 | 618 | * @return Pointer to struct srd_decoder_inst, or NULL if not found. |
57790bc8 UH |
619 | * |
620 | * @private | |
8c664ca2 UH |
621 | * |
622 | * @since 0.1.0 | |
2072ae0c | 623 | */ |
abeeed8b | 624 | SRD_PRIV struct srd_decoder_inst *srd_inst_find_by_obj(const GSList *stack, |
0ff2d191 | 625 | const PyObject *obj) |
2072ae0c | 626 | { |
32cfb920 BV |
627 | struct srd_decoder_inst *di; |
628 | struct srd_session *sess; | |
629 | GSList *l; | |
2072ae0c BV |
630 | |
631 | di = NULL; | |
32cfb920 BV |
632 | for (l = sessions; di == NULL && l != NULL; l = l->next) { |
633 | sess = l->data; | |
634 | di = srd_sess_inst_find_by_obj(sess, stack, obj); | |
2072ae0c BV |
635 | } |
636 | ||
637 | return di; | |
638 | } | |
639 | ||
21dfd91d UH |
640 | /** |
641 | * Set the list of initial (assumed) pin values. | |
642 | * | |
21dfd91d | 643 | * @param di Decoder instance to use. Must not be NULL. |
97b874bd | 644 | * @param initial_pins A GArray of uint8_t values. Must not be NULL. |
21dfd91d | 645 | * |
97b874bd | 646 | * @since 0.5.0 |
21dfd91d | 647 | */ |
97b874bd | 648 | SRD_API int srd_inst_initial_pins_set_all(struct srd_decoder_inst *di, GArray *initial_pins) |
21dfd91d UH |
649 | { |
650 | int i; | |
651 | GString *s; | |
21dfd91d | 652 | |
97b874bd | 653 | if (!di) { |
21dfd91d | 654 | srd_err("Invalid decoder instance."); |
97b874bd | 655 | return SRD_ERR_ARG; |
21dfd91d UH |
656 | } |
657 | ||
97b874bd UH |
658 | if (!initial_pins) |
659 | return SRD_ERR_ARG; | |
21dfd91d | 660 | |
97b874bd UH |
661 | if (initial_pins->len != (guint)di->dec_num_channels) { |
662 | srd_err("Incorrect number of channels (need %d, got %d).", | |
663 | di->dec_num_channels, initial_pins->len); | |
664 | return SRD_ERR_ARG; | |
21dfd91d UH |
665 | } |
666 | ||
97b874bd UH |
667 | /* Sanity-check initial pin state values. */ |
668 | for (i = 0; i < di->dec_num_channels; i++) { | |
669 | if (initial_pins->data[i] <= 2) | |
670 | continue; | |
671 | srd_err("Invalid initial channel %d pin state: %d.", | |
672 | i, initial_pins->data[i]); | |
673 | return SRD_ERR_ARG; | |
674 | } | |
21dfd91d | 675 | |
21dfd91d | 676 | s = g_string_sized_new(100); |
63bbdb44 | 677 | oldpins_array_seed(di); |
21dfd91d | 678 | for (i = 0; i < di->dec_num_channels; i++) { |
97b874bd | 679 | di->old_pins_array->data[i] = initial_pins->data[i]; |
21dfd91d UH |
680 | g_string_append_printf(s, "%d, ", di->old_pins_array->data[i]); |
681 | } | |
682 | s = g_string_truncate(s, s->len - 2); | |
683 | srd_dbg("Initial pins: %s.", s->str); | |
684 | g_string_free(s, TRUE); | |
97b874bd UH |
685 | |
686 | return SRD_OK; | |
21dfd91d UH |
687 | } |
688 | ||
63bbdb44 GS |
689 | /** @private */ |
690 | SRD_PRIV void oldpins_array_seed(struct srd_decoder_inst *di) | |
691 | { | |
692 | size_t count; | |
693 | GArray *arr; | |
694 | ||
695 | if (!di) | |
696 | return; | |
697 | if (di->old_pins_array) | |
698 | return; | |
699 | ||
700 | srd_dbg("%s: Seeding old pins, %s().", di->inst_id, __func__); | |
701 | count = di->dec_num_channels; | |
702 | arr = g_array_sized_new(FALSE, TRUE, sizeof(uint8_t), count); | |
703 | g_array_set_size(arr, count); | |
704 | memset(arr->data, SRD_INITIAL_PIN_SAME_AS_SAMPLE0, count); | |
705 | di->old_pins_array = arr; | |
706 | } | |
707 | ||
c947ab01 | 708 | /** @private */ |
04383ea8 GS |
709 | SRD_PRIV void oldpins_array_free(struct srd_decoder_inst *di) |
710 | { | |
711 | if (!di) | |
712 | return; | |
713 | if (!di->old_pins_array) | |
714 | return; | |
715 | ||
716 | srd_dbg("%s: Releasing initial pin state.", di->inst_id); | |
717 | ||
718 | g_array_free(di->old_pins_array, TRUE); | |
719 | di->old_pins_array = NULL; | |
720 | } | |
721 | ||
57790bc8 | 722 | /** @private */ |
ed416497 | 723 | SRD_PRIV int srd_inst_start(struct srd_decoder_inst *di) |
b2c19614 | 724 | { |
ed416497 | 725 | PyObject *py_res; |
2072ae0c | 726 | GSList *l; |
a8b72b05 | 727 | struct srd_decoder_inst *next_di; |
ed416497 | 728 | int ret; |
514b2edc | 729 | PyGILState_STATE gstate; |
b2c19614 | 730 | |
7a1712c4 | 731 | srd_dbg("Calling start() method on protocol decoder instance %s.", |
ed416497 | 732 | di->inst_id); |
b2c19614 | 733 | |
514b2edc UH |
734 | gstate = PyGILState_Ensure(); |
735 | ||
21dfd91d | 736 | /* Run self.start(). */ |
ed416497 | 737 | if (!(py_res = PyObject_CallMethod(di->py_inst, "start", NULL))) { |
201a85a8 | 738 | srd_exception_catch("Protocol decoder instance %s", |
ed416497 | 739 | di->inst_id); |
514b2edc | 740 | PyGILState_Release(gstate); |
7ce7775c BV |
741 | return SRD_ERR_PYTHON; |
742 | } | |
f38ec285 | 743 | Py_DecRef(py_res); |
7ce7775c | 744 | |
21dfd91d UH |
745 | /* Set self.samplenum to 0. */ |
746 | PyObject_SetAttrString(di->py_inst, "samplenum", PyLong_FromLong(0)); | |
747 | ||
e27d3e67 GS |
748 | /* Set self.matched to None. */ |
749 | PyObject_SetAttrString(di->py_inst, "matched", Py_None); | |
21dfd91d | 750 | |
514b2edc UH |
751 | PyGILState_Release(gstate); |
752 | ||
ed416497 | 753 | /* Start all the PDs stacked on top of this one. */ |
2072ae0c BV |
754 | for (l = di->next_di; l; l = l->next) { |
755 | next_di = l->data; | |
ed416497 BV |
756 | if ((ret = srd_inst_start(next_di)) != SRD_OK) |
757 | return ret; | |
2072ae0c BV |
758 | } |
759 | ||
b2c19614 BV |
760 | return SRD_OK; |
761 | } | |
762 | ||
21dfd91d UH |
763 | /** |
764 | * Check whether the specified sample matches the specified term. | |
765 | * | |
766 | * In the case of SRD_TERM_SKIP, this function can modify | |
767 | * term->num_samples_already_skipped. | |
768 | * | |
769 | * @param old_sample The value of the previous sample (0/1). | |
770 | * @param sample The value of the current sample (0/1). | |
771 | * @param term The term that should be checked for a match. Must not be NULL. | |
772 | * | |
773 | * @retval TRUE The current sample matches the specified term. | |
774 | * @retval FALSE The current sample doesn't match the specified term, or an | |
775 | * invalid term was provided. | |
776 | * | |
777 | * @private | |
778 | */ | |
779 | static gboolean sample_matches(uint8_t old_sample, uint8_t sample, struct srd_term *term) | |
780 | { | |
7a57c594 | 781 | /* Caller ensures term != NULL. */ |
21dfd91d UH |
782 | |
783 | switch (term->type) { | |
784 | case SRD_TERM_HIGH: | |
785 | if (sample == 1) | |
786 | return TRUE; | |
787 | break; | |
788 | case SRD_TERM_LOW: | |
789 | if (sample == 0) | |
790 | return TRUE; | |
791 | break; | |
792 | case SRD_TERM_RISING_EDGE: | |
793 | if (old_sample == 0 && sample == 1) | |
794 | return TRUE; | |
795 | break; | |
796 | case SRD_TERM_FALLING_EDGE: | |
797 | if (old_sample == 1 && sample == 0) | |
798 | return TRUE; | |
799 | break; | |
800 | case SRD_TERM_EITHER_EDGE: | |
801 | if ((old_sample == 1 && sample == 0) || (old_sample == 0 && sample == 1)) | |
802 | return TRUE; | |
803 | break; | |
804 | case SRD_TERM_NO_EDGE: | |
805 | if ((old_sample == 0 && sample == 0) || (old_sample == 1 && sample == 1)) | |
806 | return TRUE; | |
807 | break; | |
808 | case SRD_TERM_SKIP: | |
809 | if (term->num_samples_already_skipped == term->num_samples_to_skip) | |
810 | return TRUE; | |
811 | term->num_samples_already_skipped++; | |
812 | break; | |
813 | default: | |
814 | srd_err("Unknown term type %d.", term->type); | |
815 | break; | |
816 | } | |
817 | ||
818 | return FALSE; | |
819 | } | |
820 | ||
c947ab01 | 821 | /** @private */ |
21dfd91d UH |
822 | SRD_PRIV void match_array_free(struct srd_decoder_inst *di) |
823 | { | |
824 | if (!di || !di->match_array) | |
825 | return; | |
826 | ||
827 | g_array_free(di->match_array, TRUE); | |
828 | di->match_array = NULL; | |
829 | } | |
830 | ||
c947ab01 | 831 | /** @private */ |
21dfd91d UH |
832 | SRD_PRIV void condition_list_free(struct srd_decoder_inst *di) |
833 | { | |
834 | GSList *l, *ll; | |
835 | ||
836 | if (!di) | |
837 | return; | |
838 | ||
839 | for (l = di->condition_list; l; l = l->next) { | |
840 | ll = l->data; | |
841 | if (ll) | |
842 | g_slist_free_full(ll, g_free); | |
843 | } | |
844 | ||
845 | di->condition_list = NULL; | |
846 | } | |
847 | ||
848 | static gboolean have_non_null_conds(const struct srd_decoder_inst *di) | |
849 | { | |
850 | GSList *l, *cond; | |
851 | ||
852 | if (!di) | |
853 | return FALSE; | |
854 | ||
855 | for (l = di->condition_list; l; l = l->next) { | |
856 | cond = l->data; | |
857 | if (cond) | |
858 | return TRUE; | |
859 | } | |
860 | ||
861 | return FALSE; | |
862 | } | |
863 | ||
864 | static void update_old_pins_array(struct srd_decoder_inst *di, | |
865 | const uint8_t *sample_pos) | |
866 | { | |
867 | uint8_t sample; | |
868 | int i, byte_offset, bit_offset; | |
869 | ||
870 | if (!di || !di->dec_channelmap || !sample_pos) | |
871 | return; | |
872 | ||
63bbdb44 | 873 | oldpins_array_seed(di); |
21dfd91d UH |
874 | for (i = 0; i < di->dec_num_channels; i++) { |
875 | byte_offset = di->dec_channelmap[i] / 8; | |
876 | bit_offset = di->dec_channelmap[i] % 8; | |
877 | sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0; | |
878 | di->old_pins_array->data[i] = sample; | |
879 | } | |
880 | } | |
881 | ||
97b874bd UH |
882 | static void update_old_pins_array_initial_pins(struct srd_decoder_inst *di) |
883 | { | |
884 | uint8_t sample; | |
885 | int i, byte_offset, bit_offset; | |
886 | const uint8_t *sample_pos; | |
887 | ||
888 | if (!di || !di->dec_channelmap) | |
889 | return; | |
890 | ||
891 | sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize); | |
892 | ||
63bbdb44 | 893 | oldpins_array_seed(di); |
97b874bd UH |
894 | for (i = 0; i < di->dec_num_channels; i++) { |
895 | if (di->old_pins_array->data[i] != SRD_INITIAL_PIN_SAME_AS_SAMPLE0) | |
896 | continue; | |
897 | byte_offset = di->dec_channelmap[i] / 8; | |
898 | bit_offset = di->dec_channelmap[i] % 8; | |
899 | sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0; | |
900 | di->old_pins_array->data[i] = sample; | |
901 | } | |
902 | } | |
903 | ||
21dfd91d UH |
904 | static gboolean term_matches(const struct srd_decoder_inst *di, |
905 | struct srd_term *term, const uint8_t *sample_pos) | |
906 | { | |
907 | uint8_t old_sample, sample; | |
908 | int byte_offset, bit_offset, ch; | |
909 | ||
7a57c594 | 910 | /* Caller ensures di, di->dec_channelmap, term, sample_pos != NULL. */ |
21dfd91d | 911 | |
7a57c594 UH |
912 | if (term->type == SRD_TERM_SKIP) |
913 | return sample_matches(0, 0, term); | |
21dfd91d | 914 | |
7a57c594 UH |
915 | ch = term->channel; |
916 | byte_offset = di->dec_channelmap[ch] / 8; | |
917 | bit_offset = di->dec_channelmap[ch] % 8; | |
918 | sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0; | |
919 | old_sample = di->old_pins_array->data[ch]; | |
21dfd91d UH |
920 | |
921 | return sample_matches(old_sample, sample, term); | |
922 | } | |
923 | ||
924 | static gboolean all_terms_match(const struct srd_decoder_inst *di, | |
925 | const GSList *cond, const uint8_t *sample_pos) | |
926 | { | |
927 | const GSList *l; | |
928 | struct srd_term *term; | |
929 | ||
7a57c594 | 930 | /* Caller ensures di, cond, sample_pos != NULL. */ |
21dfd91d UH |
931 | |
932 | for (l = cond; l; l = l->next) { | |
933 | term = l->data; | |
934 | if (!term_matches(di, term, sample_pos)) | |
935 | return FALSE; | |
936 | } | |
937 | ||
938 | return TRUE; | |
939 | } | |
940 | ||
941 | static gboolean at_least_one_condition_matched( | |
942 | const struct srd_decoder_inst *di, unsigned int num_conditions) | |
943 | { | |
944 | unsigned int i; | |
945 | ||
7a57c594 | 946 | /* Caller ensures di != NULL. */ |
21dfd91d UH |
947 | |
948 | for (i = 0; i < num_conditions; i++) { | |
949 | if (di->match_array->data[i]) | |
950 | return TRUE; | |
951 | } | |
952 | ||
953 | return FALSE; | |
954 | } | |
955 | ||
956 | static gboolean find_match(struct srd_decoder_inst *di) | |
957 | { | |
21dfd91d UH |
958 | uint64_t i, j, num_samples_to_process; |
959 | GSList *l, *cond; | |
960 | const uint8_t *sample_pos; | |
961 | unsigned int num_conditions; | |
962 | ||
7a57c594 UH |
963 | /* Caller ensures di != NULL. */ |
964 | ||
21dfd91d UH |
965 | /* Check whether the condition list is NULL/empty. */ |
966 | if (!di->condition_list) { | |
967 | srd_dbg("NULL/empty condition list, automatic match."); | |
968 | return TRUE; | |
969 | } | |
970 | ||
971 | /* Check whether we have any non-NULL conditions. */ | |
972 | if (!have_non_null_conds(di)) { | |
973 | srd_dbg("Only NULL conditions in list, automatic match."); | |
974 | return TRUE; | |
975 | } | |
976 | ||
4564e8e5 | 977 | num_samples_to_process = di->abs_end_samplenum - di->abs_cur_samplenum; |
21dfd91d UH |
978 | num_conditions = g_slist_length(di->condition_list); |
979 | ||
980 | /* di->match_array is NULL here. Create a new GArray. */ | |
981 | di->match_array = g_array_sized_new(FALSE, TRUE, sizeof(gboolean), num_conditions); | |
982 | g_array_set_size(di->match_array, num_conditions); | |
983 | ||
97b874bd UH |
984 | /* Sample 0: Set di->old_pins_array for SRD_INITIAL_PIN_SAME_AS_SAMPLE0 pins. */ |
985 | if (di->abs_cur_samplenum == 0) | |
986 | update_old_pins_array_initial_pins(di); | |
987 | ||
a026a3fb | 988 | for (i = 0; i < num_samples_to_process; i++, (di->abs_cur_samplenum)++) { |
21dfd91d | 989 | |
4564e8e5 | 990 | sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize); |
21dfd91d UH |
991 | |
992 | /* Check whether the current sample matches at least one of the conditions (logical OR). */ | |
993 | /* IMPORTANT: We need to check all conditions, even if there was a match already! */ | |
994 | for (l = di->condition_list, j = 0; l; l = l->next, j++) { | |
995 | cond = l->data; | |
996 | if (!cond) | |
997 | continue; | |
998 | /* All terms in 'cond' must match (logical AND). */ | |
999 | di->match_array->data[j] = all_terms_match(di, cond, sample_pos); | |
1000 | } | |
1001 | ||
1002 | update_old_pins_array(di, sample_pos); | |
1003 | ||
1004 | /* If at least one condition matched we're done. */ | |
1005 | if (at_least_one_condition_matched(di, num_conditions)) | |
1006 | return TRUE; | |
1007 | } | |
1008 | ||
1009 | return FALSE; | |
1010 | } | |
1011 | ||
1012 | /** | |
1013 | * Process available samples and check if they match the defined conditions. | |
1014 | * | |
1015 | * This function returns if there is an error, or when a match is found, or | |
1016 | * when all samples have been processed (whether a match was found or not). | |
04383ea8 GS |
1017 | * This function immediately terminates when the decoder's wait() method |
1018 | * invocation shall get terminated. | |
21dfd91d UH |
1019 | * |
1020 | * @param di The decoder instance to use. Must not be NULL. | |
1021 | * @param found_match Will be set to TRUE if at least one condition matched, | |
1022 | * FALSE otherwise. Must not be NULL. | |
1023 | * | |
1024 | * @retval SRD_OK No errors occured, see found_match for the result. | |
1025 | * @retval SRD_ERR_ARG Invalid arguments. | |
1026 | * | |
1027 | * @private | |
1028 | */ | |
1029 | SRD_PRIV int process_samples_until_condition_match(struct srd_decoder_inst *di, gboolean *found_match) | |
1030 | { | |
1031 | if (!di || !found_match) | |
1032 | return SRD_ERR_ARG; | |
1033 | ||
04383ea8 GS |
1034 | *found_match = FALSE; |
1035 | if (di->want_wait_terminate) | |
1036 | return SRD_OK; | |
1037 | ||
21dfd91d UH |
1038 | /* Check if any of the current condition(s) match. */ |
1039 | while (TRUE) { | |
1040 | /* Feed the (next chunk of the) buffer to find_match(). */ | |
1041 | *found_match = find_match(di); | |
1042 | ||
1043 | /* Did we handle all samples yet? */ | |
4564e8e5 UH |
1044 | if (di->abs_cur_samplenum >= di->abs_end_samplenum) { |
1045 | srd_dbg("Done, handled all samples (abs cur %" PRIu64 | |
1046 | " / abs end %" PRIu64 ").", | |
1047 | di->abs_cur_samplenum, di->abs_end_samplenum); | |
21dfd91d UH |
1048 | return SRD_OK; |
1049 | } | |
1050 | ||
1051 | /* If we didn't find a match, continue looking. */ | |
1052 | if (!(*found_match)) | |
1053 | continue; | |
1054 | ||
1055 | /* At least one condition matched, return. */ | |
1056 | return SRD_OK; | |
1057 | } | |
1058 | ||
1059 | return SRD_OK; | |
1060 | } | |
1061 | ||
1062 | /** | |
1063 | * Worker thread (per PD-stack). | |
1064 | * | |
1065 | * @param data Pointer to the lowest-level PD's device instance. | |
1066 | * Must not be NULL. | |
1067 | * | |
1068 | * @return NULL if there was an error. | |
1069 | */ | |
1070 | static gpointer di_thread(gpointer data) | |
1071 | { | |
1072 | PyObject *py_res; | |
1073 | struct srd_decoder_inst *di; | |
04383ea8 | 1074 | int wanted_term; |
514b2edc | 1075 | PyGILState_STATE gstate; |
21dfd91d UH |
1076 | |
1077 | if (!data) | |
1078 | return NULL; | |
1079 | ||
1080 | di = data; | |
1081 | ||
04383ea8 GS |
1082 | srd_dbg("%s: Starting thread routine for decoder.", di->inst_id); |
1083 | ||
514b2edc UH |
1084 | gstate = PyGILState_Ensure(); |
1085 | ||
04383ea8 GS |
1086 | /* |
1087 | * Call self.decode(). Only returns if the PD throws an exception. | |
1088 | * "Regular" termination of the decode() method is not expected. | |
1089 | */ | |
21dfd91d | 1090 | Py_IncRef(di->py_inst); |
04383ea8 GS |
1091 | srd_dbg("%s: Calling decode() method.", di->inst_id); |
1092 | py_res = PyObject_CallMethod(di->py_inst, "decode", NULL); | |
1093 | srd_dbg("%s: decode() method terminated.", di->inst_id); | |
1094 | ||
1095 | /* | |
1096 | * Make sure to unblock potentially pending srd_inst_decode() | |
1097 | * calls in application threads after the decode() method might | |
1098 | * have terminated, while it neither has processed sample data | |
1099 | * nor has terminated upon request. This happens e.g. when "need | |
1100 | * a samplerate to decode" exception is thrown. | |
1101 | */ | |
1102 | g_mutex_lock(&di->data_mutex); | |
1103 | wanted_term = di->want_wait_terminate; | |
1104 | di->want_wait_terminate = TRUE; | |
1105 | di->handled_all_samples = TRUE; | |
1106 | g_cond_signal(&di->handled_all_samples_cond); | |
1107 | g_mutex_unlock(&di->data_mutex); | |
1108 | ||
1109 | /* | |
1110 | * Check for the termination cause of the decode() method. | |
1111 | * Though this is mostly for information. | |
1112 | */ | |
1113 | if (!py_res && wanted_term) { | |
1114 | /* | |
1115 | * Silently ignore errors upon return from decode() calls | |
1116 | * when termination was requested. Terminate the thread | |
1117 | * which executed this instance's decode() logic. | |
1118 | */ | |
1119 | srd_dbg("%s: Thread done (!res, want_term).", di->inst_id); | |
1120 | PyErr_Clear(); | |
514b2edc | 1121 | PyGILState_Release(gstate); |
04383ea8 GS |
1122 | return NULL; |
1123 | } | |
1124 | if (!py_res) { | |
1125 | /* | |
1126 | * The decode() invocation terminated unexpectedly. Have | |
1127 | * the back trace printed, and terminate the thread which | |
1128 | * executed the decode() method. | |
1129 | */ | |
1130 | srd_dbg("%s: decode() terminated unrequested.", di->inst_id); | |
21dfd91d | 1131 | srd_exception_catch("Protocol decoder instance %s: ", di->inst_id); |
04383ea8 | 1132 | srd_dbg("%s: Thread done (!res, !want_term).", di->inst_id); |
514b2edc | 1133 | PyGILState_Release(gstate); |
21dfd91d UH |
1134 | return NULL; |
1135 | } | |
04383ea8 GS |
1136 | |
1137 | /* | |
1138 | * TODO: By design the decode() method is not supposed to terminate. | |
1139 | * Nevertheless we have the thread joined, and srd backend calls to | |
1140 | * decode() will re-start another thread transparently. | |
1141 | */ | |
1142 | srd_dbg("%s: decode() terminated (req %d).", di->inst_id, wanted_term); | |
21dfd91d | 1143 | Py_DecRef(py_res); |
04383ea8 GS |
1144 | PyErr_Clear(); |
1145 | ||
514b2edc UH |
1146 | PyGILState_Release(gstate); |
1147 | ||
04383ea8 | 1148 | srd_dbg("%s: Thread done (with res).", di->inst_id); |
21dfd91d UH |
1149 | |
1150 | return NULL; | |
1151 | } | |
1152 | ||
b2c19614 | 1153 | /** |
e959f49a | 1154 | * Decode a chunk of samples. |
b2c19614 | 1155 | * |
4564e8e5 UH |
1156 | * The calls to this function must provide the samples that shall be |
1157 | * used by the protocol decoder | |
1158 | * - in the correct order ([...]5, 6, 4, 7, 8[...] is a bug), | |
1159 | * - starting from sample zero (2, 3, 4, 5, 6[...] is a bug), | |
1160 | * - consecutively, with no gaps (0, 1, 2, 4, 5[...] is a bug). | |
1161 | * | |
1162 | * The start- and end-sample numbers are absolute sample numbers (relative | |
1163 | * to the start of the whole capture/file/stream), i.e. they are not relative | |
1164 | * sample numbers within the chunk specified by 'inbuf' and 'inbuflen'. | |
1165 | * | |
1166 | * Correct example (4096 samples total, 4 chunks @ 1024 samples each): | |
2f7bc9d2 MC |
1167 | * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1); |
1168 | * srd_inst_decode(di, 1024, 2048, inbuf, 1024, 1); | |
1169 | * srd_inst_decode(di, 2048, 3072, inbuf, 1024, 1); | |
1170 | * srd_inst_decode(di, 3072, 4096, inbuf, 1024, 1); | |
4564e8e5 UH |
1171 | * |
1172 | * The chunk size ('inbuflen') can be arbitrary and can differ between calls. | |
1173 | * | |
1174 | * Correct example (4096 samples total, 7 chunks @ various samples each): | |
2f7bc9d2 MC |
1175 | * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1); |
1176 | * srd_inst_decode(di, 1024, 1124, inbuf, 100, 1); | |
1177 | * srd_inst_decode(di, 1124, 1424, inbuf, 300, 1); | |
1178 | * srd_inst_decode(di, 1424, 1643, inbuf, 219, 1); | |
1179 | * srd_inst_decode(di, 1643, 2048, inbuf, 405, 1); | |
1180 | * srd_inst_decode(di, 2048, 3072, inbuf, 1024, 1); | |
1181 | * srd_inst_decode(di, 3072, 4096, inbuf, 1024, 1); | |
4564e8e5 UH |
1182 | * |
1183 | * INCORRECT example (4096 samples total, 4 chunks @ 1024 samples each, but | |
1184 | * the start- and end-samplenumbers are not absolute): | |
2f7bc9d2 MC |
1185 | * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1); |
1186 | * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1); | |
1187 | * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1); | |
1188 | * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1); | |
4564e8e5 | 1189 | * |
ed416497 | 1190 | * @param di The decoder instance to call. Must not be NULL. |
4564e8e5 UH |
1191 | * @param abs_start_samplenum The absolute starting sample number for the |
1192 | * buffer's sample set, relative to the start of capture. | |
1193 | * @param abs_end_samplenum The absolute ending sample number for the | |
1194 | * buffer's sample set, relative to the start of capture. | |
7a1712c4 UH |
1195 | * @param inbuf The buffer to decode. Must not be NULL. |
1196 | * @param inbuflen Length of the buffer. Must be > 0. | |
e959f49a | 1197 | * @param unitsize The number of bytes per sample. Must be > 0. |
b2c19614 BV |
1198 | * |
1199 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
57790bc8 UH |
1200 | * |
1201 | * @private | |
b2c19614 | 1202 | */ |
21dfd91d | 1203 | SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di, |
4564e8e5 | 1204 | uint64_t abs_start_samplenum, uint64_t abs_end_samplenum, |
cda2d36c | 1205 | const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize) |
b2c19614 | 1206 | { |
d906d3f9 | 1207 | /* Return an error upon unusable input. */ |
7a1712c4 UH |
1208 | if (!di) { |
1209 | srd_dbg("empty decoder instance"); | |
d906d3f9 BV |
1210 | return SRD_ERR_ARG; |
1211 | } | |
7a1712c4 UH |
1212 | if (!inbuf) { |
1213 | srd_dbg("NULL buffer pointer"); | |
d906d3f9 BV |
1214 | return SRD_ERR_ARG; |
1215 | } | |
1216 | if (inbuflen == 0) { | |
7a1712c4 | 1217 | srd_dbg("empty buffer"); |
d906d3f9 | 1218 | return SRD_ERR_ARG; |
80f8083b UH |
1219 | } |
1220 | if (unitsize == 0) { | |
1221 | srd_dbg("unitsize 0"); | |
1222 | return SRD_ERR_ARG; | |
d906d3f9 | 1223 | } |
b2c19614 | 1224 | |
de23c992 MC |
1225 | if (abs_start_samplenum != di->abs_cur_samplenum || |
1226 | abs_end_samplenum < abs_start_samplenum) { | |
f98f4171 UH |
1227 | srd_dbg("Incorrect sample numbers: start=%" PRIu64 ", cur=%" |
1228 | PRIu64 ", end=%" PRIu64 ".", abs_start_samplenum, | |
1229 | di->abs_cur_samplenum, abs_end_samplenum); | |
de23c992 MC |
1230 | return SRD_ERR_ARG; |
1231 | } | |
1232 | ||
21dfd91d | 1233 | di->data_unitsize = unitsize; |
2730fe23 | 1234 | |
4564e8e5 | 1235 | srd_dbg("Decoding: abs start sample %" PRIu64 ", abs end sample %" |
2730fe23 | 1236 | PRIu64 " (%" PRIu64 " samples, %" PRIu64 " bytes, unitsize = " |
4564e8e5 UH |
1237 | "%d), instance %s.", abs_start_samplenum, abs_end_samplenum, |
1238 | abs_end_samplenum - abs_start_samplenum, inbuflen, di->data_unitsize, | |
2730fe23 UH |
1239 | di->inst_id); |
1240 | ||
eb883723 UH |
1241 | /* If this is the first call, start the worker thread. */ |
1242 | if (!di->thread_handle) { | |
1243 | srd_dbg("No worker thread for this decoder stack " | |
1244 | "exists yet, creating one: %s.", di->inst_id); | |
1245 | di->thread_handle = g_thread_new(di->inst_id, | |
1246 | di_thread, di); | |
1247 | } | |
04867deb | 1248 | |
eb883723 UH |
1249 | /* Push the new sample chunk to the worker thread. */ |
1250 | g_mutex_lock(&di->data_mutex); | |
1251 | di->abs_start_samplenum = abs_start_samplenum; | |
1252 | di->abs_end_samplenum = abs_end_samplenum; | |
1253 | di->inbuf = inbuf; | |
1254 | di->inbuflen = inbuflen; | |
1255 | di->got_new_samples = TRUE; | |
1256 | di->handled_all_samples = FALSE; | |
21dfd91d | 1257 | |
eb883723 UH |
1258 | /* Signal the thread that we have new data. */ |
1259 | g_cond_signal(&di->got_new_samples_cond); | |
1260 | g_mutex_unlock(&di->data_mutex); | |
1261 | ||
1262 | /* When all samples in this chunk were handled, return. */ | |
1263 | g_mutex_lock(&di->data_mutex); | |
1264 | while (!di->handled_all_samples && !di->want_wait_terminate) | |
1265 | g_cond_wait(&di->handled_all_samples_cond, &di->data_mutex); | |
1266 | g_mutex_unlock(&di->data_mutex); | |
bc5f5a43 | 1267 | |
3f3c4614 GS |
1268 | if (di->want_wait_terminate) |
1269 | return SRD_ERR_TERM_REQ; | |
7969d803 | 1270 | |
b2c19614 BV |
1271 | return SRD_OK; |
1272 | } | |
1273 | ||
9553e962 GS |
1274 | /** |
1275 | * Terminate current decoder work, prepare for re-use on new input data. | |
1276 | * | |
1277 | * Terminates all decoder operations in the specified decoder instance | |
1278 | * and the instances stacked on top of it. Resets internal state such | |
1279 | * that the previously constructed stack can process new input data that | |
1280 | * is not related to previously processed input data. This avoids the | |
1281 | * expensive and complex re-construction of decoder stacks. | |
1282 | * | |
1283 | * Callers are expected to follow up with start, metadata, and decode | |
1284 | * calls like they would for newly constructed decoder stacks. | |
1285 | * | |
1286 | * @param di The decoder instance to call. Must not be NULL. | |
7969d803 | 1287 | * |
9553e962 | 1288 | * @return SRD_OK upon success, a (negative) error code otherwise. |
7969d803 | 1289 | * |
9553e962 GS |
1290 | * @private |
1291 | */ | |
1292 | SRD_PRIV int srd_inst_terminate_reset(struct srd_decoder_inst *di) | |
1293 | { | |
1294 | PyGILState_STATE gstate; | |
1295 | PyObject *py_ret; | |
1296 | GSList *l; | |
1297 | int ret; | |
1298 | ||
1299 | if (!di) | |
1300 | return SRD_ERR_ARG; | |
1301 | ||
1302 | /* | |
1303 | * Request termination and wait for previously initiated | |
1304 | * background operation to finish. Reset internal state, but | |
1305 | * do not start releasing resources yet. This shall result in | |
1306 | * decoders' state just like after creation. This block handles | |
1307 | * the C language library side. | |
1308 | */ | |
1309 | srd_dbg("Terminating instance %s", di->inst_id); | |
1310 | srd_inst_join_decode_thread(di); | |
1311 | srd_inst_reset_state(di); | |
1312 | ||
1313 | /* | |
1314 | * Have the Python side's .reset() method executed (if the PD | |
1315 | * implements it). It's assumed that .reset() assigns variables | |
1316 | * very much like __init__() used to do in the past. Thus memory | |
1317 | * that was allocated in previous calls gets released by Python | |
1318 | * as it's not referenced any longer. | |
1319 | */ | |
1320 | gstate = PyGILState_Ensure(); | |
1321 | if (PyObject_HasAttrString(di->py_inst, "reset")) { | |
1322 | srd_dbg("Calling .reset() of instance %s", di->inst_id); | |
1323 | py_ret = PyObject_CallMethod(di->py_inst, "reset", NULL); | |
1324 | Py_XDECREF(py_ret); | |
1325 | } | |
1326 | PyGILState_Release(gstate); | |
1327 | ||
7969d803 | 1328 | /* Pass the "restart" request to all stacked decoders. */ |
9553e962 GS |
1329 | for (l = di->next_di; l; l = l->next) { |
1330 | ret = srd_inst_terminate_reset(l->data); | |
1331 | if (ret != SRD_OK) | |
1332 | return ret; | |
1333 | } | |
1334 | ||
1335 | return SRD_OK; | |
1336 | } | |
1337 | ||
57790bc8 | 1338 | /** @private */ |
12243c22 | 1339 | SRD_PRIV void srd_inst_free(struct srd_decoder_inst *di) |
fa12a21e BV |
1340 | { |
1341 | GSList *l; | |
1342 | struct srd_pd_output *pdo; | |
514b2edc | 1343 | PyGILState_STATE gstate; |
fa12a21e | 1344 | |
7969d803 | 1345 | srd_dbg("Freeing instance %s.", di->inst_id); |
fa12a21e | 1346 | |
04383ea8 | 1347 | srd_inst_join_decode_thread(di); |
514b2edc | 1348 | |
04383ea8 GS |
1349 | srd_inst_reset_state(di); |
1350 | ||
514b2edc | 1351 | gstate = PyGILState_Ensure(); |
a8b72b05 | 1352 | Py_DecRef(di->py_inst); |
514b2edc UH |
1353 | PyGILState_Release(gstate); |
1354 | ||
a8b72b05 | 1355 | g_free(di->inst_id); |
6a15597a | 1356 | g_free(di->dec_channelmap); |
d2297b87 | 1357 | g_free(di->channel_samples); |
fa12a21e BV |
1358 | g_slist_free(di->next_di); |
1359 | for (l = di->pd_output; l; l = l->next) { | |
1360 | pdo = l->data; | |
1361 | g_free(pdo->proto_id); | |
1362 | g_free(pdo); | |
1363 | } | |
1364 | g_slist_free(di->pd_output); | |
e592ac89 | 1365 | g_free(di); |
fa12a21e BV |
1366 | } |
1367 | ||
57790bc8 | 1368 | /** @private */ |
3262ef02 | 1369 | SRD_PRIV void srd_inst_free_all(struct srd_session *sess) |
fa12a21e | 1370 | { |
32cfb920 BV |
1371 | if (session_is_valid(sess) != SRD_OK) { |
1372 | srd_err("Invalid session."); | |
1373 | return; | |
1374 | } | |
1375 | ||
3262ef02 | 1376 | g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free); |
fa12a21e | 1377 | } |
b2c19614 | 1378 | |
4895418c | 1379 | /** @} */ |