#ifndef __USARTDATA_HANDLE_H
#define __USARTDATA_HANDLE_H
#include "stdbool.h"
#include "stdint.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_uart.h"
#include "common.h"

#define MAX_UART_BUF 256
extern uint8_t FMU_uart_buf[MAX_UART_BUF];

typedef enum
{
	RX_HEAD = 0,
	RX_PAYLOAD_LEN,
	RX_SEQ,
	RX_SYSID,
	RX_GROUPID,
	RX_MSGID,
	RX_PAYLOAD,
	RX_CHECK_LOW,
	RX_CHECK_HIGH
} Msg_Rx_Stage;

typedef enum
{
	GROUP_ID_FCU = 0,
	GROUP_ID_F_UPDATE = 51,
	GROUP_ID_B_UPDATE = 52,
	GROUP_ID_T_UPDATE = 56,
	GROUP_ID_EFTPUMP1_UPDATE = 91,
	GROUP_ID_EFTPUMP2_UPDATE = 92,
	GROUP_ID_EFTNOZZLE1_UPDATE = 96,
	GROUP_ID_EFTNOZZLE2_UPDATE = 97,
	GROUP_ID_EFTNOZZLE3_UPDATE = 98,
	GROUP_ID_EFTNOZZLE4_UPDATE = 99,
	GROUP_ID_EFTFB_UPDATE = 105,
	GROUP_ID_EFTSEED_UPDATE = 111,

	GROUP_ID_PMU_UPDATE = 204,
	GROUP_ID_WEIGHT_UPDATE = 222,
	GROUP_ID_Z70FB_UPDATE = 223,
	GROUP_ID_Z70FBLIFTING_UPDATE = 224,
	GROUP_ID_EFT_TURNMOTOR = 225,
	GROUP_ID_EFT_CHURN = 226,
	GROUP_ID_EFT_CHURNTURN = 227,

	GROUP_IDEND,
} GROUP_ID;


enum vklink_msgid
{
	_MSGID_VOL = 1,		   // 电压
	_MSGID_RADAR = 2,	   // 雷达
	_MSGID_FLOW = 3,	   // 流量计
	_MSGID_BMS = 4,		   // 电池
	_MSGID_PWM = 5,		   // p1 p2 a1 a2通道
	_MSGID_LED = 6,		   // 外部LED灯
	_MSGID_ATTITUDE = 8,   // 飞机姿态信息
	_MSGID_TIME = 9,	   // UTC时间信息
	_MSG_ENGIN = 10,	   // 发动机
	_MSGID_EFT_CON = 11,   // EFT输出CAN控制
	_MSGID_DEV_INFO = 12,  // 设备信息
	_MSGID_REQ = 20,	   // 请求信息
	_MSGID_ACK = 21,	   // 主控应答
	_MSGID_SET = 22,	   // 设置信息
	_MSGID_GET4D = 23,     // 4D雷达参数回复fmu相关
	_MSGID_SET4D = 24,   // 4D雷达设置相关
	_MSGID_SHA1 = 25,	   // 智能电池SHA1验证
	_MSGID_DEV_LIST = 26,  // 设备SN,软硬件管理
	_MSGID_HEART = 27,     // 心跳检测
	_MSGID_360RADAR = 30,     //360雷达
	_MSGID_DMRADAR = 31,     //电目雷达测试
	_MSGID_F4DRADAR = 32,     //电目4d前避障雷达测试
	_MSGID_B4DRADAR = 33,     //电目4d后避障雷达测试
	_MSGID_FT4DRADAR = 34,     //电目4d前避障分段高度
	_MSGID_BT4DRADAR = 35,     // 电目4d后避障分段高度
	_MSGID_UPDATA = 200,   // 升级信息
	_MSGID_CANDEBUG = 213, // CAN调试

};

enum vklink_MSGID_ACK
{
	MSGID_ACK_HEART = 1,    
	MSGID_ACK_VERSION = 7, // 版本信息
	MSGID_ACK_DEV = 26,	   // 版本信息
};

enum vklink_MSGID_SET
{
	MSGID_SET_F_RADAR = 1,		  // 设置前雷达
	MSGID_SET_B_RADAR = 2,		  // 设置后雷达
	MSGID_SET_T_RADAR = 6,		  // 设置防地雷达
	MSGID_SET_TR_BLIND = 7,       //设置雷达盲区
	MSGID_SET_BR_POWER = 8,       //设置雷达能量
		MSGID_SET_R_FUNC = 9,         //设置电目雷达功能
	MSGID_SET_GEELY = 10,		  // 设置轩浮发动机
	
