]> sigrok.org Git - libsigrokdecode.git/blame - decode.c
Rename the scripts/ directory to decoders/.
[libsigrokdecode.git] / decode.c
CommitLineData
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>
1c6fa20f 24
e6c7a826
UH
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
1c6fa20f
UH
32/**
33 * Initialize libsigrokdecode.
34 *
70e44845 35 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f
UH
36 */
37int sigrokdecode_init(void)
38{
39 /* Py_Initialize() returns void and usually cannot fail. */
40 Py_Initialize();
41
2454527d
UH
42 /* Add some more search directories for convenience. */
43 /* FIXME: Check error code. */
70e44845 44 /* FIXME: What happens if this function is called multiple times? */
2454527d
UH
45 PyRun_SimpleString(
46 "import sys;"
9c93add5
UH
47 "sys.path.append('libsigrokdecode/decoders');"
48 "sys.path.append('../libsigrokdecode/decoders');"
2454527d
UH
49 "sys.path.append('/usr/local/share/sigrok');"
50 );
1c6fa20f 51
70e44845 52 return SIGROKDECODE_OK;
1c6fa20f
UH
53}
54
5c55017c
UH
55/**
56 * Helper function to handle Python strings.
57 *
58 * TODO: @param entries.
59 *
70e44845
UH
60 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
61 * The 'outstr' argument points to a malloc()ed string upon success.
5c55017c
UH
62 */
63static 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
70e44845 98 return SIGROKDECODE_OK;
5c55017c
UH
99}
100
1c6fa20f
UH
101/**
102 * TODO
103 *
104 * @param name TODO
70e44845
UH
105 *
106 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f 107 */
5c55017c
UH
108int sigrokdecode_load_decoder(const char *name,
109 struct sigrokdecode_decoder **dec)
1c6fa20f 110{
5c55017c
UH
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;
910e5fd8 177
70e44845 178 return SIGROKDECODE_OK;
1c6fa20f
UH
179}
180
181/**
182 * Run the specified decoder function.
183 *
5c55017c 184 * @param dec TODO
1c6fa20f
UH
185 * @param inbuf TODO
186 * @param inbuflen TODO
187 * @param outbuf TODO
188 * @param outbuflen TODO
70e44845
UH
189 *
190 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f 191 */
5c55017c 192int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
2b45dd71 193 uint8_t *inbuf, uint64_t inbuflen,
194 uint8_t **outbuf, uint64_t *outbuflen)
1c6fa20f 195{
5c55017c 196 PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
1c6fa20f
UH
197 int ret;
198
199 /* TODO: Use #defines for the return codes. */
200
201 /* Return an error upon unusable input. */
5c55017c
UH
202 if (dec == NULL)
203 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 204 if (inbuf == NULL)
5c55017c 205 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 206 if (inbuflen == 0) /* No point in working on empty buffers. */
5c55017c 207 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 208 if (outbuf == NULL)
5c55017c 209 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 210 if (outbuflen == NULL)
5c55017c 211 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
1c6fa20f 212
5c55017c
UH
213 /* TODO: Error handling. */
214 py_mod = dec->py_mod;
215 py_func = dec->py_func;
1c6fa20f 216
5c55017c 217 /* TODO: Really run Py_DECREF on py_mod/py_func? */
1c6fa20f
UH
218
219 /* Create a Python tuple of size 1. */
220 if (!(py_args = PyTuple_New(1))) {
221 PyErr_Print();
222 Py_DECREF(py_func);
5c55017c
UH
223 Py_DECREF(py_mod);
224 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
1c6fa20f
UH
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);
5c55017c
UH
233 Py_DECREF(py_mod);
234 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
1c6fa20f
UH
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);
5c55017c
UH
242 Py_DECREF(py_mod);
243 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
1c6fa20f
UH
244 }
245
5c55017c 246 if (!(py_res = PyObject_CallObject(py_func, py_args))) {
1c6fa20f
UH
247 PyErr_Print();
248 Py_DECREF(py_value);
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
5c55017c 255 if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
1c6fa20f
UH
256 (Py_ssize_t *)outbuflen))) {
257 PyErr_Print();
5c55017c 258 Py_DECREF(py_res);
1c6fa20f
UH
259 Py_DECREF(py_value);
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
5c55017c 266 Py_DECREF(py_res);
1c6fa20f
UH
267 // Py_DECREF(py_value);
268 Py_DECREF(py_args);
269 Py_DECREF(py_func);
5c55017c 270 Py_DECREF(py_mod);
1c6fa20f 271
70e44845 272 return SIGROKDECODE_OK;
1c6fa20f
UH
273}
274
275/**
276 * Shutdown libsigrokdecode.
277 *
70e44845 278 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
1c6fa20f
UH
279 */
280int sigrokdecode_shutdown(void)
281{
282 /* Py_Finalize() returns void, any finalization errors are ignored. */
283 Py_Finalize();
284
70e44845 285 return SIGROKDECODE_OK;
1c6fa20f 286}