]> sigrok.org Git - libsigrokdecode.git/blame - controller.c
srd: clean up module loading/unloading, and the decoder struct
[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>
bc5f5a43 5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
b2c19614
BV
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
73191416 21#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
7ce7775c
BV
22#include "sigrokdecode-internal.h"
23#include "config.h"
b2c19614 24#include <glib.h>
1aef2f93 25#include <inttypes.h>
b2c19614 26
b2c19614 27
7ce7775c 28static GSList *di_list = NULL;
983cb0f5
BV
29static GSList *callbacks = NULL;
30
bc5f5a43
BV
31/* lives in decoder.c */
32extern GSList *pd_list;
b2c19614 33
bc5f5a43
BV
34/* lives in module_sigrokdecode.c */
35extern PyMODINIT_FUNC PyInit_sigrokdecode(void);
b2c19614 36
bc5f5a43
BV
37/* lives in type_logic.c */
38extern PyTypeObject srd_logic_type;
b2c19614 39
e5080882 40
b2c19614
BV
41/**
42 * Initialize libsigrokdecode.
43 *
44 * This initializes the Python interpreter, and creates and initializes
45 * a "sigrok" Python module with a single put() method.
46 *
47 * Then, it searches for sigrok protocol decoder files (*.py) in the
48 * "decoders" subdirectory of the the sigrok installation directory.
49 * All decoders that are found are loaded into memory and added to an
50 * internal list of decoders, which can be queried via srd_list_decoders().
51 *
52 * The caller is responsible for calling the clean-up function srd_exit(),
53 * which will properly shut down libsigrokdecode and free its allocated memory.
54 *
7ce7775c 55 * Multiple calls to srd_init(), without calling srd_exit() in between,
b2c19614
BV
56 * are not allowed.
57 *
58 * @return SRD_OK upon success, a (negative) error code otherwise.
59 * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders
60 * directory cannot be accessed, return SRD_ERR_DECODERS_DIR.
61 * If not enough memory could be allocated, return SRD_ERR_MALLOC.
62 */
63int srd_init(void)
64{
65 int ret;
66
bc5f5a43 67 PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
5f802ec6 68
b2c19614
BV
69 /* Py_Initialize() returns void and usually cannot fail. */
70 Py_Initialize();
71
b2c19614
BV
72 if ((ret = set_modulepath()) != SRD_OK) {
73 Py_Finalize();
74 return ret;
75 }
76
77 if ((ret = srd_load_all_decoders()) != SRD_OK) {
78 Py_Finalize();
79 return ret;
80 }
81
82 return SRD_OK;
83}
84
85
86/**
87 * Shutdown libsigrokdecode.
88 *
89 * This frees all the memory allocated for protocol decoders and shuts down
90 * the Python interpreter.
91 *
92 * This function should only be called if there was a (successful!) invocation
93 * of srd_init() before. Calling this function multiple times in a row, without
7ce7775c 94 * any successful srd_init() calls in between, is not allowed.
b2c19614
BV
95 *
96 * @return SRD_OK upon success, a (negative) error code otherwise.
97 */
98int srd_exit(void)
99{
100 /* Unload/free all decoders, and then the list of decoders itself. */
101 /* TODO: Error handling. */
102 srd_unload_all_decoders();
e5080882 103 g_slist_free(pd_list);
b2c19614
BV
104
105 /* Py_Finalize() returns void, any finalization errors are ignored. */
106 Py_Finalize();
107
108 return SRD_OK;
109}
110
111
112/**
113 * Add search directories for the protocol decoders.
114 *
115 * TODO: add path from env var SIGROKDECODE_PATH, config etc
116 */
117int set_modulepath(void)
118{
119 int ret;
120
bc5f5a43 121 PyRun_SimpleString("import sys");
b2c19614
BV
122 ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');");
123
124 return ret;
125}
126
127
7ce7775c
BV
128/**
129 * Create a new protocol decoder instance.
130 *
131 * TODO: this should be a decoder name, as decoder ids will disappear.
7ce7775c 132 *
42a85ed0
UH
133 * @param id Decoder 'id' field.
134 * @param instance_id Optional unique identifier for this instance. If NULL,
135 * the 'id' parameter is used.
136 * @return Pointer to a newly allocated struct srd_decoder_instance, or
137 * NULL in case of failure.
7ce7775c
BV
138 */
139struct srd_decoder_instance *srd_instance_new(const char *id,
140 const char *instance_id)
b2c19614
BV
141{
142 struct srd_decoder *dec;
143 struct srd_decoder_instance *di;
144 PyObject *py_args;
145
7ce7775c
BV
146 srd_dbg("%s: creating new %s instance", __func__, id);
147
b2c19614
BV
148 if (!(dec = srd_get_decoder_by_id(id)))
149 return NULL;
150
7ce7775c
BV
151 if (!(di = g_try_malloc(sizeof(*di)))) {
152 srd_err("failed to malloc instance");
153 return NULL;
154 }
b2c19614 155 di->decoder = dec;
7ce7775c 156 di->instance_id = g_strdup(instance_id ? instance_id : id);
b2c19614 157 di->pd_output = NULL;
7ce7775c 158 di->num_probes = 0;
bc5f5a43 159 di->unitsize = 0;
7ce7775c
BV
160 di->samplerate = 0;
161 di->next_di = NULL;
b2c19614
BV
162
163 /* Create an empty Python tuple. */
164 if (!(py_args = PyTuple_New(0))) { /* NEWREF */
165 if (PyErr_Occurred())
7ce7775c
BV
166 PyErr_Print();
167 return NULL;
b2c19614
BV
168 }
169
170 /* Create an instance of the 'Decoder' class. */
451680f1 171 di->py_instance = PyObject_Call(dec->py_dec, py_args, NULL);
b2c19614
BV
172 if (!di->py_instance) {
173 if (PyErr_Occurred())
7ce7775c 174 PyErr_Print();
b2c19614 175 Py_XDECREF(py_args);
7ce7775c 176 return NULL;
b2c19614 177 }
7ce7775c
BV
178
179 /* Instance takes input from a frontend by default. */
e5080882 180 di_list = g_slist_append(di_list, di);
b2c19614
BV
181
182 Py_XDECREF(py_args);
183
184 return di;
185}
186
7ce7775c
BV
187int srd_instance_stack(struct srd_decoder_instance *di_from,
188 struct srd_decoder_instance *di_to)
189{
190
191 if (!di_from || !di_to) {
192 srd_err("invalid from/to instance pair");
193 return SRD_ERR_ARG;
194 }
195
196 if (!g_slist_find(di_list, di_from)) {
197 srd_err("unstacked instance not found");
198 return SRD_ERR_ARG;
199 }
200
201 /* Remove from the unstacked list. */
202 di_list = g_slist_remove(di_list, di_to);
203
204 /* Stack on top of source di. */
205 di_from->next_di = g_slist_append(di_from->next_di, di_to);
206
207 return SRD_OK;
208}
209
b2c19614
BV
210
211int srd_instance_set_probe(struct srd_decoder_instance *di,
212 const char *probename, int num)
213{
214 PyObject *probedict, *probenum;
215
216 probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */
217 if (!probedict) {
218 if (PyErr_Occurred())
219 PyErr_Print(); /* Returns void. */
220
221 return SRD_ERR_PYTHON; /* TODO: More specific error? */
222 }
223
5f802ec6 224 probenum = PyLong_FromLong(num);
b2c19614
BV
225 PyMapping_SetItemString(probedict, (char *)probename, probenum);
226
227 Py_XDECREF(probenum);
228 Py_XDECREF(probedict);
229
230 return SRD_OK;
231}
232
451680f1 233/* TODO: this should go into the PD stack */
7ce7775c
BV
234struct srd_decoder_instance *srd_instance_find(char *instance_id)
235{
236 GSList *l;
237 struct srd_decoder_instance *tmp, *di;
b2c19614 238
7ce7775c
BV
239 di = NULL;
240 for (l = di_list; l; l = l->next) {
241 tmp = l->data;
242 if (!strcmp(tmp->instance_id, instance_id)) {
243 di = tmp;
244 break;
245 }
246 }
247
248 return di;
249}
250
251int srd_instance_start(struct srd_decoder_instance *di, PyObject *args)
b2c19614 252{
7ce7775c 253 PyObject *py_name, *py_res;
b2c19614 254
7ce7775c 255 srd_dbg("calling start() method on protocol decoder instance %s", di->instance_id);
b2c19614 256
7ce7775c
BV
257 if (!(py_name = PyUnicode_FromString("start"))) {
258 srd_err("unable to build python object for 'start'");
259 if (PyErr_Occurred())
260 PyErr_Print();
261 return SRD_ERR_PYTHON;
262 }
263
264 if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance,
265 py_name, args, NULL))) {
266 if (PyErr_Occurred())
267 PyErr_Print();
268 return SRD_ERR_PYTHON;
b2c19614
BV
269 }
270
7ce7775c
BV
271 Py_XDECREF(py_res);
272 Py_DECREF(py_name);
273
b2c19614
BV
274 return SRD_OK;
275}
276
b2c19614
BV
277/**
278 * Run the specified decoder function.
279 *
280 * @param dec TODO
281 * @param inbuf TODO
282 * @param inbuflen TODO
b2c19614
BV
283 *
284 * @return SRD_OK upon success, a (negative) error code otherwise.
285 */
7ce7775c 286int srd_instance_decode(uint64_t timeoffset, uint64_t duration,
bc5f5a43 287 struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen)
b2c19614
BV
288{
289 PyObject *py_instance, *py_res;
bc5f5a43 290 srd_logic *logic;
b2c19614
BV
291
292 /* Return an error upon unusable input. */
bc5f5a43 293 if (di == NULL)
a62186e4 294 return SRD_ERR_ARG; /* TODO: More specific error? */
b2c19614 295 if (inbuf == NULL)
a62186e4 296 return SRD_ERR_ARG; /* TODO: More specific error? */
b2c19614 297 if (inbuflen == 0) /* No point in working on empty buffers. */
a62186e4 298 return SRD_ERR_ARG; /* TODO: More specific error? */
b2c19614
BV
299
300 /* TODO: Error handling. */
bc5f5a43 301 py_instance = di->py_instance;
b2c19614
BV
302 Py_XINCREF(py_instance);
303
bc5f5a43
BV
304 logic = PyObject_New(srd_logic, &srd_logic_type);
305 Py_INCREF(logic);
306 logic->di = di;
307 logic->itercnt = 0;
308 logic->inbuf = inbuf;
309 logic->inbuflen = inbuflen;
310 logic->sample = PyList_New(2);
311 Py_INCREF(logic->sample);
312
b2c19614 313 if (!(py_res = PyObject_CallMethod(py_instance, "decode",
bc5f5a43 314 "KKO", timeoffset, duration, logic))) {
b2c19614
BV
315 if (PyErr_Occurred())
316 PyErr_Print(); /* Returns void. */
317
318 return SRD_ERR_PYTHON; /* TODO: More specific error? */
319 }
320
321 Py_XDECREF(py_res);
bc5f5a43 322
b2c19614
BV
323 return SRD_OK;
324}
325
326
7ce7775c
BV
327int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
328{
329 PyObject *args;
330 GSList *d, *s;
331 struct srd_decoder_instance *di;
332 int ret;
333
334 if (!(args = Py_BuildValue("{s:l}", "samplerate", (long)samplerate))) {
335 srd_err("unable to build python object for metadata");
336 return SRD_ERR_PYTHON;
337 }
338
339 /* Run the start() method on all decoders receiving frontend data. */
340 for (d = di_list; d; d = d->next) {
341 di = d->data;
342 di->num_probes = num_probes;
343 di->unitsize = unitsize;
344 di->samplerate = samplerate;
345 if ((ret = srd_instance_start(di, args) != SRD_OK))
346 return ret;
347
348 /* Run the start() method on all decoders up the stack from this one. */
349 for (s = di->next_di; s; s = s->next) {
350 /* These don't need probes, unitsize and samplerate. */
351 di = s->data;
352 if ((ret = srd_instance_start(di, args) != SRD_OK))
353 return ret;
354 }
355 }
356
357 Py_DECREF(args);
358
359 return SRD_OK;
360}
361
b2c19614 362/* Feed logic samples to decoder session. */
1aef2f93
BV
363int srd_session_feed(uint64_t timeoffset, uint64_t duration, uint8_t *inbuf,
364 uint64_t inbuflen)
b2c19614
BV
365{
366 GSList *d;
367 int ret;
368
e5080882 369 for (d = di_list; d; d = d->next) {
7ce7775c 370 if ((ret = srd_instance_decode(timeoffset, duration, d->data, inbuf,
1aef2f93 371 inbuflen)) != SRD_OK)
b2c19614
BV
372 return ret;
373 }
374
375 return SRD_OK;
376}
377
378
f9a3947a 379int pd_add(struct srd_decoder_instance *di, int output_type,
94d43b37 380 char *proto_id)
e5080882 381{
e5080882 382 struct srd_pd_output *pdo;
e5080882
BV
383
384 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output))))
385 return -1;
386
b231546d 387 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
15969949 388 pdo->pdo_id = g_slist_length(di->pd_output);
e5080882 389 pdo->output_type = output_type;
15969949 390 pdo->decoder = di->decoder;
94d43b37 391 pdo->proto_id = g_strdup(proto_id);
e5080882
BV
392 di->pd_output = g_slist_append(di->pd_output, pdo);
393
15969949 394 return pdo->pdo_id;
e5080882
BV
395}
396
bc5f5a43
BV
397struct srd_decoder_instance *get_di_by_decobject(void *decobject)
398{
7ce7775c 399 GSList *l, *s;
bc5f5a43
BV
400 struct srd_decoder_instance *di;
401
402 for (l = di_list; l; l = l->next) {
403 di = l->data;
404 if (decobject == di->py_instance)
405 return di;
7ce7775c
BV
406 /* Check decoders stacked on top of this one. */
407 for (s = di->next_di; s; s = s->next) {
408 di = s->data;
409 if (decobject == di->py_instance)
410 return di;
411 }
bc5f5a43
BV
412 }
413
414 return NULL;
415}
416
983cb0f5
BV
417int srd_register_callback(int output_type, void *cb)
418{
419 struct srd_pd_callback *pd_cb;
420
421 if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
422 return SRD_ERR_MALLOC;
423
424 pd_cb->output_type = output_type;
425 pd_cb->callback = cb;
426 callbacks = g_slist_append(callbacks, pd_cb);
427
983cb0f5
BV
428 return SRD_OK;
429}
430
431void *srd_find_callback(int output_type)
432{
433 GSList *l;
434 struct srd_pd_callback *pd_cb;
435 void *(cb);
436
437 cb = NULL;
438 for (l = callbacks; l; l = l->next) {
439 pd_cb = l->data;
440 if (pd_cb->output_type == output_type) {
441 cb = pd_cb->callback;
442 break;
443 }
444 }
445
446 return cb;
447}
e5080882 448