RingBuff.c文件实现
#include "RingBuff.h"
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] 环形缓冲区初始化
* @param[in] pRingBuff 环形缓冲区结构体地址
* @param[in] buf_size 环形缓存区大小(字节数)
* @return[out]
*/
void RingBuff_Init(RingBuff_t *pRingBuff, unsigned int buf_size)
{
RingBuff_t *ringbuff = NULL;
unsigned int size = 0;
ringbuff = pRingBuff;
size = buf_size;
ringbuff->buffer = (uint8_t*)calloc(size, sizeof(uint8_t));
ringbuff->size = size;
ringbuff->head = 0;
ringbuff->tail = 0;
}
/**
* @brief 判断环形缓冲区是否为空
* @param[in] pRingBuff 环形缓冲区结构体地址
* @return[out] RING_TRUE 空
* RING_FALSE 非空
*/
static unsigned char RingBuff_isEmpty(RingBuff_t *pRingBuff)
{
RingBuff_t *ringbuff = NULL;
ringbuff = pRingBuff;
if(ringbuff->head == ringbuff->tail)
{
return RING_TRUE;
}
return RING_FALSE;
}
/**
* @brief 判断环形缓冲区是否为满
* @param[in] pRingBuff 环形缓冲区结构体地址
* @return[out] RING_TRUE 满
* RING_FALSE 非满
*/
static unsigned char RingBuff_isFull(RingBuff_t *pRingBuff)
{
RingBuff_t *ringbuff = NULL;
ringbuff = pRingBuff;
if((ringbuff->tail + 1) % ringbuff->size == ringbuff->head)
{
return RING_TRUE;
}
return RING_FALSE;
}
/**
* @brief 从环形缓冲区头部读出一个字节
* @param[in] pRingBuff 环形缓冲区结构体地址
* @param[in] pData 读出缓冲区字节所将要存储的地址
* @return[out] RING_OK 读出成功,
* RING_ERR 读出失败
*/
static unsigned char RingBuff_ReadOneByte(RingBuff_t *pRingBuff, unsigned char *pData)
{
RingBuff_t *ringbuff = NULL;
unsigned char *temp = NULL;
ringbuff = pRingBuff;
temp = pData;
//判空
if(RING_TRUE == RingBuff_isEmpty(ringbuff))
{
SYSlog("ring buffer is empty!\r\n");
return RING_ERR;
}
*temp = ringbuff->buffer[ringbuff->head];
ringbuff->head = (ringbuff->head + 1) % ringbuff->size;
return RING_OK;
}
/**
* @brief 向环形缓冲区的尾部写入一个字节
* @param[in] pRingBuff 环形缓冲区结构体地址
* @param[in] pData 将要写入缓冲区的字节的地址
* @return[out] RING_OK 写入成功,
* RING_ERR 写入失败
*/
static unsigned char RingBuff_WriteOneByte(RingBuff_t *pRingBuff, unsigned char *pData)
{
RingBuff_t *ringbuff = NULL;
unsigned char *temp = NULL;
ringbuff = pRingBuff;
temp = pData;
//判满
if(RING_TRUE == RingBuff_isFull(ringbuff))
{
SYSlog("ring buffer is full!\r\n");
return RING_ERR;
}
ringbuff->buffer[ringbuff->tail] = *temp;
ringbuff->tail = (ringbuff->tail + 1) % ringbuff->size;
return RING_OK;
}
/**
* @brief 从环形缓冲区读出多个字节
* @param[in] pRingBuff 环形缓冲区结构体地址
* @param[in] pData 读出缓冲区多字节将要存储的首地址
* @param[in] Size 读出环形缓冲区的字节数
* @return[out] RING_OK 读出成功,
* RING_ERR 读出失败
*/
unsigned char RingBuff_ReadNByte(RingBuff_t *pRingBuff, unsigned char *pData, int Size)
{
RingBuff_t *ringbuff = NULL;
unsigned char *temp = NULL;
int size = 0;
ringbuff = pRingBuff;
temp = pData;
size = Size;
for(int i = 0; i < size; i++)
{
if(RingBuff_ReadOneByte(ringbuff, temp + i) == RING_ERR)
{
return RING_ERR;
}
}
return RING_OK;
}
//
/**
* @brief 向环形缓冲区写入多个字节
* @param[in] pRingBuff 环形缓冲区结构体地址
* @param[in] pData 将要写入缓冲区多字节的首地址
* @param[in] Size 写入环形缓冲区的字节数
* @return[out] RING_OK 写入成功,
* RING_ERR 写入失败
*/
unsigned char RingBuff_WriteNByte(RingBuff_t *pRingBuff, unsigned char *pData, int Size)
{
RingBuff_t *ringbuff = NULL;
unsigned char *temp = NULL;
int size = 0;
ringbuff = pRingBuff;
temp = pData;
size = Size;
for(int i = 0; i < size; i++)
{
if(RingBuff_WriteOneByte(ringbuff, temp + i) == RING_ERR)
{
return RING_ERR;
}
}
return RING_OK;
}
/**
* @brief 获取当前环形缓冲区中有效数据的长度
* @param[in] pRingBuff 环形缓冲区结构体地址
* @return[out] 有效数据长度
*/
int RingBuff_GetValidLen(RingBuff_t *pRingBuff)
{
RingBuff_t *ringbuff = NULL;
int temp = 0;
ringbuff = pRingBuff;
if(ringbuff->tail >= ringbuff->head)
{
temp = ringbuff->tail - ringbuff->head;
}
else
{
temp = ringbuff->tail + ringbuff->size - ringbuff->head;
}
return temp;
}
/**
* @brief 获取当前环形缓冲区中空闲数据的长度
* @param[in] pRingBuff 环形缓冲区结构体地址
* @return[out] 空闲数据长度
*/
int RingBuff_GetIdleLen(RingBuff_t *pRingBuff)
{
RingBuff_t *ringbuff = NULL;
int temp = 0;
ringbuff = pRingBuff;
if(ringbuff->tail >= ringbuff->head)
{
temp = ringbuff->size - ringbuff->tail + ringbuff->head;
}
else
{
temp = ringbuff->head - ringbuff->tail;
}
return temp;
}
/**
* @brief 获取当前头部数据
* @param[in] pRingBuff 环形缓冲区结构体地址
* @return[out] 当前头部数据
*/
unsigned char RingBuff_GetHeadItem(RingBuff_t *pRingBuff)
{
unsigned char temp = 0;
RingBuff_t *ringbuff = NULL;
ringbuff = pRingBuff;
temp = ringbuff->buffer[ringbuff->head];
return temp;
}
/**
* @brief 获取指定下标数据
* @param[in] pRingBuff 环形缓冲区结构体地址
* @param[in] index 环形缓冲区下标(环形缓冲区首地址的偏移量)
* @return[out] 环形缓冲区下标指定的数据
*/
unsigned char RingBuff_GetIndexItem(RingBuff_t *pRingBuff, int index)
{
unsigned char res = 0;
RingBuff_t *ringbuff = NULL;
int temp = 0;
ringbuff = pRingBuff;
temp = index;
res = ringbuff->buffer[temp % ringbuff->size];
return res;
}
/**
* @brief 释放环形缓冲区
* @param[in] pRingBuff 环形缓冲区结构体地址
* @return[out]
*/
void RingBuff_Release(RingBuff_t *pRingBuff)
{
RingBuff_t *ringbuff = NULL;
ringbuff = pRingBuff;
free(ringbuff->buffer);
ringbuff->size = 0;
ringbuff->head = 0;
ringbuff->tail = 0;
}
|