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