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