#ifndef _SOFT_CAN_H_
#define _SOFT_CAN_H_
#include "stm32f1xx_hal.h"
#include "stdbool.h"

extern CAN_RxHeaderTypeDef RxHeader;
extern CAN_TxHeaderTypeDef TxHeader;
extern uint32_t update_count;
#pragma pack(1)
typedef struct 
{
   bool U7E1;
   bool U7E3;
   bool U7E6;
   bool buf_flag;
   bool update_flag;  //与AG不兼容 一个判断标志
   uint8_t update_buf[8];
}Radar;
#pragma pack()
extern Radar Rupdate;

//正方
#define CAN_ZHENGFANG (0x15359826)
#define CAN_ZHENGFANG_MASk (0x1FFF0000)

//木牛
#define CAN_MUNIU (0x00090002)
//莫之比避障前后
#define CAN_OBSTACLE_1 (0x000EFF11)
#define CAN_OBSTACLE_2 (0x000EFF12)
//莫之比仿地
#define CAN_UAVH30_MSG (0x000EFF0B)

//莫之比雷达SN号ID
#define CAN_OBSTARCLE11_SN 0x410
#define CAN_OBSTARCLE12_SN 0x420
#define CAN_OBSTARCLE56_SN 0x430

//好盈电调
#define CAN_HOBBYWING_MSG_ESC_START 0x1F4E2000
#define CAN_HOBBYWING_MSG_ESC_END 0x1F4F1A00

#define CAN_HOBBYWING_SET_ESC_START 0x1FC800
#define CAN_HOBBYWING_SET_ESC_END 0x1FF200


//莫之比MASK
#define CAN_MICOB_FILTER_ID    (0x000EFF01)
#define CAN_MICOB_MASK_ID      ((uint32_t)0x1F0FFFE0)

//莫之比升级系列扩展ID
#define CAN_UAVRH_FILTER_ID    (0x000007E0)
#define CAN_UAVRH_MASK_ID      ((uint32_t)0x1FFFFFE0) //最后5位加版本位不过滤


//恩曌仿地
#define CAN_MIMO_T_ID (0x300)
//恩曌分区仿地
#define CAN_MIMO_T_ID1 (0x311)
#define CAN_MIMO_T_ID2 (0x312)
#define CAN_MIMO_T_ID3 (0x313)

//恩曌前避障(多点)
#define CAN_MIMO_FOBS_ID1 (0x401) 
#define CAN_MIMO_FOBS_ID2 (0x402) 
#define CAN_MIMO_FOBS_ID3 (0x403) 
//恩曌后避障(多点)
#define CAN_MIMO_BOBS_ID1 (0x411)
#define CAN_MIMO_BOBS_ID2 (0x412)
#define CAN_MIMO_BOBS_ID3 (0x413)
//莫之比前避障(多点)
#define CAN_UAVRH_FOBS_ID1 (0x601)
#define CAN_UAVRH_FOBS_ID2 (0x602)
#define CAN_UAVRH_FOBS_ID3 (0x603)
//莫之比后避障(多点)
#define CAN_UAVRH_BOBS_ID1 (0x611)
#define CAN_UAVRH_BOBS_ID2 (0x612)
#define CAN_UAVRH_BOBS_ID3 (0x613)

//恩曌前避障(单点)
#define CAN_MIMO_FOBS_SIG (0x301)
//恩曌后避障（单点）
#define CAN_MIMO_BOBS_SIG (0x302)
//恩曌前后避障掩码
#define CAN_MIMO_OBS_MASK (0X5E2)
//恩曌雷达版本信息应答
#define CAN_MIMO_VERSION (0XFB)

//恩曌360雷达新协议
#define CAN_360MIMO_1ID (0x501)
#define CAN_360MIMO_2ID (0x502)

//恩曌360雷达
#define CAN_HEAD_MIMO360_ID         (0x301)
#define CAN_INFO_MIMO360_ID         (0x302)

