]> sigrok.org Git - libsigrokdecode.git/blob - module_sigrokdecode.c
srd: Bring back small stuff lost in the merge.
[libsigrokdecode.git] / module_sigrokdecode.c
1 /*
2  * This file is part of the sigrok 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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21 #include "sigrokdecode-internal.h"
22 #include "config.h"
23
24
25 /* When initialized, a reference to this module inside the python interpreter
26  * lives here.
27  */
28 PyObject *mod_sigrokdecode = NULL;
29
30 /* lives in type_logic.c */
31 extern PyTypeObject srd_logic_type;
32
33
34 static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj,
35                 int *ann_format, char ***ann)
36 {
37         PyObject *py_tmp;
38         struct srd_pd_output *pdo;
39         int ann_id;
40
41         /* Should be a list of [annotation format, [string, ...]] */
42         if (!PyList_Check(obj) && !PyTuple_Check(obj)) {
43                 srd_err("Protocol decoder %s submitted %s instead of list",
44                                 di->decoder->name, obj->ob_type->tp_name);
45                 return SRD_ERR_PYTHON;
46         }
47
48         /* Should have 2 elements... */
49         if (PyList_Size(obj) != 2) {
50                 srd_err("Protocol decoder %s submitted annotation list with %d elements instead of 2",
51                                 di->decoder->name, PyList_Size(obj));
52                 return SRD_ERR_PYTHON;
53         }
54
55         /* First element should be an integer matching a previously
56          * registered annotation format. */
57         py_tmp = PyList_GetItem(obj, 0);
58         if (!PyLong_Check(py_tmp)) {
59                 srd_err("Protocol decoder %s submitted annotation list, but first element was not an integer",
60                                 di->decoder->name);
61                 return SRD_ERR_PYTHON;
62         }
63
64         ann_id = PyLong_AsLong(py_tmp);
65         if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_id))) {
66                 srd_err("Protocol decoder %s submitted data to non-existent annotation format %d",
67                                 di->decoder->name, ann_id);
68                 return SRD_ERR_PYTHON;
69         }
70         *ann_format = ann_id;
71
72         /* Second element must be a list */
73         py_tmp = PyList_GetItem(obj, 1);
74         if (!PyList_Check(py_tmp)) {
75                 srd_err("Protocol decoder %s submitted annotation list, but second element was not a list",
76                                 di->decoder->name);
77                 return SRD_ERR_PYTHON;
78         }
79         if (py_strlist_to_char(py_tmp, ann) != SRD_OK) {
80                 srd_err("Protocol decoder %s submitted annotation list, but second element was malformed",
81                                 di->decoder->name);
82                 return SRD_ERR_PYTHON;
83         }
84
85         return SRD_OK;
86 }
87
88 static PyObject *Decoder_put(PyObject *self, PyObject *args)
89 {
90         GSList *l;
91         PyObject *data, *py_res;
92         struct srd_decoder_instance *di, *next_di;
93         struct srd_pd_output *pdo;
94         struct srd_proto_data *pdata;
95         uint64_t start_sample, end_sample;
96         int output_id;
97         void (*cb)();
98
99         if (!(di = get_di_by_decobject(self)))
100                 return NULL;
101
102         if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data))
103                 return NULL;
104
105         if (!(l = g_slist_nth(di->pd_output, output_id))) {
106                 srd_err("Protocol decoder %s submitted invalid output ID %d",
107                                 di->decoder->name, output_id);
108                 return NULL;
109         }
110         pdo = l->data;
111
112         if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data))))
113                 return NULL;
114         pdata->start_sample = start_sample;
115         pdata->end_sample = end_sample;
116         pdata->pdo = pdo;
117
118         switch (pdo->output_type) {
119         case SRD_OUTPUT_ANN:
120                 /* Annotations are only fed to callbacks. */
121                 if ((cb = srd_find_callback(pdo->output_type))) {
122                         /* Annotations need converting from PyObject. */
123                         if (convert_pyobj(di, data, &pdata->ann_format,
124                                         (char ***)&pdata->data) != SRD_OK) {
125                                 /* An error was already logged. */
126                                 break;
127                         }
128                         cb(pdata);
129                 }
130                 break;
131         case SRD_OUTPUT_PROTO:
132                 for (l = di->next_di; l; l = l->next) {
133                         next_di = l->data;
134                         /* TODO: is this needed? */
135                         Py_XINCREF(next_di->py_instance);
136                         if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
137                                         "KKO", start_sample, end_sample, data))) {
138                                 if (PyErr_Occurred())
139                                         PyErr_Print();
140                         }
141                         Py_XDECREF(py_res);
142                 }
143                 break;
144         case SRD_OUTPUT_BINARY:
145                 srd_err("SRD_OUTPUT_BINARY not yet supported");
146                 break;
147         default:
148                 srd_err("Protocol decoder %s submitted invalid output type %d",
149                                 di->decoder->name, pdo->output_type);
150                 break;
151         }
152
153         g_free(pdata);
154
155         Py_RETURN_NONE;
156 }
157
158
159 static PyObject *Decoder_add(PyObject *self, PyObject *args)
160 {
161         PyObject *ret;
162         struct srd_decoder_instance *di;
163         char *proto_id;
164         int output_type, pdo_id;
165
166         if (!(di = get_di_by_decobject(self))) {
167                 srd_err("%s():%d decoder instance not found", __func__, __LINE__);
168                 PyErr_SetString(PyExc_Exception, "decoder instance not found");
169                 return NULL;
170         }
171
172         if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
173                 if (PyErr_Occurred())
174                         PyErr_Print();
175                 return NULL;
176         }
177
178         pdo_id = pd_add(di, output_type, proto_id);
179         if (pdo_id < 0)
180                 Py_RETURN_NONE;
181         else
182                 ret = Py_BuildValue("i", pdo_id);
183
184         return ret;
185 }
186
187 static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
188 static PyMethodDef Decoder_methods[] = {
189         {"put", Decoder_put, METH_VARARGS,
190          "Accepts a dictionary with the following keys: time, duration, data"},
191         {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
192         {NULL, NULL, 0, NULL}
193 };
194
195
196 PyTypeObject srd_Decoder_type = {
197         PyVarObject_HEAD_INIT(NULL, 0)
198         .tp_name = "sigrokdecode.Decoder",
199         .tp_basicsize = sizeof(srd_Decoder),
200         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
201         .tp_doc = "Sigrok Decoder base class",
202         .tp_methods = Decoder_methods,
203 };
204
205 static struct PyModuleDef sigrokdecode_module = {
206         PyModuleDef_HEAD_INIT,
207         .m_name = "sigrokdecode",
208         .m_doc = "sigrokdecode module",
209         .m_size = -1,
210         .m_methods = no_methods,
211 };
212
213 PyMODINIT_FUNC PyInit_sigrokdecode(void)
214 {
215         PyObject *mod;
216
217         /* tp_new needs to be assigned here for compiler portability */
218         srd_Decoder_type.tp_new = PyType_GenericNew;
219         if (PyType_Ready(&srd_Decoder_type) < 0)
220                 return NULL;
221
222         srd_logic_type.tp_new = PyType_GenericNew;
223         if (PyType_Ready(&srd_logic_type) < 0)
224                 return NULL;
225
226         mod = PyModule_Create(&sigrokdecode_module);
227         Py_INCREF(&srd_Decoder_type);
228         if (PyModule_AddObject(mod, "Decoder", (PyObject *)&srd_Decoder_type) == -1)
229                 return NULL;
230         Py_INCREF(&srd_logic_type);
231         if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1)
232                 return NULL;
233
234         /* expose output types as symbols in the sigrokdecode module */
235         if(PyModule_AddObject(mod, "OUTPUT_ANN",
236                         PyLong_FromLong(SRD_OUTPUT_ANN)) == -1)
237                 return NULL;
238         if(PyModule_AddObject(mod, "OUTPUT_PROTO",
239                         PyLong_FromLong(SRD_OUTPUT_PROTO)) == -1)
240                 return NULL;
241         if(PyModule_AddObject(mod, "OUTPUT_BINARY",
242                         PyLong_FromLong(SRD_OUTPUT_BINARY)) == -1)
243                 return NULL;
244
245         mod_sigrokdecode = mod;
246
247         return mod;
248 }
249