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