/* * @Description: * @Version: 2.0 * @Author: Seven * @Date: 2022-08-22 09:55:26 * @LastEditors: Seven * @LastEditTime: 2022-12-06 13:31:27 */ #include "gw_ctrl.h" #include "tools.h" #include "lora.h" #include "includes.h" #include "encoder.h" #include "tax.h" uint8_t rcv_buf[UART_RCV_PROCBUF_LEN]; uint16_t rcv_size,rcv_size_temp; msg_para_t msg_para; lora_comm_info_t lora_comm_info; ciphertext_state_t ciphertext_state; read_encoder_para_t read_encoder_para; read_56_info_t read_56_info; cmd_73_info_t cmd_73_info[2]; uart_timeout_times_t uart_timeout_times;//报税口超时连续次数 //加密芯片状态初始化 void ciphertext_state_init(void) { memset(&ciphertext_state,0,sizeof(ciphertext_state_t)); } //打开加密芯片死机定时 void ciphertext_time_start(void) { if(device_fixed_info.Encrypt == CIPHERTEXT)//密文 { if(ciphertext_state.ciphertext.flag==0)//如果未打开定时 { timeout_setValue(&ciphertext_state.ciphertext,CIPHERTEXT_RESET_TIME,1);//定时1hour } } } //加密芯片超时处理 void ciphertext_state_isout(void) { //如果设备上电,加密模块发送次数超过一次且一次也没接收,延时1hour重启 if(timeout_isOut(&ciphertext_state.ciphertext)) { printf("加密模块死了"); //复位加密芯片 port_init(PORT_LED_OFF); //初始化加密模块 ciphertext_state_init(); } } //网关下发参数存储初始化 void msg_para_init(void) { memset(&msg_para,0,sizeof(msg_para_t)); } //离线次数判断处理 void uart_timeout_time_handle(uint8_t tax) { if(tax == 1) { uart_timeout_times.tax1_times++; if(((uart_timeout_times.tax1_times%10)==9) && uart_timeout_times.tax1_times < 59) { tax_sw_reset(1); } printf("tax1 timeout num:%u\n",uart_timeout_times.tax1_times); } else if(tax == 2) { uart_timeout_times.tax2_times++; if(((uart_timeout_times.tax2_times%10)==9) && uart_timeout_times.tax2_times < 59) { tax_sw_reset(2); } printf("tax2 timeout num:%u\n",uart_timeout_times.tax2_times); } } //串口超时处理函数 void uart_timeout_handle(void) { if(timeout_isOut(&lora_comm_info.uart_timeout)) { // printf("timeout:%d\n",(TickCounter - lora_comm_info.uart_timeout.count)); printf("uart_timeout:%d\n",lora_comm_info.uart_timeout.timeout); //离线次数 uart_fifo_clear(UART4_ID);//清除串口缓存数据 uart_fifo_clear(UART5_ID);//清除串口缓存数据 if(tax_send_info.start) { if(tax_send_info.startcmd == GATE_COLL_HEARTBEAT) { if(read_56_info.step==2)//uart4 { coll_heartbeat_return(); read_56_info.step = 0; msg_para_init();//清除存储的下发参数 tax_send_end(); return; } else//uart3 { read_56_info.step = 2; tax_send_end(); read_56_start(UART5_ID); return; } } } //上报超时数据 if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_3) { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); if((msg_rcv_info.first_type == FIRST_SYSTEM_TYPE) && (msg_rcv_info.second_type == SECOND_SYSTEM_TRANS_DOWN))//透传数据 { transparent_up_ver3_return(UART_TIMEOUT,NULL,0); msg_rcv_info_init(); } } else if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_FF)//版本查询 { msg_rcv_info_init(); printf("xsp version timeout\n"); } else { if(msg_para.Uart == UART_485)//显示屏 { gateway_down_business_return(COLL_GATE_XSP_REPLY,UART_TIMEOUT,NULL,0); } else//报税口 { if(msg_para.First_type == FIRST_TYPE_SYSTEM)//透传指令 coll_transparent_return(msg_para.First_type,UART_TIMEOUT,NULL,0); else gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TIMEOUT,NULL,0); tax_send_end(); } msg_para_init();//清除存储的下发参数 } // //上报超时数据 // if(msg_para.Uart == UART3_ID)//显示屏 // { // gateway_down_business_return(COLL_GATE_XSP_REPLY,UART_TIMEOUT,NULL,0); // } // else//报税口 // { // if(msg_para.First_type == FIRST_TYPE_SYSTEM)//透传指令 // coll_transparent_return(msg_para.First_type,UART_TIMEOUT,NULL,0); // else // gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TIMEOUT,NULL,0); // } // msg_para_init();//清除存储的下发参数 } } //接收网关命令的返回 void gateway_msg_return(collect_gateway_msg_format_t *p_msg, uint8_t msg_type, uint16_t len) { char *msg = NULL; uint16_t *p_crc; p_msg->frame_header = 0xfefe; p_msg->proto_Ver = PROTOCOL_VERSION; p_msg->msg_id = msg_para.Msg_id; p_msg->first_type = msg_type; if(msg_type == FIRST_TYPE_SYSTEM) p_msg->second_type = SYSTEM_SECOND_TRANS_UP; else p_msg->second_type = 0x00; p_msg->msg_len = len+2;//后面加两字节的CRC p_crc = (uint16_t*)&p_msg->info[len]; *p_crc = CRC16_get((uint8_t *)p_msg,p_msg->msg_len+FRAME_HEADER_LEN-2); msg = (char *)lora_queue_mem_calloc_must(SYS_LORA_TX); if(msg) { memcpy(msg, (char *)p_msg, FRAME_HEADER_LEN + len+2); lora_queue_insert(SYS_LORA_TX,(char *)msg, FRAME_HEADER_LEN + len+2); } else { lora_queue_mem_free(SYS_LORA_TX,msg); } } //查询业务数据的返回 void gateway_down_business_return(uint8_t first_type, uint8_t state, uint8_t *data, uint8_t len) { collect_gateway_msg_format_t p_msg; business_query_info_t *p_tax = (business_query_info_t *)p_msg.info; p_tax->device_sn = device_info.device_sn; p_tax->coll_no = msg_para.Coll_no; p_tax->device_type = msg_para.Devices_type; p_tax->tax_no = msg_para.Tax_no; p_tax->gun_no = msg_para.Gun_no; p_tax->prot_type = msg_para.Prot_type; p_tax->status = state; if(state == UART_SUCCESS) { p_tax->data_len = len; memcpy(p_tax->info,data,len); } else { p_tax->data_len = 0; } if((first_type == COLL_GATE_TAX_REPLY) && (state != TAX_GUN_ERR)) { if(cmd_73_info[p_tax->tax_no-1].read_flag == 1) { p_tax->device_type = COLL_73; } } gateway_msg_return(&p_msg,first_type,p_tax->data_len+BUSINESS_QUERY_HEADER_LEN); // data_dump_debug(LORA_BUSINESS_DEBUG,"business return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN); if(state == UART_SUCCESS) { if(p_msg.first_type == COLL_GATE_ENCODER)//编码器数据 { data_dump_debug(MSG_ENCODER_DATA_TYPE1,"encoder return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN); } else { data_dump_debug(LORA_BUSINESS_DEBUG,"business return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN); } } else { data_dump_debug(MSG_ERR_DATA_TYPE1,"err return",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN); } } //报税口指示灯 void tax_led_set(uint8_t uartid, uint8_t state) { if(uartid == UART4_ID) { if(state == SEND_LED_FLAG) led_start(TAX1_SEND_LED); else if(state == RCV_LED_FLAG) led_start(TAX1_RCV_LED); } else if(uartid == UART5_ID) { if(state == SEND_LED_FLAG) led_start(TAX2_SEND_LED); else if(state == RCV_LED_FLAG) led_start(TAX2_RCV_LED); } } //网关下发查询业务数据分析 void gateway_down_business_proc(collect_gateway_msg_format_t *p_msg) { uint8_t encrypt=0; uint8_t order; uint8_t gunstatus; business_query_info_t *p_tax = (business_query_info_t *)p_msg->info; //保存下发的参数 msg_para.Msg_id = p_msg->msg_id; msg_para.Devices_type = p_tax->device_type; msg_para.Tax_no = p_tax->tax_no; msg_para.Gun_no = p_tax->gun_no; msg_para.Prot_type = p_tax->prot_type; printf_debug(MSG_DISPLAY_DOWN_TYPE2,"p_msg_dev_type:%04x\n",p_tax->device_type); if(p_msg->first_type == GATE_COLL_TAX_QUERY) //查报税口 { if(/*msg_para.Prot_type != device_fixed_info.Encrypt || */msg_para.Devices_type != TAX)//明文密文协议错误或查询设备类型错误 { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TYPE_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } else if(msg_para.Tax_no != 1 && msg_para.Tax_no != 2)//报税口号错误 { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_GUN_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } // if(tax_oil_judge_all())//报税口正在加油,不查询(所有报税口) // { // timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 // gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0); // msg_para_init();//清除存储的下发参数 // return; // } gunstatus = Gun_Signal_Check_all();//抬枪状态,有抬枪不读报税口 if(gunstatus) { timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0); msg_para_init();//清除存储的下发参数 return; } if(read_encoder_para.flag)//读编码器后2s内 { // gunstatus = Gun_Signal_Check_all(); if(gunstatus != read_encoder_para.gunstatus) { //至少有一把枪的抬枪状态发生了变化 timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0); msg_para_init();//清除存储的下发参数 return; } } if(msg_para.Tax_no==1) { encrypt = device_fixed_info.Encrypt&0x03; } else if(msg_para.Tax_no==2) { encrypt = device_fixed_info.Encrypt&0x0C; encrypt >>= 2; } if(((msg_para.Prot_type&0x0f) != encrypt) && (msg_para.Prot_type&0x0f)) { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); gateway_down_business_return(COLL_GATE_TAX_REPLY,UART_TYPE_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } if(((encrypt&PLAINTEXT) == PLAINTEXT)) //明文 { msg_para.Uart = UART4_ID+msg_para.Tax_no-1; tax_led_set(msg_para.Uart,SEND_LED_FLAG);//指示灯 } else //密文或未读出状态 { if((device_fixed_info.Encrypt&0x80) == 0x80) { msg_para.Uart = UART4_ID+msg_para.Tax_no-1; tax_send_info.uartid = UART4_ID+msg_para.Tax_no-1;//((msg_para.Tax_no == 0) ? UART3_ID : UART4_ID); tax_send_info.gun = msg_para.Gun_no - 1; tax_send_info.cmd = p_tax->info[3]; //if(tax_send_info.cmd != 0x70) memcpy(tax_send_info.data,p_tax->info,p_tax->data_len);// 将当前的指令存储 tax_send_info.dalen = p_tax->data_len; printf_debug(MSG_TAX_DOWN_TYPE3,"uart:%d, tax:%d gun:%d\n",msg_para.Uart+1,msg_para.Tax_no,msg_para.Gun_no); data_dump("收到指令 ", tax_send_info.data,tax_send_info.dalen); tax_send_start(GATE_COLL_TAX_QUERY); // 开始下发 return; } else { msg_para.Uart = UART4_ID; //判断加密模块是否死机条件 ciphertext_state.send_num++; if(ciphertext_state.send_num > 5) { ciphertext_time_start(); } } } //打开串口超时 timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); if(p_tax->data_len > 128)//透传的数据长度太长 { printf("uart send data too long\n"); return; } uart_msg_send(msg_para.Uart, (char *)p_tax->info,p_tax->data_len); printf_debug(MSG_TAX_DOWN_TYPE3,"uart:%d, tax:%d gun:%d\n",msg_para.Uart+1,msg_para.Tax_no,msg_para.Gun_no); data_dump_debug(MSG_TAX_DATA_TYPE1, "tax send", (uint8_t *)p_tax->info,p_tax->data_len); } else if(p_msg->first_type == GATE_COLL_XSP_QUERY) //查显示屏 { if(msg_para.Devices_type != XSP_0904) //查询设备类型错误 { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); gateway_down_business_return(COLL_GATE_XSP_REPLY,UART_TYPE_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } msg_para.Uart = UART3_ID; //指示灯 led_start(DISPLAY_SEND_LED); //打开串口超时 timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); if(p_tax->data_len > BUSINESS_DATA_LEN)//透传的数据长度太长 { printf("uart send data too long\n"); return; } // RS485_TX(); uart_msg_send(msg_para.Uart, (char *)p_tax->info,p_tax->data_len); // delay_us(120);//极限为210us,大于210us数据接收不完整。 // RS485_RX(); printf_debug(MSG_DISPLAY_DOWN_TYPE3,"uart:%d, tax:%d gun:%d\n",msg_para.Uart+1,msg_para.Tax_no,msg_para.Gun_no); data_dump_debug(MSG_DISPLAY_DATA_TYPE1, "xsp send", (uint8_t *)p_tax->info,p_tax->data_len); } else if(p_msg->first_type == GATE_COLL_ENCODER) //查询编码器数据 { order = p_tax->info[0]; printf_debug(MSG_ENCODER_DOWN_TYPE2, "port:%d, gun:%d, order:%d\n",p_tax->tax_no,p_tax->gun_no, order); if(p_tax->device_type != ENCODER)//查询设备类型错误 { gateway_down_business_return(COLL_GATE_ENCODER,UART_TYPE_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } if((msg_para.Tax_no != 1 && msg_para.Tax_no != 2))//报税口号错误 { gateway_down_business_return(COLL_GATE_ENCODER,TAX_GUN_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } if(order > 3)//编码器指令错误 { gateway_down_business_return(COLL_GATE_ENCODER,ORDER_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } if(order <= 1)//读单个枪 { if(msg_para.Gun_no == 0 || msg_para.Gun_no > ENCODER_TAX_NUM*ENCODER_GUN_NUM)//报税口号或枪号错误 { gateway_down_business_return(COLL_GATE_ENCODER,TAX_GUN_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } if(msg_para.Gun_no > ENCODER_GUN_NUM)//枪号大于一个编码口的最大枪数,报税口号只能为1 { if(msg_para.Tax_no != 1)//报税口号或枪号错误 { gateway_down_business_return(COLL_GATE_ENCODER,TAX_GUN_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } coll_encoder_return(1,msg_para.Gun_no-ENCODER_GUN_NUM,order); } else { coll_encoder_return(msg_para.Tax_no,msg_para.Gun_no,order); } } else if(order <= 3)//读单个报税口 { coll_encoder_return(msg_para.Tax_no,msg_para.Gun_no,order); } msg_para_init();//清除存储的下发参数 } } void tax_rcv_proc(uint8_t *data, uint16_t len) { tax_rcv_analyze(data,&tax_info,len); } //心跳包响应 void coll_heartbeat_return(void) { collect_gateway_msg_format_t p_msg; coll_heartbeat_t *p_heart = (coll_heartbeat_t *)p_msg.info; p_heart->device_sn = device_info.device_sn; p_heart->coll_no = msg_para.Coll_no; p_heart->status = device_fixed_info.Encrypt; gateway_msg_return(&p_msg,COLL_GATE_HEARTBEAT,HEARTBEAT_HEADER_LEN); data_dump("heartbeat",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN); } void tax_ciphertext_0x8C_process(void) { // uint8_t para[10],para_len; uint8_t send_buf[64]; uint16_t send_len; uint8_t gunstatus; if(tax_send_info.start) { if(tax_send_info.send) { gunstatus = Gun_Signal_Check_all();//抬枪状态,有抬枪不读报税口 if(gunstatus) { timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 if(tax_send_info.startcmd == GATE_COLL_HEARTBEAT)//读心跳 { if(read_56_info.step==2) { coll_heartbeat_return(); read_56_info.step = 0; msg_para_init();//清除存储的下发参数 tax_send_end(); return; } else { read_56_info.step = 2; tax_send_end(); read_56_start(UART5_ID); return; } } else if(tax_send_info.startcmd == GATE_COLL_TAX_QUERY) { gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0); } msg_para_init();//清除存储的下发参数 tax_send_end(); return; } if(read_encoder_para.flag)//读编码器后2s内 { // gunstatus = Gun_Signal_Check_all(); if(gunstatus != read_encoder_para.gunstatus) { //至少有一把枪的抬枪状态发生了变化 timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 if(tax_send_info.startcmd == GATE_COLL_HEARTBEAT)//读心跳 { if(read_56_info.step==2) { coll_heartbeat_return(); read_56_info.step = 0; msg_para_init();//清除存储的下发参数 tax_send_end(); return; } else { read_56_info.step = 2; tax_send_end(); read_56_start(UART5_ID); return; } } else if(tax_send_info.startcmd == GATE_COLL_TAX_QUERY) { gateway_down_business_return(COLL_GATE_TAX_REPLY,TAX_BUSY,NULL,0); } msg_para_init();//清除存储的下发参数 tax_send_end(); return; } } tax_send_info.send = 0; switch(tax_send_info.step) { case CMD_72: send_len = tax_send_cmd_ciphertext(0x72,NULL,0,send_buf); uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len); timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯 printf("send uartid:%d ",tax_send_info.uartid); data_dump("tax send",send_buf,send_len); break; case CMD_73: send_len = tax_send_cmd_ciphertext(0x73,tax_info.cmd_73_data,4,send_buf); uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len); timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯 printf("send uartid:%d ",tax_send_info.uartid); data_dump("tax send",send_buf,send_len); break; case CMD_70: send_len = tax_send_cmd_ciphertext(0x70,NULL,0,send_buf); uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len); timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯 printf("send uartid:%d ",tax_send_info.uartid); data_dump("tax send",send_buf,send_len); break; case CMD_8C: //send_len = tax_send_cmd_ciphertext(0x8C,tax_send_info.data,tax_send_info.dalen,send_buf); uart_msg_send(tax_send_info.uartid,(char *)tax_send_info.data,tax_send_info.dalen); timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯 printf("send uartid:%d ",tax_send_info.uartid); data_dump("tax send",tax_send_info.data,tax_send_info.dalen); break; case CMD_86: send_len = tax_send_cmd(0x86,1,send_buf); uart_msg_send(tax_send_info.uartid,(char *)send_buf,send_len); timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); tax_led_set(tax_send_info.uartid,SEND_LED_FLAG);//指示灯 printf("send uartid:%d ",tax_send_info.uartid); data_dump("tax send",send_buf,send_len); break; default: tax_send_end(); break; } } } } uart_fifo_t uart_fifo[5]; //清除串口缓存数据 void uart_fifo_clear(uint8_t uart_id) { if(uart_fifo[uart_id].flag) { uart_fifo[uart_id].flag = 0; uart_fifo[uart_id].len = 0; memset(uart_fifo[uart_id].data,0,256); } } uint8_t rcv_buf_uart3[256]; uint16_t rcv_size_uart3; //业务串口接收处理 void business_uart_rcv_proc(void) { uint8_t i,first_type; uint8_t tembuf[128] = {0}; for(i = 0; i < 2; i++) { // rcv_size = uart_blocking_read(rcv_buf,UART3_ID+i); rcv_size = uart_blocking_read((char *)rcv_buf,UART4_ID+i,1); if(rcv_size) { rcv_size_temp = rcv_size; rcv_size = uart_blocking_read((char *)rcv_buf+rcv_size_temp, UART4_ID+i, 50); rcv_size+=rcv_size_temp; printf("UART:%d\n",UART4_ID+i+1); data_dump("uart rcv", rcv_buf, rcv_size); if(rcv_size > 128) { printf("uart rcv too long\n"); return; } if(rcv_buf[2] != 0xFF)//帧号不为0xFF, { if(uart_fifo[UART4_ID+i].flag == 0) { uart_fifo[UART4_ID+i].len = 0; uart_fifo[UART4_ID+i].flag = 1; } if((uart_fifo[UART4_ID+i].len + rcv_size) > 128) { uart_fifo[UART4_ID+i].flag = 0; uart_fifo[UART4_ID+i].len = 0; return; } else{ memcpy(uart_fifo[UART4_ID+i].data+uart_fifo[UART4_ID+i].len,rcv_buf,rcv_size); uart_fifo[UART4_ID+i].len += rcv_size; if(rcv_buf[2] != 0x01) return; } } if(uart_fifo[UART4_ID+i].flag) { uart_fifo[UART4_ID+i].flag = 0; memcpy(rcv_buf,uart_fifo[UART4_ID+i].data,uart_fifo[UART4_ID+i].len); rcv_size = uart_fifo[UART4_ID+i].len; uart_fifo_clear(UART4_ID+i);//清除串口缓存数据 } printf("begin proc\n"); if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_3) { if(msg_rcv_info.uart == UART4_ID+i) { if((msg_rcv_info.first_type == FIRST_SYSTEM_TYPE) && (msg_rcv_info.second_type == SECOND_SYSTEM_TRANS_DOWN))//透传数据 { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); //指示灯 tax_led_set(UART4_ID+i,RCV_LED_FLAG); transparent_up_ver3_return(UART_SUCCESS,rcv_buf,rcv_size); msg_rcv_info_init(); } } } else { printf("msg_para.uart = %d,msg_para.first_type:%d,tax_send_start:%d\n",msg_para.Uart, msg_para.First_type,tax_send_info.start); if(msg_para.Uart == UART4_ID+i) { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); //指示灯 tax_led_set(UART4_ID+i,RCV_LED_FLAG); first_type = COLL_GATE_TAX_REPLY; if(tax_send_info.start == 1){ if( (tax_send_info.startcmd == GATE_COLL_TAX_QUERY) && (tax_send_info.step == CMD_8C)){ // 需要上报的指令 memcpy(tembuf,tax_info.tax_serino,10); memcpy(&tembuf[10],rcv_buf,rcv_size); gateway_down_business_return(first_type,UART_SUCCESS,tembuf,rcv_size+10); tax_send_end(); msg_para_init();//清除存储的下发参数 } else if((tax_send_info.startcmd == GATE_COLL_TAX_QUERY) && (tax_send_info.step == CMD_70)){ if(tax_send_info.cmd == 0x70){ memcpy(&tembuf[10],rcv_buf,rcv_size); gateway_down_business_return(first_type,UART_SUCCESS,tembuf,rcv_size+10); tax_send_end(); msg_para_init();//清除存储的下发参数 } else tax_rcv_proc(rcv_buf,rcv_size); // 解析串口收到的72 73 79 指令 } else if((tax_send_info.startcmd == GATE_COLL_HEARTBEAT) && ((tax_send_info.step == CMD_73) || (tax_send_info.step == CMD_86))) { tax_rcv_proc(rcv_buf,rcv_size); if(read_56_info.step == 2) { read_56_info.step = 0; tax_send_end(); coll_heartbeat_return(); msg_para_init();//清除存储的下发参数 return; } else { read_56_info.step = 2; tax_send_end(); read_56_start(UART5_ID); return; } // tax_send_end(); // coll_heartbeat_return(); // msg_para_init();//清除存储的下发参数 // return; } else { tax_rcv_proc(rcv_buf,rcv_size); // 解析串口收到的72 73 79 指令 } return; } if(msg_para.First_type == FIRST_TYPE_SYSTEM)//透传指令 coll_transparent_return(msg_para.First_type,UART_SUCCESS,rcv_buf,rcv_size); else gateway_down_business_return(first_type,UART_SUCCESS,rcv_buf,rcv_size); msg_para_init();//清除存储的下发参数 } } } } rcv_size_uart3 = uart_blocking_read((char *)rcv_buf_uart3,UART3_ID,1); if(rcv_size_uart3) { rcv_size_temp = rcv_size_uart3; rcv_size_uart3 = uart_blocking_read((char *)rcv_buf_uart3+rcv_size_uart3, UART3_ID, 50); rcv_size_uart3+=rcv_size_temp; // data_dump("uart3 rcv",rcv_buf,rcv_size); data_dump("uart3 rcv prot",rcv_buf_uart3,rcv_size_uart3); if(rcv_size_uart3 > BUSINESS_DATA_LEN) { printf("uart rcv too long\n"); return; } if((device_fixed_info.Work_State == DEV_WORK_STATE_UPDATING) && (ota_state.update_mode == OTA_WORK_STATE_XSP) && (xsp_ota_state.state == OTA_RETRANSMIT)) { //升级显示屏且重传阶段 xsp_master_ota_proc(rcv_buf_uart3,rcv_size_uart3); return; } if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_3) { if(msg_rcv_info.uart == UART_485) { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); //指示灯 led_start(DISPLAY_RCV_LED); if((msg_rcv_info.first_type == FIRST_SYSTEM_TYPE) && (msg_rcv_info.second_type == SECOND_SYSTEM_TRANS_DOWN))//透传数据 { transparent_up_ver3_return(UART_SUCCESS,rcv_buf_uart3,rcv_size_uart3); msg_rcv_info_init(); } } } else if(msg_rcv_info.proto_Ver == PROTOCOL_VERSION_FF)//版本查询 { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); //指示灯 led_start(DISPLAY_RCV_LED); //直接通过Lora返回 lora_queue_insert(SYS_LORA_TX,(char *)rcv_buf_uart3, rcv_size_uart3); msg_rcv_info_init(); } else { if(msg_para.Uart == UART_485) { //关闭串口超时 timeout_stop(&lora_comm_info.uart_timeout); //指示灯 led_start(DISPLAY_RCV_LED); first_type = COLL_GATE_XSP_REPLY; gateway_down_business_return(first_type,UART_SUCCESS,rcv_buf_uart3,rcv_size_uart3); msg_para_init();//清除存储的下发参数 } } } } //73指令返回 void coll_cmd73_return(uint8_t state) { collect_gateway_msg_format_t p_msg; cmd73_query_info_t *p_tax = (cmd73_query_info_t *)p_msg.info; p_tax->device_sn = device_info.device_sn; p_tax->coll_no = msg_para.Coll_no; p_tax->device_type = COLL_73; p_tax->tax_no = msg_para.Tax_no; p_tax->status = state; if(state != UART_SUCCESS) { p_tax->data_len = 0; } else { p_tax->data_len = cmd_73_info[p_tax->tax_no-1].len; memcpy(p_tax->info,cmd_73_info[p_tax->tax_no-1].data,cmd_73_info[p_tax->tax_no-1].len); cmd_73_info[p_tax->tax_no-1].read_flag = 0; } gateway_msg_return(&p_msg,COLL_GATE_CMD73,CMD73_QUERY_HEADER_LEN+p_tax->data_len); data_dump("cmd73",(uint8_t *)&p_msg,p_msg.msg_len+FRAME_HEADER_LEN); } //网关下发查询73指令分析 void gateway_down_cmd73_proc(collect_gateway_msg_format_t *p_msg) { cmd73_query_info_t *p_tax = (cmd73_query_info_t *)p_msg->info; //保存下发的参数 msg_para.Msg_id = p_msg->msg_id; msg_para.Devices_type = p_tax->device_type; msg_para.Tax_no = p_tax->tax_no; if((p_tax->tax_no != 1) && (p_tax->tax_no != 2)) { coll_cmd73_return(TAX_GUN_ERR); msg_para_init();//清除存储的下发参数 return; } coll_cmd73_return(UART_SUCCESS); msg_para_init();//清除存储的下发参数 } //查询固件信息响应 void coll_hard_return(void) { collect_gateway_msg_format_t p_msg; coll_hard_reply_t *p_hard = (coll_hard_reply_t *)p_msg.info; p_hard->device_sn = device_info.device_sn; p_hard->coll_no = msg_para.Coll_no; p_hard->bootloader_vr = device_fixed_info.Soft_ver_boot; p_hard->app_vr = device_fixed_info.Soft_ver_app; p_hard->reset_num = device_fixed_info.Reset_total_cnt; p_hard->last_reset_type = device_fixed_info.Last_reset_flag; p_hard->run_times = TickCounter_u64/1000; p_hard->voltage = device_fixed_info.Voltage; p_hard->temperature = device_fixed_info.Temperature; p_hard->updata_state = updata_state; memcpy(p_hard->UUID,device_fixed_info.MCU_UUID,12); gateway_msg_return(&p_msg,COLL_GATE_HARD_REPLY,HARD_HEADER_LEN); } //网关下发设置Lora组号处理函数 void gateway_coll_lorapara_proc(collect_gateway_msg_format_t *p_msg) { uint8_t eeprom_buf[2] = {0}; gateway_lora_para_t *p_lora = (gateway_lora_para_t *)p_msg->info; printf("coll_lora_group:%d, gate_lora_group:%d\n",lora_para_choice,p_lora->lora_group); if(p_lora->lora_group < LORA_GROUP_NUM) //lora组号索引正确 { if(lora_para_choice != p_lora->lora_group)//切换组号 { lora_para_choice = p_lora->lora_group; lora_init(); //写eeprom eeprom_buf[0] = lora_para_choice; eeprom_buf[1] = ~eeprom_buf[0]; AT24CXX_Write(LORA_ADDR,(uint8_t *)eeprom_buf,2); printf_debug(MSG_LORA_DOWN_TYPE2, "lora choice:%d\n",lora_para_choice); } } } //时间同步处理函数 void gateway_coll_timesync_proc(collect_gateway_msg_format_t *p_msg) { time_sync_t *p_time = (time_sync_t *)p_msg->info; RTC_Set_Stamp(p_time->time_stamp); } //编码器返回函数 void coll_encoder_return(uint8_t tax, uint8_t gun, uint8_t order) { uint8_t data[128]= {0}, len = 0; //读编码器后计时参数 timeout_stop(&read_encoder_para.tt_encoder); read_encoder_para.flag = 0; if(order == 0 || order == 1) { len = coll_encoder_return_0_1(tax,gun,order,data); } else if(order == 2 || order == 3) { len = coll_encoder_return_2_3(tax,gun,order,data); } gateway_down_business_return(COLL_GATE_ENCODER,UART_SUCCESS,data,len); } //根据设备类型复位分析 void gateway_coll_reboot_type_proc(collect_gateway_msg_format_t *p_msg) { reboot_type_t *p_reb = (reboot_type_t *)p_msg->info; switch(p_reb->cmd) { case 0x01://设备复位 if(p_reb->device_type == 0x0201)//采集器复位 { printf("cpu reset\n"); NVIC_SystemReset(); } else if(p_reb->device_type == 0x0601)//加密模块 { if(device_fixed_info.Encrypt == CIPHERTEXT)//密文 { //复位加密芯片 port_init(PORT_LED_OFF); //初始化加密模块 printf("ciphertext reset\n"); } } break; case 0x02://透传口复位 if(p_reb->device_type == 0x0201)//采集器 { tax_sw_reset(3); printf("pos reset\n"); } break; } } //透传指令处理 void gateway_coll_transparent_proc(collect_gateway_msg_format_t *p_msg) { uint8_t gunstatus; transparent_info_t *p_tax = (transparent_info_t *)p_msg->info; //保存下发的参数 msg_para.Msg_id = p_msg->msg_id; msg_para.First_type = p_msg->first_type; msg_para.Second_type = p_msg->second_type; msg_para.Tax_no = p_tax->tax_no; msg_para.Gw_sn = p_tax->gw_sn; if((p_tax->tax_no != 1) && (p_tax->tax_no != 2)) { timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 coll_transparent_return(msg_para.First_type,TAX_GUN_ERR,NULL,0); msg_para_init();//清除存储的下发参数 return; } gunstatus = Gun_Signal_Check_all();//抬枪状态,有抬枪不读报税口 if(gunstatus) { timeout_stop(&lora_comm_info.uart_timeout);//关闭串口超时 coll_transparent_return(msg_para.First_type,TAX_BUSY,NULL,0); msg_para_init();//清除存储的下发参数 return; } //报税口发送指示灯 if(msg_para.Tax_no == 1) { led_start(TAX1_SEND_LED); } else if(msg_para.Tax_no == 2) { led_start(TAX2_SEND_LED); } //报税口对应的串口号 if(device_fixed_info.Encrypt == PLAINTEXT) //明文 { if(msg_para.Tax_no == 1) { msg_para.Uart = UART4_ID; } else if(msg_para.Tax_no == 2) { msg_para.Uart = UART5_ID; } } else if(device_fixed_info.Encrypt == CIPHERTEXT) //密文 { msg_para.Uart = UART2_ID; } //打开串口超时 timeout_setValue(&lora_comm_info.uart_timeout,UART_TIMEOUT_TIMER,1); if(p_tax->data_len > 128)//透传的数据长度太长 { printf("uart send data too long\n"); return; } uart_msg_send(msg_para.Uart, (char *)p_tax->info,p_tax->data_len); printf_debug(MSG_TAX_DOWN_TYPE3,"uart:%d, tax:%d\n",msg_para.Uart+1,msg_para.Tax_no); data_dump_debug(MSG_TAX_DATA_TYPE1, "tax send", (uint8_t *)p_tax->info,p_tax->data_len); } //采集器透传返回 void coll_transparent_return(uint8_t first_type, uint8_t state, uint8_t *data, uint8_t len) { collect_gateway_msg_format_t p_msg; transparent_info_t *p_tax = (transparent_info_t *)p_msg.info; p_tax->gw_sn = msg_para.Gw_sn; p_tax->coll_sn = device_info.device_sn; p_tax->tax_no = msg_para.Tax_no; p_tax->status = state; if(state == UART_SUCCESS) { p_tax->data_len = len; memcpy(p_tax->info,data,len); } else { p_tax->data_len = 0; } gateway_msg_return(&p_msg,first_type,p_tax->data_len+TRANSPARENT_HEADER_LEN); } //读56初始化 void read_56_init(void) { memset(&read_56_info,0,sizeof(read_56_info_t)); } //读56开始 void read_56_start(uint8_t uart_id) { tax_send_info.uartid = uart_id; msg_para.Uart = uart_id; tax_send_start(GATE_COLL_HEARTBEAT); if(uart_id == UART3_ID) { if(read_56_info.read_way_tax1==0)//读86 { tax_send_info.step = CMD_86; } read_56_info.read_way_tax1 = !read_56_info.read_way_tax1; } else if(uart_id == UART4_ID) { if(read_56_info.read_way_tax2==0)//读86 { tax_send_info.step = CMD_86; } read_56_info.read_way_tax2 = !read_56_info.read_way_tax2; } // if(read_56_info.read_way)//读86 // { // tax_send_info.step = CMD_86; // } } //处理网关下发的控制命令 void gateway_coll_ctrl_comm(collect_gateway_msg_format_t *p_msg) { OS_ERR err; uint8_t tax1=0,tax2=0,tax_num=0; device_sn_info_t *p_sn = (device_sn_info_t *)p_msg->info; switch (p_msg->first_type) { case GATE_COLL_CMD73://网关下发查询73指令 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; printf("device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf("type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_down_cmd73_proc(p_msg); } break; case GATE_COLL_XSP_QUERY://网关下发查询显示屏数据 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; printf_debug(MSG_DISPLAY_DOWN_TYPE1,"device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_DISPLAY_DOWN_TYPE1,"type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_down_business_proc(p_msg); } break; case GATE_COLL_DISPLAY_QUERY://网关下发查询显示屏数据 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; printf_debug(MSG_DISPLAY_DOWN_TYPE1,"device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_DISPLAY_DOWN_TYPE1,"type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_down_business_proc(p_msg); } break; case GATE_COLL_HEARTBEAT://网关与采集器的下行心跳包 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; msg_para.Msg_id = p_msg->msg_id; printf("device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf("type:%x\n",p_msg->first_type); // data_dump("56 rcv",(uint8_t *)p_msg,p_msg->msg_len+FRAME_HEADER_LEN); //lora接收指示灯 led_start(LORA_RCV_LED); if(p_msg->msg_len > 7)//新协议 { printf("tax index:%d\n",p_msg->info[5]); if(p_msg->info[5] == 0x01)//报税口1 { if((device_fixed_info.Encrypt&0x03) == 0x00) { read_56_info.step = 2; read_56_start(UART4_ID); } else { delay_ms(20); coll_heartbeat_return(); } } if(p_msg->info[5] == 0x02)//报税口2 { if((device_fixed_info.Encrypt&0x0C) == 0x00) { read_56_info.step = 2; read_56_start(UART5_ID); } else { delay_ms(20); coll_heartbeat_return(); } } else if(p_msg->info[5] == 0x03)//报税口12 { if((device_fixed_info.Encrypt&0x03) != 0x00 && (device_fixed_info.Encrypt&0x0C) != 0x00)//报税口12 { delay_ms(20); coll_heartbeat_return(); } else { if((device_fixed_info.Encrypt&0x03) == 0x00)// { tax1 = 1; tax_num++; } if((device_fixed_info.Encrypt&0x0C) == 0x00)// { tax2 = 1; tax_num++; } if(tax_num==1) { if(tax1) { read_56_start(UART4_ID); } else if(tax2) { read_56_start(UART5_ID); } read_56_info.step = 2; } else if(tax_num==2) { read_56_start(UART4_ID); read_56_info.step = 1; } } } } else//老协议,只读报税口1 { if((device_fixed_info.Encrypt&0x03) == 0x00) { read_56_info.step = 2; read_56_start(UART4_ID); } else { delay_ms(20); coll_heartbeat_return(); } } } break; case GATE_COLL_TAX_QUERY: //网关下发查询采集器数据(报税口) if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; printf_debug(MSG_TAX_DOWN_TYPE1,"device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_TAX_DOWN_TYPE1,"type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_down_business_proc(p_msg); } break; case GATE_COLL_HARD_QUERY: //网关下发查询固件信息 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; msg_para.Msg_id = p_msg->msg_id; printf_debug(MSG_HART_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_HART_DOWN_TYPE1, "type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); OSTimeDlyHMSM(0, 0, 0, 20, OS_OPT_TIME_DLY, &err); coll_hard_return(); } break; case GATE_COLL_REBOOT: //采集器设备重新启动 if(p_sn->device_sn == device_info.device_sn || p_sn->device_sn == 0xffffffff) { msg_para.Coll_no = p_sn->coll_no; msg_para.Msg_id = p_msg->msg_id; printf_debug(MSG_REBOOT_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_REBOOT_DOWN_TYPE1, "type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); NVIC_SystemReset(); } break; case GATE_COLL_CTRL_LORAPARA://网关下发切换lora参数 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; msg_para.Msg_id = p_msg->msg_id; printf_debug(MSG_LORA_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_LORA_DOWN_TYPE1, "type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_coll_lorapara_proc(p_msg); } break; case GATE_COLL_TIMESYNC: //时间同步 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; msg_para.Msg_id = p_msg->msg_id; printf_debug(MSG_LORA_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_LORA_DOWN_TYPE1, "type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_coll_timesync_proc(p_msg); } break; case GATE_COLL_ENCODER: //查询编码器数据 if(hd_id == PCB_V1) { if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; printf_debug(MSG_ENCODER_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_ENCODER_DOWN_TYPE1, "type:%x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_down_business_proc(p_msg); } } break; case GATE_COLL_REBOOT_TYPE: //根据设备类型重启 if(p_sn->device_sn == device_info.device_sn) { msg_para.Coll_no = p_sn->coll_no; msg_para.Msg_id = p_msg->msg_id; printf_debug(MSG_REBOOT_TYPE_DOWN_TYPE1, "device_sn=%010u<0x%08x>\n",p_sn->device_sn,p_sn->device_sn); printf_debug(MSG_REBOOT_TYPE_DOWN_TYPE1, "type:%04x\n",p_msg->first_type); //lora接收指示灯 led_start(LORA_RCV_LED); gateway_coll_reboot_type_proc(p_msg); } break; default: break; } }