blob: f7ffb2567ca9cee480d8047bcc4daafd45947b8f [file] [log] [blame]
Bert Vermeulenb2c19612011-12-04 10:33:02 +01001/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +01005 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
Bert Vermeulenb2c19612011-12-04 10:33:02 +01006 *
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"
Uwe Hermann73191412012-01-03 19:56:01 +010022#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
Bert Vermeulenb2c19612011-12-04 10:33:02 +010023#include <glib.h>
Bert Vermeulen1aef2f92011-12-15 03:31:31 +010024#include <inttypes.h>
Bert Vermeulenb2c19612011-12-04 10:33:02 +010025
Bert Vermeulenb2c19612011-12-04 10:33:02 +010026
Bert Vermeulen983cb0f2012-01-08 03:20:12 +010027static GSList *callbacks = NULL;
28
Bert Vermeulene5080882011-12-07 09:56:49 +010029/* TODO
Bert Vermeulenb2c19612011-12-04 10:33:02 +010030static GSList *pipelines = NULL;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010031struct srd_pipeline {
32 int id;
33 GSList *decoders;
34};
Bert Vermeulene5080882011-12-07 09:56:49 +010035*/
Bert Vermeulenb2c19612011-12-04 10:33:02 +010036
37/* lives in decoder.c */
Bert Vermeulene5080882011-12-07 09:56:49 +010038extern GSList *pd_list;
39extern GSList *di_list;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010040
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010041/* lives in module_sigrokdecode.c */
42extern PyMODINIT_FUNC PyInit_sigrokdecode(void);
Bert Vermeulenb2c19612011-12-04 10:33:02 +010043
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010044/* lives in type_logic.c */
45extern PyTypeObject srd_logic_type;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010046
47
Bert Vermeulenb2c19612011-12-04 10:33:02 +010048/**
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
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010074 PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
Bert Vermeulen5f802ec2011-12-27 22:15:53 +010075
Bert Vermeulenb2c19612011-12-04 10:33:02 +010076 /* Py_Initialize() returns void and usually cannot fail. */
77 Py_Initialize();
78
Bert Vermeulenb2c19612011-12-04 10:33:02 +010079 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();
Bert Vermeulene5080882011-12-07 09:56:49 +0100110 g_slist_free(pd_list);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100111
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
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100128 PyRun_SimpleString("import sys");
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100129 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
141 fprintf(stdout, "%s: %s\n", __func__, id);
142
143 if (!(dec = srd_get_decoder_by_id(id)))
144 return NULL;
145
146 /* TODO: Error handling. Use g_try_malloc(). */
147 di = g_malloc(sizeof(*di));
148 di->decoder = dec;
149 di->pd_output = NULL;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100150 di->unitsize = 0;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100151
152 /* Create an empty Python tuple. */
153 if (!(py_args = PyTuple_New(0))) { /* NEWREF */
154 if (PyErr_Occurred())
155 PyErr_Print(); /* Returns void. */
156
157 return NULL; /* TODO: More specific error? */
158 }
159
160 /* Create an instance of the 'Decoder' class. */
161 di->py_instance = PyObject_Call(dec->py_decobj, py_args, NULL);
162 if (!di->py_instance) {
163 if (PyErr_Occurred())
164 PyErr_Print(); /* Returns void. */
165 Py_XDECREF(py_args);
166 return NULL; /* TODO: More specific error? */
167 }
Bert Vermeulene5080882011-12-07 09:56:49 +0100168 di_list = g_slist_append(di_list, di);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100169
170 Py_XDECREF(py_args);
171
172 return di;
173}
174
175
176int srd_instance_set_probe(struct srd_decoder_instance *di,
177 const char *probename, int num)
178{
179 PyObject *probedict, *probenum;
180
181 probedict = PyObject_GetAttrString(di->py_instance, "probes"); /* NEWREF */
182 if (!probedict) {
183 if (PyErr_Occurred())
184 PyErr_Print(); /* Returns void. */
185
186 return SRD_ERR_PYTHON; /* TODO: More specific error? */
187 }
188
Bert Vermeulen5f802ec2011-12-27 22:15:53 +0100189 probenum = PyLong_FromLong(num);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100190 PyMapping_SetItemString(probedict, (char *)probename, probenum);
191
192 Py_XDECREF(probenum);
193 Py_XDECREF(probedict);
194
195 return SRD_OK;
196}
197
198
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100199int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100200{
201 PyObject *py_res;
202 GSList *d;
203 struct srd_decoder_instance *di;
204
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100205 fprintf(stdout, "%s\n", __func__);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100206
Bert Vermeulene5080882011-12-07 09:56:49 +0100207 for (d = di_list; d; d = d->next) {
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100208 di = d->data;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100209 di->num_probes = num_probes;
210 di->unitsize = unitsize;
211 di->samplerate = samplerate;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100212 if (!(py_res = PyObject_CallMethod(di->py_instance, "start",
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100213 "{s:l}",
Gareth McMullin6547acf2011-12-05 20:31:32 +1300214 "samplerate", (long)samplerate))) {
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100215 if (PyErr_Occurred())
216 PyErr_Print(); /* Returns void. */
217
218 return SRD_ERR_PYTHON; /* TODO: More specific error? */
219 }
220 Py_XDECREF(py_res);
221 }
222
223 return SRD_OK;
224}
225
226
227/**
228 * Run the specified decoder function.
229 *
230 * @param dec TODO
231 * @param inbuf TODO
232 * @param inbuflen TODO
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100233 *
234 * @return SRD_OK upon success, a (negative) error code otherwise.
235 */
Bert Vermeulen1aef2f92011-12-15 03:31:31 +0100236int srd_run_decoder(uint64_t timeoffset, uint64_t duration,
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100237 struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100238{
239 PyObject *py_instance, *py_res;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100240 srd_logic *logic;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100241
242 /* Return an error upon unusable input. */
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100243 if (di == NULL)
Uwe Hermanna62186e2011-12-21 19:09:46 +0100244 return SRD_ERR_ARG; /* TODO: More specific error? */
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100245 if (inbuf == NULL)
Uwe Hermanna62186e2011-12-21 19:09:46 +0100246 return SRD_ERR_ARG; /* TODO: More specific error? */
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100247 if (inbuflen == 0) /* No point in working on empty buffers. */
Uwe Hermanna62186e2011-12-21 19:09:46 +0100248 return SRD_ERR_ARG; /* TODO: More specific error? */
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100249
250 /* TODO: Error handling. */
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100251 py_instance = di->py_instance;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100252 Py_XINCREF(py_instance);
253
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100254 logic = PyObject_New(srd_logic, &srd_logic_type);
255 Py_INCREF(logic);
256 logic->di = di;
257 logic->itercnt = 0;
258 logic->inbuf = inbuf;
259 logic->inbuflen = inbuflen;
260 logic->sample = PyList_New(2);
261 Py_INCREF(logic->sample);
262
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100263 if (!(py_res = PyObject_CallMethod(py_instance, "decode",
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100264 "KKO", timeoffset, duration, logic))) {
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100265 if (PyErr_Occurred())
266 PyErr_Print(); /* Returns void. */
267
268 return SRD_ERR_PYTHON; /* TODO: More specific error? */
269 }
270
271 Py_XDECREF(py_res);
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100272
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100273 return SRD_OK;
274}
275
276
277/* Feed logic samples to decoder session. */
Bert Vermeulen1aef2f92011-12-15 03:31:31 +0100278int srd_session_feed(uint64_t timeoffset, uint64_t duration, uint8_t *inbuf,
279 uint64_t inbuflen)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100280{
281 GSList *d;
282 int ret;
283
284// fprintf(stdout, "%s: %d bytes\n", __func__, inbuflen);
285
Bert Vermeulene5080882011-12-07 09:56:49 +0100286 for (d = di_list; d; d = d->next) {
Bert Vermeulen1aef2f92011-12-15 03:31:31 +0100287 if ((ret = srd_run_decoder(timeoffset, duration, d->data, inbuf,
288 inbuflen)) != SRD_OK)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100289 return ret;
290 }
291
292 return SRD_OK;
293}
294
295
Bert Vermeulene5080882011-12-07 09:56:49 +0100296int pd_output_new(struct srd_decoder_instance *di, int output_type,
Bert Vermeulen15969942012-01-07 02:50:14 +0100297 char *protocol_id)
Bert Vermeulene5080882011-12-07 09:56:49 +0100298{
Bert Vermeulene5080882011-12-07 09:56:49 +0100299 struct srd_pd_output *pdo;
Bert Vermeulene5080882011-12-07 09:56:49 +0100300
301 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output))))
302 return -1;
303
Bert Vermeulen15969942012-01-07 02:50:14 +0100304 /* pdo_id is just a simple index, nothing is deleted from this list anway */
305 pdo->pdo_id = g_slist_length(di->pd_output);
Bert Vermeulene5080882011-12-07 09:56:49 +0100306 pdo->output_type = output_type;
Bert Vermeulen15969942012-01-07 02:50:14 +0100307 pdo->decoder = di->decoder;
Bert Vermeulene5080882011-12-07 09:56:49 +0100308 pdo->protocol_id = g_strdup(protocol_id);
Bert Vermeulene5080882011-12-07 09:56:49 +0100309 di->pd_output = g_slist_append(di->pd_output, pdo);
310
Bert Vermeulen15969942012-01-07 02:50:14 +0100311 fprintf(stdout, "%s: output type %d, protocol_id %s, id %d\n",
312 __func__, output_type, protocol_id, pdo->pdo_id);
313
314 return pdo->pdo_id;
Bert Vermeulene5080882011-12-07 09:56:49 +0100315}
316
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100317struct srd_decoder_instance *get_di_by_decobject(void *decobject)
318{
319 GSList *l;
320 struct srd_decoder_instance *di;
321
322 for (l = di_list; l; l = l->next) {
323 di = l->data;
324 if (decobject == di->py_instance)
325 return di;
326 }
327
328 return NULL;
329}
330
Bert Vermeulen983cb0f2012-01-08 03:20:12 +0100331int srd_register_callback(int output_type, void *cb)
332{
333 struct srd_pd_callback *pd_cb;
334
335 if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
336 return SRD_ERR_MALLOC;
337
338 pd_cb->output_type = output_type;
339 pd_cb->callback = cb;
340 callbacks = g_slist_append(callbacks, pd_cb);
341
342 printf("got cb for %d: %p\n", output_type, cb);
343 return SRD_OK;
344}
345
346void *srd_find_callback(int output_type)
347{
348 GSList *l;
349 struct srd_pd_callback *pd_cb;
350 void *(cb);
351
352 cb = NULL;
353 for (l = callbacks; l; l = l->next) {
354 pd_cb = l->data;
355 if (pd_cb->output_type == output_type) {
356 cb = pd_cb->callback;
357 break;
358 }
359 }
360
361 return cb;
362}
Bert Vermeulene5080882011-12-07 09:56:49 +0100363
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100364//int srd_pipeline_new(int plid)
365//{
366//
367//
368//}
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100369
370
371
372