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;
45 extern GSList *sessions;
47 /* module_sigrokdecode.c */
48 extern SRD_PRIV PyObject *mod_sigrokdecode;
53 * Returns the list of supported/loaded protocol decoders.
55 * This is a GSList containing the names of the decoders as strings.
57 * @return List of decoders, NULL if none are supported or loaded.
59 * @since 0.1.0 (but the API changed in 0.2.0)
61 SRD_API const GSList *srd_decoder_list(void)
67 * Get the decoder with the specified ID.
69 * @param id The ID string of the decoder to return.
71 * @return The decoder with the specified ID, or NULL if not found.
75 SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
78 struct srd_decoder *dec;
80 for (l = pd_list; l; l = l->next) {
82 if (!strcmp(dec->id, id))
89 static int get_probes(const struct srd_decoder *d, const char *attr,
92 PyObject *py_probelist, *py_entry;
94 int ret, num_probes, i;
96 if (!PyObject_HasAttrString(d->py_dec, attr))
97 /* No probes of this type specified. */
100 ret = SRD_ERR_PYTHON;
101 py_probelist = py_entry = NULL;
103 py_probelist = PyObject_GetAttrString(d->py_dec, attr);
104 if (!PyList_Check(py_probelist)) {
105 srd_err("Protocol decoder %s %s attribute is not "
106 "a list.", d->name, attr);
110 num_probes = PyList_Size(py_probelist);
112 /* Empty probelist. */
115 for (i = 0; i < num_probes; i++) {
116 py_entry = PyList_GetItem(py_probelist, i);
117 if (!PyDict_Check(py_entry)) {
118 srd_err("Protocol decoder %s %s attribute is not "
119 "a list with dict elements.", d->name, attr);
123 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
124 srd_err("Failed to g_malloc() struct srd_probe.");
125 ret = SRD_ERR_MALLOC;
129 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK)
131 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK)
133 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK)
137 *pl = g_slist_append(*pl, p);
143 Py_DecRef(py_probelist);
148 static int get_options(struct srd_decoder *d)
150 PyObject *py_opts, *py_keys, *py_values, *py_val, *py_desc, *py_default;
152 struct srd_decoder_option *o;
154 int num_keys, overflow, ret;
157 ret = SRD_ERR_PYTHON;
160 if (!PyObject_HasAttrString(d->py_dec, "options"))
164 /* If present, options must be a dictionary. */
165 py_opts = PyObject_GetAttrString(d->py_dec, "options");
166 if (!PyDict_Check(py_opts)) {
167 srd_err("Protocol decoder %s options attribute is not "
168 "a dictionary.", d->name);
172 py_keys = PyDict_Keys(py_opts);
173 py_values = PyDict_Values(py_opts);
174 num_keys = PyList_Size(py_keys);
175 for (i = 0; i < num_keys; i++) {
176 py_str_as_str(PyList_GetItem(py_keys, i), &key);
177 srd_dbg("option '%s'", key);
178 py_val = PyList_GetItem(py_values, i);
179 if (!PyList_Check(py_val) || PyList_Size(py_val) != 2) {
180 srd_err("Protocol decoder %s option '%s' value must be "
181 "a list with two elements.", d->name, key);
184 py_desc = PyList_GetItem(py_val, 0);
185 if (!PyUnicode_Check(py_desc)) {
186 srd_err("Protocol decoder %s option '%s' has no "
187 "description.", d->name, key);
190 py_default = PyList_GetItem(py_val, 1);
191 if (!PyUnicode_Check(py_default) && !PyLong_Check(py_default)) {
192 srd_err("Protocol decoder %s option '%s' has default "
193 "of unsupported type '%s'.", d->name, key,
194 Py_TYPE(py_default)->tp_name);
197 if (!(o = g_try_malloc(sizeof(struct srd_decoder_option)))) {
198 srd_err("option malloc failed");
201 o->id = g_strdup(key);
202 py_str_as_str(py_desc, &o->desc);
203 if (PyUnicode_Check(py_default)) {
205 py_str_as_str(py_default, &def_str);
206 o->def = g_variant_new_string(def_str);
210 def_long = PyLong_AsLongAndOverflow(py_default, &overflow);
212 /* Value is < LONG_MIN or > LONG_MAX */
214 srd_err("Protocol decoder %s option '%s' has "
215 "invalid default value.", d->name, key);
218 o->def = g_variant_new_int64(def_long);
220 g_variant_ref_sink(o->def);
221 d->options = g_slist_append(d->options, o);
226 Py_DecRef(py_values);
238 * Load a protocol decoder module into the embedded Python interpreter.
240 * @param module_name The module name to be loaded.
242 * @return SRD_OK upon success, a (negative) error code otherwise.
246 SRD_API int srd_decoder_load(const char *module_name)
248 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann, \
249 *py_bin_classes, *py_bin_class;
250 struct srd_decoder *d;
256 if (!srd_check_init())
262 srd_dbg("Loading protocol decoder '%s'.", module_name);
264 py_basedec = py_method = py_attr = NULL;
266 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
267 srd_dbg("Failed to g_malloc() struct srd_decoder.");
268 ret = SRD_ERR_MALLOC;
272 ret = SRD_ERR_PYTHON;
274 /* Import the Python module. */
275 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
276 srd_exception_catch("Import of '%s' failed.", module_name);
280 /* Get the 'Decoder' class as Python object. */
281 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
282 /* This generated an AttributeError exception. */
284 srd_err("Decoder class not found in protocol decoder %s.",
289 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
290 srd_dbg("sigrokdecode module not loaded.");
294 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
295 srd_err("Decoder class in protocol decoder module %s is not "
296 "a subclass of sigrokdecode.Decoder.", module_name);
299 Py_CLEAR(py_basedec);
301 /* Check for a proper start() method. */
302 if (!PyObject_HasAttrString(d->py_dec, "start")) {
303 srd_err("Protocol decoder %s has no start() method Decoder "
304 "class.", module_name);
307 py_method = PyObject_GetAttrString(d->py_dec, "start");
308 if (!PyFunction_Check(py_method)) {
309 srd_err("Protocol decoder %s Decoder class attribute 'start' "
310 "is not a method.", module_name);
315 /* Check for a proper decode() method. */
316 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
317 srd_err("Protocol decoder %s has no decode() method Decoder "
318 "class.", module_name);
321 py_method = PyObject_GetAttrString(d->py_dec, "decode");
322 if (!PyFunction_Check(py_method)) {
323 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
324 "is not a method.", module_name);
329 if (get_options(d) != SRD_OK)
332 /* Check and import required probes. */
333 if (get_probes(d, "probes", &d->probes) != SRD_OK)
336 /* Check and import optional probes. */
337 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
341 * Fix order numbers for the optional probes.
344 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
345 * 'order' fields in the d->probes list = 0, 1, 2.
346 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
348 for (l = d->opt_probes; l; l = l->next) {
350 p->order += g_slist_length(d->probes);
353 /* Store required fields in newly allocated strings. */
354 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
357 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
360 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
363 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
366 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
369 /* Convert annotation class attribute to GSList of char **. */
370 d->annotations = NULL;
371 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
372 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
373 if (!PyList_Check(py_annlist)) {
374 srd_err("Protocol decoder module %s annotations "
375 "should be a list.", module_name);
378 len = PyList_Size(py_annlist);
379 for (i = 0; i < len; i++) {
380 py_ann = PyList_GetItem(py_annlist, i);
381 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
382 srd_err("Protocol decoder module %s "
383 "annotation %d should be a list with "
384 "two elements.", module_name, i + 1);
388 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
391 d->annotations = g_slist_append(d->annotations, ann);
395 /* Convert binary class to GSList of char *. */
397 if (PyObject_HasAttrString(d->py_dec, "binary")) {
398 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
399 if (!PyTuple_Check(py_bin_classes)) {
400 srd_err("Protocol decoder module %s binary classes "
401 "should be a tuple.", module_name);
404 len = PyTuple_Size(py_bin_classes);
405 for (i = 0; i < len; i++) {
406 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
407 if (!PyUnicode_Check(py_bin_class)) {
408 srd_err("Protocol decoder module %s binary "
409 "class should be a string.", module_name);
413 if (py_str_as_str(py_bin_class, &bin) != SRD_OK) {
416 d->binary = g_slist_append(d->binary, bin);
420 /* Append it to the list of supported/loaded decoders. */
421 pd_list = g_slist_append(pd_list, d);
427 Py_XDECREF(py_method);
428 Py_XDECREF(py_basedec);
429 Py_XDECREF(d->py_dec);
430 Py_XDECREF(d->py_mod);
438 * Return a protocol decoder's docstring.
440 * @param dec The loaded protocol decoder.
442 * @return A newly allocated buffer containing the protocol decoder's
443 * documentation. The caller is responsible for free'ing the buffer.
447 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
452 if (!srd_check_init())
458 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
461 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
462 srd_exception_catch("");
467 if (py_str != Py_None)
468 py_str_as_str(py_str, &doc);
474 static void free_probes(GSList *probelist)
479 if (probelist == NULL)
482 for (l = probelist; l; l = l->next) {
489 g_slist_free(probelist);
493 * Unload the specified protocol decoder.
495 * @param dec The struct srd_decoder to be unloaded.
497 * @return SRD_OK upon success, a (negative) error code otherwise.
501 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
503 struct srd_decoder_option *o;
504 struct srd_session *sess;
507 if (!srd_check_init())
513 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
516 * Since any instances of this decoder need to be released as well,
517 * but they could be anywhere in the stack, just free the entire
518 * stack. A frontend reloading a decoder thus has to restart all
519 * instances, and rebuild the stack.
521 for (l = sessions; l; l = l->next) {
523 srd_inst_free_all(sess, NULL);
526 for (l = dec->options; l; l = l->next) {
530 g_variant_unref(o->def);
533 g_slist_free(dec->options);
535 free_probes(dec->probes);
536 free_probes(dec->opt_probes);
539 g_free(dec->longname);
541 g_free(dec->license);
543 /* The module's Decoder class. */
544 Py_XDECREF(dec->py_dec);
545 /* The module itself. */
546 Py_XDECREF(dec->py_mod);
554 * Load all installed protocol decoders.
556 * @return SRD_OK upon success, a (negative) error code otherwise.
560 SRD_API int srd_decoder_load_all(void)
564 const gchar *direntry;
566 if (!srd_check_init())
569 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
570 srd_err("Unable to open %s for reading.", DECODERS_DIR);
571 return SRD_ERR_DECODERS_DIR;
574 while ((direntry = g_dir_read_name(dir)) != NULL) {
575 /* The directory name is the module name (e.g. "i2c"). */
576 srd_decoder_load(direntry);
584 * Unload all loaded protocol decoders.
586 * @return SRD_OK upon success, a (negative) error code otherwise.
590 SRD_API int srd_decoder_unload_all(void)
593 struct srd_decoder *dec;
595 for (l = pd_list; l; l = l->next) {
597 srd_decoder_unload(dec);