2024-11-08 18:02:29 +08:00
|
|
|
#include "jt808_msg_parse.h"
|
|
|
|
|
#include "jt808_pkg_transmit.h"
|
2024-11-23 16:22:04 +08:00
|
|
|
#include "local_tts.h"
|
2025-02-26 17:10:45 +08:00
|
|
|
#include "jt808_electronic_fence.h"
|
2024-11-08 18:02:29 +08:00
|
|
|
|
|
|
|
|
PrsResult_t PrsResult;
|
|
|
|
|
|
|
|
|
|
// 消息体解析
|
2024-11-23 16:22:04 +08:00
|
|
|
static int jt808_BodyParse(void *Prsmsg_body, PrsResult_t *Result){
|
|
|
|
|
switch (Result->msg_head.msg_id){
|
2024-11-08 18:02:29 +08:00
|
|
|
case ID_Plat_GenResp:{// 平台通用应答
|
2024-11-23 16:22:04 +08:00
|
|
|
memcpy(&(Result->Rsp_flow_num), Prsmsg_body, sizeof(Plat_GenResp_t));
|
2024-11-08 18:02:29 +08:00
|
|
|
// 转小端
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->Rsp_flow_num = Swap16(Result->msg_head.msg_flow_num);
|
|
|
|
|
Result->Rsp_msg_id = Swap16(Result->msg_head.msg_id);
|
|
|
|
|
// Result->Rsp_result = Result->Rsp_result;
|
2024-11-08 18:02:29 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_FillPktReq:{// 补传分包请求
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_Term_RegResp:{// 终端注册应答
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->Rsp_flow_num = (uint16_t)(((uint8_t *)Prsmsg_body)[0] << 8) | (((uint8_t *)Prsmsg_body)[1]);
|
|
|
|
|
Result->Rsp_result = ((uint8_t *)Prsmsg_body)[2];
|
|
|
|
|
if(Result->Rsp_result == Msg_ok){ // 成功时,读取鉴权码
|
|
|
|
|
if(Result->term_param_item->big_auth_info.str_auth_code != NULL){
|
|
|
|
|
jt808_free(Result->term_param_item->big_auth_info.str_auth_code);
|
|
|
|
|
Result->term_param_item->big_auth_info.str_auth_code = NULL;
|
2024-11-08 18:02:29 +08:00
|
|
|
}
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->term_param_item->big_auth_info.str_auth_code = (char *)jt808_malloc(Result->msg_head.msgbody_attr.msgbodylen - 3 +1); // 加1是为了加上结束符
|
|
|
|
|
if(Result->term_param_item->big_auth_info.str_auth_code == NULL){
|
2024-11-08 18:02:29 +08:00
|
|
|
JT808_DEBUG("[%s,%s] malloc failed\r\n", __FUNCTION__,__LINE__);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-11-23 16:22:04 +08:00
|
|
|
memset(Result->term_param_item->big_auth_info.str_auth_code, 0, Result->msg_head.msgbody_attr.msgbodylen - 3 +1);
|
|
|
|
|
memcpy(Result->term_param_item->big_auth_info.str_auth_code, Prsmsg_body + 3 , Result->msg_head.msgbody_attr.msgbodylen - 3);
|
2024-11-08 18:02:29 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_SetTermParams:{// 设置终端参数
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->term_param_item->big_specific_params.param_Total_num = ((uint8_t *)Prsmsg_body)[0]; // 总参数个数
|
2025-02-26 17:10:45 +08:00
|
|
|
uint8_t *p_Prsmsg_body = (uint8_t *)Prsmsg_body + 1;
|
2024-11-23 16:22:04 +08:00
|
|
|
for(int i = 0; i < Result->term_param_item->big_specific_params.param_Total_num; i++){
|
2024-11-08 18:02:29 +08:00
|
|
|
// 设置终端参数
|
2025-02-26 17:10:45 +08:00
|
|
|
jt808_setTermParam(((uint8_t *)(p_Prsmsg_body))[0] << 24 | ((uint8_t *)(p_Prsmsg_body))[1] << 16 | ((uint8_t *)(p_Prsmsg_body))[2] << 8 | ((uint8_t *)(p_Prsmsg_body))[3], // 参数ID
|
|
|
|
|
(void *)(p_Prsmsg_body + 5), // 参数值
|
|
|
|
|
p_Prsmsg_body[4]); // 参数长度
|
|
|
|
|
p_Prsmsg_body += 5 + p_Prsmsg_body[4]; // 参数ID(4) + 参数长度(1) + 参数值(n)
|
2024-11-08 18:02:29 +08:00
|
|
|
}
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->Rsp_flow_num = Result->msg_head.msg_flow_num;
|
|
|
|
|
Result->Rsp_msg_id = Result->msg_head.msg_id;
|
|
|
|
|
Result->Rsp_result = Msg_ok;
|
2024-11-08 18:02:29 +08:00
|
|
|
jt808_pkg_send(ID_Term_GenResp, 0);// 发送终端通用应答
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_GetTermParams:{// 查询终端参数
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_GetSpecificTermParams:{// 查询指定终端参数
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_Term_Ctrl:{// 终端控制
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_GetTermAttr:{// 查询终端属性
|
|
|
|
|
|
2025-02-11 22:42:05 +08:00
|
|
|
jt808_pkg_send(ID_GetTermAttrResp, 0);// 发送查询终端属性应答,设置本消息无发送应答
|
2024-11-08 18:02:29 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_Term_Upgrade:{// 下发终端升级包
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_GetLocInfo:{// 位置信息查询
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_LocTrackingCtrl:{// 临时位置跟踪控制
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-11-23 16:22:04 +08:00
|
|
|
case ID_TxtMsgdelivery:{// 文本信息下发
|
|
|
|
|
uint8_t device_volume, speed, volume, mode;
|
2025-02-11 22:42:05 +08:00
|
|
|
uint8_t txt_flag = ((uint8_t *)Prsmsg_body)[0];
|
|
|
|
|
JT808_DEBUG("ID_TxtMsgdelivery:%x\r\n", txt_flag);
|
|
|
|
|
Prsmsg_body = (void *)((uint8_t *)Prsmsg_body + 1); // 跳过标志位
|
2024-11-23 16:22:04 +08:00
|
|
|
device_volume =10*(((uint8_t *)Prsmsg_body)[0]-'0') + (((uint8_t *)Prsmsg_body)[1]-'0');
|
|
|
|
|
speed =10*(((uint8_t *)Prsmsg_body)[3]-'0') + (((uint8_t *)Prsmsg_body)[4]-'0');
|
|
|
|
|
volume =10*(((uint8_t *)Prsmsg_body)[6]-'0') + (((uint8_t *)Prsmsg_body)[7]-'0');
|
|
|
|
|
mode = ((uint8_t *)Prsmsg_body)[9]-'0';
|
|
|
|
|
|
|
|
|
|
Result->Rsp_result = Msg_ok;
|
|
|
|
|
if((((uint8_t *)Prsmsg_body)[2] == ',')&&(((uint8_t *)Prsmsg_body)[5] == ',')&&(((uint8_t *)Prsmsg_body)[8] == ',')&&(((uint8_t *)Prsmsg_body)[10] == ';')){ // 文本信息下发
|
|
|
|
|
if(speed > 0 && speed <= 15 && volume > 0 && volume <= 15 && mode >= 0 && mode <= 2){ // 速度、音量、模式有效
|
|
|
|
|
local_tts_volume(device_volume); // 设置音量大小
|
|
|
|
|
local_tts_set(speed, volume, mode);
|
|
|
|
|
}else{
|
|
|
|
|
JT808_DEBUG("error speed or volume or mode\r\n");
|
2024-11-25 22:16:49 +08:00
|
|
|
local_tts_volume(50); // 设置音量大小
|
|
|
|
|
local_tts_set(6, 7, CM_LOCAL_TTS_DIGIT_AUTO);
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->Rsp_result = Msg_invalid;//消息有误
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
JT808_DEBUG("error speed or volume or mode\r\n");
|
|
|
|
|
local_tts_volume(80); // 设置音量大小
|
|
|
|
|
local_tts_set(7, 15, CM_LOCAL_TTS_DIGIT_AUTO);
|
|
|
|
|
Result->Rsp_result = Msg_invalid;//消息有误
|
|
|
|
|
}
|
2024-11-25 22:16:49 +08:00
|
|
|
local_tts_text_play((char *)Prsmsg_body + 11, Result->msg_head.msgbody_attr.msgbodylen -11,0); // 开始播放文本信息// 不可打断
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->Rsp_flow_num = Result->msg_head.msg_flow_num;
|
|
|
|
|
Result->Rsp_msg_id = Result->msg_head.msg_id;
|
|
|
|
|
// Result->Rsp_result = Msg_ok;
|
|
|
|
|
jt808_pkg_send(ID_Term_GenResp, 0);// 发送终端通用应答
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-11-08 18:02:29 +08:00
|
|
|
case ID_Car_Ctrl:{// 车辆控制
|
2025-02-11 22:42:05 +08:00
|
|
|
if(Result->msg_head.msgbody_attr.msgbodylen == 1){ // 车辆控制
|
|
|
|
|
jt808_Set_CarStatus(((uint8_t *)Prsmsg_body)[0]); // 设置车辆状态
|
|
|
|
|
Result->Rsp_result = Msg_ok;
|
|
|
|
|
}else{
|
|
|
|
|
Result->Rsp_result = Msg_invalid;
|
|
|
|
|
}
|
2024-11-23 16:22:04 +08:00
|
|
|
Result->Rsp_flow_num = Result->msg_head.msg_flow_num;
|
|
|
|
|
Result->Rsp_msg_id = Result->msg_head.msg_id;
|
2024-11-08 18:02:29 +08:00
|
|
|
jt808_pkg_send(ID_Car_CtrlResp, 0);// 发送车辆控制应答,设置本消息无发送应答
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-02-26 17:10:45 +08:00
|
|
|
case ID_Set_Polygon_area:{// 设置多边形区域
|
2025-03-13 20:10:35 +08:00
|
|
|
int ret = 0;
|
2025-02-26 17:10:45 +08:00
|
|
|
uint32_t Area_ID; // 区域ID
|
|
|
|
|
uint16_t Area_att; // 区域属性
|
|
|
|
|
uint16_t Area_Points_Num; // 区域内点的数量
|
|
|
|
|
memcpy(&Area_ID, ((uint8_t *)Prsmsg_body), 4); // 区域ID
|
|
|
|
|
memcpy(&Area_att, ((uint8_t *)Prsmsg_body)+4, 2); // 区域属性
|
|
|
|
|
memcpy(&Area_Points_Num, ((uint8_t *)Prsmsg_body)+21, 2); // 区域点个数
|
2025-03-13 20:10:35 +08:00
|
|
|
if(Swap16(Area_Points_Num) <= 120){ // 区域点个数有效
|
|
|
|
|
ret = jt808_add_tail_fence_Polygon_area(Swap32(Area_ID), // 区域ID
|
|
|
|
|
Swap16(Area_att), // 区域属性
|
|
|
|
|
Swap16(Area_Points_Num), // 区域点个数
|
|
|
|
|
(AreaPoint_t *)(((uint8_t *)Prsmsg_body) + 23)); // 区域点坐标
|
|
|
|
|
if(ret == 0){
|
|
|
|
|
Result->Rsp_result = Msg_ok;
|
|
|
|
|
}else{
|
|
|
|
|
Result->Rsp_result = Msg_err;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
Result->Rsp_result = Msg_invalid;
|
|
|
|
|
}
|
2025-02-26 17:10:45 +08:00
|
|
|
Result->Rsp_flow_num = Result->msg_head.msg_flow_num;
|
|
|
|
|
Result->Rsp_msg_id = Result->msg_head.msg_id;
|
|
|
|
|
jt808_pkg_send(ID_Term_GenResp, 0);// 发送终端通用应答
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ID_Delete_Polygon_area:{// 删除多边形区域
|
2025-03-13 20:10:35 +08:00
|
|
|
int ret = 0;
|
2025-02-26 17:10:45 +08:00
|
|
|
uint8_t Area_ID_Num = ((uint8_t *)Prsmsg_body)[0]; // 区域ID个数
|
|
|
|
|
Prsmsg_body = (void *)((uint8_t *)Prsmsg_body + 1); // 跳过1字节
|
2025-03-13 20:10:35 +08:00
|
|
|
Result->Rsp_result = Msg_ok;
|
2025-02-26 17:10:45 +08:00
|
|
|
for(int i = 0; i < Area_ID_Num; i++){
|
2025-03-13 20:10:35 +08:00
|
|
|
ret = jt808_remove_fence_Polygon_area(((uint8_t *)Prsmsg_body)[i]<<24 | ((uint8_t *)Prsmsg_body)[i+1]<<16 | ((uint8_t *)Prsmsg_body)[i+2]<<8 | ((uint8_t *)Prsmsg_body)[i+3]);
|
|
|
|
|
if(ret != 0){
|
|
|
|
|
Result->Rsp_result = Msg_err;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-02-26 17:10:45 +08:00
|
|
|
}
|
|
|
|
|
Result->Rsp_flow_num = Result->msg_head.msg_flow_num;
|
|
|
|
|
Result->Rsp_msg_id = Result->msg_head.msg_id;
|
|
|
|
|
jt808_pkg_send(ID_Term_GenResp, 0);// 发送终端通用应答
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-11-23 16:22:04 +08:00
|
|
|
case ID_Data_Down:{// 数据下行透传
|
|
|
|
|
jt808_data_down_SeriaNet(((uint8_t *)Prsmsg_body)[0],((uint8_t *)Prsmsg_body) + 1, Result->msg_head.msgbody_attr.msgbodylen - 1); // 透传数据
|
|
|
|
|
Result->Rsp_flow_num = Result->msg_head.msg_flow_num;
|
|
|
|
|
Result->Rsp_msg_id = Result->msg_head.msg_id;
|
|
|
|
|
Result->Rsp_result = Msg_ok;
|
|
|
|
|
jt808_pkg_send(ID_Term_GenResp, 0);// 发送终端通用应答
|
|
|
|
|
// JT808_DEBUG("ID_Data_Down\r\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-11-08 18:02:29 +08:00
|
|
|
default:{
|
|
|
|
|
return -2; // 没有对应的消息体解析函数
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// jt808协议包处理
|
2024-11-23 16:22:04 +08:00
|
|
|
int jt808_msg_parse(const uint8_t *BufferReceive, uint16_t length, PrsResult_t *Result){
|
2024-11-08 18:02:29 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
// 解析消息头
|
2024-11-23 16:22:04 +08:00
|
|
|
memcpy(&Result->msg_head, para_Buffer + 1, sizeof(MsgHead_t) - ((para_Buffer[3] & 0x02)==0? 4 : 0));
|
|
|
|
|
Result->msg_head.msg_id = Swap16(Result->msg_head.msg_id);// 消息ID
|
|
|
|
|
Result->msg_head.msgbody_attr.val16 = Swap16(Result->msg_head.msgbody_attr.val16);// 消息体属性
|
|
|
|
|
Result->msg_head.msg_flow_num = Swap16(Result->msg_head.msg_flow_num);// 消息流水号
|
|
|
|
|
Result->msg_head.total_packet = Swap16(Result->msg_head.total_packet);// 总包数, 分包情况下使用
|
|
|
|
|
Result->msg_head.packet_seq = Swap16(Result->msg_head.packet_seq);// 当前包序号, 分包情况下使用
|
|
|
|
|
|
|
|
|
|
JT808_DEBUG("RECV Msg_ID:0x%04X len:%d\r\n", Result->msg_head.msg_id ,para_length);
|
|
|
|
|
for(uint16_t i = 0; i < para_length; i++){
|
2025-02-28 01:06:37 +08:00
|
|
|
JT808_DEBUG_DATA("%02X ", *(para_Buffer + i));
|
2024-11-23 16:22:04 +08:00
|
|
|
}
|
2025-02-28 01:06:37 +08:00
|
|
|
JT808_DEBUG_DATA("\n");
|
2024-11-08 18:02:29 +08:00
|
|
|
// 消息体解析
|
2024-11-23 16:22:04 +08:00
|
|
|
if(0 != jt808_BodyParse((void *)(para_Buffer + 1 + sizeof(MsgHead_t) - ((para_Buffer[3] & 0x02)==0? 4 : 0)) ,Result)){
|
2024-11-08 18:02:29 +08:00
|
|
|
JT808_DEBUG("error jt808_BodyParse\r\n");
|
|
|
|
|
// 释放内存
|
|
|
|
|
jt808_free(para_Buffer);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 释放内存
|
|
|
|
|
jt808_free(para_Buffer);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|