#ifndef SOFT_BMS_H
#define SOFT_BMS_H

#include "common.h"
#include "stdint.h"

void update_bms_data( void );
void BMSCanRecvHookFunction(uint32_t CanID, uint8_t data[], uint8_t len);

#pragma pack(1)
struct BMS_DATA
{
    uint8_t  bms_version[3]; //协议版本
    uint8_t  bms_group;      //电池组数
    uint8_t bms_num;         //串数

    uint16_t bms_v1; //mv
    uint16_t bms_v2;
    uint16_t bms_v3;
    uint16_t bms_v4;
    uint16_t bms_v5;
    uint16_t bms_v6;
    uint16_t bms_v7;
    uint16_t bms_v8;
    uint16_t bms_v9;
    uint16_t bms_v10;
    uint16_t bms_v11;
    uint16_t bms_v12;
    uint16_t bms_v13;
    uint16_t bms_v14;
    uint16_t bms_v15;
    uint16_t bms_v16;
    uint16_t bms_v17;
    uint16_t bms_v18;
    uint16_t bms_v19;
    uint16_t bms_v20;
    uint16_t bms_v21;
    uint16_t bms_v22;
    uint16_t bms_v23;
    uint16_t bms_v24;
    uint16_t bms_v25;
    uint16_t bms_v26;  //最大兼容26s电池

    uint8_t bms_ids[32]; //VK3协议 32字节  之前20字节
    uint16_t bms_cycle; //循环次数
    uint16_t bms_volt;  //电压 mv
    int16_t bms_ac;     //电流 10ma
    int16_t bms_temp;  //温度 0.1℃
    uint8_t bms_vs;     //充电百分比 1%
    uint16_t bms_ss;    //状态
    uint16_t serial_num; //VK3厂商编号
};
#pragma pack()

//msgid 0x02
#pragma pack(1)
typedef struct{
    uint16_t voltage; //10mv
    int      current;
    short    tempture;
    short    mosfet_tempture;
    uint16_t persent;
    uint32_t warn_flag;
    uint16_t bms_num;
    uint16_t bms_v1;
    uint16_t bms_v2;
    uint16_t bms_v3;
    uint16_t bms_v4;
    uint16_t bms_v5;
    uint16_t bms_v6;
    uint16_t bms_v7;
    uint16_t bms_v8;
    uint16_t bms_v9;
    uint16_t bms_v10;
    uint16_t bms_v11;
    uint16_t bms_v12;
    uint16_t bms_v13;
    uint16_t bms_v14;
    uint16_t bms_v15;
    uint16_t bms_v16;
    uint16_t bms_v17;
    uint16_t bms_v18;
    uint16_t bms_v19;
    uint16_t bms_v20;
    uint16_t bms_v21;
    uint16_t bms_v22;
    uint16_t bms_v23;
    uint16_t bms_v24;
    uint16_t bms_v25;
    uint16_t bms_v26;
}vkbms2;
#pragma pack()
extern vkbms2 vk_bms2;

//msgid 0x03
#pragma pack(1)
typedef struct{
    char bms_company_info[20];  //电池厂商信息
    char bms_type_info[20];     //电池型号信息
    char bms_id[20];            //电池ID信息
    char bms_hard_version[10];  //电池硬件版本
    char bms_soft_version[10];  //电池软件版本
}vkbms3;
#pragma pack()

//msgid 0x04
#pragma pack(1)
typedef struct{
    uint16_t capacity;        //电池设计容量
    uint16_t discharge_rate;  //电池放电倍率
    uint16_t normal_voltage;  //标称电压
    uint16_t bms_num;         //电池串数
}vkbms4;
#pragma pack()

//msgid 0x05
#pragma pack(1)
typedef struct{
    uint16_t health_percent;  //健康度%
    uint16_t circulation_time;//循环次数
    uint16_t over_recharge;   //过充次数
    uint16_t over_discharge;  //过放次数
    uint16_t over_current;    //过流次数
    uint16_t over_tempreature;//过温次数
    uint16_t SOC;
    uint16_t SOH;
    uint16_t recharge_capacity;//充电容量
    uint16_t surplus_capacity; //剩余容量
    uint16_t inter_resistance; //电池内阻值
}vkbms5;
#pragma pack()

#pragma pack(1)
typedef struct soft_bms
{
    Connect_check Vkbms_Link;

    uint32_t can_id;

    bool ack_to_bms;
    uint8_t source_id;

    bool if_mul_frame;
    bool recv_mul_freme_complete;
    uint8_t mul_frame_i;
    uint8_t mul_frame_num;
    uint8_t bms_can_buf[128];

    vkbms2 vk_bms2;
    vkbms3 vk_bms3;
    vkbms4 vk_bms4;
    vkbms5 vk_bms5;

    bool get_bms3_ack;
    bool get_bms4_ack;
    bool get_bms5_ack;

}Vkbms_Device;
#pragma pack()
extern Vkbms_Device Device1;
extern Vkbms_Device Device2;
extern uint8_t bms_step;

void bms_function(void);
extern struct BMS_DATA bms_data, bms_data2;
#endif
