blob: a18583bb5ea9b6ae33ebd1e17804f1f046540a15 [file] [log] [blame]
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +01001/*
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. */
Bert Vermeulen15969942012-01-07 02:50:14 +010021#include "sigrokdecode-internal.h"
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010022#include "config.h"
23
24/* lives in type_logic.c */
25extern PyTypeObject srd_logic_type;
26
27
Bert Vermeulen15969942012-01-07 02:50:14 +010028static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj,
Uwe Hermann94d43b32012-01-10 20:44:22 +010029 int *ann_format, char ***ann)
Bert Vermeulen15969942012-01-07 02:50:14 +010030{
31 PyObject *py_tmp;
32 struct srd_pd_output *pdo;
33 int ann_id;
34
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;
40 }
41
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;
47 }
48
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",
54 di->decoder->name);
55 return SRD_ERR_PYTHON;
56 }
57
58 ann_id = PyLong_AsLong(py_tmp);
Uwe Hermanne97b6ef2012-01-10 21:05:09 +010059 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_id))) {
Bert Vermeulen15969942012-01-07 02:50:14 +010060 srd_err("Protocol decoder %s submitted data to non-existent annotation format %d",
61 di->decoder->name, ann_id);
62 return SRD_ERR_PYTHON;
63 }
Uwe Hermann94d43b32012-01-10 20:44:22 +010064 *ann_format = ann_id;
Bert Vermeulen15969942012-01-07 02:50:14 +010065
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",
70 di->decoder->name);
71 return SRD_ERR_PYTHON;
72 }
Uwe Hermann94d43b32012-01-10 20:44:22 +010073 if (py_strlist_to_char(py_tmp, ann) != SRD_OK) {
Bert Vermeulen15969942012-01-07 02:50:14 +010074 srd_err("Protocol decoder %s submitted annotation list, but second element was malformed",
75 di->decoder->name);
76 return SRD_ERR_PYTHON;
77 }
78
79 return SRD_OK;
80}
81
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010082static PyObject *Decoder_put(PyObject *self, PyObject *args)
83{
84 GSList *l;
Bert Vermeulen7ce77752012-01-10 00:25:16 +010085 PyObject *data, *py_res;
86 struct srd_decoder_instance *di, *next_di;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010087 struct srd_pd_output *pdo;
Uwe Hermann94d43b32012-01-10 20:44:22 +010088 struct srd_proto_data *pdata;
Bert Vermeulen983cb0f2012-01-08 03:20:12 +010089 uint64_t start_sample, end_sample;
90 int output_id;
91 void (*cb)();
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010092
93 if (!(di = get_di_by_decobject(self)))
94 return NULL;
95
Bert Vermeulen983cb0f2012-01-08 03:20:12 +010096 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data))
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010097 return NULL;
98
99 if (!(l = g_slist_nth(di->pd_output, output_id))) {
Bert Vermeulen15969942012-01-07 02:50:14 +0100100 srd_err("Protocol decoder %s submitted invalid output ID %d",
101 di->decoder->name, output_id);
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100102 return NULL;
103 }
104 pdo = l->data;
105
Uwe Hermann94d43b32012-01-10 20:44:22 +0100106 if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data))))
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100107 return NULL;
108 pdata->start_sample = start_sample;
109 pdata->end_sample = end_sample;
110 pdata->pdo = pdo;
111
Bert Vermeulen15969942012-01-07 02:50:14 +0100112 switch (pdo->output_type) {
Uwe Hermann94d43b32012-01-10 20:44:22 +0100113 case SRD_OUTPUT_ANN:
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100114 /* Annotations are only fed to callbacks. */
115 if ((cb = srd_find_callback(pdo->output_type))) {
116 /* Annotations need converting from PyObject. */
Uwe Hermann94d43b32012-01-10 20:44:22 +0100117 if (convert_pyobj(di, data, &pdata->ann_format,
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100118 (char ***)&pdata->data) != SRD_OK) {
119 /* An error was already logged. */
120 break;
121 }
122 cb(pdata);
123 }
124 break;
Uwe Hermann94d43b32012-01-10 20:44:22 +0100125 case SRD_OUTPUT_PROTO:
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100126 for (l = di->next_di; l; l = l->next) {
127 next_di = l->data;
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())
133 PyErr_Print();
134 }
135 Py_XDECREF(py_res);
136 }
137 break;
Bert Vermeulen983cb0f2012-01-08 03:20:12 +0100138 case SRD_OUTPUT_BINARY:
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100139 srd_err("SRD_OUTPUT_BINARY not yet supported");
Bert Vermeulen15969942012-01-07 02:50:14 +0100140 break;
Bert Vermeulen15969942012-01-07 02:50:14 +0100141 default:
142 srd_err("Protocol decoder %s submitted invalid output type %d",
143 di->decoder->name, pdo->output_type);
144 break;
145 }
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100146
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100147 g_free(pdata);
Bert Vermeulen983cb0f2012-01-08 03:20:12 +0100148
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100149 Py_RETURN_NONE;
150}
151
152
Bert Vermeulenf9a39472012-01-09 00:12:19 +0100153static PyObject *Decoder_add(PyObject *self, PyObject *args)
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100154{
155 PyObject *ret;
156 struct srd_decoder_instance *di;
Uwe Hermann94d43b32012-01-10 20:44:22 +0100157 char *proto_id;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100158 int output_type, pdo_id;
159
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100160 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");
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100163 return NULL;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100164 }
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100165
Uwe Hermann94d43b32012-01-10 20:44:22 +0100166 if (!PyArg_ParseTuple(args, "is", &output_type, &proto_id)) {
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100167 if (PyErr_Occurred())
168 PyErr_Print();
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100169 return NULL;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100170 }
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100171
Uwe Hermann94d43b32012-01-10 20:44:22 +0100172 pdo_id = pd_add(di, output_type, proto_id);
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100173 if (pdo_id < 0)
174 Py_RETURN_NONE;
175 else
176 ret = Py_BuildValue("i", pdo_id);
177
178 return ret;
179}
180
181static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
182static PyMethodDef Decoder_methods[] = {
183 {"put", Decoder_put, METH_VARARGS,
184 "Accepts a dictionary with the following keys: time, duration, data"},
Bert Vermeulenf9a39472012-01-09 00:12:19 +0100185 {"add", Decoder_add, METH_VARARGS, "Create a new output stream"},
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100186 {NULL, NULL, 0, NULL}
187};
188
189
Bert Vermeulenf8e45852012-01-15 03:43:01 +0100190PyTypeObject srd_Decoder_type = {
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100191 PyVarObject_HEAD_INIT(NULL, 0)
192 .tp_name = "sigrokdecode.Decoder",
Bert Vermeulenf8e45852012-01-15 03:43:01 +0100193 .tp_basicsize = sizeof(srd_Decoder),
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100194 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Bert Vermeulenf8e45852012-01-15 03:43:01 +0100195 .tp_doc = "Sigrok Decoder base class",
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100196 .tp_methods = Decoder_methods,
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100197};
198
199static struct PyModuleDef sigrokdecode_module = {
200 PyModuleDef_HEAD_INIT,
201 .m_name = "sigrokdecode",
Bert Vermeulenf8e45852012-01-15 03:43:01 +0100202 .m_doc = "sigrokdecode module",
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100203 .m_size = -1,
204 .m_methods = no_methods,
205};
206
207PyMODINIT_FUNC PyInit_sigrokdecode(void)
208{
209 PyObject *mod;
210
211 /* tp_new needs to be assigned here for compiler portability */
212 srd_Decoder_type.tp_new = PyType_GenericNew;
213 if (PyType_Ready(&srd_Decoder_type) < 0)
214 return NULL;
215
216 srd_logic_type.tp_new = PyType_GenericNew;
217 if (PyType_Ready(&srd_logic_type) < 0)
218 return NULL;
219
220 mod = PyModule_Create(&sigrokdecode_module);
221 Py_INCREF(&srd_Decoder_type);
222 if (PyModule_AddObject(mod, "Decoder", (PyObject *)&srd_Decoder_type) == -1)
223 return NULL;
224 Py_INCREF(&srd_logic_type);
225 if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1)
226 return NULL;
227
Bert Vermeulen3e2a9de2012-01-07 04:18:16 +0100228 /* expose output types as symbols in the sigrokdecode module */
Uwe Hermann56202222012-01-11 00:52:54 +0100229 if(PyModule_AddObject(mod, "OUTPUT_ANN",
Uwe Hermann94d43b32012-01-10 20:44:22 +0100230 PyLong_FromLong(SRD_OUTPUT_ANN)) == -1)
Bert Vermeulen3e2a9de2012-01-07 04:18:16 +0100231 return NULL;
Uwe Hermann56202222012-01-11 00:52:54 +0100232 if(PyModule_AddObject(mod, "OUTPUT_PROTO",
Uwe Hermann94d43b32012-01-10 20:44:22 +0100233 PyLong_FromLong(SRD_OUTPUT_PROTO)) == -1)
Bert Vermeulen3e2a9de2012-01-07 04:18:16 +0100234 return NULL;
Uwe Hermann56202222012-01-11 00:52:54 +0100235 if(PyModule_AddObject(mod, "OUTPUT_BINARY",
Bert Vermeulen3e2a9de2012-01-07 04:18:16 +0100236 PyLong_FromLong(SRD_OUTPUT_BINARY)) == -1)
237 return NULL;
238
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100239 return mod;
240}
241