Skip to content

Commit ffcd1cb

Browse files
rsj123Rbb666
authored andcommitted
fix(drivers/ipc): correct rt_ringbuffer_peek to not consume data
1 parent f610f18 commit ffcd1cb

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

components/drivers/include/ipc/ringbuffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *pt
7171
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
7272
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
7373
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint32_t length);
74+
rt_size_t rt_ringbuffer_get_direct(struct rt_ringbuffer *rb, rt_uint8_t **ptr);
7475
rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr);
7576
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
7677
rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);

components/drivers/ipc/ringbuffer.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,18 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
219219
RTM_EXPORT(rt_ringbuffer_get);
220220

221221
/**
222-
* @brief Get the first readable byte of the ring buffer.
222+
* @brief Get data from the ring buffer in zero-copy mode.
223223
*
224224
* @param rb A pointer to the ringbuffer.
225225
* @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
226226
*
227-
* @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
227+
* @note This function returns a direct pointer to the internal buffer and consumes the data
228+
* (advances read_index). It returns the contiguous readable data length. If data wraps
229+
* around the buffer end, call this function again to get the remaining segment.
228230
*
229-
* @return Return the size of the ring buffer.
231+
* @return Return the contiguous readable data size we consumed from the ring buffer.
230232
*/
231-
rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
233+
rt_size_t rt_ringbuffer_get_direct(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
232234
{
233235
rt_size_t size;
234236

@@ -259,6 +261,44 @@ rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
259261

260262
return size;
261263
}
264+
RTM_EXPORT(rt_ringbuffer_get_direct);
265+
266+
/**
267+
* @brief Get the first readable byte of the ring buffer.
268+
*
269+
* @param rb A pointer to the ringbuffer.
270+
* @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
271+
*
272+
* @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
273+
*
274+
* @return Return the size of the ring buffer.
275+
*/
276+
rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
277+
{
278+
rt_size_t size;
279+
280+
RT_ASSERT(rb != RT_NULL);
281+
282+
*ptr = RT_NULL;
283+
284+
/* whether has enough data */
285+
size = rt_ringbuffer_data_len(rb);
286+
287+
/* no data */
288+
if (size == 0)
289+
return 0;
290+
291+
*ptr = &rb->buffer_ptr[rb->read_index];
292+
293+
if ((rt_size_t)(rb->buffer_size - rb->read_index) > size)
294+
{
295+
return size;
296+
}
297+
298+
size = rb->buffer_size - rb->read_index;
299+
300+
return size;
301+
}
262302
RTM_EXPORT(rt_ringbuffer_peek);
263303

264304
/**

components/drivers/serial/dev_serial_v2.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,13 +2000,13 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
20002000
{
20012001
rt_serial_update_write_index(&rx_fifo->dma_ping_rb, rx_length);
20022002

2003-
size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
2003+
size = rt_ringbuffer_get_direct(&rx_fifo->dma_ping_rb, &ptr);
20042004

20052005
put_len = rt_ringbuffer_put(&rx_fifo->rb, ptr, size);
20062006
if (put_len != size)
20072007
break;
20082008

2009-
size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
2009+
size = rt_ringbuffer_get_direct(&rx_fifo->dma_ping_rb, &ptr);
20102010
if (size == 0)
20112011
break;
20122012

@@ -2022,11 +2022,11 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
20222022
{
20232023
rt_serial_update_write_index(&rx_fifo->dma_ping_rb, rx_length);
20242024

2025-
size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
2025+
size = rt_ringbuffer_get_direct(&rx_fifo->dma_ping_rb, &ptr);
20262026

20272027
rt_ringbuffer_put_force(&rx_fifo->rb, ptr, size);
20282028

2029-
size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
2029+
size = rt_ringbuffer_get_direct(&rx_fifo->dma_ping_rb, &ptr);
20302030
if (size == 0)
20312031
break;
20322032

0 commit comments

Comments
 (0)