diff --git a/sys/include/chunked_ringbuffer.h b/sys/include/chunked_ringbuffer.h index 9f1ac2fa24da..d1727ba02c95 100644 --- a/sys/include/chunked_ringbuffer.h +++ b/sys/include/chunked_ringbuffer.h @@ -72,9 +72,26 @@ typedef void (*crb_foreach_callback_t)(void *ctx, uint8_t *bytes, size_t len); */ void crb_init(chunk_ringbuf_t *rb, void *buffer, size_t len); +/** + * @brief Close the current chunk + * + * @note This function is expected to be called in ISR context / with + * interrupts disabled. + * + * @param[in] rb The Ringbuffer to work on + * @param[in] valid True if the chunk is valid and should be stored + * False if the current chunk should be discarded + * + * @return true If the chunk could be stored in the valid chunk array + * @return false If there is no more space in the valid chunk array + */ +bool crb_end_chunk(chunk_ringbuf_t *rb, bool valid); + /** * @brief Start a new chunk on the ringbuffer * + * If an unfinished chunk already exists, it will be discarded. + * * @note This function is expected to be called in ISR context / with * interrupts disabled. * @@ -85,6 +102,11 @@ void crb_init(chunk_ringbuf_t *rb, void *buffer, size_t len); */ static inline bool crb_start_chunk(chunk_ringbuf_t *rb) { + /* discard stale chunk */ + if (rb->cur_start) { + crb_end_chunk(rb, false); + } + /* pointing to the start of the first chunk */ if (rb->cur == rb->protect) { return false; @@ -150,21 +172,6 @@ static inline bool crb_add_byte(chunk_ringbuf_t *rb, uint8_t b) */ bool crb_add_bytes(chunk_ringbuf_t *rb, const void *data, size_t len); -/** - * @brief Close the current chunk - * - * @note This function is expected to be called in ISR context / with - * interrupts disabled. - * - * @param[in] rb The Ringbuffer to work on - * @param[in] valid True if the chunk is valid and should be stored - * False if the current chunk should be discarded - * - * @return true If the chunk could be stored in the valid chunk array - * @return false If there is no more space in the valid chunk array - */ -bool crb_end_chunk(chunk_ringbuf_t *rb, bool valid); - /** * @brief Add a complete chunk to the Ringbuffer *