		MSGID_SET_RAW_SWITCH = 15,    //设置DM雷达原始数据开关
	MSGID_SET_VOL = 20,			  // 设置电压
	MSGID_SET_MIMO_FLOW = 21,	  // 设置恩曌流量计系数
	MSGID_SET_FLOW_BACKGROUND = 22,//清除流量背景
	MSGID_SET_WEIGHT_K = 23,	  // 设置重量传感器K
	MSGID_SET_SEED_OUT_TYPE = 24, // 设置播撒输出类型pwm can
	MSGID_SET_SEED_CAL_TYPE = 25, // 设置播撒校准类型
	MSGID_SET_WEIGHT_MODE = 26,	  // 设置重量传感器工作模式
	MSGID_SET_MAX_RATE = 27,	  // 设置播撒最大下药率
	MSGID_SET_PMU_SERIAL = 28,    // 设置pmu序列号
	MSGID_SET_RADAR_FB = 29,      // 设置当前雷达前后/恢复出厂 恩曌雷达有效
	MSGID_SET_LACKLOSS_CAL = 30,  // 断料记校准
	MSGID_SET_PNCANID   = 55,	  //设置EFT水泵电调CAN ID
	MSGID_SET_RESIWIRE_BLOWN = 60,// 熔断电阻丝
	MSGID_SET_CHURN_RESET = 61,	  // 绞龙复位
	MSGID_SET_CHURN_SIZE = 64, //设置绞龙型号
	MSGID_SET_FRADAR_SN = 98,     // 设置雷达SN号
	MSGID_SET_BRADAR_SN = 99,     // 设置雷达SN号
	MSGID_SET_TRADAR_SN = 100,    // 设置雷达SN号
	MSGID_SET_4DFRADAR_SN = 102,    // 设置4D雷达SN号
	MSGID_SET_4DBRADAR_SN = 103,    // 设置4D雷达SN号
	MSGID_SET_4DTRADAR_SN = 104,    // 设置4D雷达SN号
};


//发送设备信息ID
typedef enum
{
	DEV_SEED = 1,
	DEV_WEIGHT,
	DEV_PUMP,
	DEV_NOZZLE,
	DEV_ARM,
	DEV_FLOW,
	DEV_CHECKLOW,
	DEV_RADAR,
	DEV_BMS,
	DEV_ENGIN,
	DEV_CURRENT,
	DEV_L_PUMP1,
	DEV_L_PUMP2,
	DEV_PART_RADAR,
	DEV_PART_FRADAR,
	DEV_PART_BRADAR,
	DEV_XQ_BMS,
	DEV_TEMP_SENSOR
} Dev_Type_NUM;

//飞机状态
enum LOCK_STATUS
{
	STA_LOCK = 0,
	STA_UNLOCK = 1,
};

//机型
enum DRONE_TYPE
{
    JET_UAV_TYPE = 13,
    VK_ALL_IN_ONE = 18,
    AG_EFTZ = 15, 
};

//请求信息包
enum vklink_MSGID_REQ
{
	MSGID_REQ_VERSION = 7, // 版本信息
};

#pragma pack(1)
typedef struct
{
	short yaw;		   // 航向角
	short roll_angle;  // 0.01
	short pitch_angle; // 0.01
	short alt;
	short E_vel;	   // 东西速度
	short N_vel;	   // 南北速度
	short alt_vel;	   // 垂直速度
	short lock_status; // 0 上锁 1解锁
	short UAV_type;
	short thr_pwm;
	short Candebug_flag; // can调试 0开 1关
	int pos_x1;           //经度
	int pos_y1;			 //纬度
	float QuaterQ0;
	float QuaterQ1;
	float QuaterQ2;
	float QuaterQ3;
	float pos_x;
	float pos_y;
	float pos_z;
	float pos_flag;
} plane_para;
#pragma pack()
extern plane_para planep;

typedef struct soft_p_2_c
{
	uint8_t num;
	uint16_t content1;
	uint16_t content2;
} Set_info;
extern Set_info msgidset;

#pragma pack(1)
typedef struct 
{
	uint8_t head;  //0xFE
	uint8_t len;
	uint8_t seq;
	uint8_t system_id; //0
	uint8_t group_id; 
	uint8_t msg_id;
	uint8_t head_bytes; //6
	uint8_t check_bytes;//2
	uint16_t crc;
	uint8_t payload[MAX_UART_BUF];
    bool complete_flag;
}Vk_protocol;
#pragma pack()
extern Vk_protocol fcu_protocol;

#pragma pack(1)
typedef struct 
{
	bool vk_dev_update_flag;
	uint16_t vk_dev_pack_num;
	uint32_t bin_size;

	bool step_200_flag;
	bool step_201_flag;
	bool step_202_flag;
}Vk_update_protocol;
#pragma pack()
extern Vk_update_protocol fcu_update_info;

#pragma pack(1)
typedef struct
{
	short pump1;
	short pump2;
	short nozz1_fm;
	short nozz2_zp;
	short nozz3;
	short nozz4; 
	uint16_t aux_light; //Bit 0 :1灯  Bit 1：2灯    Bit2-8：灯1亮度     Bit9-15:灯2亮度
	short aux_steer;
}_pmu_pin;
#pragma pack()
extern _pmu_pin pmu_pin;

#pragma pack(1)
typedef struct
{
	bool version_info;

	bool key_info_checking; // 给fcu发送信息
	bool key_to_bms;
	uint8_t mos_status;
	uint8_t Dev_type;
	uint8_t Id;
	uint8_t Id_content;
	unsigned char key_data[20];

} Start_info;
extern Start_info start_msg;
#pragma pack()



extern Connect_check fcu;
void thread_usart_task_entry(void *param);
void USER_UART_IRQHandler(UART_HandleTypeDef *huart);



#endif
