]> sigrok.org Git - libsigrokdecode.git/blame_incremental - type_decoder.c
usb_packet: handle errors from usb_signalling
[libsigrokdecode.git] / type_decoder.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrokdecode project.
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
20#include <config.h>
21#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "libsigrokdecode.h"
23#include <inttypes.h>
24
25typedef struct {
26 PyObject_HEAD
27} srd_Decoder;
28
29/* This is only used for nicer srd_dbg() output.
30 */
31static 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}
42
43static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
44 struct srd_proto_data *pdata)
45{
46 PyObject *py_tmp;
47 struct srd_pd_output *pdo;
48 struct srd_proto_data_annotation *pda;
49 int ann_class;
50 char **ann_text;
51
52 /* Should be a list of [annotation class, [string, ...]]. */
53 if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
54 srd_err("Protocol decoder %s submitted an annotation that"
55 " is not a list or tuple", di->decoder->name);
56 return SRD_ERR_PYTHON;
57 }
58
59 /* Should have 2 elements. */
60 if (PyList_Size(obj) != 2) {
61 srd_err("Protocol decoder %s submitted annotation list with "
62 "%zd elements instead of 2", di->decoder->name,
63 PyList_Size(obj));
64 return SRD_ERR_PYTHON;
65 }
66
67 /*
68 * The first element should be an integer matching a previously
69 * registered annotation class.
70 */
71 py_tmp = PyList_GetItem(obj, 0);
72 if (!PyLong_Check(py_tmp)) {
73 srd_err("Protocol decoder %s submitted annotation list, but "
74 "first element was not an integer.", di->decoder->name);
75 return SRD_ERR_PYTHON;
76 }
77 ann_class = PyLong_AsLong(py_tmp);
78 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_class))) {
79 srd_err("Protocol decoder %s submitted data to unregistered "
80 "annotation class %d.", di->decoder->name, ann_class);
81 return SRD_ERR_PYTHON;
82 }
83
84 /* Second element must be a list. */
85 py_tmp = PyList_GetItem(obj, 1);
86 if (!PyList_Check(py_tmp)) {
87 srd_err("Protocol decoder %s submitted annotation list, but "
88 "second element was not a list.", di->decoder->name);
89 return SRD_ERR_PYTHON;
90 }
91 if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) {
92 srd_err("Protocol decoder %s submitted annotation list, but "
93 "second element was malformed.", di->decoder->name);
94 return SRD_ERR_PYTHON;
95 }
96
97 pda = g_malloc(sizeof(struct srd_proto_data_annotation));
98 pda->ann_class = ann_class;
99 pda->ann_text = ann_text;
100 pdata->data = pda;
101
102 return SRD_OK;
103}
104
105static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
106 struct srd_proto_data *pdata)
107{
108 struct srd_proto_data_binary *pdb;
109 PyObject *py_tmp;
110 Py_ssize_t size;
111 int bin_class;
112 char *class_name, *buf;
113
114 /* Should be a tuple of (binary class, bytes). */
115 if (!PyTuple_Check(obj)) {
116 srd_err("Protocol decoder %s submitted non-tuple for SRD_OUTPUT_BINARY.",
117 di->decoder->name);
118 return SRD_ERR_PYTHON;
119 }
120
121 /* Should have 2 elements. */
122 if (PyTuple_Size(obj) != 2) {
123 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY tuple "
124 "with %zd elements instead of 2", di->decoder->name,
125 PyList_Size(obj));
126 return SRD_ERR_PYTHON;
127 }
128
129 /* The first element should be an integer. */
130 py_tmp = PyTuple_GetItem(obj, 0);
131 if (!PyLong_Check(py_tmp)) {
132 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY tuple, "
133 "but first element was not an integer.", di->decoder->name);
134 return SRD_ERR_PYTHON;
135 }
136 bin_class = PyLong_AsLong(py_tmp);
137 if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) {
138 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with "
139 "unregistered binary class %d.", di->decoder->name, bin_class);
140 return SRD_ERR_PYTHON;
141 }
142
143 /* Second element should be bytes. */
144 py_tmp = PyTuple_GetItem(obj, 1);
145 if (!PyBytes_Check(py_tmp)) {
146 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY tuple, "
147 "but second element was not bytes.", di->decoder->name);
148 return SRD_ERR_PYTHON;
149 }
150
151 /* Consider an empty set of bytes a bug. */
152 if (PyBytes_Size(py_tmp) == 0) {
153 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY "
154 "with empty data set.", di->decoder->name);
155 return SRD_ERR_PYTHON;
156 }
157
158 pdb = g_malloc(sizeof(struct srd_proto_data_binary));
159 if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
160 return SRD_ERR_PYTHON;
161 pdb->bin_class = bin_class;
162 pdb->size = size;
163 if (!(pdb->data = g_try_malloc(pdb->size)))
164 return SRD_ERR_MALLOC;
165 memcpy((void *)pdb->data, (const void *)buf, pdb->size);
166 pdata->data = pdb;
167
168 return SRD_OK;
169}
170
171static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
172{
173 long long intvalue;
174 double dvalue;
175
176 if (pdata->pdo->meta_type == G_VARIANT_TYPE_INT64) {
177 if (!PyLong_Check(obj)) {
178 PyErr_Format(PyExc_TypeError, "This output was registered "
179 "as 'int', but something else was passed.");
180 return SRD_ERR_PYTHON;
181 }
182 intvalue = PyLong_AsLongLong(obj);
183 if (PyErr_Occurred())
184 return SRD_ERR_PYTHON;
185 pdata->data = g_variant_new_int64(intvalue);
186 } else if (pdata->pdo->meta_type == G_VARIANT_TYPE_DOUBLE) {
187 if (!PyFloat_Check(obj)) {
188 PyErr_Format(PyExc_TypeError, "This output was registered "
189 "as 'float', but something else was passed.");
190 return SRD_ERR_PYTHON;
191 }
192 dvalue = PyFloat_AsDouble(obj);
193 if (PyErr_Occurred())
194 return SRD_ERR_PYTHON;
195 pdata->data = g_variant_new_double(dvalue);
196 }
197
198 return SRD_OK;
199}
200
201static PyObject *Decoder_put(PyObject *self, PyObject *args)
202{
203 GSList *l;
204 PyObject *py_data, *py_res;
205 struct srd_decoder_inst *di, *next_di;
206 struct srd_pd_output *pdo;
207 struct srd_proto_data *pdata;
208 uint64_t start_sample, end_sample;
209 int output_id;
210 struct srd_pd_callback *cb;
211
212 if (!(di = srd_inst_find_by_obj(NULL, self))) {
213 /* Shouldn't happen. */
214 srd_dbg("put(): self instance not found.");
215 return NULL;
216 }
217
218 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
219 &output_id, &py_data)) {
220 /*
221 * This throws an exception, but by returning NULL here we let
222 * Python raise it. This results in a much better trace in
223 * controller.c on the decode() method call.
224 */
225 return NULL;
226 }
227
228 if (!(l = g_slist_nth(di->pd_output, output_id))) {
229 srd_err("Protocol decoder %s submitted invalid output ID %d.",
230 di->decoder->name, output_id);
231 return NULL;
232 }
233 pdo = l->data;
234
235 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
236 di->inst_id, start_sample, end_sample,
237 output_type_name(pdo->output_type), output_id);
238
239 pdata = g_malloc0(sizeof(struct srd_proto_data));
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. */
247 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
248 /* Convert from PyDict to srd_proto_data_annotation. */
249 if (convert_annotation(di, py_data, pdata) != SRD_OK) {
250 /* An error was already logged. */
251 break;
252 }
253 cb->cb(pdata, cb->cb_data);
254 }
255 break;
256 case SRD_OUTPUT_PYTHON:
257 for (l = di->next_di; l; l = l->next) {
258 next_di = l->data;
259 srd_spew("Sending %" PRIu64 "-%" PRIu64 " to instance %s",
260 start_sample, end_sample, next_di->inst_id);
261 if (!(py_res = PyObject_CallMethod(
262 next_di->py_inst, "decode", "KKO", start_sample,
263 end_sample, py_data))) {
264 srd_exception_catch("Calling %s decode() failed",
265 next_di->inst_id);
266 }
267 Py_XDECREF(py_res);
268 }
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 }
275 break;
276 case SRD_OUTPUT_BINARY:
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 }
285 break;
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;
296 default:
297 srd_err("Protocol decoder %s submitted invalid output type %d.",
298 di->decoder->name, pdo->output_type);
299 break;
300 }
301
302 g_free(pdata);
303
304 Py_RETURN_NONE;
305}
306
307static PyObject *Decoder_register(PyObject *self, PyObject *args,
308 PyObject *kwargs)
309{
310 struct srd_decoder_inst *di;
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;
322
323 if (!(di = srd_inst_find_by_obj(NULL, self))) {
324 PyErr_SetString(PyExc_Exception, "decoder instance not found");
325 return NULL;
326 }
327
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)) {
333 /* Let Python raise this exception. */
334 return NULL;
335 }
336
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.");
345 return NULL;
346 }
347 }
348
349 srd_dbg("Instance %s creating new output type %d for %s.",
350 di->inst_id, output_type, proto_id);
351
352 pdo = g_malloc(sizeof(struct srd_pd_output));
353
354 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
355 pdo->pdo_id = g_slist_length(di->pd_output);
356 pdo->output_type = output_type;
357 pdo->di = di;
358 pdo->proto_id = g_strdup(proto_id);
359
360 if (output_type == SRD_OUTPUT_META) {
361 pdo->meta_type = meta_type_gv;
362 pdo->meta_name = g_strdup(meta_name);
363 pdo->meta_descr = g_strdup(meta_descr);
364 }
365
366 di->pd_output = g_slist_append(di->pd_output, pdo);
367 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
368
369 return py_new_output_id;
370}
371
372static PyMethodDef Decoder_methods[] = {
373 {"put", Decoder_put, METH_VARARGS,
374 "Accepts a dictionary with the following keys: startsample, endsample, data"},
375 {"register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS,
376 "Register a new output stream"},
377 {NULL, NULL, 0, NULL}
378};
379
380/** Create the sigrokdecode.Decoder type.
381 * @return The new type object.
382 * @private
383 */
384SRD_PRIV PyObject *srd_Decoder_type_new(void)
385{
386 PyType_Spec spec;
387 PyType_Slot slots[] = {
388 { Py_tp_doc, "sigrok Decoder base class" },
389 { Py_tp_methods, Decoder_methods },
390 { Py_tp_new, (void *)&PyType_GenericNew },
391 { 0, NULL }
392 };
393 spec.name = "sigrokdecode.Decoder";
394 spec.basicsize = sizeof(srd_Decoder);
395 spec.itemsize = 0;
396 spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
397 spec.slots = slots;
398
399 return PyType_FromSpec(&spec);
400}