]> sigrok.org Git - libsigrokdecode.git/blob - decode.c
337979dee0e1bc411e73fbb1198af65dfab57cb2
[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
25 /* Re-define some string functions for Python >= 3.0. */
26 #if PY_VERSION_HEX >= 0x03000000
27 #define PyString_AsString PyBytes_AsString
28 #define PyString_FromString PyBytes_FromString
29 #define PyString_Check PyBytes_Check
30 #endif
31
32 /**
33  * Initialize libsigrokdecode.
34  *
35  * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
36  */
37 int sigrokdecode_init(void)
38 {
39         /* Py_Initialize() returns void and usually cannot fail. */
40         Py_Initialize();
41
42         /* Add some more search directories for convenience. */
43         /* FIXME: Check error code. */
44         /* FIXME: What happens if this function is called multiple times? */
45         PyRun_SimpleString(
46                 "import sys;"
47                 "sys.path.append('libsigrokdecode/decoders');"
48                 "sys.path.append('../libsigrokdecode/decoders');"
49                 "sys.path.append('/usr/local/share/sigrok');"
50                 );
51
52         return SIGROKDECODE_OK;
53 }
54
55 /**
56  * Helper function to handle Python strings.
57  *
58  * TODO: @param entries.
59  *
60  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
61  *         The 'outstr' argument points to a malloc()ed string upon success.
62  */
63 static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod,
64                  const char *key, char **outstr)
65 {
66         PyObject *py_str;
67         char *str;
68
69         py_str = PyMapping_GetItemString(py_res, (char *)key);
70         if (!py_str || !PyString_Check(py_str)) {
71                 if (PyErr_Occurred())
72                         PyErr_Print();
73                 Py_DECREF(py_func);
74                 Py_DECREF(py_mod);
75                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
76         }
77
78         if (!(str = PyString_AsString(py_str))) {
79                 if (PyErr_Occurred())
80                         PyErr_Print();
81                 Py_DECREF(py_str);
82                 Py_DECREF(py_func);
83                 Py_DECREF(py_mod);
84                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
85         }
86
87         if (!(*outstr = strdup(str))) {
88                 if (PyErr_Occurred())
89                         PyErr_Print();
90                 Py_DECREF(py_str);
91                 Py_DECREF(py_func);
92                 Py_DECREF(py_mod);
93                 return SIGROKDECODE_ERR_MALLOC;
94         }
95
96         Py_DECREF(py_str);
97
98         return SIGROKDECODE_OK;
99 }
100
101 /**
102  * TODO
103  *
104  * @param name TODO
105  *
106  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
107  */
108 int sigrokdecode_load_decoder(const char *name,
109                               struct sigrokdecode_decoder **dec)
110 {
111         struct sigrokdecode_decoder *d;
112         PyObject *py_name, *py_mod, *py_func, *py_res /* , *py_tuple */;
113         int r;
114
115         /* Get the name of the decoder module as Python string. */
116         if (!(py_name = PyString_FromString(name))) {
117                 PyErr_Print();
118                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
119         }
120
121         /* "Import" the Python module. */
122         if (!(py_mod = PyImport_Import(py_name))) {
123                 PyErr_Print();
124                 Py_DECREF(py_name);
125                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
126         }
127         Py_DECREF(py_name);
128
129         /* Get the 'register' function name as Python callable object. */
130         py_func = PyObject_GetAttrString(py_mod, "register");
131         if (!py_func || !PyCallable_Check(py_func)) {
132                 if (PyErr_Occurred())
133                         PyErr_Print();
134                 Py_DECREF(py_mod);
135                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
136         }
137
138         /* Call the 'register' function without arguments, get the result. */
139         if (!(py_res = PyObject_CallFunction(py_func, NULL))) {
140                 PyErr_Print();
141                 Py_DECREF(py_func);
142                 Py_DECREF(py_mod);
143                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
144         }
145
146         if (!(d = malloc(sizeof(struct sigrokdecode_decoder))))
147                 return SIGROKDECODE_ERR_MALLOC;
148
149         if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0)
150                 return r;
151
152         if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0)
153                 return r;
154
155         if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0)
156                 return r;
157
158         d->py_mod = py_mod;
159
160         Py_DECREF(py_res);
161         Py_DECREF(py_func);
162
163         /* Get the 'decode' function name as Python callable object. */
164         py_func = PyObject_GetAttrString(py_mod, "decode");
165         if (!py_func || !PyCallable_Check(py_func)) {
166                 if (PyErr_Occurred())
167                         PyErr_Print();
168                 Py_DECREF(py_mod);
169                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
170         }
171
172         d->py_func = py_func;
173
174         /* TODO: Handle inputformats, outputformats. */
175
176         *dec = d;
177
178         return SIGROKDECODE_OK;
179 }
180
181 /**
182  * Run the specified decoder function.
183  *
184  * @param dec TODO
185  * @param inbuf TODO
186  * @param inbuflen TODO
187  * @param outbuf TODO
188  * @param outbuflen TODO
189  *
190  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
191  */
192 int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
193                              uint8_t *inbuf, uint64_t inbuflen,
194                              uint8_t **outbuf, uint64_t *outbuflen)
195 {
196         PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
197         int ret;
198
199         /* TODO: Use #defines for the return codes. */
200
201         /* Return an error upon unusable input. */
202         if (dec == NULL)
203                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
204         if (inbuf == NULL)
205                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
206         if (inbuflen == 0) /* No point in working on empty buffers. */
207                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
208         if (outbuf == NULL)
209                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
210         if (outbuflen == NULL)
211                 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
212
213         /* TODO: Error handling. */
214         py_mod = dec->py_mod;
215         py_func = dec->py_func;
216
217         /* TODO: Really run Py_DECREF on py_mod/py_func? */
218
219         /* Create a Python tuple of size 1. */
220         if (!(py_args = PyTuple_New(1))) {
221                 PyErr_Print();
222                 Py_DECREF(py_func);
223                 Py_DECREF(py_mod);
224                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
225         }
226
227         /* Get the input buffer as Python "string" (byte array). */
228         /* TODO: int vs. uint64_t for 'inbuflen'? */
229         if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
230                 PyErr_Print();
231                 Py_DECREF(py_args);
232                 Py_DECREF(py_func);
233                 Py_DECREF(py_mod);
234                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
235         }
236
237         if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
238                 PyErr_Print();
239                 Py_DECREF(py_value);
240                 Py_DECREF(py_args);
241                 Py_DECREF(py_func);
242                 Py_DECREF(py_mod);
243                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
244         }
245
246         if (!(py_res = PyObject_CallObject(py_func, py_args))) {
247                 PyErr_Print();
248                 Py_DECREF(py_value);
249                 Py_DECREF(py_args);
250                 Py_DECREF(py_func);
251                 Py_DECREF(py_mod);
252                 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
253         }
254
255         if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
256                                          (Py_ssize_t *)outbuflen))) {
257                 PyErr_Print();
258                 Py_DECREF(py_res);
259                 Py_DECREF(py_value);
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         Py_DECREF(py_res);
267         // Py_DECREF(py_value);
268         Py_DECREF(py_args);
269         Py_DECREF(py_func);
270         Py_DECREF(py_mod);
271
272         return SIGROKDECODE_OK;
273 }
274
275 /**
276  * Shutdown libsigrokdecode.
277  *
278  * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
279  */
280 int sigrokdecode_shutdown(void)
281 {
282         /* Py_Finalize() returns void, any finalization errors are ignored. */
283         Py_Finalize();
284
285         return SIGROKDECODE_OK;
286 }