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. */
27 /* Re-define some string functions for Python >= 3.0. */
28 #if PY_VERSION_HEX >= 0x03000000
29 #define PyString_AsString PyBytes_AsString
30 #define PyString_FromString PyBytes_FromString
31 #define PyString_Check PyBytes_Check
34 /* The list of protocol decoders. */
35 GSList *list_pds = NULL;
38 * Initialize libsigrokdecode.
40 * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise.
42 int sigrokdecode_init(void)
48 /* Py_Initialize() returns void and usually cannot fail. */
51 /* Add search directory for the protocol decoders. */
52 /* FIXME: Check error code. */
53 /* FIXME: What happens if this function is called multiple times? */
54 PyRun_SimpleString("import sys;"
55 "sys.path.append(r'" DECODERS_DIR "');");
57 if (!(dir = opendir(DECODERS_DIR)))
58 return SIGROKDECODE_ERR_DECODERS_DIR;
60 while ((dp = readdir(dir)) != NULL) {
61 if (!g_str_has_suffix(dp->d_name, ".py"))
63 /* For now use the filename (without .py) as decoder name. */
64 if ((tmp = g_strndup(dp->d_name, strlen(dp->d_name) - 3)))
65 list_pds = g_slist_append(list_pds, tmp);
69 return SIGROKDECODE_OK;
73 * Returns the list of supported/loaded protocol decoders.
75 * This is a GSList containing the names of the decoders as strings.
77 * @return List of decoders, NULL if none are supported or loaded.
79 GSList *sigrokdecode_list_decoders(void)
85 * Helper function to handle Python strings.
87 * TODO: @param entries.
89 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
90 * The 'outstr' argument points to a malloc()ed string upon success.
92 static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod,
93 const char *key, char **outstr)
98 py_str = PyMapping_GetItemString(py_res, (char *)key);
99 if (!py_str || !PyString_Check(py_str)) {
100 if (PyErr_Occurred())
104 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
107 if (!(str = PyString_AsString(py_str))) {
108 if (PyErr_Occurred())
113 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
116 if (!(*outstr = strdup(str))) {
117 if (PyErr_Occurred())
122 return SIGROKDECODE_ERR_MALLOC;
127 return SIGROKDECODE_OK;
135 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
137 int sigrokdecode_load_decoder(const char *name,
138 struct sigrokdecode_decoder **dec)
140 struct sigrokdecode_decoder *d;
141 PyObject *py_name, *py_mod, *py_func, *py_res /* , *py_tuple */;
144 /* Get the name of the decoder module as Python string. */
145 if (!(py_name = PyString_FromString(name))) {
147 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
150 /* "Import" the Python module. */
151 if (!(py_mod = PyImport_Import(py_name))) {
154 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
158 /* Get the 'register' function name as Python callable object. */
159 py_func = PyObject_GetAttrString(py_mod, "register");
160 if (!py_func || !PyCallable_Check(py_func)) {
161 if (PyErr_Occurred())
164 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
167 /* Call the 'register' function without arguments, get the result. */
168 if (!(py_res = PyObject_CallFunction(py_func, NULL))) {
172 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
175 if (!(d = malloc(sizeof(struct sigrokdecode_decoder))))
176 return SIGROKDECODE_ERR_MALLOC;
178 if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0)
181 if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0)
184 if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0)
192 /* Get the 'decode' function name as Python callable object. */
193 py_func = PyObject_GetAttrString(py_mod, "decode");
194 if (!py_func || !PyCallable_Check(py_func)) {
195 if (PyErr_Occurred())
198 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
201 d->py_func = py_func;
203 /* TODO: Handle inputformats, outputformats. */
207 return SIGROKDECODE_OK;
211 * Run the specified decoder function.
215 * @param inbuflen TODO
217 * @param outbuflen TODO
219 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
221 int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec,
222 uint8_t *inbuf, uint64_t inbuflen,
223 uint8_t **outbuf, uint64_t *outbuflen)
225 PyObject *py_mod, *py_func, *py_args, *py_value, *py_res;
228 /* TODO: Use #defines for the return codes. */
230 /* Return an error upon unusable input. */
232 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
234 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
235 if (inbuflen == 0) /* No point in working on empty buffers. */
236 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
238 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
239 if (outbuflen == NULL)
240 return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */
242 /* TODO: Error handling. */
243 py_mod = dec->py_mod;
244 py_func = dec->py_func;
246 /* TODO: Really run Py_DECREF on py_mod/py_func? */
248 /* Create a Python tuple of size 1. */
249 if (!(py_args = PyTuple_New(1))) {
253 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
256 /* Get the input buffer as Python "string" (byte array). */
257 /* TODO: int vs. uint64_t for 'inbuflen'? */
258 if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) {
263 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
266 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
272 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
275 if (!(py_res = PyObject_CallObject(py_func, py_args))) {
281 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
284 if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf,
285 (Py_ssize_t *)outbuflen))) {
292 return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
296 // Py_DECREF(py_value);
301 return SIGROKDECODE_OK;
305 * Shutdown libsigrokdecode.
307 * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise.
309 int sigrokdecode_shutdown(void)
311 /* Py_Finalize() returns void, any finalization errors are ignored. */
314 return SIGROKDECODE_OK;