]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoder.c
srd: change output_new() API call to add()
[libsigrokdecode.git] / decoder.c
index e69eae8da694ff77ef2e2dec7fecbdd70866c818..f06d5e9e5a2efae5351c6af8fc695fd6fa2c4b11 100644 (file)
--- a/decoder.c
+++ b/decoder.c
  */
 
 #include "config.h"
  */
 
 #include "config.h"
-#include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
+#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
+#include "sigrokdecode-internal.h"
 #include <dirent.h>
 
 /* The list of protocol decoders. */
 #include <dirent.h>
 
 /* The list of protocol decoders. */
-GSList *list_pds = NULL;
-GSList *decoders = NULL;
+GSList *pd_list = NULL;
+GSList *di_list = NULL;
 
 
 /**
 
 
 /**
@@ -37,7 +38,7 @@ GSList *decoders = NULL;
 GSList *srd_list_decoders(void)
 {
 
 GSList *srd_list_decoders(void)
 {
 
-       return list_pds;
+       return pd_list;
 }
 
 
 }
 
 
@@ -63,17 +64,19 @@ struct srd_decoder *srd_get_decoder_by_id(const char *id)
 
 
 /**
 
 
 /**
- * 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)
 {
  *
  * @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;
        struct srd_decoder *d;
-       PyObject *py_mod, *py_res;
-       int r;
+       int alen, r, i;
+       char **ann;
 
        fprintf(stdout, "%s: %s\n", __func__, name);
 
 
        fprintf(stdout, "%s: %s\n", __func__, name);
 
@@ -96,30 +99,28 @@ int srd_load_decoder(const char *name, struct srd_decoder **dec)
        if (!(d = malloc(sizeof(struct srd_decoder))))
                return SRD_ERR_MALLOC;
 
        if (!(d = malloc(sizeof(struct srd_decoder))))
                return SRD_ERR_MALLOC;
 
-       if ((r = h_str(py_res, py_mod, "id", &(d->id))) < 0)
+       if ((r = h_str(py_res, "id", &(d->id))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "name", &(d->name))) < 0)
+       if ((r = h_str(py_res, "name", &(d->name))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "longname",
-                      &(d->longname))) < 0)
+       if ((r = h_str(py_res, "longname", &(d->longname))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "desc", &(d->desc))) < 0)
+       if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "longdesc",
-                      &(d->longdesc))) < 0)
+       if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "author", &(d->author))) < 0)
+       if ((r = h_str(py_res, "author", &(d->author))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "email", &(d->email))) < 0)
+       if ((r = h_str(py_res, "email", &(d->email))) < 0)
                return r;
 
                return r;
 
-       if ((r = h_str(py_res, py_mod, "license", &(d->license))) < 0)
+       if ((r = h_str(py_res, "license", &(d->license))) < 0)
                return r;
 
        d->py_mod = py_mod;
                return r;
 
        d->py_mod = py_mod;
@@ -131,6 +132,29 @@ int srd_load_decoder(const char *name, struct srd_decoder **dec)
        d->inputformats = NULL;
        d->outputformats = NULL;
 
        d->inputformats = NULL;
        d->outputformats = NULL;
 
+       /* Convert class annotation attribute to GSList of **char */
+       d->annotation = NULL;
+       if (PyObject_HasAttrString(py_res, "annotation")) {
+               py_annlist = PyObject_GetAttrString(py_res, "annotation");
+               if (!PyList_Check(py_annlist)) {
+                       srd_err("Protocol decoder module %s annotation 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->annotation = g_slist_append(d->annotation, ann);
+               }
+       }
+
        *dec = d;
 
        return SRD_OK;
        *dec = d;
 
        return SRD_OK;
@@ -194,7 +218,7 @@ int srd_load_all_decoders(void)
                /* TODO: Warning if loading fails for a decoder. */
                if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
                        /* Append it to the list of supported/loaded decoders. */
                /* TODO: Warning if loading fails for a decoder. */
                if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
                        /* Append it to the list of supported/loaded decoders. */
-                       list_pds = g_slist_append(list_pds, dec);
+                       pd_list = g_slist_append(pd_list, dec);
                }
        }
        closedir(dir);
                }
        }
        closedir(dir);