]>
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 | ||
c1f86f02 UH |
20 | #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
21 | #include "libsigrokdecode-internal.h" | |
d0a0ed03 | 22 | #include "config.h" |
dbeaab27 | 23 | #include <inttypes.h> |
d0a0ed03 | 24 | |
17475b09 UH |
25 | typedef struct { |
26 | PyObject_HEAD | |
27 | } srd_Decoder; | |
28 | ||
58572aed | 29 | /* This is only used for nicer srd_dbg() output. */ |
abeeed8b | 30 | static const char *OUTPUT_TYPES[] = { |
58572aed | 31 | "OUTPUT_ANN", |
f2a5df42 | 32 | "OUTPUT_PYTHON", |
58572aed | 33 | "OUTPUT_BINARY", |
7ee0c40b | 34 | "OUTPUT_META", |
58572aed BV |
35 | }; |
36 | ||
4f75f1c1 | 37 | static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj, |
0b922460 | 38 | struct srd_proto_data *pdata) |
d0a0ed03 BV |
39 | { |
40 | PyObject *py_tmp; | |
41 | struct srd_pd_output *pdo; | |
0b922460 BV |
42 | struct srd_proto_data_annotation *pda; |
43 | int ann_format; | |
44 | char **ann_text; | |
d0a0ed03 | 45 | |
361fdcaa | 46 | /* Should be a list of [annotation format, [string, ...]]. */ |
d0a0ed03 | 47 | if (!PyList_Check(obj) && !PyTuple_Check(obj)) { |
d906d3f9 | 48 | srd_err("Protocol decoder %s submitted %s instead of list.", |
c9bfccc6 | 49 | di->decoder->name, obj->ob_type->tp_name); |
d0a0ed03 BV |
50 | return SRD_ERR_PYTHON; |
51 | } | |
52 | ||
361fdcaa | 53 | /* Should have 2 elements. */ |
d0a0ed03 | 54 | if (PyList_Size(obj) != 2) { |
c9bfccc6 UH |
55 | srd_err("Protocol decoder %s submitted annotation list with " |
56 | "%d elements instead of 2", di->decoder->name, | |
57 | PyList_Size(obj)); | |
d0a0ed03 BV |
58 | return SRD_ERR_PYTHON; |
59 | } | |
60 | ||
c9bfccc6 UH |
61 | /* |
62 | * The first element should be an integer matching a previously | |
63 | * registered annotation format. | |
64 | */ | |
d0a0ed03 BV |
65 | py_tmp = PyList_GetItem(obj, 0); |
66 | if (!PyLong_Check(py_tmp)) { | |
c9bfccc6 UH |
67 | srd_err("Protocol decoder %s submitted annotation list, but " |
68 | "first element was not an integer.", di->decoder->name); | |
d0a0ed03 BV |
69 | return SRD_ERR_PYTHON; |
70 | } | |
0b922460 BV |
71 | ann_format = PyLong_AsLong(py_tmp); |
72 | if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_format))) { | |
d906d3f9 | 73 | srd_err("Protocol decoder %s submitted data to unregistered " |
0b922460 | 74 | "annotation format %d.", di->decoder->name, ann_format); |
d0a0ed03 BV |
75 | return SRD_ERR_PYTHON; |
76 | } | |
d0a0ed03 | 77 | |
361fdcaa | 78 | /* Second element must be a list. */ |
d0a0ed03 BV |
79 | py_tmp = PyList_GetItem(obj, 1); |
80 | if (!PyList_Check(py_tmp)) { | |
d906d3f9 | 81 | srd_err("Protocol decoder %s submitted annotation list, but " |
c9bfccc6 | 82 | "second element was not a list.", di->decoder->name); |
d0a0ed03 BV |
83 | return SRD_ERR_PYTHON; |
84 | } | |
62a2b15c | 85 | if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) { |
d906d3f9 | 86 | srd_err("Protocol decoder %s submitted annotation list, but " |
c9bfccc6 | 87 | "second element was malformed.", di->decoder->name); |
d0a0ed03 BV |
88 | return SRD_ERR_PYTHON; |
89 | } | |
90 | ||
0b922460 BV |
91 | if (!(pda = g_try_malloc(sizeof(struct srd_proto_data_annotation)))) |
92 | return SRD_ERR_MALLOC; | |
93 | pda->ann_format = ann_format; | |
94 | pda->ann_text = ann_text; | |
95 | pdata->data = pda; | |
96 | ||
d0a0ed03 BV |
97 | return SRD_OK; |
98 | } | |
99 | ||
d75d8a7c BV |
100 | static int convert_binary(struct srd_decoder_inst *di, PyObject *obj, |
101 | struct srd_proto_data *pdata) | |
102 | { | |
103 | struct srd_proto_data_binary *pdb; | |
104 | PyObject *py_tmp; | |
105 | Py_ssize_t size; | |
106 | int bin_class; | |
107 | char *class_name, *buf; | |
108 | ||
109 | /* Should be a tuple of (binary class, bytes). */ | |
110 | if (!PyTuple_Check(obj)) { | |
111 | srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with " | |
112 | "%s instead of tuple.", di->decoder->name, | |
113 | obj->ob_type->tp_name); | |
114 | return SRD_ERR_PYTHON; | |
115 | } | |
116 | ||
117 | /* Should have 2 elements. */ | |
118 | if (PyTuple_Size(obj) != 2) { | |
119 | srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY tuple " | |
120 | "with %d elements instead of 2", di->decoder->name, | |
121 | PyList_Size(obj)); | |
122 | return SRD_ERR_PYTHON; | |
123 | } | |
124 | ||
125 | /* The first element should be an integer. */ | |
126 | py_tmp = PyTuple_GetItem(obj, 0); | |
127 | if (!PyLong_Check(py_tmp)) { | |
128 | srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY tuple, " | |
129 | "but first element was not an integer.", di->decoder->name); | |
130 | return SRD_ERR_PYTHON; | |
131 | } | |
132 | bin_class = PyLong_AsLong(py_tmp); | |
133 | if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) { | |
134 | srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with " | |
135 | "unregistered binary class %d.", di->decoder->name, bin_class); | |
136 | return SRD_ERR_PYTHON; | |
137 | } | |
138 | ||
139 | /* Second element should be bytes. */ | |
140 | py_tmp = PyTuple_GetItem(obj, 1); | |
141 | if (!PyBytes_Check(py_tmp)) { | |
142 | srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY tuple, " | |
143 | "but second element was not bytes.", di->decoder->name); | |
144 | return SRD_ERR_PYTHON; | |
145 | } | |
146 | ||
147 | /* Consider an empty set of bytes a bug. */ | |
148 | if (PyBytes_Size(py_tmp) == 0) { | |
149 | srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY " | |
150 | "with empty data set.", di->decoder->name); | |
151 | return SRD_ERR_PYTHON; | |
152 | } | |
153 | ||
154 | if (!(pdb = g_try_malloc(sizeof(struct srd_proto_data_binary)))) | |
155 | return SRD_ERR_MALLOC; | |
156 | if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1) | |
157 | return SRD_ERR_PYTHON; | |
158 | pdb->bin_class = bin_class; | |
159 | pdb->size = size; | |
160 | if (!(pdb->data = g_try_malloc(pdb->size))) | |
161 | return SRD_ERR_MALLOC; | |
162 | memcpy((void *)pdb->data, (const void *)buf, pdb->size); | |
163 | pdata->data = pdb; | |
164 | ||
165 | return SRD_OK; | |
166 | } | |
167 | ||
7ee0c40b BV |
168 | static int convert_meta(struct srd_proto_data *pdata, PyObject *obj) |
169 | { | |
170 | long long intvalue; | |
171 | double dvalue; | |
172 | ||
173 | if (pdata->pdo->meta_type == G_VARIANT_TYPE_INT64) { | |
174 | if (!PyLong_Check(obj)) { | |
175 | PyErr_Format(PyExc_TypeError, "This output was registered " | |
176 | "as 'int', but '%s' was passed.", obj->ob_type->tp_name); | |
177 | return SRD_ERR_PYTHON; | |
178 | } | |
179 | intvalue = PyLong_AsLongLong(obj); | |
180 | if (PyErr_Occurred()) | |
181 | return SRD_ERR_PYTHON; | |
182 | pdata->data = g_variant_new_int64(intvalue); | |
183 | } else if (pdata->pdo->meta_type == G_VARIANT_TYPE_DOUBLE) { | |
184 | if (!PyFloat_Check(obj)) { | |
185 | PyErr_Format(PyExc_TypeError, "This output was registered " | |
186 | "as 'float', but '%s' was passed.", obj->ob_type->tp_name); | |
187 | return SRD_ERR_PYTHON; | |
188 | } | |
189 | dvalue = PyFloat_AsDouble(obj); | |
190 | if (PyErr_Occurred()) | |
191 | return SRD_ERR_PYTHON; | |
192 | pdata->data = g_variant_new_double(dvalue); | |
193 | } | |
194 | ||
195 | return SRD_OK; | |
196 | } | |
197 | ||
d0a0ed03 BV |
198 | static PyObject *Decoder_put(PyObject *self, PyObject *args) |
199 | { | |
200 | GSList *l; | |
4f75f1c1 | 201 | PyObject *py_data, *py_res; |
a8b72b05 | 202 | struct srd_decoder_inst *di, *next_di; |
d0a0ed03 BV |
203 | struct srd_pd_output *pdo; |
204 | struct srd_proto_data *pdata; | |
205 | uint64_t start_sample, end_sample; | |
206 | int output_id; | |
2994587f | 207 | struct srd_pd_callback *cb; |
d0a0ed03 | 208 | |
a8b72b05 | 209 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
58572aed | 210 | /* Shouldn't happen. */ |
7a1712c4 | 211 | srd_dbg("put(): self instance not found."); |
d0a0ed03 | 212 | return NULL; |
58572aed | 213 | } |
d0a0ed03 | 214 | |
c9bfccc6 | 215 | if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, |
3d14e7c9 | 216 | &output_id, &py_data)) { |
c9bfccc6 UH |
217 | /* |
218 | * This throws an exception, but by returning NULL here we let | |
219 | * Python raise it. This results in a much better trace in | |
220 | * controller.c on the decode() method call. | |
221 | */ | |
d0a0ed03 | 222 | return NULL; |
c9bfccc6 | 223 | } |
d0a0ed03 BV |
224 | |
225 | if (!(l = g_slist_nth(di->pd_output, output_id))) { | |
d906d3f9 | 226 | srd_err("Protocol decoder %s submitted invalid output ID %d.", |
c9bfccc6 | 227 | di->decoder->name, output_id); |
d0a0ed03 BV |
228 | return NULL; |
229 | } | |
230 | pdo = l->data; | |
231 | ||
1c2b0d0b | 232 | srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.", |
a8b72b05 | 233 | di->inst_id, start_sample, end_sample, |
1c2b0d0b | 234 | OUTPUT_TYPES[pdo->output_type], output_id); |
58572aed | 235 | |
a61ece20 UH |
236 | if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) { |
237 | srd_err("Failed to g_malloc() struct srd_proto_data."); | |
d0a0ed03 | 238 | return NULL; |
a61ece20 | 239 | } |
d0a0ed03 BV |
240 | pdata->start_sample = start_sample; |
241 | pdata->end_sample = end_sample; | |
242 | pdata->pdo = pdo; | |
243 | ||
244 | switch (pdo->output_type) { | |
245 | case SRD_OUTPUT_ANN: | |
246 | /* Annotations are only fed to callbacks. */ | |
32cfb920 | 247 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
d75d8a7c | 248 | /* Convert from PyDict to srd_proto_data_annotation. */ |
0b922460 | 249 | if (convert_annotation(di, py_data, pdata) != SRD_OK) { |
d0a0ed03 BV |
250 | /* An error was already logged. */ |
251 | break; | |
252 | } | |
2994587f | 253 | cb->cb(pdata, cb->cb_data); |
d0a0ed03 BV |
254 | } |
255 | break; | |
f2a5df42 | 256 | case SRD_OUTPUT_PYTHON: |
d0a0ed03 BV |
257 | for (l = di->next_di; l; l = l->next) { |
258 | next_di = l->data; | |
7a1712c4 | 259 | srd_spew("Sending %d-%d to instance %s", |
3d14e7c9 | 260 | start_sample, end_sample, next_di->inst_id); |
c9bfccc6 | 261 | if (!(py_res = PyObject_CallMethod( |
3d14e7c9 BV |
262 | next_di->py_inst, "decode", "KKO", start_sample, |
263 | end_sample, py_data))) { | |
aafeeaea | 264 | srd_exception_catch("Calling %s decode(): ", |
3d14e7c9 | 265 | next_di->inst_id); |
d0a0ed03 BV |
266 | } |
267 | Py_XDECREF(py_res); | |
268 | } | |
3d14e7c9 BV |
269 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
270 | /* Frontends aren't really supposed to get Python | |
271 | * callbacks, but it's useful for testing. */ | |
272 | pdata->data = py_data; | |
273 | cb->cb(pdata, cb->cb_data); | |
274 | } | |
d0a0ed03 BV |
275 | break; |
276 | case SRD_OUTPUT_BINARY: | |
d75d8a7c BV |
277 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { |
278 | /* Convert from PyDict to srd_proto_data_binary. */ | |
279 | if (convert_binary(di, py_data, pdata) != SRD_OK) { | |
280 | /* An error was already logged. */ | |
281 | break; | |
282 | } | |
283 | cb->cb(pdata, cb->cb_data); | |
284 | } | |
d0a0ed03 | 285 | break; |
7ee0c40b BV |
286 | case SRD_OUTPUT_META: |
287 | if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) { | |
288 | /* Annotations need converting from PyObject. */ | |
289 | if (convert_meta(pdata, py_data) != SRD_OK) { | |
290 | /* An exception was already set up. */ | |
291 | break; | |
292 | } | |
293 | cb->cb(pdata, cb->cb_data); | |
294 | } | |
295 | break; | |
d0a0ed03 | 296 | default: |
d906d3f9 | 297 | srd_err("Protocol decoder %s submitted invalid output type %d.", |
c9bfccc6 | 298 | di->decoder->name, pdo->output_type); |
d0a0ed03 BV |
299 | break; |
300 | } | |
301 | ||
302 | g_free(pdata); | |
303 | ||
304 | Py_RETURN_NONE; | |
305 | } | |
306 | ||
7ee0c40b BV |
307 | static PyObject *Decoder_register(PyObject *self, PyObject *args, |
308 | PyObject *kwargs) | |
d0a0ed03 | 309 | { |
a8b72b05 | 310 | struct srd_decoder_inst *di; |
7ee0c40b BV |
311 | struct srd_pd_output *pdo; |
312 | PyObject *py_new_output_id; | |
313 | PyTypeObject *meta_type_py; | |
314 | const GVariantType *meta_type_gv; | |
315 | int output_type; | |
316 | char *proto_id, *meta_name, *meta_descr; | |
317 | char *keywords[] = {"output_type", "proto_id", "meta", NULL}; | |
318 | ||
319 | meta_type_py = NULL; | |
320 | meta_type_gv = NULL; | |
321 | meta_name = meta_descr = NULL; | |
d0a0ed03 | 322 | |
a8b72b05 | 323 | if (!(di = srd_inst_find_by_obj(NULL, self))) { |
d0a0ed03 BV |
324 | PyErr_SetString(PyExc_Exception, "decoder instance not found"); |
325 | return NULL; | |
326 | } | |
327 | ||
7ee0c40b BV |
328 | /* Default to instance id, which defaults to class id. */ |
329 | proto_id = di->inst_id; | |
330 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords, | |
331 | &output_type, &proto_id, | |
332 | &meta_type_py, &meta_name, &meta_descr)) { | |
511e2123 | 333 | /* Let Python raise this exception. */ |
d0a0ed03 BV |
334 | return NULL; |
335 | } | |
336 | ||
7ee0c40b BV |
337 | /* Check if the meta value's type is supported. */ |
338 | if (output_type == SRD_OUTPUT_META) { | |
339 | if (meta_type_py == &PyLong_Type) | |
340 | meta_type_gv = G_VARIANT_TYPE_INT64; | |
341 | else if (meta_type_py == &PyFloat_Type) | |
342 | meta_type_gv = G_VARIANT_TYPE_DOUBLE; | |
343 | else { | |
344 | PyErr_Format(PyExc_TypeError, "Unsupported type '%s'.", | |
345 | meta_type_py->tp_name); | |
346 | return NULL; | |
347 | } | |
348 | } | |
349 | ||
350 | srd_dbg("Instance %s creating new output type %d for %s.", | |
351 | di->inst_id, output_type, proto_id); | |
352 | ||
353 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) { | |
354 | PyErr_SetString(PyExc_MemoryError, "struct srd_pd_output"); | |
355 | return NULL; | |
356 | } | |
357 | ||
358 | /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ | |
359 | pdo->pdo_id = g_slist_length(di->pd_output); | |
360 | pdo->output_type = output_type; | |
361 | pdo->di = di; | |
362 | pdo->proto_id = g_strdup(proto_id); | |
363 | ||
364 | if (output_type == SRD_OUTPUT_META) { | |
365 | pdo->meta_type = meta_type_gv; | |
366 | pdo->meta_name = g_strdup(meta_name); | |
367 | pdo->meta_descr = g_strdup(meta_descr); | |
368 | } | |
369 | ||
370 | di->pd_output = g_slist_append(di->pd_output, pdo); | |
371 | py_new_output_id = Py_BuildValue("i", pdo->pdo_id); | |
372 | ||
373 | return py_new_output_id; | |
374 | } | |
375 | ||
d0a0ed03 BV |
376 | static PyMethodDef Decoder_methods[] = { |
377 | {"put", Decoder_put, METH_VARARGS, | |
86528298 | 378 | "Accepts a dictionary with the following keys: startsample, endsample, data"}, |
7ee0c40b BV |
379 | {"register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS, |
380 | "Register a new output stream"}, | |
d0a0ed03 BV |
381 | {NULL, NULL, 0, NULL} |
382 | }; | |
383 | ||
57790bc8 | 384 | /** @cond PRIVATE */ |
55c3c5f4 | 385 | SRD_PRIV PyTypeObject srd_Decoder_type = { |
d0a0ed03 BV |
386 | PyVarObject_HEAD_INIT(NULL, 0) |
387 | .tp_name = "sigrokdecode.Decoder", | |
388 | .tp_basicsize = sizeof(srd_Decoder), | |
389 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | |
361fdcaa | 390 | .tp_doc = "sigrok Decoder base class", |
d0a0ed03 BV |
391 | .tp_methods = Decoder_methods, |
392 | }; | |
57790bc8 | 393 | /** @endcond */ |