#include "config.h"
#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
+#include "sigrokdecode-internal.h"
#include <dirent.h>
/* The list of protocol decoders. */
GSList *pd_list = NULL;
-GSList *di_list = NULL;
/**
/**
- * TODO
+ * Load a protocol decoder module into the embedded python interpreter.
*
- * @param name TODO
+ * @param name The module name to be loaded.
+ * @param dec Pointer to the struct srd_decoder filled with the loaded module.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
*/
int srd_load_decoder(const char *name, struct srd_decoder **dec)
{
+ PyObject *py_mod, *py_res, *py_annlist, *py_ann;
struct srd_decoder *d;
- PyObject *py_mod, *py_res;
- int r;
-
- fprintf(stdout, "%s: %s\n", __func__, name);
+ int alen, r, i;
+ char **ann;
/* "Import" the Python module. */
if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */
if (PyErr_Occurred())
PyErr_Print(); /* Returns void. */
Py_XDECREF(py_mod);
- fprintf(stderr, "Decoder class not found in PD module %s\n", name);
+ srd_err("Decoder class not found in PD module %s", name);
return SRD_ERR_PYTHON; /* TODO: More specific error? */
}
if ((r = h_str(py_res, "name", &(d->name))) < 0)
return r;
- if ((r = h_str(py_res, "longname",
- &(d->longname))) < 0)
+ if ((r = h_str(py_res, "longname", &(d->longname))) < 0)
return r;
if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
return r;
- if ((r = h_str(py_res, "longdesc",
- &(d->longdesc))) < 0)
+ if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0)
return r;
if ((r = h_str(py_res, "author", &(d->author))) < 0)
return r;
- if ((r = h_str(py_res, "email", &(d->email))) < 0)
- return r;
-
if ((r = h_str(py_res, "license", &(d->license))) < 0)
return r;
d->inputformats = NULL;
d->outputformats = NULL;
+ /* Convert class annotation attribute to GSList of **char */
+ d->annotations = NULL;
+ if (PyObject_HasAttrString(py_res, "annotations")) {
+ py_annlist = PyObject_GetAttrString(py_res, "annotations");
+ if (!PyList_Check(py_annlist)) {
+ srd_err("Protocol decoder module %s annotations should be a list", name);
+ return SRD_ERR_PYTHON;
+ }
+ alen = PyList_Size(py_annlist);
+ for (i = 0; i < alen; i++) {
+ py_ann = PyList_GetItem(py_annlist, i);
+ if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
+ srd_err("Protocol decoder module %s annotation %d should be a list with two elements",
+ name, i+1);
+ return SRD_ERR_PYTHON;
+ }
+
+ if (py_strlist_to_char(py_ann, &ann) != SRD_OK)
+ return SRD_ERR_PYTHON;
+ d->annotations = g_slist_append(d->annotations, ann);
+ }
+ }
+
*dec = d;
return SRD_OK;
{
g_free(dec->id);
g_free(dec->name);
+ g_free(dec->longname);
g_free(dec->desc);
+ g_free(dec->longdesc);
+ g_free(dec->author);
+ g_free(dec->license);
g_free(dec->func);
/* TODO: Free everything in inputformats and outputformats. */