| | |
| | | |
| | | |
| | | |
| | | //#define EUART_RX_BUF_SIZE 100 |
| | | //#define EUART0_RX_BUF_SIZE 100 |
| | | #define Label_id_local 1 //标签id |
| | | #define data_buff_MAX 50 //基站数量 |
| | | #define Lora_TXD_bff_MAX 220 //发送缓存区大小 >4+8+4*基站数量+2=14+4*基站数量 |
| | | |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | uint8_t mUsart2ReceivePack[150] = {0}; |
| | | uint8_t GPS_GGAmessage[150]={0}; |
| | | uint8_t GPS_GSVmessage[150]={0}; |
| | | double jd,wd; |
| | | int analysis_num,pos_state; |
| | | uint8_t state5V_prase_flag=1,gps_prase_flag=1; |
| | |
| | | extern void IO_control_init(void); |
| | | extern void blink_led(uint8_t*state); |
| | | extern void updata_led_power_state(void); |
| | | extern uint8_t gps_success_state; |
| | | extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag; |
| | | extern uint8_t gps_success_state,gps_open_flag; |
| | | extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag,gps_power_state; |
| | | extern uint16_t gps_wait_count; |
| | | typedef enum |
| | | { |
| | |
| | | sum = ~sum; |
| | | return sum; |
| | | } |
| | | |
| | | |
| | | void SendComMap0(uint8_t data_length, uint8_t index)//表示成功读取的回应包 |
| | | { |
| | | static uint8_t send_frame[EUART0_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x03; |
| | | send_frame[3] = data_length+5; |
| | | send_frame[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID0, send_frame,data_length+9, NULL); |
| | | } |
| | | void SendComMap(uint8_t data_length, uint8_t index)//表示成功读取的回应包 |
| | | { |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | static uint8_t send_frame[EUART0_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | |
| | | } |
| | | |
| | | } |
| | | void UsartParseDataHandler(uint8_t data) |
| | | double d_value; |
| | | uint16_t test_calculate,pack_checksum_test; |
| | | extern uint32_t tag_id_authorization_list[1024]; |
| | | extern uint16_t bind_distance,button_determinate_distance,change_by_frequency_distance; |
| | | extern uint8_t tag_near_frequency; |
| | | extern tag_id_authorization_list_num; |
| | | #define ID_SIZE 4 |
| | | uint32_t temp_key; |
| | | uint8_t required_size_temp; |
| | | uint8_t delete_count_temp; |
| | | int low_temp,high_temp,mid_temp,elements_to_move_temp,temp_index; |
| | | void add_new_tag_id_to_list(uint8_t* add_list,uint32_t*tag_authorization_idlist,uint32_t*authorization_list_num) |
| | | { |
| | | uint32_t count = add_list[0]; |
| | | uint32_t current_size = *authorization_list_num; |
| | | uint32_t new_size = current_size + count; |
| | | |
| | | // 处理每个新ID |
| | | for (uint32_t i = 0; i < count; i++) { |
| | | uint32_t add_offset = 1 + i * 4; // 跳过计数byte |
| | | uint32_t new_id=0; |
| | | memcpy(&new_id,&add_list[add_offset],4); |
| | | |
| | | // 二分查找确定插入位置 |
| | | uint32_t left = 0; |
| | | uint32_t right = current_size; |
| | | uint32_t insert_pos = current_size; |
| | | bool exists = false; |
| | | while (left < right) { |
| | | uint32_t mid = left + (right - left) / 2; |
| | | uint32_t mid_val = tag_authorization_idlist[mid]; |
| | | |
| | | if (mid_val == new_id) { |
| | | exists = true; |
| | | break; |
| | | } else if(tag_authorization_idlist[mid] < new_id) { |
| | | left = mid + 1; |
| | | } else { |
| | | right = mid; |
| | | } |
| | | } |
| | | |
| | | // 如果ID不存在,则插入 |
| | | if (!exists) { |
| | | insert_pos = left; |
| | | // 使用memmove移动元素为新ID腾出空间 |
| | | memmove( |
| | | &tag_authorization_idlist[left + 1], |
| | | &tag_authorization_idlist[left], |
| | | (current_size - left) * sizeof(uint32_t) |
| | | ); |
| | | |
| | | // 插入新ID |
| | | tag_authorization_idlist[left] = new_id; |
| | | current_size++; // 更新当前大小 |
| | | } |
| | | } |
| | | *authorization_list_num = current_size; // 更新总数 |
| | | |
| | | } |
| | | void delete_authorization_list(uint8_t* delete_list,uint32_t*tag_authorization_idlist,uint32_t*authorization_list_num) |
| | | { |
| | | if (*authorization_list_num == 0) { |
| | | // for (int i = 0; i < taglist_num; i++) { |
| | | // tag_authorized_List[i] = 0; |
| | | // } |
| | | return; |
| | | } |
| | | uint8_t delete_count = delete_list[0]; |
| | | delete_count_temp=delete_count; |
| | | // 遍历需要查找的每个ID |
| | | for (int i = 0; i < delete_count; i++) { |
| | | uint32_t key=0; |
| | | uint8_t required_size = 1 + i * ID_SIZE; |
| | | required_size_temp= required_size; |
| | | memcpy(&key,&delete_list[required_size],4); |
| | | int low = 0; |
| | | int high = *authorization_list_num - 1; |
| | | int found = 0; // 标记是否找到 |
| | | int index=0; |
| | | // 在授权列表中执行二分查找 |
| | | low_temp=low;//测试 |
| | | high_temp=high;//测试 |
| | | temp_key=key; |
| | | while (low <= high) { |
| | | int mid = low + (high - low) / 2; // 防止整数溢出 |
| | | LOG_INFO(TRACE_MODULE_APP,"high%d,low%d,mid%d,key%#x\r\n",high,low,mid,key); |
| | | if (tag_id_authorization_list[mid] == key) { |
| | | found = 1; // 找到匹配项 |
| | | index=mid; |
| | | temp_index=index; |
| | | break; |
| | | } else if (tag_id_authorization_list[mid] < key) { |
| | | low = mid + 1; // 在右半部分继续查找 |
| | | } else { |
| | | high = mid - 1; // 在左半部分继续查找 |
| | | } |
| | | |
| | | } |
| | | |
| | | if(found) |
| | | { |
| | | LOG_INFO(TRACE_MODULE_APP,"进入删除index%d,key%#x\r\n",index,key); |
| | | int elements_to_move = *authorization_list_num - index - 1; |
| | | elements_to_move_temp=elements_to_move; |
| | | if (elements_to_move > 0) { |
| | | // 计算源地址和目标地址 |
| | | void *src = (uint32_t *)tag_authorization_idlist + (index + 1); |
| | | void *dest =(uint32_t *)tag_authorization_idlist + index; |
| | | |
| | | // 使用memmove进行内存块移动(处理重叠内存区域) |
| | | memmove(dest, src, elements_to_move * 4); |
| | | *authorization_list_num=*authorization_list_num-1;//成功删除一个更新数量 |
| | | tag_authorization_idlist[*authorization_list_num]=0xffffffff;//将多余重叠部分重置 |
| | | }else if (elements_to_move==0&&index==0){ |
| | | *authorization_list_num=*authorization_list_num-1;//成功删除一个更新数量 |
| | | tag_authorization_idlist[index]=0xffffffff; |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | extern uint8_t bind_flag; |
| | | void Usart0ParseDataHandler(uint8_t data) |
| | | { |
| | | if(state5V_prase_flag&&!g_com_map[MODBUS_MODE]) |
| | | { //升级程序 |
| | | static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; |
| | | uint16_t checksum = 0; |
| | | static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ; |
| | | static uint8_t index = 0; |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) |
| | | { //若收到校验和包 |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsart2ReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if(((data + checksum)&0xff) == 0xFF) //校验通过 |
| | | { |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | // if(pack_index==MODBUS_MODE*2) |
| | | // { |
| | | // Uart1GpsRecDebugSend(); |
| | | // g_com_map[MODBUS_MODE] = 1; |
| | | // return; |
| | | // } |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsart2ReceivePack, pack_datalen); |
| | | if(mUsart2ReceivePack[0]==1) |
| | | UpdateProcess(pack_index); |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | //delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | break; |
| | | case CMD_READ: |
| | | //read包中data字节,即mUsart2ReceivePack[0]表示数据长度; |
| | | //从g_com_data结构体中的第index位置读取长度为mUsart2ReceivePack[0]的字节,发送出来 |
| | | SendComMap0(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 |
| | | mUsart2ReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | } else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |
| | | pack_cmd = data; |
| | | usart_receive_state = UsartReceiveWaitIndex; |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitCMD; |
| | | |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 |
| | | usart_receive_state = UsartReceiveWaitMsgType; |
| | | } else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | } |
| | | } |
| | | } |
| | | void Usart3ParseDataHandler(uint8_t data) |
| | | { |
| | | // 全局变量 |
| | | static Usart0ReceiveState usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | |
| | | static uint8_t index = 0; // 缓冲区索引 |
| | | static uint8_t pack_cmd_type = 0; // 指令类型 |
| | | static uint8_t pack_msg_type = 0; // 消息类型 |
| | | static uint8_t pack_data_len = 0; // 数据长度 |
| | | static uint8_t pack_param_addr = 0; // 参数地址 |
| | | static uint16_t pack_checksum = 0; // 接收到的校验和 |
| | | static uint16_t calculated_checksum = 0; // 计算得到的校验和 |
| | | static uint8_t send_frame[256]; // 用于计算校验和的帧 |
| | | static uint8_t frame_index = 0; // 帧缓冲区索引 |
| | | static uint8_t pack_datalen = 0; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到数据%c\r\n",data); |
| | | //LOG_INFO(TRACE_MODULE_APP,"收到 %#x\r\n",data); |
| | | switch(usart0_receive_state) { |
| | | case Usart0ReceiveWaitHead0: |
| | | if(data == 0x55) { |
| | | usart0_receive_state = Usart0ReceiveWaitHead1; |
| | | // 初始化帧缓冲区 |
| | | frame_index = 0; |
| | | send_frame[frame_index++] = data; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitHead1: |
| | | if(data == 0xAA) { |
| | | usart0_receive_state = Usart0ReceiveWaitCmdType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitCmdType: |
| | | if(data == 0x23) { |
| | | pack_cmd_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitMsgType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitMsgType: |
| | | if(data == 0x2||data==0x1||data == 0x3||data==0x4||data==0x5||data==0x6) { // 读写回复消息类型 |
| | | pack_msg_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitDataLen; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitDataLen: |
| | | pack_data_len = data; |
| | | index = 0; |
| | | usart0_receive_state = Usart0ReceiveWaitData; |
| | | send_frame[frame_index++] = data; |
| | | if( pack_data_len-3==0)//若等于0提前调到校验 |
| | | usart0_receive_state=Usart0ReceiveWaitChecksum1; |
| | | break; |
| | | |
| | | // case Usart0ReceiveWaitParamAddr: |
| | | // pack_param_addr = data; |
| | | // if(pack_msg_type==CMD_READ) |
| | | // { |
| | | // usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | // pack_datalen=pack_data_len-6; |
| | | // }else{ |
| | | // usart0_receive_state = Usart0ReceiveWaitData; |
| | | // } |
| | | // send_frame[frame_index++] = data; |
| | | // break; |
| | | |
| | | case Usart0ReceiveWaitData: |
| | | |
| | | send_frame[frame_index++] = data; |
| | | mUsart2ReceivePack[index++] = data; |
| | | |
| | | if(index == pack_data_len-3) { |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | //pack_datalen=pack_data_len; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitChecksum1: |
| | | pack_checksum = data; // 低字节 |
| | | pack_checksum_test = data; // 低字节 |
| | | send_frame[frame_index++] = data; |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum2; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验1 %#x\r\n",data); |
| | | break; |
| | | case Usart0ReceiveWaitChecksum2: |
| | | pack_checksum |= data<<8; // 高字节 |
| | | pack_checksum_test |= data<<8; // 高字节 |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验2 %#x\r\n",data); |
| | | // 计算校验和 (从第3个字节开始,长度为4+数据长度) |
| | | // if(pack_msg_type!=CMD_REPLY) |
| | | // { |
| | | calculated_checksum = Checksum_u16(&send_frame[2],pack_data_len); |
| | | // }else{ |
| | | // //calculated_checksum = Checksum_u16(&send_frame[2],4); |
| | | // |
| | | // } |
| | | test_calculate=calculated_checksum; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验 %#x,%#x\r\n",calculated_checksum,pack_checksum); |
| | | // 验证校验和 |
| | | if(pack_checksum == calculated_checksum) { |
| | | // 校验通过,处理数据 |
| | | switch(pack_msg_type) |
| | | { |
| | | case BIND_DISTANCE_FOR_TAG: |
| | | memcpy(&bind_distance,&mUsart2ReceivePack[0],2); |
| | | memcpy(&g_com_map[BIND_DISTANCE],&bind_distance,2); |
| | | bind_flag=1; |
| | | save_com_map_to_flash(); |
| | | break; |
| | | case DELETE_ALL: |
| | | memset(tag_id_authorization_list,0,sizeof(tag_id_authorization_list)); |
| | | save_taglist_map_to_flash(); |
| | | break; |
| | | case DELETE_PART: |
| | | delete_authorization_list(&mUsart2ReceivePack,&tag_id_authorization_list,&tag_id_authorization_list_num);//删表中元素操作 |
| | | save_taglist_map_to_flash(); |
| | | break; |
| | | case SET_DISTANCE_DETERMINATION: |
| | | memcpy(&button_determinate_distance,&mUsart2ReceivePack[0],2);//设定开关判断距离 |
| | | memcpy(&g_com_map[DETERMINATION_DISTANCE],&button_determinate_distance,2); |
| | | save_com_map_to_flash(); |
| | | break; |
| | | case SET_FREQUENCE_BY_DISTANCE: |
| | | memcpy(&change_by_frequency_distance,&mUsart2ReceivePack[0],2);//设定附近距离 |
| | | memcpy(&g_com_map[FREQUENCY_DISTANCE],&change_by_frequency_distance,2); |
| | | tag_near_frequency=mUsart2ReceivePack[2];//获取设定频率 |
| | | memcpy(&g_com_map[NERA_FREQUENCY],&tag_near_frequency,1); |
| | | save_com_map_to_flash(); |
| | | break; |
| | | case ADD_NEW_TAG_IN_LIST: |
| | | add_new_tag_id_to_list(&mUsart2ReceivePack,&tag_id_authorization_list,&tag_id_authorization_list_num); |
| | | save_taglist_map_to_flash();//存入flash |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 重置状态机 |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | index = 0; |
| | | frame_index = 0; |
| | | break; |
| | | |
| | | default: |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | index = 0; |
| | | frame_index = 0; |
| | | break; |
| | | } |
| | | |
| | | |
| | | } |
| | | void Usart1ParseDataHandler(uint8_t data) |
| | | { |
| | | if(state5V_prase_flag&&!g_com_map[MODBUS_MODE]) |
| | | { //升级程序 |
| | |
| | | case CMD_READ: |
| | | //read包中data字节,即mUsartReceivePack[0]表示数据长度; |
| | | //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 |
| | | SendComMap(pack_datalen,pack_index>>1); |
| | | SendComMap0(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | |
| | | analysis_num=sscanf((char*)mUsart2ReceivePack,"$%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*s",gps_header,wdrecv.m_pData,jdrecv.m_pData,Posstate.m_pData); |
| | | if(!memcmp(gps_header,"GNGGA",5)) |
| | | { |
| | | if(g_com_map[MODBUS_MODE]) |
| | | LOG_INFO(TRACE_MODULE_APP,"%s", mUsart2ReceivePack); |
| | | wd=strtod(wdrecv.m_pData,NULL); |
| | | jd=strtod(jdrecv.m_pData,NULL); |
| | | pos_state=atoi(Posstate.m_pData); |
| | | } else { |
| | | wd=0; |
| | | jd=0; |
| | | memcpy(GPS_GGAmessage,mUsart2ReceivePack,strlen(mUsart2ReceivePack)-2); |
| | | |
| | | wd=strtod(wdrecv.m_pData,NULL); |
| | | jd=strtod(jdrecv.m_pData,NULL); |
| | | if(wd!=0) |
| | | pos_state=atoi(Posstate.m_pData); |
| | | if(pos_state!=0) |
| | | { |
| | | pos_state=0;//防止多次进入 |
| | | } |
| | | |
| | | } |
| | | index = 0; |
| | | blink_led(&gps_success_state); |
| | | if(pos_state!=0) |
| | | { |
| | | //4g.jd=jd; |
| | | //4g.wd=wd; |
| | | gps_timeout_flag=1;//不超时接收状态 |
| | | gps_need_data_flag=0;//接收数据完成 |
| | | gps_wait_count=0;//清0接收状态 |
| | | // gps_enable_flag=0;//收到有效数据关闭GPS |
| | | } |
| | | //memset(mUsart2ReceivePack,0,sizeof(mUsart2ReceivePack)); |
| | | // if(!memcmp(gps_header,"GBGSV",5)) |
| | | // {memcpy(GPS_GSVmessage,mUsart2ReceivePack,strlen(mUsart2ReceivePack)-2); |
| | | // GPS_ParseGSV(GPS_GSVmessage,strlen(GPS_GSVmessage)); |
| | | // |
| | | // } |
| | | |
| | | memset(mUsart2ReceivePack,0,sizeof(mUsart2ReceivePack)); |
| | | memset(wdrecv.m_pData,0,sizeof(wdrecv.m_pData)); |
| | | memset(jdrecv.m_pData,0,sizeof(jdrecv.m_pData)); |
| | | memset(Posstate.m_pData,0,sizeof(Posstate.m_pData)); |