srd: properly recurse into PD instances when looking for one.
authorBert Vermeulen <bert@biot.com>
Wed, 25 Jan 2012 00:49:35 +0000 (01:49 +0100)
committerBert Vermeulen <bert@biot.com>
Wed, 25 Jan 2012 00:49:35 +0000 (01:49 +0100)
This fixes the confusion between parallel and linear stacks.

controller.c
sigrokdecode.h
type_decoder.c

index c913fd259355d905546a26c8d6437f88a09a215d..0cad3f544fd3002fb03c0b3c0825fc7fb8d61cbd 100644 (file)
@@ -433,23 +433,27 @@ int srd_instance_stack(struct srd_decoder_instance *di_from,
                return SRD_ERR_ARG;
        }
 
-       if (!g_slist_find(di_list, di_from)) {
-               srd_err("Unstacked instance not found.");
-               return SRD_ERR_ARG;
+       if (g_slist_find(di_list, di_to)) {
+               /* Remove from the unstacked list. */
+               di_list = g_slist_remove(di_list, di_to);
        }
 
-       /* Remove from the unstacked list. */
-       di_list = g_slist_remove(di_list, di_to);
-
        /* Stack on top of source di. */
        di_from->next_di = g_slist_append(di_from->next_di, di_to);
 
        return SRD_OK;
 }
 
-
-/* TODO: this should go into the PD stack */
-struct srd_decoder_instance *srd_instance_find(char *instance_id)
+/**
+ * Finds a decoder instance by its instance id, but only in the bottom
+ * level of instances -- instances already stacked on top of another one
+ * will not be found.
+ *
+ * @param instance_id The instance id to be found.
+ *
+ * @return Pointer to struct srd_decoder_instance, or NULL if not found.
+ */
+struct srd_decoder_instance *srd_instance_find_by_id(char *instance_id)
 {
        GSList *l;
        struct srd_decoder_instance *tmp, *di;
@@ -466,9 +470,41 @@ struct srd_decoder_instance *srd_instance_find(char *instance_id)
        return di;
 }
 
+/**
+ * Finds a decoder instance by its python object, i.e. that instance's
+ * instantiation of the sigrokdecode.Decoder class. This will recurse
+ * to find the instance anywhere in the stack tree.
+ *
+ * @param stack Pointer to a GSList of struct srd_decoder_instance,
+ *             indicating the stack to search. To start searching at the bottom
+ *             level of decoder instances, pass NULL.
+ * @param obj The python class instantiation.
+ *
+ * @return Pointer to struct srd_decoder_instance, or NULL if not found.
+ */
+struct srd_decoder_instance *srd_instance_find_by_obj(GSList *stack,
+               PyObject *obj)
+{
+       GSList *l;
+       struct srd_decoder_instance *tmp, *di;
+
+       di = NULL;
+       for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) {
+               tmp = l->data;
+               if (tmp->py_instance == obj)
+                       di = tmp;
+               else if (tmp->next_di)
+                       di = srd_instance_find_by_obj(tmp->next_di, obj);
+       }
+
+       return di;
+}
+
 int srd_instance_start(struct srd_decoder_instance *di, PyObject *args)
 {
        PyObject *py_name, *py_res;
+       GSList *l;
+       struct srd_decoder_instance *next_di;
 
        srd_dbg("srd: calling start() method on protocol decoder instance %s",
                        di->instance_id);
@@ -488,6 +524,15 @@ int srd_instance_start(struct srd_decoder_instance *di, PyObject *args)
        Py_DecRef(py_res);
        Py_DecRef(py_name);
 
+       /* Start all the PDs stacked on top of this one. Pass along the
+        * metadata all the way from the bottom PD, even though it's only
+        * applicable to logic data for now.
+        */
+       for (l = di->next_di; l; l = l->next) {
+               next_di = l->data;
+               srd_instance_start(next_di, args);
+       }
+
        return SRD_OK;
 }
 