#define CAN_MIMO_FLOW (0x210)
#define CAN_MIMO_FLOW_K (0xFB)

#define CAN_BX_RADAR    (0X003)
//格式
#define CAN_TATTU_MSG (0x01109216)
#define CAN_NEWTATTU_MSG (0x01109200)
#define CAN_NEWTATTU_MSG2 (0x011092FF)

//EFT流量计 恩曌
#define CAN_EFT_MIMO_FLOW (0x262202)

#define EFT_FILTER_ID      0x00008851U
#define EFT_MASK_ID        0xFFFFFF00U


// 第2~0位，发送端ID，默认3
#define CAN_FULLY_SENDNODE_ID_MASK  0x00000007       // 0 0000 0000 0000 0000 0000 0000 0111(29位扩展ID)
// 第3位，最后一包标志，最后一包设置为1，其他设置为0
#define FULLY_ENDPACK_FLAG_MASK     0x00000008       // 0 0000 0000 0000 0000 0000 0000 1000(29位扩展ID)
// 第9~4位，包计数:从0开始递增，第一包是0
#define FULLY_PACK_NUM_MASK         0x000003F0       // 0 0000 0000 0000 0000 0011 1111 0000(29位扩展ID)
// 第16~10位，包ID:默认值 1
#define FULLY_PACK_ID_MASK          0x0001FC00       // 0 0000 0000 0001 1111 1100 0000 0000(29位扩展ID)
// 第18~17位，发送形式： 默认值 2
#define CAN_FULLY_SEND_TYPE         0x00060000       // 0 0000 0000 0110 0000 0000 0000 0000(29位扩展ID)
// 第28~19位，消息ID：
#define CAN_FULLY_MSG_ID            0x1FF80000       // 1 1111 1111 1000 0000 0000 0000 0000(29位扩展ID)

//富力电池ID及MASK
#define FULLY_CELL_FILTER_ID   0x16040403            // 1 0110 0000 0100 0000 0100 0000 0011(29位扩展ID)
#define FULLY_CELL_MASK_ID     0x1e07fc07            // 1 1110 0000 0111 1111 1100 0000 0111(29位扩展ID)
//富力握手ID(740)
#define FULLY_CELL_HANDSHAKE   0x1724040b            // 1 0111 0010 0100 0000 0100 0000 1011(29位扩展ID)
//富力SHA1写入(741)
#define FULLY_CELL_SHA1W       0x172c040b‬            // 1 0111 0010 1100 0000 0100 0000 1011(29位扩展ID)
//富力SHA1查询(742)
#define FULLY_CELL_SHA1R       0x1734040b            // 1 0111 0011 0100 0000 0100 0000 1011(29位扩展ID)

//极目电池
#define CAN_POWERAMP (0x188)

//EZ雷达协议升级
#define CAN_EZ_R_UPDATE (0x3D)

//羽人电池
#define CAN_YRPOWER (0x7D0)

//吉利发动机
#define GEELY_ENGINE_START_ID         0x1C0
#define GEELY_ENGINE_END_ID           0X1C4
#define GEELY_ENGINE_ID_MASK    0x7F8
//吉利发动机2版协议
#define GEELY_ENGINE_START_ID2         0x1E0
#define GEELY_ENGINE_END_ID2           0X1EF

//海盈智能电池
#define HEREWIN_FILTER_ID  0x0000FE00    //               1111 1110 0000 0000
#define HEREWIN_MASK_ID    0x0300F0FF    //0011 0000 0000 1111 0000 1111 1111
                                                         // 1111 0000 0000 0000
//极目播撒本撒设备
#define POWERAMP_START_ID 0x52
#define POWERAMP_END_ID   0x67



//VK协议
#define FRAME_TYPE_MASK 0x1C000000 // 1 1100 0000 0000 0000 0000 0000 0000(29位扩展ID)
#define CAN_FMTYPE_DATA 0x00   // 数据帧
#define CAN_FMTYPE_REQ 0x01    // 请求帧

