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);
177 static int get_channels(
const struct srd_decoder *d,
const char *attr,
178 GSList **out_pdchl,
int offset)
180 PyObject *py_channellist, *py_entry;
185 if (!PyObject_HasAttrString(d->
py_dec, attr))
191 py_channellist = PyObject_GetAttrString(d->
py_dec, attr);
195 if (!PyTuple_Check(py_channellist)) {
196 srd_err(
"Protocol decoder %s %s attribute is not a tuple.",
201 for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) {
202 py_entry = PyTuple_GetItem(py_channellist, i);
206 if (!PyDict_Check(py_entry)) {
207 srd_err(
"Protocol decoder %s %s attribute is not "
208 "a list of dict elements.", d->
name, attr);
213 pdchl = g_slist_prepend(pdchl, pdch);
215 if (py_dictitem_as_str(py_entry,
"id", &pdch->
id) !=
SRD_OK)
217 if (py_dictitem_as_str(py_entry,
"name", &pdch->
name) !=
SRD_OK)
219 if (py_dictitem_as_str(py_entry,
"desc", &pdch->
desc) !=
SRD_OK)
222 pdch->
order = offset + i;
225 Py_DECREF(py_channellist);
231 srd_exception_catch(
"Failed to get %s list of %s decoder",
234 g_slist_free_full(pdchl, &channel_free);
235 Py_XDECREF(py_channellist);
242 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
248 if (!PyObject_HasAttrString(d->
py_dec,
"options"))
255 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
259 if (!PyTuple_Check(py_opts)) {
260 srd_err(
"Protocol decoder %s: options attribute is not "
265 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
266 py_opt = PyTuple_GetItem(py_opts, opt);
270 if (!PyDict_Check(py_opt)) {
271 srd_err(
"Protocol decoder %s options: each option "
272 "must consist of a dictionary.", d->
name);
278 options = g_slist_prepend(options, o);
280 py_str = PyDict_GetItemString(py_opt,
"id");
282 srd_err(
"Protocol decoder %s option %zd has no id.",
286 if (py_str_as_str(py_str, &o->
id) !=
SRD_OK)
289 py_str = PyDict_GetItemString(py_opt,
"desc");
291 if (py_str_as_str(py_str, &o->
desc) !=
SRD_OK)
295 py_default = PyDict_GetItemString(py_opt,
"default");
297 gvar = py_obj_to_variant(py_default);
299 srd_err(
"Protocol decoder %s option 'default' has "
300 "invalid default value.", d->
name);
303 o->
def = g_variant_ref_sink(gvar);
306 py_values = PyDict_GetItemString(py_opt,
"values");
311 srd_err(
"No default for option '%s'.", o->
id);
314 if (!PyTuple_Check(py_values)) {
315 srd_err(
"Option '%s' values should be a tuple.", o->
id);
319 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
320 py_item = PyTuple_GetItem(py_values, i);
324 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
325 srd_err(
"All values for option '%s' must be "
326 "of the same type as the default.",
330 gvar = py_obj_to_variant(py_item);
332 srd_err(
"Protocol decoder %s option 'values' "
333 "contains invalid value.", d->
name);
337 g_variant_ref_sink(gvar));
347 srd_exception_catch(
"Failed to get %s decoder options", d->
name);
349 g_slist_free_full(options, &decoder_option_free);
357 static int get_annotations(
struct srd_decoder *dec)
359 PyObject *py_annlist, *py_ann;
364 if (!PyObject_HasAttrString(dec->
py_dec,
"annotations"))
369 py_annlist = PyObject_GetAttrString(dec->
py_dec,
"annotations");
373 if (!PyTuple_Check(py_annlist)) {
374 srd_err(
"Protocol decoder %s annotations should "
375 "be a tuple.", dec->
name);
379 for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) {
380 py_ann = PyTuple_GetItem(py_annlist, i);
384 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
385 srd_err(
"Protocol decoder %s annotation %zd should "
386 "be a tuple with two elements.",
390 if (py_strseq_to_char(py_ann, &annpair) !=
SRD_OK)
393 annotations = g_slist_prepend(annotations, annpair);
396 Py_DECREF(py_annlist);
401 srd_exception_catch(
"Failed to get %s decoder annotations", dec->
name);
403 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
404 Py_XDECREF(py_annlist);
411 static int get_annotation_rows(
struct srd_decoder *dec)
413 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
414 GSList *annotation_rows;
419 if (!PyObject_HasAttrString(dec->
py_dec,
"annotation_rows"))
422 annotation_rows = NULL;
424 py_ann_rows = PyObject_GetAttrString(dec->
py_dec,
"annotation_rows");
428 if (!PyTuple_Check(py_ann_rows)) {
429 srd_err(
"Protocol decoder %s annotation_rows "
430 "must be a tuple.", dec->
name);
434 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
435 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
439 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
440 srd_err(
"Protocol decoder %s annotation_rows "
441 "must contain only tuples of 3 elements.",
447 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
449 py_item = PyTuple_GetItem(py_ann_row, 0);
452 if (py_str_as_str(py_item, &ann_row->
id) !=
SRD_OK)
455 py_item = PyTuple_GetItem(py_ann_row, 1);
458 if (py_str_as_str(py_item, &ann_row->
desc) !=
SRD_OK)
461 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
465 if (!PyTuple_Check(py_ann_classes)) {
466 srd_err(
"Protocol decoder %s annotation_rows tuples "
467 "must have a tuple of numbers as 3rd element.",
472 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
473 py_item = PyTuple_GetItem(py_ann_classes, k);
477 if (!PyLong_Check(py_item)) {
478 srd_err(
"Protocol decoder %s annotation row "
479 "class tuple must only contain numbers.",
483 class_idx = PyLong_AsSize_t(py_item);
484 if (PyErr_Occurred())
488 GSIZE_TO_POINTER(class_idx));
492 Py_DECREF(py_ann_rows);
497 srd_exception_catch(
"Failed to get %s decoder annotation rows",
500 g_slist_free_full(annotation_rows, &annotation_row_free);
501 Py_XDECREF(py_ann_rows);
508 static int get_binary_classes(
struct srd_decoder *dec)
510 PyObject *py_bin_classes, *py_bin_class;
515 if (!PyObject_HasAttrString(dec->
py_dec,
"binary"))
520 py_bin_classes = PyObject_GetAttrString(dec->
py_dec,
"binary");
524 if (!PyTuple_Check(py_bin_classes)) {
525 srd_err(
"Protocol decoder %s binary classes should "
526 "be a tuple.", dec->
name);
530 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
531 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
535 if (!PyTuple_Check(py_bin_class)
536 || PyTuple_Size(py_bin_class) != 2) {
537 srd_err(
"Protocol decoder %s binary classes should "
538 "consist only of tuples of 2 elements.",
542 if (py_strseq_to_char(py_bin_class, &bin) !=
SRD_OK)
545 bin_classes = g_slist_prepend(bin_classes, bin);
547 dec->
binary = bin_classes;
548 Py_DECREF(py_bin_classes);
553 srd_exception_catch(
"Failed to get %s decoder binary classes",
556 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
557 Py_XDECREF(py_bin_classes);
564 static int check_method(PyObject *py_dec,
const char *mod_name,
565 const char *method_name)
570 py_method = PyObject_GetAttrString(py_dec, method_name);
572 srd_exception_catch(
"Protocol decoder %s Decoder class "
573 "has no %s() method", mod_name, method_name);
577 is_callable = PyCallable_Check(py_method);
578 Py_DECREF(py_method);
581 srd_err(
"Protocol decoder %s Decoder class attribute '%s' "
582 "is not a method.", mod_name, method_name);
600 PyObject *py_basedec, *py_apiver;
605 if (!srd_check_init())
611 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
616 srd_dbg(
"Loading protocol decoder '%s'.", module_name);
620 d->
py_mod = py_import_by_name(module_name);
624 if (!mod_sigrokdecode) {
625 srd_err(
"sigrokdecode module not loaded.");
630 d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder");
634 py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder");
638 is_subclass = PyObject_IsSubclass(d->
py_dec, py_basedec);
639 Py_DECREF(py_basedec);
642 srd_err(
"Decoder class in protocol decoder module %s is not "
643 "a subclass of sigrokdecode.Decoder.", module_name);
651 py_apiver = PyObject_GetAttrString(d->
py_dec,
"api_version");
652 apiver = (py_apiver && PyLong_Check(py_apiver))
653 ? PyLong_AsLong(py_apiver) : 0;
654 Py_XDECREF(py_apiver);
657 srd_exception_catch(
"Only PDs of API version 2 are supported");
663 if (check_method(d->
py_dec, module_name,
"start") !=
SRD_OK)
666 if (check_method(d->
py_dec, module_name,
"decode") !=
SRD_OK)
686 if (get_options(d) !=
SRD_OK)
694 if (get_channels(d,
"optional_channels", &d->
opt_channels,
698 if (get_annotations(d) !=
SRD_OK)
701 if (get_annotation_rows(d) !=
SRD_OK)
704 if (get_binary_classes(d) !=
SRD_OK)
708 pd_list = g_slist_append(pd_list, d);
713 srd_exception_catch(
"Failed to load decoder %s", module_name);
735 if (!srd_check_init())
741 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
744 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
745 srd_exception_catch(
"Failed to get docstring");
750 if (py_str != Py_None)
751 py_str_as_str(py_str, &doc);
768 struct srd_session *sess;
771 if (!srd_check_init())
777 srd_dbg(
"Unloading protocol decoder '%s'.", dec->
name);
785 for (l = sessions; l; l = l->next) {
787 srd_inst_free_all(sess, NULL);
791 pd_list = g_slist_remove(pd_list, dec);
798 static void srd_decoder_load_all_zip_path(
char *path)
800 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
801 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
806 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
808 zipimport_mod = py_import_by_name(
"zipimport");
809 if (zipimport_mod == NULL)
812 zipimporter_class = PyObject_GetAttrString(zipimport_mod,
"zipimporter");
813 if (zipimporter_class == NULL)
816 zipimporter = PyObject_CallFunction(zipimporter_class,
"s", path);
817 if (zipimporter == NULL)
820 prefix_obj = PyObject_GetAttrString(zipimporter,
"prefix");
821 if (prefix_obj == NULL)
824 files = PyObject_GetAttrString(zipimporter,
"_files");
825 if (files == NULL || !PyDict_Check(files))
828 set = PySet_New(NULL);
832 if (py_str_as_str(prefix_obj, &prefix) !=
SRD_OK)
835 prefix_len = strlen(prefix);
837 while (PyDict_Next(files, &pos, &key, &value)) {
839 if (py_str_as_str(key, &path) ==
SRD_OK) {
840 if (strlen(path) > prefix_len
841 && memcmp(path, prefix, prefix_len) == 0
842 && (slash = strchr(path + prefix_len,
'/'))) {
844 modname = PyUnicode_FromStringAndSize(path + prefix_len,
845 slash - (path + prefix_len));
846 if (modname == NULL) {
849 PySet_Add(set, modname);
858 while ((modname = PySet_Pop(set))) {
860 if (py_str_as_str(modname, &modname_str) ==
SRD_OK) {
871 Py_XDECREF(prefix_obj);
872 Py_XDECREF(zipimporter);
873 Py_XDECREF(zipimporter_class);
874 Py_XDECREF(zipimport_mod);
878 static void srd_decoder_load_all_path(
char *path)
881 const gchar *direntry;
883 if (!(dir = g_dir_open(path, 0, NULL))) {
886 srd_decoder_load_all_zip_path(path);
893 while ((direntry = g_dir_read_name(dir)) != NULL) {
912 if (!srd_check_init())
915 for (l = searchpaths; l; l = l->next)
916 srd_decoder_load_all_path(l->data);
933 for (l = pd_list; l; l = l->next) {
937 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.
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 * 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.