]> sigrok.org Git - libsigrokdecode.git/blobdiff - type_decoder.c
Rename ade7758 decoder to ade77xx.
[libsigrokdecode.git] / type_decoder.c
index dda0de28a2aab2fd9c6a991604eabeb031d49ed2..9ed3339d418593507b61c649f3a8f09fc4a3a127 100644 (file)
@@ -419,7 +419,7 @@ static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
                        /* Value of unused channel is 0xff, instead of 0 or 1. */
                        PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff));
                } else {
-                       sample_pos = di->inbuf + ((di->cur_samplenum - di->start_samplenum) * di->data_unitsize);
+                       sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
                        byte_offset = di->dec_channelmap[i] / 8;
                        bit_offset = di->dec_channelmap[i] % 8;
                        sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
@@ -427,8 +427,6 @@ static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
                }
        }
 
-       Py_IncRef(py_pinvalues);
-
        return py_pinvalues;
 }
 
@@ -522,6 +520,15 @@ static int set_new_condition_list(PyObject *self, PyObject *args)
                return SRD_ERR;
        }
 
+       /*
+        * Return an error condition from .wait() when termination is
+        * requested, such that decode() will terminate.
+        */
+       if (di->want_wait_terminate) {
+               srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__);
+               return SRD_ERR;
+       }
+
        /* Parse the argument of self.wait() into 'py_conds'. */
        if (!PyArg_ParseTuple(args, "O", &py_conds)) {
                /* Let Python raise this exception. */
@@ -535,12 +542,14 @@ static int set_new_condition_list(PyObject *self, PyObject *args)
                num_conditions = PyList_Size(py_conditionlist);
                if (num_conditions == 0)
                        return 9999; /* The PD invoked self.wait([]). */
+               Py_IncRef(py_conditionlist);
        } else if (PyDict_Check(py_conds)) {
                /* 'py_conds' is a dict. */
                if (PyDict_Size(py_conds) == 0)
                        return 9999; /* The PD invoked self.wait({}). */
                /* Make a list and put the dict in there for convenience. */
                py_conditionlist = PyList_New(1);
+               Py_IncRef(py_conds);
                PyList_SetItem(py_conditionlist, 0, py_conds);
                num_conditions = 1;
        } else {
@@ -593,28 +602,38 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
        }
 
        ret = set_new_condition_list(self, args);
-
+       if (ret < 0) {
+               srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
+               return NULL;
+       }
        if (ret == 9999) {
                /* Empty condition list, automatic match. */
                PyObject_SetAttrString(di->py_inst, "matched", Py_None);
-               /* Leave self.samplenum unchanged (== di->cur_samplenum). */
+               /* Leave self.samplenum unchanged (== di->abs_cur_samplenum). */
                return get_current_pinvalues(di);
        }
 
        while (1) {
-               /* Wait for new samples to process. */
+               /* Wait for new samples to process, or termination request. */
                g_mutex_lock(&di->data_mutex);
-               while (!di->got_new_samples)
+               while (!di->got_new_samples && !di->want_wait_terminate)
                        g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
 
-               /* Check whether any of the current condition(s) match. */
+               /*
+                * Check whether any of the current condition(s) match.
+                * Arrange for termination requests to take a code path which
+                * won't find new samples to process, pretends to have processed
+                * previously stored samples, and returns to the main thread,
+                * while the termination request still gets signalled.
+                */
+               found_match = FALSE;
                ret = process_samples_until_condition_match(di, &found_match);
 
                /* If there's a match, set self.samplenum etc. and return. */
                if (found_match) {
                        /* Set self.samplenum to the (absolute) sample number that matched. */
                        PyObject_SetAttrString(di->py_inst, "samplenum",
-                               PyLong_FromLong(di->cur_samplenum));
+                               PyLong_FromLong(di->abs_cur_samplenum));
 
                        if (di->match_array && di->match_array->len > 0) {
                                py_matched = PyTuple_New(di->match_array->len);
@@ -636,14 +655,25 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
                /* No match, reset state for the next chunk. */
                di->got_new_samples = FALSE;
                di->handled_all_samples = TRUE;
-               di->start_samplenum = 0;
-               di->end_samplenum = 0;
+               di->abs_start_samplenum = 0;
+               di->abs_end_samplenum = 0;
                di->inbuf = NULL;
                di->inbuflen = 0;
 
                /* Signal the main thread that we handled all samples. */
                g_cond_signal(&di->handled_all_samples_cond);
 
+               /*
+                * When termination of wait() and decode() was requested,
+                * then exit the loop after releasing the mutex.
+                */
+               if (di->want_wait_terminate) {
+                       srd_dbg("%s: %s: Will return from wait().",
+                               di->inst_id, __func__);
+                       g_mutex_unlock(&di->data_mutex);
+                       return NULL;
+               }
+
                g_mutex_unlock(&di->data_mutex);
        }