// 第18~12位，目标nodeID，, ID最大0x7F
// 第11~5位， 源nodeID, ID最大0x7F
#define DES_ID_MASK 0x0007F000         // 0 0000 0000 0111 1111 0000 0000 0000(29位扩展ID)
#define SRC_ID_MASK 0x00000FE0         // 0 0000 0000 0000 0000 1111 1110 0000(29位扩展ID)
#define CAN_NODEID_MC  0x01            // 主控nodeid
#define CAN_NODEID_LED 0x02            // led灯板nodeid
#define CAN_NODEID_4G  0x03            // 4G模块nodeid
#define CAN_NODEID_LIQUED 0x15         //液位计nodeid
#define CAN_NODEID_FLOW 0x16           //流量计nodeid
#define CAN_NODEID_ENGINE 0x17         //发动机nodeid
#define CAN_NODEID_LASER  0x18         //雷达nodeid
#define CAN_NODEID_WEIGHT 0x19         //称重传感器、播撒器 nodeid
#define CAN_NODEID_RADAR_UPDATE 0x20   //设备升级  
#define CAN_NODEID_DISTRIBUTOR  0x21   //分电板
#define CAN_NODEID_PUMP_NOZZLE  0x22   //水泵离心喷头


// 第25~19位，消息ID
#define MSG_ID_MASK 0x03F80000 // 0 0011 1111 1000 0000 0000 0000 0000(29位扩展ID)
// 飞控发出给LED的消息序号
#define CAN_MSGID_MCTOLED_BLINK 0x01 // 灯闪烁
// 飞控发出给LED的配置消息序号
#define CAN_MSGID_MCTOLED_CONF 0x02 // 底板配置
// 飞控发出给LED的AD状态消息序号
#define CAN_MSGID_MCTOLED_ADOC 0x03 // AD开关

// LED发出给飞控的消息序号
#define CAN_MSGID_LEDTOMC_VOLT 0x01 // 电压监测
// LED发出给飞控的版本序号
#define CAN_MSGID_LEDTOMC_VER 0x02 // 版本信息
// 液位计发出给飞控的消息
#define CAN_MSGID_LEDTOMC_LIQUID 0x03 // 液位计信息(目前没有到)
//流量计发出给飞控的信息
#define CAN_MSGID_FLOWTOMC_INFO  0x04 //流量计信息 0x2012c0 
#define CAN_MSGID_FLOWTOMC_ACK  0x05 //流量计ACK  
//发动机发出给飞控的信息
#define CAN_MSGID_ENGINE_INFO 0x06 //CAN发动机信息(目前没有到)


#define CAN_MSGID_RADAR_START 0X07 //雷达升级开始
#define CAN_MSGID_RADAR_ING 0X08 //雷达升级中
#define CAN_MSGID_RADAR_END 0X09 //雷达升级结束  1000 0001 0100 0010 0000
//水泵信息
#define CAN_MSGID_PUMP  0x10
//离心喷头信息
#define CAN_MSGID_NOZZLE 0x11

//防地雷达发出给飞控的信息0x901300
#define CAN_MSGID_RADAR_INFO 0x12
//飞控发出给防地雷达的信息0x981300
#define CAN_MSGID_RADAR_VERSION_INFO 0x13

//电目断料计
#define CAN_MSGID_DM_LACKLOSS 0x18F2AD0B

//前避障雷达发出给飞控的信息
#define CAN_MSGID_FRADAR_INFO 0x14
//飞控发出给前避障雷达的信息
#define CAN_MSGID_FRADAR_VERSION_INFO 0x15

//后避障雷达发出给飞控的信息
#define CAN_MSGID_BRADAR_INFO 0x16
//飞控发出给后避障雷达的信息
#define CAN_MSGID_BRADAR_VERSION_INFO 0x17

