184 lines
6.3 KiB
C
184 lines
6.3 KiB
C
#include "jt808_msg_parse.h"
|
|
#include "jt808_pkg_transmit.h"
|
|
|
|
PrsResult_t PrsResult;
|
|
|
|
// 消息体解析
|
|
static int jt808_BodyParse(void *Prsmsg_body, PrsResult_t *PrsResult){
|
|
switch (PrsResult->msg_head.msg_id){
|
|
case ID_Plat_GenResp:{// 平台通用应答
|
|
memcpy(&(PrsResult->Rsp_flow_num), Prsmsg_body, sizeof(Plat_GenResp_t));
|
|
// 转小端
|
|
PrsResult->Rsp_flow_num = Swap16(PrsResult->Rsp_flow_num);
|
|
PrsResult->Rsp_msg_id = Swap16(PrsResult->Rsp_msg_id);
|
|
// PrsResult->Rsp_result = PrsResult->Rsp_result;
|
|
break;
|
|
}
|
|
case ID_FillPktReq:{// 补传分包请求
|
|
|
|
break;
|
|
}
|
|
case ID_Term_RegResp:{// 终端注册应答
|
|
PrsResult->Rsp_flow_num = (uint16_t)(((uint8_t *)Prsmsg_body)[0] << 8) | (((uint8_t *)Prsmsg_body)[1]);
|
|
PrsResult->Rsp_result = ((uint8_t *)Prsmsg_body)[2];
|
|
if(PrsResult->Rsp_result == 0){ // 成功时,读取鉴权码
|
|
if(PrsResult->term_param_item->big_auth_info.str_auth_code != NULL){
|
|
jt808_free(PrsResult->term_param_item->big_auth_info.str_auth_code);
|
|
PrsResult->term_param_item->big_auth_info.str_auth_code = NULL;
|
|
}
|
|
PrsResult->term_param_item->big_auth_info.str_auth_code = (char *)jt808_malloc(PrsResult->msg_head.msgbody_attr.msgbodylen - 3 +1); // 加1是为了加上结束符
|
|
if(PrsResult->term_param_item->big_auth_info.str_auth_code == NULL){
|
|
JT808_DEBUG("[%s,%s] malloc failed\r\n", __FUNCTION__,__LINE__);
|
|
return -1;
|
|
}
|
|
memset(PrsResult->term_param_item->big_auth_info.str_auth_code, 0, PrsResult->msg_head.msgbody_attr.msgbodylen - 3 +1);
|
|
memcpy(PrsResult->term_param_item->big_auth_info.str_auth_code, Prsmsg_body + 3 , PrsResult->msg_head.msgbody_attr.msgbodylen - 3);
|
|
}
|
|
break;
|
|
}
|
|
case ID_SetTermParams:{// 设置终端参数
|
|
PrsResult->term_param_item->big_specific_params.param_Total_num = ((uint8_t *)Prsmsg_body)[0]; // 总参数个数
|
|
uint16_t param_lenoffset = 1;
|
|
for(int i = 0; i < PrsResult->term_param_item->big_specific_params.param_Total_num; i++){
|
|
// 设置终端参数
|
|
jt808_setTermParam(Swap32(*(uint32_t *)(Prsmsg_body + param_lenoffset)), // 参数ID
|
|
(void *)(Prsmsg_body + param_lenoffset + 5), // 参数值
|
|
*((uint8_t*)(Prsmsg_body + param_lenoffset + 4))); // 参数长度
|
|
param_lenoffset += 5 + (*((uint8_t*)(Prsmsg_body + param_lenoffset + 4))); // 参数ID(4) + 参数长度(1) + 参数值(n)
|
|
}
|
|
|
|
jt808_pkg_send(ID_Term_GenResp, 0);// 发送终端通用应答
|
|
break;
|
|
}
|
|
case ID_GetTermParams:{// 查询终端参数
|
|
|
|
break;
|
|
}
|
|
case ID_GetSpecificTermParams:{// 查询指定终端参数
|
|
|
|
break;
|
|
}
|
|
case ID_Term_Ctrl:{// 终端控制
|
|
|
|
break;
|
|
}
|
|
case ID_GetTermAttr:{// 查询终端属性
|
|
|
|
break;
|
|
}
|
|
case ID_Term_Upgrade:{// 下发终端升级包
|
|
|
|
break;
|
|
}
|
|
case ID_GetLocInfo:{// 位置信息查询
|
|
|
|
break;
|
|
}
|
|
case ID_LocTrackingCtrl:{// 临时位置跟踪控制
|
|
|
|
break;
|
|
}
|
|
case ID_Car_Ctrl:{// 车辆控制
|
|
|
|
jt808_pkg_send(ID_Car_CtrlResp, 0);// 发送车辆控制应答,设置本消息无发送应答
|
|
break;
|
|
}
|
|
default:{
|
|
return -2; // 没有对应的消息体解析函数
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// jt808协议包处理
|
|
int jt808_msg_parse(const uint8_t *BufferReceive, uint16_t length, PrsResult_t *PrsResult){
|
|
uint8_t *raw_Buffer = (uint8_t *)jt808_malloc(length * sizeof(uint8_t));
|
|
if(raw_Buffer == NULL){
|
|
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
|
return -1;
|
|
}
|
|
memcpy(raw_Buffer, BufferReceive, length);
|
|
|
|
// // 打印原始数据
|
|
// app_printf("raw_Buffer: %d\r\n", length);
|
|
// for(int i = 0; i < length; i++){
|
|
// app_printf("%02x ", raw_Buffer[i]);
|
|
// }
|
|
// app_printf("\r\n");
|
|
|
|
// 计算需要逆转义字符的个数
|
|
uint16_t para_length = 0;
|
|
for(int i = 1; i < (length - 2); i++){
|
|
if(((raw_Buffer[i] == PESC)&&(raw_Buffer[i+1] == PESC_SIGN)) || // 7d 02 转义7e
|
|
((raw_Buffer[i] == PESC)&&(raw_Buffer[i+1] == PESC_ESCAPE))){ // 7d 01 转义7d
|
|
para_length++;
|
|
}
|
|
}
|
|
para_length =length - para_length;
|
|
uint8_t *para_Buffer = (uint8_t *)jt808_malloc(para_length * sizeof(uint8_t));
|
|
if(para_Buffer == NULL){
|
|
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
|
jt808_free(raw_Buffer);
|
|
return -1;
|
|
}
|
|
para_Buffer[0] = PSIGN;
|
|
para_Buffer[para_length - 1] = PSIGN;
|
|
|
|
// 逆转义
|
|
uint16_t offset_num = 0;
|
|
for(int i = 1; i < (para_length - 1); i++){
|
|
if(((raw_Buffer[i + offset_num] == PESC)&&(raw_Buffer[i + offset_num + 1] == PESC_SIGN))){ // 7d 02 转义7e
|
|
para_Buffer[i] = PSIGN;
|
|
offset_num++;
|
|
}else if(((raw_Buffer[i + offset_num] == PESC)&&(raw_Buffer[i + offset_num + 1] == PESC_ESCAPE))){ // 7d 01 转义7d
|
|
para_Buffer[i] = PESC;
|
|
offset_num++;
|
|
}else{
|
|
para_Buffer[i] =raw_Buffer[i + offset_num];
|
|
}
|
|
}
|
|
// 释放内存
|
|
jt808_free(raw_Buffer);
|
|
|
|
if(offset_num != length - para_length){ // 转义后长度有误
|
|
JT808_DEBUG("error offset_num != length - para_length\r\n");
|
|
jt808_free(para_Buffer);
|
|
return -1;
|
|
}
|
|
|
|
// // 打印逆转义后数据
|
|
// app_printf("para_Buffer: %d\r\n", para_length);
|
|
// for(int i = 0; i < para_length; i++){
|
|
// app_printf("%02x ", para_Buffer[i]);
|
|
// }
|
|
// app_printf("\r\n");
|
|
|
|
// 异或校验
|
|
if(para_Buffer[para_length - 2] != BCC_Check(para_Buffer + 1, para_length - 2 - 1)){
|
|
JT808_DEBUG("BCC_CheckSum ERROR: %x %x\r\n",para_Buffer[para_length - 2] ,BCC_Check(para_Buffer + 1, para_length - 2 - 1));
|
|
jt808_free(para_Buffer);
|
|
return -1;
|
|
}
|
|
|
|
// 解析消息头
|
|
memcpy(&PrsResult->msg_head, para_Buffer + 1, sizeof(MsgHead_t) - ((para_Buffer[3] & 0x02)==0? 4 : 0));
|
|
PrsResult->msg_head.msg_id = Swap16(PrsResult->msg_head.msg_id);// 消息ID
|
|
PrsResult->msg_head.msgbody_attr.val16 = Swap16(PrsResult->msg_head.msgbody_attr.val16);// 消息体属性
|
|
PrsResult->msg_head.msg_flow_num = Swap16(PrsResult->msg_head.msg_flow_num);// 消息流水号
|
|
PrsResult->msg_head.total_packet = Swap16(PrsResult->msg_head.total_packet);// 总包数, 分包情况下使用
|
|
PrsResult->msg_head.packet_seq = Swap16(PrsResult->msg_head.packet_seq);// 当前包序号, 分包情况下使用
|
|
|
|
// 消息体解析
|
|
if(0 != jt808_BodyParse((void *)(para_Buffer + 1 + sizeof(MsgHead_t) - ((para_Buffer[3] & 0x02)==0? 4 : 0)) ,PrsResult)){
|
|
JT808_DEBUG("error jt808_BodyParse\r\n");
|
|
// 释放内存
|
|
jt808_free(para_Buffer);
|
|
return -1;
|
|
}
|
|
|
|
// 释放内存
|
|
jt808_free(para_Buffer);
|
|
return 0;
|
|
}
|