#ifndef __GATEWAY_COLLECT_H__
#define __GATEWAY_COLLECT_H__

#include "stm32f10x.h"
//#include "downlink.h"
#include "../../APP/network/downlink.h"
#include "../globalDef.h"
//#include "../ota/ota.h"

#define TAX_REPEAD_DATA 6
/*采集器与网关通信的消息类型*/
#define GATE_COLL_HEART_QUERY       0x56	//网关与采集器的下行心跳包
#define COLL_GATE_HEART_QUERY       0x66	//网关与采集器的上行心跳包
#define GATE_COLL_TAX_QUERY         0x57    //业务数据下行发送消息
#define COLL_GATE_TAX_QUERY         0x67    //业务数据上行接收消息
#define GATE_COLL_FIRMMSG_QUERY     0x58	//网关下发查询固件信息
#define COLL_GATE_FIRMMSG_QUERY     0x68	//采集器上发固件信息
#define COLL_GATE_RESTART_QUERY     0x59	//采集器设备重新启动
#define COLL_GATE_RESTART_5D     0x5D	//采集器设备重新启动
#define COLL_GATE_CHANGE_LORAPA     0x5B    // 切换lora信道参数
#define GATE_COLL_ENCODER_QUERY     0x5C // 读取编码器数据
#define COLL_GATE_ENCODER_QUERY     0x6C // 读取编码器数据

#define COLL_GATE_TRANSMIT          0x01 // 透传指令

#define YTSF_0XA2_0X00   0xA2
#define YTSF_0XA1_0X11   0xA1
#define YTSF_0XA1_0X14   0xA4

#define FRAME_LEN 19

#define PLAINTEXT   1  //明文
#define CIPHERTEXT  2  //密文
#define DISPLAY     3  //显示屏


#define GATE_COLL_HEAD_LEN 12 // 网关与采集器通信的协议头为12个字节
#define GATE_COLL_CMD5767_LEN 13 //0x57 0x67指令的长度
#define GATE_COLL_CMD56_LEN 9 // 56命令的长度
#define GATE_COLL_CMD58_LEN 5 // 58命令的长度
#define GATE_COLL_CMD59_LEN 5 // 59命令的长度
#define GATE_COLL_CMD5D_LEN 12 // 5D命令的长度

#define GATE_COLL_CMD5B_LEN 17 // 5B命令的长度
#define GATE_COLL_CMD5C_LEN  13 // 5C命令的长度

#define GATE_COLL_PROTOTYPE 0x01 // 网关与采集器通信协议的版本
/*业务数据头0x17 0x27*/
typedef struct _tax_query_info
{
    uint32_t device_sn;         //设备编号
    uint8_t coll_no;            // 采集器索引 从1开始
    uint16_t device_type;        // 设备类型 0x0101: 网关 0x0201:报税口 0x0301: 液位仪 0x0401:显示屏
    uint8_t tax_no;             //报税口号 从1开始
    uint8_t gun_msgid;          //枪号  从1开始
    uint8_t prot_type;          //协议类型 1:明文 2：密文
    uint8_t status;             //1:成功 2：数据超时
    uint16_t dataLen;           // 发送的数据长度
    char  info[160];
}__attribute__((packed)) tax_query_info_t;

/* 查询固件信息0x58 设备重新启动0x59*/
typedef struct _TAX_COMM_HEAD_{
    uint32_t device_sn; // 设备编号
    uint8_t coll_no;            // 采集器索引 从1开始
    char info[8]; //
}__attribute__((packed))taxComHead;
typedef struct _coll_0x56_head{ /*  心跳0x56  */
    uint32_t devSn;         // 设备编号
    uint8_t  collIndex;     // 采集器索引 从1开始
    uint8_t  pIndex;        // 报税口是否读取 0x01: 读取报税口1  0x02: 读取报税口2  0x03:读取报税口1 和报税口2 （按位进行设置）
    uint8_t  reserve1;      // 备用
    uint16_t reserve2;      // 备用
    uint8_t info[8];
}__attribute__((packed))coll56Da;

/* 通用指令 0x5D*/
typedef struct _TAX_COMM_5D_{
    uint32_t device_sn; // 设备编号
    uint8_t coll_no;            // 采集器索引 从1开始
    uint16_t targettype; //设备类型
    uint8_t cmd; // 指令
    uint32_t reserve; //
    char info[8]; //
}__attribute__((packed))taxCom5D;

