]> sigrok.org Git - libsigrokdecode.git/commitdiff
Clarify that {start,end,cur}_samplenum are absolute numbers.
authorUwe Hermann <redacted>
Tue, 21 Feb 2017 21:10:35 +0000 (22:10 +0100)
committerUwe Hermann <redacted>
Tue, 21 Feb 2017 22:37:30 +0000 (23:37 +0100)
instance.c
libsigrokdecode-internal.h
libsigrokdecode.h
session.c
type_decoder.c
type_logic.c

index 00bc8023841334b54a0761cbccaaae82dd4eb0c5..1352975b81f7c9ea84347db80f17d8a928c6abcd 100644 (file)
@@ -367,11 +367,11 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
 
        di->condition_list = NULL;
        di->match_array = NULL;
 
        di->condition_list = NULL;
        di->match_array = NULL;
-       di->start_samplenum = 0;
-       di->end_samplenum = 0;
+       di->abs_start_samplenum = 0;
+       di->abs_end_samplenum = 0;
        di->inbuf = NULL;
        di->inbuflen = 0;
        di->inbuf = NULL;
        di->inbuflen = 0;
-       di->cur_samplenum = 0;
+       di->abs_cur_samplenum = 0;
        di->old_pins_array = NULL;
        di->thread_handle = NULL;
        di->got_new_samples = FALSE;
        di->old_pins_array = NULL;
        di->thread_handle = NULL;
        di->got_new_samples = FALSE;
@@ -829,16 +829,16 @@ static gboolean find_match(struct srd_decoder_inst *di)
                return TRUE;
        }
 
                return TRUE;
        }
 
-       num_samples_to_process = di->end_samplenum - di->cur_samplenum;
+       num_samples_to_process = di->abs_end_samplenum - di->abs_cur_samplenum;
        num_conditions = g_slist_length(di->condition_list);
 
        /* di->match_array is NULL here. Create a new GArray. */
        di->match_array = g_array_sized_new(FALSE, TRUE, sizeof(gboolean), num_conditions);
        g_array_set_size(di->match_array, num_conditions);
 
        num_conditions = g_slist_length(di->condition_list);
 
        /* di->match_array is NULL here. Create a new GArray. */
        di->match_array = g_array_sized_new(FALSE, TRUE, sizeof(gboolean), num_conditions);
        g_array_set_size(di->match_array, num_conditions);
 
