2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
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
33 * Initialize libsigrokdecode.
35 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
37 int sigrokdecode_init(void)
39 /* Py_Initialize() returns void and usually cannot fail. */
42 /* Add some more search directories for convenience. */
43 /* FIXME: Check error code. */
44 /* FIXME: What happens if this function is called multiple times? */
47 "sys.path.append('libsigrokdecode/decoders');"
48 "sys.path.append('../libsigrokdecode/decoders');"
49 "sys.path.append('/usr/local/share/sigrok');"
52 return SIGROKDECODE_OK;
56 * Helper function to handle Python strings.
58 * TODO: @param entries.
60 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
61 * The 'outstr' argument points to a malloc()ed string upon success.
63 static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod,
64 const char *key, char **outstr)
69 py_str = PyMapping_GetItemString(py_res, (char *)key);
70 if (!py_str || !PyString_Check(py_str)) {
75 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
78 if (!(str = PyString_AsString(py_str))) {
84 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
87 if (!(*outstr = strdup(str))) {
93 return SIGROKDECODE_ERR_MALLOC;
98 return SIGROKDECODE_OK;
106 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
108 int sigrokdecode_load_decoder(const char *name,
109 struct sigrokdecode_decoder **dec)
111 struct sigrokdecode_decoder *d;
112 PyObject *py_name, *py_mod, *py_func, *py_res /* , *py_tuple */;
115 /* Get the name of the decoder module as Python string. */
116 if (!(py_name = PyString_FromString(name))) {
118 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
121 /* "Import" the Python module. */
122 if (!(py_mod = PyImport_Import(py_name))) {
125 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
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())
135 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
138 /* Call the 'register' function without arguments, get the result. */
139 if (!(py_res = PyObject_CallFunction(py_func, NULL))) {
143 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
146 if (!(d = malloc(sizeof(struct sigrokdecode_decoder))))
147 return SIGROKDECODE_ERR_MALLOC;
149 if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0)
152 if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0)
155 if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0)
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())
169 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
172 d->py_func = py_func;
174 /* TODO: Handle inputformats, outputformats. */
178 return SIGROKDECODE_OK;
182 * Run the specified decoder function.
186 * @param inbuflen TODO
188 * @param outbuflen TODO
190 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
192 int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
193 uint8_t *inbuf, uint64_t inbuflen,
194 uint8_t **outbuf, uint64_t *outbuflen)
196 PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
199 /* TODO: Use #defines for the return codes. */
201 /* Return an error upon unusable input. */
203 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
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? */
209 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
210 if (outbuflen == NULL)
211 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
213 /* TODO: Error handling. */
214 py_mod = dec->py_mod;
215 py_func = dec->py_func;
217 /* TODO: Really run Py_DECREF on py_mod/py_func? */
219 /* Create a Python tuple of size 1. */
220 if (!(py_args = PyTuple_New(1))) {
224 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
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))) {
234 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
237 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
243 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
246 if (!(py_res = PyObject_CallObject(py_func, py_args))) {
252 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
255 if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
256 (Py_ssize_t *)outbuflen))) {
263 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
267 // Py_DECREF(py_value);
272 return SIGROKDECODE_OK;
276 * Shutdown libsigrokdecode.
278 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
280 int sigrokdecode_shutdown(void)
282 /* Py_Finalize() returns void, any finalization errors are ignored. */
285 return SIGROKDECODE_OK;