22 #include "libsigrokdecode-internal.h" 43 static GSList *pd_list = NULL;
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.");
94 for (l = pd_list; l; l = l->next) {
96 if (!strcmp(dec->
id,
id))
103 static void channel_free(
void *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)
139 static void logic_output_channel_free(
void *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)
158 g_slist_free_full(opt->
values, &variant_free);
159 variant_free(opt->
def);
167 PyGILState_STATE gstate;
172 gstate = PyGILState_Ensure();
175 PyGILState_Release(gstate);
177 g_slist_free_full(dec->
options, &decoder_option_free);
178 g_slist_free_full(dec->
binary, (GDestroyNotify)&g_strfreev);
180 g_slist_free_full(dec->
annotations, (GDestroyNotify)&g_strfreev);
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);
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;
203 PyGILState_STATE gstate;
205 gstate = PyGILState_Ensure();
207 if (!PyObject_HasAttrString(d->
py_dec, attr)) {
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 ch_idx = PyTuple_Size(py_channellist);
227 py_entry = PyTuple_GetItem(py_channellist, ch_idx);
231 if (!PyDict_Check(py_entry)) {
232 srd_err(
"Protocol decoder %s %s attribute is not " 233 "a list of dict elements.", d->
name, attr);
238 pdchl = g_slist_prepend(pdchl, pdch);
240 if (py_dictitem_as_str(py_entry,
"id", &pdch->
id) !=
SRD_OK)
242 if (py_dictitem_as_str(py_entry,
"name", &pdch->
name) !=
SRD_OK)
244 if (py_dictitem_as_str(py_entry,
"desc", &pdch->
desc) !=
SRD_OK)
247 pdch->
order = offset + ch_idx;
250 Py_DECREF(py_channellist);
253 PyGILState_Release(gstate);
258 srd_exception_catch(
"Failed to get %s list of %s decoder",
262 g_slist_free_full(pdchl, &channel_free);
263 Py_XDECREF(py_channellist);
264 PyGILState_Release(gstate);
271 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
275 ssize_t opt, val_idx;
276 PyGILState_STATE gstate;
278 gstate = PyGILState_Ensure();
280 if (!PyObject_HasAttrString(d->
py_dec,
"options")) {
282 PyGILState_Release(gstate);
289 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
293 if (!PyTuple_Check(py_opts)) {
294 srd_err(
"Protocol decoder %s: options attribute is not " 299 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
300 py_opt = PyTuple_GetItem(py_opts, opt);
304 if (!PyDict_Check(py_opt)) {
305 srd_err(
"Protocol decoder %s options: each option " 306 "must consist of a dictionary.", d->
name);
312 options = g_slist_prepend(options, o);
314 py_str = PyDict_GetItemString(py_opt,
"id");
316 srd_err(
"Protocol decoder %s option %zd has no ID.",
320 if (py_str_as_str(py_str, &o->
id) !=
SRD_OK)
323 py_str = PyDict_GetItemString(py_opt,
"desc");
325 if (py_str_as_str(py_str, &o->
desc) !=
SRD_OK)
329 py_default = PyDict_GetItemString(py_opt,
"default");
331 gvar = py_obj_to_variant(py_default);
333 srd_err(
"Protocol decoder %s option 'default' has " 334 "invalid default value.", d->
name);
337 o->
def = g_variant_ref_sink(gvar);
340 py_values = PyDict_GetItemString(py_opt,
"values");
347 srd_err(
"No default for option '%s'.", o->
id);
350 if (!PyTuple_Check(py_values)) {
351 srd_err(
"Option '%s' values should be a tuple.", o->
id);
355 val_idx = PyTuple_Size(py_values);
357 py_item = PyTuple_GetItem(py_values, val_idx);
361 if (py_default && (Py_TYPE(py_default) != Py_TYPE(py_item))) {
362 srd_err(
"All values for option '%s' must be " 363 "of the same type as the default.",
367 gvar = py_obj_to_variant(py_item);
369 srd_err(
"Protocol decoder %s option 'values' " 370 "contains invalid value.", d->
name);
374 g_variant_ref_sink(gvar));
380 PyGILState_Release(gstate);
385 srd_exception_catch(
"Failed to get %s decoder options", d->
name);
388 g_slist_free_full(options, &decoder_option_free);
390 PyGILState_Release(gstate);
396 static int get_annotations(
struct srd_decoder *dec,
size_t *ret_count)
398 PyObject *py_annlist, *py_ann;
402 PyGILState_STATE gstate;
407 gstate = PyGILState_Ensure();
409 if (!PyObject_HasAttrString(dec->
py_dec,
"annotations")) {
410 PyGILState_Release(gstate);
416 py_annlist = PyObject_GetAttrString(dec->
py_dec,
"annotations");
420 if (!PyTuple_Check(py_annlist)) {
421 srd_err(
"Protocol decoder %s annotations should be a tuple.",
426 ann_idx = PyTuple_Size(py_annlist);
428 *ret_count = ann_idx;
430 py_ann = PyTuple_GetItem(py_annlist, ann_idx);
434 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
435 srd_err(
"Protocol decoder %s annotation %zd should be a tuple with two elements.",
436 dec->
name, ann_idx + 1);
439 if (py_strseq_to_char(py_ann, &annpair) !=
SRD_OK)
442 annotations = g_slist_prepend(annotations, annpair);
445 Py_DECREF(py_annlist);
446 PyGILState_Release(gstate);
451 srd_exception_catch(
"Failed to get %s decoder annotations", dec->
name);
454 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
455 Py_XDECREF(py_annlist);
456 PyGILState_Release(gstate);
462 static int get_annotation_rows(
struct srd_decoder *dec,
size_t cls_count)
464 const char *py_member_name =
"annotation_rows";
466 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
467 GSList *annotation_rows;
469 ssize_t row_idx, item_idx;
471 PyGILState_STATE gstate;
473 gstate = PyGILState_Ensure();
475 if (!PyObject_HasAttrString(dec->
py_dec, py_member_name)) {
476 PyGILState_Release(gstate);
480 annotation_rows = NULL;
482 py_ann_rows = PyObject_GetAttrString(dec->
py_dec, py_member_name);
486 if (!PyTuple_Check(py_ann_rows)) {
487 srd_err(
"Protocol decoder %s %s must be a tuple.",
488 dec->
name, py_member_name);
492 row_idx = PyTuple_Size(py_ann_rows);
494 py_ann_row = PyTuple_GetItem(py_ann_rows, row_idx);
498 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
499 srd_err(
"Protocol decoder %s %s must contain only tuples of 3 elements.",
500 dec->
name, py_member_name);
505 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
507 py_item = PyTuple_GetItem(py_ann_row, 0);
510 if (py_str_as_str(py_item, &ann_row->
id) !=
SRD_OK)
513 py_item = PyTuple_GetItem(py_ann_row, 1);
516 if (py_str_as_str(py_item, &ann_row->
desc) !=
SRD_OK)
519 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
523 if (!PyTuple_Check(py_ann_classes)) {
524 srd_err(
"Protocol decoder %s %s tuples must have a tuple of numbers as 3rd element.",
525 dec->
name, py_member_name);
529 item_idx = PyTuple_Size(py_ann_classes);
531 py_item = PyTuple_GetItem(py_ann_classes, item_idx);
535 if (!PyLong_Check(py_item)) {
536 srd_err(
"Protocol decoder %s annotation row class tuple must only contain numbers.",
540 class_idx = PyLong_AsSize_t(py_item);
541 if (PyErr_Occurred())
543 if (class_idx >= cls_count) {
544 srd_err(
"Protocol decoder %s annotation row %zd references invalid class %zu.",
545 dec->
name, row_idx, class_idx);
550 GSIZE_TO_POINTER(class_idx));
554 Py_DECREF(py_ann_rows);
555 PyGILState_Release(gstate);
560 srd_exception_catch(
"Failed to get %s decoder annotation rows",
564 g_slist_free_full(annotation_rows, &annotation_row_free);
565 Py_XDECREF(py_ann_rows);
566 PyGILState_Release(gstate);
572 static int get_binary_classes(
struct srd_decoder *dec)
574 PyObject *py_bin_classes, *py_bin_class;
578 PyGILState_STATE gstate;
580 gstate = PyGILState_Ensure();
582 if (!PyObject_HasAttrString(dec->
py_dec,
"binary")) {
583 PyGILState_Release(gstate);
589 py_bin_classes = PyObject_GetAttrString(dec->
py_dec,
"binary");
593 if (!PyTuple_Check(py_bin_classes)) {
594 srd_err(
"Protocol decoder %s binary classes should " 595 "be a tuple.", dec->
name);
599 bin_idx = PyTuple_Size(py_bin_classes);
601 py_bin_class = PyTuple_GetItem(py_bin_classes, bin_idx);
605 if (!PyTuple_Check(py_bin_class)
606 || PyTuple_Size(py_bin_class) != 2) {
607 srd_err(
"Protocol decoder %s binary classes should " 608 "consist only of tuples of 2 elements.",
612 if (py_strseq_to_char(py_bin_class, &bin) !=
SRD_OK)
615 bin_classes = g_slist_prepend(bin_classes, bin);
617 dec->
binary = bin_classes;
618 Py_DECREF(py_bin_classes);
619 PyGILState_Release(gstate);
624 srd_exception_catch(
"Failed to get %s decoder binary classes",
628 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
629 Py_XDECREF(py_bin_classes);
630 PyGILState_Release(gstate);
636 static int get_logic_output_channels(
struct srd_decoder *dec)
638 PyObject *py_logic_out_chs, *py_logic_out_ch, *py_item;
639 GSList *logic_out_chs;
642 PyGILState_STATE gstate;
644 gstate = PyGILState_Ensure();
646 if (!PyObject_HasAttrString(dec->
py_dec,
"logic_output_channels")) {
647 PyGILState_Release(gstate);
651 logic_out_chs = NULL;
653 py_logic_out_chs = PyObject_GetAttrString(dec->
py_dec,
"logic_output_channels");
654 if (!py_logic_out_chs)
657 if (!PyTuple_Check(py_logic_out_chs)) {
658 srd_err(
"Protocol decoder %s logic_output_channels " 659 "must be a tuple.", dec->
name);
663 for (i = PyTuple_Size(py_logic_out_chs) - 1; i >= 0; i--) {
664 py_logic_out_ch = PyTuple_GetItem(py_logic_out_chs, i);
665 if (!py_logic_out_ch)
668 if (!PyTuple_Check(py_logic_out_ch) || PyTuple_Size(py_logic_out_ch) != 2) {
669 srd_err(
"Protocol decoder %s logic_output_channels " 670 "must contain only tuples of 2 elements.",
674 logic_out_ch = g_malloc0(
sizeof(*logic_out_ch));
676 logic_out_chs = g_slist_prepend(logic_out_chs, logic_out_ch);
678 py_item = PyTuple_GetItem(py_logic_out_ch, 0);
681 if (py_str_as_str(py_item, &logic_out_ch->
id) !=
SRD_OK)
684 py_item = PyTuple_GetItem(py_logic_out_ch, 1);
687 if (py_str_as_str(py_item, &logic_out_ch->
desc) !=
SRD_OK)
691 Py_DECREF(py_logic_out_chs);
692 PyGILState_Release(gstate);
697 srd_exception_catch(
"Failed to get %s decoder logic output channels",
701 g_slist_free_full(logic_out_chs, &logic_output_channel_free);
702 Py_XDECREF(py_logic_out_chs);
703 PyGILState_Release(gstate);
709 static int check_method(PyObject *py_dec,
const char *mod_name,
710 const char *method_name)
714 PyGILState_STATE gstate;
716 gstate = PyGILState_Ensure();
718 py_method = PyObject_GetAttrString(py_dec, method_name);
720 srd_exception_catch(
"Protocol decoder %s Decoder class " 721 "has no %s() method", mod_name, method_name);
722 PyGILState_Release(gstate);
726 is_callable = PyCallable_Check(py_method);
727 Py_DECREF(py_method);
729 PyGILState_Release(gstate);
732 srd_err(
"Protocol decoder %s Decoder class attribute '%s' " 733 "is not a method.", mod_name, method_name);
753 PyGILState_STATE gstate;
758 gstate = PyGILState_Ensure();
760 py_apiver = PyObject_GetAttrString(d->
py_dec,
"api_version");
761 apiver = (py_apiver && PyLong_Check(py_apiver))
762 ? PyLong_AsLong(py_apiver) : 0;
763 Py_XDECREF(py_apiver);
765 PyGILState_Release(gstate);
770 static gboolean contains_duplicates(GSList *list)
772 for (GSList *l1 = list; l1; l1 = l1->next) {
773 for (GSList *l2 = l1->next; l2; l2 = l2->next)
774 if (!strcmp(l1->data, l2->data))
781 static gboolean contains_duplicate_ids(GSList *list1, GSList *list2)
783 for (GSList *l1 = list1; l1; l1 = l1->next) {
784 unsigned int cnt = 0;
785 const char **s1 = l1->data;
786 for (GSList *l2 = list2; l2; l2 = l2->next) {
787 const char **s2 = l2->data;
788 if (!strcmp(s1[0], s2[0]))
790 if ((list1 == list2) && cnt > 1)
792 if ((list1 != list2) && cnt > 0)
800 static gboolean contains_duplicate_row_ids(GSList *list1, GSList *list2)
802 for (GSList *l1 = list1; l1; l1 = l1->next) {
803 unsigned int cnt = 0;
805 for (GSList *l2 = list2; l2; l2 = l2->next) {
807 if (!strcmp(r1->
id, r2->
id))
828 PyObject *py_basedec;
832 const char *fail_txt;
833 PyGILState_STATE gstate;
834 size_t ann_cls_count;
836 if (!srd_check_init())
842 gstate = PyGILState_Ensure();
844 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
846 PyGILState_Release(gstate);
853 d->
py_mod = py_import_by_name(module_name);
855 fail_txt =
"import by name failed";
859 if (!mod_sigrokdecode) {
860 srd_err(
"sigrokdecode module not loaded.");
861 fail_txt =
"sigrokdecode(3) not loaded";
866 d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder");
868 fail_txt =
"no 'Decoder' attribute in imported module";
872 py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder");
874 fail_txt =
"no 'Decoder' attribute in sigrokdecode(3)";
878 is_subclass = PyObject_IsSubclass(d->
py_dec, py_basedec);
879 Py_DECREF(py_basedec);
882 srd_err(
"Decoder class in protocol decoder module %s is not " 883 "a subclass of sigrokdecode.Decoder.", module_name);
884 fail_txt =
"not a subclass of sigrokdecode.Decoder";
892 apiver = srd_decoder_apiver(d);
894 srd_exception_catch(
"Only PD API version 3 is supported, " 895 "decoder %s has version %ld", module_name, apiver);
896 fail_txt =
"API version mismatch";
902 if (check_method(d->
py_dec, module_name,
"reset") !=
SRD_OK) {
903 fail_txt =
"no 'reset()' method";
907 if (check_method(d->
py_dec, module_name,
"start") !=
SRD_OK) {
908 fail_txt =
"no 'start()' method";
912 if (check_method(d->
py_dec, module_name,
"decode") !=
SRD_OK) {
913 fail_txt =
"no 'decode()' method";
919 fail_txt =
"no 'id' attribute";
924 fail_txt =
"no 'name' attribute";
929 fail_txt =
"no 'longname' attribute";
934 fail_txt =
"no 'desc' attribute";
939 fail_txt =
"no 'license' attribute";
944 fail_txt =
"missing or malformed 'inputs' attribute";
949 fail_txt =
"missing or malformed 'outputs' attribute";
954 fail_txt =
"missing or malformed 'tags' attribute";
959 if (get_options(d) !=
SRD_OK) {
960 fail_txt =
"cannot get options";
966 fail_txt =
"cannot get channels";
971 if (get_channels(d,
"optional_channels", &d->
opt_channels,
973 fail_txt =
"cannot get optional channels";
977 if (get_annotations(d, &ann_cls_count) !=
SRD_OK) {
978 fail_txt =
"cannot get annotations";
982 if (get_annotation_rows(d, ann_cls_count) !=
SRD_OK) {
983 fail_txt =
"cannot get annotation rows";
987 if (get_binary_classes(d) !=
SRD_OK) {
988 fail_txt =
"cannot get binary classes";
992 if (contains_duplicates(d->
inputs)) {
993 fail_txt =
"duplicate input IDs";
997 if (contains_duplicates(d->
outputs)) {
998 fail_txt =
"duplicate output IDs";
1002 if (contains_duplicates(d->
tags)) {
1003 fail_txt =
"duplicate tags";
1008 fail_txt =
"duplicate channel IDs";
1013 fail_txt =
"duplicate optional channel IDs";
1018 fail_txt =
"channel and optional channel IDs contain duplicates";
1023 fail_txt =
"duplicate option IDs";
1028 fail_txt =
"duplicate annotation class IDs";
1033 fail_txt =
"duplicate annotation row IDs";
1038 fail_txt =
"annotation class/row IDs contain duplicates";
1043 fail_txt =
"duplicate binary class IDs";
1047 if (get_logic_output_channels(d) !=
SRD_OK) {
1048 fail_txt =
"cannot get logic output channels";
1052 PyGILState_Release(gstate);
1055 pd_list = g_slist_append(pd_list, d);
1061 if (strcmp(module_name,
"common")) {
1062 srd_exception_catch(
"Failed to load decoder %s: %s",
1063 module_name, fail_txt);
1069 srd_err(
"Failed to load decoder %s: %s", module_name, fail_txt);
1071 PyGILState_Release(gstate);
1090 PyGILState_STATE gstate;
1092 if (!srd_check_init())
1095 if (!dec || !dec->
py_mod)
1098 gstate = PyGILState_Ensure();
1100 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
1103 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
1104 srd_exception_catch(
"Failed to get docstring");
1109 if (py_str != Py_None)
1110 py_str_as_str(py_str, &doc);
1113 PyGILState_Release(gstate);
1118 PyGILState_Release(gstate);
1134 struct srd_session *sess;
1137 if (!srd_check_init())
1149 for (l = sessions; l; l = l->next) {
1151 srd_inst_free_all(sess);
1155 pd_list = g_slist_remove(pd_list, dec);
1162 static void srd_decoder_load_all_zip_path(
char *zip_path)
1164 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
1165 PyObject *prefix_obj, *files, *key, *value, *
set, *modname;
1169 PyGILState_STATE gstate;
1171 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
1173 gstate = PyGILState_Ensure();
1175 zipimport_mod = py_import_by_name(
"zipimport");
1176 if (zipimport_mod == NULL)
1179 zipimporter_class = PyObject_GetAttrString(zipimport_mod,
"zipimporter");
1180 if (zipimporter_class == NULL)
1183 zipimporter = PyObject_CallFunction(zipimporter_class,
"s", zip_path);
1184 if (zipimporter == NULL)
1187 prefix_obj = PyObject_GetAttrString(zipimporter,
"prefix");
1188 if (prefix_obj == NULL)
1191 files = PyObject_GetAttrString(zipimporter,
"_files");
1192 if (files == NULL || !PyDict_Check(files))
1195 set = PySet_New(NULL);
1199 if (py_str_as_str(prefix_obj, &prefix) !=
SRD_OK)
1202 prefix_len = strlen(prefix);
1204 while (PyDict_Next(files, &pos, &key, &value)) {
1206 if (py_str_as_str(key, &path) ==
SRD_OK) {
1207 if (strlen(path) > prefix_len
1208 && memcmp(path, prefix, prefix_len) == 0
1209 && (slash = strchr(path + prefix_len,
'/'))) {
1211 modname = PyUnicode_FromStringAndSize(path + prefix_len,
1212 slash - (path + prefix_len));
1213 if (modname == NULL) {
1216 PySet_Add(
set, modname);
1225 while ((modname = PySet_Pop(
set))) {
1227 if (py_str_as_str(modname, &modname_str) ==
SRD_OK) {
1230 g_free(modname_str);
1238 Py_XDECREF(prefix_obj);
1239 Py_XDECREF(zipimporter);
1240 Py_XDECREF(zipimporter_class);
1241 Py_XDECREF(zipimport_mod);
1243 PyGILState_Release(gstate);
1246 static void srd_decoder_load_all_path(
char *path)
1249 const gchar *direntry;
1251 if (!(dir = g_dir_open(path, 0, NULL))) {
1253 srd_decoder_load_all_zip_path(path);
1262 while ((direntry = g_dir_read_name(dir)) != NULL) {
1280 if (!srd_check_init())
1283 for (l = searchpaths; l; l = l->next)
1284 srd_decoder_load_all_path(l->data);
1289 static void srd_decoder_unload_cb(
void *arg,
void *ignored)
1305 g_slist_foreach(pd_list, srd_decoder_unload_cb, NULL);
1306 g_slist_free(pd_list);
GSList * binary
List of binary classes.
char * longname
The (long) decoder name.
int srd_decoder_unload_all(void)
Unload all loaded protocol decoders.
GSList * options
List of decoder options.
GSList * channels
List of channels required by this decoder.
Generic/unspecified error.
char * desc
A (short, one-line) description of the decoder.
GSList * logic_output_channels
List of logic output channels (item: id, description).
void * py_mod
Python module.
int srd_decoder_load(const char *module_name)
Load a protocol decoder module into the embedded Python interpreter.
GSList * annotation_rows
List of annotation rows (row items: id, description, and a list of annotation classes belonging to th...
int srd_decoder_load_all(void)
Load all installed protocol decoders.
GSList * tags
List of tags associated with this decoder.
GSList * annotations
List of annotation classes.
int order
The index of the channel, i.e.
void * py_dec
sigrokdecode.Decoder class.
char * name
The name of the channel.
char * id
The ID of the channel.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
char * desc
The description of the channel.
const GSList * srd_decoder_list(void)
Returns the list of loaded protocol decoders.
GSList * opt_channels
List of optional channels for this decoder.
GSList * inputs
List of possible decoder input IDs.
GSList * outputs
List of possible decoder output IDs.
The public libsigrokdecode header file to be used by frontends.
int srd_decoder_unload(struct srd_decoder *dec)
Unload the specified protocol decoder.
char * license
The license of the decoder.
Structure which contains information about one protocol decoder channel.
char * srd_decoder_doc_get(const struct srd_decoder *dec)
Return a protocol decoder's docstring.
char * name
The (short) decoder name.