-       for (i = 0, s = 0; i < num_samples_to_process; i++, s++, (di->cur_samplenum)++) {
+       for (i = 0, s = 0; i < num_samples_to_process; i++, s++, (di->abs_cur_samplenum)++) {
 
 
-               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);
 
                /* Check whether the current sample matches at least one of the conditions (logical OR). */
                /* IMPORTANT: We need to check all conditions, even if there was a match already! */
 
                /* Check whether the current sample matches at least one of the conditions (logical OR). */
                /* IMPORTANT: We need to check all conditions, even if there was a match already! */
@@ -886,9 +886,10 @@ SRD_PRIV int process_samples_until_condition_match(struct srd_decoder_inst *di,
                *found_match = find_match(di);
 
                /* Did we handle all samples yet? */
                *found_match = find_match(di);
 
                /* Did we handle all samples yet? */
-               if (di->cur_samplenum >= di->end_samplenum) {
-                       srd_dbg("Done, handled all samples (%" PRIu64 "/%" PRIu64 ").",
-                               di->cur_samplenum, di->end_samplenum);
+               if (di->abs_cur_samplenum >= di->abs_end_samplenum) {
+                       srd_dbg("Done, handled all samples (abs cur %" PRIu64
+                               " / abs end %" PRIu64 ").",
+                               di->abs_cur_samplenum, di->abs_end_samplenum);
                        return SRD_OK;
                }
 
                        return SRD_OK;
                }
 
@@ -936,11 +937,45 @@ static gpointer di_thread(gpointer data)
 /**
  * Decode a chunk of samples.
  *
 /**
  * Decode a chunk of samples.
  *
+ * The calls to this function must provide the samples that shall be
+ * used by the protocol decoder
+ *  - in the correct order ([...]5, 6, 4, 7, 8[...] is a bug),
+ *  - starting from sample zero (2, 3, 4, 5, 6[...] is a bug),
+ *  - consecutively, with no gaps (0, 1, 2, 4, 5[...] is a bug).
+ *
+ * The start- and end-sample numbers are absolute sample numbers (relative
+ * to the start of the whole capture/file/stream), i.e. they are not relative
+ * sample numbers within the chunk specified by 'inbuf' and 'inbuflen'.
+ *
+ * Correct example (4096 samples total, 4 chunks @ 1024 samples each):
+ *   srd_inst_decode(di, 0,    1023, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 1024, 2047, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 2048, 3071, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 3072, 4095, inbuf, 1024, 1);
+ *
+ * The chunk size ('inbuflen') can be arbitrary and can differ between calls.
+ *
+ * Correct example (4096 samples total, 7 chunks @ various samples each):
+ *   srd_inst_decode(di, 0,    1023, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 1024, 1123, inbuf,  100, 1);
+ *   srd_inst_decode(di, 1124, 1423, inbuf,  300, 1);
+ *   srd_inst_decode(di, 1424, 1642, inbuf,  219, 1);
+ *   srd_inst_decode(di, 1643, 2047, inbuf,  405, 1);
+ *   srd_inst_decode(di, 2048, 3071, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 3072, 4095, inbuf, 1024, 1);
+ *
+ * INCORRECT example (4096 samples total, 4 chunks @ 1024 samples each, but
+ * the start- and end-samplenumbers are not absolute):
+ *   srd_inst_decode(di, 0,    1023, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 0,    1023, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 0,    1023, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 0,    1023, inbuf, 1024, 1);
+ *
  * @param di The decoder instance to call. Must not be NULL.
  * @param di The decoder instance to call. Must not be NULL.
- * @param start_samplenum The starting sample number for the buffer's sample
- *                       set, relative to the start of capture.
- * @param end_samplenum The ending sample number for the buffer's sample
- *                       set, relative to the start of capture.
+ * @param abs_start_samplenum The absolute starting sample number for the
+ *             buffer's sample set, relative to the start of capture.
+ * @param abs_end_samplenum The absolute ending sample number for the
+ *             buffer's sample set, relative to the start of capture.
  * @param inbuf The buffer to decode. Must not be NULL.
  * @param inbuflen Length of the buffer. Must be > 0.
  * @param unitsize The number of bytes per sample. Must be > 0.
  * @param inbuf The buffer to decode. Must not be NULL.
  * @param inbuflen Length of the buffer. Must be > 0.
  * @param unitsize The number of bytes per sample. Must be > 0.
@@ -950,7 +985,7 @@ static gpointer di_thread(gpointer data)
  * @private
  */
 SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
  * @private
  */
 SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
-               uint64_t start_samplenum, uint64_t end_samplenum,
+               uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
 {
        PyObject *py_res;
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
 {
        PyObject *py_res;
@@ -977,10 +1012,10 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
 
        di->data_unitsize = unitsize;
 
 
        di->data_unitsize = unitsize;
 
-       srd_dbg("Decoding: start sample %" PRIu64 ", end sample %"
+       srd_dbg("Decoding: abs start sample %" PRIu64 ", abs end sample %"
                PRIu64 " (%" PRIu64 " samples, %" PRIu64 " bytes, unitsize = "
                PRIu64 " (%" PRIu64 " samples, %" PRIu64 " bytes, unitsize = "
-               "%d), instance %s.", start_samplenum, end_samplenum,
-               end_samplenum - start_samplenum, inbuflen, di->data_unitsize,
+               "%d), instance %s.", abs_start_samplenum, abs_end_samplenum,
+               abs_end_samplenum - abs_start_samplenum, inbuflen, di->data_unitsize,
                di->inst_id);
 
        apiver = srd_decoder_apiver(di->decoder);
                di->inst_id);
 
        apiver = srd_decoder_apiver(di->decoder);
@@ -993,7 +1028,7 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
                logic = PyObject_New(srd_logic, (PyTypeObject *)srd_logic_type);
                Py_INCREF(logic);
                logic->di = (struct srd_decoder_inst *)di;
                logic = PyObject_New(srd_logic, (PyTypeObject *)srd_logic_type);
                Py_INCREF(logic);
                logic->di = (struct srd_decoder_inst *)di;
-               logic->start_samplenum = start_samplenum;
+               logic->abs_start_samplenum = abs_start_samplenum;
                logic->itercnt = 0;
                logic->inbuf = (uint8_t *)inbuf;
                logic->inbuflen = inbuflen;
                logic->itercnt = 0;
                logic->inbuf = (uint8_t *)inbuf;
                logic->inbuflen = inbuflen;
@@ -1002,7 +1037,7 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
 
                Py_IncRef(di->py_inst);
                if (!(py_res = PyObject_CallMethod(di->py_inst, "decode",
 
                Py_IncRef(di->py_inst);
                if (!(py_res = PyObject_CallMethod(di->py_inst, "decode",
-                       "KKO", start_samplenum, end_samplenum, logic))) {
+                       "KKO", abs_start_samplenum, abs_end_samplenum, logic))) {
                        srd_exception_catch("Protocol decoder instance %s",
                                        di->inst_id);
                        return SRD_ERR_PYTHON;
                        srd_exception_catch("Protocol decoder instance %s",
                                        di->inst_id);
                        return SRD_ERR_PYTHON;
@@ -1016,8 +1051,8 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
 
                /* Push the new sample chunk to the worker thread. */
                g_mutex_lock(&di->data_mutex);
 
                /* Push the new sample chunk to the worker thread. */
                g_mutex_lock(&di->data_mutex);
-               di->start_samplenum = start_samplenum;
-               di->end_samplenum = end_samplenum;
+               di->abs_start_samplenum = abs_start_samplenum;
+               di->abs_end_samplenum = abs_end_samplenum;
                di->inbuf = inbuf;
                di->inbuflen = inbuflen;
                di->got_new_samples = TRUE;
                di->inbuf = inbuf;
                di->inbuflen = inbuflen;
                di->got_new_samples = TRUE;
index 26093884111b40b70e7d7c23ed685ff149142ad3..c12b877701d750942060de523229ecd0fff29e8f 100644 (file)
@@ -49,7 +49,7 @@ struct srd_term {
 typedef struct {
        PyObject_HEAD
        struct srd_decoder_inst *di;
 typedef struct {
        PyObject_HEAD
        struct srd_decoder_inst *di;
-       uint64_t start_samplenum;
+       uint64_t abs_start_samplenum;
        unsigned int itercnt;
        uint8_t *inbuf;
        uint64_t inbuflen;
        unsigned int itercnt;
        uint8_t *inbuf;
        uint64_t inbuflen;
@@ -81,7 +81,7 @@ SRD_PRIV int srd_inst_start(struct srd_decoder_inst *di);
 SRD_PRIV void match_array_free(struct srd_decoder_inst *di);
 SRD_PRIV void condition_list_free(struct srd_decoder_inst *di);
 SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
 SRD_PRIV void match_array_free(struct srd_decoder_inst *di);
 SRD_PRIV void condition_list_free(struct srd_decoder_inst *di);
 SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
-               uint64_t start_samplenum, uint64_t end_samplenum,
+               uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize);
 SRD_PRIV int process_samples_until_condition_match(struct srd_decoder_inst *di, gboolean *found_match);
 SRD_PRIV void srd_inst_free(struct srd_decoder_inst *di);
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize);
 SRD_PRIV int process_samples_until_condition_match(struct srd_decoder_inst *di, gboolean *found_match);
 SRD_PRIV void srd_inst_free(struct srd_decoder_inst *di);
index a2055e2fecf3ae10c44605133e55185b26f62b56..8d1d5ae8212cc5f49eafd4d88488fe5c5f85376e 100644 (file)
@@ -235,10 +235,10 @@ struct srd_decoder_inst {
        GArray *match_array;
 
        /** Absolute start sample number. */
        GArray *match_array;
 
        /** Absolute start sample number. */
-       uint64_t start_samplenum;
+       uint64_t abs_start_samplenum;
 
        /** Absolute end sample number. */
 
        /** Absolute end sample number. */
-       uint64_t end_samplenum;
+       uint64_t abs_end_samplenum;
 
        /** Pointer to the buffer/chunk of input samples. */
        const uint8_t *inbuf;
 
        /** Pointer to the buffer/chunk of input samples. */
        const uint8_t *inbuf;
@@ -247,7 +247,7 @@ struct srd_decoder_inst {
        uint64_t inbuflen;
 
        /** Absolute current samplenumber. */
        uint64_t inbuflen;
 
        /** Absolute current samplenumber. */
-       uint64_t cur_samplenum;
+       uint64_t abs_cur_samplenum;
 
        /** Array of "old" (previous sample) pin values. */
        GArray *old_pins_array;
 
        /** Array of "old" (previous sample) pin values. */
        GArray *old_pins_array;
@@ -312,7 +312,7 @@ SRD_API int srd_session_start(struct srd_session *sess);
 SRD_API int srd_session_metadata_set(struct srd_session *sess, int key,
                GVariant *data);
 SRD_API int srd_session_send(struct srd_session *sess,
 SRD_API int srd_session_metadata_set(struct srd_session *sess, int key,
                GVariant *data);
 SRD_API int srd_session_send(struct srd_session *sess,
-               uint64_t start_samplenum, uint64_t end_samplenum,
+               uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize);
 SRD_API int srd_session_destroy(struct srd_session *sess);
 SRD_API int srd_pd_output_callback_add(struct srd_session *sess,
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize);
 SRD_API int srd_session_destroy(struct srd_session *sess);
 SRD_API int srd_pd_output_callback_add(struct srd_session *sess,
index 6a7628eb5c43ff351d0c88aac0de86e588d95aff..128d2240301e8de52eb788a14cf8f4fffb35f8b7 100644 (file)
--- a/session.c
+++ b/session.c
@@ -224,9 +224,45 @@ SRD_API int srd_session_metadata_set(struct srd_session *sess, int key,
  * has been configured, it is the minimum number of bytes needed to store
  * the default channels.
  *
  * has been configured, it is the minimum number of bytes needed to store
  * the default channels.
  *
+ * The calls to this function must provide the samples that shall be
+ * used by the protocol decoder
+ *  - in the correct order ([...]5, 6, 4, 7, 8[...] is a bug),
+ *  - starting from sample zero (2, 3, 4, 5, 6[...] is a bug),
+ *  - consecutively, with no gaps (0, 1, 2, 4, 5[...] is a bug).
+ *
+ * The start- and end-sample numbers are absolute sample numbers (relative
+ * to the start of the whole capture/file/stream), i.e. they are not relative
+ * sample numbers within the chunk specified by 'inbuf' and 'inbuflen'.
+ *
+ * Correct example (4096 samples total, 4 chunks @ 1024 samples each):
+ *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
+ *   srd_session_send(s, 1024, 2047, inbuf, 1024, 1);
+ *   srd_session_send(s, 2048, 3071, inbuf, 1024, 1);
+ *   srd_session_send(s, 3072, 4095, inbuf, 1024, 1);
+ *
+ * The chunk size ('inbuflen') can be arbitrary and can differ between calls.
+ *
+ * Correct example (4096 samples total, 7 chunks @ various samples each):
+ *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
+ *   srd_session_send(s, 1024, 1123, inbuf,  100, 1);
+ *   srd_session_send(s, 1124, 1423, inbuf,  300, 1);
+ *   srd_session_send(s, 1424, 1642, inbuf,  219, 1);
+ *   srd_session_send(s, 1643, 2047, inbuf,  405, 1);
+ *   srd_session_send(s, 2048, 3071, inbuf, 1024, 1);
+ *   srd_session_send(s, 3072, 4095, inbuf, 1024, 1);
+ *
+ * INCORRECT example (4096 samples total, 4 chunks @ 1024 samples each, but
+ * the start- and end-samplenumbers are not absolute):
+ *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
+ *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
+ *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
+ *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
+ *
  * @param sess The session to use. Must not be NULL.
  * @param sess The session to use. Must not be NULL.
- * @param start_samplenum The sample number of the first sample in this chunk.
- * @param end_samplenum The sample number of the last sample in this chunk.
+ * @param abs_start_samplenum The absolute starting sample number for the
+ *              buffer's sample set, relative to the start of capture.
+ * @param abs_end_samplenum The absolute ending sample number for the
+ *              buffer's sample set, relative to the start of capture.
  * @param inbuf Pointer to sample data. Must not be NULL.
  * @param inbuflen Length in bytes of the buffer. Must be > 0.
  * @param unitsize The number of bytes per sample. Must be > 0.
  * @param inbuf Pointer to sample data. Must not be NULL.
  * @param inbuflen Length in bytes of the buffer. Must be > 0.
  * @param unitsize The number of bytes per sample. Must be > 0.
@@ -236,7 +272,7 @@ SRD_API int srd_session_metadata_set(struct srd_session *sess, int key,
  * @since 0.4.0
  */
 SRD_API int srd_session_send(struct srd_session *sess,
  * @since 0.4.0
  */
 SRD_API int srd_session_send(struct srd_session *sess,
-               uint64_t start_samplenum, uint64_t end_samplenum,
+               uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
 {
        GSList *d;
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
 {
        GSList *d;
@@ -248,8 +284,8 @@ SRD_API int srd_session_send(struct srd_session *sess,
        }
 
        for (d = sess->di_list; d; d = d->next) {
        }
 
        for (d = sess->di_list; d; d = d->next) {
-               if ((ret = srd_inst_decode(d->data, start_samplenum,
-                               end_samplenum, inbuf, inbuflen, unitsize)) != SRD_OK)
+               if ((ret = srd_inst_decode(d->data, abs_start_samplenum,
+                               abs_end_samplenum, inbuf, inbuflen, unitsize)) != SRD_OK)
                        return ret;
        }
 
                        return ret;
        }
 
index dda0de28a2aab2fd9c6a991604eabeb031d49ed2..08580980070fe0ee10c654d7cafde315d7c9a3f8 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 {
                        /* 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;
                        byte_offset = di->dec_channelmap[i] / 8;
                        bit_offset = di->dec_channelmap[i] % 8;
                        sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
@@ -597,7 +597,7 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
        if (ret == 9999) {
                /* Empty condition list, automatic match. */
                PyObject_SetAttrString(di->py_inst, "matched", Py_None);
        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);
        }
 
                return get_current_pinvalues(di);
        }
 
@@ -614,7 +614,7 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
                if (found_match) {
                        /* Set self.samplenum to the (absolute) sample number that matched. */
                        PyObject_SetAttrString(di->py_inst, "samplenum",
                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);
 
                        if (di->match_array && di->match_array->len > 0) {
                                py_matched = PyTuple_New(di->match_array->len);
@@ -636,8 +636,8 @@ 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;
                /* 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;
 
                di->inbuf = NULL;
                di->inbuflen = 0;
 
index d126d7b420cd058aa89f5ec248c4aef1e8339313..ba356c0a9526246d953138275dd21aff4e9a4a04 100644 (file)
@@ -61,7 +61,7 @@ static PyObject *srd_logic_iternext(PyObject *self)
 
        /* Prepare the next samplenum/sample list in this iteration. */
        py_samplenum =
 
        /* Prepare the next samplenum/sample list in this iteration. */
        py_samplenum =
-           PyLong_FromUnsignedLongLong(logic->start_samplenum +
+           PyLong_FromUnsignedLongLong(logic->abs_start_samplenum +
                                        logic->itercnt);
        PyList_SetItem(logic->sample, 0, py_samplenum);
        py_samples = PyBytes_FromStringAndSize((const char *)logic->di->channel_samples,
                                        logic->itercnt);
        PyList_SetItem(logic->sample, 0, py_samplenum);
        py_samples = PyBytes_FromStringAndSize((const char *)logic->di->channel_samples,