]> sigrok.org Git - libsigrokdecode.git/blame_incremental - type_decoder.c
Prune dead code
[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 "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21#include "libsigrokdecode-internal.h"
22#include "config.h"
23#include <inttypes.h>
24
25/* This is only used for nicer srd_dbg() output. */
26static const char *OUTPUT_TYPES[] = {
27 "OUTPUT_ANN",
28 "OUTPUT_PYTHON",
29 "OUTPUT_BINARY",
30 "OUTPUT_META",
31};
32
33static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
34 struct srd_proto_data *pdata)
35{
36 PyObject *py_tmp;
37 struct srd_pd_output *pdo;
38 struct srd_proto_data_annotation *pda;
39 int ann_format;
40 char **ann_text;
41
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;
47 }
48
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,
53 PyList_Size(obj));
54 return SRD_ERR_PYTHON;
55 }
56
57 /*
58 * The first element should be an integer matching a previously
59 * registered annotation format.
60 */
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;
66 }
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;
72 }
73
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;
80 }
81 if (py_strlist_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;
85 }
86
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
93 return SRD_OK;
94}
95
96static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
97{
98 long long intvalue;
99 double dvalue;
100
101 if (pdata->pdo->meta_type == G_VARIANT_TYPE_INT64) {
102 if (!PyLong_Check(obj)) {
103 PyErr_Format(PyExc_TypeError, "This output was registered "
104 "as 'int', but '%s' was passed.", obj->ob_type->tp_name);
105 return SRD_ERR_PYTHON;
106 }
107 intvalue = PyLong_AsLongLong(obj);
108 if (PyErr_Occurred())
109 return SRD_ERR_PYTHON;
110 pdata->data = g_variant_new_int64(intvalue);
111 } else if (pdata->pdo->meta_type == G_VARIANT_TYPE_DOUBLE) {
112 if (!PyFloat_Check(obj)) {
113 PyErr_Format(PyExc_TypeError, "This output was registered "
114 "as 'float', but '%s' was passed.", obj->ob_type->tp_name);
115 return SRD_ERR_PYTHON;
116 }
117 dvalue = PyFloat_AsDouble(obj);
118 if (PyErr_Occurred())
119 return SRD_ERR_PYTHON;
120 pdata->data = g_variant_new_double(dvalue);
121 }
122
123 return SRD_OK;
124}
125
126static PyObject *Decoder_put(PyObject *self, PyObject *args)
127{
128 GSList *l;
129 PyObject *py_data, *py_res;
130 struct srd_decoder_inst *di, *next_di;
131 struct srd_pd_output *pdo;
132 struct srd_proto_data *pdata;
133 uint64_t start_sample, end_sample;
134 int output_id;
135 struct srd_pd_callback *cb;
136
137 if (!(di = srd_inst_find_by_obj(NULL, self))) {
138 /* Shouldn't happen. */
139 srd_dbg("put(): self instance not found.");
140 return NULL;
141 }
142
143 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
144 &output_id, &py_data)) {
145 /*
146 * This throws an exception, but by returning NULL here we let
147 * Python raise it. This results in a much better trace in
148 * controller.c on the decode() method call.
149 */
150 return NULL;
151 }
152
153 if (!(l = g_slist_nth(di->pd_output, output_id))) {
154 srd_err("Protocol decoder %s submitted invalid output ID %d.",
155 di->decoder->name, output_id);
156 return NULL;
157 }
158 pdo = l->data;
159
160 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
161 di->inst_id, start_sample, end_sample,
162 OUTPUT_TYPES[pdo->output_type], output_id);
163
164 if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) {
165 srd_err("Failed to g_malloc() struct srd_proto_data.");
166 return NULL;
167 }
168 pdata->start_sample = start_sample;
169 pdata->end_sample = end_sample;
170 pdata->pdo = pdo;
171
172 switch (pdo->output_type) {
173 case SRD_OUTPUT_ANN:
174 /* Annotations are only fed to callbacks. */
175 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
176 /* Annotations need converting from PyObject. */
177 if (convert_annotation(di, py_data, pdata) != SRD_OK) {
178 /* An error was already logged. */
179 break;
180 }
181 cb->cb(pdata, cb->cb_data);
182 }
183 break;
184 case SRD_OUTPUT_PYTHON:
185 for (l = di->next_di; l; l = l->next) {
186 next_di = l->data;
187 /* TODO: Is this needed? */
188 Py_XINCREF(next_di->py_inst);
189 srd_spew("Sending %d-%d to instance %s",
190 start_sample, end_sample,
191 next_di->inst_id);
192 if (!(py_res = PyObject_CallMethod(
193 next_di->py_inst, "decode", "KKO", start_sample,
194 end_sample, py_data))) {
195 srd_exception_catch("Calling %s decode(): ",
196 next_di->inst_id);
197 }
198 Py_XDECREF(py_res);
199 }
200 break;
201 case SRD_OUTPUT_BINARY:
202 srd_err("SRD_OUTPUT_BINARY not yet supported.");
203 break;
204 case SRD_OUTPUT_META:
205 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
206 /* Annotations need converting from PyObject. */
207 if (convert_meta(pdata, py_data) != SRD_OK) {
208 /* An exception was already set up. */
209 break;
210 }
211 cb->cb(pdata, cb->cb_data);
212 }
213 break;
214 default:
215 srd_err("Protocol decoder %s submitted invalid output type %d.",
216 di->decoder->name, pdo->output_type);
217 break;
218 }
219
220 g_free(pdata);
221
222 Py_RETURN_NONE;
223}
224
225static PyObject *Decoder_register(PyObject *self, PyObject *args,
226 PyObject *kwargs)
227{
228 struct srd_decoder_inst *di;
229 struct srd_pd_output *pdo;
230 PyObject *py_new_output_id;
231 PyTypeObject *meta_type_py;
232 const GVariantType *meta_type_gv;
233 int output_type;
234 char *proto_id, *meta_name, *meta_descr;
235 char *keywords[] = {"output_type", "proto_id", "meta", NULL};
236
237 meta_type_py = NULL;
238 meta_type_gv = NULL;
239 meta_name = meta_descr = NULL;
240
241 if (!(di = srd_inst_find_by_obj(NULL, self))) {
242 PyErr_SetString(PyExc_Exception, "decoder instance not found");
243 return NULL;
244 }
245
246 /* Default to instance id, which defaults to class id. */
247 proto_id = di->inst_id;
248 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords,
249 &output_type, &proto_id,
250 &meta_type_py, &meta_name, &meta_descr)) {
251 /* Let Python raise this exception. */
252 return NULL;
253 }
254
255 /* Check if the meta value's type is supported. */
256 if (output_type == SRD_OUTPUT_META) {
257 if (meta_type_py == &PyLong_Type)
258 meta_type_gv = G_VARIANT_TYPE_INT64;
259 else if (meta_type_py == &PyFloat_Type)
260 meta_type_gv = G_VARIANT_TYPE_DOUBLE;
261 else {
262 PyErr_Format(PyExc_TypeError, "Unsupported type '%s'.",
263 meta_type_py->tp_name);
264 return NULL;
265 }
266 }
267
268 srd_dbg("Instance %s creating new output type %d for %s.",
269 di->inst_id, output_type, proto_id);
270
271 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) {
272 PyErr_SetString(PyExc_MemoryError, "struct srd_pd_output");
273 return NULL;
274 }
275
276 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
277 pdo->pdo_id = g_slist_length(di->pd_output);
278 pdo->output_type = output_type;
279 pdo->di = di;
280 pdo->proto_id = g_strdup(proto_id);
281
282 if (output_type == SRD_OUTPUT_META) {
283 pdo->meta_type = meta_type_gv;
284 pdo->meta_name = g_strdup(meta_name);
285 pdo->meta_descr = g_strdup(meta_descr);
286 }
287
288 di->pd_output = g_slist_append(di->pd_output, pdo);
289 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
290
291 return py_new_output_id;
292}
293
294/* TODO: this is just a stub that calls _register() until all PDs
295 * are changed to use the new register API. */
296static PyObject *Decoder_add(PyObject *self, PyObject *args)
297{
298 PyObject *py_keywords, *py_new_output_id;
299
300 py_keywords = PyDict_New();
301 py_new_output_id = Decoder_register(self, args, py_keywords);
302 Py_DecRef(py_keywords);
303
304 return py_new_output_id;
305}
306
307static PyMethodDef Decoder_methods[] = {
308 {"put", Decoder_put, METH_VARARGS,
309 "Accepts a dictionary with the following keys: startsample, endsample, data"},
310 {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
311 {"register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS,
312 "Register a new output stream"},
313 {NULL, NULL, 0, NULL}
314};
315
316/** @cond PRIVATE */
317SRD_PRIV PyTypeObject srd_Decoder_type = {
318 PyVarObject_HEAD_INIT(NULL, 0)
319 .tp_name = "sigrokdecode.Decoder",
320 .tp_basicsize = sizeof(srd_Decoder),
321 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
322 .tp_doc = "sigrok Decoder base class",
323 .tp_methods = Decoder_methods,
324};
325/** @endcond */