]> sigrok.org Git - libsigrokdecode.git/blame - module_sigrokdecode.c
i2c: fix address handling
[libsigrokdecode.git] / module_sigrokdecode.c
CommitLineData
bc5f5a43
BV
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. */
15969949 21#include "sigrokdecode-internal.h"
bc5f5a43
BV
22#include "config.h"
23
24/* lives in type_logic.c */
25extern PyTypeObject srd_logic_type;
26
27
15969949
BV
28static int convert_pyobj(struct srd_decoder_instance *di, PyObject *obj,
29 int *annotation_format, char ***annotation)
30{
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);
59 if (!(pdo = g_slist_nth_data(di->decoder->annotation, 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;
63 }
64 *annotation_format = ann_id;
65
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 }
73 if (py_strlist_to_char(py_tmp, annotation) != SRD_OK) {
74 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
bc5f5a43
BV
82/* TODO: not used, doesn't work actually */
83static PyObject *Decoder_init(PyObject *self, PyObject *args)
84{
85 (void)self;
86 (void)args;
87 printf("init Decoder object %p\n", self);
88
89 Py_RETURN_NONE;
90}
91
92static PyObject *Decoder_put(PyObject *self, PyObject *args)
93{
94 GSList *l;
95 PyObject *data;
96 struct srd_decoder_instance *di;
97 struct srd_pd_output *pdo;
983cb0f5
BV
98 struct srd_protocol_data *pdata;
99 uint64_t start_sample, end_sample;
100 int output_id;
101 void (*cb)();
bc5f5a43
BV
102
103 if (!(di = get_di_by_decobject(self)))
104 return NULL;
105
983cb0f5 106 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample, &output_id, &data))
bc5f5a43
BV
107 return NULL;
108
109 if (!(l = g_slist_nth(di->pd_output, output_id))) {
15969949
BV
110 srd_err("Protocol decoder %s submitted invalid output ID %d",
111 di->decoder->name, output_id);
bc5f5a43
BV
112 return NULL;
113 }
114 pdo = l->data;
115
15969949
BV
116 switch (pdo->output_type) {
117 case SRD_OUTPUT_ANNOTATION:
15969949 118 case SRD_OUTPUT_PROTOCOL:
983cb0f5 119 case SRD_OUTPUT_BINARY:
15969949 120 break;
15969949
BV
121 default:
122 srd_err("Protocol decoder %s submitted invalid output type %d",
123 di->decoder->name, pdo->output_type);
983cb0f5 124 return NULL;
15969949
BV
125 break;
126 }
bc5f5a43 127
983cb0f5
BV
128 if ((cb = srd_find_callback(pdo->output_type))) {
129 /* Something registered an interest in this output type. */
130 if (!(pdata = g_try_malloc0(sizeof(struct srd_protocol_data))))
131 return NULL;
132 pdata->start_sample = start_sample;
133 pdata->end_sample = end_sample;
134 pdata->pdo = pdo;
135 if (pdo->output_type == SRD_OUTPUT_ANNOTATION) {
136 /* annotations need converting from PyObject */
137 if (convert_pyobj(di, data, &pdata->annotation_format,
138 (char ***)&pdata->data) != SRD_OK)
139 return NULL;
140 } else {
141 /* annotation_format is unused, data is an opaque blob. */
142 pdata->data = data;
143 }
144 cb(pdata);
145 }
146
bc5f5a43
BV
147 Py_RETURN_NONE;
148}
149
150
0ee4dd54 151static PyObject *Decoder_output_new(PyObject *self, PyObject *args)
bc5f5a43
BV
152{
153 PyObject *ret;
154 struct srd_decoder_instance *di;
15969949 155 char *protocol_id;
bc5f5a43
BV
156 int output_type, pdo_id;
157
158 if (!(di = get_di_by_decobject(self)))
159 return NULL;
160
161 printf("output_new di %s\n", di->decoder->name);
162
0ee4dd54 163 if (!PyArg_ParseTuple(args, "is", &output_type, &protocol_id))
bc5f5a43
BV
164 return NULL;
165
15969949 166 pdo_id = pd_output_new(di, output_type, protocol_id);
bc5f5a43
BV
167 if (pdo_id < 0)
168 Py_RETURN_NONE;
169 else
170 ret = Py_BuildValue("i", pdo_id);
171
172 return ret;
173}
174
175static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
176static PyMethodDef Decoder_methods[] = {
177 {"put", Decoder_put, METH_VARARGS,
178 "Accepts a dictionary with the following keys: time, duration, data"},
179 {"output_new", Decoder_output_new, METH_VARARGS,
180 "Create a new output stream"},
181 {NULL, NULL, 0, NULL}
182};
183
184
185typedef struct {
186 PyObject_HEAD
187} sigrok_Decoder_object;
188
189static PyTypeObject srd_Decoder_type = {
190 PyVarObject_HEAD_INIT(NULL, 0)
191 .tp_name = "sigrokdecode.Decoder",
192 .tp_basicsize = sizeof(sigrok_Decoder_object),
193 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
194 .tp_doc = "Sigrok Decoder object",
195 .tp_methods = Decoder_methods,
196 .tp_init = (initproc) Decoder_init,
197};
198
199static struct PyModuleDef sigrokdecode_module = {
200 PyModuleDef_HEAD_INIT,
201 .m_name = "sigrokdecode",
202 .m_doc = "sigrokdecode base class",
203 .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
3e2a9de2
BV
228 /* expose output types as symbols in the sigrokdecode module */
229 if(PyModule_AddObject(mod, "SRD_OUTPUT_ANNOTATION",
230 PyLong_FromLong(SRD_OUTPUT_ANNOTATION)) == -1)
231 return NULL;
232 if(PyModule_AddObject(mod, "SRD_OUTPUT_PROTOCOL",
233 PyLong_FromLong(SRD_OUTPUT_PROTOCOL)) == -1)
234 return NULL;
235 if(PyModule_AddObject(mod, "SRD_OUTPUT_BINARY",
236 PyLong_FromLong(SRD_OUTPUT_BINARY)) == -1)
237 return NULL;
238
bc5f5a43
BV
239 return mod;
240}
241