补充 ringbufer 接口

Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
2022-10-14 14:44:22 +08:00
parent a37313f662
commit 6308fb2e79
2 changed files with 81 additions and 1 deletions

View File

@@ -360,7 +360,7 @@
<Group>
<GroupName>DeviceDrivers</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>

View File

@@ -464,4 +464,84 @@ void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
}
RTM_EXPORT(rt_ringbuffer_destroy);
/**
* copy data from ring buffer
*/
rt_size_t rt_ringbuffer_cpy(struct rt_ringbuffer *rb,
rt_uint8_t *ptr,
rt_uint16_t length)
{
rt_size_t size;
RT_ASSERT(rb != RT_NULL);
/* whether has enough data */
size = rt_ringbuffer_data_len(rb);
/* no data */
if (size == 0)
return 0;
/* less data */
if (size < length)
length = size;
if (rb->buffer_size - rb->read_index > length)
{
/* copy all of data */
memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
/* this should not cause overflow because there is enough space for
* length of data in current mirror */
//rb->read_index += length;
return length;
}
memcpy(&ptr[0],
&rb->buffer_ptr[rb->read_index],
rb->buffer_size - rb->read_index);
memcpy(&ptr[rb->buffer_size - rb->read_index],
&rb->buffer_ptr[0],
length - (rb->buffer_size - rb->read_index));
/* we are going into the other side of the mirror */
//rb->read_mirror = ~rb->read_mirror;
//rb->read_index = length - (rb->buffer_size - rb->read_index);
return length;
}
RTM_EXPORT(rt_ringbuffer_copy);
/**
* del data from ring buffer
*/
rt_size_t rt_ringbuffer_del(struct rt_ringbuffer *rb,
rt_uint16_t length)
{
rt_size_t size;
RT_ASSERT(rb != RT_NULL);
/* whether has enough data */
size = rt_ringbuffer_data_len(rb);
/* no data */
if (size == 0)
return 0;
/* less data */
if (size < length)
length = size;
if (rb->buffer_size - rb->read_index > length)
{
rb->read_index += length;
return length;
}
rb->read_mirror = ~rb->read_mirror;
rb->read_index = length - (rb->buffer_size - rb->read_index);
return length;
}
RTM_EXPORT(rt_ringbuffer_del);
#endif