]>
Commit | Line | Data |
---|---|---|
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 | #include <string.h> | |
24 | ||
25 | /** | |
26 | * Initialize libsigrokdecode. | |
27 | * | |
28 | * @return SIGROKDECODE_OK upon success, a (negative) error code otherwise. | |
29 | */ | |
30 | int sigrokdecode_init(void) | |
31 | { | |
32 | /* Py_Initialize() returns void and usually cannot fail. */ | |
33 | Py_Initialize(); | |
34 | ||
35 | /* Add some more search directories for convenience. */ | |
36 | /* FIXME: Check error code. */ | |
37 | /* FIXME: What happens if this function is called multiple times? */ | |
38 | PyRun_SimpleString( | |
39 | "import sys;" | |
40 | "sys.path.append('libsigrokdecode/scripts');" | |
41 | "sys.path.append('../libsigrokdecode/scripts');" | |
42 | "sys.path.append('/usr/local/share/sigrok');" | |
43 | ); | |
44 | ||
45 | return SIGROKDECODE_OK; | |
46 | } | |
47 | ||
48 | /** | |
49 | * Helper function to handle Python strings. | |
50 | * | |
51 | * TODO: @param entries. | |
52 | * | |
53 | * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise. | |
54 | * The 'outstr' argument points to a malloc()ed string upon success. | |
55 | */ | |
56 | static int h_str(PyObject *py_res, PyObject *py_func, PyObject *py_mod, | |
57 | const char *key, char **outstr) | |
58 | { | |
59 | PyObject *py_str; | |
60 | char *str; | |
61 | ||
62 | py_str = PyMapping_GetItemString(py_res, (char *)key); | |
63 | if (!py_str || !PyString_Check(py_str)) { | |
64 | if (PyErr_Occurred()) | |
65 | PyErr_Print(); | |
66 | Py_DECREF(py_func); | |
67 | Py_DECREF(py_mod); | |
68 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
69 | } | |
70 | ||
71 | if (!(str = PyString_AsString(py_str))) { | |
72 | if (PyErr_Occurred()) | |
73 | PyErr_Print(); | |
74 | Py_DECREF(py_str); | |
75 | Py_DECREF(py_func); | |
76 | Py_DECREF(py_mod); | |
77 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
78 | } | |
79 | ||
80 | if (!(*outstr = strdup(str))) { | |
81 | if (PyErr_Occurred()) | |
82 | PyErr_Print(); | |
83 | Py_DECREF(py_str); | |
84 | Py_DECREF(py_func); | |
85 | Py_DECREF(py_mod); | |
86 | return SIGROKDECODE_ERR_MALLOC; | |
87 | } | |
88 | ||
89 | Py_DECREF(py_str); | |
90 | ||
91 | return SIGROKDECODE_OK; | |
92 | } | |
93 | ||
94 | /** | |
95 | * TODO | |
96 | * | |
97 | * @param name TODO | |
98 | * | |
99 | * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise. | |
100 | */ | |
101 | int sigrokdecode_load_decoder(const char *name, | |
102 | struct sigrokdecode_decoder **dec) | |
103 | { | |
104 | struct sigrokdecode_decoder *d; | |
105 | PyObject *py_name, *py_mod, *py_func, *py_res /* , *py_tuple */; | |
106 | int r; | |
107 | ||
108 | /* Get the name of the decoder module as Python string. */ | |
109 | if (!(py_name = PyString_FromString(name))) { | |
110 | PyErr_Print(); | |
111 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
112 | } | |
113 | ||
114 | /* "Import" the Python module. */ | |
115 | if (!(py_mod = PyImport_Import(py_name))) { | |
116 | PyErr_Print(); | |
117 | Py_DECREF(py_name); | |
118 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
119 | } | |
120 | Py_DECREF(py_name); | |
121 | ||
122 | /* Get the 'register' function name as Python callable object. */ | |
123 | py_func = PyObject_GetAttrString(py_mod, "register"); | |
124 | if (!py_func || !PyCallable_Check(py_func)) { | |
125 | if (PyErr_Occurred()) | |
126 | PyErr_Print(); | |
127 | Py_DECREF(py_mod); | |
128 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
129 | } | |
130 | ||
131 | /* Call the 'register' function without arguments, get the result. */ | |
132 | if (!(py_res = PyObject_CallFunction(py_func, NULL))) { | |
133 | PyErr_Print(); | |
134 | Py_DECREF(py_func); | |
135 | Py_DECREF(py_mod); | |
136 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
137 | } | |
138 | ||
139 | if (!(d = malloc(sizeof(struct sigrokdecode_decoder)))) | |
140 | return SIGROKDECODE_ERR_MALLOC; | |
141 | ||
142 | if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0) | |
143 | return r; | |
144 | ||
145 | if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0) | |
146 | return r; | |
147 | ||
148 | if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0) | |
149 | return r; | |
150 | ||
151 | d->py_mod = py_mod; | |
152 | ||
153 | Py_DECREF(py_res); | |
154 | Py_DECREF(py_func); | |
155 | ||
156 | /* Get the 'decode' function name as Python callable object. */ | |
157 | py_func = PyObject_GetAttrString(py_mod, "decode"); | |
158 | if (!py_func || !PyCallable_Check(py_func)) { | |
159 | if (PyErr_Occurred()) | |
160 | PyErr_Print(); | |
161 | Py_DECREF(py_mod); | |
162 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
163 | } | |
164 | ||
165 | d->py_func = py_func; | |
166 | ||
167 | /* TODO: Handle inputformats, outputformats. */ | |
168 | ||
169 | *dec = d; | |
170 | ||
171 | return SIGROKDECODE_OK; | |
172 | } | |
173 | ||
174 | /** | |
175 | * Run the specified decoder function. | |
176 | * | |
177 | * @param dec TODO | |
178 | * @param inbuf TODO | |
179 | * @param inbuflen TODO | |
180 | * @param outbuf TODO | |
181 | * @param outbuflen TODO | |
182 | * | |
183 | * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise. | |
184 | */ | |
185 | int sigrokdecode_run_decoder(struct sigrokdecode_decoder *dec, | |
186 | uint8_t *inbuf, uint64_t inbuflen, | |
187 | uint8_t **outbuf, uint64_t *outbuflen) | |
188 | { | |
189 | PyObject *py_mod, *py_func, *py_args, *py_value, *py_res; | |
190 | int ret; | |
191 | ||
192 | /* TODO: Use #defines for the return codes. */ | |
193 | ||
194 | /* Return an error upon unusable input. */ | |
195 | if (dec == NULL) | |
196 | return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */ | |
197 | if (inbuf == NULL) | |
198 | return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */ | |
199 | if (inbuflen == 0) /* No point in working on empty buffers. */ | |
200 | return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */ | |
201 | if (outbuf == NULL) | |
202 | return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */ | |
203 | if (outbuflen == NULL) | |
204 | return SIGROKDECODE_ERR_ARGS; /* TODO: More specific error? */ | |
205 | ||
206 | /* TODO: Error handling. */ | |
207 | py_mod = dec->py_mod; | |
208 | py_func = dec->py_func; | |
209 | ||
210 | /* TODO: Really run Py_DECREF on py_mod/py_func? */ | |
211 | ||
212 | /* Create a Python tuple of size 1. */ | |
213 | if (!(py_args = PyTuple_New(1))) { | |
214 | PyErr_Print(); | |
215 | Py_DECREF(py_func); | |
216 | Py_DECREF(py_mod); | |
217 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
218 | } | |
219 | ||
220 | /* Get the input buffer as Python "string" (byte array). */ | |
221 | /* TODO: int vs. uint64_t for 'inbuflen'? */ | |
222 | if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) { | |
223 | PyErr_Print(); | |
224 | Py_DECREF(py_args); | |
225 | Py_DECREF(py_func); | |
226 | Py_DECREF(py_mod); | |
227 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
228 | } | |
229 | ||
230 | if (PyTuple_SetItem(py_args, 0, py_value) != 0) { | |
231 | PyErr_Print(); | |
232 | Py_DECREF(py_value); | |
233 | Py_DECREF(py_args); | |
234 | Py_DECREF(py_func); | |
235 | Py_DECREF(py_mod); | |
236 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
237 | } | |
238 | ||
239 | if (!(py_res = PyObject_CallObject(py_func, py_args))) { | |
240 | PyErr_Print(); | |
241 | Py_DECREF(py_value); | |
242 | Py_DECREF(py_args); | |
243 | Py_DECREF(py_func); | |
244 | Py_DECREF(py_mod); | |
245 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
246 | } | |
247 | ||
248 | if ((ret = PyObject_AsCharBuffer(py_res, (const char **)outbuf, | |
249 | (Py_ssize_t *)outbuflen))) { | |
250 | PyErr_Print(); | |
251 | Py_DECREF(py_res); | |
252 | Py_DECREF(py_value); | |
253 | Py_DECREF(py_args); | |
254 | Py_DECREF(py_func); | |
255 | Py_DECREF(py_mod); | |
256 | return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */ | |
257 | } | |
258 | ||
259 | Py_DECREF(py_res); | |
260 | // Py_DECREF(py_value); | |
261 | Py_DECREF(py_args); | |
262 | Py_DECREF(py_func); | |
263 | Py_DECREF(py_mod); | |
264 | ||
265 | return SIGROKDECODE_OK; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Shutdown libsigrokdecode. | |
270 | * | |
271 | * @return LIBSIGROKDECODE_OK upon success, a (negative) error code otherwise. | |
272 | */ | |
273 | int sigrokdecode_shutdown(void) | |
274 | { | |
275 | /* Py_Finalize() returns void, any finalization errors are ignored. */ | |
276 | Py_Finalize(); | |
277 | ||
278 | return SIGROKDECODE_OK; | |
279 | } |