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 decoder_option_free(
void *data)
146 g_slist_free_full(opt->
values, &variant_free);
147 variant_free(opt->
def);
155 PyGILState_STATE gstate;
160 gstate = PyGILState_Ensure();
163 PyGILState_Release(gstate);
165 g_slist_free_full(dec->
options, &decoder_option_free);
166 g_slist_free_full(dec->
binary, (GDestroyNotify)&g_strfreev);
168 g_slist_free_full(dec->
annotations, (GDestroyNotify)&g_strfreev);
170 g_slist_free_full(dec->
channels, &channel_free);
172 g_slist_free_full(dec->
outputs, g_free);
173 g_slist_free_full(dec->
inputs, g_free);
183 static int get_channels(
const struct srd_decoder *d,
const char *attr,
184 GSList **out_pdchl,
int offset)
186 PyObject *py_channellist, *py_entry;
190 PyGILState_STATE gstate;
192 gstate = PyGILState_Ensure();
194 if (!PyObject_HasAttrString(d->
py_dec, attr)) {
196 PyGILState_Release(gstate);
202 py_channellist = PyObject_GetAttrString(d->
py_dec, attr);
206 if (!PyTuple_Check(py_channellist)) {
207 srd_err(
"Protocol decoder %s %s attribute is not a tuple.",
212 for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) {
213 py_entry = PyTuple_GetItem(py_channellist, i);
217 if (!PyDict_Check(py_entry)) {
218 srd_err(
"Protocol decoder %s %s attribute is not "
219 "a list of dict elements.", d->
name, attr);
224 pdchl = g_slist_prepend(pdchl, pdch);
226 if (py_dictitem_as_str(py_entry,
"id", &pdch->
id) !=
SRD_OK)
228 if (py_dictitem_as_str(py_entry,
"name", &pdch->
name) !=
SRD_OK)
230 if (py_dictitem_as_str(py_entry,
"desc", &pdch->
desc) !=
SRD_OK)
233 pdch->
order = offset + i;
236 Py_DECREF(py_channellist);
239 PyGILState_Release(gstate);
244 srd_exception_catch(
"Failed to get %s list of %s decoder",
247 g_slist_free_full(pdchl, &channel_free);
248 Py_XDECREF(py_channellist);
249 PyGILState_Release(gstate);
256 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
261 PyGILState_STATE gstate;
263 gstate = PyGILState_Ensure();
265 if (!PyObject_HasAttrString(d->
py_dec,
"options")) {
267 PyGILState_Release(gstate);
274 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
278 if (!PyTuple_Check(py_opts)) {
279 srd_err(
"Protocol decoder %s: options attribute is not "
284 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
285 py_opt = PyTuple_GetItem(py_opts, opt);
289 if (!PyDict_Check(py_opt)) {
290 srd_err(
"Protocol decoder %s options: each option "
291 "must consist of a dictionary.", d->
name);
297 options = g_slist_prepend(options, o);
299 py_str = PyDict_GetItemString(py_opt,
"id");
301 srd_err(
"Protocol decoder %s option %zd has no ID.",
305 if (py_str_as_str(py_str, &o->
id) !=
SRD_OK)
308 py_str = PyDict_GetItemString(py_opt,
"desc");
310 if (py_str_as_str(py_str, &o->
desc) !=
SRD_OK)
314 py_default = PyDict_GetItemString(py_opt,
"default");
316 gvar = py_obj_to_variant(py_default);
318 srd_err(
"Protocol decoder %s option 'default' has "
319 "invalid default value.", d->
name);
322 o->
def = g_variant_ref_sink(gvar);
325 py_values = PyDict_GetItemString(py_opt,
"values");
330 srd_err(
"No default for option '%s'.", o->
id);
333 if (!PyTuple_Check(py_values)) {
334 srd_err(
"Option '%s' values should be a tuple.", o->
id);
338 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
339 py_item = PyTuple_GetItem(py_values, i);
343 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
344 srd_err(
"All values for option '%s' must be "
345 "of the same type as the default.",
349 gvar = py_obj_to_variant(py_item);
351 srd_err(
"Protocol decoder %s option 'values' "
352 "contains invalid value.", d->
name);
356 g_variant_ref_sink(gvar));
362 PyGILState_Release(gstate);
367 srd_exception_catch(
"Failed to get %s decoder options", d->
name);
369 g_slist_free_full(options, &decoder_option_free);
371 PyGILState_Release(gstate);
378 static int get_annotations(
struct srd_decoder *dec)
380 PyObject *py_annlist, *py_ann;
384 PyGILState_STATE gstate;
386 gstate = PyGILState_Ensure();
388 if (!PyObject_HasAttrString(dec->
py_dec,
"annotations")) {
389 PyGILState_Release(gstate);
395 py_annlist = PyObject_GetAttrString(dec->
py_dec,
"annotations");
399 if (!PyTuple_Check(py_annlist)) {
400 srd_err(
"Protocol decoder %s annotations should "
401 "be a tuple.", dec->
name);
405 for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) {
406 py_ann = PyTuple_GetItem(py_annlist, i);
410 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
411 srd_err(
"Protocol decoder %s annotation %zd should "
412 "be a tuple with two elements.",
416 if (py_strseq_to_char(py_ann, &annpair) !=
SRD_OK)
419 annotations = g_slist_prepend(annotations, annpair);
422 Py_DECREF(py_annlist);
423 PyGILState_Release(gstate);
428 srd_exception_catch(
"Failed to get %s decoder annotations", dec->
name);
430 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
431 Py_XDECREF(py_annlist);
432 PyGILState_Release(gstate);
439 static int get_annotation_rows(
struct srd_decoder *dec)
441 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
442 GSList *annotation_rows;
446 PyGILState_STATE gstate;
448 gstate = PyGILState_Ensure();
450 if (!PyObject_HasAttrString(dec->
py_dec,
"annotation_rows")) {
451 PyGILState_Release(gstate);
455 annotation_rows = NULL;
457 py_ann_rows = PyObject_GetAttrString(dec->
py_dec,
"annotation_rows");
461 if (!PyTuple_Check(py_ann_rows)) {
462 srd_err(
"Protocol decoder %s annotation_rows "
463 "must be a tuple.", dec->
name);
467 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
468 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
472 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
473 srd_err(
"Protocol decoder %s annotation_rows "
474 "must contain only tuples of 3 elements.",
480 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
482 py_item = PyTuple_GetItem(py_ann_row, 0);
485 if (py_str_as_str(py_item, &ann_row->
id) !=
SRD_OK)
488 py_item = PyTuple_GetItem(py_ann_row, 1);
491 if (py_str_as_str(py_item, &ann_row->
desc) !=
SRD_OK)
494 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
498 if (!PyTuple_Check(py_ann_classes)) {
499 srd_err(
"Protocol decoder %s annotation_rows tuples "
500 "must have a tuple of numbers as 3rd element.",
505 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
506 py_item = PyTuple_GetItem(py_ann_classes, k);
510 if (!PyLong_Check(py_item)) {
511 srd_err(
"Protocol decoder %s annotation row "
512 "class tuple must only contain numbers.",
516 class_idx = PyLong_AsSize_t(py_item);
517 if (PyErr_Occurred())
521 GSIZE_TO_POINTER(class_idx));
525 Py_DECREF(py_ann_rows);
526 PyGILState_Release(gstate);
531 srd_exception_catch(
"Failed to get %s decoder annotation rows",
534 g_slist_free_full(annotation_rows, &annotation_row_free);
535 Py_XDECREF(py_ann_rows);
536 PyGILState_Release(gstate);
543 static int get_binary_classes(
struct srd_decoder *dec)
545 PyObject *py_bin_classes, *py_bin_class;
549 PyGILState_STATE gstate;
551 gstate = PyGILState_Ensure();
553 if (!PyObject_HasAttrString(dec->
py_dec,
"binary")) {
554 PyGILState_Release(gstate);
560 py_bin_classes = PyObject_GetAttrString(dec->
py_dec,
"binary");
564 if (!PyTuple_Check(py_bin_classes)) {
565 srd_err(
"Protocol decoder %s binary classes should "
566 "be a tuple.", dec->
name);
570 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
571 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
575 if (!PyTuple_Check(py_bin_class)
576 || PyTuple_Size(py_bin_class) != 2) {
577 srd_err(
"Protocol decoder %s binary classes should "
578 "consist only of tuples of 2 elements.",
582 if (py_strseq_to_char(py_bin_class, &bin) !=
SRD_OK)
585 bin_classes = g_slist_prepend(bin_classes, bin);
587 dec->
binary = bin_classes;
588 Py_DECREF(py_bin_classes);
589 PyGILState_Release(gstate);
594 srd_exception_catch(
"Failed to get %s decoder binary classes",
597 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
598 Py_XDECREF(py_bin_classes);
599 PyGILState_Release(gstate);
606 static int check_method(PyObject *py_dec,
const char *mod_name,
607 const char *method_name)
611 PyGILState_STATE gstate;
613 gstate = PyGILState_Ensure();
615 py_method = PyObject_GetAttrString(py_dec, method_name);
617 srd_exception_catch(
"Protocol decoder %s Decoder class "
618 "has no %s() method", mod_name, method_name);
619 PyGILState_Release(gstate);
623 is_callable = PyCallable_Check(py_method);
624 Py_DECREF(py_method);
626 PyGILState_Release(gstate);
629 srd_err(
"Protocol decoder %s Decoder class attribute '%s' "
630 "is not a method.", mod_name, method_name);
650 PyGILState_STATE gstate;
655 gstate = PyGILState_Ensure();
657 py_apiver = PyObject_GetAttrString(d->
py_dec,
"api_version");
658 apiver = (py_apiver && PyLong_Check(py_apiver))
659 ? PyLong_AsLong(py_apiver) : 0;
660 Py_XDECREF(py_apiver);
662 PyGILState_Release(gstate);
678 PyObject *py_basedec;
682 const char *fail_txt;
683 PyGILState_STATE gstate;
685 if (!srd_check_init())
691 gstate = PyGILState_Ensure();
693 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
695 PyGILState_Release(gstate);
702 d->
py_mod = py_import_by_name(module_name);
704 fail_txt =
"import by name failed";
708 if (!mod_sigrokdecode) {
709 srd_err(
"sigrokdecode module not loaded.");
710 fail_txt =
"sigrokdecode(3) not loaded";
715 d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder");
717 fail_txt =
"no 'Decoder' attribute in imported module";
721 py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder");
723 fail_txt =
"no 'Decoder' attribute in sigrokdecode(3)";
727 is_subclass = PyObject_IsSubclass(d->
py_dec, py_basedec);
728 Py_DECREF(py_basedec);
731 srd_err(
"Decoder class in protocol decoder module %s is not "
732 "a subclass of sigrokdecode.Decoder.", module_name);
733 fail_txt =
"not a subclass of sigrokdecode.Decoder";
741 apiver = srd_decoder_apiver(d);
743 srd_exception_catch(
"Only PD API version 3 is supported, "
744 "decoder %s has version %ld", module_name, apiver);
745 fail_txt =
"API version mismatch";
751 if (check_method(d->
py_dec, module_name,
"start") !=
SRD_OK) {
752 fail_txt =
"no 'start()' method";
756 if (check_method(d->
py_dec, module_name,
"decode") !=
SRD_OK) {
757 fail_txt =
"no 'decode()' method";
763 fail_txt =
"no 'id' attribute";
768 fail_txt =
"no 'name' attribute";
773 fail_txt =
"no 'longname' attribute";
778 fail_txt =
"no 'desc' attribute";
783 fail_txt =
"no 'license' attribute";
788 fail_txt =
"missing or malformed 'inputs' attribute";
793 fail_txt =
"missing or malformed 'outputs' attribute";
798 if (get_options(d) !=
SRD_OK) {
799 fail_txt =
"cannot get options";
805 fail_txt =
"cannot get channels";
810 if (get_channels(d,
"optional_channels", &d->
opt_channels,
812 fail_txt =
"cannot get optional channels";
816 if (get_annotations(d) !=
SRD_OK) {
817 fail_txt =
"cannot get annotations";
821 if (get_annotation_rows(d) !=
SRD_OK) {
822 fail_txt =
"cannot get annotation rows";
826 if (get_binary_classes(d) !=
SRD_OK) {
827 fail_txt =
"cannot get binary classes";
831 PyGILState_Release(gstate);
834 pd_list = g_slist_append(pd_list, d);
840 if (strcmp(module_name,
"common")) {
841 srd_exception_catch(
"Failed to load decoder %s: %s",
842 module_name, fail_txt);
847 srd_err(
"Failed to load decoder %s: %s", module_name, fail_txt);
849 PyGILState_Release(gstate);
868 PyGILState_STATE gstate;
870 if (!srd_check_init())
876 gstate = PyGILState_Ensure();
878 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
881 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
882 srd_exception_catch(
"Failed to get docstring");
887 if (py_str != Py_None)
888 py_str_as_str(py_str, &doc);
891 PyGILState_Release(gstate);
896 PyGILState_Release(gstate);
912 struct srd_session *sess;
915 if (!srd_check_init())
927 for (l = sessions; l; l = l->next) {
929 srd_inst_free_all(sess);
933 pd_list = g_slist_remove(pd_list, dec);
940 static void srd_decoder_load_all_zip_path(
char *path)
942 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
943 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
947 PyGILState_STATE gstate;
949 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
951 gstate = PyGILState_Ensure();
953 zipimport_mod = py_import_by_name(
"zipimport");
954 if (zipimport_mod == NULL)
957 zipimporter_class = PyObject_GetAttrString(zipimport_mod,
"zipimporter");
958 if (zipimporter_class == NULL)
961 zipimporter = PyObject_CallFunction(zipimporter_class,
"s", path);
962 if (zipimporter == NULL)
965 prefix_obj = PyObject_GetAttrString(zipimporter,
"prefix");
966 if (prefix_obj == NULL)
969 files = PyObject_GetAttrString(zipimporter,
"_files");
970 if (files == NULL || !PyDict_Check(files))
973 set = PySet_New(NULL);
977 if (py_str_as_str(prefix_obj, &prefix) !=
SRD_OK)
980 prefix_len = strlen(prefix);
982 while (PyDict_Next(files, &pos, &key, &value)) {
984 if (py_str_as_str(key, &path) ==
SRD_OK) {
985 if (strlen(path) > prefix_len
986 && memcmp(path, prefix, prefix_len) == 0
987 && (slash = strchr(path + prefix_len,
'/'))) {
989 modname = PyUnicode_FromStringAndSize(path + prefix_len,
990 slash - (path + prefix_len));
991 if (modname == NULL) {
994 PySet_Add(set, modname);
1003 while ((modname = PySet_Pop(set))) {
1005 if (py_str_as_str(modname, &modname_str) ==
SRD_OK) {
1008 g_free(modname_str);
1016 Py_XDECREF(prefix_obj);
1017 Py_XDECREF(zipimporter);
1018 Py_XDECREF(zipimporter_class);
1019 Py_XDECREF(zipimport_mod);
1021 PyGILState_Release(gstate);
1024 static void srd_decoder_load_all_path(
char *path)
1027 const gchar *direntry;
1029 if (!(dir = g_dir_open(path, 0, NULL))) {
1032 srd_decoder_load_all_zip_path(path);
1039 while ((direntry = g_dir_read_name(dir)) != NULL) {
1058 if (!srd_check_init())
1061 for (l = searchpaths; l; l = l->next)
1062 srd_decoder_load_all_path(l->data);
1077 g_slist_free(pd_list);
char * license
The license of the decoder.
char * srd_decoder_doc_get(const struct srd_decoder *dec)
Return a protocol decoder's docstring.
GSList * opt_channels
List of optional channels for this decoder.
const GSList * srd_decoder_list(void)
Returns the list of loaded protocol decoders.
GSList * outputs
List of possible decoder output IDs.
Structure which contains information about one protocol decoder channel.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
char * name
The (short) decoder name.
char * longname
The (long) decoder name.
int order
The index of the channel, i.e.
char * id
The ID of the channel.
char * name
The name of the channel.
GSList * annotations
List of NULL-terminated char[], containing descriptions of the supported annotation output...
The public libsigrokdecode header file to be used by frontends.
char * desc
A (short, one-line) description of the decoder.
int srd_decoder_unload_all(void)
Unload all loaded protocol decoders.
int srd_decoder_load(const char *module_name)
Load a protocol decoder module into the embedded Python interpreter.
int srd_decoder_unload(struct srd_decoder *dec)
Unload the specified protocol decoder.
GSList * binary
List of NULL-terminated char[], containing descriptions of the supported binary output.
GSList * inputs
List of possible decoder input IDs.
GSList * options
List of decoder options.
char * desc
The description of the channel.
void * py_mod
Python module.
GSList * annotation_rows
List of annotation rows (row items: id, description, and a list of annotation classes belonging to th...
void * py_dec
sigrokdecode.Decoder class.
GSList * channels
List of channels required by this decoder.
Generic/unspecified error.
int srd_decoder_load_all(void)
Load all installed protocol decoders.