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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "sigrokdecode-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 /* module_sigrokdecode.c */
46 extern SRD_PRIV PyObject *mod_sigrokdecode;
51 * Returns the list of supported/loaded protocol decoders.
53 * This is a GSList containing the names of the decoders as strings.
55 * @return List of decoders, NULL if none are supported or loaded.
57 SRD_API const GSList *srd_decoder_list(void)
63 * Get the decoder with the specified ID.
65 * @param id The ID string of the decoder to return.
67 * @return The decoder with the specified ID, or NULL if not found.
69 SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
72 struct srd_decoder *dec;
74 for (l = pd_list; l; l = l->next) {
76 if (!strcmp(dec->id, id))
83 static int get_probes(const struct srd_decoder *d, const char *attr,
86 PyObject *py_probelist, *py_entry;
88 int ret, num_probes, i;
90 if (!PyObject_HasAttrString(d->py_dec, attr))
91 /* No probes of this type specified. */
95 py_probelist = py_entry = NULL;
97 py_probelist = PyObject_GetAttrString(d->py_dec, attr);
98 if (!PyList_Check(py_probelist)) {
99 srd_err("Protocol decoder %s %s attribute is not "
100 "a list.", d->name, attr);
104 num_probes = PyList_Size(py_probelist);
106 /* Empty probelist. */
109 for (i = 0; i < num_probes; i++) {
110 py_entry = PyList_GetItem(py_probelist, i);
111 if (!PyDict_Check(py_entry)) {
112 srd_err("Protocol decoder %s %s attribute is not "
113 "a list with dict elements.", d->name, attr);
117 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
118 srd_err("Failed to g_malloc() struct srd_probe.");
119 ret = SRD_ERR_MALLOC;
123 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK)
125 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK)
127 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK)
131 *pl = g_slist_append(*pl, p);
137 Py_DecRef(py_probelist);
142 static int get_options(struct srd_decoder *d)
144 PyObject *py_opts, *py_keys, *py_values, *py_val, *py_desc, *py_default;
146 struct srd_decoder_option *o;
148 int num_keys, overflow, ret;
151 ret = SRD_ERR_PYTHON;
154 if (!PyObject_HasAttrString(d->py_dec, "options"))
158 /* If present, options must be a dictionary. */
159 py_opts = PyObject_GetAttrString(d->py_dec, "options");
160 if (!PyDict_Check(py_opts)) {
161 srd_err("Protocol decoder %s options attribute is not "
162 "a dictionary.", d->name);
166 py_keys = PyDict_Keys(py_opts);
167 py_values = PyDict_Values(py_opts);
168 num_keys = PyList_Size(py_keys);
169 for (i = 0; i < num_keys; i++) {
170 py_str_as_str(PyList_GetItem(py_keys, i), &key);
171 srd_dbg("option '%s'", key);
172 py_val = PyList_GetItem(py_values, i);
173 if (!PyList_Check(py_val) || PyList_Size(py_val) != 2) {
174 srd_err("Protocol decoder %s option '%s' value must be "
175 "a list with two elements.", d->name, key);
178 py_desc = PyList_GetItem(py_val, 0);
179 if (!PyUnicode_Check(py_desc)) {
180 srd_err("Protocol decoder %s option '%s' has no "
181 "description.", d->name, key);
184 py_default = PyList_GetItem(py_val, 1);
185 if (!PyUnicode_Check(py_default) && !PyLong_Check(py_default)) {
186 srd_err("Protocol decoder %s option '%s' has default "
187 "of unsupported type '%s'.", d->name, key,
188 Py_TYPE(py_default)->tp_name);
191 if (!(o = g_try_malloc(sizeof(struct srd_decoder_option)))) {
192 srd_err("option malloc failed");
195 o->id = g_strdup(key);
196 py_str_as_str(py_desc, &o->desc);
197 if (PyUnicode_Check(py_default)) {
199 py_str_as_str(py_default, &def_str);
200 o->def = g_variant_new_string(def_str);
204 def_long = PyLong_AsLongAndOverflow(py_default, &overflow);
206 /* Value is < LONG_MIN or > LONG_MAX */
208 srd_err("Protocol decoder %s option '%s' has "
209 "invalid default value.", d->name, key);
212 o->def = g_variant_new_int64(def_long);
214 g_variant_ref_sink(o->def);
215 d->options = g_slist_append(d->options, o);
218 Py_DecRef(py_values);
230 * Load a protocol decoder module into the embedded Python interpreter.
232 * @param module_name The module name to be loaded.
234 * @return SRD_OK upon success, a (negative) error code otherwise.
236 SRD_API int srd_decoder_load(const char *module_name)
238 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
239 struct srd_decoder *d;
245 srd_dbg("Loading protocol decoder '%s'.", module_name);
247 py_basedec = py_method = py_attr = NULL;
249 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
250 srd_dbg("Failed to g_malloc() struct srd_decoder.");
251 ret = SRD_ERR_MALLOC;
255 ret = SRD_ERR_PYTHON;
257 /* Import the Python module. */
258 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
259 srd_exception_catch("Import of '%s' failed.", module_name);
263 /* Get the 'Decoder' class as Python object. */
264 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
265 /* This generated an AttributeError exception. */
267 srd_err("Decoder class not found in protocol decoder %s.",
272 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
273 srd_dbg("sigrokdecode module not loaded.");
277 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
278 srd_err("Decoder class in protocol decoder module %s is not "
279 "a subclass of sigrokdecode.Decoder.", module_name);
282 Py_CLEAR(py_basedec);
284 /* Check for a proper start() method. */
285 if (!PyObject_HasAttrString(d->py_dec, "start")) {
286 srd_err("Protocol decoder %s has no start() method Decoder "
287 "class.", module_name);
290 py_method = PyObject_GetAttrString(d->py_dec, "start");
291 if (!PyFunction_Check(py_method)) {
292 srd_err("Protocol decoder %s Decoder class attribute 'start' "
293 "is not a method.", module_name);
298 /* Check for a proper decode() method. */
299 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
300 srd_err("Protocol decoder %s has no decode() method Decoder "
301 "class.", module_name);
304 py_method = PyObject_GetAttrString(d->py_dec, "decode");
305 if (!PyFunction_Check(py_method)) {
306 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
307 "is not a method.", module_name);
312 if (get_options(d) != SRD_OK)
315 /* Check and import required probes. */
316 if (get_probes(d, "probes", &d->probes) != SRD_OK)
319 /* Check and import optional probes. */
320 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
324 * Fix order numbers for the optional probes.
327 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
328 * 'order' fields in the d->probes list = 0, 1, 2.
329 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
331 for (l = d->opt_probes; l; l = l->next) {
333 p->order += g_slist_length(d->probes);
336 /* Store required fields in newly allocated strings. */
337 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
340 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
343 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
346 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
349 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
352 /* Convert class annotation attribute to GSList of **char. */
353 d->annotations = NULL;
354 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
355 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
356 if (!PyList_Check(py_annlist)) {
357 srd_err("Protocol decoder module %s annotations "
358 "should be a list.", module_name);
361 alen = PyList_Size(py_annlist);
362 for (i = 0; i < alen; i++) {
363 py_ann = PyList_GetItem(py_annlist, i);
364 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
365 srd_err("Protocol decoder module %s "
366 "annotation %d should be a list with "
367 "two elements.", module_name, i + 1);
371 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
374 d->annotations = g_slist_append(d->annotations, ann);
378 /* Append it to the list of supported/loaded decoders. */
379 pd_list = g_slist_append(pd_list, d);
385 Py_XDECREF(py_method);
386 Py_XDECREF(py_basedec);
387 Py_XDECREF(d->py_dec);
388 Py_XDECREF(d->py_mod);
396 * Return a protocol decoder's docstring.
398 * @param dec The loaded protocol decoder.
400 * @return A newly allocated buffer containing the protocol decoder's
401 * documentation. The caller is responsible for free'ing the buffer.
403 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
408 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
411 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
412 srd_exception_catch("");
417 if (py_str != Py_None)
418 py_str_as_str(py_str, &doc);
424 static void free_probes(GSList *probelist)
429 if (probelist == NULL)
432 for (l = probelist; l; l = l->next) {
439 g_slist_free(probelist);
443 * Unload decoder module.
445 * @param dec The struct srd_decoder to be unloaded.
447 * @return SRD_OK upon success, a (negative) error code otherwise.
449 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
451 struct srd_decoder_option *o;
454 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
457 * Since any instances of this decoder need to be released as well,
458 * but they could be anywhere in the stack, just free the entire
459 * stack. A frontend reloading a decoder thus has to restart all
460 * instances, and rebuild the stack.
462 srd_inst_free_all(NULL);
464 for (l = dec->options; l; l = l->next) {
468 g_variant_unref(o->def);
471 g_slist_free(dec->options);
473 free_probes(dec->probes);
474 free_probes(dec->opt_probes);
477 g_free(dec->longname);
479 g_free(dec->license);
481 /* The module's Decoder class. */
482 Py_XDECREF(dec->py_dec);
483 /* The module itself. */
484 Py_XDECREF(dec->py_mod);
486 /* TODO: (g_)free dec itself? */
492 * Load all installed protocol decoders.
494 * @return SRD_OK upon success, a (negative) error code otherwise.
496 SRD_API int srd_decoder_load_all(void)
500 const gchar *direntry;
502 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
503 srd_err("Unable to open %s for reading.", DECODERS_DIR);
504 return SRD_ERR_DECODERS_DIR;
507 while ((direntry = g_dir_read_name(dir)) != NULL) {
508 /* The directory name is the module name (e.g. "i2c"). */
509 srd_decoder_load(direntry);
517 * Unload all loaded protocol decoders.
519 * @return SRD_OK upon success, a (negative) error code otherwise.
521 SRD_API int srd_decoder_unload_all(void)
524 struct srd_decoder *dec;
526 for (l = pd_list; l; l = l->next) {
528 srd_decoder_unload(dec);