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