]> sigrok.org Git - libsigrokdecode.git/blame - decode.c
Fix some compiler warnings.
[libsigrokdecode.git] / decode.c
CommitLineData
1c6fa20f
UH
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>
5c55017c 23#include <string.h>
7c24d086 24#include <dirent.h>
d842aebe 25#include <config.h>
1c6fa20f 26
e6c7a826
UH
27/* Re-define some string functions for Python >= 3.0. */
28#if PY_VERSION_HEX >= 0x03000000
29#define PyString_AsString PyBytes_AsString
30#define PyString_FromString PyBytes_FromString
31#define PyString_Check PyBytes_Check
32#endif
33
7c24d086 34/* The list of protocol decoders. */
871d21e2 35GSList *list_pds = NULL;
7c24d086 36
f800adc4
UH
37/*
38 * Here's a quick overview of Python/C API reference counting.
39 *
40 * Check the Python/C API docs for what type of reference a function returns.
41 *
2557ad9d 42 * - If it returns a "new reference", you're responsible to Py_XDECREF() it.
f800adc4 43 *
2557ad9d 44 * - If it returns a "borrowed reference", you MUST NOT Py_XDECREF() it.
f800adc4
UH
45 *
46 * - If a function "steals" a reference, you no longer are responsible for
2557ad9d 47 * Py_XDECREF()ing it (someone else will do it for you at some point).
f800adc4
UH
48 */
49
1c6fa20f
UH
50/**
51 * Initialize libsigrokdecode.
52 *
70e44845 53 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f
UH
54 */
55int sigrokdecode_init(void)
56{
7c24d086
UH
57 DIR *dir;
58 struct dirent *dp;
59 char *tmp;
60
1c6fa20f
UH
61 /* Py_Initialize() returns void and usually cannot fail. */
62 Py_Initialize();
63
0895f56a 64 /* Add search directory for the protocol decoders. */
2454527d 65 /* FIXME: Check error code. */
70e44845 66 /* FIXME: What happens if this function is called multiple times? */
0895f56a 67 PyRun_SimpleString("import sys;"
10426068 68 "sys.path.append(r'" DECODERS_DIR "');");
1c6fa20f 69
7c24d086
UH
70 if (!(dir = opendir(DECODERS_DIR)))
71 return SIGROKDECODE_ERR_DECODERS_DIR;
72
73 while ((dp = readdir(dir)) != NULL) {
276b55eb 74 if (!g_str_has_suffix(dp->d_name, ".py"))
7c24d086 75 continue;
5b05904d
UH
76 /* For now use the filename (without .py) as decoder name. */
77 if ((tmp = g_strndup(dp->d_name, strlen(dp->d_name) - 3)))
7c24d086
UH
78 list_pds = g_slist_append(list_pds, tmp);
79 }
80 closedir(dir);
81
70e44845 82 return SIGROKDECODE_OK;
1c6fa20f
UH
83}
84
871d21e2
UH
85/**
86 * Returns the list of supported/loaded protocol decoders.
87 *
88 * This is a GSList containing the names of the decoders as strings.
89 *
90 * @return List of decoders, NULL if none are supported or loaded.
91 */
92GSList *sigrokdecode_list_decoders(void)
93{
94 return list_pds;
95}
96
5c55017c
UH
97/**
98 * Helper function to handle Python strings.
99 *
100 * TODO: @param entries.
101 *
3a9a7e38 102 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
70e44845 103 * The 'outstr' argument points to a malloc()ed string upon success.
5c55017c
UH
104 */
105static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod,
106 const char *key, char **outstr)
107{
108 PyObject *py_str;
109 char *str;
f7313eea 110 int ret;
5c55017c
UH
111
112 py_str = PyMapping_GetItemString(py_res, (char *)key);
113 if (!py_str || !PyString_Check(py_str)) {
f7313eea
UH
114 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
115 goto err_h_decref_func;
5c55017c
UH
116 }
117
6b08da24
UH
118 /*
119 * PyString_AsString()'s returned string refers to an internal buffer
120 * (not a copy), i.e. the data must not be modified, and the memory
121 * must not be free()'d.
122 */
5c55017c 123 if (!(str = PyString_AsString(py_str))) {
f7313eea
UH
124 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
125 goto err_h_decref_str;
5c55017c
UH
126 }
127
128 if (!(*outstr = strdup(str))) {
f7313eea
UH
129 ret = SIGROKDECODE_ERR_MALLOC;
130 goto err_h_decref_str;
5c55017c
UH
131 }
132
2557ad9d 133 Py_XDECREF(py_str);
5c55017c 134
70e44845 135 return SIGROKDECODE_OK;
f7313eea
UH
136
137err_h_decref_str:
138 Py_XDECREF(py_str);
139err_h_decref_func:
140 Py_XDECREF(py_func);
f7313eea
UH
141 Py_XDECREF(py_mod);
142
143 if (PyErr_Occurred())
144 PyErr_Print(); /* Returns void. */
145
146 return ret;
5c55017c
UH
147}
148
1c6fa20f
UH
149/**
150 * TODO
151 *
152 * @param name TODO
70e44845 153 *
3a9a7e38 154 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f 155 */
5c55017c
UH
156int sigrokdecode_load_decoder(const char *name,
157 struct sigrokdecode_decoder **dec)
1c6fa20f 158{
5c55017c 159 struct sigrokdecode_decoder *d;
c7bb5dff 160 PyObject *py_mod, *py_func, *py_res /* , *py_tuple */;
5c55017c
UH
161 int r;
162
5c55017c 163 /* "Import" the Python module. */
c7bb5dff 164 if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */
6b08da24 165 PyErr_Print(); /* Returns void. */
5c55017c
UH
166 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
167 }
5c55017c
UH
168
169 /* Get the 'register' function name as Python callable object. */
6b08da24 170 py_func = PyObject_GetAttrString(py_mod, "register"); /* NEWREF */
5c55017c
UH
171 if (!py_func || !PyCallable_Check(py_func)) {
172 if (PyErr_Occurred())
6b08da24 173 PyErr_Print(); /* Returns void. */
2557ad9d 174 Py_XDECREF(py_mod);
5c55017c
UH
175 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
176 }
177
178 /* Call the 'register' function without arguments, get the result. */
6b08da24
UH
179 if (!(py_res = PyObject_CallFunction(py_func, NULL))) { /* NEWREF */
180 PyErr_Print(); /* Returns void. */
2557ad9d
UH
181 Py_XDECREF(py_func);
182 Py_XDECREF(py_mod);
5c55017c
UH
183 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
184 }
185
186 if (!(d = malloc(sizeof(struct sigrokdecode_decoder))))
187 return SIGROKDECODE_ERR_MALLOC;
188
189 if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0)
190 return r;
191
192 if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0)
193 return r;
194
195 if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0)
196 return r;
197
198 d->py_mod = py_mod;
199
2557ad9d
UH
200 Py_XDECREF(py_res);
201 Py_XDECREF(py_func);
5c55017c
UH
202
203 /* Get the 'decode' function name as Python callable object. */
6b08da24 204 py_func = PyObject_GetAttrString(py_mod, "decode"); /* NEWREF */
5c55017c
UH
205 if (!py_func || !PyCallable_Check(py_func)) {
206 if (PyErr_Occurred())
6b08da24 207 PyErr_Print(); /* Returns void. */
2557ad9d 208 Py_XDECREF(py_mod);
5c55017c
UH
209 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
210 }
211
212 d->py_func = py_func;
213
214 /* TODO: Handle inputformats, outputformats. */
215
216 *dec = d;
910e5fd8 217
70e44845 218 return SIGROKDECODE_OK;
1c6fa20f
UH
219}
220
221/**
222 * Run the specified decoder function.
223 *
5c55017c 224 * @param dec TODO
1c6fa20f
UH
225 * @param inbuf TODO
226 * @param inbuflen TODO
227 * @param outbuf TODO
228 * @param outbuflen TODO
70e44845 229 *
3a9a7e38 230 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f 231 */
5c55017c 232int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
2b45dd71 233 uint8_t *inbuf, uint64_t inbuflen,
234 uint8_t **outbuf, uint64_t *outbuflen)
1c6fa20f 235{
5c55017c 236 PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
036e6f67 237 int r, ret;
1c6fa20f
UH
238
239 /* TODO: Use #defines for the return codes. */
240
241 /* Return an error upon unusable input. */
5c55017c
UH
242 if (dec == NULL)
243 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 244 if (inbuf == NULL)
5c55017c 245 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 246 if (inbuflen == 0) /* No point in working on empty buffers. */
5c55017c 247 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 248 if (outbuf == NULL)
5c55017c 249 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 250 if (outbuflen == NULL)
5c55017c 251 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 252
5c55017c
UH
253 /* TODO: Error handling. */
254 py_mod = dec->py_mod;
2557ad9d 255 Py_XINCREF(py_mod);
5c55017c 256 py_func = dec->py_func;
2557ad9d 257 Py_XINCREF(py_func);
1c6fa20f
UH
258
259 /* Create a Python tuple of size 1. */
6b08da24 260 if (!(py_args = PyTuple_New(1))) { /* NEWREF */
036e6f67
UH
261 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
262 goto err_run_decref_func;
1c6fa20f
UH
263 }
264
265 /* Get the input buffer as Python "string" (byte array). */
266 /* TODO: int vs. uint64_t for 'inbuflen'? */
6b08da24 267 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) { /* NEWREF */
036e6f67
UH
268 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
269 goto err_run_decref_args;
1c6fa20f
UH
270 }
271
f800adc4
UH
272 /*
273 * IMPORTANT: PyTuple_SetItem() "steals" a reference to py_value!
2557ad9d 274 * That means we are no longer responsible for Py_XDECREF()'ing it.
f800adc4
UH
275 * It will automatically be free'd when the 'py_args' tuple is free'd.
276 */
6b08da24 277 if (PyTuple_SetItem(py_args, 0, py_value) != 0) { /* STEAL */
036e6f67 278 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
2557ad9d 279 Py_XDECREF(py_value); /* TODO: Ref. stolen upon error? */
036e6f67 280 goto err_run_decref_args;
1c6fa20f
UH
281 }
282
6b08da24 283 if (!(py_res = PyObject_CallObject(py_func, py_args))) { /* NEWREF */
036e6f67
UH
284 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
285 goto err_run_decref_args;
1c6fa20f
UH
286 }
287
036e6f67
UH
288 if ((r = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
289 (Py_ssize_t *)outbuflen))) {
290 ret = SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
2557ad9d 291 Py_XDECREF(py_res);
036e6f67 292 goto err_run_decref_args;
1c6fa20f
UH
293 }
294
036e6f67
UH
295 ret = SIGROKDECODE_OK;
296
2557ad9d 297 Py_XDECREF(py_res);
036e6f67
UH
298
299err_run_decref_args:
2557ad9d 300 Py_XDECREF(py_args);
036e6f67 301err_run_decref_func:
2557ad9d 302 Py_XDECREF(py_func);
2557ad9d 303 Py_XDECREF(py_mod);
1c6fa20f 304
036e6f67
UH
305 if (PyErr_Occurred())
306 PyErr_Print(); /* Returns void. */
307
308 return ret;
1c6fa20f
UH
309}
310
311/**
312 * Shutdown libsigrokdecode.
313 *
3a9a7e38 314 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f
UH
315 */
316int sigrokdecode_shutdown(void)
317{
318 /* Py_Finalize() returns void, any finalization errors are ignored. */
319 Py_Finalize();
320
70e44845 321 return SIGROKDECODE_OK;
1c6fa20f 322}