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"
24 /* lives in type_logic.c */
25 extern PyTypeObject srd_logic_type;
28 static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj,
29 int *ann_format, char ***ann)
32 struct srd_pd_output *pdo;
35 /* Should be a list of [annotation format, [string, ...]] */
36 if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
37 srd_err("Protocol decoder %s submitted %s instead of list",
38 di->decoder->name, obj->ob_type->tp_name);
39 return SRD_ERR_PYTHON;
42 /* Should have 2 elements... */
43 if (PyList_Size(obj) != 2) {
44 srd_err("Protocol decoder %s submitted annotation list with %d elements instead of 2",
45 di->decoder->name, PyList_Size(obj));
46 return SRD_ERR_PYTHON;
49 /* First element should be an integer matching a previously
50 * registered annotation format. */
51 py_tmp = PyList_GetItem(obj, 0);
52 if (!PyLong_Check(py_tmp)) {
53 srd_err("Protocol decoder %s submitted annotation list, but first element was not an integer",
55 return SRD_ERR_PYTHON;
58 ann_id = PyLong_AsLong(py_tmp);
59 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_id))) {
60 srd_err("Protocol decoder %s submitted data to non-existent annotation format %d",
61 di->decoder->name, ann_id);
62 return SRD_ERR_PYTHON;
66 /* Second element must be a list */
67 py_tmp = PyList_GetItem(obj, 1);
68 if (!PyList_Check(py_tmp)) {
69 srd_err("Protocol decoder %s submitted annotation list, but second element was not a list",
71 return SRD_ERR_PYTHON;
73 if (py_strlist_to_char(py_tmp, ann) != SRD_OK) {
74 srd_err("Protocol decoder %s submitted annotation list, but second element was malformed",
76 return SRD_ERR_PYTHON;
82 static PyObject *Decoder_put(PyObject *self, PyObject *args)
85 PyObject *data, *py_res;
86 struct srd_decoder_instance *di, *next_di;
87 struct srd_pd_output *pdo;
88 struct srd_proto_data *pdata;
89 uint64_t start_sample, end_sample;
93 if (!(di = get_di_by_decobject(self)))
96 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data))
99 if (!(l = g_slist_nth(di->pd_output, output_id))) {
100 srd_err("Protocol decoder %s submitted invalid output ID %d",
101 di->decoder->name, output_id);
106 if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data))))
108 pdata->start_sample = start_sample;
109 pdata->end_sample = end_sample;
112 switch (pdo->output_type) {
114 /* Annotations are only fed to callbacks. */
115 if ((cb = srd_find_callback(pdo->output_type))) {
116 /* Annotations need converting from PyObject. */
117 if (convert_pyobj(di, data, &pdata->ann_format,
118 (char ***)&pdata->data) != SRD_OK) {
119 /* An error was already logged. */
125 case SRD_OUTPUT_PROTO:
126 for (l = di->next_di; l; l = l->next) {
128 /* TODO: is this needed? */
129 Py_XINCREF(next_di->py_instance);
130 if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
131 "KKO", start_sample, end_sample, data))) {
132 if (PyErr_Occurred())
138 case SRD_OUTPUT_BINARY:
139 srd_err("SRD_OUTPUT_BINARY not yet supported");
142 srd_err("Protocol decoder %s submitted invalid output type %d",
143 di->decoder->name, pdo->output_type);
153 static PyObject *Decoder_add(PyObject *self, PyObject *args)
156 struct srd_decoder_instance *di;
158 int output_type, pdo_id;
160 if (!(di = get_di_by_decobject(self))) {
161 srd_err("%s():%d decoder instance not found", __func__, __LINE__);
162 PyErr_SetString(PyExc_Exception, "decoder instance not found");
166 if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
167 if (PyErr_Occurred())
172 pdo_id = pd_add(di, output_type, proto_id);
176 ret = Py_BuildValue("i", pdo_id);
181 static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
182 static PyMethodDef Decoder_methods[] = {
183 {"put", Decoder_put, METH_VARARGS,
184 "Accepts a dictionary with the following keys: time, duration, data"},
185 {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
186 {NULL, NULL, 0, NULL}
192 } sigrok_Decoder_object;
194 static PyTypeObject srd_Decoder_type = {
195 PyVarObject_HEAD_INIT(NULL, 0)
196 .tp_name = "sigrokdecode.Decoder",
197 .tp_basicsize = sizeof(sigrok_Decoder_object),
198 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
199 .tp_doc = "Sigrok Decoder object",
200 .tp_methods = Decoder_methods,
203 static struct PyModuleDef sigrokdecode_module = {
204 PyModuleDef_HEAD_INIT,
205 .m_name = "sigrokdecode",
206 .m_doc = "sigrokdecode base class",
208 .m_methods = no_methods,
211 PyMODINIT_FUNC PyInit_sigrokdecode(void)
215 /* tp_new needs to be assigned here for compiler portability */
216 srd_Decoder_type.tp_new = PyType_GenericNew;
217 if (PyType_Ready(&srd_Decoder_type) < 0)
220 srd_logic_type.tp_new = PyType_GenericNew;
221 if (PyType_Ready(&srd_logic_type) < 0)
224 mod = PyModule_Create(&sigrokdecode_module);
225 Py_INCREF(&srd_Decoder_type);
226 if (PyModule_AddObject(mod, "Decoder", (PyObject *)&srd_Decoder_type) == -1)
228 Py_INCREF(&srd_logic_type);
229 if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1)
232 /* expose output types as symbols in the sigrokdecode module */
233 if(PyModule_AddObject(mod, "OUTPUT_ANN",
234 PyLong_FromLong(SRD_OUTPUT_ANN)) == -1)
236 if(PyModule_AddObject(mod, "OUTPUT_PROTO",
237 PyLong_FromLong(SRD_OUTPUT_PROTO)) == -1)
239 if(PyModule_AddObject(mod, "OUTPUT_BINARY",
240 PyLong_FromLong(SRD_OUTPUT_BINARY)) == -1)