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 *decodername, uint8_t *inbuf,
72 uint64_t inbuflen, uint8_t **outbuf,
75 const char *decoder_filename = "transitioncounter"; /* FIXME */
76 // const char *decoder_filename = "i2c"; /* FIXME */
77 PyObject *py_name, *py_module, *py_func, *py_args;
78 PyObject *py_value, *py_result;
81 /* TODO: Use #defines for the return codes. */
83 /* Return an error upon unusable input. */
84 if (decodername == NULL)
88 if (inbuflen == 0) /* No point in working on empty buffers. */
92 if (outbuflen == NULL)
95 /* Get the name of the decoder module/file as Python string. */
96 if (!(py_name = PyString_FromString(decoder_filename))) {
101 /* "Import" the python file/module. */
102 if (!(py_module = PyImport_Import(py_name))) {
109 /* Get the decoder/function name as Python callable object. */
110 py_func = PyObject_GetAttrString(py_module, decodername);
111 if (!py_func || !PyCallable_Check(py_func)) {
112 if (PyErr_Occurred())
114 Py_DECREF(py_module);
118 /* Create a Python tuple of size 1. */
119 if (!(py_args = PyTuple_New(1))) {
122 Py_DECREF(py_module);
126 /* Get the input buffer as Python "string" (byte array). */
127 /* TODO: int vs. uint64_t for 'inbuflen'? */
128 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
132 Py_DECREF(py_module);
136 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
141 Py_DECREF(py_module);
145 if (!(py_result = PyObject_CallObject(py_func, py_args))) {
150 Py_DECREF(py_module);
154 if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf,
155 (Py_ssize_t *)outbuflen))) {
157 Py_DECREF(py_result);
161 Py_DECREF(py_module);
165 Py_DECREF(py_result);
166 // Py_DECREF(py_value);
169 Py_DECREF(py_module);
175 * Shutdown libsigrokdecode.
177 * @return 0 upon success, non-zero otherwise.
179 int sigrokdecode_shutdown(void)
181 /* Py_Finalize() returns void, any finalization errors are ignored. */