#include "edpkit.h"
/*
* 函数名: EdpPacketClass
* 功能: 构造函数
* 说明:
*
* 相关函数:
* 参数: 无
*
* 返回值: 无
*/
EdpPacketClass::EdpPacketClass()
{
this->data=NULL;
this->write_pos=0;
this->read_pos=0;
this->capacity=EDP_BUFFER_SIZE;
data=(uint8*)malloc(sizeof(uint8)*EDP_BUFFER_SIZE);
assert(data!=NULL);
}
/*
* 函数名: ~EdpPacketClass
* 功能: 析构函数
* 说明:
*
* 相关函数:
* 参数: 无
*
* 返回值: 无
*/
EdpPacketClass::~EdpPacketClass()
{
free(this->data);
this->data=NULL;
}
/*
* 函数名: GetData
* 功能: 获取数据成员,EDP包的起始地址
* 说明:
*
* 相关函数:
* 参数: 无
* 返回值: 类型 (uint8*)
* EDP包的起始地址
*/
uint8* EdpPacketClass::GetData(void)
{
return this->data;
}
/*
* 函数名: GetWritepos
* 功能: 获取数据成员,EDP包的写位置
* 说明:
*
* 相关函数:
* 参数: 无
* 返回值: 类型 (uint32)
* EDP包的写入位置
*/
uint32 EdpPacketClass::GetWritepos(void)
{
return this->write_pos;
}
/*
* 函数名: GetReadpos
* 功能: 获取数据成员,EDP包的读位置
* 说明:
*
* 相关函数:
* 参数: 无
* 返回值: 类型 (uint32)
* EDP包的读位置
*/
uint32 EdpPacketClass::GetReadpos(void)
{
return this->read_pos;
}
/*
* 函数名: ClearParameter
* 功能: 重置数据成员,没一个打包函数先调用该函数,然后重新打包数据
* 说明:
*
* 相关函数:
* 参数: 无
* 返回值: void
*/
void EdpPacketClass::ClearParameter(void)
{
this->read_pos=0;
this->write_pos=0;
memset(data,0,EDP_BUFFER_SIZE);
}
/*
* 函数名: CheckCapacity
* 功能: 检查buffer是否能够写入长度为len的字节流,如果不够,自动扩展Buffer的容量(不影响buffer数据)
* 说明:
*
* 相关函数:
* 参数: 无
* 返回值: void
*/
int32 EdpPacketClass::CheckCapacity(uint32 len)
{
uint32 cap_len = capacity;
int32 flag = 0;
uint8* pdata = NULL;
while (cap_len - write_pos < len) /* remain len < len */
{
cap_len = cap_len << 1;
if (++flag > 32)
break; /* overflow */
}
if (flag > 32)
return -1;
if (cap_len > capacity)
{
pdata = (uint8*)malloc(sizeof(uint8) * cap_len);
memcpy(pdata, data, write_pos);
free(data);
data = pdata;
capacity = cap_len;
}
return 0;
}
/*
* 函数名: ReadByte
* 功能: 按EDP协议,从BUFFER中读取一个字节的数据
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::ReadByte(uint8* val)
{
if (read_pos+1 > write_pos)
return -1;
*val =data[read_pos];
read_pos += 1;
return 0;
}
/*
* 函数名: ReadBytes
* 功能: 按EDP协议,从BUFFER中读取count个字节的数据
* 说明: 其中VAL是MALLOC函数分配的存储空间,在使用完之后,需要free.
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::ReadBytes(uint8** val, uint32 count)
{
if (read_pos+count > write_pos)
return -1;
*val = (uint8*)malloc(sizeof(uint8) * count);
memcpy(*val, data+read_pos, count);
read_pos += count;
return 0;
}
/*
* 函数名: ReadUint16
* 功能: 按EDP协议,从BUFFER中读取UINT16值
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::ReadUint16(uint16* val)
{
uint8 msb, lsb;
if (read_pos+2 > write_pos)
return -1;
msb = data[read_pos];
read_pos++;
lsb = data[read_pos];
read_pos++;
*val = (msb<<8) + lsb;
return 0;
}
/*
* 函数名: ReadUint32
* 功能: 按EDP协议,从BUFFER中读取UINT32值
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::ReadUint32(uint32* val)
{
int32 i = 0;
uint32 tmpval = 0;
if (read_pos+4 > write_pos)
return -1;
while (i++ < 4) {
tmpval = (tmpval << 8) | (data[read_pos]);
read_pos++;
}
*val = tmpval;
return 0;
}
/*
* 函数名: ReadStr
* 功能: 按EDP协议,从BUFFER中读取字符串
* 说明: val是malloc函数分配的存储空间,在使用完后需要free
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::ReadStr(char** val)
{
uint16 len = 0;
int rc = 0;
/* read str len */
rc = ReadUint16(&len);
if (rc)
return rc;
if (read_pos+len > write_pos)
return -1;
/* copy str val */
*val = (char*)malloc(sizeof(char) * (len + 1));
memset(*val, 0, len+1);
strncpy(*val, (const char *)(data + read_pos), len);
read_pos += len;
return 0;
}
/*
* 函数名: ReadRemainlen
* 功能: 按EDP协议,从BUFFER中读取消息剩余长度
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::ReadRemainlen(uint32* len_val)
{
uint32 multiplier = 1;
uint32 len_len = 0;
uint8 onebyte = 0;
int32 rc;
*len_val = 0;
do {
rc = ReadByte(&onebyte);
if (rc)
return rc;
if(len_len==0)
{
*len_val += (onebyte & 0x7f);
}
else if(len_len==1)
{
*len_val += (onebyte & 0x7f) <<7;
}
else if(len_len==2)
{
*len_val += (onebyte & 0x7f) <<14;
}
else if(len_len==3)
{
*len_val += (onebyte & 0x7f) <<21;
}
else
{;}
len_len++;
if (len_len > 4) {
return -1;/*len of len more than 4;*/
}
} while((onebyte & 0x80) != 0);
return 0;
}
/*
* 函数名: WriteByte
* 功能: 按EDP协议,将一个字节数据写入EDP包缓冲(数据成员data所指的空间)
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::WriteByte(uint8 byte)
{
assert(read_pos == 0);
if (CheckCapacity(1))
return -1;
data[write_pos] = byte;
write_pos++;
return 0;
}
/*
* 函数名: WriteBytes
* 功能: 按EDP协议,将count个字节数据写入EDP包缓冲(数据成员data所指的空间)
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::WriteBytes(const void* bytes, uint32 count)
{
assert(read_pos == 0);
if (CheckCapacity(count))
return -1;
memcpy(data + write_pos, bytes, count);
write_pos += count;
return 0;
}
/*
* 函数名: WriteUint16
* 功能: 按EDP协议,将uINT16值写入EDP包缓冲(数据成员data所指的空间)
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::WriteUint16(uint16 val)
{
assert( read_pos == 0);
return WriteByte(0xff&((val)>>8))
|| WriteByte(0xff&(val));
}
/*
* 函数名: WriteUint32
* 功能: 按EDP协议,将uINT32值写入EDP包缓冲(数据成员data所指的空间)
* 说明:
*
* 相关函数:
* 参数:
* 返回值:
*/
int32 EdpPacketClass::WriteUint32(uint32 val)
{
assert(read_pos == 0);
return WriteByte((val >> 24) & 0x00FF)
|| WriteByte((val >> 16) & 0x00FF)
评论0
最新资源