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 (!PyList_Check(py_probelist)) {
106 srd_err("Protocol decoder %s %s attribute is not a list.",
108 return SRD_ERR_PYTHON;
111 if ((num_probes = PyList_Size(py_probelist)) == 0)
112 /* Empty probelist. */
116 for (i = 0; i < num_probes; i++) {
117 py_entry = PyList_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_keys, *py_values, *py_val, *py_desc, *py_default;
157 struct srd_decoder_option *o;
159 int num_keys, overflow, ret;
162 ret = SRD_ERR_PYTHON;
165 if (!PyObject_HasAttrString(d->py_dec, "options"))
169 /* If present, options must be a dictionary. */
170 py_opts = PyObject_GetAttrString(d->py_dec, "options");
171 if (!PyDict_Check(py_opts)) {
172 srd_err("Protocol decoder %s options attribute is not "
173 "a dictionary.", d->name);
177 py_keys = PyDict_Keys(py_opts);
178 py_values = PyDict_Values(py_opts);
179 num_keys = PyList_Size(py_keys);
180 for (i = 0; i < num_keys; i++) {
181 py_str_as_str(PyList_GetItem(py_keys, i), &key);
182 srd_dbg("option '%s'", key);
183 py_val = PyList_GetItem(py_values, i);
184 if (!PyList_Check(py_val) || PyList_Size(py_val) != 2) {
185 srd_err("Protocol decoder %s option '%s' value must be "
186 "a list with two elements.", d->name, key);
189 py_desc = PyList_GetItem(py_val, 0);
190 if (!PyUnicode_Check(py_desc)) {
191 srd_err("Protocol decoder %s option '%s' has no "
192 "description.", d->name, key);
195 py_default = PyList_GetItem(py_val, 1);
196 if (!PyUnicode_Check(py_default) && !PyLong_Check(py_default)) {
197 srd_err("Protocol decoder %s option '%s' has default "
198 "of unsupported type '%s'.", d->name, key,
199 Py_TYPE(py_default)->tp_name);
202 if (!(o = g_try_malloc(sizeof(struct srd_decoder_option)))) {
203 srd_err("option malloc failed");
206 o->id = g_strdup(key);
207 py_str_as_str(py_desc, &o->desc);
208 if (PyUnicode_Check(py_default)) {
210 py_str_as_str(py_default, &def_str);
211 o->def = g_variant_new_string(def_str);
215 def_long = PyLong_AsLongAndOverflow(py_default, &overflow);
217 /* Value is < LONG_MIN or > LONG_MAX */
219 srd_err("Protocol decoder %s option '%s' has "
220 "invalid default value.", d->name, key);
223 o->def = g_variant_new_int64(def_long);
225 g_variant_ref_sink(o->def);
226 d->options = g_slist_append(d->options, o);
231 Py_DecRef(py_values);
243 * Load a protocol decoder module into the embedded Python interpreter.
245 * @param module_name The module name to be loaded.
247 * @return SRD_OK upon success, a (negative) error code otherwise.
251 SRD_API int srd_decoder_load(const char *module_name)
253 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
254 PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
255 PyObject *py_ann_classes, *py_long;
256 struct srd_decoder *d;
258 char **ann, **bin, *ann_row_id, *ann_row_desc;
260 GSList *l, *ann_classes;
261 struct srd_decoder_annotation_row *ann_row;
263 if (!srd_check_init())
269 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
270 /* Module was already imported. */
274 srd_dbg("Loading protocol decoder '%s'.", module_name);
276 py_basedec = py_method = py_attr = NULL;
278 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
279 srd_dbg("Failed to g_malloc() struct srd_decoder.");
280 ret = SRD_ERR_MALLOC;
284 ret = SRD_ERR_PYTHON;
286 /* Import the Python module. */
287 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
288 srd_exception_catch("Import of '%s' failed.", module_name);
292 /* Get the 'Decoder' class as Python object. */
293 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
294 /* This generated an AttributeError exception. */
296 srd_err("Decoder class not found in protocol decoder %s.",
301 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
302 srd_dbg("sigrokdecode module not loaded.");
306 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
307 srd_err("Decoder class in protocol decoder module %s is not "
308 "a subclass of sigrokdecode.Decoder.", module_name);
311 Py_CLEAR(py_basedec);
313 /* Check for a proper start() method. */
314 if (!PyObject_HasAttrString(d->py_dec, "start")) {
315 srd_err("Protocol decoder %s has no start() method Decoder "
316 "class.", module_name);
319 py_method = PyObject_GetAttrString(d->py_dec, "start");
320 if (!PyFunction_Check(py_method)) {
321 srd_err("Protocol decoder %s Decoder class attribute 'start' "
322 "is not a method.", module_name);
327 /* Check for a proper decode() method. */
328 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
329 srd_err("Protocol decoder %s has no decode() method Decoder "
330 "class.", module_name);
333 py_method = PyObject_GetAttrString(d->py_dec, "decode");
334 if (!PyFunction_Check(py_method)) {
335 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
336 "is not a method.", module_name);
341 if (get_options(d) != SRD_OK)
344 /* Check and import required probes. */
345 if (get_probes(d, "probes", &d->probes) != SRD_OK)
348 /* Check and import optional probes. */
349 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
353 * Fix order numbers for the optional probes.
356 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
357 * 'order' fields in the d->probes list = 0, 1, 2.
358 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
360 for (l = d->opt_probes; l; l = l->next) {
362 p->order += g_slist_length(d->probes);
365 /* Store required fields in newly allocated strings. */
366 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
369 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
372 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
375 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
378 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
381 /* Convert annotation class attribute to GSList of char **. */
382 d->annotations = NULL;
383 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
384 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
385 if (!PyList_Check(py_annlist)) {
386 srd_err("Protocol decoder %s annotations should "
387 "be a list.", module_name);
390 for (i = 0; i < PyList_Size(py_annlist); i++) {
391 py_ann = PyList_GetItem(py_annlist, i);
392 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
393 srd_err("Protocol decoder %s annotation %d should "
394 "be a list with two elements.", module_name, i + 1);
398 if (py_strseq_to_char(py_ann, &ann) != SRD_OK) {
401 d->annotations = g_slist_append(d->annotations, ann);
405 /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
406 d->annotation_rows = NULL;
407 if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) {
408 py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows");
409 if (!PyTuple_Check(py_ann_rows)) {
410 srd_err("Protocol decoder %s annotation row list "
411 "must be a tuple.", module_name);
414 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
415 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
416 if (!PyTuple_Check(py_ann_row)) {
417 srd_err("Protocol decoder %s annotation rows "
418 "must be tuples.", module_name);
421 if (PyTuple_Size(py_ann_row) != 3
422 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
423 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
424 || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
425 srd_err("Protocol decoder %s annotation rows "
426 "must contain tuples containing two "
427 "strings and a tuple.", module_name);
431 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK)
434 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK)
437 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
439 for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
440 py_long = PyTuple_GetItem(py_ann_classes, j);
441 if (!PyLong_Check(py_long)) {
442 srd_err("Protocol decoder %s annotation row class "
443 "list must only contain numbers.", module_name);
446 ann_classes = g_slist_append(ann_classes,
447 GINT_TO_POINTER(PyLong_AsLong(py_long)));
450 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
451 ann_row->id = ann_row_id;
452 ann_row->desc = ann_row_desc;
453 ann_row->ann_classes = ann_classes;
454 d->annotation_rows = g_slist_append(d->annotation_rows, ann_row);
458 /* Convert binary class to GSList of char *. */
460 if (PyObject_HasAttrString(d->py_dec, "binary")) {
461 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
462 if (!PyTuple_Check(py_bin_classes)) {
463 srd_err("Protocol decoder %s binary classes should "
464 "be a tuple.", module_name);
467 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
468 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
469 if (!PyTuple_Check(py_bin_class)) {
470 srd_err("Protocol decoder %s binary classes "
471 "should consist of tuples.", module_name);
474 if (PyTuple_Size(py_bin_class) != 2
475 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
476 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
477 srd_err("Protocol decoder %s binary classes should "
478 "contain tuples with two strings.", module_name);
482 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) {
485 d->binary = g_slist_append(d->binary, bin);
489 /* Append it to the list of supported/loaded decoders. */
490 pd_list = g_slist_append(pd_list, d);
496 Py_XDECREF(py_method);
497 Py_XDECREF(py_basedec);
498 Py_XDECREF(d->py_dec);
499 Py_XDECREF(d->py_mod);
507 * Return a protocol decoder's docstring.
509 * @param dec The loaded protocol decoder.
511 * @return A newly allocated buffer containing the protocol decoder's
512 * documentation. The caller is responsible for free'ing the buffer.
516 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
521 if (!srd_check_init())
527 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
530 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
531 srd_exception_catch("");
536 if (py_str != Py_None)
537 py_str_as_str(py_str, &doc);
543 static void free_probes(GSList *probelist)
548 if (probelist == NULL)
551 for (l = probelist; l; l = l->next) {
558 g_slist_free(probelist);
562 * Unload the specified protocol decoder.
564 * @param dec The struct srd_decoder to be unloaded.
566 * @return SRD_OK upon success, a (negative) error code otherwise.
570 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
572 struct srd_decoder_option *o;
573 struct srd_session *sess;
576 if (!srd_check_init())
582 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
585 * Since any instances of this decoder need to be released as well,
586 * but they could be anywhere in the stack, just free the entire
587 * stack. A frontend reloading a decoder thus has to restart all
588 * instances, and rebuild the stack.
590 for (l = sessions; l; l = l->next) {
592 srd_inst_free_all(sess, NULL);
595 for (l = dec->options; l; l = l->next) {
599 g_variant_unref(o->def);
602 g_slist_free(dec->options);
604 free_probes(dec->probes);
605 free_probes(dec->opt_probes);
608 g_free(dec->longname);
610 g_free(dec->license);
612 /* The module's Decoder class. */
613 Py_XDECREF(dec->py_dec);
614 /* The module itself. */
615 Py_XDECREF(dec->py_mod);
622 static void srd_decoder_load_all_path(char *path)
625 const gchar *direntry;
627 if (!(dir = g_dir_open(path, 0, NULL)))
628 /* Not really fatal */
631 /* This ignores errors returned by srd_decoder_load(). That
632 * function will have logged the cause, but in any case we
633 * want to continue anyway. */
634 while ((direntry = g_dir_read_name(dir)) != NULL) {
635 /* The directory name is the module name (e.g. "i2c"). */
636 srd_decoder_load(direntry);
643 * Load all installed protocol decoders.
645 * @return SRD_OK upon success, a (negative) error code otherwise.
649 SRD_API int srd_decoder_load_all(void)
653 if (!srd_check_init())
656 for (l = searchpaths; l; l = l->next)
657 srd_decoder_load_all_path(l->data);
663 * Unload all loaded protocol decoders.
665 * @return SRD_OK upon success, a (negative) error code otherwise.
669 SRD_API int srd_decoder_unload_all(void)
672 struct srd_decoder *dec;
674 for (l = pd_list; l; l = l->next) {
676 srd_decoder_unload(dec);
678 g_slist_free(pd_list);