| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /*
- * @Description:
- * @Version: 2.0
- * @Author: Seven
- * @Date: 2022-08-22 09:55:26
- * @LastEditors: Seven
- * @LastEditTime: 2022-09-27 15:18:29
- */
- #ifndef __GW_CTRL_H__
- #define __GW_CTRL_H__
- #include "stm32f10x.h"
- #include "tools.h"
- #include "Uart.h"
- #define PROTOCOL_VERSION 0X01 //协议版本
- #define PROTOCOL_VERSION_2 0x02 //2.0协议版本
- #define PROTOCOL_VERSION_3 0x03 //3.0协议版本
- #define PROTOCOL_VERSION_FF 0xFF //FF协议版本,查当前协议版本
- #define UART_TIMEOUT_TIMER (1000) //串口超时时间
- #define LORA_GROUP_NUM 4 //Lora组个数
- #define LORA_ADDR 0X40 //Lora参数起始地址
- #define LORA_ADDR_OFFSET 0x0C //Lora参数地址偏移量
- //协议消息类型
- /*网关下发*/
- #define GATE_COLL_CMD73 0x52 //读取73指令
- #define GATE_COLL_XSP_QUERY 0x54 //显示屏(0904)命令下行
- #define GATE_COLL_DISPLAY_QUERY 0x55 //网关下发查询显示屏数据
- #define GATE_COLL_HEARTBEAT 0X56 //网关与采集器的下行心跳包
- #define GATE_COLL_TAX_QUERY 0x57 //网关下发查询采集器数据(报税口、显示屏)
- #define GATE_COLL_HARD_QUERY 0x58 //网关下发查询固件信息
- #define GATE_COLL_REBOOT 0x59 //采集器设备重新启动
- #define GATE_COLL_TIMESYNC 0x5A //时间同步
- #define GATE_COLL_CTRL_LORAPARA 0x5B //网关下发切换lora参数
- #define GATE_COLL_ENCODER 0x5C //网关下发查询编码器数据
- #define GATE_COLL_REBOOT_TYPE 0x5D //根据设备类型重启
- /*采集器上传*/
- #define COLL_GATE_CMD73 0x62 //返回73指令
- #define COLL_GATE_XSP_REPLY 0x64 //显示屏(0904)命令上行
- //#define COLL_GATE_DISPLAY_REPLY 0x65 //采集器上传显示屏查询数据
- #define COLL_GATE_HEARTBEAT 0x66 //网关与采集器的上行心跳包
- #define COLL_GATE_TAX_REPLY 0x67 //采集器上传报税口查询的数据
- #define COLL_GATE_HARD_REPLY 0x68 //采集器上发固件信息
- #define COLL_GATE_ENCODER 0x6C //采集器上传编码器数据
- #define FIRST_TYPE_SYSTEM 0x01 //一级类型 系统类 transparent
- #define SYSTEM_SECOND_TRANS_DOWN 0x1052 //透传指令下行
- #define SYSTEM_SECOND_TRANS_UP 0x2052 //透传指令上行
- #define PLAINTEXT 1 //明文
- #define CIPHERTEXT 2 //密文
- #define POS 4 //POS口
- #define TAX 0x0201 //报税口
- #define DISPLAY 0x1FFF //显示屏
- #define XSP_0904 0x0904 //显示屏(0904)
- #define ENCODER 0x0501 //编码器
- #define COLL 0x0201 //采集器
- #define COLL_73 0x0701 //读73指令
- #define UART_SUCCESS 1 //报税口或显示屏口收到数据
- #define UART_TIMEOUT 2 //报税口或显示屏口超时
- #define UART_TYPE_ERR 3 //明文密文协议错误或查询设备类型错误
- #define TAX_GUN_ERR 4 //报税口号或枪号错误
- #define ORDER_ERR 5 //编码器指令错误
- #define TAX_BUSY 6 //报税口忙,正在加油
- //报税口超时连续次数
- typedef struct _uart_timeout_times_t
- {
- uint8_t tax1_ch;
- uint32_t tax1_times;
- uint8_t tax2_ch;
- uint32_t tax2_times;
- }uart_timeout_times_t;
- extern uart_timeout_times_t uart_timeout_times;
- #define CIPHERTEXT_ERR_LEN 12//加密模块返回错误指令的最大长度(实际为10字节)
- #define CIPHERTEXT_RESET_TIME (1*60*60*1000) //加密模块死机重启定时时间(1hour)
- typedef struct _ciphertext_state
- {
- // uint8_t flag;
- uint8_t send_num;
- // uint8_t rcv_num;
- timeout_t ciphertext;
- }ciphertext_state_t;
- extern ciphertext_state_t ciphertext_state;
- //读编码器时参数记录
- #define ENCODER_GUNSTATUS_TIMEOUT (2*1000)//2S
- typedef struct _read_encoder_para_t
- {
- uint8_t flag; //标记,1 表示1s定时未结束,需读取抬枪状态; 0 表示已结束,不用读取
- uint8_t tax; //上次读取报税口
- uint8_t gunstatus; //上次抬枪状态
- timeout_t tt_encoder; //编码器定时器
- }read_encoder_para_t;
- extern read_encoder_para_t read_encoder_para;
- //下发命令参数存储
- typedef struct _msg_para_t
- {
- uint32_t Msg_id; //消息序列号
- uint8_t Coll_no; //采集器号
- uint16_t Devices_type; //设备类型
- uint8_t Uart; //串口号
- uint8_t Tax_no; //报税口号
- uint8_t Gun_no; //枪号
- uint8_t Prot_type; //协议类型 1表示明文,2表示密文
- uint8_t First_type; //一级消息类型
- uint16_t Second_type; //二级消息类型
- uint32_t Gw_sn; //网关sn
- }msg_para_t;
- extern msg_para_t msg_para;
- //Lora通信信息
- typedef struct _lora_comm_info_t
- {
- timeout_t uart_timeout;
- }lora_comm_info_t;
- extern lora_comm_info_t lora_comm_info;
- //网关下发的设备sn信息
- typedef struct _device_sn_info
- {
- uint32_t device_sn;
- uint8_t coll_no;
- }__attribute__((packed)) device_sn_info_t;
- #define MSG_DATA_LEN 244
- //网关和采集器通信协议头
- typedef struct _collect_gateway_msg_format
- {
- uint16_t frame_header; //帧头
- uint8_t proto_Ver; //版本号
- uint32_t msg_id; //消息序列号
- uint8_t first_type; //一级消息类型
- uint16_t second_type; //二级消息类型
- uint16_t msg_len; //消息长度
- char info[MSG_DATA_LEN];
- }__attribute__((packed)) collect_gateway_msg_format_t;
- //网关读取73指令结构体
- typedef struct _cmd73_query_info
- {
- uint32_t device_sn; //设备sn
- uint8_t coll_no; //采集器号
- uint16_t device_type; //查询设备类型(0x0701)
- uint8_t tax_no; //报税口号
- uint8_t status; //执行结果(1:成功 ;4:报税口号或枪号错误;)
- uint16_t data_len; //后面数据长度
- char info[128];
- }__attribute__((packed)) cmd73_query_info_t;
- #define BUSINESS_DATA_LEN 229
- //网关查询业务数据的结构体(报税口、显示屏、编码器)
- typedef struct _business_query_info
- {
- uint32_t device_sn; //设备sn
- uint8_t coll_no; //采集器号
- uint16_t device_type; //查询设备类型(0x0101:网关;0x0201:采集器;0x0301:液位仪采集器;0x0401:屏采集器;0x0501:编码器)
- uint8_t tax_no; //报税口号
- uint8_t gun_no; //枪号
- uint8_t prot_type; //协议类型 1表示明文,2表示密文
- uint8_t status; //执行结果 1:成功 2:读取数据超时
- uint16_t data_len; //后面数据长度
- char info[BUSINESS_DATA_LEN+2];
- }__attribute__((packed)) business_query_info_t;
- //心跳包返回结构体
- typedef struct _coll_heartbeat
- {
- uint32_t device_sn; //设备sn
- uint8_t coll_no; //采集器号
- uint8_t status; //采集器明文密文类型,1表示明文,2表示密文 (网关下发时没有这个参数)
- }__attribute__((packed)) coll_heartbeat_t;
- //采集器固件信息上传
- typedef struct _coll_hard_reply
- {
- uint32_t device_sn; //设备sn
- uint8_t coll_no; //采集器号
- uint32_t bootloader_vr; //bootloader版本
- uint32_t app_vr; //app版本
- uint32_t reset_num; //复位次数
- uint8_t last_reset_type; //最后一次复位类型
- uint32_t run_times; //运行时长
- uint32_t voltage; //电压
- uint32_t temperature; //温度
- uint8_t updata_state; //升级状态
- uint8_t UUID[12]; //UUID
- }__attribute__((packed)) coll_hard_reply_t;
- //Lora下发参数
- typedef struct _gateway_lora_para
- {
- uint32_t device_sn; //设备sn
- uint8_t coll_no; //采集器号
- uint8_t para_flag; //0:仅lora_paraIndex参数字段有效,其他参数字段无效,默认。 1:全部参数字段有效
- uint8_t lora_group; //Lora参数组号
- uint32_t lora_fre; //频率
- uint8_t lora_tx_output_power; //功率
- uint8_t lora_bandwidth; //带宽
- uint8_t lora_spreading_factor; //扩频因子
- uint8_t lora_codingrate; //纠错编码率
- uint8_t lora_preamble_length; //前导码长度
- }__attribute__((packed)) gateway_lora_para_t;
- //时间同步
- typedef struct _time_sync
- {
- uint32_t device_id; //设备编号
- uint8_t coll_no; //采集器号
- uint32_t time_stamp; //时间戳
- }__attribute__((packed)) time_sync_t;
- //根据设备类型复位
- typedef struct _reboot_type_t
- {
- uint32_t device_id; //设备sn
- uint8_t coll_no; //采集器号
- uint16_t device_type; //设置类型
- uint8_t cmd; //命令(0x01,重启)
- uint32_t reserve; //预留
- }__attribute__((packed)) reboot_type_t;
- //透传指令
- typedef struct _transparent_info_t
- {
- uint32_t gw_sn; //网关sn
- uint32_t coll_sn; //采集器sn
- uint8_t tax_no; //报税口号
- uint8_t status; //状态
- uint16_t data_len; //数据长度
- uint8_t info[128]; //数据
- }__attribute__((packed)) transparent_info_t;
- //读明密文状态信息
- typedef struct _read_56_inifo
- {
- uint8_t step;
- uint8_t num;
- uint8_t read_way; //读取方式,1: 72、73, 0: 86
- uint8_t read_way_tax1; //读取方式,1: 72、73, 0: 86
- uint8_t read_way_tax2; //读取方式,1: 72、73, 0: 86
- }read_56_info_t;
- extern read_56_info_t read_56_info;
- #define CMD73_LEN 32
- //73信息存储
- typedef struct _cmd_73_info
- {
- uint8_t read_flag;
- uint16_t len;
- uint8_t data[CMD73_LEN];
- }__attribute__((packed)) cmd_73_info_t;
- extern cmd_73_info_t cmd_73_info[2];
- //结构体头的长度
- #define FRAME_HEADER_LEN 12
- #define BUSINESS_QUERY_HEADER_LEN 13
- #define HEARTBEAT_HEADER_LEN (sizeof(coll_heartbeat_t))
- #define HARD_HEADER_LEN (sizeof(coll_hard_reply_t))
- #define TRANSPARENT_HEADER_LEN 12
- #define CMD73_QUERY_HEADER_LEN (sizeof(cmd73_query_info_t)-128)
-
- extern uint8_t rcv_buf[UART_RCV_PROCBUF_LEN];
- extern uint16_t rcv_size,rcv_size_temp;
- void ciphertext_state_isout(void);
- void uart_timeout_handle(void);
- void gateway_down_business_return(uint8_t first_type, uint8_t state, uint8_t *data, uint8_t len);
- void business_uart_rcv_proc(void);
- void gateway_coll_ctrl_comm(collect_gateway_msg_format_t *p_msg);
- void coll_encoder_return(uint8_t tax, uint8_t gun, uint8_t order);
- void coll_transparent_return(uint8_t first_type, uint8_t state, uint8_t *data, uint8_t len);
- void gateway_coll_transparent_proc(collect_gateway_msg_format_t *p_msg);
- void read_56_init(void);
- void read_56_start(uint8_t uart_id);
- extern void tax_rcv_proc(uint8_t *data, uint16_t len);
- void uart_fifo_clear(uint8_t uart_id);
- extern void tax_ciphertext_0x8C_process(void);
- extern void coll_heartbeat_return(void);
- #endif
|