]>
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 | |
a4e9ca6f UH |
25 | /** @cond PRIVATE */ |
26 | extern SRD_PRIV GSList *sessions; | |
27 | /** @endcond */ | |
28 | ||
17475b09 UH |
29 | typedef struct { |
30 | PyObject_HEAD | |
31 | } srd_Decoder; | |
32 | ||
7969d803 | 33 | /* This is only used for nicer srd_dbg() output. */ |
201a85a8 DE |
34 | static const char *output_type_name(unsigned int idx) |
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 |
47 | static 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 | 55 | static 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 | |
120 | err: | |
121 | PyGILState_Release(gstate); | |
122 | ||
123 | return SRD_ERR_PYTHON; | |
d0a0ed03 BV |
124 | } |
125 | ||
c1b2bbc1 GS |
126 | static 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 |
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 | ||
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 | |
203 | err: | |
204 | PyGILState_Release(gstate); | |
205 | ||
206 | return SRD_ERR_PYTHON; | |
d75d8a7c BV |
207 | } |
208 | ||
29adc171 | 209 | static 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 | 246 | static 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 |
268 | static 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 | |
302 | err: | |
303 | PyGILState_Release(gstate); | |
304 | ||
305 | return SRD_ERR_PYTHON; | |
7ee0c40b BV |
306 | } |
307 | ||
287e2788 UH |
308 | static void release_meta(GVariant *gvar) |
309 | { | |
310 | if (!gvar) | |
311 | return; | |
312 | g_variant_unref(gvar); | |
313 | } | |
314 | ||
d0a0ed03 BV |
315 | static 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) { | |
358 | srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.", | |
359 | di->inst_id, start_sample, end_sample, | |
360 | output_type_name(pdo->output_type), output_id); | |
361 | } | |
58572aed | 362 | |
8a9f60b1 UH |
363 | pdata.start_sample = start_sample; |
364 | pdata.end_sample = end_sample; | |
365 | pdata.pdo = pdo; | |
366 | pdata.data = NULL; | |
d0a0ed03 BV |
367 | |
368 | switch (pdo->output_type) { | |
369 | case SRD_OUTPUT_ANN: | |
370 | /* Annotations are only fed to callbacks. */ | |
32cfb920 | 371 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
ee63c5dd | 372 | pdata.data = &pda; |
d75d8a7c | 373 | /* Convert from PyDict to srd_proto_data_annotation. */ |
8a9f60b1 | 374 | if (convert_annotation(di, py_data, &pdata) != SRD_OK) { |
d0a0ed03 BV |
375 | /* An error was already logged. */ |
376 | break; | |
377 | } | |
514b2edc | 378 | Py_BEGIN_ALLOW_THREADS |
8a9f60b1 | 379 | cb->cb(&pdata, cb->cb_data); |
514b2edc | 380 | Py_END_ALLOW_THREADS |
c1b2bbc1 | 381 | release_annotation(pdata.data); |
d0a0ed03 BV |
382 | } |
383 | break; | |
f2a5df42 | 384 | case SRD_OUTPUT_PYTHON: |
d0a0ed03 BV |
385 | for (l = di->next_di; l; l = l->next) { |
386 | next_di = l->data; | |
43dc0396 UH |
387 | srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on " |
388 | "oid %d to instance %s.", di->inst_id, start_sample, | |
389 | end_sample, output_type_name(pdo->output_type), | |
390 | output_id, next_di->inst_id); | |
c9bfccc6 | 391 | if (!(py_res = PyObject_CallMethod( |
3d14e7c9 BV |
392 | next_di->py_inst, "decode", "KKO", start_sample, |
393 | end_sample, py_data))) { | |
201a85a8 | 394 | srd_exception_catch("Calling %s decode() failed", |
3d14e7c9 | 395 | next_di->inst_id); |
d0a0ed03 BV |
396 | } |
397 | Py_XDECREF(py_res); | |
398 | } | |
3d14e7c9 | 399 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
7969d803 UH |
400 | /* |
401 | * Frontends aren't really supposed to get Python | |
402 | * callbacks, but it's useful for testing. | |
403 | */ | |
8a9f60b1 UH |
404 | pdata.data = py_data; |
405 | cb->cb(&pdata, cb->cb_data); | |
3d14e7c9 | 406 | } |
d0a0ed03 BV |
407 | break; |
408 | case SRD_OUTPUT_BINARY: | |
d75d8a7c | 409 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
c94631f1 | 410 | pdata.data = &pdb; |
d75d8a7c | 411 | /* Convert from PyDict to srd_proto_data_binary. */ |
8a9f60b1 | 412 | if (convert_binary(di, py_data, &pdata) != SRD_OK) { |
d75d8a7c BV |
413 | /* An error was already logged. */ |
414 | break; | |
415 | } | |
514b2edc | 416 | Py_BEGIN_ALLOW_THREADS |
8a9f60b1 | 417 | cb->cb(&pdata, cb->cb_data); |
514b2edc | 418 | Py_END_ALLOW_THREADS |
c1b2bbc1 | 419 | release_binary(pdata.data); |
d75d8a7c | 420 | } |
d0a0ed03 | 421 | break; |
7ee0c40b BV |
422 | case SRD_OUTPUT_META: |
423 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { | |
424 | /* Annotations need converting from PyObject. */ | |
8a9f60b1 | 425 | if (convert_meta(&pdata, py_data) != SRD_OK) { |
7ee0c40b BV |
426 | /* An exception was already set up. */ |
427 | break; | |
428 | } | |
514b2edc | 429 | Py_BEGIN_ALLOW_THREADS |
8a9f60b1 | 430 | cb->cb(&pdata, cb->cb_data); |
514b2edc | 431 | Py_END_ALLOW_THREADS |
287e2788 | 432 | release_meta(pdata.data); |
7ee0c40b BV |
433 | } |
434 | break; | |
d0a0ed03 | 435 | default: |
d906d3f9 | 436 | srd_err("Protocol decoder %s submitted invalid output type %d.", |
c9bfccc6 | 437 | di->decoder->name, pdo->output_type); |
d0a0ed03 BV |
438 | break; |
439 | } | |
440 | ||
514b2edc UH |
441 | PyGILState_Release(gstate); |
442 | ||
d0a0ed03 | 443 | Py_RETURN_NONE; |
514b2edc UH |
444 | |
445 | err: | |
446 | PyGILState_Release(gstate); | |
447 | ||
448 | return NULL; | |
d0a0ed03 BV |
449 | } |
450 | ||
7ee0c40b BV |
451 | static PyObject *Decoder_register(PyObject *self, PyObject *args, |
452 | PyObject *kwargs) | |
d0a0ed03 | 453 | { |
a8b72b05 | 454 | struct srd_decoder_inst *di; |
7ee0c40b BV |
455 | struct srd_pd_output *pdo; |
456 | PyObject *py_new_output_id; | |
457 | PyTypeObject *meta_type_py; | |
458 | const GVariantType *meta_type_gv; | |
459 | int output_type; | |
460 | char *proto_id, *meta_name, *meta_descr; | |
7969d803 | 461 | char *keywords[] = { "output_type", "proto_id", "meta", NULL }; |
514b2edc | 462 | PyGILState_STATE gstate; |
7d82e3c1 GS |
463 | gboolean is_meta; |
464 | GSList *l; | |
465 | struct srd_pd_output *cmp; | |
514b2edc UH |
466 | |
467 | gstate = PyGILState_Ensure(); | |
7ee0c40b BV |
468 | |
469 | meta_type_py = NULL; | |
470 | meta_type_gv = NULL; | |
471 | meta_name = meta_descr = NULL; | |
d0a0ed03 | 472 | |
a8b72b05 | 473 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
d0a0ed03 | 474 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); |
514b2edc | 475 | goto err; |
d0a0ed03 BV |
476 | } |
477 | ||
7969d803 | 478 | /* Default to instance ID, which defaults to class ID. */ |
7ee0c40b BV |
479 | proto_id = di->inst_id; |
480 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords, | |
481 | &output_type, &proto_id, | |
482 | &meta_type_py, &meta_name, &meta_descr)) { | |
511e2123 | 483 | /* Let Python raise this exception. */ |
514b2edc | 484 | goto err; |
d0a0ed03 BV |
485 | } |
486 | ||
7ee0c40b | 487 | /* Check if the meta value's type is supported. */ |
7d82e3c1 GS |
488 | is_meta = output_type == SRD_OUTPUT_META; |
489 | if (is_meta) { | |
7ee0c40b BV |
490 | if (meta_type_py == &PyLong_Type) |
491 | meta_type_gv = G_VARIANT_TYPE_INT64; | |
492 | else if (meta_type_py == &PyFloat_Type) | |
493 | meta_type_gv = G_VARIANT_TYPE_DOUBLE; | |
494 | else { | |
201a85a8 | 495 | PyErr_Format(PyExc_TypeError, "Unsupported type."); |
514b2edc | 496 | goto err; |
7ee0c40b BV |
497 | } |
498 | } | |
499 | ||
7d82e3c1 GS |
500 | pdo = NULL; |
501 | for (l = di->pd_output; l; l = l->next) { | |
502 | cmp = l->data; | |
503 | if (cmp->output_type != output_type) | |
504 | continue; | |
505 | if (strcmp(cmp->proto_id, proto_id) != 0) | |
506 | continue; | |
507 | if (is_meta && cmp->meta_type != meta_type_gv) | |
508 | continue; | |
509 | if (is_meta && strcmp(cmp->meta_name, meta_name) != 0) | |
510 | continue; | |
511 | if (is_meta && strcmp(cmp->meta_descr, meta_descr) != 0) | |
512 | continue; | |
513 | pdo = cmp; | |
514 | break; | |
515 | } | |
516 | if (pdo) { | |
517 | py_new_output_id = Py_BuildValue("i", pdo->pdo_id); | |
518 | PyGILState_Release(gstate); | |
519 | return py_new_output_id; | |
520 | } | |
521 | ||
7ee0c40b BV |
522 | srd_dbg("Instance %s creating new output type %d for %s.", |
523 | di->inst_id, output_type, proto_id); | |
524 | ||
077fa8ac | 525 | pdo = g_malloc(sizeof(struct srd_pd_output)); |
7ee0c40b BV |
526 | |
527 | /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ | |
528 | pdo->pdo_id = g_slist_length(di->pd_output); | |
529 | pdo->output_type = output_type; | |
530 | pdo->di = di; | |
531 | pdo->proto_id = g_strdup(proto_id); | |
532 | ||
533 | if (output_type == SRD_OUTPUT_META) { | |
534 | pdo->meta_type = meta_type_gv; | |
535 | pdo->meta_name = g_strdup(meta_name); | |
536 | pdo->meta_descr = g_strdup(meta_descr); | |
537 | } | |
538 | ||
539 | di->pd_output = g_slist_append(di->pd_output, pdo); | |
540 | py_new_output_id = Py_BuildValue("i", pdo->pdo_id); | |
541 | ||
514b2edc UH |
542 | PyGILState_Release(gstate); |
543 | ||
7ee0c40b | 544 | return py_new_output_id; |
514b2edc UH |
545 | |
546 | err: | |
547 | PyGILState_Release(gstate); | |
548 | ||
549 | return NULL; | |
7ee0c40b BV |
550 | } |
551 | ||
21dfd91d UH |
552 | static int get_term_type(const char *v) |
553 | { | |
554 | switch (v[0]) { | |
555 | case 'h': | |
556 | return SRD_TERM_HIGH; | |
557 | case 'l': | |
558 | return SRD_TERM_LOW; | |
559 | case 'r': | |
560 | return SRD_TERM_RISING_EDGE; | |
561 | case 'f': | |
562 | return SRD_TERM_FALLING_EDGE; | |
563 | case 'e': | |
564 | return SRD_TERM_EITHER_EDGE; | |
565 | case 'n': | |
566 | return SRD_TERM_NO_EDGE; | |
4e7ccaf9 UH |
567 | default: |
568 | return -1; | |
21dfd91d UH |
569 | } |
570 | ||
571 | return -1; | |
572 | } | |
573 | ||
574 | /** | |
575 | * Get the pin values at the current sample number. | |
576 | * | |
577 | * @param di The decoder instance to use. Must not be NULL. | |
578 | * The number of channels must be >= 1. | |
579 | * | |
580 | * @return A newly allocated PyTuple containing the pin values at the | |
581 | * current sample number. | |
582 | */ | |
583 | static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di) | |
584 | { | |
585 | int i; | |
586 | uint8_t sample; | |
587 | const uint8_t *sample_pos; | |
588 | int byte_offset, bit_offset; | |
589 | PyObject *py_pinvalues; | |
514b2edc UH |
590 | PyGILState_STATE gstate; |
591 | ||
21dfd91d UH |
592 | if (!di) { |
593 | srd_err("Invalid decoder instance."); | |
594 | return NULL; | |
595 | } | |
596 | ||
73578d2e UH |
597 | gstate = PyGILState_Ensure(); |
598 | ||
21dfd91d UH |
599 | py_pinvalues = PyTuple_New(di->dec_num_channels); |
600 | ||
601 | for (i = 0; i < di->dec_num_channels; i++) { | |
602 | /* A channelmap value of -1 means "unused optional channel". */ | |
603 | if (di->dec_channelmap[i] == -1) { | |
604 | /* Value of unused channel is 0xff, instead of 0 or 1. */ | |
605 | PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff)); | |
606 | } else { | |
4564e8e5 | 607 | sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize); |
21dfd91d UH |
608 | byte_offset = di->dec_channelmap[i] / 8; |
609 | bit_offset = di->dec_channelmap[i] % 8; | |
610 | sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0; | |
611 | PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(sample)); | |
612 | } | |
613 | } | |
614 | ||
514b2edc UH |
615 | PyGILState_Release(gstate); |
616 | ||
21dfd91d UH |
617 | return py_pinvalues; |
618 | } | |
619 | ||
620 | /** | |
621 | * Create a list of terms in the specified condition. | |
622 | * | |
623 | * If there are no terms in the condition, 'term_list' will be NULL. | |
624 | * | |
625 | * @param py_dict A Python dict containing terms. Must not be NULL. | |
626 | * @param term_list Pointer to a GSList which will be set to the newly | |
627 | * created list of terms. Must not be NULL. | |
628 | * | |
629 | * @return SRD_OK upon success, a negative error code otherwise. | |
630 | */ | |
631 | static int create_term_list(PyObject *py_dict, GSList **term_list) | |
632 | { | |
633 | Py_ssize_t pos = 0; | |
634 | PyObject *py_key, *py_value; | |
635 | struct srd_term *term; | |
636 | uint64_t num_samples_to_skip; | |
637 | char *term_str; | |
514b2edc | 638 | PyGILState_STATE gstate; |
21dfd91d UH |
639 | |
640 | if (!py_dict || !term_list) | |
641 | return SRD_ERR_ARG; | |
642 | ||
643 | /* "Create" an empty GSList of terms. */ | |
644 | *term_list = NULL; | |
645 | ||
514b2edc UH |
646 | gstate = PyGILState_Ensure(); |
647 | ||
21dfd91d UH |
648 | /* Iterate over all items in the current dict. */ |
649 | while (PyDict_Next(py_dict, &pos, &py_key, &py_value)) { | |
650 | /* Check whether the current key is a string or a number. */ | |
651 | if (PyLong_Check(py_key)) { | |
652 | /* The key is a number. */ | |
653 | /* TODO: Check if the number is a valid channel. */ | |
654 | /* Get the value string. */ | |
655 | if ((py_pydictitem_as_str(py_dict, py_key, &term_str)) != SRD_OK) { | |
656 | srd_err("Failed to get the value."); | |
514b2edc | 657 | goto err; |
21dfd91d | 658 | } |
35c10c0e | 659 | term = g_malloc(sizeof(struct srd_term)); |
21dfd91d UH |
660 | term->type = get_term_type(term_str); |
661 | term->channel = PyLong_AsLong(py_key); | |
662 | g_free(term_str); | |
663 | } else if (PyUnicode_Check(py_key)) { | |
664 | /* The key is a string. */ | |
665 | /* TODO: Check if it's "skip". */ | |
666 | if ((py_pydictitem_as_long(py_dict, py_key, &num_samples_to_skip)) != SRD_OK) { | |
667 | srd_err("Failed to get number of samples to skip."); | |
514b2edc | 668 | goto err; |
21dfd91d | 669 | } |
35c10c0e | 670 | term = g_malloc(sizeof(struct srd_term)); |
21dfd91d UH |
671 | term->type = SRD_TERM_SKIP; |
672 | term->num_samples_to_skip = num_samples_to_skip; | |
673 | term->num_samples_already_skipped = 0; | |
674 | } else { | |
675 | srd_err("Term key is neither a string nor a number."); | |
514b2edc | 676 | goto err; |
21dfd91d UH |
677 | } |
678 | ||
679 | /* Add the term to the list of terms. */ | |
680 | *term_list = g_slist_append(*term_list, term); | |
681 | } | |
682 | ||
514b2edc UH |
683 | PyGILState_Release(gstate); |
684 | ||
21dfd91d | 685 | return SRD_OK; |
514b2edc UH |
686 | |
687 | err: | |
688 | PyGILState_Release(gstate); | |
689 | ||
690 | return SRD_ERR; | |
21dfd91d UH |
691 | } |
692 | ||
693 | /** | |
694 | * Replace the current condition list with the new one. | |
695 | * | |
696 | * @param self TODO. Must not be NULL. | |
697 | * @param args TODO. Must not be NULL. | |
698 | * | |
699 | * @retval SRD_OK The new condition list was set successfully. | |
700 | * @retval SRD_ERR There was an error setting the new condition list. | |
701 | * The contents of di->condition_list are undefined. | |
702 | * @retval 9999 TODO. | |
703 | */ | |
704 | static int set_new_condition_list(PyObject *self, PyObject *args) | |
705 | { | |
706 | struct srd_decoder_inst *di; | |
707 | GSList *term_list; | |
708 | PyObject *py_conditionlist, *py_conds, *py_dict; | |
709 | int i, num_conditions, ret; | |
514b2edc | 710 | PyGILState_STATE gstate; |
21dfd91d UH |
711 | |
712 | if (!self || !args) | |
713 | return SRD_ERR_ARG; | |
714 | ||
514b2edc UH |
715 | gstate = PyGILState_Ensure(); |
716 | ||
21dfd91d UH |
717 | /* Get the decoder instance. */ |
718 | if (!(di = srd_inst_find_by_obj(NULL, self))) { | |
719 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); | |
514b2edc | 720 | goto err; |
21dfd91d UH |
721 | } |
722 | ||
04383ea8 GS |
723 | /* |
724 | * Return an error condition from .wait() when termination is | |
725 | * requested, such that decode() will terminate. | |
726 | */ | |
727 | if (di->want_wait_terminate) { | |
728 | srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__); | |
514b2edc | 729 | goto err; |
04383ea8 GS |
730 | } |
731 | ||
6e19f325 GS |
732 | /* |
733 | * Parse the argument of self.wait() into 'py_conds', and check | |
734 | * the data type. The argument is optional, None is assumed in | |
735 | * its absence. None or an empty dict or an empty list mean that | |
736 | * there is no condition, and the next available sample shall | |
737 | * get returned to the caller. | |
738 | */ | |
739 | py_conds = Py_None; | |
740 | if (!PyArg_ParseTuple(args, "|O", &py_conds)) { | |
21dfd91d | 741 | /* Let Python raise this exception. */ |
514b2edc | 742 | goto err; |
21dfd91d | 743 | } |
6e19f325 GS |
744 | if (py_conds == Py_None) { |
745 | /* 'py_conds' is None. */ | |
514b2edc | 746 | goto ret_9999; |
6e19f325 | 747 | } else if (PyList_Check(py_conds)) { |
21dfd91d UH |
748 | /* 'py_conds' is a list. */ |
749 | py_conditionlist = py_conds; | |
750 | num_conditions = PyList_Size(py_conditionlist); | |
751 | if (num_conditions == 0) | |
514b2edc | 752 | goto ret_9999; /* The PD invoked self.wait([]). */ |
066fbafd | 753 | Py_IncRef(py_conditionlist); |
21dfd91d UH |
754 | } else if (PyDict_Check(py_conds)) { |
755 | /* 'py_conds' is a dict. */ | |
756 | if (PyDict_Size(py_conds) == 0) | |
514b2edc | 757 | goto ret_9999; /* The PD invoked self.wait({}). */ |
21dfd91d UH |
758 | /* Make a list and put the dict in there for convenience. */ |
759 | py_conditionlist = PyList_New(1); | |
01f15bdf | 760 | Py_IncRef(py_conds); |
21dfd91d UH |
761 | PyList_SetItem(py_conditionlist, 0, py_conds); |
762 | num_conditions = 1; | |
763 | } else { | |
764 | srd_err("Condition list is neither a list nor a dict."); | |
514b2edc | 765 | goto err; |
21dfd91d UH |
766 | } |
767 | ||
768 | /* Free the old condition list. */ | |
769 | condition_list_free(di); | |
770 | ||
771 | ret = SRD_OK; | |
772 | ||
773 | /* Iterate over the conditions, set di->condition_list accordingly. */ | |
774 | for (i = 0; i < num_conditions; i++) { | |
775 | /* Get a condition (dict) from the condition list. */ | |
776 | py_dict = PyList_GetItem(py_conditionlist, i); | |
777 | if (!PyDict_Check(py_dict)) { | |
778 | srd_err("Condition is not a dict."); | |
779 | ret = SRD_ERR; | |
780 | break; | |
781 | } | |
782 | ||
783 | /* Create the list of terms in this condition. */ | |
784 | if ((ret = create_term_list(py_dict, &term_list)) < 0) | |
785 | break; | |
786 | ||
787 | /* Add the new condition to the PD instance's condition list. */ | |
788 | di->condition_list = g_slist_append(di->condition_list, term_list); | |
789 | } | |
790 | ||
791 | Py_DecRef(py_conditionlist); | |
792 | ||
514b2edc UH |
793 | PyGILState_Release(gstate); |
794 | ||
21dfd91d | 795 | return ret; |
514b2edc UH |
796 | |
797 | err: | |
798 | PyGILState_Release(gstate); | |
799 | ||
800 | return SRD_ERR; | |
801 | ||
802 | ret_9999: | |
803 | PyGILState_Release(gstate); | |
804 | ||
805 | return 9999; | |
21dfd91d UH |
806 | } |
807 | ||
96e1cee7 GS |
808 | /** |
809 | * Create a SKIP condition list for condition-less .wait() calls. | |
810 | * | |
811 | * @param di Decoder instance. | |
812 | * @param count Number of samples to skip. | |
813 | * | |
814 | * @retval SRD_OK The new condition list was set successfully. | |
815 | * @retval SRD_ERR There was an error setting the new condition list. | |
816 | * The contents of di->condition_list are undefined. | |
817 | * | |
818 | * This routine is a reduced and specialized version of the @ref | |
819 | * set_new_condition_list() and @ref create_term_list() routines which | |
820 | * gets invoked when .wait() was called without specifications for | |
821 | * conditions. This minor duplication of the SKIP term list creation | |
822 | * simplifies the logic and avoids the creation of expensive Python | |
823 | * objects with "constant" values which the caller did not pass in the | |
824 | * first place. It results in maximum sharing of match handling code | |
825 | * paths. | |
826 | */ | |
827 | static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count) | |
828 | { | |
829 | struct srd_term *term; | |
830 | GSList *term_list; | |
831 | ||
832 | condition_list_free(di); | |
35c10c0e | 833 | term = g_malloc(sizeof(*term)); |
96e1cee7 GS |
834 | term->type = SRD_TERM_SKIP; |
835 | term->num_samples_to_skip = count; | |
836 | term->num_samples_already_skipped = 0; | |
837 | term_list = g_slist_append(NULL, term); | |
838 | di->condition_list = g_slist_append(di->condition_list, term_list); | |
839 | ||
840 | return SRD_OK; | |
841 | } | |
842 | ||
21dfd91d UH |
843 | static PyObject *Decoder_wait(PyObject *self, PyObject *args) |
844 | { | |
845 | int ret; | |
96e1cee7 | 846 | uint64_t skip_count; |
21dfd91d UH |
847 | unsigned int i; |
848 | gboolean found_match; | |
849 | struct srd_decoder_inst *di; | |
850 | PyObject *py_pinvalues, *py_matched; | |
514b2edc | 851 | PyGILState_STATE gstate; |
21dfd91d UH |
852 | |
853 | if (!self || !args) | |
854 | return NULL; | |
855 | ||
514b2edc UH |
856 | gstate = PyGILState_Ensure(); |
857 | ||
21dfd91d UH |
858 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
859 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); | |
514b2edc | 860 | PyGILState_Release(gstate); |
21dfd91d UH |
861 | Py_RETURN_NONE; |
862 | } | |
863 | ||
864 | ret = set_new_condition_list(self, args); | |
04383ea8 GS |
865 | if (ret < 0) { |
866 | srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__); | |
514b2edc | 867 | goto err; |
04383ea8 | 868 | } |
21dfd91d | 869 | if (ret == 9999) { |
96e1cee7 GS |
870 | /* |
871 | * Empty condition list, automatic match. Arrange for the | |
872 | * execution of regular match handling code paths such that | |
873 | * the next available sample is returned to the caller. | |
874 | * Make sure to skip one sample when "anywhere within the | |
875 | * stream", yet make sure to not skip sample number 0. | |
876 | */ | |
877 | if (di->abs_cur_samplenum) | |
878 | skip_count = 1; | |
879 | else if (!di->condition_list) | |
880 | skip_count = 0; | |
881 | else | |
882 | skip_count = 1; | |
883 | ret = set_skip_condition(di, skip_count); | |
884 | if (ret < 0) { | |
885 | srd_dbg("%s: %s: Cannot setup condition-less wait().", | |
886 | di->inst_id, __func__); | |
514b2edc | 887 | goto err; |
96e1cee7 | 888 | } |
21dfd91d UH |
889 | } |
890 | ||
891 | while (1) { | |
514b2edc UH |
892 | |
893 | Py_BEGIN_ALLOW_THREADS | |
894 | ||
04383ea8 | 895 | /* Wait for new samples to process, or termination request. */ |
21dfd91d | 896 | g_mutex_lock(&di->data_mutex); |
04383ea8 | 897 | while (!di->got_new_samples && !di->want_wait_terminate) |
21dfd91d UH |
898 | g_cond_wait(&di->got_new_samples_cond, &di->data_mutex); |
899 | ||
04383ea8 GS |
900 | /* |
901 | * Check whether any of the current condition(s) match. | |
902 | * Arrange for termination requests to take a code path which | |
903 | * won't find new samples to process, pretends to have processed | |
904 | * previously stored samples, and returns to the main thread, | |
905 | * while the termination request still gets signalled. | |
906 | */ | |
907 | found_match = FALSE; | |
60d4764a UH |
908 | |
909 | /* Ignore return value for now, should never be negative. */ | |
910 | (void)process_samples_until_condition_match(di, &found_match); | |
21dfd91d | 911 | |
514b2edc UH |
912 | Py_END_ALLOW_THREADS |
913 | ||
21dfd91d UH |
914 | /* If there's a match, set self.samplenum etc. and return. */ |
915 | if (found_match) { | |
916 | /* Set self.samplenum to the (absolute) sample number that matched. */ | |
917 | PyObject_SetAttrString(di->py_inst, "samplenum", | |
4564e8e5 | 918 | PyLong_FromLong(di->abs_cur_samplenum)); |
21dfd91d UH |
919 | |
920 | if (di->match_array && di->match_array->len > 0) { | |
921 | py_matched = PyTuple_New(di->match_array->len); | |
922 | for (i = 0; i < di->match_array->len; i++) | |
923 | PyTuple_SetItem(py_matched, i, PyBool_FromLong(di->match_array->data[i])); | |
924 | PyObject_SetAttrString(di->py_inst, "matched", py_matched); | |
925 | match_array_free(di); | |
926 | } else { | |
927 | PyObject_SetAttrString(di->py_inst, "matched", Py_None); | |
928 | } | |
fa168be6 | 929 | |
21dfd91d UH |
930 | py_pinvalues = get_current_pinvalues(di); |
931 | ||
932 | g_mutex_unlock(&di->data_mutex); | |
933 | ||
514b2edc UH |
934 | PyGILState_Release(gstate); |
935 | ||
21dfd91d UH |
936 | return py_pinvalues; |
937 | } | |
938 | ||
939 | /* No match, reset state for the next chunk. */ | |
940 | di->got_new_samples = FALSE; | |
941 | di->handled_all_samples = TRUE; | |
4564e8e5 UH |
942 | di->abs_start_samplenum = 0; |
943 | di->abs_end_samplenum = 0; | |
21dfd91d UH |
944 | di->inbuf = NULL; |
945 | di->inbuflen = 0; | |
946 | ||
947 | /* Signal the main thread that we handled all samples. */ | |
948 | g_cond_signal(&di->handled_all_samples_cond); | |
949 | ||
04383ea8 GS |
950 | /* |
951 | * When termination of wait() and decode() was requested, | |
952 | * then exit the loop after releasing the mutex. | |
953 | */ | |
954 | if (di->want_wait_terminate) { | |
955 | srd_dbg("%s: %s: Will return from wait().", | |
956 | di->inst_id, __func__); | |
957 | g_mutex_unlock(&di->data_mutex); | |
514b2edc | 958 | goto err; |
04383ea8 GS |
959 | } |
960 | ||
21dfd91d UH |
961 | g_mutex_unlock(&di->data_mutex); |
962 | } | |
963 | ||
514b2edc UH |
964 | PyGILState_Release(gstate); |
965 | ||
21dfd91d | 966 | Py_RETURN_NONE; |
514b2edc UH |
967 | |
968 | err: | |
969 | PyGILState_Release(gstate); | |
970 | ||
971 | return NULL; | |
21dfd91d UH |
972 | } |
973 | ||
974 | /** | |
975 | * Return whether the specified channel was supplied to the decoder. | |
976 | * | |
977 | * @param self TODO. Must not be NULL. | |
978 | * @param args TODO. Must not be NULL. | |
979 | * | |
980 | * @retval Py_True The channel has been supplied by the frontend. | |
981 | * @retval Py_False The channel has been supplied by the frontend. | |
982 | * @retval NULL An error occurred. | |
983 | */ | |
984 | static PyObject *Decoder_has_channel(PyObject *self, PyObject *args) | |
985 | { | |
cd8d9188 | 986 | int idx, count; |
21dfd91d | 987 | struct srd_decoder_inst *di; |
514b2edc | 988 | PyGILState_STATE gstate; |
21dfd91d UH |
989 | |
990 | if (!self || !args) | |
991 | return NULL; | |
992 | ||
514b2edc UH |
993 | gstate = PyGILState_Ensure(); |
994 | ||
21dfd91d UH |
995 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
996 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); | |
514b2edc | 997 | goto err; |
21dfd91d UH |
998 | } |
999 | ||
cd8d9188 GS |
1000 | /* |
1001 | * Get the integer argument of self.has_channel(). Check for | |
1002 | * the range of supported PD input channel numbers. | |
1003 | */ | |
1004 | if (!PyArg_ParseTuple(args, "i", &idx)) { | |
21dfd91d | 1005 | /* Let Python raise this exception. */ |
514b2edc | 1006 | goto err; |
21dfd91d UH |
1007 | } |
1008 | ||
cd8d9188 GS |
1009 | count = g_slist_length(di->decoder->channels) + |
1010 | g_slist_length(di->decoder->opt_channels); | |
1011 | if (idx < 0 || idx >= count) { | |
1012 | srd_err("Invalid index %d, PD channel count %d.", idx, count); | |
1013 | PyErr_SetString(PyExc_IndexError, "invalid channel index"); | |
514b2edc | 1014 | goto err; |
21dfd91d UH |
1015 | } |
1016 | ||
514b2edc UH |
1017 | PyGILState_Release(gstate); |
1018 | ||
21dfd91d | 1019 | return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True; |
514b2edc UH |
1020 | |
1021 | err: | |
1022 | PyGILState_Release(gstate); | |
1023 | ||
1024 | return NULL; | |
21dfd91d UH |
1025 | } |
1026 | ||
d0a0ed03 | 1027 | static PyMethodDef Decoder_methods[] = { |
7969d803 UH |
1028 | { "put", Decoder_put, METH_VARARGS, |
1029 | "Accepts a dictionary with the following keys: startsample, endsample, data" }, | |
1030 | { "register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS, | |
1031 | "Register a new output stream" }, | |
1032 | { "wait", Decoder_wait, METH_VARARGS, | |
1033 | "Wait for one or more conditions to occur" }, | |
1034 | { "has_channel", Decoder_has_channel, METH_VARARGS, | |
1035 | "Report whether a channel was supplied" }, | |
d0a0ed03 BV |
1036 | {NULL, NULL, 0, NULL} |
1037 | }; | |
1038 | ||
21dfd91d UH |
1039 | /** |
1040 | * Create the sigrokdecode.Decoder type. | |
1041 | * | |
201a85a8 | 1042 | * @return The new type object. |
21dfd91d | 1043 | * |
201a85a8 DE |
1044 | * @private |
1045 | */ | |
1046 | SRD_PRIV PyObject *srd_Decoder_type_new(void) | |
1047 | { | |
1048 | PyType_Spec spec; | |
1049 | PyType_Slot slots[] = { | |
1050 | { Py_tp_doc, "sigrok Decoder base class" }, | |
1051 | { Py_tp_methods, Decoder_methods }, | |
1052 | { Py_tp_new, (void *)&PyType_GenericNew }, | |
1053 | { 0, NULL } | |
1054 | }; | |
514b2edc UH |
1055 | PyObject *py_obj; |
1056 | PyGILState_STATE gstate; | |
1057 | ||
1058 | gstate = PyGILState_Ensure(); | |
1059 | ||
201a85a8 DE |
1060 | spec.name = "sigrokdecode.Decoder"; |
1061 | spec.basicsize = sizeof(srd_Decoder); | |
1062 | spec.itemsize = 0; | |
1063 | spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; | |
1064 | spec.slots = slots; | |
1065 | ||
514b2edc UH |
1066 | py_obj = PyType_FromSpec(&spec); |
1067 | ||
1068 | PyGILState_Release(gstate); | |
1069 | ||
1070 | return py_obj; | |
201a85a8 | 1071 | } |