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