]>
Commit | Line | Data |
---|---|---|
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 | 35 | GSList *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 | ||
d752b12b | 50 | static int srd_load_decoder(const char *name, struct srd_decoder **dec); |
53bec0cf | 51 | |
1c6fa20f UH |
52 | /** |
53 | * Initialize libsigrokdecode. | |
54 | * | |
d752b12b | 55 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 56 | */ |
d752b12b | 57 | int srd_init(void) |
1c6fa20f | 58 | { |
7c24d086 UH |
59 | DIR *dir; |
60 | struct dirent *dp; | |
53bec0cf | 61 | char *decodername; |
d752b12b | 62 | struct srd_decoder *dec; |
53bec0cf | 63 | int ret; |
7c24d086 | 64 | |
1c6fa20f UH |
65 | /* Py_Initialize() returns void and usually cannot fail. */ |
66 | Py_Initialize(); | |
67 | ||
0895f56a | 68 | /* Add search directory for the protocol decoders. */ |
2454527d | 69 | /* FIXME: Check error code. */ |
70e44845 | 70 | /* FIXME: What happens if this function is called multiple times? */ |
0895f56a | 71 | PyRun_SimpleString("import sys;" |
10426068 | 72 | "sys.path.append(r'" DECODERS_DIR "');"); |
1c6fa20f | 73 | |
7c24d086 | 74 | if (!(dir = opendir(DECODERS_DIR))) |
d752b12b | 75 | return SRD_ERR_DECODERS_DIR; |
7c24d086 UH |
76 | |
77 | while ((dp = readdir(dir)) != NULL) { | |
276b55eb | 78 | if (!g_str_has_suffix(dp->d_name, ".py")) |
7c24d086 | 79 | continue; |
53bec0cf UH |
80 | |
81 | /* Decoder name == filename (without .py suffix). */ | |
82 | decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); | |
83 | ||
84 | /* TODO: Error handling. */ | |
d752b12b | 85 | dec = malloc(sizeof(struct srd_decoder)); |
53bec0cf UH |
86 | |
87 | /* Load the decoder. */ | |
d752b12b | 88 | ret = srd_load_decoder(decodername, &dec); |
53bec0cf UH |
89 | |
90 | /* Append it to the list of supported/loaded decoders. */ | |
91 | list_pds = g_slist_append(list_pds, dec); | |
7c24d086 UH |
92 | } |
93 | closedir(dir); | |
94 | ||
d752b12b | 95 | return SRD_OK; |
1c6fa20f UH |
96 | } |
97 | ||
871d21e2 UH |
98 | /** |
99 | * Returns the list of supported/loaded protocol decoders. | |
100 | * | |
101 | * This is a GSList containing the names of the decoders as strings. | |
102 | * | |
103 | * @return List of decoders, NULL if none are supported or loaded. | |
104 | */ | |
d752b12b | 105 | GSList *srd_list_decoders(void) |
871d21e2 UH |
106 | { |
107 | return list_pds; | |
108 | } | |
109 | ||
53bec0cf UH |
110 | /** |
111 | * Get the decoder with the specified ID. | |
112 | * | |
113 | * @param id The ID string of the decoder to return. | |
114 | * @return The decoder with the specified ID, or NULL if not found. | |
115 | */ | |
d752b12b | 116 | struct srd_decoder *srd_get_decoder_by_id(const char *id) |
53bec0cf UH |
117 | { |
118 | GSList *l; | |
d752b12b | 119 | struct srd_decoder *dec; |
53bec0cf | 120 | |
d752b12b | 121 | for (l = srd_list_decoders(); l; l = l->next) { |
53bec0cf UH |
122 | dec = l->data; |
123 | if (!strcmp(dec->id, id)) | |
124 | return dec; | |
125 | } | |
126 | ||
127 | return NULL; | |
128 | } | |
129 | ||
5c55017c UH |
130 | /** |
131 | * Helper function to handle Python strings. | |
132 | * | |
133 | * TODO: @param entries. | |
134 | * | |
d752b12b | 135 | * @return SRD_OK upon success, a (negative) error code otherwise. |
70e44845 | 136 | * The 'outstr' argument points to a malloc()ed string upon success. |
5c55017c UH |
137 | */ |
138 | static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod, | |
139 | const char *key, char **outstr) | |
140 | { | |
141 | PyObject *py_str; | |
142 | char *str; | |
f7313eea | 143 | int ret; |
5c55017c UH |
144 | |
145 | py_str = PyMapping_GetItemString(py_res, (char *)key); | |
146 | if (!py_str || !PyString_Check(py_str)) { | |
d752b12b | 147 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
f7313eea | 148 | goto err_h_decref_func; |
5c55017c UH |
149 | } |
150 | ||
6b08da24 UH |
151 | /* |
152 | * PyString_AsString()'s returned string refers to an internal buffer | |
153 | * (not a copy), i.e. the data must not be modified, and the memory | |
154 | * must not be free()'d. | |
155 | */ | |
5c55017c | 156 | if (!(str = PyString_AsString(py_str))) { |
d752b12b | 157 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
f7313eea | 158 | goto err_h_decref_str; |
5c55017c UH |
159 | } |
160 | ||
f9b54a6c | 161 | if (!(*outstr = g_strdup(str))) { |
d752b12b | 162 | ret = SRD_ERR_MALLOC; |
f7313eea | 163 | goto err_h_decref_str; |
5c55017c UH |
164 | } |
165 | ||
2557ad9d | 166 | Py_XDECREF(py_str); |
5c55017c | 167 | |
d752b12b | 168 | return SRD_OK; |
f7313eea UH |
169 | |
170 | err_h_decref_str: | |
171 | Py_XDECREF(py_str); | |
172 | err_h_decref_func: | |
173 | Py_XDECREF(py_func); | |
f7313eea UH |
174 | Py_XDECREF(py_mod); |
175 | ||
176 | if (PyErr_Occurred()) | |
177 | PyErr_Print(); /* Returns void. */ | |
178 | ||
179 | return ret; | |
5c55017c UH |
180 | } |
181 | ||
1c6fa20f UH |
182 | /** |
183 | * TODO | |
184 | * | |
185 | * @param name TODO | |
70e44845 | 186 | * |
d752b12b | 187 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 188 | */ |
d752b12b UH |
189 | static int srd_load_decoder(const char *name, |
190 | struct srd_decoder **dec) | |
1c6fa20f | 191 | { |
d752b12b | 192 | struct srd_decoder *d; |
c7bb5dff | 193 | PyObject *py_mod, *py_func, *py_res /* , *py_tuple */; |
5c55017c UH |
194 | int r; |
195 | ||
5c55017c | 196 | /* "Import" the Python module. */ |
c7bb5dff | 197 | if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */ |
6b08da24 | 198 | PyErr_Print(); /* Returns void. */ |
d752b12b | 199 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c | 200 | } |
5c55017c UH |
201 | |
202 | /* Get the 'register' function name as Python callable object. */ | |
6b08da24 | 203 | py_func = PyObject_GetAttrString(py_mod, "register"); /* NEWREF */ |
5c55017c UH |
204 | if (!py_func || !PyCallable_Check(py_func)) { |
205 | if (PyErr_Occurred()) | |
6b08da24 | 206 | PyErr_Print(); /* Returns void. */ |
2557ad9d | 207 | Py_XDECREF(py_mod); |
d752b12b | 208 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c UH |
209 | } |
210 | ||
211 | /* Call the 'register' function without arguments, get the result. */ | |
6b08da24 UH |
212 | if (!(py_res = PyObject_CallFunction(py_func, NULL))) { /* NEWREF */ |
213 | PyErr_Print(); /* Returns void. */ | |
2557ad9d UH |
214 | Py_XDECREF(py_func); |
215 | Py_XDECREF(py_mod); | |
d752b12b | 216 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c UH |
217 | } |
218 | ||
d752b12b UH |
219 | if (!(d = malloc(sizeof(struct srd_decoder)))) |
220 | return SRD_ERR_MALLOC; | |
5c55017c UH |
221 | |
222 | if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0) | |
223 | return r; | |
224 | ||
225 | if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0) | |
226 | return r; | |
227 | ||
228 | if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0) | |
229 | return r; | |
230 | ||
231 | d->py_mod = py_mod; | |
232 | ||
2557ad9d UH |
233 | Py_XDECREF(py_res); |
234 | Py_XDECREF(py_func); | |
5c55017c UH |
235 | |
236 | /* Get the 'decode' function name as Python callable object. */ | |
6b08da24 | 237 | py_func = PyObject_GetAttrString(py_mod, "decode"); /* NEWREF */ |
5c55017c UH |
238 | if (!py_func || !PyCallable_Check(py_func)) { |
239 | if (PyErr_Occurred()) | |
6b08da24 | 240 | PyErr_Print(); /* Returns void. */ |
2557ad9d | 241 | Py_XDECREF(py_mod); |
d752b12b | 242 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c UH |
243 | } |
244 | ||
245 | d->py_func = py_func; | |
246 | ||
f9b54a6c UH |
247 | /* TODO: Handle func, inputformats, outputformats. */ |
248 | /* Note: They must at least be set to NULL, will segfault otherwise. */ | |
249 | d->func = NULL; | |
250 | d->inputformats = NULL; | |
251 | d->outputformats = NULL; | |
5c55017c UH |
252 | |
253 | *dec = d; | |
910e5fd8 | 254 | |
d752b12b | 255 | return SRD_OK; |
1c6fa20f UH |
256 | } |
257 | ||
258 | /** | |
259 | * Run the specified decoder function. | |
260 | * | |
5c55017c | 261 | * @param dec TODO |
1c6fa20f UH |
262 | * @param inbuf TODO |
263 | * @param inbuflen TODO | |
264 | * @param outbuf TODO | |
265 | * @param outbuflen TODO | |
70e44845 | 266 | * |
d752b12b | 267 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 268 | */ |
d752b12b | 269 | int srd_run_decoder(struct srd_decoder *dec, |
2b45dd71 | 270 | uint8_t *inbuf, uint64_t inbuflen, |
271 | uint8_t **outbuf, uint64_t *outbuflen) | |
1c6fa20f | 272 | { |
5c55017c | 273 | PyObject *py_mod, *py_func, *py_args, *py_value, *py_res; |
036e6f67 | 274 | int r, ret; |
1c6fa20f UH |
275 | |
276 | /* TODO: Use #defines for the return codes. */ | |
277 | ||
278 | /* Return an error upon unusable input. */ | |
5c55017c | 279 | if (dec == NULL) |
d752b12b | 280 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 281 | if (inbuf == NULL) |
d752b12b | 282 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 283 | if (inbuflen == 0) /* No point in working on empty buffers. */ |
d752b12b | 284 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 285 | if (outbuf == NULL) |
d752b12b | 286 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 287 | if (outbuflen == NULL) |
d752b12b | 288 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 289 | |
5c55017c UH |
290 | /* TODO: Error handling. */ |
291 | py_mod = dec->py_mod; | |
2557ad9d | 292 | Py_XINCREF(py_mod); |
5c55017c | 293 | py_func = dec->py_func; |
2557ad9d | 294 | Py_XINCREF(py_func); |
1c6fa20f UH |
295 | |
296 | /* Create a Python tuple of size 1. */ | |
6b08da24 | 297 | if (!(py_args = PyTuple_New(1))) { /* NEWREF */ |
d752b12b | 298 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
036e6f67 | 299 | goto err_run_decref_func; |
1c6fa20f UH |
300 | } |
301 | ||
302 | /* Get the input buffer as Python "string" (byte array). */ | |
303 | /* TODO: int vs. uint64_t for 'inbuflen'? */ | |
6b08da24 | 304 | if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) { /* NEWREF */ |
d752b12b | 305 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
036e6f67 | 306 | goto err_run_decref_args; |
1c6fa20f UH |
307 | } |
308 | ||
f800adc4 UH |
309 | /* |
310 | * IMPORTANT: PyTuple_SetItem() "steals" a reference to py_value! | |
2557ad9d | 311 | * That means we are no longer responsible for Py_XDECREF()'ing it. |
f800adc4 UH |
312 | * It will automatically be free'd when the 'py_args' tuple is free'd. |
313 | */ | |
6b08da24 | 314 | if (PyTuple_SetItem(py_args, 0, py_value) != 0) { /* STEAL */ |
d752b12b | 315 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
2557ad9d | 316 | Py_XDECREF(py_value); /* TODO: Ref. stolen upon error? */ |
036e6f67 | 317 | goto err_run_decref_args; |
1c6fa20f UH |
318 | } |
319 | ||
6b08da24 | 320 | if (!(py_res = PyObject_CallObject(py_func, py_args))) { /* NEWREF */ |
d752b12b | 321 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
036e6f67 | 322 | goto err_run_decref_args; |
1c6fa20f UH |
323 | } |
324 | ||
036e6f67 UH |
325 | if ((r = PyObject_AsCharBuffer(py_res, (const char **)outbuf, |
326 | (Py_ssize_t *)outbuflen))) { | |
d752b12b | 327 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
2557ad9d | 328 | Py_XDECREF(py_res); |
036e6f67 | 329 | goto err_run_decref_args; |
1c6fa20f UH |
330 | } |
331 | ||
d752b12b | 332 | ret = SRD_OK; |
036e6f67 | 333 | |
2557ad9d | 334 | Py_XDECREF(py_res); |
036e6f67 UH |
335 | |
336 | err_run_decref_args: | |
2557ad9d | 337 | Py_XDECREF(py_args); |
036e6f67 | 338 | err_run_decref_func: |
2557ad9d | 339 | Py_XDECREF(py_func); |
2557ad9d | 340 | Py_XDECREF(py_mod); |
1c6fa20f | 341 | |
036e6f67 UH |
342 | if (PyErr_Occurred()) |
343 | PyErr_Print(); /* Returns void. */ | |
344 | ||
345 | return ret; | |
1c6fa20f UH |
346 | } |
347 | ||
f9b54a6c UH |
348 | /** |
349 | * TODO | |
350 | */ | |
d752b12b | 351 | static int srd_unload_decoder(struct srd_decoder *dec) |
f9b54a6c UH |
352 | { |
353 | g_free(dec->id); | |
354 | g_free(dec->name); | |
355 | g_free(dec->desc); | |
356 | g_free(dec->func); | |
357 | ||
358 | /* TODO: Free everything in inputformats and outputformats. */ | |
359 | ||
360 | if (dec->inputformats != NULL) | |
361 | g_slist_free(dec->inputformats); | |
362 | if (dec->outputformats != NULL) | |
363 | g_slist_free(dec->outputformats); | |
364 | ||
365 | Py_XDECREF(dec->py_func); | |
366 | Py_XDECREF(dec->py_mod); | |
367 | ||
d752b12b | 368 | return SRD_OK; |
f9b54a6c UH |
369 | } |
370 | ||
371 | /** | |
372 | * TODO | |
373 | */ | |
d752b12b | 374 | static int srd_unload_all_decoders(void) |
f9b54a6c UH |
375 | { |
376 | GSList *l; | |
d752b12b | 377 | struct srd_decoder *dec; |
f9b54a6c | 378 | |
d752b12b | 379 | for (l = srd_list_decoders(); l; l = l->next) { |
f9b54a6c UH |
380 | dec = l->data; |
381 | /* TODO: Error handling. */ | |
d752b12b | 382 | srd_unload_decoder(dec); |
f9b54a6c UH |
383 | } |
384 | ||
d752b12b | 385 | return SRD_OK; |
f9b54a6c UH |
386 | } |
387 | ||
1c6fa20f UH |
388 | /** |
389 | * Shutdown libsigrokdecode. | |
390 | * | |
d752b12b | 391 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 392 | */ |
d752b12b | 393 | int srd_shutdown(void) |
1c6fa20f | 394 | { |
f9b54a6c UH |
395 | /* Unload/free all decoders, and then the list of decoders itself. */ |
396 | /* TODO: Error handling. */ | |
d752b12b | 397 | srd_unload_all_decoders(); |
f9b54a6c UH |
398 | g_slist_free(list_pds); |
399 | ||
1c6fa20f UH |
400 | /* Py_Finalize() returns void, any finalization errors are ignored. */ |
401 | Py_Finalize(); | |
402 | ||
d752b12b | 403 | return SRD_OK; |
1c6fa20f | 404 | } |