2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "libsigrokdecode-internal.h"
29 * Listing, loading, unloading, and handling protocol decoders.
33 * @defgroup grp_decoder Protocol decoders
35 * Handling protocol decoders.
42 /* The list of protocol decoders. */
43 SRD_PRIV GSList *pd_list = NULL;
46 extern GSList *searchpaths;
49 extern GSList *sessions;
51 /* module_sigrokdecode.c */
52 extern SRD_PRIV PyObject *mod_sigrokdecode;
57 * Returns the list of supported/loaded protocol decoders.
59 * This is a GSList containing the names of the decoders as strings.
61 * @return List of decoders, NULL if none are supported or loaded.
63 * @since 0.1.0 (but the API changed in 0.2.0)
65 SRD_API const GSList *srd_decoder_list(void)
71 * Get the decoder with the specified ID.
73 * @param id The ID string of the decoder to return.
75 * @return The decoder with the specified ID, or NULL if not found.
79 SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
82 struct srd_decoder *dec;
84 for (l = pd_list; l; l = l->next) {
86 if (!strcmp(dec->id, id))
93 static int get_probes(const struct srd_decoder *d, const char *attr,
96 PyObject *py_probelist, *py_entry;
98 int ret, num_probes, i;
100 if (!PyObject_HasAttrString(d->py_dec, attr))
101 /* No probes of this type specified. */
104 py_probelist = PyObject_GetAttrString(d->py_dec, attr);
105 if (!PyTuple_Check(py_probelist)) {
106 srd_err("Protocol decoder %s %s attribute is not a tuple.",
108 return SRD_ERR_PYTHON;
111 if ((num_probes = PyTuple_Size(py_probelist)) == 0)
112 /* Empty probelist. */
116 for (i = 0; i < num_probes; i++) {
117 py_entry = PyTuple_GetItem(py_probelist, i);
118 if (!PyDict_Check(py_entry)) {
119 srd_err("Protocol decoder %s %s attribute is not "
120 "a list with dict elements.", d->name, attr);
121 ret = SRD_ERR_PYTHON;
125 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
126 srd_err("Failed to g_malloc() struct srd_probe.");
127 ret = SRD_ERR_MALLOC;
131 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK) {
132 ret = SRD_ERR_PYTHON;
135 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK) {
136 ret = SRD_ERR_PYTHON;
139 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK) {
140 ret = SRD_ERR_PYTHON;
145 *pl = g_slist_append(*pl, p);
148 Py_DecRef(py_probelist);
153 static int get_options(struct srd_decoder *d)
155 PyObject *py_opts, *py_opt, *py_val, *py_default, *py_item;
157 struct srd_decoder_option *o;
164 if (!PyObject_HasAttrString(d->py_dec, "options"))
165 /* No options, that's fine. */
168 /* If present, options must be a tuple. */
169 py_opts = PyObject_GetAttrString(d->py_dec, "options");
170 if (!PyTuple_Check(py_opts)) {
171 srd_err("Protocol decoder %s: options attribute is not "
173 return SRD_ERR_PYTHON;
176 for (opt = 0; opt < PyTuple_Size(py_opts); opt++) {
177 py_opt = PyTuple_GetItem(py_opts, opt);
178 if (!PyDict_Check(py_opt)) {
179 srd_err("Protocol decoder %s options: each option "
180 "must consist of a dictionary.", d->name);
181 return SRD_ERR_PYTHON;
183 if (!(py_val = PyDict_GetItemString(py_opt, "id"))) {
184 srd_err("Protocol decoder %s option %d has no "
186 return SRD_ERR_PYTHON;
188 o = g_malloc0(sizeof(struct srd_decoder_option));
189 py_str_as_str(py_val, &o->id);
191 if ((py_val = PyDict_GetItemString(py_opt, "desc")))
192 py_str_as_str(py_val, &o->desc);
194 if ((py_default = PyDict_GetItemString(py_opt, "default"))) {
195 if (PyUnicode_Check(py_default)) {
197 py_str_as_str(py_default, &sval);
198 o->def = g_variant_new_string(sval);
200 } else if (PyLong_Check(py_default)) {
202 lval = PyLong_AsLongAndOverflow(py_default, &overflow);
204 /* Value is < LONG_MIN or > LONG_MAX */
206 srd_err("Protocol decoder %s option 'default' has "
207 "invalid default value.", d->name);
208 return SRD_ERR_PYTHON;
210 o->def = g_variant_new_int64(lval);
211 } else if (PyFloat_Check(py_default)) {
213 if ((dval = PyFloat_AsDouble(py_default)) == -1.0) {
215 srd_err("Protocol decoder %s option 'default' has "
216 "invalid default value.", d->name);
217 return SRD_ERR_PYTHON;
219 o->def = g_variant_new_double(dval);
221 srd_err("Protocol decoder %s option 'default' has "
222 "value of unsupported type '%s'.", d->name,
223 Py_TYPE(py_default)->tp_name);
224 return SRD_ERR_PYTHON;
226 g_variant_ref_sink(o->def);
229 if ((py_val = PyDict_GetItemString(py_opt, "values"))) {
230 /* A default is required if a list of values is
231 * given, since it's used to verify their type. */
233 srd_err("No default for option '%s'", o->id);
234 return SRD_ERR_PYTHON;
236 if (!PyTuple_Check(py_val)) {
237 srd_err("Option '%s' values should be a tuple.", o->id);
238 return SRD_ERR_PYTHON;
240 for (i = 0; i < PyTuple_Size(py_val); i++) {
241 py_item = PyTuple_GetItem(py_val, i);
242 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
243 srd_err("All values for option '%s' must be "
244 "of the same type as the default.",
246 return SRD_ERR_PYTHON;
248 if (PyUnicode_Check(py_item)) {
250 py_str_as_str(py_item, &sval);
251 gvar = g_variant_new_string(sval);
252 g_variant_ref_sink(gvar);
254 o->values = g_slist_append(o->values, gvar);
255 } else if (PyLong_Check(py_item)) {
257 lval = PyLong_AsLongAndOverflow(py_item, &overflow);
259 /* Value is < LONG_MIN or > LONG_MAX */
261 srd_err("Protocol decoder %s option 'values' "
262 "has invalid value.", d->name);
263 return SRD_ERR_PYTHON;
265 gvar = g_variant_new_int64(lval);
266 g_variant_ref_sink(gvar);
267 o->values = g_slist_append(o->values, gvar);
268 } else if (PyFloat_Check(py_item)) {
270 if ((dval = PyFloat_AsDouble(py_item)) == -1.0) {
272 srd_err("Protocol decoder %s option 'default' has "
273 "invalid default value.", d->name);
274 return SRD_ERR_PYTHON;
276 gvar = g_variant_new_double(dval);
277 g_variant_ref_sink(gvar);
278 o->values = g_slist_append(o->values, gvar);
282 d->options = g_slist_append(d->options, o);
289 * Load a protocol decoder module into the embedded Python interpreter.
291 * @param module_name The module name to be loaded.
293 * @return SRD_OK upon success, a (negative) error code otherwise.
297 SRD_API int srd_decoder_load(const char *module_name)
299 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
300 PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
301 PyObject *py_ann_classes, *py_long;
302 struct srd_decoder *d;
304 char **ann, **bin, *ann_row_id, *ann_row_desc;
306 GSList *l, *ann_classes;
307 struct srd_decoder_annotation_row *ann_row;
309 if (!srd_check_init())
315 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
316 /* Module was already imported. */
320 srd_dbg("Loading protocol decoder '%s'.", module_name);
322 py_basedec = py_method = py_attr = NULL;
324 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
325 srd_dbg("Failed to g_malloc() struct srd_decoder.");
326 ret = SRD_ERR_MALLOC;
330 ret = SRD_ERR_PYTHON;
332 /* Import the Python module. */
333 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
334 srd_exception_catch("Import of '%s' failed.", module_name);
338 /* Get the 'Decoder' class as Python object. */
339 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
340 /* This generated an AttributeError exception. */
342 srd_err("Decoder class not found in protocol decoder %s.",
347 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
348 srd_dbg("sigrokdecode module not loaded.");
352 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
353 srd_err("Decoder class in protocol decoder module %s is not "
354 "a subclass of sigrokdecode.Decoder.", module_name);
357 Py_CLEAR(py_basedec);
359 /* Check for a proper start() method. */
360 if (!PyObject_HasAttrString(d->py_dec, "start")) {
361 srd_err("Protocol decoder %s has no start() method Decoder "
362 "class.", module_name);
365 py_method = PyObject_GetAttrString(d->py_dec, "start");
366 if (!PyFunction_Check(py_method)) {
367 srd_err("Protocol decoder %s Decoder class attribute 'start' "
368 "is not a method.", module_name);
373 /* Check for a proper decode() method. */
374 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
375 srd_err("Protocol decoder %s has no decode() method Decoder "
376 "class.", module_name);
379 py_method = PyObject_GetAttrString(d->py_dec, "decode");
380 if (!PyFunction_Check(py_method)) {
381 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
382 "is not a method.", module_name);
387 /* Store required fields in newly allocated strings. */
388 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
391 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
394 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
397 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
400 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
403 /* All options and their default values. */
404 if (get_options(d) != SRD_OK)
407 /* Check and import required probes. */
408 if (get_probes(d, "probes", &d->probes) != SRD_OK)
411 /* Check and import optional probes. */
412 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
416 * Fix order numbers for the optional probes.
419 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
420 * 'order' fields in the d->probes list = 0, 1, 2.
421 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
423 for (l = d->opt_probes; l; l = l->next) {
425 p->order += g_slist_length(d->probes);
428 /* Convert annotation class attribute to GSList of char **. */
429 d->annotations = NULL;
430 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
431 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
432 if (!PyTuple_Check(py_annlist)) {
433 srd_err("Protocol decoder %s annotations should "
434 "be a tuple.", module_name);
437 for (i = 0; i < PyTuple_Size(py_annlist); i++) {
438 py_ann = PyTuple_GetItem(py_annlist, i);
439 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
440 srd_err("Protocol decoder %s annotation %d should "
441 "be a tuple with two elements.", module_name, i + 1);
445 if (py_strseq_to_char(py_ann, &ann) != SRD_OK) {
448 d->annotations = g_slist_append(d->annotations, ann);
452 /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
453 d->annotation_rows = NULL;
454 if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) {
455 py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows");
456 if (!PyTuple_Check(py_ann_rows)) {
457 srd_err("Protocol decoder %s annotation row list "
458 "must be a tuple.", module_name);
461 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
462 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
463 if (!PyTuple_Check(py_ann_row)) {
464 srd_err("Protocol decoder %s annotation rows "
465 "must be tuples.", module_name);
468 if (PyTuple_Size(py_ann_row) != 3
469 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
470 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
471 || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
472 srd_err("Protocol decoder %s annotation rows "
473 "must contain tuples containing two "
474 "strings and a tuple.", module_name);
478 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK)
481 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK)
484 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
486 for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
487 py_long = PyTuple_GetItem(py_ann_classes, j);
488 if (!PyLong_Check(py_long)) {
489 srd_err("Protocol decoder %s annotation row class "
490 "list must only contain numbers.", module_name);
493 ann_classes = g_slist_append(ann_classes,
494 GINT_TO_POINTER(PyLong_AsLong(py_long)));
497 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
498 ann_row->id = ann_row_id;
499 ann_row->desc = ann_row_desc;
500 ann_row->ann_classes = ann_classes;
501 d->annotation_rows = g_slist_append(d->annotation_rows, ann_row);
505 /* Convert binary class to GSList of char *. */
507 if (PyObject_HasAttrString(d->py_dec, "binary")) {
508 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
509 if (!PyTuple_Check(py_bin_classes)) {
510 srd_err("Protocol decoder %s binary classes should "
511 "be a tuple.", module_name);
514 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
515 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
516 if (!PyTuple_Check(py_bin_class)) {
517 srd_err("Protocol decoder %s binary classes "
518 "should consist of tuples.", module_name);
521 if (PyTuple_Size(py_bin_class) != 2
522 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
523 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
524 srd_err("Protocol decoder %s binary classes should "
525 "contain tuples with two strings.", module_name);
529 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) {
532 d->binary = g_slist_append(d->binary, bin);
536 /* Append it to the list of supported/loaded decoders. */
537 pd_list = g_slist_append(pd_list, d);
543 Py_XDECREF(py_method);
544 Py_XDECREF(py_basedec);
545 Py_XDECREF(d->py_dec);
546 Py_XDECREF(d->py_mod);
554 * Return a protocol decoder's docstring.
556 * @param dec The loaded protocol decoder.
558 * @return A newly allocated buffer containing the protocol decoder's
559 * documentation. The caller is responsible for free'ing the buffer.
563 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
568 if (!srd_check_init())
574 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
577 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
578 srd_exception_catch("");
583 if (py_str != Py_None)
584 py_str_as_str(py_str, &doc);
590 static void free_probes(GSList *probelist)
595 if (probelist == NULL)
598 for (l = probelist; l; l = l->next) {
605 g_slist_free(probelist);
609 * Unload the specified protocol decoder.
611 * @param dec The struct srd_decoder to be unloaded.
613 * @return SRD_OK upon success, a (negative) error code otherwise.
617 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
619 struct srd_decoder_option *o;
620 struct srd_session *sess;
623 if (!srd_check_init())
629 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
632 * Since any instances of this decoder need to be released as well,
633 * but they could be anywhere in the stack, just free the entire
634 * stack. A frontend reloading a decoder thus has to restart all
635 * instances, and rebuild the stack.
637 for (l = sessions; l; l = l->next) {
639 srd_inst_free_all(sess, NULL);
642 for (l = dec->options; l; l = l->next) {
646 g_variant_unref(o->def);
649 g_slist_free(dec->options);
651 free_probes(dec->probes);
652 free_probes(dec->opt_probes);
655 g_free(dec->longname);
657 g_free(dec->license);
659 /* The module's Decoder class. */
660 Py_XDECREF(dec->py_dec);
661 /* The module itself. */
662 Py_XDECREF(dec->py_mod);
669 static void srd_decoder_load_all_path(char *path)
672 const gchar *direntry;
674 if (!(dir = g_dir_open(path, 0, NULL)))
675 /* Not really fatal */
678 /* This ignores errors returned by srd_decoder_load(). That
679 * function will have logged the cause, but in any case we
680 * want to continue anyway. */
681 while ((direntry = g_dir_read_name(dir)) != NULL) {
682 /* The directory name is the module name (e.g. "i2c"). */
683 srd_decoder_load(direntry);
690 * Load all installed protocol decoders.
692 * @return SRD_OK upon success, a (negative) error code otherwise.
696 SRD_API int srd_decoder_load_all(void)
700 if (!srd_check_init())
703 for (l = searchpaths; l; l = l->next)
704 srd_decoder_load_all_path(l->data);
710 * Unload all loaded protocol decoders.
712 * @return SRD_OK upon success, a (negative) error code otherwise.
716 SRD_API int srd_decoder_unload_all(void)
719 struct srd_decoder *dec;
721 for (l = pd_list; l; l = l->next) {
723 srd_decoder_unload(dec);
725 g_slist_free(pd_list);