]>
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 | ||
17830fe1 | 21 | #include "config.h" |
1c6fa20f UH |
22 | #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
23 | #include <stdio.h> | |
5c55017c | 24 | #include <string.h> |
7c24d086 | 25 | #include <dirent.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. */ |
a0d48257 | 35 | static 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 | |
922763ab KS |
52 | static int _unitsize = 1; |
53 | ||
922763ab KS |
54 | static PyObject* |
55 | emb_put(PyObject *self, PyObject *args) | |
56 | { | |
57 | PyObject *arg; | |
58 | ||
6eb87578 GM |
59 | (void)self; |
60 | ||
922763ab KS |
61 | if (!PyArg_ParseTuple(args, "O:put", &arg)) |
62 | return NULL; | |
63 | ||
052f32ee | 64 | fprintf(stderr, "sigrok.put() called by decoder:\n"); |
3b24e378 KS |
65 | PyObject_Print(arg, stdout, Py_PRINT_RAW); |
66 | ||
922763ab KS |
67 | Py_RETURN_NONE; |
68 | } | |
69 | ||
70 | static PyMethodDef EmbMethods[] = { | |
922763ab KS |
71 | {"put", emb_put, METH_VARARGS, |
72 | "Accepts a dictionary with the following keys: time, duration, data"}, | |
73 | {NULL, NULL, 0, NULL} | |
74 | }; | |
75 | ||
1c6fa20f UH |
76 | /** |
77 | * Initialize libsigrokdecode. | |
78 | * | |
d752b12b | 79 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 80 | */ |
d752b12b | 81 | int srd_init(void) |
1c6fa20f | 82 | { |
7c24d086 UH |
83 | DIR *dir; |
84 | struct dirent *dp; | |
53bec0cf | 85 | char *decodername; |
d752b12b | 86 | struct srd_decoder *dec; |
53bec0cf | 87 | int ret; |
7c24d086 | 88 | |
1c6fa20f UH |
89 | /* Py_Initialize() returns void and usually cannot fail. */ |
90 | Py_Initialize(); | |
91 | ||
922763ab KS |
92 | Py_InitModule("sigrok", EmbMethods); |
93 | ||
0895f56a | 94 | /* Add search directory for the protocol decoders. */ |
2454527d | 95 | /* FIXME: Check error code. */ |
70e44845 | 96 | /* FIXME: What happens if this function is called multiple times? */ |
0895f56a | 97 | PyRun_SimpleString("import sys;" |
10426068 | 98 | "sys.path.append(r'" DECODERS_DIR "');"); |
1c6fa20f | 99 | |
7c24d086 | 100 | if (!(dir = opendir(DECODERS_DIR))) |
d752b12b | 101 | return SRD_ERR_DECODERS_DIR; |
7c24d086 UH |
102 | |
103 | while ((dp = readdir(dir)) != NULL) { | |
276b55eb | 104 | if (!g_str_has_suffix(dp->d_name, ".py")) |
7c24d086 | 105 | continue; |
53bec0cf UH |
106 | |
107 | /* Decoder name == filename (without .py suffix). */ | |
108 | decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); | |
109 | ||
110 | /* TODO: Error handling. */ | |
d752b12b | 111 | dec = malloc(sizeof(struct srd_decoder)); |
53bec0cf UH |
112 | |
113 | /* Load the decoder. */ | |
d752b12b | 114 | ret = srd_load_decoder(decodername, &dec); |
3b24e378 KS |
115 | if (!ret) |
116 | { | |
117 | /* Append it to the list of supported/loaded decoders. */ | |
118 | list_pds = g_slist_append(list_pds, dec); | |
119 | } | |
7c24d086 UH |
120 | } |
121 | closedir(dir); | |
122 | ||
d752b12b | 123 | return SRD_OK; |
1c6fa20f UH |
124 | } |
125 | ||
871d21e2 UH |
126 | /** |
127 | * Returns the list of supported/loaded protocol decoders. | |
128 | * | |
129 | * This is a GSList containing the names of the decoders as strings. | |
130 | * | |
131 | * @return List of decoders, NULL if none are supported or loaded. | |
132 | */ | |
d752b12b | 133 | GSList *srd_list_decoders(void) |
871d21e2 UH |
134 | { |
135 | return list_pds; | |
136 | } | |
137 | ||
53bec0cf UH |
138 | /** |
139 | * Get the decoder with the specified ID. | |
140 | * | |
141 | * @param id The ID string of the decoder to return. | |
142 | * @return The decoder with the specified ID, or NULL if not found. | |
143 | */ | |
d752b12b | 144 | struct srd_decoder *srd_get_decoder_by_id(const char *id) |
53bec0cf UH |
145 | { |
146 | GSList *l; | |
d752b12b | 147 | struct srd_decoder *dec; |
53bec0cf | 148 | |
d752b12b | 149 | for (l = srd_list_decoders(); l; l = l->next) { |
53bec0cf UH |
150 | dec = l->data; |
151 | if (!strcmp(dec->id, id)) | |
152 | return dec; | |
153 | } | |
154 | ||
155 | return NULL; | |
156 | } | |
157 | ||
5c55017c UH |
158 | /** |
159 | * Helper function to handle Python strings. | |
160 | * | |
161 | * TODO: @param entries. | |
162 | * | |
d752b12b | 163 | * @return SRD_OK upon success, a (negative) error code otherwise. |
70e44845 | 164 | * The 'outstr' argument points to a malloc()ed string upon success. |
5c55017c | 165 | */ |
922763ab | 166 | static int h_str(PyObject *py_res, PyObject *py_mod, |
5c55017c UH |
167 | const char *key, char **outstr) |
168 | { | |
169 | PyObject *py_str; | |
170 | char *str; | |
f7313eea | 171 | int ret; |
5c55017c | 172 | |
6eb87578 | 173 | py_str = PyObject_GetAttrString(py_res, (char *)key); /* NEWREF */ |
5c55017c | 174 | if (!py_str || !PyString_Check(py_str)) { |
d752b12b | 175 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
922763ab | 176 | goto err_h_decref_mod; |
5c55017c UH |
177 | } |
178 | ||
6b08da24 UH |
179 | /* |
180 | * PyString_AsString()'s returned string refers to an internal buffer | |
181 | * (not a copy), i.e. the data must not be modified, and the memory | |
182 | * must not be free()'d. | |
183 | */ | |
5c55017c | 184 | if (!(str = PyString_AsString(py_str))) { |
d752b12b | 185 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
f7313eea | 186 | goto err_h_decref_str; |
5c55017c UH |
187 | } |
188 | ||
f9b54a6c | 189 | if (!(*outstr = g_strdup(str))) { |
d752b12b | 190 | ret = SRD_ERR_MALLOC; |
f7313eea | 191 | goto err_h_decref_str; |
5c55017c UH |
192 | } |
193 | ||
2557ad9d | 194 | Py_XDECREF(py_str); |
5c55017c | 195 | |
d752b12b | 196 | return SRD_OK; |
f7313eea UH |
197 | |
198 | err_h_decref_str: | |
199 | Py_XDECREF(py_str); | |
922763ab | 200 | err_h_decref_mod: |
f7313eea UH |
201 | Py_XDECREF(py_mod); |
202 | ||
203 | if (PyErr_Occurred()) | |
204 | PyErr_Print(); /* Returns void. */ | |
205 | ||
206 | return ret; | |
5c55017c UH |
207 | } |
208 | ||
1c6fa20f UH |
209 | /** |
210 | * TODO | |
211 | * | |
212 | * @param name TODO | |
70e44845 | 213 | * |
d752b12b | 214 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 215 | */ |
d752b12b UH |
216 | static int srd_load_decoder(const char *name, |
217 | struct srd_decoder **dec) | |
1c6fa20f | 218 | { |
d752b12b | 219 | struct srd_decoder *d; |
6eb87578 GM |
220 | PyObject *py_mod, *py_res; |
221 | PyObject *py_args, *py_value, *py_instance; | |
5c55017c | 222 | int r; |
6eb87578 | 223 | fprintf(stdout, "%s: %s\n", __func__, name); |
5c55017c | 224 | |
5c55017c | 225 | /* "Import" the Python module. */ |
c7bb5dff | 226 | if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */ |
6b08da24 | 227 | PyErr_Print(); /* Returns void. */ |
d752b12b | 228 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c | 229 | } |
5c55017c | 230 | |
6eb87578 GM |
231 | /* Get the 'Decoder' class as Python object. */ |
232 | py_res = PyObject_GetAttrString(py_mod, "Decoder"); /* NEWREF */ | |
233 | if (!py_res) { | |
5c55017c | 234 | if (PyErr_Occurred()) |
6b08da24 | 235 | PyErr_Print(); /* Returns void. */ |
2557ad9d | 236 | Py_XDECREF(py_mod); |
6eb87578 | 237 | fprintf(stderr, "Decoder class not found in PD module %s\n", name); |
d752b12b | 238 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c UH |
239 | } |
240 | ||
d752b12b UH |
241 | if (!(d = malloc(sizeof(struct srd_decoder)))) |
242 | return SRD_ERR_MALLOC; | |
5c55017c | 243 | |
6eb87578 GM |
244 | /* We'll just use the name of the module for the id */ |
245 | d->id = strdup(name); | |
5c55017c | 246 | |
922763ab | 247 | if ((r = h_str(py_res, py_mod, "name", &(d->name))) < 0) |
5c55017c UH |
248 | return r; |
249 | ||
922763ab | 250 | if ((r = h_str(py_res, py_mod, "longname", |
75a282e5 UH |
251 | &(d->longname))) < 0) |
252 | return r; | |
253 | ||
922763ab | 254 | if ((r = h_str(py_res, py_mod, "desc", &(d->desc))) < 0) |
5c55017c UH |
255 | return r; |
256 | ||
922763ab | 257 | if ((r = h_str(py_res, py_mod, "longdesc", |
75a282e5 UH |
258 | &(d->longdesc))) < 0) |
259 | return r; | |
260 | ||
922763ab | 261 | if ((r = h_str(py_res, py_mod, "author", &(d->author))) < 0) |
75a282e5 UH |
262 | return r; |
263 | ||
922763ab | 264 | if ((r = h_str(py_res, py_mod, "email", &(d->email))) < 0) |
75a282e5 UH |
265 | return r; |
266 | ||
922763ab | 267 | if ((r = h_str(py_res, py_mod, "license", &(d->license))) < 0) |
75a282e5 UH |
268 | return r; |
269 | ||
5c55017c | 270 | d->py_mod = py_mod; |
6eb87578 | 271 | d->py_decobj = py_res; |
5c55017c | 272 | |
6eb87578 GM |
273 | /* Create a Python tuple of size 1. */ |
274 | if (!(py_args = PyTuple_New(0))) { /* NEWREF */ | |
5c55017c | 275 | if (PyErr_Occurred()) |
6b08da24 | 276 | PyErr_Print(); /* Returns void. */ |
3b24e378 | 277 | |
6eb87578 GM |
278 | Py_XDECREF(py_res); |
279 | Py_XDECREF(py_mod); | |
3b24e378 | 280 | |
6eb87578 | 281 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
5c55017c | 282 | } |
6eb87578 GM |
283 | |
284 | py_value = Py_BuildValue("{sssisd}", | |
285 | "driver", "demo", | |
286 | "unitsize", _unitsize, //FIXME: Pass in a unitsize that matches the selected LA | |
287 | "starttime", 129318231823.0 //TODO: Fill with something reasonable. | |
288 | ); | |
289 | /* Create an instance of the Decoder class */ | |
290 | py_instance = PyObject_Call(py_res, py_args, py_value); | |
291 | if (!py_instance) { | |
292 | if (PyErr_Occurred()) | |
293 | PyErr_Print(); /* Returns void. */ | |
294 | Py_XDECREF(py_value); /* TODO: Ref. stolen upon error? */ | |
295 | Py_XDECREF(py_res); | |
296 | Py_XDECREF(py_mod); | |
297 | fprintf(stderr, "Unable to create instance of Decoder class in PD module %s\n", name); | |
298 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ | |
299 | } | |
052f32ee | 300 | d->py_instance = py_instance; |
5c55017c | 301 | |
f9b54a6c UH |
302 | /* TODO: Handle func, inputformats, outputformats. */ |
303 | /* Note: They must at least be set to NULL, will segfault otherwise. */ | |
304 | d->func = NULL; | |
305 | d->inputformats = NULL; | |
306 | d->outputformats = NULL; | |
5c55017c UH |
307 | |
308 | *dec = d; | |
910e5fd8 | 309 | |
d752b12b | 310 | return SRD_OK; |
1c6fa20f UH |
311 | } |
312 | ||
313 | /** | |
314 | * Run the specified decoder function. | |
315 | * | |
5c55017c | 316 | * @param dec TODO |
1c6fa20f UH |
317 | * @param inbuf TODO |
318 | * @param inbuflen TODO | |
319 | * @param outbuf TODO | |
320 | * @param outbuflen TODO | |
70e44845 | 321 | * |
d752b12b | 322 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 323 | */ |
d752b12b | 324 | int srd_run_decoder(struct srd_decoder *dec, |
2b45dd71 | 325 | uint8_t *inbuf, uint64_t inbuflen, |
326 | uint8_t **outbuf, uint64_t *outbuflen) | |
1c6fa20f | 327 | { |
052f32ee | 328 | PyObject *py_mod, *py_instance, *py_args, *py_value, *py_res; |
036e6f67 | 329 | int r, ret; |
9d29ffad KS |
330 | |
331 | /* FIXME: Don't have a timebase available here. Make one up. */ | |
332 | static int _timehack = 0; | |
333 | _timehack += inbuflen; | |
1c6fa20f UH |
334 | |
335 | /* TODO: Use #defines for the return codes. */ | |
336 | ||
337 | /* Return an error upon unusable input. */ | |
5c55017c | 338 | if (dec == NULL) |
d752b12b | 339 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 340 | if (inbuf == NULL) |
d752b12b | 341 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 342 | if (inbuflen == 0) /* No point in working on empty buffers. */ |
d752b12b | 343 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 344 | if (outbuf == NULL) |
d752b12b | 345 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
1c6fa20f | 346 | if (outbuflen == NULL) |
d752b12b | 347 | return SRD_ERR_ARGS; /* TODO: More specific error? */ |
922763ab | 348 | |
5c55017c UH |
349 | /* TODO: Error handling. */ |
350 | py_mod = dec->py_mod; | |
2557ad9d | 351 | Py_XINCREF(py_mod); |
052f32ee GM |
352 | py_instance = dec->py_instance; |
353 | Py_XINCREF(py_instance); | |
1c6fa20f UH |
354 | |
355 | /* Create a Python tuple of size 1. */ | |
6b08da24 | 356 | if (!(py_args = PyTuple_New(1))) { /* NEWREF */ |
d752b12b | 357 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
036e6f67 | 358 | goto err_run_decref_func; |
1c6fa20f UH |
359 | } |
360 | ||
9d29ffad KS |
361 | /* Get the input buffer as Python "string" (byte array). */ |
362 | /* TODO: int vs. uint64_t for 'inbuflen'? */ | |
363 | ||
364 | py_value = Py_BuildValue("{sisiss#}", | |
365 | "time", _timehack, | |
366 | "duration", 10, | |
367 | "data", inbuf, inbuflen / _unitsize | |
368 | ); | |
369 | ||
052f32ee GM |
370 | if (!(py_res = PyObject_CallMethod(py_instance, "decode", |
371 | "O", py_value))) { /* NEWREF */ | |
9d29ffad KS |
372 | ret = SRD_ERR_PYTHON; /* TODO: More specific error? */ |
373 | goto err_run_decref_args; | |
374 | } | |
375 | ||
1c6fa20f | 376 | |
d752b12b | 377 | ret = SRD_OK; |
036e6f67 | 378 | |
2557ad9d | 379 | Py_XDECREF(py_res); |
036e6f67 UH |
380 | |
381 | err_run_decref_args: | |
2557ad9d | 382 | Py_XDECREF(py_args); |
036e6f67 | 383 | err_run_decref_func: |
052f32ee | 384 | Py_XDECREF(py_instance); |
2557ad9d | 385 | Py_XDECREF(py_mod); |
1c6fa20f | 386 | |
036e6f67 UH |
387 | if (PyErr_Occurred()) |
388 | PyErr_Print(); /* Returns void. */ | |
389 | ||
390 | return ret; | |
1c6fa20f UH |
391 | } |
392 | ||
f9b54a6c UH |
393 | /** |
394 | * TODO | |
395 | */ | |
d752b12b | 396 | static int srd_unload_decoder(struct srd_decoder *dec) |
f9b54a6c UH |
397 | { |
398 | g_free(dec->id); | |
399 | g_free(dec->name); | |
400 | g_free(dec->desc); | |
401 | g_free(dec->func); | |
402 | ||
403 | /* TODO: Free everything in inputformats and outputformats. */ | |
404 | ||
405 | if (dec->inputformats != NULL) | |
406 | g_slist_free(dec->inputformats); | |
407 | if (dec->outputformats != NULL) | |
408 | g_slist_free(dec->outputformats); | |
409 | ||
052f32ee | 410 | Py_XDECREF(dec->py_instance); |
f9b54a6c UH |
411 | Py_XDECREF(dec->py_mod); |
412 | ||
d752b12b | 413 | return SRD_OK; |
f9b54a6c UH |
414 | } |
415 | ||
416 | /** | |
417 | * TODO | |
418 | */ | |
d752b12b | 419 | static int srd_unload_all_decoders(void) |
f9b54a6c UH |
420 | { |
421 | GSList *l; | |
d752b12b | 422 | struct srd_decoder *dec; |
f9b54a6c | 423 | |
d752b12b | 424 | for (l = srd_list_decoders(); l; l = l->next) { |
f9b54a6c UH |
425 | dec = l->data; |
426 | /* TODO: Error handling. */ | |
d752b12b | 427 | srd_unload_decoder(dec); |
f9b54a6c UH |
428 | } |
429 | ||
d752b12b | 430 | return SRD_OK; |
f9b54a6c UH |
431 | } |
432 | ||
1c6fa20f UH |
433 | /** |
434 | * Shutdown libsigrokdecode. | |
435 | * | |
d752b12b | 436 | * @return SRD_OK upon success, a (negative) error code otherwise. |
1c6fa20f | 437 | */ |
7ea73e33 | 438 | int srd_exit(void) |
1c6fa20f | 439 | { |
f9b54a6c UH |
440 | /* Unload/free all decoders, and then the list of decoders itself. */ |
441 | /* TODO: Error handling. */ | |
d752b12b | 442 | srd_unload_all_decoders(); |
f9b54a6c UH |
443 | g_slist_free(list_pds); |
444 | ||
1c6fa20f UH |
445 | /* Py_Finalize() returns void, any finalization errors are ignored. */ |
446 | Py_Finalize(); | |
447 | ||
d752b12b | 448 | return SRD_OK; |
1c6fa20f | 449 | } |