#include "version_manager.h"
#include "flash_manager.h"
#include "eeprom_manager.h"

#include "kit_data.h"

#define SOFT_ETAG_ADDR			(APP_START_ADDR + APP_CFG_TAG_OFFSET)
#define BOOT_SOFT_ETAG_ADDR		(APP_CFG_BOOT_START_ADDR + APP_CFG_TAG_OFFSET)

typedef struct
{
	SoftType soft_type;
	uint8_t  bulid_ver;
    uint16_t produce_number; //产品编码
	uint32_t soft_ver;
	uint8_t compile_date[12];
	uint8_t compile_time[10];
	uint8_t reverse2[68];
}SoftEtag;

const SoftEtag soft_etag __attribute__((at(SOFT_ETAG_ADDR))) = 
{
	kSoftType_App,
	DEVICE_SW_BUILD_VER,
    PRODUCT_NUMBER,
	DEVICE_SW_VERSION,
	__DATE__,
	__TIME__,
	{0},
};

typedef struct
{
    uint16_t magic_num;
    uint32_t data[kTagData_End];
}TagItem;

TagItem tag_item;

void bms_init_tag(void)
{
    tag_item.data[kTagData_DevType] = APP_DEVICE_TYPE;
    tag_item.data[kTagData_SoftVer] = soft_etag.soft_ver;
    tag_item.data[kTagData_ProductNumber] = soft_etag.produce_number;
    tag_item.data[kTagData_HardVer] = *((uint16_t *)TAG_HARDWARE_VER_ADDR);
    if(tag_item.data[kTagData_HardVer] == 0xFFFF)
    {
        tag_item.data[kTagData_HardVer] = DEVICE_HW_VERSION;
    }
    tag_item.data[kTagData_BootVer] = ((SoftEtag *)BOOT_SOFT_ETAG_ADDR)->soft_ver;
	
    tag_item.magic_num = *((uint16_t *)TAG_UUID_ADDR);
	//uuid中的时间以大端存入
	tag_item.data[kTagData_ProductTime] = kit_bt_read_buf((uint8_t *)TAG_UUID_ADDR, 0, 4);
    tag_item.data[kTagData_DevAddr] = get_eeprom_data(kEep_DevAddr_SlaverNum, kEepromDataType_High);
}

uint32_t bms_get_tag_data(TagData idx)
{
    uint32_t res = 0;
    if(idx < kTagData_End)
    {
        res = tag_item.data[idx];
    }
    return res;
}

void bms_set_tag_data(TagData idx, uint32_t data)
{
    if(idx < kTagData_End)
    {
        tag_item.data[idx] = data;
    }
}

uint64_t bms_get_uuid(void)
{
    return ((uint64_t)tag_item.data[kTagData_ProductTime] << 16 | tag_item.magic_num);
}

