23 #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 int get_channels(
const struct srd_decoder *d,
const char *attr,
106 PyObject *py_channellist, *py_entry;
108 int ret, num_channels, i;
110 if (!PyObject_HasAttrString(d->
py_dec, attr))
114 py_channellist = PyObject_GetAttrString(d->
py_dec, attr);
115 if (!PyTuple_Check(py_channellist)) {
116 srd_err(
"Protocol decoder %s %s attribute is not a tuple.",
121 if ((num_channels = PyTuple_Size(py_channellist)) == 0)
126 for (i = 0; i < num_channels; i++) {
127 py_entry = PyTuple_GetItem(py_channellist, i);
128 if (!PyDict_Check(py_entry)) {
129 srd_err(
"Protocol decoder %s %s attribute is not "
130 "a list with dict elements.", d->
name, attr);
135 if (!(pdch = g_try_malloc(
sizeof(
struct srd_channel)))) {
136 srd_err(
"Failed to g_malloc() struct srd_channel.");
141 if ((py_dictitem_as_str(py_entry,
"id", &pdch->
id)) !=
SRD_OK) {
145 if ((py_dictitem_as_str(py_entry,
"name", &pdch->
name)) !=
SRD_OK) {
149 if ((py_dictitem_as_str(py_entry,
"desc", &pdch->
desc)) !=
SRD_OK) {
155 *pdchl = g_slist_append(*pdchl, pdch);
158 Py_DecRef(py_channellist);
165 PyObject *py_opts, *py_opt, *py_val, *py_default, *py_item;
174 if (!PyObject_HasAttrString(d->
py_dec,
"options"))
179 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
180 if (!PyTuple_Check(py_opts)) {
181 srd_err(
"Protocol decoder %s: options attribute is not "
186 for (opt = 0; opt < PyTuple_Size(py_opts); opt++) {
187 py_opt = PyTuple_GetItem(py_opts, opt);
188 if (!PyDict_Check(py_opt)) {
189 srd_err(
"Protocol decoder %s options: each option "
190 "must consist of a dictionary.", d->
name);
193 if (!(py_val = PyDict_GetItemString(py_opt,
"id"))) {
194 srd_err(
"Protocol decoder %s option %d has no "
199 py_str_as_str(py_val, &o->
id);
201 if ((py_val = PyDict_GetItemString(py_opt,
"desc")))
202 py_str_as_str(py_val, &o->
desc);
204 if ((py_default = PyDict_GetItemString(py_opt,
"default"))) {
205 if (PyUnicode_Check(py_default)) {
207 py_str_as_str(py_default, &sval);
208 o->
def = g_variant_new_string(sval);
210 }
else if (PyLong_Check(py_default)) {
212 lval = PyLong_AsLongAndOverflow(py_default, &overflow);
216 srd_err(
"Protocol decoder %s option 'default' has "
217 "invalid default value.", d->
name);
220 o->
def = g_variant_new_int64(lval);
221 }
else if (PyFloat_Check(py_default)) {
223 if ((dval = PyFloat_AsDouble(py_default)) == -1.0) {
225 srd_err(
"Protocol decoder %s option 'default' has "
226 "invalid default value.", d->
name);
229 o->
def = g_variant_new_double(dval);
231 srd_err(
"Protocol decoder %s option 'default' has "
232 "value of unsupported type '%s'.", d->
name,
233 Py_TYPE(py_default)->tp_name);
236 g_variant_ref_sink(o->
def);
239 if ((py_val = PyDict_GetItemString(py_opt,
"values"))) {
243 srd_err(
"No default for option '%s'", o->
id);
246 if (!PyTuple_Check(py_val)) {
247 srd_err(
"Option '%s' values should be a tuple.", o->
id);
250 for (i = 0; i < PyTuple_Size(py_val); i++) {
251 py_item = PyTuple_GetItem(py_val, i);
252 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
253 srd_err(
"All values for option '%s' must be "
254 "of the same type as the default.",
258 if (PyUnicode_Check(py_item)) {
260 py_str_as_str(py_item, &sval);
261 gvar = g_variant_new_string(sval);
262 g_variant_ref_sink(gvar);
265 }
else if (PyLong_Check(py_item)) {
267 lval = PyLong_AsLongAndOverflow(py_item, &overflow);
271 srd_err(
"Protocol decoder %s option 'values' "
272 "has invalid value.", d->
name);
275 gvar = g_variant_new_int64(lval);
276 g_variant_ref_sink(gvar);
278 }
else if (PyFloat_Check(py_item)) {
280 if ((dval = PyFloat_AsDouble(py_item)) == -1.0) {
282 srd_err(
"Protocol decoder %s option 'default' has "
283 "invalid default value.", d->
name);
286 gvar = g_variant_new_double(dval);
287 g_variant_ref_sink(gvar);
309 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
310 PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
311 PyObject *py_ann_classes, *py_long;
314 char **ann, **bin, *ann_row_id, *ann_row_desc;
316 GSList *l, *ann_classes;
319 if (!srd_check_init())
325 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
330 srd_dbg(
"Loading protocol decoder '%s'.", module_name);
332 py_basedec = py_method = py_attr = NULL;
334 if (!(d = g_try_malloc0(
sizeof(
struct srd_decoder)))) {
335 srd_dbg(
"Failed to g_malloc() struct srd_decoder.");
343 if (!(d->
py_mod = PyImport_ImportModule(module_name))) {
344 srd_exception_catch(
"Import of '%s' failed.", module_name);
349 if (!(d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder"))) {
352 srd_err(
"Decoder class not found in protocol decoder %s.",
357 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder"))) {
358 srd_dbg(
"sigrokdecode module not loaded.");
362 if (!PyObject_IsSubclass(d->
py_dec, py_basedec)) {
363 srd_err(
"Decoder class in protocol decoder module %s is not "
364 "a subclass of sigrokdecode.Decoder.", module_name);
367 Py_CLEAR(py_basedec);
373 py_long = PyObject_GetAttrString(d->
py_dec,
"api_version");
374 if (PyLong_AsLong(py_long) != 2) {
375 srd_err(
"Only PDs of API version 2 are supported.");
381 if (!PyObject_HasAttrString(d->
py_dec,
"start")) {
382 srd_err(
"Protocol decoder %s has no start() method Decoder "
383 "class.", module_name);
386 py_method = PyObject_GetAttrString(d->
py_dec,
"start");
387 if (!PyFunction_Check(py_method)) {
388 srd_err(
"Protocol decoder %s Decoder class attribute 'start' "
389 "is not a method.", module_name);
395 if (!PyObject_HasAttrString(d->
py_dec,
"decode")) {
396 srd_err(
"Protocol decoder %s has no decode() method Decoder "
397 "class.", module_name);
400 py_method = PyObject_GetAttrString(d->
py_dec,
"decode");
401 if (!PyFunction_Check(py_method)) {
402 srd_err(
"Protocol decoder %s Decoder class attribute 'decode' "
403 "is not a method.", module_name);
425 if (get_options(d) !=
SRD_OK)
451 if (PyObject_HasAttrString(d->
py_dec,
"annotations")) {
452 py_annlist = PyObject_GetAttrString(d->
py_dec,
"annotations");
453 if (!PyTuple_Check(py_annlist)) {
454 srd_err(
"Protocol decoder %s annotations should "
455 "be a tuple.", module_name);
458 for (i = 0; i < PyTuple_Size(py_annlist); i++) {
459 py_ann = PyTuple_GetItem(py_annlist, i);
460 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
461 srd_err(
"Protocol decoder %s annotation %d should "
462 "be a tuple with two elements.", module_name, i + 1);
466 if (py_strseq_to_char(py_ann, &ann) !=
SRD_OK) {
475 if (PyObject_HasAttrString(d->
py_dec,
"annotation_rows")) {
476 py_ann_rows = PyObject_GetAttrString(d->
py_dec,
"annotation_rows");
477 if (!PyTuple_Check(py_ann_rows)) {
478 srd_err(
"Protocol decoder %s annotation row list "
479 "must be a tuple.", module_name);
482 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
483 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
484 if (!PyTuple_Check(py_ann_row)) {
485 srd_err(
"Protocol decoder %s annotation rows "
486 "must be tuples.", module_name);
489 if (PyTuple_Size(py_ann_row) != 3
490 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
491 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
492 || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
493 srd_err(
"Protocol decoder %s annotation rows "
494 "must contain tuples containing two "
495 "strings and a tuple.", module_name);
499 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) !=
SRD_OK)
502 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) !=
SRD_OK)
505 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
507 for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
508 py_long = PyTuple_GetItem(py_ann_classes, j);
509 if (!PyLong_Check(py_long)) {
510 srd_err(
"Protocol decoder %s annotation row class "
511 "list must only contain numbers.", module_name);
514 ann_classes = g_slist_append(ann_classes,
515 GINT_TO_POINTER(PyLong_AsLong(py_long)));
519 ann_row->
id = ann_row_id;
520 ann_row->
desc = ann_row_desc;
528 if (PyObject_HasAttrString(d->
py_dec,
"binary")) {
529 py_bin_classes = PyObject_GetAttrString(d->
py_dec,
"binary");
530 if (!PyTuple_Check(py_bin_classes)) {
531 srd_err(
"Protocol decoder %s binary classes should "
532 "be a tuple.", module_name);
535 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
536 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
537 if (!PyTuple_Check(py_bin_class)) {
538 srd_err(
"Protocol decoder %s binary classes "
539 "should consist of tuples.", module_name);
542 if (PyTuple_Size(py_bin_class) != 2
543 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
544 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
545 srd_err(
"Protocol decoder %s binary classes should "
546 "contain tuples with two strings.", module_name);
550 if (py_strseq_to_char(py_bin_class, &bin) !=
SRD_OK) {
558 pd_list = g_slist_append(pd_list, d);
564 Py_XDECREF(py_method);
565 Py_XDECREF(py_basedec);
589 if (!srd_check_init())
595 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
598 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
599 srd_exception_catch(
"");
604 if (py_str != Py_None)
605 py_str_as_str(py_str, &doc);
611 static void free_channels(GSList *channellist)
616 if (channellist == NULL)
619 for (l = channellist; l; l = l->next) {
626 g_slist_free(channellist);
641 struct srd_session *sess;
644 if (!srd_check_init())
650 srd_dbg(
"Unloading protocol decoder '%s'.", dec->
name);
658 for (l = sessions; l; l = l->next) {
660 srd_inst_free_all(sess, NULL);
663 for (l = dec->
options; l; l = l->next) {
667 g_variant_unref(o->
def);
690 static void srd_decoder_load_all_path(
char *path)
693 const gchar *direntry;
695 if (!(dir = g_dir_open(path, 0, NULL)))
702 while ((direntry = g_dir_read_name(dir)) != NULL) {
721 if (!srd_check_init())
724 for (l = searchpaths; l; l = l->next)
725 srd_decoder_load_all_path(l->data);
742 for (l = pd_list; l; l = l->next) {
746 g_slist_free(pd_list);
char * license
The license of the decoder.
GSList * opt_channels
List of optional channels for this decoder.
The public libsigrokdecode header file to be used by frontends.
PyObject * py_dec
sigrokdecode.Decoder class.
int srd_decoder_unload_all(void)
Unload all loaded protocol decoders.
Structure which contains information about one protocol decoder channel.
int srd_decoder_load(const char *module_name)
Load a protocol decoder module into the embedded Python interpreter.
char * srd_decoder_doc_get(const struct srd_decoder *dec)
Return a protocol decoder's docstring.
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...
char * desc
A (short, one-line) description of the decoder.
PyObject * py_mod
Python module.
GSList * binary
List of NULL-terminated char[], containing descriptions of the supported binary output.
Generic/unspecified error.
GSList * options
List of decoder options.
int srd_decoder_load_all(void)
Load all installed protocol decoders.
char * desc
The description of the channel.
const GSList * srd_decoder_list(void)
Returns the list of supported/loaded protocol decoders.
GSList * annotation_rows
List of annotation rows (row items: id, description, and a list of annotation classes belonging to th...
struct srd_decoder * srd_decoder_get_by_id(const char *id)
Get the decoder with the specified ID.
int srd_decoder_unload(struct srd_decoder *dec)
Unload the specified protocol decoder.
Malloc/calloc/realloc error.
GSList * channels
List of channels required by this decoder.