--- a/mb_ds_util.h Mon Mar 22 14:57:48 2021 +0100
+++ b/mb_ds_util.h Mon Jun 07 11:21:26 2021 +0200
@@ -97,6 +97,7 @@
int data_start; /* offset within *data were valid data starts */
int data_end; /* offset within *data were valid data ends */
int max_data_start; /* optimization parameter! When should it be normalised? */
+ int marked_for_purge; /* Number of bytes to be deleted in next call to lb_data_purge() */
} lb_buf_t;
/* NOTE: lb = Linear Buffer */
@@ -111,6 +112,7 @@
buf->data_size = size;
buf->data_start = 0;
buf->data_end = 0;
+ buf->marked_for_purge = 0;
buf->max_data_start = max_data_start;
buf->data = (u8 *)malloc(size);
return buf->data;
@@ -122,9 +124,12 @@
}
static inline u8 *lb_normalize(lb_buf_t *buf) {
- return (u8 *)memmove(buf->data,
- buf->data + buf->data_start,
- buf->data_end - buf->data_start);
+ u8 *ptr = (u8 *)memmove(buf->data,
+ buf->data + buf->data_start,
+ buf->data_end - buf->data_start);
+ buf->data_end -= buf->data_start;
+ buf->data_start = 0;
+ return ptr;
}
static inline u8 *lb_data(lb_buf_t *buf) {
@@ -140,12 +145,17 @@
buf->data_end = buf->data_size - 1;
}
+static inline void lb_data_mark_for_purge(lb_buf_t *buf, int count) {
+ buf->marked_for_purge += count;
+}
+
static inline u8 *lb_data_purge(lb_buf_t *buf, int count) {
- buf->data_start += count;
- if (buf->data_start > buf->data_end)
- buf->data_start = buf->data_end;
+ buf->data_start += count + buf->marked_for_purge;
+ buf->marked_for_purge = 0;
- if ((buf->data_end == buf->data_size) || (buf->data_start >= buf->max_data_start))
+ if (buf->data_start >= buf->data_end)
+ buf->data_start = buf->data_end = 0; // no bytes in buffer, might just as well normalize it
+ else if ((buf->data_end == buf->data_size) || (buf->data_start >= buf->max_data_start))
return lb_normalize(buf);
return buf->data + buf->data_start;
@@ -153,6 +163,7 @@
static inline void lb_data_purge_all(lb_buf_t *buf) {
buf->data_start = buf->data_end = 0;
+ buf->marked_for_purge = 0;
}
static inline u8 *lb_free(lb_buf_t *buf) {
--- a/mb_rtu.c Mon Mar 22 14:57:48 2021 +0100
+++ b/mb_rtu.c Mon Jun 07 11:21:26 2021 +0200
@@ -1422,8 +1422,11 @@
#endif
/* set the data pointer */
*recv_data_ptr = lb_data(&(buf->data_buf));
- /* remove the frame bytes off the buffer */
- lb_data_purge(&(buf->data_buf), frame_length);
+ /* Mark the frame bytes to be deleted off the buffer by the next call to lb_data_purge() */
+ /* Notice that we cannot delete the frame bytes right away, as they will still be accessed by whoever
+ * called read_frame(). We can only delete this data on the next call to read_frame()
+ */
+ lb_data_mark_for_purge(&(buf->data_buf), frame_length);
/* reset the search_history flag */
buf->frame_search_history = 0;
/* if the buffer becomes empty, then reset boundary flag */
@@ -1532,6 +1535,16 @@
*recv_data_ptr = NULL;
/*===================================*
+ * Delete any previously received data that has already been returned as a valid frame in *
+ *===================================*/
+ /* Delete any previously received data that has already been returned as a valid frame in
+ * the previous invocation of read_frame().
+ * Notice that the data that will be deleted hass ben marked for deletion by calling
+ * lb_data_mark_for_purge() in return_frame()
+ */
+ lb_data_purge(&(recv_buf->data_buf), 0);
+
+ /*===================================*
* Check for frame in left over data *
*===================================*/
/* If we have any data left over from previous call to read_frame()
--- a/mb_rtu_private.h Mon Mar 22 14:57:48 2021 +0100
+++ b/mb_rtu_private.h Mon Jun 07 11:21:26 2021 +0200
@@ -49,7 +49,7 @@
* due to the algorithm used to work around aborted frames.
*/
#define RECV_BUFFER_SIZE_SMALL (MAX_RTU_FRAME_LENGTH + 10)
-#define RECV_BUFFER_SIZE_LARGE (2 * MAX_RTU_FRAME_LENGTH)
+#define RECV_BUFFER_SIZE_LARGE (2 * (MAX_RTU_FRAME_LENGTH))
/* Frame lengths... */
--- a/mb_util.h Mon Mar 22 14:57:48 2021 +0100
+++ b/mb_util.h Mon Jun 07 11:21:26 2021 +0200
@@ -86,7 +86,7 @@
#define TCP_HEADER_LENGTH 6
/* Global Frame sizes */
-#define MAX_RTU_FRAME_LENGTH MAX_L2_FRAME_LENGTH + RTU_FRAME_CRC_LENGTH
+#define MAX_RTU_FRAME_LENGTH (MAX_L2_FRAME_LENGTH + RTU_FRAME_CRC_LENGTH)
#define MAX_ASC_FRAME_LENGTH ((MAX_L2_FRAME_LENGTH * L2_TO_ASC_CODING) + \
ASC_FRAME_HEADER_LENGTH + ASC_FRAME_TAIL_LENGTH + \
ASC_FRAME_LRC_LENGTH)