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 * Initialize libsigrokdecode.
27 * @return 0 upon success, non-zero otherwise.
29 int sigrokdecode_init(void)
31 /* Py_Initialize() returns void and usually cannot fail. */
34 /* Add some more search directories for convenience. */
35 /* FIXME: Check error code. */
38 "sys.path.append('libsigrokdecode/scripts');"
39 "sys.path.append('../libsigrokdecode/scripts');"
40 "sys.path.append('/usr/local/share/sigrok');"
50 * @return 0 upon success, non-zero otherwise.
52 int sigrokdecode_load_decoder_file(const char *name)
62 * Run the specified decoder function.
64 * @param decodername TODO
66 * @param inbuflen TODO
68 * @param outbuflen TODO
69 * @return 0 upon success, non-zero otherwise.
71 int sigrokdecode_run_decoder(const char *modulename, const char *decodername,
72 uint8_t *inbuf, uint64_t inbuflen,
73 uint8_t **outbuf, uint64_t *outbuflen)
75 PyObject *py_name, *py_module, *py_func, *py_args;
76 PyObject *py_value, *py_result;
79 /* TODO: Use #defines for the return codes. */
81 /* Return an error upon unusable input. */
82 if (decodername == NULL)
86 if (inbuflen == 0) /* No point in working on empty buffers. */
90 if (outbuflen == NULL)
93 /* Get the name of the decoder module/file as Python string. */
94 if (!(py_name = PyString_FromString(modulename))) {
99 /* "Import" the python file/module. */
100 if (!(py_module = PyImport_Import(py_name))) {
107 /* Get the decoder/function name as Python callable object. */
108 py_func = PyObject_GetAttrString(py_module, decodername);
109 if (!py_func || !PyCallable_Check(py_func)) {
110 if (PyErr_Occurred())
112 Py_DECREF(py_module);
116 /* Create a Python tuple of size 1. */
117 if (!(py_args = PyTuple_New(1))) {
120 Py_DECREF(py_module);
124 /* Get the input buffer as Python "string" (byte array). */
125 /* TODO: int vs. uint64_t for 'inbuflen'? */
126 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
130 Py_DECREF(py_module);
134 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
139 Py_DECREF(py_module);
143 if (!(py_result = PyObject_CallObject(py_func, py_args))) {
148 Py_DECREF(py_module);
152 if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf,
153 (Py_ssize_t *)outbuflen))) {
155 Py_DECREF(py_result);
159 Py_DECREF(py_module);
163 Py_DECREF(py_result);
164 // Py_DECREF(py_value);
167 Py_DECREF(py_module);
173 * Shutdown libsigrokdecode.
175 * @return 0 upon success, non-zero otherwise.
177 int sigrokdecode_shutdown(void)
179 /* Py_Finalize() returns void, any finalization errors are ignored. */