]> sigrok.org Git - libsigrokdecode.git/blame - type_decoder.c
decoder: accept skip count 0 (onewire_link, usb_signalling)
[libsigrokdecode.git] / type_decoder.c
CommitLineData
d0a0ed03 1/*
50bd5d25 2 * This file is part of the libsigrokdecode project.
d0a0ed03
BV
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
36784362 20#include <config.h>
f6c7eade
MC
21#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "libsigrokdecode.h"
dbeaab27 23#include <inttypes.h>
d0a0ed03 24
a4e9ca6f
UH
25/** @cond PRIVATE */
26extern SRD_PRIV GSList *sessions;
27/** @endcond */
28
17475b09
UH
29typedef struct {
30 PyObject_HEAD
31} srd_Decoder;
32
7969d803 33/* This is only used for nicer srd_dbg() output. */
5c723a57 34SRD_PRIV const char *output_type_name(unsigned int idx)
201a85a8
DE
35{
36 static const char names[][16] = {
37 "OUTPUT_ANN",
38 "OUTPUT_PYTHON",
39 "OUTPUT_BINARY",
40 "OUTPUT_META",
41 "(invalid)"
42 };
7969d803 43
201a85a8
DE
44 return names[MIN(idx, G_N_ELEMENTS(names) - 1)];
45}
58572aed 46
c1b2bbc1
GS
47static void release_annotation(struct srd_proto_data_annotation *pda)
48{
49 if (!pda)
50 return;
51 if (pda->ann_text)
52 g_strfreev(pda->ann_text);
c1b2bbc1
GS
53}
54
4f75f1c1 55static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
0b922460 56 struct srd_proto_data *pdata)
d0a0ed03
BV
57{
58 PyObject *py_tmp;
59 struct srd_pd_output *pdo;
0b922460 60 struct srd_proto_data_annotation *pda;
280d554c 61 int ann_class;
0b922460 62 char **ann_text;
514b2edc
UH
63 PyGILState_STATE gstate;
64
65 gstate = PyGILState_Ensure();
d0a0ed03 66
280d554c 67 /* Should be a list of [annotation class, [string, ...]]. */
0679f5bf 68 if (!PyList_Check(obj)) {
201a85a8 69 srd_err("Protocol decoder %s submitted an annotation that"
0679f5bf 70 " is not a list", di->decoder->name);
514b2edc 71 goto err;
d0a0ed03
BV
72 }
73
361fdcaa 74 /* Should have 2 elements. */
d0a0ed03 75 if (PyList_Size(obj) != 2) {
c9bfccc6 76 srd_err("Protocol decoder %s submitted annotation list with "
4916b173 77 "%zd elements instead of 2", di->decoder->name,
c9bfccc6 78 PyList_Size(obj));
514b2edc 79 goto err;
d0a0ed03
BV
80 }
81
c9bfccc6
UH
82 /*
83 * The first element should be an integer matching a previously
280d554c 84 * registered annotation class.
c9bfccc6 85 */
d0a0ed03
BV
86 py_tmp = PyList_GetItem(obj, 0);
87 if (!PyLong_Check(py_tmp)) {
c9bfccc6
UH
88 srd_err("Protocol decoder %s submitted annotation list, but "
89 "first element was not an integer.", di->decoder->name);
514b2edc 90 goto err;
d0a0ed03 91 }
280d554c
UH
92 ann_class = PyLong_AsLong(py_tmp);
93 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_class))) {
d906d3f9 94 srd_err("Protocol decoder %s submitted data to unregistered "
280d554c 95 "annotation class %d.", di->decoder->name, ann_class);
514b2edc 96 goto err;
d0a0ed03 97 }
d0a0ed03 98
361fdcaa 99 /* Second element must be a list. */
d0a0ed03
BV
100 py_tmp = PyList_GetItem(obj, 1);
101 if (!PyList_Check(py_tmp)) {
d906d3f9 102 srd_err("Protocol decoder %s submitted annotation list, but "
c9bfccc6 103 "second element was not a list.", di->decoder->name);
514b2edc 104 goto err;
d0a0ed03 105 }
62a2b15c 106 if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) {
d906d3f9 107 srd_err("Protocol decoder %s submitted annotation list, but "
c9bfccc6 108 "second element was malformed.", di->decoder->name);
514b2edc 109 goto err;
d0a0ed03
BV
110 }
111
ee63c5dd 112 pda = pdata->data;
280d554c 113 pda->ann_class = ann_class;
0b922460 114 pda->ann_text = ann_text;
0b922460 115
514b2edc
UH
116 PyGILState_Release(gstate);
117
d0a0ed03 118 return SRD_OK;
514b2edc
UH
119
120err:
121 PyGILState_Release(gstate);
122
123 return SRD_ERR_PYTHON;
d0a0ed03
BV
124}
125
c1b2bbc1
GS
126static void release_binary(struct srd_proto_data_binary *pdb)
127{
128 if (!pdb)
129 return;
8390f85a 130 g_free((void *)pdb->data);
c1b2bbc1
GS
131}
132
d75d8a7c
BV
133static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
134 struct srd_proto_data *pdata)
135{
136 struct srd_proto_data_binary *pdb;
137 PyObject *py_tmp;
138 Py_ssize_t size;
139 int bin_class;
140 char *class_name, *buf;
514b2edc
UH
141 PyGILState_STATE gstate;
142
143 gstate = PyGILState_Ensure();
d75d8a7c 144
2824e811
UH
145 /* Should be a list of [binary class, bytes]. */
146 if (!PyList_Check(obj)) {
147 srd_err("Protocol decoder %s submitted non-list for SRD_OUTPUT_BINARY.",
201a85a8 148 di->decoder->name);
514b2edc 149 goto err;
d75d8a7c
BV
150 }
151
152 /* Should have 2 elements. */
2824e811
UH
153 if (PyList_Size(obj) != 2) {
154 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list "
4916b173 155 "with %zd elements instead of 2", di->decoder->name,
d75d8a7c 156 PyList_Size(obj));
514b2edc 157 goto err;
d75d8a7c
BV
158 }
159
160 /* The first element should be an integer. */
2824e811 161 py_tmp = PyList_GetItem(obj, 0);
d75d8a7c 162 if (!PyLong_Check(py_tmp)) {
2824e811 163 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
d75d8a7c 164 "but first element was not an integer.", di->decoder->name);
514b2edc 165 goto err;
d75d8a7c
BV
166 }
167 bin_class = PyLong_AsLong(py_tmp);
168 if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) {
169 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with "
170 "unregistered binary class %d.", di->decoder->name, bin_class);
514b2edc 171 goto err;
d75d8a7c
BV
172 }
173
174 /* Second element should be bytes. */
2824e811 175 py_tmp = PyList_GetItem(obj, 1);
d75d8a7c 176 if (!PyBytes_Check(py_tmp)) {
2824e811 177 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
d75d8a7c 178 "but second element was not bytes.", di->decoder->name);
514b2edc 179 goto err;
d75d8a7c
BV
180 }
181
182 /* Consider an empty set of bytes a bug. */
183 if (PyBytes_Size(py_tmp) == 0) {
184 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY "
185 "with empty data set.", di->decoder->name);
514b2edc 186 goto err;
d75d8a7c
BV
187 }
188
d75d8a7c 189 if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
514b2edc
UH
190 goto err;
191
192 PyGILState_Release(gstate);
193
c94631f1 194 pdb = pdata->data;
d75d8a7c
BV
195 pdb->bin_class = bin_class;
196 pdb->size = size;
c94631f1 197 if (!(pdb->data = g_try_malloc(pdb->size)))
d75d8a7c
BV
198 return SRD_ERR_MALLOC;
199 memcpy((void *)pdb->data, (const void *)buf, pdb->size);
d75d8a7c
BV
200
201 return SRD_OK;
514b2edc
UH
202
203err:
204 PyGILState_Release(gstate);
205
206 return SRD_ERR_PYTHON;
d75d8a7c
BV
207}
208
29adc171 209static inline struct srd_decoder_inst *srd_sess_inst_find_by_obj(
a4e9ca6f
UH
210 struct srd_session *sess, const GSList *stack, const PyObject *obj)
211{
212 const GSList *l;
213 struct srd_decoder_inst *tmp, *di;
214
215 if (!sess)
216 return NULL;
217
218 di = NULL;
219 for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
220 tmp = l->data;
221 if (tmp->py_inst == obj)
222 di = tmp;
223 else if (tmp->next_di)
224 di = srd_sess_inst_find_by_obj(sess, tmp->next_di, obj);
225 }
226
227 return di;
228}
229
230/**
231 * Find a decoder instance by its Python object.
232 *
233 * I.e. find that instance's instantiation of the sigrokdecode.Decoder class.
234 * This will recurse to find the instance anywhere in the stack tree of all
235 * sessions.
236 *
237 * @param stack Pointer to a GSList of struct srd_decoder_inst, indicating the
238 * stack to search. To start searching at the bottom level of
239 * decoder instances, pass NULL.
240 * @param obj The Python class instantiation.
241 *
242 * @return Pointer to struct srd_decoder_inst, or NULL if not found.
243 *
244 * @since 0.1.0
245 */
29adc171 246static inline struct srd_decoder_inst *srd_inst_find_by_obj(
a4e9ca6f
UH
247 const GSList *stack, const PyObject *obj)
248{
249 struct srd_decoder_inst *di;
250 struct srd_session *sess;
251 GSList *l;
252
996b17db
UH
253 /* Performance shortcut: Handle the most common case first. */
254 sess = sessions->data;
255 di = sess->di_list->data;
256 if (di->py_inst == obj)
4f0d192d 257 return di;
996b17db 258
a4e9ca6f
UH
259 di = NULL;
260 for (l = sessions; di == NULL && l != NULL; l = l->next) {
261 sess = l->data;
262 di = srd_sess_inst_find_by_obj(sess, stack, obj);
263 }
264
265 return di;
266}
267
7ee0c40b
BV
268static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
269{
270 long long intvalue;
271 double dvalue;
514b2edc
UH
272 PyGILState_STATE gstate;
273
274 gstate = PyGILState_Ensure();
7ee0c40b 275
ec31b982 276 if (g_variant_type_equal(pdata->pdo->meta_type, G_VARIANT_TYPE_INT64)) {
7ee0c40b
BV
277 if (!PyLong_Check(obj)) {
278 PyErr_Format(PyExc_TypeError, "This output was registered "
201a85a8 279 "as 'int', but something else was passed.");
514b2edc 280 goto err;
7ee0c40b
BV
281 }
282 intvalue = PyLong_AsLongLong(obj);
283 if (PyErr_Occurred())
514b2edc 284 goto err;
7ee0c40b 285 pdata->data = g_variant_new_int64(intvalue);
ec31b982 286 } else if (g_variant_type_equal(pdata->pdo->meta_type, G_VARIANT_TYPE_DOUBLE)) {
7ee0c40b
BV
287 if (!PyFloat_Check(obj)) {
288 PyErr_Format(PyExc_TypeError, "This output was registered "
201a85a8 289 "as 'float', but something else was passed.");
514b2edc 290 goto err;
7ee0c40b
BV
291 }
292 dvalue = PyFloat_AsDouble(obj);
293 if (PyErr_Occurred())
514b2edc 294 goto err;
7ee0c40b
BV
295 pdata->data = g_variant_new_double(dvalue);
296 }
297
514b2edc
UH
298 PyGILState_Release(gstate);
299
7ee0c40b 300 return SRD_OK;
514b2edc
UH
301
302err:
303 PyGILState_Release(gstate);
304
305 return SRD_ERR_PYTHON;
7ee0c40b
BV
306}
307
287e2788
UH
308static void release_meta(GVariant *gvar)
309{
310 if (!gvar)
311 return;
312 g_variant_unref(gvar);
313}
314
d0a0ed03
BV
315static PyObject *Decoder_put(PyObject *self, PyObject *args)
316{
317 GSList *l;
4f75f1c1 318 PyObject *py_data, *py_res;
a8b72b05 319 struct srd_decoder_inst *di, *next_di;
d0a0ed03 320 struct srd_pd_output *pdo;
8a9f60b1 321 struct srd_proto_data pdata;
ee63c5dd 322 struct srd_proto_data_annotation pda;
c94631f1 323 struct srd_proto_data_binary pdb;
d0a0ed03
BV
324 uint64_t start_sample, end_sample;
325 int output_id;
2994587f 326 struct srd_pd_callback *cb;
514b2edc
UH
327 PyGILState_STATE gstate;
328
287e2788
UH
329 py_data = NULL;
330
514b2edc 331 gstate = PyGILState_Ensure();
d0a0ed03 332
a8b72b05 333 if (!(di = srd_inst_find_by_obj(NULL, self))) {
58572aed 334 /* Shouldn't happen. */
7a1712c4 335 srd_dbg("put(): self instance not found.");
514b2edc 336 goto err;
58572aed 337 }
d0a0ed03 338
c9bfccc6 339 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
3d14e7c9 340 &output_id, &py_data)) {
c9bfccc6
UH
341 /*
342 * This throws an exception, but by returning NULL here we let
343 * Python raise it. This results in a much better trace in
344 * controller.c on the decode() method call.
345 */
514b2edc 346 goto err;
c9bfccc6 347 }
d0a0ed03
BV
348
349 if (!(l = g_slist_nth(di->pd_output, output_id))) {
d906d3f9 350 srd_err("Protocol decoder %s submitted invalid output ID %d.",
c9bfccc6 351 di->decoder->name, output_id);
514b2edc 352 goto err;
d0a0ed03
BV
353 }
354 pdo = l->data;
355
43dc0396
UH
356 /* Upon SRD_OUTPUT_PYTHON for stacked PDs, we have a nicer log message later. */
357 if (pdo->output_type != SRD_OUTPUT_PYTHON && di->next_di != NULL) {
755e4a1e
UH
358 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on "
359 "oid %d (%s).", di->inst_id, start_sample, end_sample,
360 output_type_name(pdo->output_type), output_id,
361 pdo->proto_id);
43dc0396 362 }
58572aed 363
8a9f60b1
UH
364 pdata.start_sample = start_sample;
365 pdata.end_sample = end_sample;
366 pdata.pdo = pdo;
367 pdata.data = NULL;
d0a0ed03
BV
368
369 switch (pdo->output_type) {
370 case SRD_OUTPUT_ANN:
371 /* Annotations are only fed to callbacks. */
32cfb920 372 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
ee63c5dd 373 pdata.data = &pda;
d75d8a7c 374 /* Convert from PyDict to srd_proto_data_annotation. */
8a9f60b1 375 if (convert_annotation(di, py_data, &pdata) != SRD_OK) {
d0a0ed03
BV
376 /* An error was already logged. */
377 break;
378 }
514b2edc 379 Py_BEGIN_ALLOW_THREADS
8a9f60b1 380 cb->cb(&pdata, cb->cb_data);
514b2edc 381 Py_END_ALLOW_THREADS
c1b2bbc1 382 release_annotation(pdata.data);
d0a0ed03
BV
383 }
384 break;
f2a5df42 385 case SRD_OUTPUT_PYTHON:
d0a0ed03
BV
386 for (l = di->next_di; l; l = l->next) {
387 next_di = l->data;
755e4a1e
UH
388 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s "
389 "on oid %d (%s) to instance %s.", di->inst_id,
390 start_sample,
43dc0396 391 end_sample, output_type_name(pdo->output_type),
755e4a1e 392 output_id, pdo->proto_id, next_di->inst_id);
c9bfccc6 393 if (!(py_res = PyObject_CallMethod(
3d14e7c9
BV
394 next_di->py_inst, "decode", "KKO", start_sample,
395 end_sample, py_data))) {
201a85a8 396 srd_exception_catch("Calling %s decode() failed",
3d14e7c9 397 next_di->inst_id);
d0a0ed03
BV
398 }
399 Py_XDECREF(py_res);
400 }
3d14e7c9 401 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
7969d803
UH
402 /*
403 * Frontends aren't really supposed to get Python
404 * callbacks, but it's useful for testing.
405 */
8a9f60b1
UH
406 pdata.data = py_data;
407 cb->cb(&pdata, cb->cb_data);
3d14e7c9 408 }
d0a0ed03
BV
409 break;
410 case SRD_OUTPUT_BINARY:
d75d8a7c 411 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
c94631f1 412 pdata.data = &pdb;
d75d8a7c 413 /* Convert from PyDict to srd_proto_data_binary. */
8a9f60b1 414 if (convert_binary(di, py_data, &pdata) != SRD_OK) {
d75d8a7c
BV
415 /* An error was already logged. */
416 break;
417 }
514b2edc 418 Py_BEGIN_ALLOW_THREADS
8a9f60b1 419 cb->cb(&pdata, cb->cb_data);
514b2edc 420 Py_END_ALLOW_THREADS
c1b2bbc1 421 release_binary(pdata.data);
d75d8a7c 422 }
d0a0ed03 423 break;
7ee0c40b
BV
424 case SRD_OUTPUT_META:
425 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
426 /* Annotations need converting from PyObject. */
8a9f60b1 427 if (convert_meta(&pdata, py_data) != SRD_OK) {
7ee0c40b
BV
428 /* An exception was already set up. */
429 break;
430 }
514b2edc 431 Py_BEGIN_ALLOW_THREADS
8a9f60b1 432 cb->cb(&pdata, cb->cb_data);
514b2edc 433 Py_END_ALLOW_THREADS
287e2788 434 release_meta(pdata.data);
7ee0c40b
BV
435 }
436 break;
d0a0ed03 437 default:
d906d3f9 438 srd_err("Protocol decoder %s submitted invalid output type %d.",
c9bfccc6 439 di->decoder->name, pdo->output_type);
d0a0ed03
BV
440 break;
441 }
442
514b2edc
UH
443 PyGILState_Release(gstate);
444
d0a0ed03 445 Py_RETURN_NONE;
514b2edc
UH
446
447err:
448 PyGILState_Release(gstate);
449
450 return NULL;
d0a0ed03
BV
451}
452
7ee0c40b
BV
453static PyObject *Decoder_register(PyObject *self, PyObject *args,
454 PyObject *kwargs)
d0a0ed03 455{
a8b72b05 456 struct srd_decoder_inst *di;
7ee0c40b
BV
457 struct srd_pd_output *pdo;
458 PyObject *py_new_output_id;
459 PyTypeObject *meta_type_py;
460 const GVariantType *meta_type_gv;
461 int output_type;
462 char *proto_id, *meta_name, *meta_descr;
7969d803 463 char *keywords[] = { "output_type", "proto_id", "meta", NULL };
514b2edc 464 PyGILState_STATE gstate;
7d82e3c1
GS
465 gboolean is_meta;
466 GSList *l;
467 struct srd_pd_output *cmp;
514b2edc
UH
468
469 gstate = PyGILState_Ensure();
7ee0c40b
BV
470
471 meta_type_py = NULL;
472 meta_type_gv = NULL;
473 meta_name = meta_descr = NULL;
d0a0ed03 474
a8b72b05 475 if (!(di = srd_inst_find_by_obj(NULL, self))) {
d0a0ed03 476 PyErr_SetString(PyExc_Exception, "decoder instance not found");
514b2edc 477 goto err;
d0a0ed03
BV
478 }
479
7969d803 480 /* Default to instance ID, which defaults to class ID. */
7ee0c40b
BV
481 proto_id = di->inst_id;
482 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords,
483 &output_type, &proto_id,
484 &meta_type_py, &meta_name, &meta_descr)) {
511e2123 485 /* Let Python raise this exception. */
514b2edc 486 goto err;
d0a0ed03
BV
487 }
488
7ee0c40b 489 /* Check if the meta value's type is supported. */
7d82e3c1
GS
490 is_meta = output_type == SRD_OUTPUT_META;
491 if (is_meta) {
7ee0c40b
BV
492 if (meta_type_py == &PyLong_Type)
493 meta_type_gv = G_VARIANT_TYPE_INT64;
494 else if (meta_type_py == &PyFloat_Type)
495 meta_type_gv = G_VARIANT_TYPE_DOUBLE;
496 else {
201a85a8 497 PyErr_Format(PyExc_TypeError, "Unsupported type.");
514b2edc 498 goto err;
7ee0c40b
BV
499 }
500 }
501
7d82e3c1
GS
502 pdo = NULL;
503 for (l = di->pd_output; l; l = l->next) {
504 cmp = l->data;
505 if (cmp->output_type != output_type)
506 continue;
507 if (strcmp(cmp->proto_id, proto_id) != 0)
508 continue;
509 if (is_meta && cmp->meta_type != meta_type_gv)
510 continue;
511 if (is_meta && strcmp(cmp->meta_name, meta_name) != 0)
512 continue;
513 if (is_meta && strcmp(cmp->meta_descr, meta_descr) != 0)
514 continue;
515 pdo = cmp;
516 break;
517 }
518 if (pdo) {
519 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
520 PyGILState_Release(gstate);
521 return py_new_output_id;
522 }
523
077fa8ac 524 pdo = g_malloc(sizeof(struct srd_pd_output));
7ee0c40b
BV
525
526 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
527 pdo->pdo_id = g_slist_length(di->pd_output);
528 pdo->output_type = output_type;
529 pdo->di = di;
530 pdo->proto_id = g_strdup(proto_id);
531
532 if (output_type == SRD_OUTPUT_META) {
533 pdo->meta_type = meta_type_gv;
534 pdo->meta_name = g_strdup(meta_name);
535 pdo->meta_descr = g_strdup(meta_descr);
536 }
537
538 di->pd_output = g_slist_append(di->pd_output, pdo);
539 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
540
514b2edc
UH
541 PyGILState_Release(gstate);
542
755e4a1e
UH
543 srd_dbg("Instance %s creating new output type %s as oid %d (%s).",
544 di->inst_id, output_type_name(output_type), pdo->pdo_id,
545 proto_id);
546
7ee0c40b 547 return py_new_output_id;
514b2edc
UH
548
549err:
550 PyGILState_Release(gstate);
551
552 return NULL;
7ee0c40b
BV
553}
554
21dfd91d
UH
555static int get_term_type(const char *v)
556{
557 switch (v[0]) {
558 case 'h':
559 return SRD_TERM_HIGH;
560 case 'l':
561 return SRD_TERM_LOW;
562 case 'r':
563 return SRD_TERM_RISING_EDGE;
564 case 'f':
565 return SRD_TERM_FALLING_EDGE;
566 case 'e':
567 return SRD_TERM_EITHER_EDGE;
568 case 'n':
569 return SRD_TERM_NO_EDGE;
4e7ccaf9
UH
570 default:
571 return -1;
21dfd91d
UH
572 }
573
574 return -1;
575}
576
577/**
578 * Get the pin values at the current sample number.
579 *
580 * @param di The decoder instance to use. Must not be NULL.
581 * The number of channels must be >= 1.
582 *
583 * @return A newly allocated PyTuple containing the pin values at the
584 * current sample number.
585 */
586static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
587{
588 int i;
589 uint8_t sample;
590 const uint8_t *sample_pos;
591 int byte_offset, bit_offset;
592 PyObject *py_pinvalues;
514b2edc
UH
593 PyGILState_STATE gstate;
594
21dfd91d
UH
595 if (!di) {
596 srd_err("Invalid decoder instance.");
597 return NULL;
598 }
599
73578d2e
UH
600 gstate = PyGILState_Ensure();
601
21dfd91d
UH
602 py_pinvalues = PyTuple_New(di->dec_num_channels);
603
604 for (i = 0; i < di->dec_num_channels; i++) {
605 /* A channelmap value of -1 means "unused optional channel". */
606 if (di->dec_channelmap[i] == -1) {
607 /* Value of unused channel is 0xff, instead of 0 or 1. */
608 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff));
609 } else {
4564e8e5 610 sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
21dfd91d
UH
611 byte_offset = di->dec_channelmap[i] / 8;
612 bit_offset = di->dec_channelmap[i] % 8;
613 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
614 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(sample));
615 }
616 }
617
514b2edc
UH
618 PyGILState_Release(gstate);
619
21dfd91d
UH
620 return py_pinvalues;
621}
622
623/**
624 * Create a list of terms in the specified condition.
625 *
626 * If there are no terms in the condition, 'term_list' will be NULL.
627 *
1cb40e10 628 * @param di The decoder instance to use. Must not be NULL.
21dfd91d
UH
629 * @param py_dict A Python dict containing terms. Must not be NULL.
630 * @param term_list Pointer to a GSList which will be set to the newly
631 * created list of terms. Must not be NULL.
632 *
633 * @return SRD_OK upon success, a negative error code otherwise.
634 */
1cb40e10
GS
635static int create_term_list(struct srd_decoder_inst *di,
636 PyObject *py_dict, GSList **term_list)
21dfd91d
UH
637{
638 Py_ssize_t pos = 0;
639 PyObject *py_key, *py_value;
640 struct srd_term *term;
1cb40e10 641 int64_t num_samples_to_skip;
21dfd91d 642 char *term_str;
514b2edc 643 PyGILState_STATE gstate;
21dfd91d
UH
644
645 if (!py_dict || !term_list)
646 return SRD_ERR_ARG;
647
648 /* "Create" an empty GSList of terms. */
649 *term_list = NULL;
650
514b2edc
UH
651 gstate = PyGILState_Ensure();
652
21dfd91d
UH
653 /* Iterate over all items in the current dict. */
654 while (PyDict_Next(py_dict, &pos, &py_key, &py_value)) {
655 /* Check whether the current key is a string or a number. */
656 if (PyLong_Check(py_key)) {
657 /* The key is a number. */
21dfd91d
UH
658 /* Get the value string. */
659 if ((py_pydictitem_as_str(py_dict, py_key, &term_str)) != SRD_OK) {
660 srd_err("Failed to get the value.");
514b2edc 661 goto err;
21dfd91d 662 }
35c10c0e 663 term = g_malloc(sizeof(struct srd_term));
21dfd91d
UH
664 term->type = get_term_type(term_str);
665 term->channel = PyLong_AsLong(py_key);
1cb40e10
GS
666 if (term->channel < 0 || term->channel >= di->dec_num_channels)
667 term->type = SRD_TERM_ALWAYS_FALSE;
21dfd91d
UH
668 g_free(term_str);
669 } else if (PyUnicode_Check(py_key)) {
670 /* The key is a string. */
1cb40e10 671 /* TODO: Check if the key is "skip". */
21dfd91d
UH
672 if ((py_pydictitem_as_long(py_dict, py_key, &num_samples_to_skip)) != SRD_OK) {
673 srd_err("Failed to get number of samples to skip.");
514b2edc 674 goto err;
21dfd91d 675 }
35c10c0e 676 term = g_malloc(sizeof(struct srd_term));
21dfd91d
UH
677 term->type = SRD_TERM_SKIP;
678 term->num_samples_to_skip = num_samples_to_skip;
679 term->num_samples_already_skipped = 0;
99e26489 680 if (num_samples_to_skip < 0)
1cb40e10 681 term->type = SRD_TERM_ALWAYS_FALSE;
21dfd91d
UH
682 } else {
683 srd_err("Term key is neither a string nor a number.");
514b2edc 684 goto err;
21dfd91d
UH
685 }
686
687 /* Add the term to the list of terms. */
688 *term_list = g_slist_append(*term_list, term);
689 }
690
514b2edc
UH
691 PyGILState_Release(gstate);
692
21dfd91d 693 return SRD_OK;
514b2edc
UH
694
695err:
696 PyGILState_Release(gstate);
697
698 return SRD_ERR;
21dfd91d
UH
699}
700
701/**
702 * Replace the current condition list with the new one.
703 *
704 * @param self TODO. Must not be NULL.
705 * @param args TODO. Must not be NULL.
706 *
707 * @retval SRD_OK The new condition list was set successfully.
708 * @retval SRD_ERR There was an error setting the new condition list.
709 * The contents of di->condition_list are undefined.
710 * @retval 9999 TODO.
711 */
712static int set_new_condition_list(PyObject *self, PyObject *args)
713{
714 struct srd_decoder_inst *di;
715 GSList *term_list;
716 PyObject *py_conditionlist, *py_conds, *py_dict;
717 int i, num_conditions, ret;
514b2edc 718 PyGILState_STATE gstate;
21dfd91d
UH
719
720 if (!self || !args)
721 return SRD_ERR_ARG;
722
514b2edc
UH
723 gstate = PyGILState_Ensure();
724
21dfd91d
UH
725 /* Get the decoder instance. */
726 if (!(di = srd_inst_find_by_obj(NULL, self))) {
727 PyErr_SetString(PyExc_Exception, "decoder instance not found");
514b2edc 728 goto err;
21dfd91d
UH
729 }
730
04383ea8
GS
731 /*
732 * Return an error condition from .wait() when termination is
733 * requested, such that decode() will terminate.
734 */
735 if (di->want_wait_terminate) {
736 srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__);
514b2edc 737 goto err;
04383ea8
GS
738 }
739
6e19f325
GS
740 /*
741 * Parse the argument of self.wait() into 'py_conds', and check
742 * the data type. The argument is optional, None is assumed in
743 * its absence. None or an empty dict or an empty list mean that
744 * there is no condition, and the next available sample shall
745 * get returned to the caller.
746 */
747 py_conds = Py_None;
748 if (!PyArg_ParseTuple(args, "|O", &py_conds)) {
21dfd91d 749 /* Let Python raise this exception. */
514b2edc 750 goto err;
21dfd91d 751 }
6e19f325
GS
752 if (py_conds == Py_None) {
753 /* 'py_conds' is None. */
514b2edc 754 goto ret_9999;
6e19f325 755 } else if (PyList_Check(py_conds)) {
21dfd91d
UH
756 /* 'py_conds' is a list. */
757 py_conditionlist = py_conds;
758 num_conditions = PyList_Size(py_conditionlist);
759 if (num_conditions == 0)
514b2edc 760 goto ret_9999; /* The PD invoked self.wait([]). */
733047ed 761 Py_INCREF(py_conditionlist);
21dfd91d
UH
762 } else if (PyDict_Check(py_conds)) {
763 /* 'py_conds' is a dict. */
764 if (PyDict_Size(py_conds) == 0)
514b2edc 765 goto ret_9999; /* The PD invoked self.wait({}). */
21dfd91d
UH
766 /* Make a list and put the dict in there for convenience. */
767 py_conditionlist = PyList_New(1);
733047ed 768 Py_INCREF(py_conds);
21dfd91d
UH
769 PyList_SetItem(py_conditionlist, 0, py_conds);
770 num_conditions = 1;
771 } else {
772 srd_err("Condition list is neither a list nor a dict.");
514b2edc 773 goto err;
21dfd91d
UH
774 }
775
776 /* Free the old condition list. */
777 condition_list_free(di);
778
779 ret = SRD_OK;
780
781 /* Iterate over the conditions, set di->condition_list accordingly. */
782 for (i = 0; i < num_conditions; i++) {
783 /* Get a condition (dict) from the condition list. */
784 py_dict = PyList_GetItem(py_conditionlist, i);
785 if (!PyDict_Check(py_dict)) {
786 srd_err("Condition is not a dict.");
787 ret = SRD_ERR;
788 break;
789 }
790
791 /* Create the list of terms in this condition. */
1cb40e10 792 if ((ret = create_term_list(di, py_dict, &term_list)) < 0)
21dfd91d
UH
793 break;
794
795 /* Add the new condition to the PD instance's condition list. */
796 di->condition_list = g_slist_append(di->condition_list, term_list);
797 }
798
799 Py_DecRef(py_conditionlist);
800
514b2edc
UH
801 PyGILState_Release(gstate);
802
21dfd91d 803 return ret;
514b2edc
UH
804
805err:
806 PyGILState_Release(gstate);
807
808 return SRD_ERR;
809
810ret_9999:
811 PyGILState_Release(gstate);
812
813 return 9999;
21dfd91d
UH
814}
815
96e1cee7
GS
816/**
817 * Create a SKIP condition list for condition-less .wait() calls.
818 *
819 * @param di Decoder instance.
820 * @param count Number of samples to skip.
821 *
822 * @retval SRD_OK The new condition list was set successfully.
823 * @retval SRD_ERR There was an error setting the new condition list.
824 * The contents of di->condition_list are undefined.
825 *
826 * This routine is a reduced and specialized version of the @ref
827 * set_new_condition_list() and @ref create_term_list() routines which
828 * gets invoked when .wait() was called without specifications for
829 * conditions. This minor duplication of the SKIP term list creation
830 * simplifies the logic and avoids the creation of expensive Python
831 * objects with "constant" values which the caller did not pass in the
832 * first place. It results in maximum sharing of match handling code
833 * paths.
834 */
835static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
836{
837 struct srd_term *term;
838 GSList *term_list;
839
840 condition_list_free(di);
35c10c0e 841 term = g_malloc(sizeof(*term));
96e1cee7
GS
842 term->type = SRD_TERM_SKIP;
843 term->num_samples_to_skip = count;
844 term->num_samples_already_skipped = 0;
845 term_list = g_slist_append(NULL, term);
846 di->condition_list = g_slist_append(di->condition_list, term_list);
847
848 return SRD_OK;
849}
850
21dfd91d
UH
851static PyObject *Decoder_wait(PyObject *self, PyObject *args)
852{
853 int ret;
96e1cee7 854 uint64_t skip_count;
21dfd91d
UH
855 unsigned int i;
856 gboolean found_match;
857 struct srd_decoder_inst *di;
e2768fbc 858 PyObject *py_pinvalues, *py_matched, *py_samplenum;
514b2edc 859 PyGILState_STATE gstate;
21dfd91d
UH
860
861 if (!self || !args)
862 return NULL;
863
514b2edc
UH
864 gstate = PyGILState_Ensure();
865
21dfd91d
UH
866 if (!(di = srd_inst_find_by_obj(NULL, self))) {
867 PyErr_SetString(PyExc_Exception, "decoder instance not found");
514b2edc 868 PyGILState_Release(gstate);
21dfd91d
UH
869 Py_RETURN_NONE;
870 }
871
872 ret = set_new_condition_list(self, args);
04383ea8
GS
873 if (ret < 0) {
874 srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
514b2edc 875 goto err;
04383ea8 876 }
21dfd91d 877 if (ret == 9999) {
96e1cee7
GS
878 /*
879 * Empty condition list, automatic match. Arrange for the
880 * execution of regular match handling code paths such that
881 * the next available sample is returned to the caller.
882 * Make sure to skip one sample when "anywhere within the
883 * stream", yet make sure to not skip sample number 0.
884 */
885 if (di->abs_cur_samplenum)
886 skip_count = 1;
887 else if (!di->condition_list)
888 skip_count = 0;
889 else
890 skip_count = 1;
891 ret = set_skip_condition(di, skip_count);
892 if (ret < 0) {
893 srd_dbg("%s: %s: Cannot setup condition-less wait().",
894 di->inst_id, __func__);
514b2edc 895 goto err;
96e1cee7 896 }
21dfd91d
UH
897 }
898
899 while (1) {
514b2edc
UH
900
901 Py_BEGIN_ALLOW_THREADS
902
04383ea8 903 /* Wait for new samples to process, or termination request. */
21dfd91d 904 g_mutex_lock(&di->data_mutex);
04383ea8 905 while (!di->got_new_samples && !di->want_wait_terminate)
21dfd91d
UH
906 g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
907
04383ea8
GS
908 /*
909 * Check whether any of the current condition(s) match.
910 * Arrange for termination requests to take a code path which
911 * won't find new samples to process, pretends to have processed
912 * previously stored samples, and returns to the main thread,
913 * while the termination request still gets signalled.
914 */
915 found_match = FALSE;
60d4764a
UH
916
917 /* Ignore return value for now, should never be negative. */
918 (void)process_samples_until_condition_match(di, &found_match);
21dfd91d 919
514b2edc
UH
920 Py_END_ALLOW_THREADS
921
21dfd91d
UH
922 /* If there's a match, set self.samplenum etc. and return. */
923 if (found_match) {
924 /* Set self.samplenum to the (absolute) sample number that matched. */
e2768fbc
UH
925 py_samplenum = PyLong_FromLong(di->abs_cur_samplenum);
926 PyObject_SetAttrString(di->py_inst, "samplenum", py_samplenum);
927 Py_DECREF(py_samplenum);
21dfd91d
UH
928
929 if (di->match_array && di->match_array->len > 0) {
930 py_matched = PyTuple_New(di->match_array->len);
931 for (i = 0; i < di->match_array->len; i++)
932 PyTuple_SetItem(py_matched, i, PyBool_FromLong(di->match_array->data[i]));
933 PyObject_SetAttrString(di->py_inst, "matched", py_matched);
e2768fbc 934 Py_DECREF(py_matched);
21dfd91d
UH
935 match_array_free(di);
936 } else {
937 PyObject_SetAttrString(di->py_inst, "matched", Py_None);
938 }
fa168be6 939
21dfd91d
UH
940 py_pinvalues = get_current_pinvalues(di);
941
942 g_mutex_unlock(&di->data_mutex);
943
514b2edc
UH
944 PyGILState_Release(gstate);
945
21dfd91d
UH
946 return py_pinvalues;
947 }
948
949 /* No match, reset state for the next chunk. */
950 di->got_new_samples = FALSE;
951 di->handled_all_samples = TRUE;
4564e8e5
UH
952 di->abs_start_samplenum = 0;
953 di->abs_end_samplenum = 0;
21dfd91d
UH
954 di->inbuf = NULL;
955 di->inbuflen = 0;
956
957 /* Signal the main thread that we handled all samples. */
958 g_cond_signal(&di->handled_all_samples_cond);
959
04383ea8
GS
960 /*
961 * When termination of wait() and decode() was requested,
962 * then exit the loop after releasing the mutex.
963 */
964 if (di->want_wait_terminate) {
965 srd_dbg("%s: %s: Will return from wait().",
966 di->inst_id, __func__);
967 g_mutex_unlock(&di->data_mutex);
514b2edc 968 goto err;
04383ea8
GS
969 }
970
21dfd91d
UH
971 g_mutex_unlock(&di->data_mutex);
972 }
973
514b2edc
UH
974 PyGILState_Release(gstate);
975
21dfd91d 976 Py_RETURN_NONE;
514b2edc
UH
977
978err:
979 PyGILState_Release(gstate);
980
981 return NULL;
21dfd91d
UH
982}
983
984/**
985 * Return whether the specified channel was supplied to the decoder.
986 *
987 * @param self TODO. Must not be NULL.
988 * @param args TODO. Must not be NULL.
989 *
990 * @retval Py_True The channel has been supplied by the frontend.
991 * @retval Py_False The channel has been supplied by the frontend.
992 * @retval NULL An error occurred.
993 */
994static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
995{
cd8d9188 996 int idx, count;
21dfd91d 997 struct srd_decoder_inst *di;
514b2edc 998 PyGILState_STATE gstate;
21dfd91d
UH
999
1000 if (!self || !args)
1001 return NULL;
1002
514b2edc
UH
1003 gstate = PyGILState_Ensure();
1004
21dfd91d
UH
1005 if (!(di = srd_inst_find_by_obj(NULL, self))) {
1006 PyErr_SetString(PyExc_Exception, "decoder instance not found");
514b2edc 1007 goto err;
21dfd91d
UH
1008 }
1009
cd8d9188
GS
1010 /*
1011 * Get the integer argument of self.has_channel(). Check for
1012 * the range of supported PD input channel numbers.
1013 */
1014 if (!PyArg_ParseTuple(args, "i", &idx)) {
21dfd91d 1015 /* Let Python raise this exception. */
514b2edc 1016 goto err;
21dfd91d
UH
1017 }
1018
cd8d9188
GS
1019 count = g_slist_length(di->decoder->channels) +
1020 g_slist_length(di->decoder->opt_channels);
1021 if (idx < 0 || idx >= count) {
1022 srd_err("Invalid index %d, PD channel count %d.", idx, count);
1023 PyErr_SetString(PyExc_IndexError, "invalid channel index");
514b2edc 1024 goto err;
21dfd91d
UH
1025 }
1026
514b2edc
UH
1027 PyGILState_Release(gstate);
1028
21dfd91d 1029 return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True;
514b2edc
UH
1030
1031err:
1032 PyGILState_Release(gstate);
1033
1034 return NULL;
21dfd91d
UH
1035}
1036
d0a0ed03 1037static PyMethodDef Decoder_methods[] = {
7969d803
UH
1038 { "put", Decoder_put, METH_VARARGS,
1039 "Accepts a dictionary with the following keys: startsample, endsample, data" },
1040 { "register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS,
1041 "Register a new output stream" },
1042 { "wait", Decoder_wait, METH_VARARGS,
1043 "Wait for one or more conditions to occur" },
1044 { "has_channel", Decoder_has_channel, METH_VARARGS,
1045 "Report whether a channel was supplied" },
d0a0ed03
BV
1046 {NULL, NULL, 0, NULL}
1047};
1048
21dfd91d
UH
1049/**
1050 * Create the sigrokdecode.Decoder type.
1051 *
201a85a8 1052 * @return The new type object.
21dfd91d 1053 *
201a85a8
DE
1054 * @private
1055 */
1056SRD_PRIV PyObject *srd_Decoder_type_new(void)
1057{
1058 PyType_Spec spec;
1059 PyType_Slot slots[] = {
1060 { Py_tp_doc, "sigrok Decoder base class" },
1061 { Py_tp_methods, Decoder_methods },
1062 { Py_tp_new, (void *)&PyType_GenericNew },
1063 { 0, NULL }
1064 };
514b2edc
UH
1065 PyObject *py_obj;
1066 PyGILState_STATE gstate;
1067
1068 gstate = PyGILState_Ensure();
1069
201a85a8
DE
1070 spec.name = "sigrokdecode.Decoder";
1071 spec.basicsize = sizeof(srd_Decoder);
1072 spec.itemsize = 0;
1073 spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1074 spec.slots = slots;
1075
514b2edc
UH
1076 py_obj = PyType_FromSpec(&spec);
1077
1078 PyGILState_Release(gstate);
1079
1080 return py_obj;
201a85a8 1081}