]> sigrok.org Git - libsigrokdecode.git/blob - decode.c
configure.ac: Fix firmware/decoders dir variables.
[libsigrokdecode.git] / decode.c
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>
23 #include <string.h>
24 #include <dirent.h>
25 #include <config.h>
26
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
34 /* The list of protocol decoders. */
35 GSList *list_pds = NULL;
36
37 /**
38  * Initialize libsigrokdecode.
39  *
40  * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
41  */
42 int sigrokdecode_init(void)
43 {
44         DIR *dir;
45         struct dirent *dp;
46         char *tmp;
47
48         /* Py_Initialize() returns void and usually cannot fail. */
49         Py_Initialize();
50
51         /* Add search directory for the protocol decoders. */
52         /* FIXME: Check error code. */
53         /* FIXME: What happens if this function is called multiple times? */
54         PyRun_SimpleString("import sys;"
55                            "sys.path.append(r'" DECODERS_DIR "');");
56
57         if (!(dir = opendir(DECODERS_DIR)))
58                 return SIGROKDECODE_ERR_DECODERS_DIR;
59
60         while ((dp = readdir(dir)) != NULL) {
61                 if (!g_str_has_suffix(dp->d_name, ".py"))
62                         continue;
63                 /* For now use the filename (without .py) as decoder name. */
64                 if ((tmp = g_strndup(dp->d_name, strlen(dp->d_name) - 3)))
65                         list_pds = g_slist_append(list_pds, tmp);
66         }
67         closedir(dir);
68
69         return SIGROKDECODE_OK;
70 }
71
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
84 /**
85  * Helper function to handle Python strings.
86  *
87  * TODO: @param entries.
88  *
89  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
90  *         The 'outstr' argument points to a malloc()ed string upon success.
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
127         return SIGROKDECODE_OK;
128 }
129
130 /**
131  * TODO
132  *
133  * @param name TODO
134  *
135  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
136  */
137 int sigrokdecode_load_decoder(const char *name,
138                               struct sigrokdecode_decoder **dec)
139 {
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;
206
207         return SIGROKDECODE_OK;
208 }
209
210 /**
211  * Run the specified decoder function.
212  *
213  * @param dec TODO
214  * @param inbuf TODO
215  * @param inbuflen TODO
216  * @param outbuf TODO
217  * @param outbuflen TODO
218  *
219  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
220  */
221 int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
222                              uint8_t *inbuf, uint64_t inbuflen,
223                              uint8_t **outbuf, uint64_t *outbuflen)
224 {
225         PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
226         int ret;
227
228         /* TODO: Use #defines for the return codes. */
229
230         /* Return an error upon unusable input. */
231         if (dec == NULL)
232                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
233         if (inbuf == NULL)
234                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
235         if (inbuflen == 0) /* No point in working on empty buffers. */
236                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
237         if (outbuf == NULL)
238                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
239         if (outbuflen == NULL)
240                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
241
242         /* TODO: Error handling. */
243         py_mod = dec->py_mod;
244         py_func = dec->py_func;
245
246         /* TODO: Really run Py_DECREF on py_mod/py_func? */
247
248         /* Create a Python tuple of size 1. */
249         if (!(py_args = PyTuple_New(1))) {
250                 PyErr_Print();
251                 Py_DECREF(py_func);
252                 Py_DECREF(py_mod);
253                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
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);
262                 Py_DECREF(py_mod);
263                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
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);
271                 Py_DECREF(py_mod);
272                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
273         }
274
275         if (!(py_res = PyObject_CallObject(py_func, py_args))) {
276                 PyErr_Print();
277                 Py_DECREF(py_value);
278                 Py_DECREF(py_args);
279                 Py_DECREF(py_func);
280                 Py_DECREF(py_mod);
281                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
282         }
283
284         if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
285                                          (Py_ssize_t *)outbuflen))) {
286                 PyErr_Print();
287                 Py_DECREF(py_res);
288                 Py_DECREF(py_value);
289                 Py_DECREF(py_args);
290                 Py_DECREF(py_func);
291                 Py_DECREF(py_mod);
292                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
293         }
294
295         Py_DECREF(py_res);
296         // Py_DECREF(py_value);
297         Py_DECREF(py_args);
298         Py_DECREF(py_func);
299         Py_DECREF(py_mod);
300
301         return SIGROKDECODE_OK;
302 }
303
304 /**
305  * Shutdown libsigrokdecode.
306  *
307  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
308  */
309 int sigrokdecode_shutdown(void)
310 {
311         /* Py_Finalize() returns void, any finalization errors are ignored. */
312         Py_Finalize();
313
314         return SIGROKDECODE_OK;
315 }