/*心跳返回的数据0x56*/
typedef struct _TAX_Encry_STATUS_{
    uint32_t device_sn; // 设备编号
    uint8_t coll_no;            // 采集器索引 从1开始
    uint8_t factory; // 1：明文  2：密文
}__attribute__((packed))taxEncryptStatus;

/*  信道切换指令 0x5B */
typedef struct _TAX_CHANGE_LORA_{
    uint32_t device_sn; // 采集器SN
    uint8_t coll_no; // 采集器索引
    uint8_t paEnable; // 参数是否有效
    uint8_t loraPaindex; // lora信道索引号
    uint32_t freq;       // lora 发射频率
    uint8_t power;       // 发射功率
    uint8_t bandwidth;   // 带宽
    uint8_t spreadingfactor; // 扩频因子
    uint8_t codingrate;     // 纠错编码率
    uint16_t preambleLen;    // 前导码长度
    char info[8];       //数据内容
}__attribute__((packed)) taxChangeLora;

/*编码器数据 0x5C */
#if(0)
typedef struct _ENCODER_DA_00_{
    uint8_t cmd; // 指令
    uint8_t gunstatus1; // 报税口1的抬枪状态
    uint8_t gunstatus2; // 报税口2的抬枪状态
    uint8_t codestatus; // 有无新交易状态，
    uint32_t codeId; // 记录ID
    uint8_t codestyle; // 记录产生方式
    uint32_t pulse; // 单次脉冲数
    uint32_t totalPulse; // 总脉冲数
    uint32_t timer;  // 时间戳
}__attribute__((packed))encoderDa_00;
#else
typedef struct _ENCODER_DATA_{
    uint32_t codeId; // 记录ID
    uint32_t pulse; // 单次脉冲数
    uint32_t totalPulse; // 总脉冲数
    uint32_t timer;  // 时间戳
}__attribute__((packed)) enData;

typedef struct _ENCODER_DA_00_{
    uint8_t cmd; // 指令
    uint8_t gunstatus1; // 报税口1的抬枪状态
    uint8_t gunstatus2; // 报税口2的抬枪状态
    uint8_t codestatus; // 有无新交易状态，按位取
    uint8_t codestyle; // 记录产生方式 ，按位取
    uint8_t gunNum;   // 编码器的最大枪数，指一个报税口下的
    enData enDa[6];
}__attribute__((packed))encoderDa_00;

#endif
typedef struct _ENCODER_DA_{
    uint32_t device_sn;         //设备编号
    uint8_t coll_no;            // 采集器索引 从1开始
    uint16_t device_type;        // 设备类型 0x0101: 网关 0x0201:报税口 0x0301: 液位仪 0x0401:显示屏
    uint8_t tax_no;             //报税口号 从1开始
    uint8_t gun_msgid;          //枪号  从1开始
    uint8_t prot_type;          //协议类型 预留
    uint8_t status;             //1:成功 2：数据超时 3：设备类型错误
    uint16_t dataLen;           // 发送的数据长度
    char  info[32];
}__attribute__((packed))encoderDa;

/*固件信息返回0x58*/
typedef struct _TAX_FIRM_MSG_{
    uint32_t device_sn;    //设备SN
    uint8_t coll_no;            // 采集器索引 从1开始
    uint32_t bootloaderVr; // bootloader版本事情
    uint32_t appVr;       //APP版本号
    uint32_t resetNum;    //复位次数
    uint8_t resetType;    //最后一次复位类型
    uint32_t runTime;     //运行时间
    uint32_t voltage;    // 电压
    uint32_t temperature; // 温度
    uint8_t updateStatus; // 升级状态码
    uint8_t uuid[12];       //UUID
    char info[160];       //数据内容
}__attribute__((packed))taxFirmMsg;

/* 0x01---0x1053 0x1023指令的格式 */
typedef struct _sys_1053_2053_
{
    uint16_t firstLevelType;    //一级设备类型
    uint32_t firstLevelSn;      //一级设备SN
    uint16_t rcvDevType;        // 接收设备类型
    uint32_t rcvDevSn;          // 接收设备SN
    uint16_t targetDevType;     // 目标设备类型
    uint32_t targetDevSn;       // 目标设备SN
    uint32_t reserve;
    uint8_t optType;            // 执行操作1：设置 2：读取
    uint8_t cmdType;            // 执行指令 1：编码器与抬枪信号正反序
    uint16_t cmdVal;      // 1:正序 2：反序 其它：无
    uint32_t reserve01;           // 预留
}__attribute__((packed))sys1053_2053;