@@ -555,7 +600,7 @@ int srd_instance_decode(uint64_t start_samplenum,
 int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
 {
        PyObject *args;
-       GSList *d, *s;
+       GSList *d;
        struct srd_decoder_instance *di;
        int ret;
 
@@ -577,20 +622,12 @@ int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
                di->data_unitsize = unitsize;
                di->data_samplerate = samplerate;
                if ((ret = srd_instance_start(di, args) != SRD_OK))
-                       return ret;
-
-               /* Run the start() method on all decoders up the stack from this one. */
-               for (s = di->next_di; s; s = s->next) {
-                       /* These don't need probes, unitsize and samplerate. */
-                       di = s->data;
-                       if ((ret = srd_instance_start(di, args) != SRD_OK))
-                               return ret;
-               }
+                       break;
        }
 
        Py_DecRef(args);
 
-       return SRD_OK;
+       return ret;
 }
 
 /* Feed logic samples to decoder session. */
@@ -670,23 +707,3 @@ int pd_add(struct srd_decoder_instance *di, int output_type,
        return pdo->pdo_id;
 }
 
-struct srd_decoder_instance *get_di_by_decobject(void *decobject)
-{
-       GSList *l, *s;
-       struct srd_decoder_instance *di;
-
-       for (l = di_list; l; l = l->next) {
-               di = l->data;
-               if (decobject == di->py_instance)
-                       return di;
-               /* Check decoders stacked on top of this one. */
-               for (s = di->next_di; s; s = s->next) {
-                       di = s->data;
-                       if (decobject == di->py_instance)
-                               return di;
-               }
-       }
-
-       return NULL;
-}
-
index dca2f981aafacc4dfbacecfe67cfaf7b744d043b..eaf1e60fc159b44e311ebd52b3013d59a0e804e2 100644 (file)
@@ -186,7 +186,9 @@ struct srd_decoder_instance *srd_instance_new(const char *id,
                GHashTable *options);
 int srd_instance_stack(struct srd_decoder_instance *di_from,
                struct srd_decoder_instance *di_to);
-struct srd_decoder_instance *srd_instance_find(char *instance_id);
+struct srd_decoder_instance *srd_instance_find_by_id(char *instance_id);
+struct srd_decoder_instance *srd_instance_find_by_obj(GSList *stack,
+               PyObject *obj);
 int srd_instance_start(struct srd_decoder_instance *di, PyObject *args);
 int srd_instance_decode(uint64_t start_samplenum,
                struct srd_decoder_instance *dec, uint8_t *inbuf, uint64_t inbuflen);
index 1434cf233a9f89fdf8ff2b34c9f0e5527833f6c1..6e34dff4f3c12d84ed39691ed1cf7d4d2d9f5c02 100644 (file)
@@ -95,7 +95,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
        int output_id;
        void (*cb)();
 
-       if (!(di = get_di_by_decobject(self))) {
+       if (!(di = srd_instance_find_by_obj(NULL, self))) {
                /* Shouldn't happen. */
                srd_dbg("srd: put(): self instance not found.");
                return NULL;
@@ -114,8 +114,8 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
        }
        pdo = l->data;
 
-       srd_spew("srd: instance %s put %d-%d %s %d", di->instance_id,
-                       start_sample, end_sample, OUTPUT_TYPES[output_id], output_id);
+       srd_spew("srd: instance %s put %d-%d %s on oid %d", di->instance_id,
+                       start_sample, end_sample, OUTPUT_TYPES[pdo->output_type], output_id);
 
        if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data))))
                return NULL;
@@ -141,6 +141,8 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        next_di = l->data;
                        /* TODO: is this needed? */
                        Py_XINCREF(next_di->py_instance);
+                       srd_spew("srd: sending %d-%d to instance %s",
+                                       start_sample, end_sample, next_di->instance_id);
                        if (!(py_res = PyObject_CallMethod(next_di->py_instance, "decode",
                                        "KKO", start_sample, end_sample, data))) {
                                catch_exception("calling %s decode(): ", next_di->instance_id);
@@ -170,7 +172,7 @@ static PyObject *Decoder_add(PyObject *self, PyObject *args)
        char *proto_id;
        int output_type, pdo_id;
 
-       if (!(di = get_di_by_decobject(self))) {
+       if (!(di = srd_instance_find_by_obj(NULL, self))) {
                PyErr_SetString(PyExc_Exception, "decoder instance not found");
                return NULL;
        }