]> sigrok.org Git - libsigrokdecode.git/blobdiff - type_decoder.c
decoder: Accept more forms of "unconditional wait()" (None, no args)
[libsigrokdecode.git] / type_decoder.c
index 9ed3339d418593507b61c649f3a8f09fc4a3a127..231a8939b461ce6c1c09a26761f6b3e41e3aea28 100644 (file)
@@ -529,14 +529,22 @@ static int set_new_condition_list(PyObject *self, PyObject *args)
                return SRD_ERR;
        }
 
-       /* Parse the argument of self.wait() into 'py_conds'. */
-       if (!PyArg_ParseTuple(args, "O", &py_conds)) {
+       /*
+        * Parse the argument of self.wait() into 'py_conds', and check
+        * the data type. The argument is optional, None is assumed in
+        * its absence. None or an empty dict or an empty list mean that
+        * there is no condition, and the next available sample shall
+        * get returned to the caller.
+        */
+       py_conds = Py_None;
+       if (!PyArg_ParseTuple(args, "|O", &py_conds)) {
                /* Let Python raise this exception. */
                return SRD_ERR;
        }
-
-       /* Check whether 'py_conds' is a dict or a list. */
-       if (PyList_Check(py_conds)) {
+       if (py_conds == Py_None) {
+               /* 'py_conds' is None. */
+               return 9999;
+       } else if (PyList_Check(py_conds)) {
                /* 'py_conds' is a list. */
                py_conditionlist = py_conds;
                num_conditions = PyList_Size(py_conditionlist);
@@ -585,9 +593,45 @@ static int set_new_condition_list(PyObject *self, PyObject *args)
        return ret;
 }
 
+/**
+ * Create a SKIP condition list for condition-less .wait() calls.
+ *
+ * @param di Decoder instance.
+ * @param count Number of samples to skip.
+ *
+ * @retval SRD_OK The new condition list was set successfully.
+ * @retval SRD_ERR There was an error setting the new condition list.
+ *                 The contents of di->condition_list are undefined.
+ *
+ * This routine is a reduced and specialized version of the @ref
+ * set_new_condition_list() and @ref create_term_list() routines which
+ * gets invoked when .wait() was called without specifications for
+ * conditions. This minor duplication of the SKIP term list creation
+ * simplifies the logic and avoids the creation of expensive Python
+ * objects with "constant" values which the caller did not pass in the
+ * first place. It results in maximum sharing of match handling code
+ * paths.
+ */
+static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
+{
+       struct srd_term *term;
+       GSList *term_list;
+
+       condition_list_free(di);
+       term = g_malloc0(sizeof(*term));
+       term->type = SRD_TERM_SKIP;
+       term->num_samples_to_skip = count;
+       term->num_samples_already_skipped = 0;
+       term_list = g_slist_append(NULL, term);
+       di->condition_list = g_slist_append(di->condition_list, term_list);
+
+       return SRD_OK;
+}
+
 static PyObject *Decoder_wait(PyObject *self, PyObject *args)
 {
        int ret;
+       uint64_t skip_count;
        unsigned int i;
        gboolean found_match;
        struct srd_decoder_inst *di;
@@ -607,10 +651,25 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
                return NULL;
        }
        if (ret == 9999) {
-               /* Empty condition list, automatic match. */
-               PyObject_SetAttrString(di->py_inst, "matched", Py_None);
-               /* Leave self.samplenum unchanged (== di->abs_cur_samplenum). */
-               return get_current_pinvalues(di);
+               /*
+                * Empty condition list, automatic match. Arrange for the
+                * execution of regular match handling code paths such that
+                * the next available sample is returned to the caller.
+                * Make sure to skip one sample when "anywhere within the
+                * stream", yet make sure to not skip sample number 0.
+                */
+               if (di->abs_cur_samplenum)
+                       skip_count = 1;
+               else if (!di->condition_list)
+                       skip_count = 0;
+               else
+                       skip_count = 1;
+               ret = set_skip_condition(di, skip_count);
+               if (ret < 0) {
+                       srd_dbg("%s: %s: Cannot setup condition-less wait().",
+                               di->inst_id, __func__);
+                       return NULL;
+               }
        }
 
        while (1) {