#include <ctype.h>
#include "AepServiceCodes.h"
//无符号整型16位
uint_16 aep_htons(uint_16 source)
{
if(AEP_ENDIANNESS == AEP_BIG_ENDIAN)
return source;
else
return (uint_16)( 0
| ((source & 0x00ff) << 8)
| ((source & 0xff00) >> 8) );
}
//无符号整型32位
uint_32 aep_htoni(uint_32 source)
{
if(AEP_ENDIANNESS == AEP_BIG_ENDIAN)
return source;
else
return 0
| ((source & 0x000000ff) << 24)
| ((source & 0x0000ff00) << 8)
| ((source & 0x00ff0000) >> 8)
| ((source & 0xff000000) >> 24);
}
//无符号整型64位
uint_64 aep_htonl(uint_64 source)
{
if(AEP_ENDIANNESS == AEP_BIG_ENDIAN)
return source;
else
return 0
| ((source & (uint_64)(0x00000000000000ff)) << 56)
| ((source & (uint_64)(0x000000000000ff00)) << 40)
| ((source & (uint_64)(0x0000000000ff0000)) << 24)
| ((source & (uint_64)(0x00000000ff000000)) << 8)
| ((source & (uint_64)(0x000000ff00000000)) >> 8)
| ((source & (uint_64)(0x0000ff0000000000)) >> 24)
| ((source & (uint_64)(0x00ff000000000000)) >> 40)
| ((source & (uint_64)(0xff00000000000000)) >> 56);
}
//float
float aep_htonf(float source)
{
if(AEP_ENDIANNESS == AEP_BIG_ENDIAN)
return source;
else
{
uint_32 t= 0
| ((*(uint_32*)&source & 0x000000ff) << 24)
| ((*(uint_32*)&source & 0x0000ff00) << 8)
| ((*(uint_32*)&source & 0x00ff0000) >> 8)
| ((*(uint_32*)&source & 0xff000000) >> 24);
return *(float*)&t;
}
}
//double
double aep_htond(double source)
{
if(AEP_ENDIANNESS == AEP_BIG_ENDIAN)
return source;
else
{
uint_64 t= 0
| ((*(uint_64*)&source & (uint_64)(0x00000000000000ff)) << 56)
| ((*(uint_64*)&source & (uint_64)(0x000000000000ff00)) << 40)
| ((*(uint_64*)&source & (uint_64)(0x0000000000ff0000)) << 24)
| ((*(uint_64*)&source & (uint_64)(0x00000000ff000000)) << 8)
| ((*(uint_64*)&source & (uint_64)(0x000000ff00000000)) >> 8)
| ((*(uint_64*)&source & (uint_64)(0x0000ff0000000000)) >> 24)
| ((*(uint_64*)&source & (uint_64)(0x00ff000000000000)) >> 40)
| ((*(uint_64*)&source & (uint_64)(0xff00000000000000)) >> 56);
return *(double*)&t;
}
}
//16进制转字符串
void HexToStr(char *pbDest, char *pbSrc, int nLen)
{
unsigned char ddl,ddh;
int i;
for (i=0; i<nLen; i++)
{
ddh = 48 + (unsigned char)pbSrc[i] / 16;
ddl = 48 + (unsigned char)pbSrc[i] % 16;
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest[i*2] = ddh;
pbDest[i*2+1] = ddl;
}
//pbDest[nLen*2] = '\0';
}
//字符串转16进制
void StrToHex(char *pbDest, char *pbSrc, int nLen)
{
unsigned char h1,h2;
unsigned char s1,s2;
int i;
for (i=0; i<nLen; i++)
{
h1 = pbSrc[2*i];
h2 = pbSrc[2*i+1];
s1 = toupper(h1) - 0x30;
if (s1 > 9)
s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9)
s2 -= 7;
pbDest[i] = s1*16 + s2;
}
}
//数据上报:事件上报
AepString Event_report_CodeDataReport (Event_report srcStruct)
{
char* index;
AepString resultStruct;
//unsigned short tempLen;
unsigned short payloadLen = 31;
//resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.len = (payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
//memcpy(index, "02", 2);
//index += 1 * 2;
//tempLen = aep_htons(11);//服务ID
//HexToStr(index, (char *)&tempLen, 2);
//index += 2 * 2;
//tempLen = aep_htons(payloadLen);
//HexToStr(index, (char *)&tempLen, 2);
//index += 2 * 2;
HexToStr(index, (char *)srcStruct.head, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.protocol_version, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.command, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.data_length, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.device_type, 1);
index += 1 * 2;
HexToStr(index, (char *)srcStruct.Imei, 15);
index += 15 * 2;
HexToStr(index, (char *)&srcStruct.event_type, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.door_state, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.battery_vol, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.CSQ, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.reserve, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.reserve2, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.reserve3, 1);
index += 1 * 2;
HexToStr(index, (char *)srcStruct.Tail, 3);
index += 3 * 2;
return resultStruct;
}
//事件上报:门磁报警
AepString alarm_type_report_CodeEventReport (alarm_type_report srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 1;
resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
memcpy(index, "07", 2);
index += 1 * 2;
tempLen = aep_htons(1001);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.alarm_type, 1);
index += 1 * 2;
return resultStruct;
}
//指令下发:下发回复
int command_resp_DecodeCmdDown (char* source, command_resp* dest)
{
char* index = source;
int srcStrLen = strlen(source);
int len = 27;
memset(dest, 0, sizeof(command_resp));
StrToHex(dest->head, index, 2);
index += 2 * 2;
StrToHex((char *)&dest->protocol_version, index, 1);
index += 1 * 2;
StrToHex(dest->Imei, index, 15);
index += 15 * 2;
StrToHex((char *)&dest->command, index, 1);
index += 1 * 2;
StrToHex((char *)&dest->data_length, index, 1);
index += 1 * 2;
StrToHex((char *)&dest->cmmand_type, index, 1);
index += 1 * 2;
StrToHex((char *)&dest->result, index, 1);
index += 1 * 2;
StrToHex((char *)&dest->option_data, index, 2);
dest->option_data = aep_htons(dest->option_data);
index += 2 * 2;
StrToHex(dest->Tail, index, 3);
index += 3 * 2;
if (len * 2 > srcStrLen)
{
return AEP_CMD_PAYLOAD_PARSING_FAILED;
}
return AEP_CMD_SUCCESS;
}
//数据上报:业务数据上报
AepString data_report_CodeDataReport (data_report srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 1;
resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
memcpy(index, "02", 2);
index += 1 * 2;
tempLen = aep_htons(1);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.open_state, 1);
index += 1 * 2;
return resultStruct;
}
//数据上报:设备注册
AepString device_register_CodeDataReport (device_register srcStruct)
{
char* index;
AepString resultStruct;
//unsigned short tempLen;
unsigned short payloadLen = 61;
resultStruct.len = (payloadLen) * 2;//resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
//memcpy(index, "02", 2);
//index += 1 * 2;
//tempLen = aep_htons(10);//服务ID
//HexToStr(index, (char *)&tempLen, 2);
//index += 2 * 2;
//tempLen = aep_htons(payloadLen);
//HexToStr(index, (char *)&tempLen, 2);
//index += 2 * 2;
HexToStr(index, (char *)srcStruct.head, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.protocol_version, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.command, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.data_length, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.device_type, 1);
index += 1 * 2;
HexToStr(index, (char *)srcStruct.Imei, 15);
index += 15 * 2;
HexToStr(index, (char *)srcStruct.Imsi, 15);
index += 15 * 2;
HexToStr(index, (char *)srcStruct.ICCID, 20);
index += 20 * 2;
HexToStr(index, (char *)&srcStruct.battery_vol, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.CSQ, 1);
index += 1 * 2;
HexToStr(index, (char *)srcStruct.Tail, 3);
index += 3 * 2;
return resultStruct;
}
//事件上报:开关门事件上报
AepString event_report_CodeEventReport (event_report srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 10;
resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
srcStruct.battery_voltage = aep_htonf(srcStruct.battery_voltage);
srcStruct.battery_value = aep_htoni(srcStruct.battery_value);
index = resultStruct.str;
memcpy(index, "07", 2);
index += 1 * 2;
tempLen = aep_htons(1002);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.open_state, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.arming_state, 1);
index += 1 * 2;
HexToStr(index, (char *)&srcStruct.battery_voltage, 4);
index += 4 * 2;
HexToStr(index, (char *)&srcStruct.battery_value, 4);
index += 4 * 2;
return resultStruct;
}
//数据上报:心跳
AepString heartbeat_CodeDataReport (heartbeat srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 12 + (srcStruct.manufacturer_name.len + 2) + (srcStruct.terminal_type.len + 2) + (srcStruct.hardware_version.len + 2) + (srcStruct.software_version.len + 2) + (srcStruct.IMEI.len + 2) + (srcStruct.IMSI.len + 2);
resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
srcStruct.heartbeat_time = aep_htonf(srcStruct.heartbeat_time);
srcStruct.battery_value = aep_htoni(srcStruct.battery_value);
srcStruct.battery_voltage = aep_htonf(srcStruct.battery_voltage);
index = resultStruct.str;
memcpy(index, "02", 2);
index += 1 * 2;
tempLen = aep_htons(3);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.heartbeat_time, 4);
index += 4 * 2;
tempLen = aep_htons(srcStruct.manufacturer_name.len);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)srcStruct.manufacturer_name.str, srcStruct.manufacturer_name.len);
index += srcStruct.manufacturer_name.len * 2;
tempLen = aep_htons(srcStruct.terminal_type.len);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)srcStruct.terminal_type.str, srcStruct.terminal_type.len);
index += srcStruct.terminal_type.len * 2;
tempLen = aep_htons(srcStruct.hardware_version.len);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)srcStruct.hardware_version.str, srcStruct.hardware_version.len);
index += srcStruct.hardware_version.len * 2;
tempLen = aep_htons(srcStruct.software_version.len);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)srcStruct.software_version.str, srcStruct.software_version.len);
index += srcStruct.software_version.len * 2;
tempLen = aep_htons(srcStruct.IMEI.len);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)srcStruct.IMEI.str, srcStruct.IMEI.len);
index += srcStruct.IMEI.len * 2;
tempLen = aep_htons(srcStruct.IMSI.len);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)srcStruct.IMSI.str, srcStruct.IMSI.len);
index += srcStruct.IMSI.len * 2;
HexToStr(index, (char *)&srcStruct.battery_value, 4);
index += 4 * 2;
HexToStr(index, (char *)&srcStruct.battery_voltage, 4);
index += 4 * 2;
return resultStruct;
}
//指令下发:布防设置
int set_arming_state_DecodeCmdDown (char* source, set_arming_state* dest)
{
char* index = source;
int srcStrLen = strlen(source);
int len = 1;
memset(dest, 0, sizeof(set_arming_state));
StrToHex((char *)&dest->arming_state, index, 1);
index += 1 * 2;
if (len * 2 > srcStrLen)
{
return AEP_CMD_PAYLOAD_PARSING_FAILED;
}
return AEP_CMD_SUCCESS;
}
//指令下发响应:布防设置响应
AepString set_arming_state_resp_CodeCmdResponse (set_arming_state_resp srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 1;
resultStruct.len = (1 + 2 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
memcpy(index, "86", 2);
index += 1 * 2;
tempLen = aep_htons(9001);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(srcStruct.taskId);//taskID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.arming_state, 1);
index += 1 * 2;
return resultStruct;
}
//指令下发:声光报警设置
int set_audible_alarm_DecodeCmdDown (char* source, set_audible_alarm* dest)
{
char* index = source;
int srcStrLen = strlen(source);
int len = 1;
memset(dest, 0, sizeof(set_audible_alarm));
StrToHex((char *)&dest->audible_alarm, index, 1);
index += 1 * 2;
if (len * 2 > srcStrLen)
{
return AEP_CMD_PAYLOAD_PARSING_FAILED;
}
return AEP_CMD_SUCCESS;
}
//指令下发响应:声光报警设置响应
AepString set_audible_alarm_resp_CodeCmdResponse (set_audible_alarm_resp srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 1;
resultStruct.len = (1 + 2 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
memcpy(index, "86", 2);
index += 1 * 2;
tempLen = aep_htons(9003);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(srcStruct.taskId);//taskID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.audible_alarm, 1);
index += 1 * 2;
return resultStruct;
}
//指令下发:防拆设置
int set_tamper_DecodeCmdDown (char* source, set_tamper* dest)
{
char* index = source;
int srcStrLen = strlen(source);
int len = 1;
memset(dest, 0, sizeof(set_tamper));
StrToHex((char *)&dest->tamper, index, 1);
index += 1 * 2;
if (len * 2 > srcStrLen)
{
return AEP_CMD_PAYLOAD_PARSING_FAILED;
}
return AEP_CMD_SUCCESS;
}
//指令下发响应:防拆设置响应
AepString set_tamper_resp_CodeCmdResponse (set_tamper_resp srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 1;
resultStruct.len = (1 + 2 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
index = resultStruct.str;
memcpy(index, "86", 2);
index += 1 * 2;
tempLen = aep_htons(9002);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(srcStruct.taskId);//taskID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.tamper, 1);
index += 1 * 2;
return resultStruct;
}
//数据上报:信号数据上报
AepString signal_report_CodeDataReport (signal_report srcStruct)
{
char* index;
AepString resultStruct;
unsigned short tempLen;
unsigned short payloadLen = 20;
resultStruct.len = (1 + 2 + 2 + payloadLen) * 2;
resultStruct.str = (char *)malloc(resultStruct.len + 1);
memset(resultStruct.str, 0, resultStruct.len + 1);
srcStruct.rsrp = aep_htoni(srcStruct.rsrp);
srcStruct.sinr = aep_htoni(srcStruct.sinr);
srcStruct.pci = aep_htoni(srcStruct.pci);
srcStruct.ecl = aep_htoni(srcStruct.ecl);
srcStruct.cell_id = aep_htoni(srcStruct.cell_id);
index = resultStruct.str;
memcpy(index, "02", 2);
index += 1 * 2;
tempLen = aep_htons(2);//服务ID
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
tempLen = aep_htons(payloadLen);
HexToStr(index, (char *)&tempLen, 2);
index += 2 * 2;
HexToStr(index, (char *)&srcStruct.rsrp, 4);
index += 4 * 2;
HexToStr(index, (char *)&srcStruct.sinr, 4);
index += 4 * 2;
HexToStr(index, (char *)&srcStruct.pci, 4);
index += 4 * 2;
HexToStr(index, (char *)&srcStruct.ecl, 4);
index += 4 * 2;
HexToStr(index, (char *)&srcStruct.cell_id, 4);
index += 4 * 2;
return resultStruct;
}
AepCmdData decodeCmdDownFromStr(char* source)
{
char* index;
AepCmdData result;
char cmdType;
unsigned short serviceId;
unsigned short payloadLen;
memset(&result, 0, sizeof(AepCmdData));
index = source;
//解析指令类型
StrToHex(&cmdType, index, 1);
index += 1 * 2;
if (cmdType != 0x06)
{
result.code = AEP_CMD_INVALID_DATASET_TYPE;
}
//服务Id解析
StrToHex((char *)&serviceId, index, 2);
serviceId = aep_htons(serviceId);
index += 2 * 2;
StrToHex((char *)&result.taskId, index, 2);
result.taskId = aep_htons(result.taskId);
index += 2 * 2;
//payload长度解析
StrToHex((char *)&payloadLen, index, 2);
payloadLen = aep_htons(payloadLen);
index += 2 * 2;
if (strlen(index) < payloadLen * 2)
{
result.code = AEP_CMD_PAYLOAD_PARSING_FAILED;
return result;
}
if (serviceId == 8010)
{
result.serviceIdentifier = "command_resp";
result.data = malloc(sizeof(command_resp));
memset(result.data, 0, sizeof(command_resp));
result.code = command_resp_DecodeCmdDown(index, (command_resp*)result.data);
}
else if (serviceId == 8001)
{
result.serviceIdentifier = "set_arming_state";
result.data = malloc(sizeof(set_arming_state));
memset(result.data, 0, sizeof(set_arming_state));
result.code = set_arming_state_DecodeCmdDown(index, (set_arming_state*)result.data);
}
else if (serviceId == 8003)
{
result.serviceIdentifier = "set_audible_alarm";
result.data = malloc(sizeof(set_audible_alarm));
memset(result.data, 0, sizeof(set_audible_alarm));
result.code = set_audible_alarm_DecodeCmdDown(index, (set_audible_alarm*)result.data);
}
else if (serviceId == 8002)
{
result.serviceIdentifier = "set_tamper";
result.data = malloc(sizeof(set_tamper));
memset(result.data, 0, sizeof(set_tamper));
result.code = set_tamper_DecodeCmdDown(index, (set_tamper*)result.data);
}
else
{
result.serviceIdentifier = NULL;
result.data = malloc(payloadLen);
memset(result.data, 0, sizeof(payloadLen));
StrToHex((char *)result.data, index, payloadLen);
result.code = AEP_CMD_INVALID_DATASET_IDENTIFIER;
}
return result;
}
AepCmdData decodeCmdDownFromBytes(char* source, int len)
{
char * str = malloc(len * 2 + 1);
AepCmdData result;
HexToStr(str, source, len);
str[len * 2] = 0;
result = decodeCmdDownFromStr(str);
free(str);
return result;
}
AepString codeDataReportByIdToStr (int serviceId, void * srcStruct)
{
if (serviceId == 11)
{
return Event_report_CodeDataReport(*(Event_report*)srcStruct);
}
else if (serviceId == 1001)
{
return alarm_type_report_CodeEventReport(*(alarm_type_report*)srcStruct);
}
else if (serviceId == 1)
{
return data_report_CodeDataReport(*(data_report*)srcStruct);
}
else if (serviceId == 10)
{
return device_register_CodeDataReport(*(device_register*)srcStruct);
}
else if (serviceId == 1002)
{
return event_report_CodeEventReport(*(event_report*)srcStruct);
}
else if (serviceId == 3)
{
return heartbeat_CodeDataReport(*(heartbeat*)srcStruct);
}
else if (serviceId == 9001)
{
return set_arming_state_resp_CodeCmdResponse(*(set_arming_state_resp*)srcStruct);
}
else if (serviceId == 9003)
{
return set_audible_alarm_resp_CodeCmdResponse(*(set_audible_alarm_resp*)srcStruct);
}
else if (serviceId == 9002)
{
return set_tamper_resp_CodeCmdResponse(*(set_tamper_resp*)srcStruct);
}
else if (serviceId == 2)
{
return signal_report_CodeDataReport(*(signal_report*)srcStruct);
}
else
{
AepString result = {0};
return result;
}
}
AepBytes codeDataReportByIdToBytes(int serviceId, void * srcStruct)
{
AepString temp = codeDataReportByIdToStr(serviceId, srcStruct);
AepBytes result = {0};
result.len = temp.len / 2;
if (result.len > 0)
{
result.str = malloc(result.len);
StrToHex(result.str, temp.str, result.len);
free(temp.str);
}
return result;
}
AepString codeDataReportByIdentifierToStr (char* serviceIdentifier, void * srcStruct)
{
if (strcmp(serviceIdentifier, "Event_report") == 0)
{
return Event_report_CodeDataReport(*(Event_report*)srcStruct);
}
else if (strcmp(serviceIdentifier, "alarm_type_report") == 0)
{
return alarm_type_report_CodeEventReport(*(alarm_type_report*)srcStruct);
}
else if (strcmp(serviceIdentifier, "data_report") == 0)
{
return data_report_CodeDataReport(*(data_report*)srcStruct);
}
else if (strcmp(serviceIdentifier, "device_register") == 0)
{
return device_register_CodeDataReport(*(device_register*)srcStruct);
}
else if (strcmp(serviceIdentifier, "event_report") == 0)
{
return event_report_CodeEventReport(*(event_report*)srcStruct);
}
else if (strcmp(serviceIdentifier, "heartbeat") == 0)
{
return heartbeat_CodeDataReport(*(heartbeat*)srcStruct);
}
else if (strcmp(serviceIdentifier, "set_arming_state_resp") == 0)
{
return set_arming_state_resp_CodeCmdResponse(*(set_arming_state_resp*)srcStruct);
}
else if (strcmp(serviceIdentifier, "set_audible_alarm_resp") == 0)
{
return set_audible_alarm_resp_CodeCmdResponse(*(set_audible_alarm_resp*)srcStruct);
}
else if (strcmp(serviceIdentifier, "set_tamper_resp") == 0)
{
return set_tamper_resp_CodeCmdResponse(*(set_tamper_resp*)srcStruct);
}
else if (strcmp(serviceIdentifier, "signal_report") == 0)
{
return signal_report_CodeDataReport(*(signal_report*)srcStruct);
}
else
{
AepString result = {0};
return result;
}
}
AepBytes codeDataReportByIdentifierToBytes(char* serviceIdentifier, void * srcStruct)
{
AepString temp = codeDataReportByIdentifierToStr(serviceIdentifier, srcStruct);
AepBytes result = {0};
result.len = temp.len / 2;
if (result.len > 0)
{
result.str = malloc(result.len);
StrToHex(result.str, temp.str, result.len);
free(temp.str);
}
return result;
}
/*************************************.h**************************************************************************/
/***
*AepServiceCodes.h - 定义上下行数据的结构体,还有提供组装上行报文的函数和解析下行报文的函数
*
*Purpose:
* 1.数据结构体命名和平台定义的服务标识一致
* 2.codeDataReportByIdToStr、codeDataReportByIdToBytes、codeDataReportByIdentifierToStr、codeDataReportByIdentifierToBytes为组装上报数据的函数,具体说明见函数前的注释
* 3.decodeCmdDownFromStr、decodeCmdDownFromBytes为解析平台发送过来数据的函数,具体说明见函数前的注释
****/
#ifndef AEPSERVICECODES_H
#define AEPSERVICECODES_H
#include <stdlib.h>
#include <string.h>
#define AEP_BIG_ENDIAN 'b'
#define AEP_LITTLE_ENDIAN 'l'
static union { char c[4]; unsigned long mylong; } endian_test = {{ 'l', '?', '?', 'b' } };
#define AEP_ENDIANNESS ((char)endian_test.mylong)
typedef unsigned long long uint_64;
typedef unsigned int uint_32;
typedef unsigned short uint_16;
//命令解析响应码
#define AEP_CMD_SUCCESS 0 //执行成功
#define AEP_CMD_FAILED 1 //执行失败
#define AEP_CMD_INVALID_DATASET_TYPE 2 //无效数据集类型
#define AEP_CMD_INVALID_DATASET_IDENTIFIER 3 //无效数据集标识
#define AEP_CMD_PAYLOAD_PARSING_FAILED 4 //指令数据集Payload解析失败,紧凑二进制编码内容长度不符等
typedef struct AepStrStruct
{
unsigned short len;
char* str;
} AepString;
typedef AepString AepBytes;
//无符号整型16位
uint_16 aep_htons(uint_16 source);
//无符号整型32位
uint_32 aep_htoni(uint_32 source);
//无符号整型64位
uint_64 aep_htonl(uint_64 source);
//float
float aep_htonf(float source);
//double
double aep_htond(double source);
//16进制转字符串
void HexToStr(char *pbDest, char *pbSrc, int nLen);
//字符串转16进制
void StrToHex(char *pbDest, char *pbSrc, int nLen);
//根据服务id生成上报数据的十六进制字符串,srcStruct需要根据服务定义传入对应的类型,返回结果为字符串
AepString codeDataReportByIdToStr(int serviceId, void * srcStruct);
//根据服务id生成上报数据的字节流,srcStruct需要根据服务定义传入对应的类型,返回结果为字节流
AepBytes codeDataReportByIdToBytes(int serviceId, void * srcStruct);
//根据服务标识生成上报数据的十六进制字符串,srcStruct需要根据服务定义传入对应的类型,返回结果为字符串
AepString codeDataReportByIdentifierToStr(char * serviceIdentifier, void * srcStruct);
//根据服务标识生成上报数据的字节流,srcStruct需要根据服务定义传入对应的类型,返回结果为字节流
AepBytes codeDataReportByIdentifierToBytes(char * serviceIdentifier, void * srcStruct);
//指令解析返回结构体,data在使用时需要根据serviceId强转为对应类型
typedef struct CmdStruct
{
char* serviceIdentifier;
unsigned short taskId;
void * data;
int code;
} AepCmdData;
//解析接受到的报文数据,入参为十六进制字符串
AepCmdData decodeCmdDownFromStr(char* source);
//解析接受到的报文数据,入参为原始字节流
AepCmdData decodeCmdDownFromBytes(char* source, int len);
typedef struct Event_reportStruct
{
char head[2];
unsigned char protocol_version;
char command;
unsigned char data_length;
char device_type;
char Imei[15];
char event_type;
char door_state;
unsigned char battery_vol;
unsigned char CSQ;
unsigned char reserve;
unsigned char reserve2;
unsigned char reserve3;
char Tail[3];
} Event_report;
//数据上报:事件上报
AepString Event_report_CodeDataReport (Event_report srcStruct);
typedef struct alarm_type_reportStruct
{
char alarm_type;
} alarm_type_report;
//事件上报:门磁报警
AepString alarm_type_report_CodeEventReport (alarm_type_report srcStruct);
typedef struct command_respStruct
{
char head[2];
unsigned char protocol_version;
char Imei[15];
char command;
unsigned char data_length;
char cmmand_type;
char result;
unsigned short option_data;
char Tail[3];
} command_resp;
//指令下发:下发回复
int command_resp_DecodeCmdDown (char* source, command_resp* dest);
typedef struct data_reportStruct
{
char open_state;
} data_report;
//数据上报:业务数据上报
AepString data_report_CodeDataReport (data_report srcStruct);
typedef struct device_registerStruct
{
char head[2];
unsigned char protocol_version;
char command;
unsigned char data_length;
char device_type;
char Imei[15];
char Imsi[15];
char ICCID[20];
unsigned char battery_vol;
unsigned char CSQ;
char Tail[3];
} device_register;
//数据上报:设备注册
AepString device_register_CodeDataReport (device_register srcStruct);
typedef struct event_reportStruct
{
char open_state;
char arming_state;
float battery_voltage;
int battery_value;
} event_report;
//事件上报:开关门事件上报
AepString event_report_CodeEventReport (event_report srcStruct);
typedef struct heartbeatStruct
{
float heartbeat_time;
AepString manufacturer_name;
AepString terminal_type;
AepString hardware_version;
AepString software_version;
AepString IMEI;
AepString IMSI;
int battery_value;
float battery_voltage;
} heartbeat;
//数据上报:心跳
AepString heartbeat_CodeDataReport (heartbeat srcStruct);
typedef struct set_arming_stateStruct
{
char arming_state;
} set_arming_state;
//指令下发:布防设置
int set_arming_state_DecodeCmdDown (char* source, set_arming_state* dest);
typedef struct set_arming_state_respStruct
{
unsigned short taskId;
char arming_state;
} set_arming_state_resp;
//指令下发响应:布防设置响应
AepString set_arming_state_resp_CodeCmdResponse (set_arming_state_resp srcStruct);
typedef struct set_audible_alarmStruct
{
char audible_alarm;
} set_audible_alarm;
//指令下发:声光报警设置
int set_audible_alarm_DecodeCmdDown (char* source, set_audible_alarm* dest);
typedef struct set_audible_alarm_respStruct
{
unsigned short taskId;
char audible_alarm;
} set_audible_alarm_resp;
//指令下发响应:声光报警设置响应
AepString set_audible_alarm_resp_CodeCmdResponse (set_audible_alarm_resp srcStruct);
typedef struct set_tamperStruct
{
char tamper;
} set_tamper;
//指令下发:防拆设置
int set_tamper_DecodeCmdDown (char* source, set_tamper* dest);
typedef struct set_tamper_respStruct
{
unsigned short taskId;
char tamper;
} set_tamper_resp;
//指令下发响应:防拆设置响应
AepString set_tamper_resp_CodeCmdResponse (set_tamper_resp srcStruct);
typedef struct signal_reportStruct
{
int rsrp;
int sinr;
int pci;
int ecl;
int cell_id;
} signal_report;
//数据上报:信号数据上报
AepString signal_report_CodeDataReport (signal_report srcStruct);
#endif