]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decode.c
Don't expose libsigrokdecode's interal GSList.
[libsigrokdecode.git] / decode.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include <stdio.h>
23#include <string.h>
24#include <dirent.h>
25
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/* The list of protocol decoders. */
34GSList *list_pds = NULL;
35
36/**
37 * Initialize libsigrokdecode.
38 *
39 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
40 */
41int sigrokdecode_init(void)
42{
43 DIR *dir;
44 struct dirent *dp;
45 char *tmp;
46
47 /* Py_Initialize() returns void and usually cannot fail. */
48 Py_Initialize();
49
50 /* Add some more search directories for convenience. */
51 /* FIXME: Check error code. */
52 /* FIXME: What happens if this function is called multiple times? */
53 PyRun_SimpleString(
54 "import sys;"
55 "sys.path.append('libsigrokdecode/decoders');"
56 "sys.path.append('" DECODERS_DIR "');"
57 );
58
59 if (!(dir = opendir(DECODERS_DIR)))
60 return SIGROKDECODE_ERR_DECODERS_DIR;
61
62 while ((dp = readdir(dir)) != NULL) {
63 if (!strstr(dp->d_name, ".py"))
64 continue;
65 if ((tmp = strdup(dp->d_name)))
66 list_pds = g_slist_append(list_pds, tmp);
67 }
68 closedir(dir);
69
70 return SIGROKDECODE_OK;
71}
72
73/**
74 * Returns the list of supported/loaded protocol decoders.
75 *
76 * This is a GSList containing the names of the decoders as strings.
77 *
78 * @return List of decoders, NULL if none are supported or loaded.
79 */
80GSList *sigrokdecode_list_decoders(void)
81{
82 return list_pds;
83}
84
85/**
86 * Helper function to handle Python strings.
87 *
88 * TODO: @param entries.
89 *
90 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
91 * The 'outstr' argument points to a malloc()ed string upon success.
92 */
93static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod,
94 const char *key, char **outstr)
95{
96 PyObject *py_str;
97 char *str;
98
99 py_str = PyMapping_GetItemString(py_res, (char *)key);
100 if (!py_str || !PyString_Check(py_str)) {
101 if (PyErr_Occurred())
102 PyErr_Print();
103 Py_DECREF(py_func);
104 Py_DECREF(py_mod);
105 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
106 }
107
108 if (!(str = PyString_AsString(py_str))) {
109 if (PyErr_Occurred())
110 PyErr_Print();
111 Py_DECREF(py_str);
112 Py_DECREF(py_func);
113 Py_DECREF(py_mod);
114 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
115 }
116
117 if (!(*outstr = strdup(str))) {
118 if (PyErr_Occurred())
119 PyErr_Print();
120 Py_DECREF(py_str);
121 Py_DECREF(py_func);
122 Py_DECREF(py_mod);
123 return SIGROKDECODE_ERR_MALLOC;
124 }
125
126 Py_DECREF(py_str);
127
128 return SIGROKDECODE_OK;
129}
130
131/**
132 * TODO
133 *
134 * @param name TODO
135 *
136 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
137 */
138int sigrokdecode_load_decoder(const char *name,
139 struct sigrokdecode_decoder **dec)
140{
141 struct sigrokdecode_decoder *d;
142 PyObject *py_name, *py_mod, *py_func, *py_res /* , *py_tuple */;
143 int r;
144
145 /* Get the name of the decoder module as Python string. */
146 if (!(py_name = PyString_FromString(name))) {
147 PyErr_Print();
148 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
149 }
150
151 /* "Import" the Python module. */
152 if (!(py_mod = PyImport_Import(py_name))) {
153 PyErr_Print();
154 Py_DECREF(py_name);
155 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
156 }
157 Py_DECREF(py_name);
158
159 /* Get the 'register' function name as Python callable object. */
160 py_func = PyObject_GetAttrString(py_mod, "register");
161 if (!py_func || !PyCallable_Check(py_func)) {
162 if (PyErr_Occurred())
163 PyErr_Print();
164 Py_DECREF(py_mod);
165 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
166 }
167
168 /* Call the 'register' function without arguments, get the result. */
169 if (!(py_res = PyObject_CallFunction(py_func, NULL))) {
170 PyErr_Print();
171 Py_DECREF(py_func);
172 Py_DECREF(py_mod);
173 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
174 }
175
176 if (!(d = malloc(sizeof(struct sigrokdecode_decoder))))
177 return SIGROKDECODE_ERR_MALLOC;
178
179 if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0)
180 return r;
181
182 if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0)
183 return r;
184
185 if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0)
186 return r;
187
188 d->py_mod = py_mod;
189
190 Py_DECREF(py_res);
191 Py_DECREF(py_func);
192
193 /* Get the 'decode' function name as Python callable object. */
194 py_func = PyObject_GetAttrString(py_mod, "decode");
195 if (!py_func || !PyCallable_Check(py_func)) {
196 if (PyErr_Occurred())
197 PyErr_Print();
198 Py_DECREF(py_mod);
199 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
200 }
201
202 d->py_func = py_func;
203
204 /* TODO: Handle inputformats, outputformats. */
205
206 *dec = d;
207
208 return SIGROKDECODE_OK;
209}
210
211/**
212 * Run the specified decoder function.
213 *
214 * @param dec TODO
215 * @param inbuf TODO
216 * @param inbuflen TODO
217 * @param outbuf TODO
218 * @param outbuflen TODO
219 *
220 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
221 */
222int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
223 uint8_t *inbuf, uint64_t inbuflen,
224 uint8_t **outbuf, uint64_t *outbuflen)
225{
226 PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
227 int ret;
228
229 /* TODO: Use #defines for the return codes. */
230
231 /* Return an error upon unusable input. */
232 if (dec == NULL)
233 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
234 if (inbuf == NULL)
235 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
236 if (inbuflen == 0) /* No point in working on empty buffers. */
237 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
238 if (outbuf == NULL)
239 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
240 if (outbuflen == NULL)
241 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
242
243 /* TODO: Error handling. */
244 py_mod = dec->py_mod;
245 py_func = dec->py_func;
246
247 /* TODO: Really run Py_DECREF on py_mod/py_func? */
248
249 /* Create a Python tuple of size 1. */
250 if (!(py_args = PyTuple_New(1))) {
251 PyErr_Print();
252 Py_DECREF(py_func);
253 Py_DECREF(py_mod);
254 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
255 }
256
257 /* Get the input buffer as Python "string" (byte array). */
258 /* TODO: int vs. uint64_t for 'inbuflen'? */
259 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
260 PyErr_Print();
261 Py_DECREF(py_args);
262 Py_DECREF(py_func);
263 Py_DECREF(py_mod);
264 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
265 }
266
267 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
268 PyErr_Print();
269 Py_DECREF(py_value);
270 Py_DECREF(py_args);
271 Py_DECREF(py_func);
272 Py_DECREF(py_mod);
273 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
274 }
275
276 if (!(py_res = PyObject_CallObject(py_func, py_args))) {
277 PyErr_Print();
278 Py_DECREF(py_value);
279 Py_DECREF(py_args);
280 Py_DECREF(py_func);
281 Py_DECREF(py_mod);
282 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
283 }
284
285 if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
286 (Py_ssize_t *)outbuflen))) {
287 PyErr_Print();
288 Py_DECREF(py_res);
289 Py_DECREF(py_value);
290 Py_DECREF(py_args);
291 Py_DECREF(py_func);
292 Py_DECREF(py_mod);
293 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
294 }
295
296 Py_DECREF(py_res);
297 // Py_DECREF(py_value);
298 Py_DECREF(py_args);
299 Py_DECREF(py_func);
300 Py_DECREF(py_mod);
301
302 return SIGROKDECODE_OK;
303}
304
305/**
306 * Shutdown libsigrokdecode.
307 *
308 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
309 */
310int sigrokdecode_shutdown(void)
311{
312 /* Py_Finalize() returns void, any finalization errors are ignored. */
313 Py_Finalize();
314
315 return SIGROKDECODE_OK;
316}