
#include "bsp_task.h"
#include "bsp_queue.h"

#include "bmu_manager.h"
#include "comm_manager.h"
#include "gpio_manager.h"
#include "fault_manager.h"
#include "flash_manager.h"
#include "eeprom_manager.h"
#include "statistic_manager.h"
#include "run_status.h"

#include "kit_time.h"
#include "kit_data.h"
#include "kit_debug.h"
#include "kit_macro.h"
#include "bmu_adbms1818.h"

#define BMU_BASE_INFO_PGN		(0x12u)
#define BMU_VOLT_INFO_PGN		(0x13u)
#define BMU_TEMP_INFO_PGN		(0x14u)
#define BMU_REQUEST_PERIOD      (20u)

extern QueueItem bmu_can_queue;
void bmu_msg_handler(CanMsg *msg);
void bms_analyse_bmu_fault(uint32_t base_time);

//���������ͳ����Ϣ
BmuTemperatureItem bmu_stat_high_temperatures[BMU_STAT_HIGH_TEMPERATURE_NUM];
uint8_t bmu_stat_high_temperature_count = 0;

BmuTemperatureItem bmu_stat_low_temperatures[BMU_STAT_LOW_TEMPERATURE_NUM];
uint8_t bmu_stat_low_temperature_count = 0;

BmuVoltageItem bmu_stat_high_voltages[BMU_STAT_HIGH_VOLTAGE_NUM];
uint8_t bmu_stat_high_voltage_count = 0;

BmuVoltageItem bmu_stat_low_voltages[BMU_STAT_LOW_VOLTAGE_NUM];
uint8_t bmu_stat_low_voltage_count = 0;

//���������ͳ����Ϣ��ʱ����
BmuTemperatureItem bmu_stat_temp_high_temperatures[BMU_STAT_HIGH_TEMPERATURE_NUM];
uint8_t bmu_stat_temp_high_temperature_count = 0;

BmuTemperatureItem bmu_stat_temp_low_temperatures[BMU_STAT_LOW_TEMPERATURE_NUM];
uint8_t bmu_stat_temp_low_temperature_count = 0;

BmuVoltageItem bmu_stat_temp_high_voltages[BMU_STAT_HIGH_VOLTAGE_NUM];
uint8_t bmu_stat_temp_high_voltage_count = 0;

BmuVoltageItem bmu_stat_temp_low_voltages[BMU_STAT_LOW_VOLTAGE_NUM];
uint8_t bmu_stat_temp_low_voltage_count = 0;


uint8_t  bms_get_bmu_num(void)
{
	return bmu_data.bmu_num;
}

void bms_set_bmu_num(uint8_t number)
{
	bmu_data.bmu_num = number;
	bmu_data.total_ic_num = number;
}

void bms_set_bmu_volt_num(uint8_t number)
{
	bmu_data.total_cell_num = number * 16;
}

void bms_set_bmu_temp_num(uint8_t number)
{
	bmu_data.total_temp_num = number * 4;
}

uint8_t  bms_get_bmu_online_num(void)
{
	return bmu_data.bmu_online_num;
}

uint16_t bms_get_cell_num(void)
{
    return bmu_data.total_cell_num;
}

uint16_t bms_get_temp_num(void)
{
    return bmu_data.total_temp_num;
}


uint16_t bms_get_cell_volt(uint16_t idx)
{
    uint16_t tmp = 0;
    if(idx < BMU_MAX_CELL_NUM)
    {
        return bmu_data.cell_volt_total_buf[idx];
    }
    return tmp;
}

uint16_t bms_get_cell_temp(uint16_t idx)
{
    uint16_t tmp = 0;
    if(idx < BMU_MAX_TEMP_NUM)
    {
        return bmu_data.cell_temp_total_buf[idx];
    }
    return tmp;
}

uint32_t bms_get_cell_volt_buf_addr(void)
{
    return (uint32_t)&bmu_data.cell_volt_total_buf;
}

uint32_t bms_get_cell_temp_buf_addr(void)
{
    return (uint32_t)&bmu_data.cell_temp_total_buf;
}



