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