// CANfilterID,（扩展帧 29位）
// filterID中，按照目标和源ID进行过滤，目标ID为自己的固定不变，源ID需要把所有的需要接收的node合并一起组成
#define CAN_MC_FILTER_ID   ((unsigned int)(CAN_FMTYPE_DATA<<26 | CAN_MSGID_LEDTOMC_VOLT<<19 | \
                                         CAN_NODEID_MC<<12 | CAN_NODEID_LED<<5))

// CANmaskID, (扩展帧 29位) 0  0x12  0x18
// maskID中为1的位，需要filterID和消息帧中对应位的值完全一样，才能通过过滤
// 消息类型位全置1，目标nodeID的位全置1, 消息ID以及源节点ID处寻找相同位的做过滤
#define CAN_MC_MASK_ID   ((unsigned int)(FRAME_TYPE_MASK | \
(MSG_ID_MASK & (((0x7f & (~(0x00 | CAN_MSGID_LEDTOMC_VOLT | CAN_MSGID_LEDTOMC_VER | CAN_MSGID_ENGINE_INFO \
 | CAN_MSGID_FLOWTOMC_INFO | CAN_MSGID_FLOWTOMC_ACK | CAN_MSGID_RADAR_INFO | CAN_MSGID_BRADAR_INFO |CAN_MSGID_FRADAR_INFO |CAN_MSGID_RADAR_START| CAN_MSGID_RADAR_ING | CAN_MSGID_RADAR_END)))|\
 (0x7f & CAN_MSGID_LEDTOMC_VOLT & CAN_MSGID_LEDTOMC_VER & CAN_MSGID_FLOWTOMC_INFO & CAN_MSGID_FLOWTOMC_ACK & CAN_MSGID_ENGINE_INFO \
  & CAN_MSGID_RADAR_START& CAN_MSGID_RADAR_INFO & CAN_MSGID_BRADAR_INFO & CAN_MSGID_FRADAR_INFO & CAN_MSGID_RADAR_ING & CAN_MSGID_RADAR_END))<<19)) | \
                            DES_ID_MASK | \
(SRC_ID_MASK & (((0x7f & (~(0x00 | CAN_NODEID_LED | CAN_NODEID_4G | CAN_NODEID_LIQUED | CAN_NODEID_FLOW | \
    CAN_NODEID_ENGINE | CAN_NODEID_LASER |CAN_NODEID_WEIGHT | CAN_NODEID_RADAR_UPDATE | CAN_NODEID_DISTRIBUTOR | CAN_NODEID_PUMP_NOZZLE))) |\
 (0x7f & CAN_NODEID_LED & CAN_NODEID_4G & CAN_NODEID_LIQUED & CAN_NODEID_FLOW & \
  CAN_NODEID_ENGINE & CAN_NODEID_LASER & CAN_NODEID_WEIGHT & CAN_NODEID_RADAR_UPDATE & CAN_NODEID_DISTRIBUTOR & CAN_NODEID_PUMP_NOZZLE))<<5))))


//VK电池协议 0x1E0958
#define VK_BMS_FILTER_ID 0xE0800           //0 0000 0000 1110 0000 1000 0000 0000   flag 111 目标节点0x1
#define VK_BMS_MASK_ID   0x1C0FF800       //1 1100 0000 1111 1111 1000 0000 0000
  //0x1C0E0800
#define VK_MSG_MASK_ID 0X3F00000
#define VK_SRC_MASK_ID 0X7E0

void can_start_init(void);
void Can_filter_init(void);
void Can_obstacle_update(void);
void check_radar_link_status(void);

void can_send_msg_normal(unsigned char *data, unsigned char length, unsigned int extid);
void can_send_msg_normalstd(unsigned char *data, unsigned char length, unsigned int stdid);
void put_date_to_can(uint8_t *buf,uint8_t par1,uint8_t par2,uint8_t par3,uint8_t par4,uint8_t par5,
                        uint8_t par6,uint8_t par7,uint8_t par8);

#endif
