2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "libsigrokdecode.h"
29 * Listing, loading, unloading, and handling protocol decoders.
33 * @defgroup grp_decoder Protocol decoders
35 * Handling protocol decoders.
42 /* The list of loaded protocol decoders. */
43 static GSList *pd_list = NULL;
46 extern SRD_PRIV GSList *searchpaths;
49 extern SRD_PRIV GSList *sessions;
50 extern SRD_PRIV int max_session_id;
52 /* module_sigrokdecode.c */
53 extern SRD_PRIV PyObject *mod_sigrokdecode;
57 static gboolean srd_check_init(void)
59 if (max_session_id < 0) {
60 srd_err("Library is not initialized.");
67 * Returns the list of loaded protocol decoders.
69 * This is a GSList of pointers to struct srd_decoder items.
71 * @return List of decoders, NULL if none are supported or loaded.
75 SRD_API const GSList *srd_decoder_list(void)
81 * Get the decoder with the specified ID.
83 * @param id The ID string of the decoder to return.
85 * @return The decoder with the specified ID, or NULL if not found.
89 SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
92 struct srd_decoder *dec;
94 for (l = pd_list; l; l = l->next) {
96 if (!strcmp(dec->id, id))
103 static void channel_free(void *data)
105 struct srd_channel *ch = data;
116 static void variant_free(void *data)
118 GVariant *var = data;
123 g_variant_unref(var);
126 static void annotation_row_free(void *data)
128 struct srd_decoder_annotation_row *row = data;
133 g_slist_free(row->ann_classes);
139 static void logic_output_channel_free(void *data)
141 struct srd_decoder_logic_output_channel *logic_out_ch = data;
146 g_free(logic_out_ch->desc);
147 g_free(logic_out_ch->id);
148 g_free(logic_out_ch);
151 static void decoder_option_free(void *data)
153 struct srd_decoder_option *opt = data;
158 g_slist_free_full(opt->values, &variant_free);
159 variant_free(opt->def);
165 static void decoder_free(struct srd_decoder *dec)
167 PyGILState_STATE gstate;
172 gstate = PyGILState_Ensure();
173 Py_XDECREF(dec->py_dec);
174 Py_XDECREF(dec->py_mod);
175 PyGILState_Release(gstate);
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);
184 g_slist_free_full(dec->outputs, g_free);
185 g_slist_free_full(dec->inputs, g_free);
186 g_slist_free_full(dec->tags, g_free);
187 g_free(dec->license);
189 g_free(dec->longname);
196 static int get_channels(const struct srd_decoder *d, const char *attr,
197 GSList **out_pdchl, int offset)
199 PyObject *py_channellist, *py_entry;
200 struct srd_channel *pdch;
203 PyGILState_STATE gstate;
205 gstate = PyGILState_Ensure();
207 if (!PyObject_HasAttrString(d->py_dec, attr)) {
208 /* No channels of this type specified. */
209 PyGILState_Release(gstate);
215 py_channellist = PyObject_GetAttrString(d->py_dec, attr);
219 if (!PyTuple_Check(py_channellist)) {
220 srd_err("Protocol decoder %s %s attribute is not a tuple.",
225 for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) {
226 py_entry = PyTuple_GetItem(py_channellist, i);
230 if (!PyDict_Check(py_entry)) {
231 srd_err("Protocol decoder %s %s attribute is not "
232 "a list of dict elements.", d->name, attr);
235 pdch = g_malloc(sizeof(struct srd_channel));
236 /* Add to list right away so it doesn't get lost. */
237 pdchl = g_slist_prepend(pdchl, pdch);
239 if (py_dictitem_as_str(py_entry, "id", &pdch->id) != SRD_OK)
241 if (py_dictitem_as_str(py_entry, "name", &pdch->name) != SRD_OK)
243 if (py_dictitem_as_str(py_entry, "desc", &pdch->desc) != SRD_OK)
246 pdch->order = offset + i;
249 Py_DECREF(py_channellist);
252 PyGILState_Release(gstate);
257 srd_exception_catch("Failed to get %s list of %s decoder",
261 g_slist_free_full(pdchl, &channel_free);
262 Py_XDECREF(py_channellist);
263 PyGILState_Release(gstate);
265 return SRD_ERR_PYTHON;
268 static int get_options(struct srd_decoder *d)
270 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
272 struct srd_decoder_option *o;
275 PyGILState_STATE gstate;
277 gstate = PyGILState_Ensure();
279 if (!PyObject_HasAttrString(d->py_dec, "options")) {
280 /* No options, that's fine. */
281 PyGILState_Release(gstate);
287 /* If present, options must be a tuple. */
288 py_opts = PyObject_GetAttrString(d->py_dec, "options");
292 if (!PyTuple_Check(py_opts)) {
293 srd_err("Protocol decoder %s: options attribute is not "
298 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
299 py_opt = PyTuple_GetItem(py_opts, opt);
303 if (!PyDict_Check(py_opt)) {
304 srd_err("Protocol decoder %s options: each option "
305 "must consist of a dictionary.", d->name);
309 o = g_malloc0(sizeof(struct srd_decoder_option));
310 /* Add to list right away so it doesn't get lost. */
311 options = g_slist_prepend(options, o);
313 py_str = PyDict_GetItemString(py_opt, "id");
315 srd_err("Protocol decoder %s option %zd has no ID.",
319 if (py_str_as_str(py_str, &o->id) != SRD_OK)
322 py_str = PyDict_GetItemString(py_opt, "desc");
324 if (py_str_as_str(py_str, &o->desc) != SRD_OK)
328 py_default = PyDict_GetItemString(py_opt, "default");
330 gvar = py_obj_to_variant(py_default);
332 srd_err("Protocol decoder %s option 'default' has "
333 "invalid default value.", d->name);
336 o->def = g_variant_ref_sink(gvar);
339 py_values = PyDict_GetItemString(py_opt, "values");
342 * A default is required if a list of values is
343 * given, since it's used to verify their type.
346 srd_err("No default for option '%s'.", o->id);
349 if (!PyTuple_Check(py_values)) {
350 srd_err("Option '%s' values should be a tuple.", o->id);
354 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
355 py_item = PyTuple_GetItem(py_values, i);
359 if (py_default && (Py_TYPE(py_default) != Py_TYPE(py_item))) {
360 srd_err("All values for option '%s' must be "
361 "of the same type as the default.",
365 gvar = py_obj_to_variant(py_item);
367 srd_err("Protocol decoder %s option 'values' "
368 "contains invalid value.", d->name);
371 o->values = g_slist_prepend(o->values,
372 g_variant_ref_sink(gvar));
376 d->options = options;
378 PyGILState_Release(gstate);
383 srd_exception_catch("Failed to get %s decoder options", d->name);
386 g_slist_free_full(options, &decoder_option_free);
388 PyGILState_Release(gstate);
390 return SRD_ERR_PYTHON;
393 /* Convert annotation class attribute to GSList of char **. */
394 static int get_annotations(struct srd_decoder *dec)
396 PyObject *py_annlist, *py_ann;
400 PyGILState_STATE gstate;
402 gstate = PyGILState_Ensure();
404 if (!PyObject_HasAttrString(dec->py_dec, "annotations")) {
405 PyGILState_Release(gstate);
411 py_annlist = PyObject_GetAttrString(dec->py_dec, "annotations");
415 if (!PyTuple_Check(py_annlist)) {
416 srd_err("Protocol decoder %s annotations should be a tuple.",
421 for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) {
422 py_ann = PyTuple_GetItem(py_annlist, i);
426 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
427 srd_err("Protocol decoder %s annotation %zd should be a tuple with two elements.",
431 if (py_strseq_to_char(py_ann, &annpair) != SRD_OK)
434 annotations = g_slist_prepend(annotations, annpair);
436 dec->annotations = annotations;
437 Py_DECREF(py_annlist);
438 PyGILState_Release(gstate);
443 srd_exception_catch("Failed to get %s decoder annotations", dec->name);
446 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
447 Py_XDECREF(py_annlist);
448 PyGILState_Release(gstate);
450 return SRD_ERR_PYTHON;
453 /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
454 static int get_annotation_rows(struct srd_decoder *dec)
456 const char *py_member_name = "annotation_rows";
458 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
459 GSList *annotation_rows;
460 struct srd_decoder_annotation_row *ann_row;
463 PyGILState_STATE gstate;
465 gstate = PyGILState_Ensure();
467 if (!PyObject_HasAttrString(dec->py_dec, py_member_name)) {
468 PyGILState_Release(gstate);
472 annotation_rows = NULL;
474 py_ann_rows = PyObject_GetAttrString(dec->py_dec, py_member_name);
478 if (!PyTuple_Check(py_ann_rows)) {
479 srd_err("Protocol decoder %s %s must be a tuple.",
480 dec->name, py_member_name);
484 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
485 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
489 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
490 srd_err("Protocol decoder %s %s must contain only tuples of 3 elements.",
491 dec->name, py_member_name);
494 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
495 /* Add to list right away so it doesn't get lost. */
496 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
498 py_item = PyTuple_GetItem(py_ann_row, 0);
501 if (py_str_as_str(py_item, &ann_row->id) != SRD_OK)
504 py_item = PyTuple_GetItem(py_ann_row, 1);
507 if (py_str_as_str(py_item, &ann_row->desc) != SRD_OK)
510 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
514 if (!PyTuple_Check(py_ann_classes)) {
515 srd_err("Protocol decoder %s %s tuples must have a tuple of numbers as 3rd element.",
516 dec->name, py_member_name);
520 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
521 py_item = PyTuple_GetItem(py_ann_classes, k);
525 if (!PyLong_Check(py_item)) {
526 srd_err("Protocol decoder %s annotation row class tuple must only contain numbers.",
530 class_idx = PyLong_AsSize_t(py_item);
531 if (PyErr_Occurred())
534 ann_row->ann_classes = g_slist_prepend(ann_row->ann_classes,
535 GSIZE_TO_POINTER(class_idx));
538 dec->annotation_rows = annotation_rows;
539 Py_DECREF(py_ann_rows);
540 PyGILState_Release(gstate);
545 srd_exception_catch("Failed to get %s decoder annotation rows",
549 g_slist_free_full(annotation_rows, &annotation_row_free);
550 Py_XDECREF(py_ann_rows);
551 PyGILState_Release(gstate);
553 return SRD_ERR_PYTHON;
556 /* Convert binary classes to GSList of char **. */
557 static int get_binary_classes(struct srd_decoder *dec)
559 PyObject *py_bin_classes, *py_bin_class;
563 PyGILState_STATE gstate;
565 gstate = PyGILState_Ensure();
567 if (!PyObject_HasAttrString(dec->py_dec, "binary")) {
568 PyGILState_Release(gstate);
574 py_bin_classes = PyObject_GetAttrString(dec->py_dec, "binary");
578 if (!PyTuple_Check(py_bin_classes)) {
579 srd_err("Protocol decoder %s binary classes should "
580 "be a tuple.", dec->name);
584 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
585 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
589 if (!PyTuple_Check(py_bin_class)
590 || PyTuple_Size(py_bin_class) != 2) {
591 srd_err("Protocol decoder %s binary classes should "
592 "consist only of tuples of 2 elements.",
596 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK)
599 bin_classes = g_slist_prepend(bin_classes, bin);
601 dec->binary = bin_classes;
602 Py_DECREF(py_bin_classes);
603 PyGILState_Release(gstate);
608 srd_exception_catch("Failed to get %s decoder binary classes",
612 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
613 Py_XDECREF(py_bin_classes);
614 PyGILState_Release(gstate);
616 return SRD_ERR_PYTHON;
619 /* Convert logic_output_channels to GSList of 'struct srd_decoder_logic_output_channel'. */
620 static int get_logic_output_channels(struct srd_decoder *dec)
622 PyObject *py_logic_out_chs, *py_logic_out_ch, *py_item;
623 GSList *logic_out_chs;
624 struct srd_decoder_logic_output_channel *logic_out_ch;
626 PyGILState_STATE gstate;
628 gstate = PyGILState_Ensure();
630 if (!PyObject_HasAttrString(dec->py_dec, "logic_output_channels")) {
631 PyGILState_Release(gstate);
635 logic_out_chs = NULL;
637 py_logic_out_chs = PyObject_GetAttrString(dec->py_dec, "logic_output_channels");
638 if (!py_logic_out_chs)
641 if (!PyTuple_Check(py_logic_out_chs)) {
642 srd_err("Protocol decoder %s logic_output_channels "
643 "must be a tuple.", dec->name);
647 for (i = PyTuple_Size(py_logic_out_chs) - 1; i >= 0; i--) {
648 py_logic_out_ch = PyTuple_GetItem(py_logic_out_chs, i);
649 if (!py_logic_out_ch)
652 if (!PyTuple_Check(py_logic_out_ch) || PyTuple_Size(py_logic_out_ch) != 2) {
653 srd_err("Protocol decoder %s logic_output_channels "
654 "must contain only tuples of 2 elements.",
658 logic_out_ch = g_malloc0(sizeof(*logic_out_ch));
659 /* Add to list right away so it doesn't get lost. */
660 logic_out_chs = g_slist_prepend(logic_out_chs, logic_out_ch);
662 py_item = PyTuple_GetItem(py_logic_out_ch, 0);
665 if (py_str_as_str(py_item, &logic_out_ch->id) != SRD_OK)
668 py_item = PyTuple_GetItem(py_logic_out_ch, 1);
671 if (py_str_as_str(py_item, &logic_out_ch->desc) != SRD_OK)
674 dec->logic_output_channels = logic_out_chs;
675 Py_DECREF(py_logic_out_chs);
676 PyGILState_Release(gstate);
681 srd_exception_catch("Failed to get %s decoder logic output channels",
685 g_slist_free_full(logic_out_chs, &logic_output_channel_free);
686 Py_XDECREF(py_logic_out_chs);
687 PyGILState_Release(gstate);
689 return SRD_ERR_PYTHON;
692 /* Check whether the Decoder class defines the named method. */
693 static int check_method(PyObject *py_dec, const char *mod_name,
694 const char *method_name)
698 PyGILState_STATE gstate;
700 gstate = PyGILState_Ensure();
702 py_method = PyObject_GetAttrString(py_dec, method_name);
704 srd_exception_catch("Protocol decoder %s Decoder class "
705 "has no %s() method", mod_name, method_name);
706 PyGILState_Release(gstate);
707 return SRD_ERR_PYTHON;
710 is_callable = PyCallable_Check(py_method);
711 Py_DECREF(py_method);
713 PyGILState_Release(gstate);
716 srd_err("Protocol decoder %s Decoder class attribute '%s' "
717 "is not a method.", mod_name, method_name);
718 return SRD_ERR_PYTHON;
725 * Get the API version of the specified decoder.
727 * @param d The decoder to use. Must not be NULL.
729 * @return The API version of the decoder, or 0 upon errors.
733 SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d)
737 PyGILState_STATE gstate;
742 gstate = PyGILState_Ensure();
744 py_apiver = PyObject_GetAttrString(d->py_dec, "api_version");
745 apiver = (py_apiver && PyLong_Check(py_apiver))
746 ? PyLong_AsLong(py_apiver) : 0;
747 Py_XDECREF(py_apiver);
749 PyGILState_Release(gstate);
754 static gboolean contains_duplicates(GSList *list)
756 for (GSList *l1 = list; l1; l1 = l1->next) {
757 for (GSList *l2 = l1->next; l2; l2 = l2->next)
758 if (!strcmp(l1->data, l2->data))
765 static gboolean contains_duplicate_ids(GSList *list1, GSList *list2)
767 for (GSList *l1 = list1; l1; l1 = l1->next) {
768 unsigned int cnt = 0;
769 const char **s1 = l1->data;
770 for (GSList *l2 = list2; l2; l2 = l2->next) {
771 const char **s2 = l2->data;
772 if (!strcmp(s1[0], s2[0]))
774 if ((list1 == list2) && cnt > 1)
776 if ((list1 != list2) && cnt > 0)
784 static gboolean contains_duplicate_row_ids(GSList *list1, GSList *list2)
786 for (GSList *l1 = list1; l1; l1 = l1->next) {
787 unsigned int cnt = 0;
788 const struct srd_decoder_annotation_row *r1 = l1->data;
789 for (GSList *l2 = list2; l2; l2 = l2->next) {
790 const struct srd_decoder_annotation_row *r2 = l2->data;
791 if (!strcmp(r1->id, r2->id))
802 * Load a protocol decoder module into the embedded Python interpreter.
804 * @param module_name The module name to be loaded.
806 * @return SRD_OK upon success, a (negative) error code otherwise.
810 SRD_API int srd_decoder_load(const char *module_name)
812 PyObject *py_basedec;
813 struct srd_decoder *d;
816 const char *fail_txt;
817 PyGILState_STATE gstate;
819 if (!srd_check_init())
825 gstate = PyGILState_Ensure();
827 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
828 /* Module was already imported. */
829 PyGILState_Release(gstate);
833 d = g_malloc0(sizeof(struct srd_decoder));
836 d->py_mod = py_import_by_name(module_name);
838 fail_txt = "import by name failed";
842 if (!mod_sigrokdecode) {
843 srd_err("sigrokdecode module not loaded.");
844 fail_txt = "sigrokdecode(3) not loaded";
848 /* Get the 'Decoder' class as Python object. */
849 d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder");
851 fail_txt = "no 'Decoder' attribute in imported module";
855 py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder");
857 fail_txt = "no 'Decoder' attribute in sigrokdecode(3)";
861 is_subclass = PyObject_IsSubclass(d->py_dec, py_basedec);
862 Py_DECREF(py_basedec);
865 srd_err("Decoder class in protocol decoder module %s is not "
866 "a subclass of sigrokdecode.Decoder.", module_name);
867 fail_txt = "not a subclass of sigrokdecode.Decoder";
872 * Check that this decoder has the correct PD API version.
873 * PDs of different API versions are incompatible and cannot work.
875 apiver = srd_decoder_apiver(d);
877 srd_exception_catch("Only PD API version 3 is supported, "
878 "decoder %s has version %ld", module_name, apiver);
879 fail_txt = "API version mismatch";
883 /* Check Decoder class for required methods. */
885 if (check_method(d->py_dec, module_name, "reset") != SRD_OK) {
886 fail_txt = "no 'reset()' method";
890 if (check_method(d->py_dec, module_name, "start") != SRD_OK) {
891 fail_txt = "no 'start()' method";
895 if (check_method(d->py_dec, module_name, "decode") != SRD_OK) {
896 fail_txt = "no 'decode()' method";
900 /* Store required fields in newly allocated strings. */
901 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) {
902 fail_txt = "no 'id' attribute";
906 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) {
907 fail_txt = "no 'name' attribute";
911 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) {
912 fail_txt = "no 'longname' attribute";
916 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) {
917 fail_txt = "no 'desc' attribute";
921 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) {
922 fail_txt = "no 'license' attribute";
926 if (py_attr_as_strlist(d->py_dec, "inputs", &(d->inputs)) != SRD_OK) {
927 fail_txt = "missing or malformed 'inputs' attribute";
931 if (py_attr_as_strlist(d->py_dec, "outputs", &(d->outputs)) != SRD_OK) {
932 fail_txt = "missing or malformed 'outputs' attribute";
936 if (py_attr_as_strlist(d->py_dec, "tags", &(d->tags)) != SRD_OK) {
937 fail_txt = "missing or malformed 'tags' attribute";
941 /* All options and their default values. */
942 if (get_options(d) != SRD_OK) {
943 fail_txt = "cannot get options";
947 /* Check and import required channels. */
948 if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) {
949 fail_txt = "cannot get channels";
953 /* Check and import optional channels. */
954 if (get_channels(d, "optional_channels", &d->opt_channels,
955 g_slist_length(d->channels)) != SRD_OK) {
956 fail_txt = "cannot get optional channels";
960 if (get_annotations(d) != SRD_OK) {
961 fail_txt = "cannot get annotations";
965 if (get_annotation_rows(d) != SRD_OK) {
966 fail_txt = "cannot get annotation rows";
970 if (get_binary_classes(d) != SRD_OK) {
971 fail_txt = "cannot get binary classes";
975 if (contains_duplicates(d->inputs)) {
976 fail_txt = "duplicate input IDs";
980 if (contains_duplicates(d->outputs)) {
981 fail_txt = "duplicate output IDs";
985 if (contains_duplicates(d->tags)) {
986 fail_txt = "duplicate tags";
990 if (contains_duplicate_ids(d->channels, d->channels)) {
991 fail_txt = "duplicate channel IDs";
995 if (contains_duplicate_ids(d->opt_channels, d->opt_channels)) {
996 fail_txt = "duplicate optional channel IDs";
1000 if (contains_duplicate_ids(d->channels, d->opt_channels)) {
1001 fail_txt = "channel and optional channel IDs contain duplicates";
1005 if (contains_duplicate_ids(d->options, d->options)) {
1006 fail_txt = "duplicate option IDs";
1010 if (contains_duplicate_ids(d->annotations, d->annotations)) {
1011 fail_txt = "duplicate annotation class IDs";
1015 if (contains_duplicate_row_ids(d->annotation_rows, d->annotation_rows)) {
1016 fail_txt = "duplicate annotation row IDs";
1020 if (contains_duplicate_ids(d->annotations, d->annotation_rows)) {
1021 fail_txt = "annotation class/row IDs contain duplicates";
1025 if (contains_duplicate_ids(d->binary, d->binary)) {
1026 fail_txt = "duplicate binary class IDs";
1030 if (get_logic_output_channels(d) != SRD_OK) {
1031 fail_txt = "cannot get logic output channels";
1035 PyGILState_Release(gstate);
1037 /* Append it to the list of loaded decoders. */
1038 pd_list = g_slist_append(pd_list, d);
1043 /* Don't show a message for the "common" directory, it's not a PD. */
1044 if (strcmp(module_name, "common")) {
1045 srd_exception_catch("Failed to load decoder %s: %s",
1046 module_name, fail_txt);
1052 srd_err("Failed to load decoder %s: %s", module_name, fail_txt);
1054 PyGILState_Release(gstate);
1056 return SRD_ERR_PYTHON;
1060 * Return a protocol decoder's docstring.
1062 * @param dec The loaded protocol decoder. Must not be NULL.
1064 * @return A newly allocated buffer containing the protocol decoder's
1065 * documentation. The caller is responsible for free'ing the buffer.
1069 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
1073 PyGILState_STATE gstate;
1075 if (!srd_check_init())
1078 if (!dec || !dec->py_mod)
1081 gstate = PyGILState_Ensure();
1083 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
1086 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
1087 srd_exception_catch("Failed to get docstring");
1092 if (py_str != Py_None)
1093 py_str_as_str(py_str, &doc);
1096 PyGILState_Release(gstate);
1101 PyGILState_Release(gstate);
1107 * Unload the specified protocol decoder.
1109 * @param dec The struct srd_decoder to be unloaded.
1111 * @return SRD_OK upon success, a (negative) error code otherwise.
1115 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
1117 struct srd_session *sess;
1120 if (!srd_check_init())
1127 * Since any instances of this decoder need to be released as well,
1128 * but they could be anywhere in the stack, just free the entire
1129 * stack. A frontend reloading a decoder thus has to restart all
1130 * instances, and rebuild the stack.
1132 for (l = sessions; l; l = l->next) {
1134 srd_inst_free_all(sess);
1137 /* Remove the PD from the list of loaded decoders. */
1138 pd_list = g_slist_remove(pd_list, dec);
1145 static void srd_decoder_load_all_zip_path(char *zip_path)
1147 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
1148 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
1152 PyGILState_STATE gstate;
1154 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
1156 gstate = PyGILState_Ensure();
1158 zipimport_mod = py_import_by_name("zipimport");
1159 if (zipimport_mod == NULL)
1162 zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter");
1163 if (zipimporter_class == NULL)
1166 zipimporter = PyObject_CallFunction(zipimporter_class, "s", zip_path);
1167 if (zipimporter == NULL)
1170 prefix_obj = PyObject_GetAttrString(zipimporter, "prefix");
1171 if (prefix_obj == NULL)
1174 files = PyObject_GetAttrString(zipimporter, "_files");
1175 if (files == NULL || !PyDict_Check(files))
1178 set = PySet_New(NULL);
1182 if (py_str_as_str(prefix_obj, &prefix) != SRD_OK)
1185 prefix_len = strlen(prefix);
1187 while (PyDict_Next(files, &pos, &key, &value)) {
1189 if (py_str_as_str(key, &path) == SRD_OK) {
1190 if (strlen(path) > prefix_len
1191 && memcmp(path, prefix, prefix_len) == 0
1192 && (slash = strchr(path + prefix_len, '/'))) {
1194 modname = PyUnicode_FromStringAndSize(path + prefix_len,
1195 slash - (path + prefix_len));
1196 if (modname == NULL) {
1199 PySet_Add(set, modname);
1208 while ((modname = PySet_Pop(set))) {
1210 if (py_str_as_str(modname, &modname_str) == SRD_OK) {
1211 /* The directory name is the module name (e.g. "i2c"). */
1212 srd_decoder_load(modname_str);
1213 g_free(modname_str);
1221 Py_XDECREF(prefix_obj);
1222 Py_XDECREF(zipimporter);
1223 Py_XDECREF(zipimporter_class);
1224 Py_XDECREF(zipimport_mod);
1226 PyGILState_Release(gstate);
1229 static void srd_decoder_load_all_path(char *path)
1232 const gchar *direntry;
1234 if (!(dir = g_dir_open(path, 0, NULL))) {
1235 /* Not really fatal. Try zipimport method too. */
1236 srd_decoder_load_all_zip_path(path);
1241 * This ignores errors returned by srd_decoder_load(). That
1242 * function will have logged the cause, but in any case we
1243 * want to continue anyway.
1245 while ((direntry = g_dir_read_name(dir)) != NULL) {
1246 /* The directory name is the module name (e.g. "i2c"). */
1247 srd_decoder_load(direntry);
1253 * Load all installed protocol decoders.
1255 * @return SRD_OK upon success, a (negative) error code otherwise.
1259 SRD_API int srd_decoder_load_all(void)
1263 if (!srd_check_init())
1266 for (l = searchpaths; l; l = l->next)
1267 srd_decoder_load_all_path(l->data);
1272 static void srd_decoder_unload_cb(void *arg, void *ignored)
1276 srd_decoder_unload((struct srd_decoder *)arg);
1280 * Unload all loaded protocol decoders.
1282 * @return SRD_OK upon success, a (negative) error code otherwise.
1286 SRD_API int srd_decoder_unload_all(void)
1288 g_slist_foreach(pd_list, srd_decoder_unload_cb, NULL);
1289 g_slist_free(pd_list);