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",
248 g_slist_free_full(pdchl, &channel_free);
249 Py_XDECREF(py_channellist);
250 PyGILState_Release(gstate);
257 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
262 PyGILState_STATE gstate;
264 gstate = PyGILState_Ensure();
266 if (!PyObject_HasAttrString(d->
py_dec,
"options")) {
268 PyGILState_Release(gstate);
275 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
279 if (!PyTuple_Check(py_opts)) {
280 srd_err(
"Protocol decoder %s: options attribute is not " 285 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
286 py_opt = PyTuple_GetItem(py_opts, opt);
290 if (!PyDict_Check(py_opt)) {
291 srd_err(
"Protocol decoder %s options: each option " 292 "must consist of a dictionary.", d->
name);
298 options = g_slist_prepend(options, o);
300 py_str = PyDict_GetItemString(py_opt,
"id");
302 srd_err(
"Protocol decoder %s option %zd has no ID.",
306 if (py_str_as_str(py_str, &o->
id) !=
SRD_OK)
309 py_str = PyDict_GetItemString(py_opt,
"desc");
311 if (py_str_as_str(py_str, &o->
desc) !=
SRD_OK)
315 py_default = PyDict_GetItemString(py_opt,
"default");
317 gvar = py_obj_to_variant(py_default);
319 srd_err(
"Protocol decoder %s option 'default' has " 320 "invalid default value.", d->
name);
323 o->
def = g_variant_ref_sink(gvar);
326 py_values = PyDict_GetItemString(py_opt,
"values");
333 srd_err(
"No default for option '%s'.", o->
id);
336 if (!PyTuple_Check(py_values)) {
337 srd_err(
"Option '%s' values should be a tuple.", o->
id);
341 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
342 py_item = PyTuple_GetItem(py_values, i);
346 if (py_default && (Py_TYPE(py_default) != Py_TYPE(py_item))) {
347 srd_err(
"All values for option '%s' must be " 348 "of the same type as the default.",
352 gvar = py_obj_to_variant(py_item);
354 srd_err(
"Protocol decoder %s option 'values' " 355 "contains invalid value.", d->
name);
359 g_variant_ref_sink(gvar));
365 PyGILState_Release(gstate);
370 srd_exception_catch(
"Failed to get %s decoder options", d->
name);
373 g_slist_free_full(options, &decoder_option_free);
375 PyGILState_Release(gstate);
381 static int get_annotations(
struct srd_decoder *dec)
383 PyObject *py_annlist, *py_ann;
387 PyGILState_STATE gstate;
389 gstate = PyGILState_Ensure();
391 if (!PyObject_HasAttrString(dec->
py_dec,
"annotations")) {
392 PyGILState_Release(gstate);
398 py_annlist = PyObject_GetAttrString(dec->
py_dec,
"annotations");
402 if (!PyTuple_Check(py_annlist)) {
403 srd_err(
"Protocol decoder %s annotations should " 404 "be a tuple.", dec->
name);
408 for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) {
409 py_ann = PyTuple_GetItem(py_annlist, i);
413 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
414 srd_err(
"Protocol decoder %s annotation %zd should " 415 "be a tuple with two elements.",
419 if (py_strseq_to_char(py_ann, &annpair) !=
SRD_OK)
422 annotations = g_slist_prepend(annotations, annpair);
425 Py_DECREF(py_annlist);
426 PyGILState_Release(gstate);
431 srd_exception_catch(
"Failed to get %s decoder annotations", dec->
name);
434 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
435 Py_XDECREF(py_annlist);
436 PyGILState_Release(gstate);
442 static int get_annotation_rows(
struct srd_decoder *dec)
444 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
445 GSList *annotation_rows;
449 PyGILState_STATE gstate;
451 gstate = PyGILState_Ensure();
453 if (!PyObject_HasAttrString(dec->
py_dec,
"annotation_rows")) {
454 PyGILState_Release(gstate);
458 annotation_rows = NULL;
460 py_ann_rows = PyObject_GetAttrString(dec->
py_dec,
"annotation_rows");
464 if (!PyTuple_Check(py_ann_rows)) {
465 srd_err(
"Protocol decoder %s annotation_rows " 466 "must be a tuple.", dec->
name);
470 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
471 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
475 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
476 srd_err(
"Protocol decoder %s annotation_rows " 477 "must contain only tuples of 3 elements.",
483 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
485 py_item = PyTuple_GetItem(py_ann_row, 0);
488 if (py_str_as_str(py_item, &ann_row->
id) !=
SRD_OK)
491 py_item = PyTuple_GetItem(py_ann_row, 1);
494 if (py_str_as_str(py_item, &ann_row->
desc) !=
SRD_OK)
497 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
501 if (!PyTuple_Check(py_ann_classes)) {
502 srd_err(
"Protocol decoder %s annotation_rows tuples " 503 "must have a tuple of numbers as 3rd element.",
508 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
509 py_item = PyTuple_GetItem(py_ann_classes, k);
513 if (!PyLong_Check(py_item)) {
514 srd_err(
"Protocol decoder %s annotation row " 515 "class tuple must only contain numbers.",
519 class_idx = PyLong_AsSize_t(py_item);
520 if (PyErr_Occurred())
524 GSIZE_TO_POINTER(class_idx));
528 Py_DECREF(py_ann_rows);
529 PyGILState_Release(gstate);
534 srd_exception_catch(
"Failed to get %s decoder annotation rows",
538 g_slist_free_full(annotation_rows, &annotation_row_free);
539 Py_XDECREF(py_ann_rows);
540 PyGILState_Release(gstate);
546 static int get_binary_classes(
struct srd_decoder *dec)
548 PyObject *py_bin_classes, *py_bin_class;
552 PyGILState_STATE gstate;
554 gstate = PyGILState_Ensure();
556 if (!PyObject_HasAttrString(dec->
py_dec,
"binary")) {
557 PyGILState_Release(gstate);
563 py_bin_classes = PyObject_GetAttrString(dec->
py_dec,
"binary");
567 if (!PyTuple_Check(py_bin_classes)) {
568 srd_err(
"Protocol decoder %s binary classes should " 569 "be a tuple.", dec->
name);
573 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
574 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
578 if (!PyTuple_Check(py_bin_class)
579 || PyTuple_Size(py_bin_class) != 2) {
580 srd_err(
"Protocol decoder %s binary classes should " 581 "consist only of tuples of 2 elements.",
585 if (py_strseq_to_char(py_bin_class, &bin) !=
SRD_OK)
588 bin_classes = g_slist_prepend(bin_classes, bin);
590 dec->
binary = bin_classes;
591 Py_DECREF(py_bin_classes);
592 PyGILState_Release(gstate);
597 srd_exception_catch(
"Failed to get %s decoder binary classes",
601 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
602 Py_XDECREF(py_bin_classes);
603 PyGILState_Release(gstate);
609 static int check_method(PyObject *py_dec,
const char *mod_name,
610 const char *method_name)
614 PyGILState_STATE gstate;
616 gstate = PyGILState_Ensure();
618 py_method = PyObject_GetAttrString(py_dec, method_name);
620 srd_exception_catch(
"Protocol decoder %s Decoder class " 621 "has no %s() method", mod_name, method_name);
622 PyGILState_Release(gstate);
626 is_callable = PyCallable_Check(py_method);
627 Py_DECREF(py_method);
629 PyGILState_Release(gstate);
632 srd_err(
"Protocol decoder %s Decoder class attribute '%s' " 633 "is not a method.", mod_name, method_name);
653 PyGILState_STATE gstate;
658 gstate = PyGILState_Ensure();
660 py_apiver = PyObject_GetAttrString(d->
py_dec,
"api_version");
661 apiver = (py_apiver && PyLong_Check(py_apiver))
662 ? PyLong_AsLong(py_apiver) : 0;
663 Py_XDECREF(py_apiver);
665 PyGILState_Release(gstate);
681 PyObject *py_basedec;
685 const char *fail_txt;
686 PyGILState_STATE gstate;
688 if (!srd_check_init())
694 gstate = PyGILState_Ensure();
696 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
698 PyGILState_Release(gstate);
705 d->
py_mod = py_import_by_name(module_name);
707 fail_txt =
"import by name failed";
711 if (!mod_sigrokdecode) {
712 srd_err(
"sigrokdecode module not loaded.");
713 fail_txt =
"sigrokdecode(3) not loaded";
718 d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder");
720 fail_txt =
"no 'Decoder' attribute in imported module";
724 py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder");
726 fail_txt =
"no 'Decoder' attribute in sigrokdecode(3)";
730 is_subclass = PyObject_IsSubclass(d->
py_dec, py_basedec);
731 Py_DECREF(py_basedec);
734 srd_err(
"Decoder class in protocol decoder module %s is not " 735 "a subclass of sigrokdecode.Decoder.", module_name);
736 fail_txt =
"not a subclass of sigrokdecode.Decoder";
744 apiver = srd_decoder_apiver(d);
746 srd_exception_catch(
"Only PD API version 3 is supported, " 747 "decoder %s has version %ld", module_name, apiver);
748 fail_txt =
"API version mismatch";
754 if (check_method(d->
py_dec, module_name,
"start") !=
SRD_OK) {
755 fail_txt =
"no 'start()' method";
759 if (check_method(d->
py_dec, module_name,
"decode") !=
SRD_OK) {
760 fail_txt =
"no 'decode()' method";
766 fail_txt =
"no 'id' attribute";
771 fail_txt =
"no 'name' attribute";
776 fail_txt =
"no 'longname' attribute";
781 fail_txt =
"no 'desc' attribute";
786 fail_txt =
"no 'license' attribute";
791 fail_txt =
"missing or malformed 'inputs' attribute";
796 fail_txt =
"missing or malformed 'outputs' attribute";
801 if (get_options(d) !=
SRD_OK) {
802 fail_txt =
"cannot get options";
808 fail_txt =
"cannot get channels";
813 if (get_channels(d,
"optional_channels", &d->
opt_channels,
815 fail_txt =
"cannot get optional channels";
819 if (get_annotations(d) !=
SRD_OK) {
820 fail_txt =
"cannot get annotations";
824 if (get_annotation_rows(d) !=
SRD_OK) {
825 fail_txt =
"cannot get annotation rows";
829 if (get_binary_classes(d) !=
SRD_OK) {
830 fail_txt =
"cannot get binary classes";
834 PyGILState_Release(gstate);
837 pd_list = g_slist_append(pd_list, d);
843 if (strcmp(module_name,
"common")) {
844 srd_exception_catch(
"Failed to load decoder %s: %s",
845 module_name, fail_txt);
851 srd_err(
"Failed to load decoder %s: %s", module_name, fail_txt);
853 PyGILState_Release(gstate);
872 PyGILState_STATE gstate;
874 if (!srd_check_init())
880 gstate = PyGILState_Ensure();
882 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
885 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
886 srd_exception_catch(
"Failed to get docstring");
891 if (py_str != Py_None)
892 py_str_as_str(py_str, &doc);
895 PyGILState_Release(gstate);
900 PyGILState_Release(gstate);
916 struct srd_session *sess;
919 if (!srd_check_init())
931 for (l = sessions; l; l = l->next) {
933 srd_inst_free_all(sess);
937 pd_list = g_slist_remove(pd_list, dec);
944 static void srd_decoder_load_all_zip_path(
char *zip_path)
946 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
947 PyObject *prefix_obj, *files, *key, *value, *
set, *modname;
951 PyGILState_STATE gstate;
953 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
955 gstate = PyGILState_Ensure();
957 zipimport_mod = py_import_by_name(
"zipimport");
958 if (zipimport_mod == NULL)
961 zipimporter_class = PyObject_GetAttrString(zipimport_mod,
"zipimporter");
962 if (zipimporter_class == NULL)
965 zipimporter = PyObject_CallFunction(zipimporter_class,
"s", zip_path);
966 if (zipimporter == NULL)
969 prefix_obj = PyObject_GetAttrString(zipimporter,
"prefix");
970 if (prefix_obj == NULL)
973 files = PyObject_GetAttrString(zipimporter,
"_files");
974 if (files == NULL || !PyDict_Check(files))
977 set = PySet_New(NULL);
981 if (py_str_as_str(prefix_obj, &prefix) !=
SRD_OK)
984 prefix_len = strlen(prefix);
986 while (PyDict_Next(files, &pos, &key, &value)) {
988 if (py_str_as_str(key, &path) ==
SRD_OK) {
989 if (strlen(path) > prefix_len
990 && memcmp(path, prefix, prefix_len) == 0
991 && (slash = strchr(path + prefix_len,
'/'))) {
993 modname = PyUnicode_FromStringAndSize(path + prefix_len,
994 slash - (path + prefix_len));
995 if (modname == NULL) {
998 PySet_Add(
set, modname);
1007 while ((modname = PySet_Pop(
set))) {
1009 if (py_str_as_str(modname, &modname_str) ==
SRD_OK) {
1012 g_free(modname_str);
1020 Py_XDECREF(prefix_obj);
1021 Py_XDECREF(zipimporter);
1022 Py_XDECREF(zipimporter_class);
1023 Py_XDECREF(zipimport_mod);
1025 PyGILState_Release(gstate);
1028 static void srd_decoder_load_all_path(
char *path)
1031 const gchar *direntry;
1033 if (!(dir = g_dir_open(path, 0, NULL))) {
1035 srd_decoder_load_all_zip_path(path);
1044 while ((direntry = g_dir_read_name(dir)) != NULL) {
1062 if (!srd_check_init())
1065 for (l = searchpaths; l; l = l->next)
1066 srd_decoder_load_all_path(l->data);
1080 for (GSList *l = pd_list; l; l = l->next)
1082 g_slist_free(pd_list);
GSList * inputs
List of possible decoder input IDs.
char * name
The name of the channel.
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
int order
The index of the channel, i.e.
Structure which contains information about one protocol decoder channel.
int srd_decoder_load_all(void)
Load all installed protocol decoders.
void * py_mod
Python module.
char * desc
The description of the channel.
const GSList * srd_decoder_list(void)
Returns the list of loaded protocol decoders.
GSList * outputs
List of possible decoder output IDs.
int srd_decoder_unload(struct srd_decoder *dec)
Unload the specified protocol decoder.
Generic/unspecified error.
int srd_decoder_unload_all(void)
Unload all loaded protocol decoders.
char * license
The license of the decoder.
GSList * opt_channels
List of optional channels for this decoder.
void * py_dec
sigrokdecode.Decoder class.
char * longname
The (long) decoder name.
char * id
The ID of the channel.
The public libsigrokdecode header file to be used by frontends.
GSList * annotation_rows
List of annotation rows (row items: id, description, and a list of annotation classes belonging to th...
int srd_decoder_load(const char *module_name)
Load a protocol decoder module into the embedded Python interpreter.
char * name
The (short) decoder name.
GSList * options
List of decoder options.
GSList * channels
List of channels required by this decoder.
GSList * annotations
List of NULL-terminated char[], containing descriptions of the supported annotation output...
GSList * binary
List of NULL-terminated char[], containing descriptions of the supported binary output.
char * srd_decoder_doc_get(const struct srd_decoder *dec)
Return a protocol decoder's docstring.
char * desc
A (short, one-line) description of the decoder.