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