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