]> sigrok.org Git - libsigrokdecode.git/blame - controller.c
code cleanup
[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
21#include "config.h"
73191416 22#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
b2c19614 23#include <glib.h>
1aef2f93 24#include <inttypes.h>
b2c19614 25
b2c19614 26
983cb0f5
BV
27static GSList *callbacks = NULL;
28
e5080882 29/* TODO
b2c19614 30static GSList *pipelines = NULL;
b2c19614
BV
31struct srd_pipeline {
32 int id;
33 GSList *decoders;
34};
bc5f5a43 35*/
b2c19614 36
bc5f5a43
BV
37/* lives in decoder.c */
38extern GSList *pd_list;
39extern GSList *di_list;
b2c19614 40
bc5f5a43
BV
41/* lives in module_sigrokdecode.c */
42extern PyMODINIT_FUNC PyInit_sigrokdecode(void);
b2c19614 43
bc5f5a43
BV
44/* lives in type_logic.c */
45extern PyTypeObject srd_logic_type;
b2c19614 46
e5080882 47
b2c19614
BV
48/**
49 * Initialize libsigrokdecode.
50 *
51 * This initializes the Python interpreter, and creates and initializes
52 * a "sigrok" Python module with a single put() method.
53 *
54 * Then, it searches for sigrok protocol decoder files (*.py) in the
55 * "decoders" subdirectory of the the sigrok installation directory.
56 * All decoders that are found are loaded into memory and added to an
57 * internal list of decoders, which can be queried via srd_list_decoders().
58 *
59 * The caller is responsible for calling the clean-up function srd_exit(),
60 * which will properly shut down libsigrokdecode and free its allocated memory.
61 *
62 * Multiple calls to srd_init(), without calling srd_exit() inbetween,
63 * are not allowed.
64 *
65 * @return SRD_OK upon success, a (negative) error code otherwise.
66 * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders
67 * directory cannot be accessed, return SRD_ERR_DECODERS_DIR.
68 * If not enough memory could be allocated, return SRD_ERR_MALLOC.
69 */
70int srd_init(void)
71{
72 int ret;
73
bc5f5a43 74 PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
5f802ec6 75
b2c19614
BV
76 /* Py_Initialize() returns void and usually cannot fail. */
77 Py_Initialize();
78
b2c19614
BV
79 if ((ret = set_modulepath()) != SRD_OK) {
80 Py_Finalize();
81 return ret;
82 }
83
84 if ((ret = srd_load_all_decoders()) != SRD_OK) {
85 Py_Finalize();
86 return ret;
87 }
88
89 return SRD_OK;
90}
91
92
93/**
94 * Shutdown libsigrokdecode.
95 *
96 * This frees all the memory allocated for protocol decoders and shuts down
97 * the Python interpreter.
98 *
99 * This function should only be called if there was a (successful!) invocation
100 * of srd_init() before. Calling this function multiple times in a row, without
101 * any successful srd_init() calls inbetween, is not allowed.
102 *
103 * @return SRD_OK upon success, a (negative) error code otherwise.
104 */
105int srd_exit(void)
106{
107 /* Unload/free all decoders, and then the list of decoders itself. */
108 /* TODO: Error handling. */
109 srd_unload_all_decoders();
e5080882 110 g_slist_free(pd_list);
b2c19614
BV
111
112 /* Py_Finalize() returns void, any finalization errors are ignored. */
113 Py_Finalize();
114
115 return SRD_OK;
116}
117
118
119/**
120 * Add search directories for the protocol decoders.
121 *
122 * TODO: add path from env var SIGROKDECODE_PATH, config etc
123 */
124int set_modulepath(void)
125{
126 int ret;
127
bc5f5a43 128 PyRun_SimpleString("import sys");
b2c19614
BV
129 ret = PyRun_SimpleString("sys.path.append(r'" DECODERS_DIR "');");
130
131 return ret;
132}
133
134
135struct srd_decoder_instance *srd_instance_new(const char *id)
136{
137 struct srd_decoder *dec;
138 struct srd_decoder_instance *di;
139 PyObject *py_args;
140
b2c19614
BV
141 if (!(dec = srd_get_decoder_by_id(id)))
142 return NULL;
143
144 /* TODO: Error handling. Use g_try_malloc(). */
145 di = g_malloc(sizeof(*di));
146 di->decoder = dec;
147 di->pd_output = NULL;
bc5f5a43 148 di->unitsize = 0;
b2c19614
BV
149
150 /* Create an empty Python tuple. */
151 if (!(py_args = PyTuple_New(0))) { /* NEWREF */
152 if (PyErr_Occurred())
153 PyErr_Print(); /* Returns void. */
154
155 return NULL; /* TODO: More specific error? */
156 }
157
158 /* Create an instance of the 'Decoder' class. */
159 di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL);
160 if (!di->py_instance) {
161 if (PyErr_Occurred())
162 PyErr_Print(); /* Returns void. */
163 Py_XDECREF(py_args);
164 return NULL; /* TODO: More specific error? */
165 }
e5080882 166 di_list = g_slist_append(di_list, di);
b2c19614
BV
167
168 Py_XDECREF(py_args);
169
170 return di;
171}
172
173
174int srd_instance_set_probe(struct srd_decoder_instance *di,
175 const char *probename, int num)
176{
177 PyObject *probedict, *probenum;
178
179 probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */
180 if (!probedict) {
181 if (PyErr_Occurred())
182 PyErr_Print(); /* Returns void. */
183
184 return SRD_ERR_PYTHON; /* TODO: More specific error? */
185 }
186
5f802ec6 187 probenum = PyLong_FromLong(num);
b2c19614
BV
188 PyMapping_SetItemString(probedict, (char *)probename, probenum);
189
190 Py_XDECREF(probenum);
191 Py_XDECREF(probedict);
192
193 return SRD_OK;
194}
195
196
bc5f5a43 197int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
b2c19614
BV
198{
199 PyObject *py_res;
200 GSList *d;
201 struct srd_decoder_instance *di;
202
e5080882 203 for (d = di_list; d; d = d->next) {
b2c19614 204 di = d->data;
bc5f5a43
BV
205 di->num_probes = num_probes;
206 di->unitsize = unitsize;
207 di->samplerate = samplerate;
b2c19614 208 if (!(py_res = PyObject_CallMethod(di->py_instance, "start",
bc5f5a43 209 "{s:l}",
6547acfd 210 "samplerate", (long)samplerate))) {
b2c19614
BV
211 if (PyErr_Occurred())
212 PyErr_Print(); /* Returns void. */
213
214 return SRD_ERR_PYTHON; /* TODO: More specific error? */
215 }
216 Py_XDECREF(py_res);
217 }
218
219 return SRD_OK;
220}
221
222
223/**
224 * Run the specified decoder function.
225 *
226 * @param dec TODO
227 * @param inbuf TODO
228 * @param inbuflen TODO
b2c19614
BV
229 *
230 * @return SRD_OK upon success, a (negative) error code otherwise.
231 */
1aef2f93 232int srd_run_decoder(uint64_t timeoffset, uint64_t duration,
bc5f5a43 233 struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen)
b2c19614
BV
234{
235 PyObject *py_instance, *py_res;
bc5f5a43 236 srd_logic *logic;
b2c19614
BV
237
238 /* Return an error upon unusable input. */
bc5f5a43 239 if (di == NULL)
a62186e4 240 return SRD_ERR_ARG; /* TODO: More specific error? */
b2c19614 241 if (inbuf == NULL)
a62186e4 242 return SRD_ERR_ARG; /* TODO: More specific error? */
b2c19614 243 if (inbuflen == 0) /* No point in working on empty buffers. */
a62186e4 244 return SRD_ERR_ARG; /* TODO: More specific error? */
b2c19614
BV
245
246 /* TODO: Error handling. */
bc5f5a43 247 py_instance = di->py_instance;
b2c19614
BV
248 Py_XINCREF(py_instance);
249
bc5f5a43
BV
250 logic = PyObject_New(srd_logic, &srd_logic_type);
251 Py_INCREF(logic);
252 logic->di = di;
253 logic->itercnt = 0;
254 logic->inbuf = inbuf;
255 logic->inbuflen = inbuflen;
256 logic->sample = PyList_New(2);
257 Py_INCREF(logic->sample);
258
b2c19614 259 if (!(py_res = PyObject_CallMethod(py_instance, "decode",
bc5f5a43 260 "KKO", timeoffset, duration, logic))) {
b2c19614
BV
261 if (PyErr_Occurred())
262 PyErr_Print(); /* Returns void. */
263
264 return SRD_ERR_PYTHON; /* TODO: More specific error? */
265 }
266
267 Py_XDECREF(py_res);
bc5f5a43 268
b2c19614
BV
269 return SRD_OK;
270}
271
272
273/* Feed logic samples to decoder session. */
1aef2f93
BV
274int srd_session_feed(uint64_t timeoffset, uint64_t duration, uint8_t *inbuf,
275 uint64_t inbuflen)
b2c19614
BV
276{
277 GSList *d;
278 int ret;
279
e5080882 280 for (d = di_list; d; d = d->next) {
1aef2f93
BV
281 if ((ret = srd_run_decoder(timeoffset, duration, d->data, inbuf,
282 inbuflen)) != SRD_OK)
b2c19614
BV
283 return ret;
284 }
285
286 return SRD_OK;
287}
288
289
f9a3947a 290int pd_add(struct srd_decoder_instance *di, int output_type,
15969949 291 char *protocol_id)
e5080882 292{
e5080882 293 struct srd_pd_output *pdo;
e5080882
BV
294
295 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output))))
296 return -1;
297
b231546d 298 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
15969949 299 pdo->pdo_id = g_slist_length(di->pd_output);
e5080882 300 pdo->output_type = output_type;
15969949 301 pdo->decoder = di->decoder;
e5080882 302 pdo->protocol_id = g_strdup(protocol_id);
e5080882
BV
303 di->pd_output = g_slist_append(di->pd_output, pdo);
304
15969949 305 return pdo->pdo_id;
e5080882
BV
306}
307
bc5f5a43
BV
308struct srd_decoder_instance *get_di_by_decobject(void *decobject)
309{
310 GSList *l;
311 struct srd_decoder_instance *di;
312
313 for (l = di_list; l; l = l->next) {
314 di = l->data;
315 if (decobject == di->py_instance)
316 return di;
317 }
318
319 return NULL;
320}
321
983cb0f5
BV
322int srd_register_callback(int output_type, void *cb)
323{
324 struct srd_pd_callback *pd_cb;
325
326 if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
327 return SRD_ERR_MALLOC;
328
329 pd_cb->output_type = output_type;
330 pd_cb->callback = cb;
331 callbacks = g_slist_append(callbacks, pd_cb);
332
983cb0f5
BV
333 return SRD_OK;
334}
335
336void *srd_find_callback(int output_type)
337{
338 GSList *l;
339 struct srd_pd_callback *pd_cb;
340 void *(cb);
341
342 cb = NULL;
343 for (l = callbacks; l; l = l->next) {
344 pd_cb = l->data;
345 if (pd_cb->output_type == output_type) {
346 cb = pd_cb->callback;
347 break;
348 }
349 }
350
351 return cb;
352}
e5080882 353
b2c19614
BV
354//int srd_pipeline_new(int plid)
355//{
356//
357//
358//}
b2c19614
BV
359
360
361
362