]> sigrok.org Git - libsigrokdecode.git/blame - decode.c
sigrokdecode_run_decoder(): Pass in the module name.
[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>
23
24/**
25 * Initialize libsigrokdecode.
26 *
27 * @return 0 upon success, non-zero otherwise.
28 */
29int sigrokdecode_init(void)
30{
31 /* Py_Initialize() returns void and usually cannot fail. */
32 Py_Initialize();
33
2454527d
UH
34 /* Add some more search directories for convenience. */
35 /* FIXME: Check error code. */
36 PyRun_SimpleString(
37 "import sys;"
38 "sys.path.append('libsigrokdecode/scripts');"
39 "sys.path.append('../libsigrokdecode/scripts');"
40 "sys.path.append('/usr/local/share/sigrok');"
41 );
1c6fa20f
UH
42
43 return 0;
44}
45
46/**
47 * TODO
48 *
49 * @param name TODO
50 * @return 0 upon success, non-zero otherwise.
51 */
52int sigrokdecode_load_decoder_file(const char *name)
53{
910e5fd8
UH
54 /* QUICK HACK */
55 name = name;
56
1c6fa20f
UH
57 /* TODO */
58 return 0;
59}
60
61/**
62 * Run the specified decoder function.
63 *
64 * @param decodername TODO
65 * @param inbuf TODO
66 * @param inbuflen TODO
67 * @param outbuf TODO
68 * @param outbuflen TODO
69 * @return 0 upon success, non-zero otherwise.
70 */
2b45dd71 71int sigrokdecode_run_decoder(const char *modulename, const char *decodername,
72 uint8_t *inbuf, uint64_t inbuflen,
73 uint8_t **outbuf, uint64_t *outbuflen)
1c6fa20f 74{
1c6fa20f
UH
75 PyObject *py_name, *py_module, *py_func, *py_args;
76 PyObject *py_value, *py_result;
77 int ret;
78
79 /* TODO: Use #defines for the return codes. */
80
81 /* Return an error upon unusable input. */
82 if (decodername == NULL)
83 return -1;
84 if (inbuf == NULL)
85 return -2;
86 if (inbuflen == 0) /* No point in working on empty buffers. */
87 return -3;
88 if (outbuf == NULL)
89 return -4;
90 if (outbuflen == NULL)
91 return -5;
92
93 /* Get the name of the decoder module/file as Python string. */
2b45dd71 94 if (!(py_name = PyString_FromString(modulename))) {
1c6fa20f
UH
95 PyErr_Print();
96 return -6;
97 }
98
99 /* "Import" the python file/module. */
100 if (!(py_module = PyImport_Import(py_name))) {
101 PyErr_Print();
102 Py_DECREF(py_name);
103 return -7;
104 }
105 Py_DECREF(py_name);
106
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())
111 PyErr_Print();
112 Py_DECREF(py_module);
113 return -8;
114 }
115
116 /* Create a Python tuple of size 1. */
117 if (!(py_args = PyTuple_New(1))) {
118 PyErr_Print();
119 Py_DECREF(py_func);
120 Py_DECREF(py_module);
121 return -9;
122 }
123
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))) {
127 PyErr_Print();
128 Py_DECREF(py_args);
129 Py_DECREF(py_func);
130 Py_DECREF(py_module);
131 return -10;
132 }
133
134 if (PyTuple_SetItem(py_args, 0, py_value) != 0) {
135 PyErr_Print();
136 Py_DECREF(py_value);
137 Py_DECREF(py_args);
138 Py_DECREF(py_func);
139 Py_DECREF(py_module);
140 return -11;
141 }
142
143 if (!(py_result = PyObject_CallObject(py_func, py_args))) {
144 PyErr_Print();
145 Py_DECREF(py_value);
146 Py_DECREF(py_args);
147 Py_DECREF(py_func);
148 Py_DECREF(py_module);
149 return -12;
150 }
151
152 if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf,
153 (Py_ssize_t *)outbuflen))) {
154 PyErr_Print();
155 Py_DECREF(py_result);
156 Py_DECREF(py_value);
157 Py_DECREF(py_args);
158 Py_DECREF(py_func);
159 Py_DECREF(py_module);
160 return -13;
161 }
162
163 Py_DECREF(py_result);
164 // Py_DECREF(py_value);
165 Py_DECREF(py_args);
166 Py_DECREF(py_func);
167 Py_DECREF(py_module);
168
169 return 0;
170}
171
172/**
173 * Shutdown libsigrokdecode.
174 *
175 * @return 0 upon success, non-zero otherwise.
176 */
177int sigrokdecode_shutdown(void)
178{
179 /* Py_Finalize() returns void, any finalization errors are ignored. */
180 Py_Finalize();
181
182 return 0;
183}