Change PD options to be a tuple of dictionaries.
[libsigrokdecode.git] / decoder.c
1 /*
2  * This file is part of the libsigrokdecode project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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
21 #include "config.h"
22 #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "libsigrokdecode-internal.h"
24 #include <glib.h>
25
26 /**
27  * @file
28  *
29  * Listing, loading, unloading, and handling protocol decoders.
30  */
31
32 /**
33  * @defgroup grp_decoder Protocol decoders
34  *
35  * Handling protocol decoders.
36  *
37  * @{
38  */
39
40 /** @cond PRIVATE */
41
42 /* The list of protocol decoders. */
43 SRD_PRIV GSList *pd_list = NULL;
44
45 /* srd.c */
46 extern GSList *searchpaths;
47
48 /* session.c */
49 extern GSList *sessions;
50
51 /* module_sigrokdecode.c */
52 extern SRD_PRIV PyObject *mod_sigrokdecode;
53
54 /** @endcond */
55
56 /**
57  * Returns the list of supported/loaded protocol decoders.
58  *
59  * This is a GSList containing the names of the decoders as strings.
60  *
61  * @return List of decoders, NULL if none are supported or loaded.
62  *
63  * @since 0.1.0 (but the API changed in 0.2.0)
64  */
65 SRD_API const GSList *srd_decoder_list(void)
66 {
67         return pd_list;
68 }
69
70 /**
71  * Get the decoder with the specified ID.
72  *
73  * @param id The ID string of the decoder to return.
74  *
75  * @return The decoder with the specified ID, or NULL if not found.
76  *
77  * @since 0.1.0
78  */
79 SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
80 {
81         GSList *l;
82         struct srd_decoder *dec;
83
84         for (l = pd_list; l; l = l->next) {
85                 dec = l->data;
86                 if (!strcmp(dec->id, id))
87                         return dec;
88         }
89
90         return NULL;
91 }
92
93 static int get_probes(const struct srd_decoder *d, const char *attr,
94                 GSList **pl)
95 {
96         PyObject *py_probelist, *py_entry;
97         struct srd_probe *p;
98         int ret, num_probes, i;
99
100         if (!PyObject_HasAttrString(d->py_dec, attr))
101                 /* No probes of this type specified. */
102                 return SRD_OK;
103
104         py_probelist = PyObject_GetAttrString(d->py_dec, attr);
105         if (!PyList_Check(py_probelist)) {
106                 srd_err("Protocol decoder %s %s attribute is not a list.",
107                                 d->name, attr);
108                 return SRD_ERR_PYTHON;
109         }
110
111         if ((num_probes = PyList_Size(py_probelist)) == 0)
112                 /* Empty probelist. */
113                 return SRD_OK;
114
115         ret = SRD_OK;
116         for (i = 0; i < num_probes; i++) {
117                 py_entry = PyList_GetItem(py_probelist, i);
118                 if (!PyDict_Check(py_entry)) {
119                         srd_err("Protocol decoder %s %s attribute is not "
120                                 "a list with dict elements.", d->name, attr);
121                         ret = SRD_ERR_PYTHON;
122                         break;
123                 }
124
125                 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
126                         srd_err("Failed to g_malloc() struct srd_probe.");
127                         ret = SRD_ERR_MALLOC;
128                         break;
129                 }
130
131                 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK) {
132                         ret = SRD_ERR_PYTHON;
133                         break;
134                 }
135                 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK) {
136                         ret = SRD_ERR_PYTHON;
137                         break;
138                 }
139                 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK) {
140                         ret = SRD_ERR_PYTHON;
141                         break;
142                 }
143                 p->order = i;
144
145                 *pl = g_slist_append(*pl, p);
146         }
147
148         Py_DecRef(py_probelist);
149
150         return ret;
151 }
152
153 static int get_options(struct srd_decoder *d)
154 {
155         PyObject *py_opts, *py_opt, *py_val, *py_default, *py_item;
156         Py_ssize_t opt, i;
157         struct srd_decoder_option *o;
158         GVariant *gvar;
159         gint64 lval;
160         double dval;
161         int overflow;
162         char *sval;
163
164         if (!PyObject_HasAttrString(d->py_dec, "options"))
165                 /* No options, that's fine. */
166                 return SRD_OK;
167
168         /* If present, options must be a tuple. */
169         py_opts = PyObject_GetAttrString(d->py_dec, "options");
170         if (!PyTuple_Check(py_opts)) {
171                 srd_err("Protocol decoder %s: options attribute is not "
172                                 "a tuple.", d->id);
173                 return SRD_ERR_PYTHON;
174         }
175
176         for (opt = 0; opt < PyTuple_Size(py_opts); opt++) {
177                 py_opt = PyTuple_GetItem(py_opts, opt);
178                 if (!PyDict_Check(py_opt)) {
179                         srd_err("Protocol decoder %s options: each option "
180                                         "must consist of a dictionary.", d->name);
181                         return SRD_ERR_PYTHON;
182                 }
183                 if (!(py_val = PyDict_GetItemString(py_opt, "id"))) {
184                         srd_err("Protocol decoder %s option %d has no "
185                                         "id.", d->name);
186                         return SRD_ERR_PYTHON;
187                 }
188                 o = g_malloc0(sizeof(struct srd_decoder_option));
189                 py_str_as_str(py_val, &o->id);
190
191                 if ((py_val = PyDict_GetItemString(py_opt, "desc")))
192                         py_str_as_str(py_val, &o->desc);
193
194                 if ((py_default = PyDict_GetItemString(py_opt, "default"))) {
195                         if (PyUnicode_Check(py_default)) {
196                                 /* UTF-8 string */
197                                 py_str_as_str(py_default, &sval);
198                                 o->def = g_variant_new_string(sval);
199                                 g_free(sval);
200                         } else if (PyLong_Check(py_default)) {
201                                 /* Long */
202                                 lval = PyLong_AsLongAndOverflow(py_default, &overflow);
203                                 if (overflow) {
204                                         /* Value is < LONG_MIN or > LONG_MAX */
205                                         PyErr_Clear();
206                                         srd_err("Protocol decoder %s option 'default' has "
207                                                         "invalid default value.", d->name);
208                                         return SRD_ERR_PYTHON;
209                                 }
210                                 o->def = g_variant_new_int64(lval);
211                         } else if (PyFloat_Check(py_default)) {
212                                 /* Float */
213                                 if ((dval = PyFloat_AsDouble(py_default)) == -1.0) {
214                                         PyErr_Clear();
215                                         srd_err("Protocol decoder %s option 'default' has "
216                                                         "invalid default value.", d->name);
217                                         return SRD_ERR_PYTHON;
218                                 }
219                                 o->def = g_variant_new_double(dval);
220                         } else {
221                                 srd_err("Protocol decoder %s option 'default' has "
222                                                 "value of unsupported type '%s'.", d->name,
223                                                 Py_TYPE(py_default)->tp_name);
224                                 return SRD_ERR_PYTHON;
225                         }
226                         g_variant_ref_sink(o->def);
227                 }
228
229                 if ((py_val = PyDict_GetItemString(py_opt, "values"))) {
230                         /* A default is required if a list of values is
231                          * given, since it's used to verify their type. */
232                         if (!o->def) {
233                                 srd_err("No default for option '%s'", o->id);
234                                 return SRD_ERR_PYTHON;
235                         }
236                         if (!PyTuple_Check(py_val)) {
237                                 srd_err("Option '%s' values should be a tuple.", o->id);
238                                 return SRD_ERR_PYTHON;
239                         }
240                         for (i = 0; i < PyTuple_Size(py_val); i++) {
241                                 py_item = PyTuple_GetItem(py_val, i);
242                                 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
243                                         srd_err("All values for option '%s' must be "
244                                                         "of the same type as the default.",
245                                                         o->id);
246                                         return SRD_ERR_PYTHON;
247                                 }
248                                 if (PyUnicode_Check(py_item)) {
249                                         /* UTF-8 string */
250                                         py_str_as_str(py_item, &sval);
251                                         gvar = g_variant_new_string(sval);
252                                         g_variant_ref_sink(gvar);
253                                         g_free(sval);
254                                         o->values = g_slist_append(o->values, gvar);
255                                 } else if (PyLong_Check(py_item)) {
256                                         /* Long */
257                                         lval = PyLong_AsLongAndOverflow(py_default, &overflow);
258                                         if (overflow) {
259                                                 /* Value is < LONG_MIN or > LONG_MAX */
260                                                 PyErr_Clear();
261                                                 srd_err("Protocol decoder %s option 'values' "
262                                                                 "has invalid value.", d->name);
263                                                 return SRD_ERR_PYTHON;
264                                         }
265                                         gvar = g_variant_new_int64(lval);
266                                         g_variant_ref_sink(gvar);
267                                         o->values = g_slist_append(o->values, gvar);
268                                 } else if (PyFloat_Check(py_default)) {
269                                         /* Float */
270                                         if ((dval = PyFloat_AsDouble(py_default)) == -1.0) {
271                                                 PyErr_Clear();
272                                                 srd_err("Protocol decoder %s option 'default' has "
273                                                                 "invalid default value.", d->name);
274                                                 return SRD_ERR_PYTHON;
275                                         }
276                                         gvar = g_variant_new_double(dval);
277                                         g_variant_ref_sink(gvar);
278                                         o->values = g_slist_append(o->values, gvar);
279                                 }
280                         }
281                 }
282                 d->options = g_slist_append(d->options, o);
283         }
284
285         return SRD_OK;
286 }
287
288 /**
289  * Load a protocol decoder module into the embedded Python interpreter.
290  *
291  * @param module_name The module name to be loaded.
292  *
293  * @return SRD_OK upon success, a (negative) error code otherwise.
294  *
295  * @since 0.1.0
296  */
297 SRD_API int srd_decoder_load(const char *module_name)
298 {
299         PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
300         PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
301         PyObject *py_ann_classes, *py_long;
302         struct srd_decoder *d;
303         int ret, i, j;
304         char **ann, **bin, *ann_row_id, *ann_row_desc;
305         struct srd_probe *p;
306         GSList *l, *ann_classes;
307         struct srd_decoder_annotation_row *ann_row;
308
309         if (!srd_check_init())
310                 return SRD_ERR;
311
312         if (!module_name)
313                 return SRD_ERR_ARG;
314
315         if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
316                 /* Module was already imported. */
317                 return SRD_OK;
318         }
319
320         srd_dbg("Loading protocol decoder '%s'.", module_name);
321
322         py_basedec = py_method = py_attr = NULL;
323
324         if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
325                 srd_dbg("Failed to g_malloc() struct srd_decoder.");
326                 ret = SRD_ERR_MALLOC;
327                 goto err_out;
328         }
329
330         ret = SRD_ERR_PYTHON;
331
332         /* Import the Python module. */
333         if (!(d->py_mod = PyImport_ImportModule(module_name))) {
334                 srd_exception_catch("Import of '%s' failed.", module_name);
335                 goto err_out;
336         }
337
338         /* Get the 'Decoder' class as Python object. */
339         if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
340                 /* This generated an AttributeError exception. */
341                 PyErr_Clear();
342                 srd_err("Decoder class not found in protocol decoder %s.",
343                         module_name);
344                 goto err_out;
345         }
346
347         if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
348                 srd_dbg("sigrokdecode module not loaded.");
349                 goto err_out;
350         }
351
352         if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
353                 srd_err("Decoder class in protocol decoder module %s is not "
354                         "a subclass of sigrokdecode.Decoder.", module_name);
355                 goto err_out;
356         }
357         Py_CLEAR(py_basedec);
358
359         /* Check for a proper start() method. */
360         if (!PyObject_HasAttrString(d->py_dec, "start")) {
361                 srd_err("Protocol decoder %s has no start() method Decoder "
362                         "class.", module_name);
363                 goto err_out;
364         }
365         py_method = PyObject_GetAttrString(d->py_dec, "start");
366         if (!PyFunction_Check(py_method)) {
367                 srd_err("Protocol decoder %s Decoder class attribute 'start' "
368                         "is not a method.", module_name);
369                 goto err_out;
370         }
371         Py_CLEAR(py_method);
372
373         /* Check for a proper decode() method. */
374         if (!PyObject_HasAttrString(d->py_dec, "decode")) {
375                 srd_err("Protocol decoder %s has no decode() method Decoder "
376                         "class.", module_name);
377                 goto err_out;
378         }
379         py_method = PyObject_GetAttrString(d->py_dec, "decode");
380         if (!PyFunction_Check(py_method)) {
381                 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
382                         "is not a method.", module_name);
383                 goto err_out;
384         }
385         Py_CLEAR(py_method);
386
387         /* Store required fields in newly allocated strings. */
388         if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
389                 goto err_out;
390
391         if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
392                 goto err_out;
393
394         if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
395                 goto err_out;
396
397         if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
398                 goto err_out;
399
400         if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
401                 goto err_out;
402
403         /* All options and their default values. */
404         if (get_options(d) != SRD_OK)
405                 goto err_out;
406
407         /* Check and import required probes. */
408         if (get_probes(d, "probes", &d->probes) != SRD_OK)
409                 goto err_out;
410
411         /* Check and import optional probes. */
412         if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
413                 goto err_out;
414
415         /*
416          * Fix order numbers for the optional probes.
417          *
418          * Example:
419          * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
420          * 'order' fields in the d->probes list = 0, 1, 2.
421          * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
422          */
423         for (l = d->opt_probes; l; l = l->next) {
424                 p = l->data;
425                 p->order += g_slist_length(d->probes);
426         }
427
428         /* Convert annotation class attribute to GSList of char **. */
429         d->annotations = NULL;
430         if (PyObject_HasAttrString(d->py_dec, "annotations")) {
431                 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
432                 if (!PyList_Check(py_annlist)) {
433                         srd_err("Protocol decoder %s annotations should "
434                                         "be a list.", module_name);
435                         goto err_out;
436                 }
437                 for (i = 0; i < PyList_Size(py_annlist); i++) {
438                         py_ann = PyList_GetItem(py_annlist, i);
439                         if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
440                                 srd_err("Protocol decoder %s annotation %d should "
441                                                 "be a list with two elements.", module_name, i + 1);
442                                 goto err_out;
443                         }
444
445                         if (py_strseq_to_char(py_ann, &ann) != SRD_OK) {
446                                 goto err_out;
447                         }
448                         d->annotations = g_slist_append(d->annotations, ann);
449                 }
450         }
451
452         /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
453         d->annotation_rows = NULL;
454         if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) {
455                 py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows");
456                 if (!PyTuple_Check(py_ann_rows)) {
457                         srd_err("Protocol decoder %s annotation row list "
458                                 "must be a tuple.", module_name);
459                         goto err_out;
460                 }
461                 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
462                         py_ann_row = PyTuple_GetItem(py_ann_rows, i);
463                         if (!PyTuple_Check(py_ann_row)) {
464                                 srd_err("Protocol decoder %s annotation rows "
465                                         "must be tuples.", module_name);
466                                 goto err_out;
467                         }
468                         if (PyTuple_Size(py_ann_row) != 3
469                                         || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
470                                         || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
471                                         || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
472                                 srd_err("Protocol decoder %s annotation rows "
473                                         "must contain tuples containing two "
474                                         "strings and a tuple.", module_name);
475                                 goto err_out;
476                         }
477
478                         if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK)
479                                 goto err_out;
480
481                         if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK)
482                                 goto err_out;
483
484                         py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
485                         ann_classes = NULL;
486                         for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
487                                 py_long = PyTuple_GetItem(py_ann_classes, j);
488                                 if (!PyLong_Check(py_long)) {
489                                         srd_err("Protocol decoder %s annotation row class "
490                                                 "list must only contain numbers.", module_name);
491                                         goto err_out;
492                                 }
493                                 ann_classes = g_slist_append(ann_classes,
494                                         GINT_TO_POINTER(PyLong_AsLong(py_long)));
495                         }
496
497                         ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
498                         ann_row->id = ann_row_id;
499                         ann_row->desc = ann_row_desc;
500                         ann_row->ann_classes = ann_classes;
501                         d->annotation_rows = g_slist_append(d->annotation_rows, ann_row);
502                 }
503         }
504
505         /* Convert binary class to GSList of char *. */
506         d->binary = NULL;
507         if (PyObject_HasAttrString(d->py_dec, "binary")) {
508                 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
509                 if (!PyTuple_Check(py_bin_classes)) {
510                         srd_err("Protocol decoder %s binary classes should "
511                                         "be a tuple.", module_name);
512                         goto err_out;
513                 }
514                 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
515                         py_bin_class = PyTuple_GetItem(py_bin_classes, i);
516                         if (!PyTuple_Check(py_bin_class)) {
517                                 srd_err("Protocol decoder %s binary classes "
518                                                 "should consist of tuples.", module_name);
519                                 goto err_out;
520                         }
521                         if (PyTuple_Size(py_bin_class) != 2
522                                         || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
523                                         || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
524                                 srd_err("Protocol decoder %s binary classes should "
525                                                 "contain tuples with two strings.", module_name);
526                                 goto err_out;
527                         }
528
529                         if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) {
530                                 goto err_out;
531                         }
532                         d->binary = g_slist_append(d->binary, bin);
533                 }
534         }
535
536         /* Append it to the list of supported/loaded decoders. */
537         pd_list = g_slist_append(pd_list, d);
538
539         ret = SRD_OK;
540
541 err_out:
542         if (ret != SRD_OK) {
543                 Py_XDECREF(py_method);
544                 Py_XDECREF(py_basedec);
545                 Py_XDECREF(d->py_dec);
546                 Py_XDECREF(d->py_mod);
547                 g_free(d);
548         }
549
550         return ret;
551 }
552
553 /**
554  * Return a protocol decoder's docstring.
555  *
556  * @param dec The loaded protocol decoder.
557  *
558  * @return A newly allocated buffer containing the protocol decoder's
559  *         documentation. The caller is responsible for free'ing the buffer.
560  *
561  * @since 0.1.0
562  */
563 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
564 {
565         PyObject *py_str;
566         char *doc;
567
568         if (!srd_check_init())
569                 return NULL;
570
571         if (!dec)
572                 return NULL;
573
574         if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
575                 return NULL;
576
577         if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
578                 srd_exception_catch("");
579                 return NULL;
580         }
581
582         doc = NULL;
583         if (py_str != Py_None)
584                 py_str_as_str(py_str, &doc);
585         Py_DecRef(py_str);
586
587         return doc;
588 }
589
590 static void free_probes(GSList *probelist)
591 {
592         GSList *l;
593         struct srd_probe *p;
594
595         if (probelist == NULL)
596                 return;
597
598         for (l = probelist; l; l = l->next) {
599                 p = l->data;
600                 g_free(p->id);
601                 g_free(p->name);
602                 g_free(p->desc);
603                 g_free(p);
604         }
605         g_slist_free(probelist);
606 }
607
608 /**
609  * Unload the specified protocol decoder.
610  *
611  * @param dec The struct srd_decoder to be unloaded.
612  *
613  * @return SRD_OK upon success, a (negative) error code otherwise.
614  *
615  * @since 0.1.0
616  */
617 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
618 {
619         struct srd_decoder_option *o;
620         struct srd_session *sess;
621         GSList *l;
622
623         if (!srd_check_init())
624                 return SRD_ERR;
625
626         if (!dec)
627                 return SRD_ERR_ARG;
628
629         srd_dbg("Unloading protocol decoder '%s'.", dec->name);
630
631         /*
632          * Since any instances of this decoder need to be released as well,
633          * but they could be anywhere in the stack, just free the entire
634          * stack. A frontend reloading a decoder thus has to restart all
635          * instances, and rebuild the stack.
636          */
637         for (l = sessions; l; l = l->next) {
638                 sess = l->data;
639                 srd_inst_free_all(sess, NULL);
640         }
641
642         for (l = dec->options; l; l = l->next) {
643                 o = l->data;
644                 g_free(o->id);
645                 g_free(o->desc);
646                 g_variant_unref(o->def);
647                 g_free(o);
648         }
649         g_slist_free(dec->options);
650
651         free_probes(dec->probes);
652         free_probes(dec->opt_probes);
653         g_free(dec->id);
654         g_free(dec->name);
655         g_free(dec->longname);
656         g_free(dec->desc);
657         g_free(dec->license);
658
659         /* The module's Decoder class. */
660         Py_XDECREF(dec->py_dec);
661         /* The module itself. */
662         Py_XDECREF(dec->py_mod);
663
664         g_free(dec);
665
666         return SRD_OK;
667 }
668
669 static void srd_decoder_load_all_path(char *path)
670 {
671         GDir *dir;
672         const gchar *direntry;
673
674         if (!(dir = g_dir_open(path, 0, NULL)))
675                 /* Not really fatal */
676                 return;
677
678         /* This ignores errors returned by srd_decoder_load(). That
679          * function will have logged the cause, but in any case we
680          * want to continue anyway. */
681         while ((direntry = g_dir_read_name(dir)) != NULL) {
682                 /* The directory name is the module name (e.g. "i2c"). */
683                 srd_decoder_load(direntry);
684         }
685         g_dir_close(dir);
686
687 }
688
689 /**
690  * Load all installed protocol decoders.
691  *
692  * @return SRD_OK upon success, a (negative) error code otherwise.
693  *
694  * @since 0.1.0
695  */
696 SRD_API int srd_decoder_load_all(void)
697 {
698         GSList *l;
699
700         if (!srd_check_init())
701                 return SRD_ERR;
702
703         for (l = searchpaths; l; l = l->next)
704                 srd_decoder_load_all_path(l->data);
705
706         return SRD_OK;
707 }
708
709 /**
710  * Unload all loaded protocol decoders.
711  *
712  * @return SRD_OK upon success, a (negative) error code otherwise.
713  *
714  * @since 0.1.0
715  */
716 SRD_API int srd_decoder_unload_all(void)
717 {
718         GSList *l;
719         struct srd_decoder *dec;
720
721         for (l = pd_list; l; l = l->next) {
722                 dec = l->data;
723                 srd_decoder_unload(dec);
724         }
725         g_slist_free(pd_list);
726         pd_list = NULL;
727
728         return SRD_OK;
729 }
730
731 /** @} */