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