]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
decoder: check annotation rows' references to annotation classes
[libsigrokdecode.git] / decoder.c
CommitLineData
b2c19614 1/*
50bd5d25 2 * This file is part of the libsigrokdecode project.
b2c19614
BV
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
4fadb128 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
36784362 21#include <config.h>
f6c7eade
MC
22#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include "libsigrokdecode.h"
eb2bbd66 24#include <glib.h>
b2c19614 25
54fdeeef
UH
26/**
27 * @file
28 *
29 * Listing, loading, unloading, and handling protocol decoders.
30 */
31
4895418c
UH
32/**
33 * @defgroup grp_decoder Protocol decoders
34 *
35 * Handling protocol decoders.
36 *
37 * @{
38 */
39
57790bc8
UH
40/** @cond PRIVATE */
41
c63f5a87 42/* The list of loaded protocol decoders. */
2060510a 43static GSList *pd_list = NULL;
b2c19614 44
ea81b49a 45/* srd.c */
23a29d24 46extern SRD_PRIV GSList *searchpaths;
ea81b49a
BV
47
48/* session.c */
23a29d24
UH
49extern SRD_PRIV GSList *sessions;
50extern SRD_PRIV int max_session_id;
32cfb920 51
c9bfccc6 52/* module_sigrokdecode.c */
55c3c5f4 53extern SRD_PRIV PyObject *mod_sigrokdecode;
451680f1 54
57790bc8
UH
55/** @endcond */
56
f6527cc4
UH
57static gboolean srd_check_init(void)
58{
59 if (max_session_id < 0) {
60 srd_err("Library is not initialized.");
61 return FALSE;
62 } else
63 return TRUE;
64}
65
b2c19614 66/**
c63f5a87 67 * Returns the list of loaded protocol decoders.
b2c19614 68 *
3a1501e9 69 * This is a GSList of pointers to struct srd_decoder items.
b2c19614
BV
70 *
71 * @return List of decoders, NULL if none are supported or loaded.
8c664ca2 72 *
c57d1013 73 * @since 0.2.0
b2c19614 74 */
38ff5046 75SRD_API const GSList *srd_decoder_list(void)
b2c19614 76{
e5080882 77 return pd_list;
b2c19614
BV
78}
79
b2c19614
BV
80/**
81 * Get the decoder with the specified ID.
82 *
83 * @param id The ID string of the decoder to return.
582c8473 84 *
b2c19614 85 * @return The decoder with the specified ID, or NULL if not found.
8c664ca2
UH
86 *
87 * @since 0.1.0
b2c19614 88 */
9d122fd5 89SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
b2c19614
BV
90{
91 GSList *l;
92 struct srd_decoder *dec;
93
38ff5046 94 for (l = pd_list; l; l = l->next) {
b2c19614
BV
95 dec = l->data;
96 if (!strcmp(dec->id, id))
97 return dec;
98 }
99
100 return NULL;
101}
102
6d67d057
DE
103static void channel_free(void *data)
104{
105 struct srd_channel *ch = data;
106
107 if (!ch)
108 return;
109
110 g_free(ch->desc);
111 g_free(ch->name);
112 g_free(ch->id);
113 g_free(ch);
114}
115
116static void variant_free(void *data)
117{
118 GVariant *var = data;
119
120 if (!var)
121 return;
122
123 g_variant_unref(var);
124}
125
126static void annotation_row_free(void *data)
127{
128 struct srd_decoder_annotation_row *row = data;
129
130 if (!row)
131 return;
132
133 g_slist_free(row->ann_classes);
134 g_free(row->desc);
135 g_free(row->id);
136 g_free(row);
137}
138
ddcde186
UH
139static void logic_output_channel_free(void *data)
140{
141 struct srd_decoder_logic_output_channel *logic_out_ch = data;
142
143 if (!logic_out_ch)
144 return;
145
146 g_free(logic_out_ch->desc);
147 g_free(logic_out_ch->id);
148 g_free(logic_out_ch);
149}
150
6d67d057
DE
151static void decoder_option_free(void *data)
152{
153 struct srd_decoder_option *opt = data;
154
155 if (!opt)
156 return;
157
158 g_slist_free_full(opt->values, &variant_free);
159 variant_free(opt->def);
160 g_free(opt->desc);
161 g_free(opt->id);
162 g_free(opt);
163}
164
165static void decoder_free(struct srd_decoder *dec)
166{
514b2edc
UH
167 PyGILState_STATE gstate;
168
6d67d057
DE
169 if (!dec)
170 return;
171
514b2edc 172 gstate = PyGILState_Ensure();
6d67d057
DE
173 Py_XDECREF(dec->py_dec);
174 Py_XDECREF(dec->py_mod);
514b2edc 175 PyGILState_Release(gstate);
6d67d057
DE
176
177 g_slist_free_full(dec->options, &decoder_option_free);
178 g_slist_free_full(dec->binary, (GDestroyNotify)&g_strfreev);
179 g_slist_free_full(dec->annotation_rows, &annotation_row_free);
180 g_slist_free_full(dec->annotations, (GDestroyNotify)&g_strfreev);
181 g_slist_free_full(dec->opt_channels, &channel_free);
182 g_slist_free_full(dec->channels, &channel_free);
183
d480174d
UH
184 g_slist_free_full(dec->outputs, g_free);
185 g_slist_free_full(dec->inputs, g_free);
4c180223 186 g_slist_free_full(dec->tags, g_free);
6d67d057
DE
187 g_free(dec->license);
188 g_free(dec->desc);
189 g_free(dec->longname);
190 g_free(dec->name);
191 g_free(dec->id);
192
193 g_free(dec);
194}
195
6a15597a 196static int get_channels(const struct srd_decoder *d, const char *attr,
6d67d057 197 GSList **out_pdchl, int offset)
f38ec285 198{
6a15597a
UH
199 PyObject *py_channellist, *py_entry;
200 struct srd_channel *pdch;
6d67d057
DE
201 GSList *pdchl;
202 ssize_t i;
514b2edc
UH
203 PyGILState_STATE gstate;
204
205 gstate = PyGILState_Ensure();
f38ec285 206
514b2edc 207 if (!PyObject_HasAttrString(d->py_dec, attr)) {
6a15597a 208 /* No channels of this type specified. */
514b2edc 209 PyGILState_Release(gstate);
f38ec285 210 return SRD_OK;
514b2edc 211 }
f38ec285 212
6d67d057
DE
213 pdchl = NULL;
214
6a15597a 215 py_channellist = PyObject_GetAttrString(d->py_dec, attr);
6d67d057
DE
216 if (!py_channellist)
217 goto except_out;
218
6a15597a 219 if (!PyTuple_Check(py_channellist)) {
da9bcbd9 220 srd_err("Protocol decoder %s %s attribute is not a tuple.",
6d67d057
DE
221 d->name, attr);
222 goto err_out;
f38ec285
BV
223 }
224
6d67d057 225 for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) {
6a15597a 226 py_entry = PyTuple_GetItem(py_channellist, i);
6d67d057
DE
227 if (!py_entry)
228 goto except_out;
229
f38ec285
BV
230 if (!PyDict_Check(py_entry)) {
231 srd_err("Protocol decoder %s %s attribute is not "
6d67d057
DE
232 "a list of dict elements.", d->name, attr);
233 goto err_out;
f38ec285 234 }
35c10c0e 235 pdch = g_malloc(sizeof(struct srd_channel));
6d67d057
DE
236 /* Add to list right away so it doesn't get lost. */
237 pdchl = g_slist_prepend(pdchl, pdch);
f38ec285 238
6d67d057
DE
239 if (py_dictitem_as_str(py_entry, "id", &pdch->id) != SRD_OK)
240 goto err_out;
241 if (py_dictitem_as_str(py_entry, "name", &pdch->name) != SRD_OK)
242 goto err_out;
243 if (py_dictitem_as_str(py_entry, "desc", &pdch->desc) != SRD_OK)
244 goto err_out;
f38ec285 245
6d67d057 246 pdch->order = offset + i;
f38ec285 247 }
f38ec285 248
6d67d057
DE
249 Py_DECREF(py_channellist);
250 *out_pdchl = pdchl;
251
514b2edc
UH
252 PyGILState_Release(gstate);
253
6d67d057 254 return SRD_OK;
f38ec285 255
6d67d057
DE
256except_out:
257 srd_exception_catch("Failed to get %s list of %s decoder",
258 attr, d->name);
7969d803 259
6d67d057
DE
260err_out:
261 g_slist_free_full(pdchl, &channel_free);
262 Py_XDECREF(py_channellist);
514b2edc 263 PyGILState_Release(gstate);
6d67d057
DE
264
265 return SRD_ERR_PYTHON;
f38ec285
BV
266}
267
2f395bff
BV
268static int get_options(struct srd_decoder *d)
269{
6d67d057
DE
270 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
271 GSList *options;
2f395bff 272 struct srd_decoder_option *o;
84c1c0b5 273 GVariant *gvar;
6d67d057 274 ssize_t opt, i;
514b2edc 275 PyGILState_STATE gstate;
2f395bff 276
514b2edc
UH
277 gstate = PyGILState_Ensure();
278
279 if (!PyObject_HasAttrString(d->py_dec, "options")) {
84c1c0b5 280 /* No options, that's fine. */
514b2edc 281 PyGILState_Release(gstate);
2f395bff 282 return SRD_OK;
514b2edc 283 }
2f395bff 284
6d67d057
DE
285 options = NULL;
286
84c1c0b5 287 /* If present, options must be a tuple. */
2f395bff 288 py_opts = PyObject_GetAttrString(d->py_dec, "options");
6d67d057
DE
289 if (!py_opts)
290 goto except_out;
291
84c1c0b5
BV
292 if (!PyTuple_Check(py_opts)) {
293 srd_err("Protocol decoder %s: options attribute is not "
294 "a tuple.", d->id);
6d67d057 295 goto err_out;
2f395bff
BV
296 }
297
6d67d057 298 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
84c1c0b5 299 py_opt = PyTuple_GetItem(py_opts, opt);
6d67d057
DE
300 if (!py_opt)
301 goto except_out;
302
84c1c0b5
BV
303 if (!PyDict_Check(py_opt)) {
304 srd_err("Protocol decoder %s options: each option "
305 "must consist of a dictionary.", d->name);
6d67d057 306 goto err_out;
2f395bff 307 }
6d67d057 308
84c1c0b5 309 o = g_malloc0(sizeof(struct srd_decoder_option));
6d67d057
DE
310 /* Add to list right away so it doesn't get lost. */
311 options = g_slist_prepend(options, o);
312
313 py_str = PyDict_GetItemString(py_opt, "id");
314 if (!py_str) {
fa168be6 315 srd_err("Protocol decoder %s option %zd has no ID.",
6d67d057
DE
316 d->name, opt);
317 goto err_out;
318 }
319 if (py_str_as_str(py_str, &o->id) != SRD_OK)
320 goto err_out;
321
322 py_str = PyDict_GetItemString(py_opt, "desc");
323 if (py_str) {
324 if (py_str_as_str(py_str, &o->desc) != SRD_OK)
325 goto err_out;
326 }
327
328 py_default = PyDict_GetItemString(py_opt, "default");
329 if (py_default) {
330 gvar = py_obj_to_variant(py_default);
331 if (!gvar) {
84c1c0b5 332 srd_err("Protocol decoder %s option 'default' has "
6d67d057
DE
333 "invalid default value.", d->name);
334 goto err_out;
84c1c0b5 335 }
6d67d057 336 o->def = g_variant_ref_sink(gvar);
2f395bff 337 }
84c1c0b5 338
6d67d057
DE
339 py_values = PyDict_GetItemString(py_opt, "values");
340 if (py_values) {
7969d803
UH
341 /*
342 * A default is required if a list of values is
343 * given, since it's used to verify their type.
344 */
84c1c0b5 345 if (!o->def) {
6d67d057
DE
346 srd_err("No default for option '%s'.", o->id);
347 goto err_out;
84c1c0b5 348 }
6d67d057 349 if (!PyTuple_Check(py_values)) {
84c1c0b5 350 srd_err("Option '%s' values should be a tuple.", o->id);
6d67d057 351 goto err_out;
84c1c0b5 352 }
6d67d057
DE
353
354 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
355 py_item = PyTuple_GetItem(py_values, i);
356 if (!py_item)
357 goto except_out;
358
2d661bcb 359 if (py_default && (Py_TYPE(py_default) != Py_TYPE(py_item))) {
84c1c0b5 360 srd_err("All values for option '%s' must be "
6d67d057
DE
361 "of the same type as the default.",
362 o->id);
363 goto err_out;
84c1c0b5 364 }
6d67d057
DE
365 gvar = py_obj_to_variant(py_item);
366 if (!gvar) {
367 srd_err("Protocol decoder %s option 'values' "
368 "contains invalid value.", d->name);
369 goto err_out;
84c1c0b5 370 }
6d67d057
DE
371 o->values = g_slist_prepend(o->values,
372 g_variant_ref_sink(gvar));
2f395bff 373 }
2f395bff 374 }
6d67d057
DE
375 }
376 d->options = options;
377 Py_DECREF(py_opts);
514b2edc 378 PyGILState_Release(gstate);
6d67d057
DE
379
380 return SRD_OK;
381
382except_out:
383 srd_exception_catch("Failed to get %s decoder options", d->name);
7969d803 384
6d67d057
DE
385err_out:
386 g_slist_free_full(options, &decoder_option_free);
387 Py_XDECREF(py_opts);
514b2edc 388 PyGILState_Release(gstate);
6d67d057
DE
389
390 return SRD_ERR_PYTHON;
391}
392
7969d803 393/* Convert annotation class attribute to GSList of char **. */
8ab7ede6 394static int get_annotations(struct srd_decoder *dec, size_t *ret_count)
6d67d057
DE
395{
396 PyObject *py_annlist, *py_ann;
397 GSList *annotations;
398 char **annpair;
399 ssize_t i;
514b2edc
UH
400 PyGILState_STATE gstate;
401
8ab7ede6
GS
402 if (ret_count)
403 *ret_count = 0;
404
514b2edc 405 gstate = PyGILState_Ensure();
6d67d057 406
514b2edc
UH
407 if (!PyObject_HasAttrString(dec->py_dec, "annotations")) {
408 PyGILState_Release(gstate);
6d67d057 409 return SRD_OK;
514b2edc 410 }
6d67d057
DE
411
412 annotations = NULL;
413
414 py_annlist = PyObject_GetAttrString(dec->py_dec, "annotations");
415 if (!py_annlist)
416 goto except_out;
417
418 if (!PyTuple_Check(py_annlist)) {
fdb1a907
GS
419 srd_err("Protocol decoder %s annotations should be a tuple.",
420 dec->name);
6d67d057
DE
421 goto err_out;
422 }
423
8ab7ede6
GS
424 i = PyTuple_Size(py_annlist);
425 if (ret_count)
426 *ret_count = i;
427 while (i--) {
6d67d057
DE
428 py_ann = PyTuple_GetItem(py_annlist, i);
429 if (!py_ann)
430 goto except_out;
431
432 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
fdb1a907 433 srd_err("Protocol decoder %s annotation %zd should be a tuple with two elements.",
6d67d057
DE
434 dec->name, i + 1);
435 goto err_out;
436 }
437 if (py_strseq_to_char(py_ann, &annpair) != SRD_OK)
438 goto err_out;
439
440 annotations = g_slist_prepend(annotations, annpair);
441 }
442 dec->annotations = annotations;
443 Py_DECREF(py_annlist);
514b2edc 444 PyGILState_Release(gstate);
6d67d057
DE
445
446 return SRD_OK;
447
448except_out:
449 srd_exception_catch("Failed to get %s decoder annotations", dec->name);
7969d803 450
6d67d057
DE
451err_out:
452 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
453 Py_XDECREF(py_annlist);
514b2edc 454 PyGILState_Release(gstate);
6d67d057
DE
455
456 return SRD_ERR_PYTHON;
457}
458
7969d803 459/* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
8ab7ede6 460static int get_annotation_rows(struct srd_decoder *dec, size_t cls_count)
6d67d057 461{
fdb1a907
GS
462 const char *py_member_name = "annotation_rows";
463
6d67d057
DE
464 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
465 GSList *annotation_rows;
466 struct srd_decoder_annotation_row *ann_row;
467 ssize_t i, k;
468 size_t class_idx;
514b2edc
UH
469 PyGILState_STATE gstate;
470
471 gstate = PyGILState_Ensure();
6d67d057 472
fdb1a907 473 if (!PyObject_HasAttrString(dec->py_dec, py_member_name)) {
514b2edc 474 PyGILState_Release(gstate);
6d67d057 475 return SRD_OK;
514b2edc 476 }
6d67d057
DE
477
478 annotation_rows = NULL;
479
fdb1a907 480 py_ann_rows = PyObject_GetAttrString(dec->py_dec, py_member_name);
6d67d057
DE
481 if (!py_ann_rows)
482 goto except_out;
483
484 if (!PyTuple_Check(py_ann_rows)) {
fdb1a907
GS
485 srd_err("Protocol decoder %s %s must be a tuple.",
486 dec->name, py_member_name);
6d67d057
DE
487 goto err_out;
488 }
489
490 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
491 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
492 if (!py_ann_row)
493 goto except_out;
494
495 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
fdb1a907
GS
496 srd_err("Protocol decoder %s %s must contain only tuples of 3 elements.",
497 dec->name, py_member_name);
6d67d057
DE
498 goto err_out;
499 }
500 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
501 /* Add to list right away so it doesn't get lost. */
502 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
503
504 py_item = PyTuple_GetItem(py_ann_row, 0);
505 if (!py_item)
506 goto except_out;
507 if (py_str_as_str(py_item, &ann_row->id) != SRD_OK)
508 goto err_out;
509
510 py_item = PyTuple_GetItem(py_ann_row, 1);
511 if (!py_item)
512 goto except_out;
513 if (py_str_as_str(py_item, &ann_row->desc) != SRD_OK)
514 goto err_out;
515
516 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
517 if (!py_ann_classes)
518 goto except_out;
519
520 if (!PyTuple_Check(py_ann_classes)) {
fdb1a907
GS
521 srd_err("Protocol decoder %s %s tuples must have a tuple of numbers as 3rd element.",
522 dec->name, py_member_name);
6d67d057
DE
523 goto err_out;
524 }
525
526 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
527 py_item = PyTuple_GetItem(py_ann_classes, k);
528 if (!py_item)
529 goto except_out;
530
531 if (!PyLong_Check(py_item)) {
fdb1a907 532 srd_err("Protocol decoder %s annotation row class tuple must only contain numbers.",
6d67d057
DE
533 dec->name);
534 goto err_out;
535 }
536 class_idx = PyLong_AsSize_t(py_item);
537 if (PyErr_Occurred())
538 goto except_out;
8ab7ede6
GS
539 if (class_idx >= cls_count) {
540 srd_err("Protocol decoder %s annotation row %zd references invalid class %zu.",
541 dec->name, i, class_idx);
542 goto err_out;
543 }
6d67d057
DE
544
545 ann_row->ann_classes = g_slist_prepend(ann_row->ann_classes,
546 GSIZE_TO_POINTER(class_idx));
547 }
548 }
549 dec->annotation_rows = annotation_rows;
550 Py_DECREF(py_ann_rows);
514b2edc 551 PyGILState_Release(gstate);
6d67d057
DE
552
553 return SRD_OK;
554
555except_out:
556 srd_exception_catch("Failed to get %s decoder annotation rows",
557 dec->name);
7969d803 558
6d67d057
DE
559err_out:
560 g_slist_free_full(annotation_rows, &annotation_row_free);
561 Py_XDECREF(py_ann_rows);
514b2edc 562 PyGILState_Release(gstate);
6d67d057
DE
563
564 return SRD_ERR_PYTHON;
565}
566
7969d803 567/* Convert binary classes to GSList of char **. */
6d67d057
DE
568static int get_binary_classes(struct srd_decoder *dec)
569{
570 PyObject *py_bin_classes, *py_bin_class;
571 GSList *bin_classes;
572 char **bin;
573 ssize_t i;
514b2edc 574 PyGILState_STATE gstate;
6d67d057 575
514b2edc
UH
576 gstate = PyGILState_Ensure();
577
578 if (!PyObject_HasAttrString(dec->py_dec, "binary")) {
579 PyGILState_Release(gstate);
6d67d057 580 return SRD_OK;
514b2edc 581 }
6d67d057
DE
582
583 bin_classes = NULL;
584
585 py_bin_classes = PyObject_GetAttrString(dec->py_dec, "binary");
586 if (!py_bin_classes)
587 goto except_out;
588
589 if (!PyTuple_Check(py_bin_classes)) {
590 srd_err("Protocol decoder %s binary classes should "
591 "be a tuple.", dec->name);
592 goto err_out;
593 }
594
595 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
596 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
597 if (!py_bin_class)
598 goto except_out;
599
600 if (!PyTuple_Check(py_bin_class)
601 || PyTuple_Size(py_bin_class) != 2) {
602 srd_err("Protocol decoder %s binary classes should "
603 "consist only of tuples of 2 elements.",
604 dec->name);
605 goto err_out;
606 }
607 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK)
608 goto err_out;
609
610 bin_classes = g_slist_prepend(bin_classes, bin);
611 }
612 dec->binary = bin_classes;
613 Py_DECREF(py_bin_classes);
514b2edc 614 PyGILState_Release(gstate);
6d67d057
DE
615
616 return SRD_OK;
617
618except_out:
619 srd_exception_catch("Failed to get %s decoder binary classes",
620 dec->name);
7969d803 621
6d67d057
DE
622err_out:
623 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
624 Py_XDECREF(py_bin_classes);
514b2edc 625 PyGILState_Release(gstate);
6d67d057
DE
626
627 return SRD_ERR_PYTHON;
628}
629
ddcde186
UH
630/* Convert logic_output_channels to GSList of 'struct srd_decoder_logic_output_channel'. */
631static int get_logic_output_channels(struct srd_decoder *dec)
632{
460e6cfa 633 PyObject *py_logic_out_chs, *py_logic_out_ch, *py_item;
ddcde186
UH
634 GSList *logic_out_chs;
635 struct srd_decoder_logic_output_channel *logic_out_ch;
636 ssize_t i;
637 PyGILState_STATE gstate;
638
639 gstate = PyGILState_Ensure();
640
641 if (!PyObject_HasAttrString(dec->py_dec, "logic_output_channels")) {
642 PyGILState_Release(gstate);
643 return SRD_OK;
644 }
645
646 logic_out_chs = NULL;
647
648 py_logic_out_chs = PyObject_GetAttrString(dec->py_dec, "logic_output_channels");
649 if (!py_logic_out_chs)
650 goto except_out;
651
652 if (!PyTuple_Check(py_logic_out_chs)) {
653 srd_err("Protocol decoder %s logic_output_channels "
654 "must be a tuple.", dec->name);
655 goto err_out;
656 }
657
658 for (i = PyTuple_Size(py_logic_out_chs) - 1; i >= 0; i--) {
659 py_logic_out_ch = PyTuple_GetItem(py_logic_out_chs, i);
660 if (!py_logic_out_ch)
661 goto except_out;
662
460e6cfa 663 if (!PyTuple_Check(py_logic_out_ch) || PyTuple_Size(py_logic_out_ch) != 2) {
ddcde186 664 srd_err("Protocol decoder %s logic_output_channels "
460e6cfa 665 "must contain only tuples of 2 elements.",
ddcde186
UH
666 dec->name);
667 goto err_out;
668 }
669 logic_out_ch = g_malloc0(sizeof(*logic_out_ch));
670 /* Add to list right away so it doesn't get lost. */
671 logic_out_chs = g_slist_prepend(logic_out_chs, logic_out_ch);
672
673 py_item = PyTuple_GetItem(py_logic_out_ch, 0);
674 if (!py_item)
675 goto except_out;
676 if (py_str_as_str(py_item, &logic_out_ch->id) != SRD_OK)
677 goto err_out;
678
679 py_item = PyTuple_GetItem(py_logic_out_ch, 1);
680 if (!py_item)
681 goto except_out;
682 if (py_str_as_str(py_item, &logic_out_ch->desc) != SRD_OK)
683 goto err_out;
ddcde186
UH
684 }
685 dec->logic_output_channels = logic_out_chs;
686 Py_DECREF(py_logic_out_chs);
687 PyGILState_Release(gstate);
688
689 return SRD_OK;
690
691except_out:
692 srd_exception_catch("Failed to get %s decoder logic output channels",
693 dec->name);
694
695err_out:
696 g_slist_free_full(logic_out_chs, &logic_output_channel_free);
697 Py_XDECREF(py_logic_out_chs);
698 PyGILState_Release(gstate);
699
700 return SRD_ERR_PYTHON;
701}
702
7969d803 703/* Check whether the Decoder class defines the named method. */
6d67d057
DE
704static int check_method(PyObject *py_dec, const char *mod_name,
705 const char *method_name)
706{
707 PyObject *py_method;
708 int is_callable;
514b2edc
UH
709 PyGILState_STATE gstate;
710
711 gstate = PyGILState_Ensure();
6d67d057
DE
712
713 py_method = PyObject_GetAttrString(py_dec, method_name);
714 if (!py_method) {
715 srd_exception_catch("Protocol decoder %s Decoder class "
716 "has no %s() method", mod_name, method_name);
514b2edc 717 PyGILState_Release(gstate);
6d67d057
DE
718 return SRD_ERR_PYTHON;
719 }
720
721 is_callable = PyCallable_Check(py_method);
722 Py_DECREF(py_method);
723
514b2edc
UH
724 PyGILState_Release(gstate);
725
6d67d057
DE
726 if (!is_callable) {
727 srd_err("Protocol decoder %s Decoder class attribute '%s' "
728 "is not a method.", mod_name, method_name);
729 return SRD_ERR_PYTHON;
2f395bff 730 }
2f395bff 731
84c1c0b5 732 return SRD_OK;
2f395bff
BV
733}
734
40589f25
UH
735/**
736 * Get the API version of the specified decoder.
737 *
738 * @param d The decoder to use. Must not be NULL.
739 *
740 * @return The API version of the decoder, or 0 upon errors.
c947ab01
UH
741 *
742 * @private
40589f25
UH
743 */
744SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d)
745{
746 PyObject *py_apiver;
747 long apiver;
514b2edc 748 PyGILState_STATE gstate;
40589f25
UH
749
750 if (!d)
751 return 0;
752
514b2edc
UH
753 gstate = PyGILState_Ensure();
754
40589f25
UH
755 py_apiver = PyObject_GetAttrString(d->py_dec, "api_version");
756 apiver = (py_apiver && PyLong_Check(py_apiver))
757 ? PyLong_AsLong(py_apiver) : 0;
758 Py_XDECREF(py_apiver);
759
514b2edc
UH
760 PyGILState_Release(gstate);
761
40589f25
UH
762 return apiver;
763}
764
1734f4c2
UH
765static gboolean contains_duplicates(GSList *list)
766{
767 for (GSList *l1 = list; l1; l1 = l1->next) {
768 for (GSList *l2 = l1->next; l2; l2 = l2->next)
769 if (!strcmp(l1->data, l2->data))
770 return TRUE;
771 }
772
773 return FALSE;
774}
775
776static gboolean contains_duplicate_ids(GSList *list1, GSList *list2)
777{
778 for (GSList *l1 = list1; l1; l1 = l1->next) {
779 unsigned int cnt = 0;
780 const char **s1 = l1->data;
781 for (GSList *l2 = list2; l2; l2 = l2->next) {
782 const char **s2 = l2->data;
783 if (!strcmp(s1[0], s2[0]))
784 cnt++;
785 if ((list1 == list2) && cnt > 1)
786 return TRUE;
787 if ((list1 != list2) && cnt > 0)
788 return TRUE;
789 }
790 }
791
792 return FALSE;
793}
794
795static gboolean contains_duplicate_row_ids(GSList *list1, GSList *list2)
796{
797 for (GSList *l1 = list1; l1; l1 = l1->next) {
798 unsigned int cnt = 0;
799 const struct srd_decoder_annotation_row *r1 = l1->data;
800 for (GSList *l2 = list2; l2; l2 = l2->next) {
801 const struct srd_decoder_annotation_row *r2 = l2->data;
802 if (!strcmp(r1->id, r2->id))
803 cnt++;
804 if (cnt > 1)
805 return TRUE;
806 }
807 }
808
809 return FALSE;
810}
811
b2c19614 812/**
64c29e28 813 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 814 *
38ff5046 815 * @param module_name The module name to be loaded.
b2c19614
BV
816 *
817 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
818 *
819 * @since 0.1.0
b2c19614 820 */
9d122fd5 821SRD_API int srd_decoder_load(const char *module_name)
b2c19614 822{
40589f25 823 PyObject *py_basedec;
b2c19614 824 struct srd_decoder *d;
6d67d057
DE
825 long apiver;
826 int is_subclass;
60697682 827 const char *fail_txt;
514b2edc 828 PyGILState_STATE gstate;
8ab7ede6 829 size_t ann_cls_count;
b2c19614 830
e195c025
BV
831 if (!srd_check_init())
832 return SRD_ERR;
833
8a014683
BV
834 if (!module_name)
835 return SRD_ERR_ARG;
836
514b2edc
UH
837 gstate = PyGILState_Ensure();
838
aac68131
BV
839 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
840 /* Module was already imported. */
514b2edc 841 PyGILState_Release(gstate);
aac68131
BV
842 return SRD_OK;
843 }
844
077fa8ac 845 d = g_malloc0(sizeof(struct srd_decoder));
60697682 846 fail_txt = NULL;
b2c19614 847
e9dd2fea 848 d->py_mod = py_import_by_name(module_name);
60697682
GS
849 if (!d->py_mod) {
850 fail_txt = "import by name failed";
6d67d057 851 goto except_out;
60697682 852 }
6d67d057
DE
853
854 if (!mod_sigrokdecode) {
855 srd_err("sigrokdecode module not loaded.");
60697682 856 fail_txt = "sigrokdecode(3) not loaded";
451680f1
BV
857 goto err_out;
858 }
b2c19614 859
451680f1 860 /* Get the 'Decoder' class as Python object. */
6d67d057 861 d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder");
60697682
GS
862 if (!d->py_dec) {
863 fail_txt = "no 'Decoder' attribute in imported module";
6d67d057 864 goto except_out;
60697682 865 }
b2c19614 866
6d67d057 867 py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder");
60697682
GS
868 if (!py_basedec) {
869 fail_txt = "no 'Decoder' attribute in sigrokdecode(3)";
6d67d057 870 goto except_out;
60697682 871 }
6d67d057
DE
872
873 is_subclass = PyObject_IsSubclass(d->py_dec, py_basedec);
874 Py_DECREF(py_basedec);
b2c19614 875
6d67d057 876 if (!is_subclass) {
451680f1 877 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 878 "a subclass of sigrokdecode.Decoder.", module_name);
60697682 879 fail_txt = "not a subclass of sigrokdecode.Decoder";
451680f1
BV
880 goto err_out;
881 }
b2c19614 882
5c0c9cb3 883 /*
077fa8ac 884 * Check that this decoder has the correct PD API version.
5c0c9cb3
UH
885 * PDs of different API versions are incompatible and cannot work.
886 */
40589f25 887 apiver = srd_decoder_apiver(d);
eb883723
UH
888 if (apiver != 3) {
889 srd_exception_catch("Only PD API version 3 is supported, "
60697682
GS
890 "decoder %s has version %ld", module_name, apiver);
891 fail_txt = "API version mismatch";
1d552cd3
BV
892 goto err_out;
893 }
1d552cd3 894
7969d803
UH
895 /* Check Decoder class for required methods. */
896
be760e59
GS
897 if (check_method(d->py_dec, module_name, "reset") != SRD_OK) {
898 fail_txt = "no 'reset()' method";
899 goto err_out;
900 }
901
60697682
GS
902 if (check_method(d->py_dec, module_name, "start") != SRD_OK) {
903 fail_txt = "no 'start()' method";
1d552cd3 904 goto err_out;
60697682 905 }
6d67d057 906
60697682
GS
907 if (check_method(d->py_dec, module_name, "decode") != SRD_OK) {
908 fail_txt = "no 'decode()' method";
1d552cd3 909 goto err_out;
60697682 910 }
1d552cd3 911
84c1c0b5 912 /* Store required fields in newly allocated strings. */
60697682
GS
913 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) {
914 fail_txt = "no 'id' attribute";
84c1c0b5 915 goto err_out;
60697682 916 }
84c1c0b5 917
60697682
GS
918 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) {
919 fail_txt = "no 'name' attribute";
84c1c0b5 920 goto err_out;
60697682 921 }
84c1c0b5 922
60697682
GS
923 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) {
924 fail_txt = "no 'longname' attribute";
84c1c0b5 925 goto err_out;
60697682 926 }
84c1c0b5 927
60697682
GS
928 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) {
929 fail_txt = "no 'desc' attribute";
84c1c0b5 930 goto err_out;
60697682 931 }
84c1c0b5 932
60697682
GS
933 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) {
934 fail_txt = "no 'license' attribute";
84c1c0b5 935 goto err_out;
60697682 936 }
84c1c0b5 937
d480174d
UH
938 if (py_attr_as_strlist(d->py_dec, "inputs", &(d->inputs)) != SRD_OK) {
939 fail_txt = "missing or malformed 'inputs' attribute";
940 goto err_out;
941 }
942
943 if (py_attr_as_strlist(d->py_dec, "outputs", &(d->outputs)) != SRD_OK) {
944 fail_txt = "missing or malformed 'outputs' attribute";
945 goto err_out;
946 }
947
4c180223
SA
948 if (py_attr_as_strlist(d->py_dec, "tags", &(d->tags)) != SRD_OK) {
949 fail_txt = "missing or malformed 'tags' attribute";
950 goto err_out;
951 }
952
84c1c0b5 953 /* All options and their default values. */
60697682
GS
954 if (get_options(d) != SRD_OK) {
955 fail_txt = "cannot get options";
2f395bff 956 goto err_out;
60697682 957 }
11ea8ae1 958
6a15597a 959 /* Check and import required channels. */
60697682
GS
960 if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) {
961 fail_txt = "cannot get channels";
f38ec285 962 goto err_out;
60697682 963 }
f38ec285 964
6a15597a 965 /* Check and import optional channels. */
6d67d057 966 if (get_channels(d, "optional_channels", &d->opt_channels,
60697682
GS
967 g_slist_length(d->channels)) != SRD_OK) {
968 fail_txt = "cannot get optional channels";
f38ec285 969 goto err_out;
60697682 970 }
f38ec285 971
8ab7ede6 972 if (get_annotations(d, &ann_cls_count) != SRD_OK) {
60697682 973 fail_txt = "cannot get annotations";
6d67d057 974 goto err_out;
60697682 975 }
1ce46817 976
8ab7ede6 977 if (get_annotation_rows(d, ann_cls_count) != SRD_OK) {
60697682 978 fail_txt = "cannot get annotation rows";
6d67d057 979 goto err_out;
60697682 980 }
d75d8a7c 981
60697682
GS
982 if (get_binary_classes(d) != SRD_OK) {
983 fail_txt = "cannot get binary classes";
6d67d057 984 goto err_out;
60697682 985 }
d75d8a7c 986
1734f4c2
UH
987 if (contains_duplicates(d->inputs)) {
988 fail_txt = "duplicate input IDs";
989 goto err_out;
990 }
991
992 if (contains_duplicates(d->outputs)) {
993 fail_txt = "duplicate output IDs";
994 goto err_out;
995 }
996
997 if (contains_duplicates(d->tags)) {
998 fail_txt = "duplicate tags";
999 goto err_out;
1000 }
1001
1002 if (contains_duplicate_ids(d->channels, d->channels)) {
1003 fail_txt = "duplicate channel IDs";
1004 goto err_out;
1005 }
1006
1007 if (contains_duplicate_ids(d->opt_channels, d->opt_channels)) {
1008 fail_txt = "duplicate optional channel IDs";
1009 goto err_out;
1010 }
1011
1012 if (contains_duplicate_ids(d->channels, d->opt_channels)) {
1013 fail_txt = "channel and optional channel IDs contain duplicates";
1014 goto err_out;
1015 }
1016
1017 if (contains_duplicate_ids(d->options, d->options)) {
1018 fail_txt = "duplicate option IDs";
1019 goto err_out;
1020 }
1021
1022 if (contains_duplicate_ids(d->annotations, d->annotations)) {
1023 fail_txt = "duplicate annotation class IDs";
1024 goto err_out;
1025 }
1026
1027 if (contains_duplicate_row_ids(d->annotation_rows, d->annotation_rows)) {
1028 fail_txt = "duplicate annotation row IDs";
1029 goto err_out;
1030 }
1031
1032 if (contains_duplicate_ids(d->annotations, d->annotation_rows)) {
1033 fail_txt = "annotation class/row IDs contain duplicates";
1034 goto err_out;
1035 }
1036
1037 if (contains_duplicate_ids(d->binary, d->binary)) {
1038 fail_txt = "duplicate binary class IDs";
1039 goto err_out;
1040 }
1041
ddcde186
UH
1042 if (get_logic_output_channels(d) != SRD_OK) {
1043 fail_txt = "cannot get logic output channels";
1044 goto err_out;
1045 }
1046
514b2edc
UH
1047 PyGILState_Release(gstate);
1048
c63f5a87 1049 /* Append it to the list of loaded decoders. */
9b37109d
BV
1050 pd_list = g_slist_append(pd_list, d);
1051
6d67d057 1052 return SRD_OK;
451680f1 1053
6d67d057 1054except_out:
e51475ad
UH
1055 /* Don't show a message for the "common" directory, it's not a PD. */
1056 if (strcmp(module_name, "common")) {
1057 srd_exception_catch("Failed to load decoder %s: %s",
1058 module_name, fail_txt);
1059 }
01b60707 1060 fail_txt = NULL;
7969d803 1061
451680f1 1062err_out:
60697682
GS
1063 if (fail_txt)
1064 srd_err("Failed to load decoder %s: %s", module_name, fail_txt);
6d67d057 1065 decoder_free(d);
514b2edc 1066 PyGILState_Release(gstate);
b2c19614 1067
6d67d057 1068 return SRD_ERR_PYTHON;
451680f1
BV
1069}
1070
582c8473
BV
1071/**
1072 * Return a protocol decoder's docstring.
1073 *
f8c24e2e 1074 * @param dec The loaded protocol decoder. Must not be NULL.
582c8473 1075 *
361fdcaa 1076 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 1077 * documentation. The caller is responsible for free'ing the buffer.
8c664ca2
UH
1078 *
1079 * @since 0.1.0
582c8473 1080 */
b33b8aa5 1081SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 1082{
e7edca07 1083 PyObject *py_str;
451680f1 1084 char *doc;
514b2edc 1085 PyGILState_STATE gstate;
451680f1 1086
e195c025
BV
1087 if (!srd_check_init())
1088 return NULL;
1089
f8c24e2e 1090 if (!dec || !dec->py_mod)
e195c025
BV
1091 return NULL;
1092
514b2edc
UH
1093 gstate = PyGILState_Ensure();
1094
e7edca07 1095 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
514b2edc 1096 goto err;
e7edca07
BV
1097
1098 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
201a85a8 1099 srd_exception_catch("Failed to get docstring");
514b2edc 1100 goto err;
e7edca07
BV
1101 }
1102
451680f1 1103 doc = NULL;
e7edca07
BV
1104 if (py_str != Py_None)
1105 py_str_as_str(py_str, &doc);
6d67d057 1106 Py_DECREF(py_str);
451680f1 1107
514b2edc
UH
1108 PyGILState_Release(gstate);
1109
451680f1 1110 return doc;
514b2edc
UH
1111
1112err:
1113 PyGILState_Release(gstate);
1114
1115 return NULL;
b2c19614
BV
1116}
1117
b2c19614 1118/**
4cc0d9fe 1119 * Unload the specified protocol decoder.
451680f1 1120 *
fa12a21e 1121 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
1122 *
1123 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
1124 *
1125 * @since 0.1.0
b2c19614 1126 */
9d122fd5 1127SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 1128{
32cfb920 1129 struct srd_session *sess;
2f395bff
BV
1130 GSList *l;
1131
e195c025
BV
1132 if (!srd_check_init())
1133 return SRD_ERR;
1134
1135 if (!dec)
1136 return SRD_ERR_ARG;
1137
361fdcaa
UH
1138 /*
1139 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
1140 * but they could be anywhere in the stack, just free the entire
1141 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
1142 * instances, and rebuild the stack.
1143 */
32cfb920
BV
1144 for (l = sessions; l; l = l->next) {
1145 sess = l->data;
3262ef02 1146 srd_inst_free_all(sess);
32cfb920 1147 }
fa12a21e 1148
c63f5a87
UH
1149 /* Remove the PD from the list of loaded decoders. */
1150 pd_list = g_slist_remove(pd_list, dec);
1151
6d67d057 1152 decoder_free(dec);
b2c19614
BV
1153
1154 return SRD_OK;
1155}
1156
1d757906 1157static void srd_decoder_load_all_zip_path(char *zip_path)
6a034159
MC
1158{
1159 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
1160 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
1161 Py_ssize_t pos = 0;
1162 char *prefix;
1163 size_t prefix_len;
514b2edc 1164 PyGILState_STATE gstate;
6a034159
MC
1165
1166 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
1167
514b2edc
UH
1168 gstate = PyGILState_Ensure();
1169
e9dd2fea 1170 zipimport_mod = py_import_by_name("zipimport");
6a034159
MC
1171 if (zipimport_mod == NULL)
1172 goto err_out;
1173
1174 zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter");
1175 if (zipimporter_class == NULL)
1176 goto err_out;
1177
1d757906 1178 zipimporter = PyObject_CallFunction(zipimporter_class, "s", zip_path);
6a034159
MC
1179 if (zipimporter == NULL)
1180 goto err_out;
1181
1182 prefix_obj = PyObject_GetAttrString(zipimporter, "prefix");
1183 if (prefix_obj == NULL)
1184 goto err_out;
1185
1186 files = PyObject_GetAttrString(zipimporter, "_files");
6d67d057 1187 if (files == NULL || !PyDict_Check(files))
6a034159
MC
1188 goto err_out;
1189
1190 set = PySet_New(NULL);
1191 if (set == NULL)
1192 goto err_out;
1193
1194 if (py_str_as_str(prefix_obj, &prefix) != SRD_OK)
1195 goto err_out;
1196
1197 prefix_len = strlen(prefix);
1198
1199 while (PyDict_Next(files, &pos, &key, &value)) {
1200 char *path, *slash;
1201 if (py_str_as_str(key, &path) == SRD_OK) {
6d67d057
DE
1202 if (strlen(path) > prefix_len
1203 && memcmp(path, prefix, prefix_len) == 0
1204 && (slash = strchr(path + prefix_len, '/'))) {
1205
1206 modname = PyUnicode_FromStringAndSize(path + prefix_len,
1207 slash - (path + prefix_len));
6a034159
MC
1208 if (modname == NULL) {
1209 PyErr_Clear();
1210 } else {
1211 PySet_Add(set, modname);
6d67d057 1212 Py_DECREF(modname);
6a034159
MC
1213 }
1214 }
6d67d057 1215 g_free(path);
6a034159
MC
1216 }
1217 }
6d67d057 1218 g_free(prefix);
6a034159
MC
1219
1220 while ((modname = PySet_Pop(set))) {
1221 char *modname_str;
1222 if (py_str_as_str(modname, &modname_str) == SRD_OK) {
1223 /* The directory name is the module name (e.g. "i2c"). */
1224 srd_decoder_load(modname_str);
6d67d057 1225 g_free(modname_str);
6a034159 1226 }
6d67d057 1227 Py_DECREF(modname);
6a034159
MC
1228 }
1229
1230err_out:
1231 Py_XDECREF(set);
1232 Py_XDECREF(files);
1233 Py_XDECREF(prefix_obj);
1234 Py_XDECREF(zipimporter);
1235 Py_XDECREF(zipimporter_class);
1236 Py_XDECREF(zipimport_mod);
1237 PyErr_Clear();
514b2edc 1238 PyGILState_Release(gstate);
6a034159
MC
1239}
1240
ea81b49a
BV
1241static void srd_decoder_load_all_path(char *path)
1242{
1243 GDir *dir;
1244 const gchar *direntry;
1245
6a034159 1246 if (!(dir = g_dir_open(path, 0, NULL))) {
7969d803 1247 /* Not really fatal. Try zipimport method too. */
6a034159 1248 srd_decoder_load_all_zip_path(path);
ea81b49a 1249 return;
6a034159 1250 }
ea81b49a 1251
7969d803
UH
1252 /*
1253 * This ignores errors returned by srd_decoder_load(). That
ea81b49a 1254 * function will have logged the cause, but in any case we
7969d803
UH
1255 * want to continue anyway.
1256 */
ea81b49a
BV
1257 while ((direntry = g_dir_read_name(dir)) != NULL) {
1258 /* The directory name is the module name (e.g. "i2c"). */
1259 srd_decoder_load(direntry);
1260 }
1261 g_dir_close(dir);
ea81b49a
BV
1262}
1263
582c8473 1264/**
9b37109d 1265 * Load all installed protocol decoders.
582c8473
BV
1266 *
1267 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
1268 *
1269 * @since 0.1.0
582c8473 1270 */
8ad6e500 1271SRD_API int srd_decoder_load_all(void)
b2c19614 1272{
ea81b49a 1273 GSList *l;
b2c19614 1274
e195c025
BV
1275 if (!srd_check_init())
1276 return SRD_ERR;
1277
ea81b49a
BV
1278 for (l = searchpaths; l; l = l->next)
1279 srd_decoder_load_all_path(l->data);
b2c19614
BV
1280
1281 return SRD_OK;
1282}
1283
611a7340
JB
1284static void srd_decoder_unload_cb(void *arg, void *ignored)
1285{
1286 (void)ignored;
1287
1288 srd_decoder_unload((struct srd_decoder *)arg);
1289}
1290
b2c19614 1291/**
582c8473
BV
1292 * Unload all loaded protocol decoders.
1293 *
1294 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
1295 *
1296 * @since 0.1.0
b2c19614 1297 */
8ad6e500 1298SRD_API int srd_decoder_unload_all(void)
b2c19614 1299{
611a7340 1300 g_slist_foreach(pd_list, srd_decoder_unload_cb, NULL);
820bf448
BV
1301 g_slist_free(pd_list);
1302 pd_list = NULL;
b2c19614
BV
1303
1304 return SRD_OK;
1305}
4895418c
UH
1306
1307/** @} */