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