typedef  union TAX_DATA_INFO_{
    tax_query_info_t data; // 业务数据
    taxComHead comdata; //心跳 查询固件信息 设备重新启动信息
    coll56Da  coll56da;             // 采集器心跳
    taxCom5D  da5D;   // 0x5D 的数据
    taxFirmMsg firmMsg; //固件信息
    taxEncryptStatus taxEnStatus; // 心跳包的返回
    taxChangeLora taxLora;  // lora信道切换
    encoderDa  encoderda; // 编码器数据
    char info[160];
}__attribute__((packed))taxDaInfo;

/*网关向采集发送或是接收消息的结构体*/
typedef struct _gateway_collect_com
{
    uint16_t frame_header;      //帧头
    uint8_t  proto_ver;         //协议版本 0x11
    uint32_t seq_no;            //消息序列号
    uint8_t  type;              //一级消息类型
    uint16_t secondType;        // 2级消息类型
    uint16_t len;               //消息长度
    taxDaInfo taxinfo;            //
}__attribute__((packed)) gateway_collect_com_t;


///////////////////////////



extern int collect_rcv_analyze0x67(uint8_t *data, uint8_t len);
int tax_cmd_set(uint8_t cmd, uint8_t *parm, uint8_t size, void *puser);
void gateway_collect_cmd_set(uint8_t *data, uint8_t len,uint8_t coll_index, uint8_t tax, uint8_t uart, uint8_t gun, uint8_t prottype,uint32_t seq,uint32_t id);
//void gateway_collect_cmd_set(uint8_t *data, uint8_t len, uint8_t uart, uint8_t prottype,uint32_t seq,uint32_t id,uint8_t gun);
int ytsf_send_cmd(uint8_t cmd, uint8_t info_type, uint8_t node, uint8_t port, uint8_t gun);

extern int tax_cmd_set_0x8C(uint8_t cmd,uint8_t *parm, uint8_t size, void *puser);

extern void gateway_collect_set_cmd0x56(uint8_t cmd,uint8_t curCollIndex,int8_t pIndex,uint32_t device_sn);
extern void gateway_collect_set_cmd0x58(uint8_t cmd,uint8_t curCollIndex,uint32_t device_sn);
extern void gateway_collect_set_cmd0x59(uint8_t curCollIndex,uint32_t device_sn);
extern void gateway_collect_set_cmd0x5B(uint8_t curCollIndex,uint32_t device_sn,uint8_t index,uint8_t bpaEnable);
extern void gateway_collect_set_cmd0x5C(uint8_t *data, uint8_t len,uint8_t coll_index, uint8_t tax, uint8_t gun,uint32_t seq,uint32_t id);
extern void gateway_collect_set_cmd0x5D(uint8_t curCollIndex,uint32_t device_sn,uint16_t targettype,uint8_t cmd);
extern void gateway_collect_cmd_0x01_0x1052(uint8_t *data, uint16_t size);

extern void create_0x01_0x1053(uint8_t *outdata,uint16_t *len,uint16_t rcvType,uint32_t rcvSn,uint16_t targetType,uint16_t targetSn,uint8_t optType, uint8_t cmdType,uint16_t optval);
extern void gateway_collect_cmd_0x01_0x1053(uint8_t *data, uint16_t size);

extern int collect_rcv_analyze0x66(uint8_t *data, uint8_t len);
extern int collect_rcv_analyze0x68(uint8_t *data, uint8_t len);
extern int collect_rcv_analyze0x6C(uint8_t *data, uint8_t len);
extern int collect_rcv_analyze0x01_0x2052(uint16_t cmd, uint8_t *data, uint8_t len);

extern void gateway_net_send_gatewayMsg(void);
extern void up_send_log_srcdata(uint8_t *data,uint16_t len);
extern void up_send_log_err(uint16_t sendcmd, uint8_t type,uint8_t node,uint8_t port,uint8_t gun,uint32_t id,uint8_t cmd,uint8_t err);
#endif


