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