2 * This file is part of the sigrok 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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21 #include "sigrokdecode-internal.h"
25 static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj,
26 int *ann_format, char ***ann)
29 struct srd_pd_output *pdo;
32 /* Should be a list of [annotation format, [string, ...]] */
33 if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
34 srd_err("Protocol decoder %s submitted %s instead of list",
35 di->decoder->name, obj->ob_type->tp_name);
36 return SRD_ERR_PYTHON;
39 /* Should have 2 elements... */
40 if (PyList_Size(obj) != 2) {
41 srd_err("Protocol decoder %s submitted annotation list with %d elements instead of 2",
42 di->decoder->name, PyList_Size(obj));
43 return SRD_ERR_PYTHON;
46 /* First element should be an integer matching a previously
47 * registered annotation format. */
48 py_tmp = PyList_GetItem(obj, 0);
49 if (!PyLong_Check(py_tmp)) {
50 srd_err("Protocol decoder %s submitted annotation list, but first element was not an integer",
52 return SRD_ERR_PYTHON;
55 ann_id = PyLong_AsLong(py_tmp);
56 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_id))) {
57 srd_err("Protocol decoder %s submitted data to non-existent annotation format %d",
58 di->decoder->name, ann_id);
59 return SRD_ERR_PYTHON;
63 /* Second element must be a list */
64 py_tmp = PyList_GetItem(obj, 1);
65 if (!PyList_Check(py_tmp)) {
66 srd_err("Protocol decoder %s submitted annotation list, but second element was not a list",
68 return SRD_ERR_PYTHON;
70 if (py_strlist_to_char(py_tmp, ann) != SRD_OK) {
71 srd_err("Protocol decoder %s submitted annotation list, but second element was malformed",
73 return SRD_ERR_PYTHON;
79 static PyObject *Decoder_put(PyObject *self, PyObject *args)
82 PyObject *data, *py_res;
83 struct srd_decoder_instance *di, *next_di;
84 struct srd_pd_output *pdo;
85 struct srd_proto_data *pdata;
86 uint64_t start_sample, end_sample;
90 if (!(di = get_di_by_decobject(self)))
93 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data))
96 if (!(l = g_slist_nth(di->pd_output, output_id))) {
97 srd_err("Protocol decoder %s submitted invalid output ID %d",
98 di->decoder->name, output_id);
103 if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data))))
105 pdata->start_sample = start_sample;
106 pdata->end_sample = end_sample;
109 switch (pdo->output_type) {
111 /* Annotations are only fed to callbacks. */
112 if ((cb = srd_find_callback(pdo->output_type))) {
113 /* Annotations need converting from PyObject. */
114 if (convert_pyobj(di, data, &pdata->ann_format,
115 (char ***)&pdata->data) != SRD_OK) {
116 /* An error was already logged. */
122 case SRD_OUTPUT_PROTO:
123 for (l = di->next_di; l; l = l->next) {
125 /* TODO: is this needed? */
126 Py_XINCREF(next_di->py_instance);
127 if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
128 "KKO", start_sample, end_sample, data))) {
129 if (PyErr_Occurred())
135 case SRD_OUTPUT_BINARY:
136 srd_err("SRD_OUTPUT_BINARY not yet supported");
139 srd_err("Protocol decoder %s submitted invalid output type %d",
140 di->decoder->name, pdo->output_type);
150 static PyObject *Decoder_add(PyObject *self, PyObject *args)
153 struct srd_decoder_instance *di;
155 int output_type, pdo_id;
157 if (!(di = get_di_by_decobject(self))) {
158 srd_err("%s():%d decoder instance not found", __func__, __LINE__);
159 PyErr_SetString(PyExc_Exception, "decoder instance not found");
163 if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
164 if (PyErr_Occurred())
169 pdo_id = pd_add(di, output_type, proto_id);
173 ret = Py_BuildValue("i", pdo_id);
178 static PyMethodDef Decoder_methods[] = {
179 {"put", Decoder_put, METH_VARARGS,
180 "Accepts a dictionary with the following keys: time, duration, data"},
181 {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
182 {NULL, NULL, 0, NULL}
186 PyTypeObject srd_Decoder_type = {
187 PyVarObject_HEAD_INIT(NULL, 0)
188 .tp_name = "sigrokdecode.Decoder",
189 .tp_basicsize = sizeof(srd_Decoder),
190 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
191 .tp_doc = "Sigrok Decoder base class",
192 .tp_methods = Decoder_methods,