]> sigrok.org Git - libsigrokdecode.git/blame - controller.c
refactored PD framework, now using new sigrok.Decoder object
[libsigrokdecode.git] / controller.c
CommitLineData
b2c19614
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "config.h"
22#include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include <glib.h>
24
25/* TODO: this should probably be in sigrokdecode.h */
26/* Re-define some string functions for Python >= 3.0. */
27#if PY_VERSION_HEX >= 0x03000000
28#define PyString_AsString PyBytes_AsString
29#define PyString_FromString PyBytes_FromString
30#define PyString_Check PyBytes_Check
31#endif
32
33
34static GSList *pipelines = NULL;
35
36/* lives in decoder.c */
37extern GSList *list_pds;
38extern GSList *decoders;
39
40struct srd_pipeline {
41 int id;
42 GSList *decoders;
43};
44
45
46
47static PyObject *Decoder_init(PyObject *self, PyObject *args)
48{
49 (void)self;
50 (void)args;
51// printf("init object %x\n", self);
52
53 Py_RETURN_NONE;
54}
55
56
57static PyObject *Decoder_put(PyObject *self, PyObject *args)
58{
59 PyObject *arg;
60
61// printf("put object %x\n", self);
62
63 if (!PyArg_ParseTuple(args, "O:put", &arg))
64 return NULL;
65
66 // fprintf(stdout, "sigrok.put() called by decoder:\n");
67 PyObject_Print(arg, stdout, Py_PRINT_RAW);
68 puts("");
69
70 Py_RETURN_NONE;
71}
72
73static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
74static PyMethodDef Decoder_methods[] = {
75 {"__init__", Decoder_init, METH_VARARGS, ""},
76 {"put", Decoder_put, METH_VARARGS,
77 "Accepts a dictionary with the following keys: time, duration, data"},
78 {NULL, NULL, 0, NULL}
79};
80
81
82// class Decoder(sigrok.Decoder):
83typedef struct {
84 PyObject_HEAD
85} sigrok_Decoder_object;
86
87static PyTypeObject sigrok_Decoder_type = {
88 PyObject_HEAD_INIT(NULL)
89 0,
90 "sigrok.Decoder",
91 sizeof(sigrok_Decoder_object),
92 0,
93 0,
94 0,
95 0,
96 0,
97 0,
98 0,
99 0,
100 0,
101 0,
102 0,
103 0,
104 0,
105 0,
106 0,
107 0,
108 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
109 "Sigrok Decoder object",
110 0,
111 0,
112 0,
113 0,
114 0,
115 0,
116 Decoder_methods,
117};
118
119PyMODINIT_FUNC init_sigrok_Decoder(void)
120{
121 PyObject *mod;
122
123 sigrok_Decoder_type.tp_new = PyType_GenericNew;
124 if (PyType_Ready(&sigrok_Decoder_type) < 0)
125 return;
126
127 mod = Py_InitModule3("sigrok", no_methods, "sigrok base classes");
128 Py_INCREF(&sigrok_Decoder_type);
129 PyModule_AddObject(mod, "Decoder", (PyObject *)&sigrok_Decoder_type);
130
131}
132
133
134/**
135 * Initialize libsigrokdecode.
136 *
137 * This initializes the Python interpreter, and creates and initializes
138 * a "sigrok" Python module with a single put() method.
139 *
140 * Then, it searches for sigrok protocol decoder files (*.py) in the
141 * "decoders" subdirectory of the the sigrok installation directory.
142 * All decoders that are found are loaded into memory and added to an
143 * internal list of decoders, which can be queried via srd_list_decoders().
144 *
145 * The caller is responsible for calling the clean-up function srd_exit(),
146 * which will properly shut down libsigrokdecode and free its allocated memory.
147 *
148 * Multiple calls to srd_init(), without calling srd_exit() inbetween,
149 * are not allowed.
150 *
151 * @return SRD_OK upon success, a (negative) error code otherwise.
152 * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders
153 * directory cannot be accessed, return SRD_ERR_DECODERS_DIR.
154 * If not enough memory could be allocated, return SRD_ERR_MALLOC.
155 */
156int srd_init(void)
157{
158 int ret;
159
160 /* Py_Initialize() returns void and usually cannot fail. */
161 Py_Initialize();
162
163 init_sigrok_Decoder();
164
165 PyRun_SimpleString("import sys;");
166 if ((ret = set_modulepath()) != SRD_OK) {
167 Py_Finalize();
168 return ret;
169 }
170
171 if ((ret = srd_load_all_decoders()) != SRD_OK) {
172 Py_Finalize();
173 return ret;
174 }
175
176 return SRD_OK;
177}
178
179
180/**
181 * Shutdown libsigrokdecode.
182 *
183 * This frees all the memory allocated for protocol decoders and shuts down
184 * the Python interpreter.
185 *
186 * This function should only be called if there was a (successful!) invocation
187 * of srd_init() before. Calling this function multiple times in a row, without
188 * any successful srd_init() calls inbetween, is not allowed.
189 *
190 * @return SRD_OK upon success, a (negative) error code otherwise.
191 */
192int srd_exit(void)
193{
194 /* Unload/free all decoders, and then the list of decoders itself. */
195 /* TODO: Error handling. */
196 srd_unload_all_decoders();
197 g_slist_free(list_pds);
198
199 /* Py_Finalize() returns void, any finalization errors are ignored. */
200 Py_Finalize();
201
202 return SRD_OK;
203}
204
205
206/**
207 * Add search directories for the protocol decoders.
208 *
209 * TODO: add path from env var SIGROKDECODE_PATH, config etc
210 */
211int set_modulepath(void)
212{
213 int ret;
214
215 ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');");
216
217 return ret;
218}
219
220
221struct srd_decoder_instance *srd_instance_new(const char *id)
222{
223 struct srd_decoder *dec;
224 struct srd_decoder_instance *di;
225 PyObject *py_args;
226
227 fprintf(stdout, "%s: %s\n", __func__, id);
228
229 if (!(dec = srd_get_decoder_by_id(id)))
230 return NULL;
231
232 /* TODO: Error handling. Use g_try_malloc(). */
233 di = g_malloc(sizeof(*di));
234 di->decoder = dec;
235 di->pd_output = NULL;
236
237 /* Create an empty Python tuple. */
238 if (!(py_args = PyTuple_New(0))) { /* NEWREF */
239 if (PyErr_Occurred())
240 PyErr_Print(); /* Returns void. */
241
242 return NULL; /* TODO: More specific error? */
243 }
244
245 /* Create an instance of the 'Decoder' class. */
246 di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL);
247 if (!di->py_instance) {
248 if (PyErr_Occurred())
249 PyErr_Print(); /* Returns void. */
250 Py_XDECREF(py_args);
251 return NULL; /* TODO: More specific error? */
252 }
253 decoders = g_slist_append(decoders, di);
254
255 Py_XDECREF(py_args);
256
257 return di;
258}
259
260
261int srd_instance_set_probe(struct srd_decoder_instance *di,
262 const char *probename, int num)
263{
264 PyObject *probedict, *probenum;
265
266 probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */
267 if (!probedict) {
268 if (PyErr_Occurred())
269 PyErr_Print(); /* Returns void. */
270
271 return SRD_ERR_PYTHON; /* TODO: More specific error? */
272 }
273
274 probenum = PyInt_FromLong(num);
275 PyMapping_SetItemString(probedict, (char *)probename, probenum);
276
277 Py_XDECREF(probenum);
278 Py_XDECREF(probedict);
279
280 return SRD_OK;
281}
282
283
284int srd_session_start(const char *driver, int unitsize, uint64_t starttime,
285 uint64_t samplerate)
286{
287 PyObject *py_res;
288 GSList *d;
289 struct srd_decoder_instance *di;
290
291 fprintf(stdout, "%s: %s\n", __func__, driver);
292
293 for (d = decoders; d; d = d->next) {
294 di = d->data;
295 if (!(py_res = PyObject_CallMethod(di->py_instance, "start",
296 "{s:s,s:i,s:d}",
297 "driver", driver,
298 "unitsize", unitsize,
299 "starttime", starttime))) {
300 if (PyErr_Occurred())
301 PyErr_Print(); /* Returns void. */
302
303 return SRD_ERR_PYTHON; /* TODO: More specific error? */
304 }
305 Py_XDECREF(py_res);
306 }
307
308 return SRD_OK;
309}
310
311
312/**
313 * Run the specified decoder function.
314 *
315 * @param dec TODO
316 * @param inbuf TODO
317 * @param inbuflen TODO
318 * @param outbuf TODO
319 * @param outbuflen TODO
320 *
321 * @return SRD_OK upon success, a (negative) error code otherwise.
322 */
323int srd_run_decoder(struct srd_decoder_instance *dec,
324 uint8_t *inbuf, uint64_t inbuflen)
325{
326 PyObject *py_instance, *py_res;
327 /* FIXME: Don't have a timebase available here. Make one up. */
328 static int _timehack = 0;
329
330 _timehack += inbuflen;
331
332// fprintf(stdout, "%s: %s\n", __func__, dec->decoder->name);
333
334 /* Return an error upon unusable input. */
335 if (dec == NULL)
336 return SRD_ERR_ARGS; /* TODO: More specific error? */
337 if (inbuf == NULL)
338 return SRD_ERR_ARGS; /* TODO: More specific error? */
339 if (inbuflen == 0) /* No point in working on empty buffers. */
340 return SRD_ERR_ARGS; /* TODO: More specific error? */
341
342 /* TODO: Error handling. */
343 py_instance = dec->py_instance;
344 Py_XINCREF(py_instance);
345
346 if (!(py_res = PyObject_CallMethod(py_instance, "decode",
347 "{s:i,s:i,s:s#}",
348 "time", _timehack,
349 "duration", 10,
350 "data", inbuf, inbuflen))) { /* NEWREF */
351 if (PyErr_Occurred())
352 PyErr_Print(); /* Returns void. */
353
354 return SRD_ERR_PYTHON; /* TODO: More specific error? */
355 }
356
357 Py_XDECREF(py_res);
358 return SRD_OK;
359}
360
361
362/* Feed logic samples to decoder session. */
363int srd_session_feed(uint8_t *inbuf, uint64_t inbuflen)
364{
365 GSList *d;
366 int ret;
367
368// fprintf(stdout, "%s: %d bytes\n", __func__, inbuflen);
369
370 for (d = decoders; d; d = d->next) {
371 if ((ret = srd_run_decoder(d->data, inbuf, inbuflen)) != SRD_OK)
372 return ret;
373 }
374
375 return SRD_OK;
376}
377
378
379//int srd_pipeline_new(int plid)
380//{
381//
382//
383//}
384//
385//
386//int pd_output_new(int output_type, char *output_id, char *description)
387//{
388//
389//
390//}
391
392
393
394