2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21 #include "libsigrokdecode-internal.h"
25 /* This is only used for nicer srd_dbg() output. */
26 static const char *OUTPUT_TYPES[] = {
33 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
34 struct srd_proto_data *pdata)
37 struct srd_pd_output *pdo;
38 struct srd_proto_data_annotation *pda;
42 /* Should be a list of [annotation format, [string, ...]]. */
43 if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
44 srd_err("Protocol decoder %s submitted %s instead of list.",
45 di->decoder->name, obj->ob_type->tp_name);
46 return SRD_ERR_PYTHON;
49 /* Should have 2 elements. */
50 if (PyList_Size(obj) != 2) {
51 srd_err("Protocol decoder %s submitted annotation list with "
52 "%d elements instead of 2", di->decoder->name,
54 return SRD_ERR_PYTHON;
58 * The first element should be an integer matching a previously
59 * registered annotation format.
61 py_tmp = PyList_GetItem(obj, 0);
62 if (!PyLong_Check(py_tmp)) {
63 srd_err("Protocol decoder %s submitted annotation list, but "
64 "first element was not an integer.", di->decoder->name);
65 return SRD_ERR_PYTHON;
67 ann_format = PyLong_AsLong(py_tmp);
68 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_format))) {
69 srd_err("Protocol decoder %s submitted data to unregistered "
70 "annotation format %d.", di->decoder->name, ann_format);
71 return SRD_ERR_PYTHON;
74 /* Second element must be a list. */
75 py_tmp = PyList_GetItem(obj, 1);
76 if (!PyList_Check(py_tmp)) {
77 srd_err("Protocol decoder %s submitted annotation list, but "
78 "second element was not a list.", di->decoder->name);
79 return SRD_ERR_PYTHON;
81 if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) {
82 srd_err("Protocol decoder %s submitted annotation list, but "
83 "second element was malformed.", di->decoder->name);
84 return SRD_ERR_PYTHON;
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;
96 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
97 struct srd_proto_data *pdata)
99 struct srd_proto_data_binary *pdb;
103 char *class_name, *buf;
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;
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,
118 return SRD_ERR_PYTHON;
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;
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;
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;
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;
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;
156 if (!(pdb->data = g_try_malloc(pdb->size)))
157 return SRD_ERR_MALLOC;
158 memcpy((void *)pdb->data, (const void *)buf, pdb->size);
164 static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
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;
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;
185 dvalue = PyFloat_AsDouble(obj);
186 if (PyErr_Occurred())
187 return SRD_ERR_PYTHON;
188 pdata->data = g_variant_new_double(dvalue);
194 static PyObject *Decoder_put(PyObject *self, PyObject *args)
197 PyObject *py_data, *py_res;
198 struct srd_decoder_inst *di, *next_di;
199 struct srd_pd_output *pdo;
200 struct srd_proto_data *pdata;
201 uint64_t start_sample, end_sample;
203 struct srd_pd_callback *cb;
205 if (!(di = srd_inst_find_by_obj(NULL, self))) {
206 /* Shouldn't happen. */
207 srd_dbg("put(): self instance not found.");
211 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
212 &output_id, &py_data)) {
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.
221 if (!(l = g_slist_nth(di->pd_output, output_id))) {
222 srd_err("Protocol decoder %s submitted invalid output ID %d.",
223 di->decoder->name, output_id);
228 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
229 di->inst_id, start_sample, end_sample,
230 OUTPUT_TYPES[pdo->output_type], output_id);
232 if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) {
233 srd_err("Failed to g_malloc() struct srd_proto_data.");
236 pdata->start_sample = start_sample;
237 pdata->end_sample = end_sample;
240 switch (pdo->output_type) {
242 /* Annotations are only fed to callbacks. */
243 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
244 /* Convert from PyDict to srd_proto_data_annotation. */
245 if (convert_annotation(di, py_data, pdata) != SRD_OK) {
246 /* An error was already logged. */
249 cb->cb(pdata, cb->cb_data);
252 case SRD_OUTPUT_PYTHON:
253 for (l = di->next_di; l; l = l->next) {
255 srd_spew("Sending %d-%d to instance %s",
256 start_sample, end_sample, next_di->inst_id);
257 if (!(py_res = PyObject_CallMethod(
258 next_di->py_inst, "decode", "KKO", start_sample,
259 end_sample, py_data))) {
260 srd_exception_catch("Calling %s decode(): ",
265 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
266 /* Frontends aren't really supposed to get Python
267 * callbacks, but it's useful for testing. */
268 pdata->data = py_data;
269 cb->cb(pdata, cb->cb_data);
272 case SRD_OUTPUT_BINARY:
273 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
274 /* Convert from PyDict to srd_proto_data_binary. */
275 if (convert_binary(di, py_data, pdata) != SRD_OK) {
276 /* An error was already logged. */
279 cb->cb(pdata, cb->cb_data);
282 case SRD_OUTPUT_META:
283 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
284 /* Annotations need converting from PyObject. */
285 if (convert_meta(pdata, py_data) != SRD_OK) {
286 /* An exception was already set up. */
289 cb->cb(pdata, cb->cb_data);
293 srd_err("Protocol decoder %s submitted invalid output type %d.",
294 di->decoder->name, pdo->output_type);
303 static PyObject *Decoder_register(PyObject *self, PyObject *args,
306 struct srd_decoder_inst *di;
307 struct srd_pd_output *pdo;
308 PyObject *py_new_output_id;
309 PyTypeObject *meta_type_py;
310 const GVariantType *meta_type_gv;
312 char *proto_id, *meta_name, *meta_descr;
313 char *keywords[] = {"output_type", "proto_id", "meta", NULL};
317 meta_name = meta_descr = NULL;
319 if (!(di = srd_inst_find_by_obj(NULL, self))) {
320 PyErr_SetString(PyExc_Exception, "decoder instance not found");
324 /* Default to instance id, which defaults to class id. */
325 proto_id = di->inst_id;
326 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords,
327 &output_type, &proto_id,
328 &meta_type_py, &meta_name, &meta_descr)) {
329 /* Let Python raise this exception. */
333 /* Check if the meta value's type is supported. */
334 if (output_type == SRD_OUTPUT_META) {
335 if (meta_type_py == &PyLong_Type)
336 meta_type_gv = G_VARIANT_TYPE_INT64;
337 else if (meta_type_py == &PyFloat_Type)
338 meta_type_gv = G_VARIANT_TYPE_DOUBLE;
340 PyErr_Format(PyExc_TypeError, "Unsupported type '%s'.",
341 meta_type_py->tp_name);
346 srd_dbg("Instance %s creating new output type %d for %s.",
347 di->inst_id, output_type, proto_id);
349 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) {
350 PyErr_SetString(PyExc_MemoryError, "struct srd_pd_output");
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;
358 pdo->proto_id = g_strdup(proto_id);
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);
366 di->pd_output = g_slist_append(di->pd_output, pdo);
367 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
369 return py_new_output_id;
372 static 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}
381 SRD_PRIV PyTypeObject srd_Decoder_type = {
382 PyVarObject_HEAD_INIT(NULL, 0)
383 .tp_name = "sigrokdecode.Decoder",
384 .tp_basicsize = sizeof(srd_Decoder),
385 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
386 .tp_doc = "sigrok Decoder base class",
387 .tp_methods = Decoder_methods,