308 lines
16 KiB
C
308 lines
16 KiB
C
#include "jt808_msg_pkg.h"
|
||
#include "jt808_msg_parse.h"
|
||
|
||
// 协议消息体打包(会自动为p_msg_body分配合适长度的内存,注意释放)
|
||
static int jt808_BodyPackage(JT808_2013_MsgFrame_t *p_MsgFrame, MessageID_t Msg_ID){
|
||
if(PrsResult.term_param_item == NULL){
|
||
JT808_DEBUG("[%s,%s] term_param_item is NULL \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = 0;// 消息体长度
|
||
p_MsgFrame->msg_head.msgbody_attr.encrypt = 0;// 数据加密方式
|
||
p_MsgFrame->msg_head.msgbody_attr.packet = 0;// 分包标记
|
||
p_MsgFrame->msg_head.msgbody_attr.retain = 0;// 保留2位
|
||
|
||
p_MsgFrame->msg_head.total_packet = 0;// 总包数, 分包情况下使用
|
||
p_MsgFrame->msg_head.packet_seq = 0;// 当前包序号, 分包情况下使用
|
||
// 填充消息体
|
||
switch(Msg_ID){
|
||
case ID_Term_GenResp:{ // 终端通用应答
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(Term_GenResp_t);// 消息体长度
|
||
|
||
// 定义大端结构体
|
||
Term_GenResp_t *big_term_reg_resp = (Term_GenResp_t *)jt808_malloc(sizeof(Term_GenResp_t));
|
||
if(big_term_reg_resp == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
memset(big_term_reg_resp, 0, sizeof(Term_GenResp_t));
|
||
// 大小端转换
|
||
big_term_reg_resp->msg_flow_num = Swap16(PrsResult.Rsp_flow_num); // 返回消息应答结果
|
||
big_term_reg_resp->msg_id_ack = Swap16(PrsResult.Rsp_msg_id);
|
||
big_term_reg_resp->result = PrsResult.Rsp_result;
|
||
p_MsgFrame->p_msg_body = (void *)big_term_reg_resp;
|
||
break;
|
||
}
|
||
case ID_Term_HB:{ // 终端心跳
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = 0;// 消息体长度
|
||
break;
|
||
}
|
||
case ID_Term_Reg:{ // 终端注册
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(Term_RegInfo_t);// 消息体长度
|
||
|
||
// 定义大端结构体
|
||
Term_RegInfo_t *big_term_reg = (Term_RegInfo_t *)jt808_malloc(sizeof(Term_RegInfo_t));
|
||
if(big_term_reg == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
// memset(big_term_reg, 0, sizeof(Term_RegInfo_t));
|
||
memcpy(big_term_reg, &PrsResult.term_param_item->big_reg_info, sizeof(Term_RegInfo_t)); // 复制注册信息内容
|
||
p_MsgFrame->p_msg_body = (void *)big_term_reg;
|
||
break;
|
||
}
|
||
case ID_Term_Logout:{ // 终端注销
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = 0;// 消息体长度
|
||
break;
|
||
}
|
||
case ID_Term_Auth:{ // 终端鉴权
|
||
// JT808_DEBUG("ID_Term_Auth\r\n");
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = strlen(PrsResult.term_param_item->big_auth_info.str_auth_code);// 消息体长度
|
||
if((NULL == PrsResult.term_param_item->big_auth_info.str_auth_code) || (1 >= p_MsgFrame->msg_head.msgbody_attr.msgbodylen)){
|
||
JT808_DEBUG("[%s,%s] auth_code len is 0 \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
void *str_auth_code = jt808_malloc(p_MsgFrame->msg_head.msgbody_attr.msgbodylen);
|
||
if(str_auth_code == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
// memset(str_auth_code, 0, p_MsgFrame->msg_head.msgbody_attr.msgbodylen);
|
||
JT808_DEBUG("auth_code:%d,%s\r\n",strlen(PrsResult.term_param_item->big_auth_info.str_auth_code),
|
||
PrsResult.term_param_item->big_auth_info.str_auth_code);
|
||
memcpy(str_auth_code, PrsResult.term_param_item->big_auth_info.str_auth_code, p_MsgFrame->msg_head.msgbody_attr.msgbodylen);
|
||
p_MsgFrame->p_msg_body = (void *)str_auth_code;
|
||
break;
|
||
}
|
||
case ID_GetTermParamsResp:{ // 查询终端参数应答
|
||
// p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(GetTermParamsResp_t);// 消息体长度
|
||
// GetTermParamsResp_t *get_term_params_resp = (GetTermParamsResp_t *)jt808_realloc(p_MsgFrame->p_msg_body, sizeof(GetTermParamsResp_t));
|
||
// if(get_term_params_resp == NULL){
|
||
// JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
// return -1;
|
||
// }
|
||
// p_MsgFrame->p_msg_body = (void *)get_term_params_resp;
|
||
break;
|
||
}
|
||
case ID_GetTermAttrResp:{ // 查询终端属性应答
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(GetTermAttrResp_t);// 消息体长度
|
||
GetTermAttrResp_t *get_term_attr_resp = (GetTermAttrResp_t *)jt808_malloc(p_MsgFrame->msg_head.msgbody_attr.msgbodylen);
|
||
if(get_term_attr_resp == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
memcpy(get_term_attr_resp, &PrsResult.term_param_item->big_term_attr_resp, sizeof(GetTermAttrResp_t)); // 复制查询终端属性应答内容
|
||
p_MsgFrame->p_msg_body = (void *)get_term_attr_resp;
|
||
break;
|
||
}
|
||
case ID_Term_UpgradeResult:{ // 终端升级结果通知
|
||
// p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(Term_UpgradeResult_t);// 消息体长度
|
||
// Term_UpgradeResult_t *term_upgrade_result = (Term_UpgradeResult_t *)jt808_realloc(p_MsgFrame->p_msg_body, sizeof(Term_UpgradeResult_t));
|
||
// if(term_upgrade_result == NULL){
|
||
// JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
// return -1;
|
||
// }
|
||
// p_MsgFrame->p_msg_body = (void *)term_upgrade_result;
|
||
break;
|
||
}
|
||
case ID_LocReport:{ // 位置信息汇报
|
||
jt808_LocReport_param_update(); // 更新位置信息参数
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(Loc_basic_info_t);// 消息体长度
|
||
Loc_addi_info_t *p_loc_addi_info = PrsResult.term_param_item->big_loc_report.addi_info;
|
||
while(p_loc_addi_info != NULL){ // 计算附加信息长度
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen += p_loc_addi_info->msg_len + 2;
|
||
p_loc_addi_info = p_loc_addi_info->next;
|
||
}
|
||
uint8_t *loc_report_buf = (uint8_t *)jt808_malloc(p_MsgFrame->msg_head.msgbody_attr.msgbodylen);
|
||
if(loc_report_buf == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
memcpy(loc_report_buf, &PrsResult.term_param_item->big_loc_report, sizeof(Loc_basic_info_t)); // 复制基本信息内容
|
||
p_loc_addi_info = PrsResult.term_param_item->big_loc_report.addi_info;
|
||
uint16_t loc_report_offset = sizeof(Loc_basic_info_t);
|
||
uint8_t loc_len = 0;
|
||
while(p_loc_addi_info != NULL){ // 填充附加信息内容
|
||
loc_len= p_loc_addi_info->msg_len + 2;
|
||
memcpy(loc_report_buf + loc_report_offset, &p_loc_addi_info->msg_id, 2);
|
||
memcpy(loc_report_buf + loc_report_offset + 2, p_loc_addi_info->msg, p_loc_addi_info->msg_len);
|
||
loc_report_offset += loc_len;
|
||
p_loc_addi_info = p_loc_addi_info->next;
|
||
}
|
||
p_MsgFrame->p_msg_body = (void *)loc_report_buf;
|
||
break;
|
||
}
|
||
case ID_GetLocInfoResp:{ // 查询位置信息应答
|
||
// p_MsgFrame->msg_head.msgbody_attr.msgbodylen = sizeof(GetLocInfoResp_t);// 消息体长度
|
||
// GetLocInfoResp_t *get_loc_info_resp = (GetLocInfoResp_t *)jt808_realloc(p_MsgFrame->p_msg_body, sizeof(GetLocInfoResp_t));
|
||
// if(get_loc_info_resp == NULL){
|
||
// JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
// return -1;
|
||
// }
|
||
// p_MsgFrame->p_msg_body = (void *)get_loc_info_resp;
|
||
break;
|
||
}
|
||
case ID_Car_CtrlResp:{ // 车辆控制应答
|
||
jt808_LocReport_param_update(); // 更新位置信息参数
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen =2 + sizeof(Loc_basic_info_t);// 消息体长度
|
||
Loc_addi_info_t *p_loc_addi_info = PrsResult.term_param_item->big_loc_report.addi_info;
|
||
while(p_loc_addi_info != NULL){ // 计算附加信息长度
|
||
p_MsgFrame->msg_head.msgbody_attr.msgbodylen += p_loc_addi_info->msg_len + 2;
|
||
p_loc_addi_info = p_loc_addi_info->next;
|
||
}
|
||
uint8_t *Car_CtrlResp_buf = (uint8_t *)jt808_malloc(p_MsgFrame->msg_head.msgbody_attr.msgbodylen);
|
||
if(Car_CtrlResp_buf == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
Car_CtrlResp_buf[0] = (PrsResult.msg_head.msg_flow_num >>8) & 0xFF; // 对应的车辆控制消息的流水号
|
||
Car_CtrlResp_buf[1] = PrsResult.msg_head.msg_flow_num & 0xFF; // 对应的车辆控制消息的流水号
|
||
memcpy(Car_CtrlResp_buf + 2, &PrsResult.term_param_item->big_loc_report, sizeof(Loc_basic_info_t)); // 复制基本信息内容
|
||
p_loc_addi_info = PrsResult.term_param_item->big_loc_report.addi_info;
|
||
uint16_t loc_report_offset =2 + sizeof(Loc_basic_info_t);
|
||
uint8_t loc_len = 0;
|
||
while(p_loc_addi_info != NULL){ // 填充附加信息内容
|
||
loc_len= p_loc_addi_info->msg_len + 2;
|
||
memcpy(Car_CtrlResp_buf + loc_report_offset, &p_loc_addi_info->msg_id, 2);
|
||
memcpy(Car_CtrlResp_buf + loc_report_offset + 2, p_loc_addi_info->msg, p_loc_addi_info->msg_len);
|
||
loc_report_offset += loc_len;
|
||
p_loc_addi_info = p_loc_addi_info->next;
|
||
}
|
||
p_MsgFrame->p_msg_body = (void *)Car_CtrlResp_buf;
|
||
break;
|
||
}
|
||
case ID_Data_Up:{// 数据上行透传
|
||
uint8_t *up_SeriaNet_buf = NULL;
|
||
if(NULL != PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data){
|
||
up_SeriaNet_buf = (uint8_t *)jt808_malloc(PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data_Len + 1);
|
||
if(up_SeriaNet_buf == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
return -1;
|
||
}
|
||
up_SeriaNet_buf[0] = PrsResult.term_param_item->big_data_up_SeriaNet.Msg_type;
|
||
memcpy(up_SeriaNet_buf + 1, PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data,
|
||
PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data_Len); // 复制数据内容
|
||
|
||
p_MsgFrame->p_msg_body = (void *)up_SeriaNet_buf; //
|
||
|
||
jt808_free(PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data); // 释放数据内存
|
||
PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data = NULL;
|
||
PrsResult.term_param_item->big_data_up_SeriaNet.Msg_Data_Len = 0;
|
||
PrsResult.term_param_item->big_data_up_SeriaNet.Msg_type = 0;
|
||
}
|
||
// JT808_DEBUG("ID_Data_Up\r\n");
|
||
break;
|
||
}
|
||
default:{
|
||
return -2;
|
||
break;
|
||
}
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
// 消息帧打包 (会自动为msgEscape->buf分配合适长度的内存,注意释放)
|
||
int jt808_msg_pkg(MessageID_t Msg_ID, JT808MsgESC_t *msgEscape){
|
||
JT808_2013_MsgFrame_t big_MsgFrame;// 定义大端消息帧
|
||
|
||
big_MsgFrame.p_msg_body = NULL;
|
||
if(0 != jt808_BodyPackage(&big_MsgFrame, Msg_ID)){// 填充消息体 (注意释放p_msg_body内存)
|
||
JT808_DEBUG("jt808_BodyPackage failed\r\n");
|
||
return -1;
|
||
}
|
||
// 计算消息帧总长度
|
||
uint16_t msg_total_len = 1 + sizeof(MsgHead_t) - (1 ==big_MsgFrame.msg_head.msgbody_attr.packet ? 0 : 4);
|
||
uint16_t msgbody_len =big_MsgFrame.msg_head.msgbody_attr.msgbodylen;
|
||
msg_total_len += msgbody_len + 2;
|
||
// JT808_DEBUG("total_len:%d,len:%d\r\n", msg_total_len, msgbody_len);
|
||
|
||
big_MsgFrame.Head_SIGN = PSIGN;
|
||
big_MsgFrame.Tail_SIGN = PSIGN;
|
||
|
||
// 大端转换
|
||
big_MsgFrame.msg_head.msg_id = Swap16(Msg_ID);// 消息ID
|
||
big_MsgFrame.msg_head.msgbody_attr.val16 = Swap16(big_MsgFrame.msg_head.msgbody_attr.val16);
|
||
memcpy(big_MsgFrame.msg_head.phone_BCDnum, PrsResult.term_param_item->phone_BCDnum, 6);
|
||
big_MsgFrame.msg_head.msg_flow_num = Swap16(PrsResult.term_param_item->msg_flow_num);// 消息流水号
|
||
big_MsgFrame.msg_head.total_packet = Swap16(big_MsgFrame.msg_head.total_packet);// 总包数, 分包情况下使用
|
||
big_MsgFrame.msg_head.packet_seq = Swap16(big_MsgFrame.msg_head.total_packet);// 当前包序号, 分包情况下使用
|
||
|
||
uint8_t *msg_buf = (uint8_t *)jt808_malloc(msg_total_len);
|
||
if(msg_buf == NULL){
|
||
JT808_DEBUG("[%s,%s] malloc failed \r\n", __FUNCTION__,__LINE__);
|
||
if(big_MsgFrame.p_msg_body != NULL){ // 释放消息体内存(必要操作)
|
||
jt808_free(big_MsgFrame.p_msg_body);
|
||
big_MsgFrame.p_msg_body = NULL;
|
||
}
|
||
return -1;
|
||
}
|
||
// 填充头标识和消息头
|
||
memcpy(msg_buf, &big_MsgFrame.Head_SIGN, msg_total_len - msgbody_len - 2);
|
||
// 填充消息体
|
||
if(0 < msgbody_len && big_MsgFrame.p_msg_body != NULL){
|
||
memcpy(msg_buf + (msg_total_len - msgbody_len - 2), big_MsgFrame.p_msg_body, msgbody_len);
|
||
}
|
||
// 计算BCC校验码
|
||
big_MsgFrame.BCC_Check = BCC_Check(msg_buf + 1, msg_total_len - 2 - 1);
|
||
memcpy(msg_buf + msg_total_len - 2, &big_MsgFrame.BCC_Check, 2);
|
||
|
||
//// DEBUG
|
||
// app_printf("JT808_2013_MsgFrame_t:\r\n");
|
||
// for (size_t i = 0; i < msg_total_len - msgbody_len - 2; i++){
|
||
// app_printf("%02X ", *((uint8_t *)(&big_MsgFrame) + i));
|
||
// }
|
||
// app_printf("\r\n");
|
||
// for (size_t i = 0; i < msgbody_len; i++){
|
||
// app_printf("%02X ", *((uint8_t *)big_MsgFrame.p_msg_body + i));
|
||
// }
|
||
// app_printf("\r\n");
|
||
// for (size_t i = 0; i < 2; i++){
|
||
// app_printf("%02X ", *((uint8_t *)(&big_MsgFrame.BCC_Check) + i));
|
||
// }
|
||
// app_printf("\r\n");
|
||
|
||
if(big_MsgFrame.p_msg_body != NULL){ // 释放消息体内存(必要操作)
|
||
jt808_free(big_MsgFrame.p_msg_body);
|
||
big_MsgFrame.p_msg_body = NULL;
|
||
}
|
||
|
||
// 7E 转义处理
|
||
uint16_t escape_len = 0;
|
||
for(uint16_t i = 1; i < msg_total_len - 1; i++){
|
||
if(msg_buf[i] == PSIGN || msg_buf[i] == PESC){
|
||
escape_len++;
|
||
}
|
||
}
|
||
msgEscape->buf = (uint8_t *)jt808_malloc(msg_total_len + escape_len);
|
||
uint16_t offset_num = 0;
|
||
msgEscape->buf[0] = PSIGN;
|
||
msgEscape->buf[msg_total_len + escape_len - 1] = PSIGN;
|
||
|
||
for(uint16_t i = 1; i < msg_total_len - 1; i++){
|
||
if(msg_buf[i] == PSIGN){
|
||
msgEscape->buf[i + offset_num++] = PESC;
|
||
msgEscape->buf[i + offset_num] = PESC_SIGN;
|
||
}else if(msg_buf[i] == PESC){
|
||
msgEscape->buf[i + offset_num++] = PESC;
|
||
msgEscape->buf[i + offset_num] = PESC_ESCAPE;
|
||
}else {
|
||
msgEscape->buf[i + offset_num] = msg_buf[i];
|
||
}
|
||
}
|
||
jt808_free(msg_buf);
|
||
|
||
// 返回消息帧总长度
|
||
msgEscape->len = msg_total_len + escape_len;
|
||
|
||
JT808_DEBUG("SEND Msg_ID:0x%04X len:%d\r\n", Msg_ID ,msg_total_len + escape_len);
|
||
for(uint16_t i = 0; i < msg_total_len + escape_len; i++){
|
||
JT808_DEBUG_DATA("%02X ", *(msgEscape->buf + i));
|
||
}
|
||
|
||
JT808_DEBUG_DATA("\r\n");
|
||
// jt808_free(msgEscape->buf);
|
||
|
||
return 0;
|
||
}
|