void bms_set_fan_status(uint8_t bmu_idx, bool is_on)
{
    if(bmu_idx < BMU_MAX_NUM)
	{
		bmu_data.bmu_unit[bmu_idx].is_fan_on = is_on;
	}
}

bool bms_is_fan_open(uint8_t bmu_idx)
{
    bool res = false;
    if(bmu_idx < BMU_MAX_NUM)
	{
		res = bmu_data.bmu_unit[bmu_idx].is_fan_on;
	}
    return res;
}

uint16_t bms_get_bmu_data(uint8_t bmu_idx, BmuData data)
{
    uint16_t tmp = 0;
	
	if((bmu_idx < BMU_MAX_NUM) && (data < kBmuData_End))
	{
		tmp = bmu_data.bmu_unit[bmu_idx].data[data];
	}
	return tmp;
}

bool bms_is_bmu_online(uint8_t bmu_idx)
{
    bool res = false;
    if(bmu_idx < BMU_MAX_NUM)
	{
		res = (KIT_GET_BIT_32(bmu_data.bmu_unit[bmu_idx].data[kBmuData_Status], kBmuStatus_Online) != 0);
	}
    return res;
}

uint64_t bms_get_bmu_fault_bit(BmuFaultBit idx)
{
    uint64_t tmp = 0;
	
	if(idx < kBmuFaultBit_End)
	{
		tmp = bmu_data.bmu_fault_bit[idx];
	}
	return tmp;
}

void bms_set_bmu_fault_bit(BmuFaultBit idx,uint8_t value)
{
	if(idx < kBmuFaultBit_End)
	{
		bmu_data.bmu_fault_bit[idx] = value;
	}
}

uint32_t bms_get_balance_status(uint8_t bmu_idx, uint8_t idx)
{
	uint32_t tmp = 0;
	
	if((bmu_idx < BMU_MAX_NUM) && (idx < kBmuStatistic_End))
	{
		tmp = bmu_data.bmu_unit[bmu_idx].balance_status[idx];
	}
	return tmp;
}

uint32_t bms_get_bmu_statistic_data(uint8_t bmu_idx, BmuStatisticData idx)
{
	uint32_t tmp = 0;
	tmp = bmu_data.bmu_unit[0].statistic_data[0];
	
	if((bmu_idx < BMU_MAX_NUM) && (idx < kBmuStatistic_End))
	{
		tmp = bmu_data.bmu_unit[bmu_idx].statistic_data[idx];
	}
	return tmp;
}

uint16_t bms_get_cell_volt_by_bmu(uint8_t bmu_idx, uint16_t idx)
{
	uint16_t tmp = 0;
	
	if((bmu_idx < BMU_MAX_NUM) && (idx < bmu_data.bmu_unit[bmu_idx].data[kBmuData_CellNum]))
	{
		tmp = bmu_data.bmu_unit[bmu_idx].cell_volt_buf[idx];
	}
	return tmp;
}

uint16_t bms_get_cell_temp_by_bmu(uint8_t bmu_idx, uint16_t idx)
{
	uint16_t tmp = 0;
	
	if((bmu_idx < BMU_MAX_NUM) && (idx < bmu_data.bmu_unit[bmu_idx].data[kBmuData_TempNum]))
	{
		tmp = bmu_data.bmu_unit[bmu_idx].cell_temp_buf[idx];
	}
	return tmp;
}

uint16_t bms_get_pole_temp_by_bmu(uint8_t bmu_idx, uint16_t idx)
{
	uint16_t tmp = 0;
	
	if((bmu_idx < BMU_MAX_NUM) && (idx < 2) && (bmu_data.has_pole_temp == true))
	{
		tmp = bmu_data.pole_temp_buf[(bmu_idx << 1) + idx];
	}
	return tmp;
}

uint16_t bms_get_balance_temp_by_bmu(uint8_t bmu_idx, uint16_t idx)
{
	uint16_t tmp = 0;
	
	if((bmu_idx < BMU_MAX_NUM) && (idx < 8))
	{
		tmp = bmu_data.bmu_unit[bmu_idx].balance_temp[idx];
	}
	return tmp;
}
