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);
161 g_slist_free_full(dec->
options, &decoder_option_free);
162 g_slist_free_full(dec->
binary, (GDestroyNotify)&g_strfreev);
164 g_slist_free_full(dec->
annotations, (GDestroyNotify)&g_strfreev);
166 g_slist_free_full(dec->
channels, &channel_free);
168 g_slist_free_full(dec->
outputs, g_free);
169 g_slist_free_full(dec->
inputs, g_free);
179 static int get_channels(
const struct srd_decoder *d,
const char *attr,
180 GSList **out_pdchl,
int offset)
182 PyObject *py_channellist, *py_entry;
187 if (!PyObject_HasAttrString(d->
py_dec, attr))
193 py_channellist = PyObject_GetAttrString(d->
py_dec, attr);
197 if (!PyTuple_Check(py_channellist)) {
198 srd_err(
"Protocol decoder %s %s attribute is not a tuple.",
203 for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) {
204 py_entry = PyTuple_GetItem(py_channellist, i);
208 if (!PyDict_Check(py_entry)) {
209 srd_err(
"Protocol decoder %s %s attribute is not " 210 "a list of dict elements.", d->
name, attr);
215 pdchl = g_slist_prepend(pdchl, pdch);
217 if (py_dictitem_as_str(py_entry,
"id", &pdch->
id) !=
SRD_OK)
219 if (py_dictitem_as_str(py_entry,
"name", &pdch->
name) !=
SRD_OK)
221 if (py_dictitem_as_str(py_entry,
"desc", &pdch->
desc) !=
SRD_OK)
224 pdch->
order = offset + i;
227 Py_DECREF(py_channellist);
233 srd_exception_catch(
"Failed to get %s list of %s decoder",
236 g_slist_free_full(pdchl, &channel_free);
237 Py_XDECREF(py_channellist);
244 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
250 if (!PyObject_HasAttrString(d->
py_dec,
"options"))
257 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
261 if (!PyTuple_Check(py_opts)) {
262 srd_err(
"Protocol decoder %s: options attribute is not " 267 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
268 py_opt = PyTuple_GetItem(py_opts, opt);
272 if (!PyDict_Check(py_opt)) {
273 srd_err(
"Protocol decoder %s options: each option " 274 "must consist of a dictionary.", d->
name);
280 options = g_slist_prepend(options, o);
282 py_str = PyDict_GetItemString(py_opt,
"id");
284 srd_err(
"Protocol decoder %s option %zd has no id.",
288 if (py_str_as_str(py_str, &o->
id) !=
SRD_OK)
291 py_str = PyDict_GetItemString(py_opt,
"desc");
293 if (py_str_as_str(py_str, &o->
desc) !=
SRD_OK)
297 py_default = PyDict_GetItemString(py_opt,
"default");
299 gvar = py_obj_to_variant(py_default);
301 srd_err(
"Protocol decoder %s option 'default' has " 302 "invalid default value.", d->
name);
305 o->
def = g_variant_ref_sink(gvar);
308 py_values = PyDict_GetItemString(py_opt,
"values");
313 srd_err(
"No default for option '%s'.", o->
id);
316 if (!PyTuple_Check(py_values)) {
317 srd_err(
"Option '%s' values should be a tuple.", o->
id);
321 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
322 py_item = PyTuple_GetItem(py_values, i);
326 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
327 srd_err(
"All values for option '%s' must be " 328 "of the same type as the default.",
332 gvar = py_obj_to_variant(py_item);
334 srd_err(
"Protocol decoder %s option 'values' " 335 "contains invalid value.", d->
name);
339 g_variant_ref_sink(gvar));
349 srd_exception_catch(
"Failed to get %s decoder options", d->
name);
351 g_slist_free_full(options, &decoder_option_free);
359 static int get_annotations(
struct srd_decoder *dec)
361 PyObject *py_annlist, *py_ann;
366 if (!PyObject_HasAttrString(dec->
py_dec,
"annotations"))
371 py_annlist = PyObject_GetAttrString(dec->
py_dec,
"annotations");
375 if (!PyTuple_Check(py_annlist)) {
376 srd_err(
"Protocol decoder %s annotations should " 377 "be a tuple.", dec->
name);
381 for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) {
382 py_ann = PyTuple_GetItem(py_annlist, i);
386 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
387 srd_err(
"Protocol decoder %s annotation %zd should " 388 "be a tuple with two elements.",
392 if (py_strseq_to_char(py_ann, &annpair) !=
SRD_OK)
395 annotations = g_slist_prepend(annotations, annpair);
398 Py_DECREF(py_annlist);
403 srd_exception_catch(
"Failed to get %s decoder annotations", dec->
name);
405 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
406 Py_XDECREF(py_annlist);
413 static int get_annotation_rows(
struct srd_decoder *dec)
415 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
416 GSList *annotation_rows;
421 if (!PyObject_HasAttrString(dec->
py_dec,
"annotation_rows"))
424 annotation_rows = NULL;
426 py_ann_rows = PyObject_GetAttrString(dec->
py_dec,
"annotation_rows");
430 if (!PyTuple_Check(py_ann_rows)) {
431 srd_err(
"Protocol decoder %s annotation_rows " 432 "must be a tuple.", dec->
name);
436 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
437 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
441 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
442 srd_err(
"Protocol decoder %s annotation_rows " 443 "must contain only tuples of 3 elements.",
449 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
451 py_item = PyTuple_GetItem(py_ann_row, 0);
454 if (py_str_as_str(py_item, &ann_row->
id) !=
SRD_OK)
457 py_item = PyTuple_GetItem(py_ann_row, 1);
460 if (py_str_as_str(py_item, &ann_row->
desc) !=
SRD_OK)
463 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
467 if (!PyTuple_Check(py_ann_classes)) {
468 srd_err(
"Protocol decoder %s annotation_rows tuples " 469 "must have a tuple of numbers as 3rd element.",
474 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
475 py_item = PyTuple_GetItem(py_ann_classes, k);
479 if (!PyLong_Check(py_item)) {
480 srd_err(
"Protocol decoder %s annotation row " 481 "class tuple must only contain numbers.",
485 class_idx = PyLong_AsSize_t(py_item);
486 if (PyErr_Occurred())
490 GSIZE_TO_POINTER(class_idx));
494 Py_DECREF(py_ann_rows);
499 srd_exception_catch(
"Failed to get %s decoder annotation rows",
502 g_slist_free_full(annotation_rows, &annotation_row_free);
503 Py_XDECREF(py_ann_rows);
510 static int get_binary_classes(
struct srd_decoder *dec)
512 PyObject *py_bin_classes, *py_bin_class;
517 if (!PyObject_HasAttrString(dec->
py_dec,
"binary"))
522 py_bin_classes = PyObject_GetAttrString(dec->
py_dec,
"binary");
526 if (!PyTuple_Check(py_bin_classes)) {
527 srd_err(
"Protocol decoder %s binary classes should " 528 "be a tuple.", dec->
name);
532 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
533 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
537 if (!PyTuple_Check(py_bin_class)
538 || PyTuple_Size(py_bin_class) != 2) {
539 srd_err(
"Protocol decoder %s binary classes should " 540 "consist only of tuples of 2 elements.",
544 if (py_strseq_to_char(py_bin_class, &bin) !=
SRD_OK)
547 bin_classes = g_slist_prepend(bin_classes, bin);
549 dec->
binary = bin_classes;
550 Py_DECREF(py_bin_classes);
555 srd_exception_catch(
"Failed to get %s decoder binary classes",
558 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
559 Py_XDECREF(py_bin_classes);
566 static int check_method(PyObject *py_dec,
const char *mod_name,
567 const char *method_name)
572 py_method = PyObject_GetAttrString(py_dec, method_name);
574 srd_exception_catch(
"Protocol decoder %s Decoder class " 575 "has no %s() method", mod_name, method_name);
579 is_callable = PyCallable_Check(py_method);
580 Py_DECREF(py_method);
583 srd_err(
"Protocol decoder %s Decoder class attribute '%s' " 584 "is not a method.", mod_name, method_name);
608 py_apiver = PyObject_GetAttrString(d->
py_dec,
"api_version");
609 apiver = (py_apiver && PyLong_Check(py_apiver))
610 ? PyLong_AsLong(py_apiver) : 0;
611 Py_XDECREF(py_apiver);
627 PyObject *py_basedec;
631 const char *fail_txt;
633 if (!srd_check_init())
639 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
644 srd_dbg(
"Loading protocol decoder '%s'.", module_name);
649 d->
py_mod = py_import_by_name(module_name);
651 fail_txt =
"import by name failed";
655 if (!mod_sigrokdecode) {
656 srd_err(
"sigrokdecode module not loaded.");
657 fail_txt =
"sigrokdecode(3) not loaded";
662 d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder");
664 fail_txt =
"no 'Decoder' attribute in imported module";
668 py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder");
670 fail_txt =
"no 'Decoder' attribute in sigrokdecode(3)";
674 is_subclass = PyObject_IsSubclass(d->
py_dec, py_basedec);
675 Py_DECREF(py_basedec);
678 srd_err(
"Decoder class in protocol decoder module %s is not " 679 "a subclass of sigrokdecode.Decoder.", module_name);
680 fail_txt =
"not a subclass of sigrokdecode.Decoder";
688 apiver = srd_decoder_apiver(d);
689 if (apiver != 2 && apiver != 3) {
690 srd_exception_catch(
"Only PD API version 2/3 is supported, " 691 "decoder %s has version %ld", module_name, apiver);
692 fail_txt =
"API version mismatch";
698 if (check_method(d->
py_dec, module_name,
"start") !=
SRD_OK) {
699 fail_txt =
"no 'start()' method";
703 if (check_method(d->
py_dec, module_name,
"decode") !=
SRD_OK) {
704 fail_txt =
"no 'decode()' method";
710 fail_txt =
"no 'id' attribute";
715 fail_txt =
"no 'name' attribute";
720 fail_txt =
"no 'longname' attribute";
725 fail_txt =
"no 'desc' attribute";
730 fail_txt =
"no 'license' attribute";
735 fail_txt =
"missing or malformed 'inputs' attribute";
740 fail_txt =
"missing or malformed 'outputs' attribute";
745 if (get_options(d) !=
SRD_OK) {
746 fail_txt =
"cannot get options";
752 fail_txt =
"cannot get channels";
757 if (get_channels(d,
"optional_channels", &d->
opt_channels,
759 fail_txt =
"cannot get optional channels";
763 if (get_annotations(d) !=
SRD_OK) {
764 fail_txt =
"cannot get annotations";
768 if (get_annotation_rows(d) !=
SRD_OK) {
769 fail_txt =
"cannot get annotation rows";
773 if (get_binary_classes(d) !=
SRD_OK) {
774 fail_txt =
"cannot get binary classes";
779 pd_list = g_slist_append(pd_list, d);
785 if (strcmp(module_name,
"common")) {
786 srd_exception_catch(
"Failed to load decoder %s: %s",
787 module_name, fail_txt);
792 srd_err(
"Failed to load decoder %s: %s", module_name, fail_txt);
813 if (!srd_check_init())
819 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
822 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
823 srd_exception_catch(
"Failed to get docstring");
828 if (py_str != Py_None)
829 py_str_as_str(py_str, &doc);
846 struct srd_session *sess;
849 if (!srd_check_init())
855 srd_dbg(
"Unloading protocol decoder '%s'.", dec->
name);
863 for (l = sessions; l; l = l->next) {
865 srd_inst_free_all(sess);
869 pd_list = g_slist_remove(pd_list, dec);
876 static void srd_decoder_load_all_zip_path(
char *path)
878 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
879 PyObject *prefix_obj, *files, *key, *value, *
set, *modname;
884 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
886 zipimport_mod = py_import_by_name(
"zipimport");
887 if (zipimport_mod == NULL)
890 zipimporter_class = PyObject_GetAttrString(zipimport_mod,
"zipimporter");
891 if (zipimporter_class == NULL)
894 zipimporter = PyObject_CallFunction(zipimporter_class,
"s", path);
895 if (zipimporter == NULL)
898 prefix_obj = PyObject_GetAttrString(zipimporter,
"prefix");
899 if (prefix_obj == NULL)
902 files = PyObject_GetAttrString(zipimporter,
"_files");
903 if (files == NULL || !PyDict_Check(files))
906 set = PySet_New(NULL);
910 if (py_str_as_str(prefix_obj, &prefix) !=
SRD_OK)
913 prefix_len = strlen(prefix);
915 while (PyDict_Next(files, &pos, &key, &value)) {
917 if (py_str_as_str(key, &path) ==
SRD_OK) {
918 if (strlen(path) > prefix_len
919 && memcmp(path, prefix, prefix_len) == 0
920 && (slash = strchr(path + prefix_len,
'/'))) {
922 modname = PyUnicode_FromStringAndSize(path + prefix_len,
923 slash - (path + prefix_len));
924 if (modname == NULL) {
927 PySet_Add(
set, modname);
936 while ((modname = PySet_Pop(
set))) {
938 if (py_str_as_str(modname, &modname_str) ==
SRD_OK) {
949 Py_XDECREF(prefix_obj);
950 Py_XDECREF(zipimporter);
951 Py_XDECREF(zipimporter_class);
952 Py_XDECREF(zipimport_mod);
956 static void srd_decoder_load_all_path(
char *path)
959 const gchar *direntry;
961 if (!(dir = g_dir_open(path, 0, NULL))) {
964 srd_decoder_load_all_zip_path(path);
971 while ((direntry = g_dir_read_name(dir)) != NULL) {
990 if (!srd_check_init())
993 for (l = searchpaths; l; l = l->next)
994 srd_decoder_load_all_path(l->data);
1009 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.