]>
Commit | Line | Data |
---|---|---|
b2c19614 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
bc5f5a43 | 5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
b2c19614 BV |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
73191416 | 21 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
7ce7775c BV |
22 | #include "sigrokdecode-internal.h" |
23 | #include "config.h" | |
b2c19614 | 24 | #include <glib.h> |
1aef2f93 | 25 | #include <inttypes.h> |
0bdadba2 | 26 | #include <stdlib.h> |
b2c19614 | 27 | |
b2c19614 | 28 | |
7ce7775c | 29 | static GSList *di_list = NULL; |
983cb0f5 BV |
30 | static GSList *callbacks = NULL; |
31 | ||
bc5f5a43 BV |
32 | /* lives in decoder.c */ |
33 | extern GSList *pd_list; | |
b2c19614 | 34 | |
bc5f5a43 BV |
35 | /* lives in module_sigrokdecode.c */ |
36 | extern PyMODINIT_FUNC PyInit_sigrokdecode(void); | |
b2c19614 | 37 | |
bc5f5a43 BV |
38 | /* lives in type_logic.c */ |
39 | extern PyTypeObject srd_logic_type; | |
b2c19614 | 40 | |
e5080882 | 41 | |
b2c19614 BV |
42 | /** |
43 | * Initialize libsigrokdecode. | |
44 | * | |
45 | * This initializes the Python interpreter, and creates and initializes | |
46 | * a "sigrok" Python module with a single put() method. | |
47 | * | |
48 | * Then, it searches for sigrok protocol decoder files (*.py) in the | |
49 | * "decoders" subdirectory of the the sigrok installation directory. | |
50 | * All decoders that are found are loaded into memory and added to an | |
51 | * internal list of decoders, which can be queried via srd_list_decoders(). | |
52 | * | |
53 | * The caller is responsible for calling the clean-up function srd_exit(), | |
54 | * which will properly shut down libsigrokdecode and free its allocated memory. | |
55 | * | |
7ce7775c | 56 | * Multiple calls to srd_init(), without calling srd_exit() in between, |
b2c19614 BV |
57 | * are not allowed. |
58 | * | |
59 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
60 | * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders | |
61 | * directory cannot be accessed, return SRD_ERR_DECODERS_DIR. | |
62 | * If not enough memory could be allocated, return SRD_ERR_MALLOC. | |
63 | */ | |
64 | int srd_init(void) | |
65 | { | |
66 | int ret; | |
67 | ||
bc5f5a43 | 68 | PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode); |
5f802ec6 | 69 | |
b2c19614 BV |
70 | /* Py_Initialize() returns void and usually cannot fail. */ |
71 | Py_Initialize(); | |
72 | ||
b2c19614 BV |
73 | if ((ret = set_modulepath()) != SRD_OK) { |
74 | Py_Finalize(); | |
75 | return ret; | |
76 | } | |
77 | ||
78 | if ((ret = srd_load_all_decoders()) != SRD_OK) { | |
79 | Py_Finalize(); | |
80 | return ret; | |
81 | } | |
82 | ||
83 | return SRD_OK; | |
84 | } | |
85 | ||
86 | ||
87 | /** | |
88 | * Shutdown libsigrokdecode. | |
89 | * | |
90 | * This frees all the memory allocated for protocol decoders and shuts down | |
91 | * the Python interpreter. | |
92 | * | |
93 | * This function should only be called if there was a (successful!) invocation | |
94 | * of srd_init() before. Calling this function multiple times in a row, without | |
7ce7775c | 95 | * any successful srd_init() calls in between, is not allowed. |
b2c19614 BV |
96 | * |
97 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
98 | */ | |
99 | int srd_exit(void) | |
100 | { | |
101 | /* Unload/free all decoders, and then the list of decoders itself. */ | |
102 | /* TODO: Error handling. */ | |
103 | srd_unload_all_decoders(); | |
e5080882 | 104 | g_slist_free(pd_list); |
b2c19614 BV |
105 | |
106 | /* Py_Finalize() returns void, any finalization errors are ignored. */ | |
107 | Py_Finalize(); | |
108 | ||
109 | return SRD_OK; | |
110 | } | |
111 | ||
112 | ||
113 | /** | |
114 | * Add search directories for the protocol decoders. | |
115 | * | |
116 | * TODO: add path from env var SIGROKDECODE_PATH, config etc | |
639c5689 | 117 | * TODO: Should take directoryname/path as input. |
b2c19614 BV |
118 | */ |
119 | int set_modulepath(void) | |
120 | { | |
121 | int ret; | |
639c5689 | 122 | gchar *path, *s; |
b2c19614 | 123 | |
639c5689 UH |
124 | #ifdef _WIN32 |
125 | gchar **splitted; | |
126 | ||
127 | /* | |
128 | * On Windows/MinGW, Python's sys.path needs entries of the form | |
129 | * 'C:\\foo\\bar' instead of '/foo/bar'. | |
130 | */ | |
131 | ||
132 | splitted = g_strsplit(DECODERS_DIR, "/", 0); | |
133 | path = g_build_pathv("\\\\", splitted); | |
134 | g_strfreev(splitted); | |
135 | #else | |
136 | path = g_strdup(DECODERS_DIR); | |
137 | #endif | |
138 | ||
139 | /* TODO: Prepend instead of appending. */ | |
140 | /* TODO: Sanity check on 'path' (length, escape special chars, ...). */ | |
141 | s = g_strdup_printf("import sys; sys.path.append(r'%s')", path); | |
142 | ||
143 | ret = PyRun_SimpleString(s); | |
144 | ||
145 | g_free(path); | |
146 | g_free(s); | |
b2c19614 BV |
147 | |
148 | return ret; | |
149 | } | |
150 | ||
151 | ||
7ce7775c | 152 | /** |
0bdadba2 BV |
153 | * Set options in a decoder instance. |
154 | * | |
155 | * @param di Decoder instance. | |
156 | * @param options A GHashTable of options to set. | |
7ce7775c | 157 | * |
0bdadba2 BV |
158 | * Handled options are removed from the hash. |
159 | * | |
160 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
161 | */ | |
162 | int srd_instance_set_options(struct srd_decoder_instance *di, | |
163 | GHashTable *options) | |
164 | { | |
165 | PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval; | |
166 | PyObject *py_optlist, *py_classval; | |
167 | Py_UNICODE *py_ustr; | |
168 | unsigned long long int val_ull; | |
169 | int num_optkeys, ret, size, i; | |
170 | char *key, *value; | |
171 | ||
e431d9cc | 172 | if(!PyObject_HasAttrString(di->decoder->py_dec, "options")) { |
0bdadba2 | 173 | /* Decoder has no options. */ |
e431d9cc BV |
174 | if (g_hash_table_size(options) == 0) { |
175 | /* No options provided. */ | |
176 | return SRD_OK; | |
177 | } else { | |
178 | srd_err("Protocol decoder has no options."); | |
179 | return SRD_ERR_ARG; | |
180 | } | |
0bdadba2 | 181 | return SRD_OK; |
e431d9cc | 182 | } |
0bdadba2 BV |
183 | |
184 | ret = SRD_ERR_PYTHON; | |
185 | key = NULL; | |
186 | py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL; | |
187 | py_optlist = py_classval = NULL; | |
188 | py_dec_options = PyObject_GetAttrString(di->decoder->py_dec, "options"); | |
0bdadba2 BV |
189 | |
190 | /* All of these are synthesized objects, so they're good. */ | |
191 | py_dec_optkeys = PyDict_Keys(py_dec_options); | |
192 | num_optkeys = PyList_Size(py_dec_optkeys); | |
193 | if (!(py_di_options = PyObject_GetAttrString(di->py_instance, "options"))) | |
194 | goto err_out; | |
195 | for (i = 0; i < num_optkeys; i++) { | |
196 | /* Get the default class value for this option. */ | |
197 | py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key); | |
198 | if (!(py_optlist = PyDict_GetItemString(py_dec_options, key))) | |
199 | goto err_out; | |
200 | if (!(py_classval = PyList_GetItem(py_optlist, 1))) | |
201 | goto err_out; | |
130ef08a BV |
202 | if (!PyUnicode_Check(py_classval) && !PyLong_Check(py_classval)) { |
203 | srd_err("Options of type %s are not yet supported.", Py_TYPE(py_classval)->tp_name); | |
204 | goto err_out; | |
205 | } | |
0bdadba2 BV |
206 | |
207 | if ((value = g_hash_table_lookup(options, key))) { | |
208 | /* An override for this option was provided. */ | |
209 | if (PyUnicode_Check(py_classval)) { | |
210 | if (!(py_optval = PyUnicode_FromString(value))) { | |
211 | /* Some UTF-8 encoding error. */ | |
212 | PyErr_Clear(); | |
213 | goto err_out; | |
214 | } | |
215 | } else if (PyLong_Check(py_classval)) { | |
216 | if (!(py_optval = PyLong_FromString(value, NULL, 0))) { | |
217 | /* ValueError Exception */ | |
218 | PyErr_Clear(); | |
219 | srd_err("Option %s has invalid value %s: expected integer", | |
220 | key, value); | |
221 | goto err_out; | |
222 | } | |
223 | } | |
224 | g_hash_table_remove(options, key); | |
225 | } else { | |
226 | /* Use the class default for this option. */ | |
227 | if (PyUnicode_Check(py_classval)) { | |
228 | /* Make a brand new copy of the string. */ | |
229 | py_ustr = PyUnicode_AS_UNICODE(py_classval); | |
230 | size = PyUnicode_GET_SIZE(py_classval); | |
231 | py_optval = PyUnicode_FromUnicode(py_ustr, size); | |
232 | } else if (PyLong_Check(py_classval)) { | |
233 | /* Make a brand new copy of the integer. */ | |
234 | val_ull = PyLong_AsUnsignedLongLong(py_classval); | |
235 | if (val_ull == (unsigned long long)-1) { | |
236 | /* OverFlowError exception */ | |
237 | PyErr_Clear(); | |
238 | srd_err("Invalid integer value for %s: expected integer", key); | |
239 | goto err_out; | |
240 | } | |
241 | if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull))) | |
242 | goto err_out; | |
243 | } | |
244 | } | |
245 | ||
246 | /* If we got here, py_optval holds a known good new reference | |
247 | * to the instance option to set. | |
248 | */ | |
249 | if (PyDict_SetItemString(py_di_options, key, py_optval) == -1) | |
250 | goto err_out; | |
251 | } | |
252 | ||
253 | ret = SRD_OK; | |
254 | ||
255 | err_out: | |
256 | Py_XDECREF(py_optlist); | |
257 | Py_XDECREF(py_di_options); | |
258 | Py_XDECREF(py_dec_optkeys); | |
259 | Py_XDECREF(py_dec_options); | |
260 | if (key) | |
261 | g_free(key); | |
262 | if (PyErr_Occurred()) { | |
263 | srd_dbg("stray exception!"); | |
264 | PyErr_Print(); | |
265 | PyErr_Clear(); | |
266 | } | |
267 | ||
268 | return ret; | |
269 | } | |
270 | ||
f38ec285 BV |
271 | static gint compare_probe_id(struct srd_probe *a, char *probe_id) |
272 | { | |
273 | ||
274 | return strcmp(a->id, probe_id); | |
275 | } | |
276 | ||
0bdadba2 BV |
277 | /** |
278 | * Set probes in a decoder instance. | |
279 | * | |
280 | * @param di Decoder instance. | |
281 | * @param probes A GHashTable of probes to set. Key is probe name, value is | |
282 | * the probe number. Samples passed to this instance will be arranged in this | |
283 | * order. | |
284 | * | |
0bdadba2 BV |
285 | * @return SRD_OK upon success, a (negative) error code otherwise. |
286 | */ | |
287 | int srd_instance_set_probes(struct srd_decoder_instance *di, | |
f38ec285 | 288 | GHashTable *new_probes) |
0bdadba2 | 289 | { |
f38ec285 BV |
290 | GList *l; |
291 | GSList *sl; | |
292 | struct srd_probe *p; | |
293 | int *new_probemap, new_probenum; | |
294 | char *probe_id; | |
0bdadba2 | 295 | |
f38ec285 | 296 | if (g_hash_table_size(new_probes) == 0) |
0bdadba2 BV |
297 | /* No probes provided. */ |
298 | return SRD_OK; | |
299 | ||
f38ec285 BV |
300 | if(!PyObject_HasAttrString(di->decoder->py_dec, "probes") |
301 | && !PyObject_HasAttrString(di->decoder->py_dec, "extra_probes")) { | |
0bdadba2 | 302 | /* Decoder has no probes. */ |
f38ec285 BV |
303 | srd_err("Protocol decoder %s has no probes to define.", |
304 | di->decoder->name); | |
305 | return SRD_ERR_ARG; | |
306 | } | |
0bdadba2 | 307 | |
f38ec285 | 308 | new_probemap = NULL; |
0bdadba2 | 309 | |
f38ec285 BV |
310 | if (!(new_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) { |
311 | srd_err("Failed to malloc new probe map."); | |
312 | return SRD_ERR_MALLOC; | |
0bdadba2 BV |
313 | } |
314 | ||
f38ec285 BV |
315 | for (l = g_hash_table_get_keys(new_probes); l; l = l->next) { |
316 | probe_id = l->data; | |
317 | new_probenum = strtol(g_hash_table_lookup(new_probes, probe_id), NULL, 10); | |
318 | if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id, | |
319 | (GCompareFunc)compare_probe_id))) { | |
320 | /* Fall back on optional probes. */ | |
321 | if (!(sl = g_slist_find_custom(di->decoder->extra_probes, | |
322 | probe_id, (GCompareFunc)compare_probe_id))) { | |
323 | srd_err("Protocol decoder %s has no probe '%s'", | |
324 | di->decoder->name, probe_id); | |
325 | g_free(new_probemap); | |
326 | return SRD_ERR_ARG; | |
327 | } | |
328 | } | |
329 | p = sl->data; | |
330 | new_probemap[p->order] = new_probenum; | |
331 | } | |
332 | g_free(di->dec_probemap); | |
333 | di->dec_probemap = new_probemap; | |
0bdadba2 | 334 | |
f38ec285 | 335 | return SRD_OK; |
0bdadba2 BV |
336 | } |
337 | ||
338 | /** | |
339 | * Create a new protocol decoder instance. | |
7ce7775c | 340 | * |
42a85ed0 | 341 | * @param id Decoder 'id' field. |
0bdadba2 BV |
342 | * @param options GHashtable of options which override the defaults set in |
343 | * the decoder class. | |
42a85ed0 | 344 | * @return Pointer to a newly allocated struct srd_decoder_instance, or |
0bdadba2 | 345 | * NULL in case of failure. |
7ce7775c | 346 | */ |
0bdadba2 BV |
347 | struct srd_decoder_instance *srd_instance_new(const char *decoder_id, |
348 | GHashTable *options) | |
b2c19614 BV |
349 | { |
350 | struct srd_decoder *dec; | |
351 | struct srd_decoder_instance *di; | |
f38ec285 | 352 | int i; |
0bdadba2 | 353 | char *instance_id; |
b2c19614 | 354 | |
0bdadba2 | 355 | srd_dbg("%s: creating new %s instance", __func__, decoder_id); |
7ce7775c | 356 | |
0bdadba2 BV |
357 | if (!(dec = srd_get_decoder_by_id(decoder_id))) { |
358 | srd_err("Protocol decoder %s not found.", decoder_id); | |
b2c19614 | 359 | return NULL; |
0bdadba2 | 360 | } |
b2c19614 | 361 | |
0bdadba2 BV |
362 | if (!(di = g_try_malloc0(sizeof(*di)))) { |
363 | srd_err("Failed to malloc instance."); | |
7ce7775c BV |
364 | return NULL; |
365 | } | |
0bdadba2 BV |
366 | |
367 | instance_id = g_hash_table_lookup(options, "id"); | |
b2c19614 | 368 | di->decoder = dec; |
0bdadba2 BV |
369 | di->instance_id = g_strdup(instance_id ? instance_id : decoder_id); |
370 | g_hash_table_remove(options, "id"); | |
b2c19614 | 371 | |
f38ec285 BV |
372 | /* Prepare a default probe map, where samples come in the |
373 | * order in which the decoder class defined them. | |
374 | */ | |
375 | di->dec_num_probes = g_slist_length(di->decoder->probes) + | |
376 | g_slist_length(di->decoder->extra_probes); | |
377 | if (!(di->dec_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) { | |
378 | srd_err("Failed to malloc probe map."); | |
379 | g_free(di); | |
380 | return NULL; | |
381 | } | |
382 | for (i = 0; i < di->dec_num_probes; i++) | |
383 | di->dec_probemap[i] = i; | |
384 | ||
0bdadba2 BV |
385 | /* Create a new instance of this decoder class. */ |
386 | if (!(di->py_instance = PyObject_CallObject(dec->py_dec, NULL))) { | |
b2c19614 | 387 | if (PyErr_Occurred()) |
7ce7775c | 388 | PyErr_Print(); |
f38ec285 | 389 | g_free(di->dec_probemap); |
0bdadba2 | 390 | g_free(di); |
7ce7775c | 391 | return NULL; |
b2c19614 | 392 | } |
7ce7775c | 393 | |
0bdadba2 | 394 | if (srd_instance_set_options(di, options) != SRD_OK) { |
f38ec285 | 395 | g_free(di->dec_probemap); |
0bdadba2 BV |
396 | g_free(di); |
397 | return NULL; | |
398 | } | |
b2c19614 | 399 | |
f38ec285 BV |
400 | /* Instance takes input from a frontend by default. */ |
401 | di_list = g_slist_append(di_list, di); | |
402 | ||
b2c19614 BV |
403 | return di; |
404 | } | |
405 | ||
7ce7775c BV |
406 | int srd_instance_stack(struct srd_decoder_instance *di_from, |
407 | struct srd_decoder_instance *di_to) | |
408 | { | |
409 | ||
410 | if (!di_from || !di_to) { | |
411 | srd_err("invalid from/to instance pair"); | |
412 | return SRD_ERR_ARG; | |
413 | } | |
414 | ||
415 | if (!g_slist_find(di_list, di_from)) { | |
416 | srd_err("unstacked instance not found"); | |
417 | return SRD_ERR_ARG; | |
418 | } | |
419 | ||
420 | /* Remove from the unstacked list. */ | |
421 | di_list = g_slist_remove(di_list, di_to); | |
422 | ||
423 | /* Stack on top of source di. */ | |
424 | di_from->next_di = g_slist_append(di_from->next_di, di_to); | |
425 | ||
426 | return SRD_OK; | |
427 | } | |
428 | ||
b2c19614 | 429 | |
451680f1 | 430 | /* TODO: this should go into the PD stack */ |
7ce7775c BV |
431 | struct srd_decoder_instance *srd_instance_find(char *instance_id) |
432 | { | |
433 | GSList *l; | |
434 | struct srd_decoder_instance *tmp, *di; | |
b2c19614 | 435 | |
7ce7775c BV |
436 | di = NULL; |
437 | for (l = di_list; l; l = l->next) { | |
438 | tmp = l->data; | |
439 | if (!strcmp(tmp->instance_id, instance_id)) { | |
440 | di = tmp; | |
441 | break; | |
442 | } | |
443 | } | |
444 | ||
445 | return di; | |
446 | } | |
447 | ||
448 | int srd_instance_start(struct srd_decoder_instance *di, PyObject *args) | |
b2c19614 | 449 | { |
7ce7775c | 450 | PyObject *py_name, *py_res; |
b2c19614 | 451 | |
7ce7775c | 452 | srd_dbg("calling start() method on protocol decoder instance %s", di->instance_id); |
b2c19614 | 453 | |
7ce7775c BV |
454 | if (!(py_name = PyUnicode_FromString("start"))) { |
455 | srd_err("unable to build python object for 'start'"); | |
456 | if (PyErr_Occurred()) | |
457 | PyErr_Print(); | |
458 | return SRD_ERR_PYTHON; | |
459 | } | |
460 | ||
461 | if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance, | |
462 | py_name, args, NULL))) { | |
463 | if (PyErr_Occurred()) | |
464 | PyErr_Print(); | |
465 | return SRD_ERR_PYTHON; | |
b2c19614 BV |
466 | } |
467 | ||
f38ec285 BV |
468 | Py_DecRef(py_res); |
469 | Py_DecRef(py_name); | |
7ce7775c | 470 | |
b2c19614 BV |
471 | return SRD_OK; |
472 | } | |
473 | ||
b2c19614 BV |
474 | /** |
475 | * Run the specified decoder function. | |
476 | * | |
477 | * @param dec TODO | |
478 | * @param inbuf TODO | |
479 | * @param inbuflen TODO | |
b2c19614 BV |
480 | * |
481 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
482 | */ | |
86528298 | 483 | int srd_instance_decode(uint64_t start_samplenum, |
bc5f5a43 | 484 | struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen) |
b2c19614 BV |
485 | { |
486 | PyObject *py_instance, *py_res; | |
bc5f5a43 | 487 | srd_logic *logic; |
86528298 | 488 | uint64_t end_samplenum; |
b2c19614 BV |
489 | |
490 | /* Return an error upon unusable input. */ | |
bc5f5a43 | 491 | if (di == NULL) |
a62186e4 | 492 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
b2c19614 | 493 | if (inbuf == NULL) |
a62186e4 | 494 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
b2c19614 | 495 | if (inbuflen == 0) /* No point in working on empty buffers. */ |
a62186e4 | 496 | return SRD_ERR_ARG; /* TODO: More specific error? */ |
b2c19614 BV |
497 | |
498 | /* TODO: Error handling. */ | |
bc5f5a43 | 499 | py_instance = di->py_instance; |
b2c19614 BV |
500 | Py_XINCREF(py_instance); |
501 | ||
bc5f5a43 BV |
502 | logic = PyObject_New(srd_logic, &srd_logic_type); |
503 | Py_INCREF(logic); | |
504 | logic->di = di; | |
86528298 | 505 | logic->start_samplenum = start_samplenum; |
bc5f5a43 BV |
506 | logic->itercnt = 0; |
507 | logic->inbuf = inbuf; | |
508 | logic->inbuflen = inbuflen; | |
509 | logic->sample = PyList_New(2); | |
510 | Py_INCREF(logic->sample); | |
511 | ||
f38ec285 | 512 | end_samplenum = start_samplenum + inbuflen / di->data_unitsize; |
b2c19614 | 513 | if (!(py_res = PyObject_CallMethod(py_instance, "decode", |
86528298 | 514 | "KKO", logic->start_samplenum, end_samplenum, logic))) { |
b2c19614 BV |
515 | if (PyErr_Occurred()) |
516 | PyErr_Print(); /* Returns void. */ | |
517 | ||
518 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ | |
519 | } | |
520 | ||
521 | Py_XDECREF(py_res); | |
bc5f5a43 | 522 | |
b2c19614 BV |
523 | return SRD_OK; |
524 | } | |
525 | ||
526 | ||
7ce7775c BV |
527 | int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) |
528 | { | |
529 | PyObject *args; | |
530 | GSList *d, *s; | |
531 | struct srd_decoder_instance *di; | |
532 | int ret; | |
533 | ||
534 | if (!(args = Py_BuildValue("{s:l}", "samplerate", (long)samplerate))) { | |
535 | srd_err("unable to build python object for metadata"); | |
536 | return SRD_ERR_PYTHON; | |
537 | } | |
538 | ||
539 | /* Run the start() method on all decoders receiving frontend data. */ | |
540 | for (d = di_list; d; d = d->next) { | |
541 | di = d->data; | |
f38ec285 BV |
542 | di->data_num_probes = num_probes; |
543 | di->data_unitsize = unitsize; | |
544 | di->data_samplerate = samplerate; | |
7ce7775c BV |
545 | if ((ret = srd_instance_start(di, args) != SRD_OK)) |
546 | return ret; | |
547 | ||
548 | /* Run the start() method on all decoders up the stack from this one. */ | |
549 | for (s = di->next_di; s; s = s->next) { | |
550 | /* These don't need probes, unitsize and samplerate. */ | |
551 | di = s->data; | |
552 | if ((ret = srd_instance_start(di, args) != SRD_OK)) | |
553 | return ret; | |
554 | } | |
555 | } | |
556 | ||
557 | Py_DECREF(args); | |
558 | ||
559 | return SRD_OK; | |
560 | } | |
561 | ||
b2c19614 | 562 | /* Feed logic samples to decoder session. */ |
86528298 | 563 | int srd_session_feed(uint64_t start_samplenum, uint8_t *inbuf, uint64_t inbuflen) |
b2c19614 BV |
564 | { |
565 | GSList *d; | |
566 | int ret; | |
567 | ||
e5080882 | 568 | for (d = di_list; d; d = d->next) { |
86528298 | 569 | if ((ret = srd_instance_decode(start_samplenum, d->data, inbuf, |
1aef2f93 | 570 | inbuflen)) != SRD_OK) |
b2c19614 BV |
571 | return ret; |
572 | } | |
573 | ||
574 | return SRD_OK; | |
575 | } | |
576 | ||
577 | ||
f9a3947a | 578 | int pd_add(struct srd_decoder_instance *di, int output_type, |
94d43b37 | 579 | char *proto_id) |
e5080882 | 580 | { |
e5080882 | 581 | struct srd_pd_output *pdo; |
e5080882 BV |
582 | |
583 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) | |
584 | return -1; | |
585 | ||
b231546d | 586 | /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ |
15969949 | 587 | pdo->pdo_id = g_slist_length(di->pd_output); |
e5080882 | 588 | pdo->output_type = output_type; |
15969949 | 589 | pdo->decoder = di->decoder; |
94d43b37 | 590 | pdo->proto_id = g_strdup(proto_id); |
e5080882 BV |
591 | di->pd_output = g_slist_append(di->pd_output, pdo); |
592 | ||
15969949 | 593 | return pdo->pdo_id; |
e5080882 BV |
594 | } |
595 | ||
bc5f5a43 BV |
596 | struct srd_decoder_instance *get_di_by_decobject(void *decobject) |
597 | { | |
7ce7775c | 598 | GSList *l, *s; |
bc5f5a43 BV |
599 | struct srd_decoder_instance *di; |
600 | ||
601 | for (l = di_list; l; l = l->next) { | |
602 | di = l->data; | |
603 | if (decobject == di->py_instance) | |
604 | return di; | |
7ce7775c BV |
605 | /* Check decoders stacked on top of this one. */ |
606 | for (s = di->next_di; s; s = s->next) { | |
607 | di = s->data; | |
608 | if (decobject == di->py_instance) | |
609 | return di; | |
610 | } | |
bc5f5a43 BV |
611 | } |
612 | ||
613 | return NULL; | |
614 | } | |
615 | ||
983cb0f5 BV |
616 | int srd_register_callback(int output_type, void *cb) |
617 | { | |
618 | struct srd_pd_callback *pd_cb; | |
619 | ||
620 | if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback)))) | |
621 | return SRD_ERR_MALLOC; | |
622 | ||
623 | pd_cb->output_type = output_type; | |
624 | pd_cb->callback = cb; | |
625 | callbacks = g_slist_append(callbacks, pd_cb); | |
626 | ||
983cb0f5 BV |
627 | return SRD_OK; |
628 | } | |
629 | ||
630 | void *srd_find_callback(int output_type) | |
631 | { | |
632 | GSList *l; | |
633 | struct srd_pd_callback *pd_cb; | |
634 | void *(cb); | |
635 | ||
636 | cb = NULL; | |
637 | for (l = callbacks; l; l = l->next) { | |
638 | pd_cb = l->data; | |
639 | if (pd_cb->output_type == output_type) { | |
640 | cb = pd_cb->callback; | |
641 | break; | |
642 | } | |
643 | } | |
644 | ||
645 | return cb; | |
646 | } | |
e5080882 | 647 |