声明:
基于visual studio 2017的登陆报文调试,源码摘自Onenet EDP协议SDK。版权解释权为中国移动Onenet。
本文章仅用于个人程序调试记录,给更多调试后来者以参考。
#include
#include
#include
#include
#define assert(com) { if(com != 1) printf("error...\r\n");}
typedef unsigned char uint8;
typedef char int8;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned int uint32;
typedef int int32;
typedef struct Buffer
{
uint8* _data; /* buffer数据 */
uint32 _write_pos; /* buffer写入位置 */
uint32 _read_pos; /* buffer读取位置 */
uint32 _capacity; /* buffer容量 */
}Buffer, SendBuffer, RecvBuffer, EdpPacket;
/*----------------------------消息类型---------------------------------------*/
/* 连接请求 */
#define CONNREQ 0x10
/* 连接响应 */
#define CONNRESP 0x20
/* 转发(透传)数据 */
#define PUSHDATA 0x30
/* 升级请求 */
#define UPDATEREQ 0x50
/* 升级响应 */
#define UPDATERESP 0x60
/* 存储(转发)数据 */
#define SAVEDATA 0x80
/* 存储确认 */
#define SAVEACK 0x90
/* 命令请求 */
#define CMDREQ 0xA0
/* 命令响应 */
#define CMDRESP 0xB0
/* 心跳请求 */
#define PINGREQ 0xC0
/* 心跳响应 */
#define PINGRESP 0xD0
/* 加密请求 */
#define ENCRYPTREQ 0xE0
/* 加密响应 */
#define ENCRYPTRESP 0xF0
#define UPDATE 0x50
/*---------------------------------------------------------------------------*/
#define MOSQ_MSB(A) (uint8)((A & 0xFF00) >> 8)
#define MOSQ_LSB(A) (uint8)(A & 0x00FF)
#define BUFFER_SIZE (0x01<<20)
#define PROTOCOL_NAME "EDP"
#define PROTOCOL_VERSION 1
#define MAX_FLOAT_DPS_COUNT 1000
/*---------------------------------------------------------------------------*/
int32 WriteRemainlen(Buffer* buf, uint32 len_val)
{
uint32 remaining_length = len_val;
int32 remaining_count = 0;
uint8 byte = 0;
assert(buf->_read_pos == 0);
do {
byte = remaining_length % 128;
remaining_length = remaining_length / 128;
/* If there are more digits to encode, set the top bit of this digit */
if (remaining_length > 0) {
byte = byte | 0x80;
}
buf->_data[buf->_write_pos++] = byte;
remaining_count++;
} while (remaining_length > 0 && remaining_count < 5);
assert(remaining_count != 5);
return 0;
}
/*---------------------------------------------------------------------------*/
Buffer* NewBuffer()
{
Buffer* buf = (Buffer*)malloc(sizeof(Buffer));
buf->_data = (uint8*)malloc(sizeof(uint8) * BUFFER_SIZE);
buf->_write_pos = 0;
buf->_read_pos = 0;
buf->_capacity = BUFFER_SIZE;
return buf;
}
/*---------------------------------------------------------------------------*/
void DeleteBuffer(Buffer** buf)
{
uint8* pdata = (*buf)->_data;
free(pdata);
free(*buf);
*buf = 0;
}
/*---------------------------------------------------------------------------*/
int32 CheckCapacity(Buffer* buf, uint32 len)
{
uint32 cap_len = buf->_capacity;
int32 flag = 0;
uint8* pdata = NULL;
while (cap_len - buf->_write_pos < len) /* remain len < len */
{
cap_len = cap_len << 1;
if (++flag > 32)
break; /* overflow */
}
if (flag > 32)
return -1;
if (cap_len > buf->_capacity)
{
pdata = (uint8*)malloc(sizeof(uint8) * cap_len);
memcpy(pdata, buf->_data, buf->_write_pos);
free(buf->_data);
buf->_data = pdata;
buf->_capacity = cap_len;
}
return 0;
}
/*---------------------------------------------------------------------------*/
int32 WriteByte(Buffer* buf, uint8 byte)
{
assert(buf->_read_pos == 0);
if (CheckCapacity(buf, 1))
return -1;
buf->_data[buf->_write_pos] = byte;
buf->_write_pos++;
return 0;
}
/*---------------------------------------------------------------------------*/
int32 WriteBytes(Buffer* buf, const void* bytes, uint32 count)
{
assert(buf->_read_pos == 0);
if (CheckCapacity(buf, count))
return -1;
memcpy(buf->_data + buf->_write_pos, bytes, count);
buf->_write_pos += count;
return 0;
}
/*---------------------------------------------------------------------------*/
int32 WriteUint16(Buffer* buf, uint16 val)
{
assert(buf->_read_pos == 0);
return WriteByte(buf, MOSQ_MSB(val))
|| WriteByte(buf, MOSQ_LSB(val));
}
/*---------------------------------------------------------------------------*/
int32 WriteStr(Buffer* buf, const char *str)
{
uint16 length = 0;
assert(buf->_read_pos == 0);
length = strlen(str);
return WriteUint16(buf, length)
|| WriteBytes(buf, str, length);
}
/*---------------------------------------------------------------------------*/
EdpPacket* PacketConnect2(const char* userid, const char* auth_info)
{
EdpPacket* pkg = NULL;
uint32 remainlen;
pkg = NewBuffer();
/* msg type */
WriteByte(pkg, CONNREQ);
/* remain len */
remainlen = (2 + 3) + 1 + 1 + 2 + 2 + (2 + strlen(userid)) + (2 + strlen(auth_info));
WriteRemainlen(pkg, remainlen);
/* protocol desc */
WriteStr(pkg, PROTOCOL_NAME);
/* protocol version */
WriteByte(pkg, PROTOCOL_VERSION);
/* connect flag */
WriteByte(pkg, 0xC0);
/* keep time */
WriteUint16(pkg, 0x0080);
/* devid */
WriteByte(pkg, 0x00);
WriteByte(pkg, 0x00);
/* USERID */
WriteStr(pkg, userid);
/* auth info */
WriteStr(pkg, auth_info);
return pkg;
}
/*---------------------------------------------------------------------------*/
int32 IsPkgComplete(RecvBuffer* buf)
{
uint8* data = NULL;
uint32 data_len = 0;
uint32 multiplier = 1;
uint32 len_val = 0;
uint32 len_len = 1;
uint8* pdigit = NULL;
uint32 pkg_total_len = 0;
data = buf->_data;
data_len = buf->_write_pos;
if (data_len <= 1) {
return 0; /* continue receive */
}
/* recevie remaining len */
pdigit = data;
do {
if (len_len > 4) {
return -1; /* protocol error; */
}
if (len_len > data_len - 1) {
return 0; /* continue receive */
}
len_len++;
pdigit++;
len_val += ((*pdigit) & 0x7f) * multiplier;
multiplier *= 0x80;
} while (((*pdigit) & 0x80) != 0);
pkg_total_len = len_len + len_val;
/* receive payload */
if (pkg_total_len <= (uint32)data_len) {
#ifdef _DEBUG
printf("a complete packet len:%d\n", pkg_total_len);
#endif
return pkg_total_len; /* all data for this pkg is read */
}
else {
return 0; /* continue receive */
}
}
/*---------------------------------------------------------------------------*/
EdpPacket* GetEdpPacket(RecvBuffer* buf)
{
EdpPacket* pkg = NULL;
int32 flag = 0;
assert(buf->_read_pos == 0);
flag = IsPkgComplete(buf);
if (flag <= 0)
return pkg;
pkg = NewBuffer();
WriteBytes(pkg, buf->_data, flag);
/* shrink buffer */
memmove(buf->_data, buf->_data + flag, buf->_write_pos - flag);
buf->_write_pos -= flag;
return pkg;
}
/*---------------------------------------------------------------------------*/
int main(int argc, char * argv[])
{
EdpPacket* send_pkg = NULL;
send_pkg = PacketConnect2("112988","test1");
int len = send_pkg->_write_pos;
printf("len = %d\r\n", len);
for (int i = 0; i < len;i++)
{
printf("0x%X", send_pkg->_data[i]);
printf(" ");
}
DeleteBuffer(&send_pkg);
system("pause");
}