#include "../User/include/includes.h"

GroupInfo_t g_portGp;
OTADataFlashInfo_t p_image_flash;
RAR_Info g_rar_info;

//条件组条件判断默认值
/*  uint8_t     Exist_Flag;         //有人无人标记    1:无人至有人    2:有人至无人  3:短暂离开  4：长时间离开
    uint8_t     Condi_Gruop;        //条件组
    uint8_t     Condi_Subset;       //条件序号

    uint16_t    Judgment_Time;      //条件 - 判定成立时间
    uint8_t     Judgment_Unit;      //条件 - 判定成立时间单位     1,秒     2,分    3,时

    uint8_t     Port_State[VIRTUAL_PORT_MAX];   //端口,0:不判断  1:触发    2:释放    3,开至关    4,关至开

    uint16_t    Timeout_Time;       //条件 - 判定超时时间
    uint8_t     Timeout_Unit;       //条件 - 判定超时时间单位

    uint8_t     Trigger_Flag;       //条件成立标记
 * */
const uint8_t DefaultGroupInfo[VIRTUAL_GP_NUM_MAX][15]=
     {  //无人到有人
        {1,  1,1,  0,0,1,   0,4,0,0,0,  2,0,1,   0},        //开门瞬间
        {1,  2,1,  0,0,1,   0,0,1,0,0,  20,0,1,  0},        //传感器1 触发
        {1,  2,2,  0,0,1,   0,0,0,1,0,  20,0,1,  0},        //传感器2 触发
        {1,  2,3,  0,0,1,   0,0,0,0,1,  20,0,1,  0},        //传感器3 触发
        //有人到无人
        {3,  3,1,  0,0,1,   0,3,0,0,0,  2,0,1,   0},        //关门瞬间
        {3,  4,1,  5,0,1,   0,1,2,2,2,  10,0,2,  0},        //短离
        {4,  5,1,  5,0,1,   0,1,2,2,2,  10,0,2,  0},        //长离
        {2,  6,1,  5,0,2,   0,1,2,2,2,  10,0,2,  0}};       //关门，传感器1，2，3 释放

//端口信息，0A是插拔卡，2是门磁和传感器输入
uint8_t PortInfo[VIRTUAL_PORT_MAX][11]={0x00};


void EEPROM_Init(void)
{
    uint8_t rev = 0x00;
    uint8_t bufEE[5] = {0x00};
    //读取升级标记
    FLASH_ROM_READ(OTA_DATAFLASH_ADD, &p_image_flash, 4);
    /* 擦除整个第一块 */
    FLASH_ROM_ERASE(OTA_DATAFLASH_ADD, FLASH_BLOCK_SIZE);
    /* 更新信息 */
    // Dbg_Println(DBG_BIT_PC_COMM,"Revd:%d",p_image_flash.Revd[0]);
    p_image_flash.Revd[0] = 0;

    if(p_image_flash.ImageFlag != IMAGE_A_FLAG){
        p_image_flash.ImageFlag = IMAGE_A_FLAG;
    }
    /* 编程DataFlash */
    FLASH_ROM_WRITE(OTA_DATAFLASH_ADD, &p_image_flash, 4);

    memset(&g_portGp,0x00,sizeof(GroupInfo_t));
    memset(&g_rar_info,0x00,sizeof(RAR_Info));

    //读取端口号
    rev = EEPROM_Read_PortInfo();
    if(rev == 0x00)
    {
        //读取成功
    }else{
        //读取失败，使用默认数据
        Dbg_Println(DBG_BIT_DEVICE_STATUS,"Use Default PortInfo");
        EEPROM_Default_PortInfo();
    }

    //读取条件组
    rev = EEPROM_Read_PortGroupData(&g_portGp);
    if(rev == 0x00)
    {

    }else{
        //：读取失败，使用默认数据
        Dbg_Println(DBG_BIT_DEVICE_STATUS,"Use Default GroupData");
        EEPROM_Default_PortGroupData();
    }

    rev = EEPROM_Read_RARDelayTim(&g_rar_info);
    if(rev == 0x00)
    {

    }else{
        //：读取失败，使用默认数据
        Dbg_Println(DBG_BIT_DEVICE_STATUS,"Use Default RARDelayTim");
        EEPROM_Default_RARDelayTim(&g_rar_info);
    }

}

uint8_t EEPROM_CheckSum(uint8_t *data,uint16_t len)
{
    uint8_t data_sum = 0;

    for(uint16_t i = 0;i<len;i++)
    {
        data_sum += data[i];
    }
    return ~data_sum;
}

//读取设备的端口设置参数信息
uint8_t EEPROM_Read_PortInfo(void)
{
    uint16_t RD_Len = sizeof(PortInfo);
    uint16_t read_len = 0x00;
    uint8_t read_info[4];
    uint8_t para_data[RD_Len];

    memset(read_info,0,sizeof(read_info));
    memset(para_data,0,sizeof(para_data));

    FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR1,read_info,4);

    if(read_info[0] == EEPROM_SaveInfo_Flag){
        read_len = read_info[2];
        read_len <<= 8;
        read_len |= read_info[1];

        if(read_len <= (EEPROM_INFO_Size - 4) ){
            FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR1+4,para_data,RD_Len);

            if(EEPROM_CheckSum(para_data,read_len) == read_info[3]){
                //校验成功
                memcpy(&PortInfo,para_data,read_len);

                Dbg_Println(DBG_BIT_DEVICE_STATUS, "EEPROM_Read SUCC");
                return 0x00;
            }
        }
    }else{
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "EEPROM_Read Flag Fail:%02x",read_info[0]);
    }

    return 0x01;

}

//端口参数默认信息
void EEPROM_Default_PortInfo(void)
{
    uint8_t Port_DefaultData[11] = {0x00};

    for(uint8_t i = 0; i < VIRTUAL_PORT_MAX; i++)
    {
        if(i == 0x00)       //插卡
        {
            Port_DefaultData[0] = 0x0A;
            Port_DefaultData[1] = 0x01;
            Port_DefaultData[2] = (i + 1);  //映射回路
            Port_DefaultData[3] = 0x00;

            Port_DefaultData[4] = 50;       //释放阈值 有人->无人

            Port_DefaultData[5] = (i + 1);  //虚拟端口
            Port_DefaultData[6] = 0x00;     //单个回路长时间检测 启用标记,默认不开启

            Port_DefaultData[7] = 0x02;     //回路检测统计时间
            Port_DefaultData[8] = 0x00;
            Port_DefaultData[9] = 0x02;     //回路检测统计时间单位

            Port_DefaultData[10] = 20;      //触发阈值 无人->有人
        }
        else if(i == 0x01)     //门磁
        {
            Port_DefaultData[0] = 0x02;
            Port_DefaultData[1] = 0x00;
            Port_DefaultData[2] = (i + 1);  //映射回路
            Port_DefaultData[3] = 0x00;

            Port_DefaultData[4] = 50;

            Port_DefaultData[5] = (i + 1);  //虚拟端口
            Port_DefaultData[6] = 0x00;     //单个回路长时间检测 启用标记,默认不开启

            Port_DefaultData[7] = 0x02;
            Port_DefaultData[8] = 0x00;
            Port_DefaultData[9] = 0x02;

            Port_DefaultData[10] = 20;
        }
        else{                   //其它传感器
            Port_DefaultData[0] = 0x02;
            Port_DefaultData[1] = 0x00;
            Port_DefaultData[2] = (i + 1);  //映射回路
            Port_DefaultData[3] = 0x00;

            Port_DefaultData[4] = 80;

            Port_DefaultData[5] = (i + 1);  //虚拟端口
            Port_DefaultData[6] = 0x00;     //单个回路长时间检测 启用标记,默认不开启

            Port_DefaultData[7] = 0x02;
            Port_DefaultData[8] = 0x00;
            Port_DefaultData[9] = 0x03;

            Port_DefaultData[10] = 20;
        }

        memcpy(&PortInfo[i],Port_DefaultData,11);

    }

}



//保存设备的端口设置参数信息
uint8_t EEPROM_Write_PortInfo(void)
{
    uint16_t data_len = sizeof(PortInfo);
    uint16_t WR_Len = (data_len/4 +1)*4 + 4 ;
    uint8_t data_buff[WR_Len];
    uint8_t rev = 0;
    if(WR_Len > EEPROM_INFO_Size) return 0x01;

    memset(data_buff,0,(data_len + 4));

    data_buff[0] = EEPROM_SaveInfo_Flag;
    data_buff[1] = data_len & 0xFF;
    data_buff[2] = (data_len >> 8) & 0xFF;
    data_buff[3] = 0x00;    //SumCheck

    memcpy(&data_buff[4],(uint8_t *)PortInfo,data_len);
    data_buff[3] = EEPROM_CheckSum(&data_buff[4],data_len);
    data_len += 4;

   // Dbg_Println(DBG_BIT_PC_COMM, "flag:%02x,len:%d,sum:%02x",EEPROM_SaveInfo_Flag,data_len,data_buff[3]);

    //擦除
    rev = FLASH_ROM_ERASE(EEPROM_INFO_SAVE_ADDR1,EEPROM_INFO_Size);
    if(rev == 0x00){
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "1擦除成功!");
    }else {
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "1擦除失败 %02X!",rev);
        return 0x01;
    }

    //写入
    rev = FLASH_ROM_WRITE(EEPROM_INFO_SAVE_ADDR1, data_buff, WR_Len);
    if(rev == 0x00){
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "1参数保存成功");
    }else {
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "1参数保存失败 %02X!",rev);
        return 0x02;
    }

    return 0x00;
}



/***************************************> 条件判断参数   <********************************************/


//读取设备的条件判断参数信息
uint8_t EEPROM_Read_PortGroupData(GroupInfo_t *info)
{
    uint16_t RD_Len = sizeof(GroupInfo_t) ;
    uint16_t read_len = 0x00;
    uint8_t read_info[4];
    uint8_t para_data[RD_Len];

    memset(read_info,0,sizeof(read_info));
    memset(para_data,0,sizeof(para_data));

    FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR2,read_info,4);

    // Dbg_Println(DBG_BIT_PC_COMM, "GroupData:%02x,%02x,%02x,%02x",read_info[0],read_info[1],read_info[2],read_info[3]);
    if(read_info[0] == EEPROM_SaveInfo_Flag){
        read_len = read_info[2];
        read_len <<= 8;
        read_len |= read_info[1];

        if(read_len <= (EEPROM_INFO_Size - 4) ){
            FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR2+4,para_data,read_len);
            // Dbg_Println(DBG_BIT_PC_COMM, "GroupData:%d,%02x,%02x",read_len,EEPROM_CheckSum(para_data,sizeof(PortGroupInfo)),read_info[3]);
            if(EEPROM_CheckSum(para_data,read_len) == read_info[3]){
                // 读取有人、无人条件组数量
                memcpy(info,para_data,sizeof(GroupInfo_t));
                // 读取成功
                Dbg_Println(DBG_BIT_DEVICE_STATUS,"Use:%d,%d,%d,%d",info->VC_ConNToSGruop,info->VC_ConNToSSubset,info->VC_ConSToNGruop,info->VC_ConSToNSubset);

                return 0x00;
            }
        }
    }else{
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "EEPROM_Read Flag Fail2:%02x",read_info[0]);
    }

    return 0x01;
}

//条件组判断默认信息
void EEPROM_Default_PortGroupData(void)
{
    g_portGp.VC_ConNToSGruop = 0x02;     //无卡取电无人至有人条件组数
    g_portGp.VC_ConNToSSubset = 0x04;    //无卡取电无人至有人子条件总数
    g_portGp.VC_ConSToNGruop = 0x06;       //条件组数(总数)
    g_portGp.VC_ConSToNSubset = 0x04;      //无卡取电有人至无人子条件总数

    memcpy(&g_portGp.PortGroupInfo,&DefaultGroupInfo,sizeof(DefaultGroupInfo));

}

//保存设备的条件判断信息
uint8_t EEPROM_Write_PortGroupData(GroupInfo_t *info)
{
    uint16_t data_len = sizeof(GroupInfo_t);      //加上条件组数量相关参数
    uint16_t WR_Len = (data_len/4 +1)*4 + 4 ;
    uint8_t data_buff[WR_Len];
    uint8_t rev = 0;

    if(WR_Len > EEPROM_INFO_Size) return 0x01;

    Dbg_Println(DBG_BIT_DEVICE_STATUS, "EEPROM_Save_GroupData1:%d",data_len);
    memset(data_buff,0x00,WR_Len);

    data_buff[0] = EEPROM_SaveInfo_Flag;
    data_buff[1] = data_len & 0xFF;
    data_buff[2] = (data_len >> 8) & 0xFF;
    data_buff[3] = 0x00;    //Sum Check

    memcpy(&data_buff[4],(uint8_t *)info,data_len);     //减去条件组数量相关参数
    data_buff[3] = EEPROM_CheckSum(&data_buff[4],data_len);
    data_len += 4;

   // Dbg_Println(DBG_BIT_PC_COMM, "GroupDataW22:%d,%d,%d,%d",data_buff[4],data_buff[5],data_buff[6],data_buff[7]);
   // Dbg_Println(DBG_BIT_PC_COMM, "flag:%02x,len:%d,sum:%02x",EEPROM_SaveInfo_Flag,data_len,data_buff[3]);

    //擦除
    rev = FLASH_ROM_ERASE(EEPROM_INFO_SAVE_ADDR2,EEPROM_INFO_Size);
    if(rev == 0x00){
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "2擦除成功!");
    }else {
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "2擦除失败 %02X!",rev);
        return 0x01;
    }

    //写入
    rev = FLASH_ROM_WRITE(EEPROM_INFO_SAVE_ADDR2, data_buff, WR_Len);
    if(rev == 0x00){
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "2参数保存成功");
    }else {
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "2参数保存失败 %02X!",rev);
        return 0x02;
    }

    return 0x00;
}



//保存设备的条件判断信息
uint8_t EEPROM_Read_PortGroupData_2(uint32_t addr,uint16_t len,CONDITION_STRUCT *info)
{
    uint16_t RD_Len = sizeof(CONDITION_STRUCT) ;
    uint16_t read_len = 0x00;
    uint8_t read_info[4];       //读取头
    uint8_t para_data[RD_Len];


    memset(read_info,0,sizeof(read_info));
    memset(para_data,0,sizeof(para_data));

    FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR2,read_info,4);
   // Dbg_Println(DBG_BIT_PC_COMM, "GroupData:%02x,%02x,%02x,%02x",read_info[0],read_info[1],read_info[2],read_info[3]);

    if(read_info[0] == EEPROM_SaveInfo_Flag){
        read_len = read_info[2];
        read_len <<= 8;
        read_len |= read_info[1];

        if(read_len <= (EEPROM_INFO_Size - 4) ){

            FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR2+4,(uint8_t *)info,RD_Len);
        }
    }

    return 0x01;
}

/*********************************   延时处理参数   ***********************************************************/

/***************************************************************
 * 函数名        : EEPROM_Read_RARDelayTim
 * 函数功能    : 读取延迟事件的延迟参数信息
 * 输入参数    : RarInfo : 结构体
 * 返回参数    :
 ****************************************************************/
uint8_t EEPROM_Read_RARDelayTim(RAR_Info *RarInfo)
{
    uint16_t RD_Len = sizeof(RAR_Info) ;
    uint16_t read_len = 0x00;
    uint8_t read_info[4];
    uint8_t para_data[RD_Len];

    memset(read_info,0,sizeof(read_info));
    memset(para_data,0,sizeof(para_data));

    FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR3,read_info,4);

    // Dbg_Println(DBG_BIT_PC_COMM, "GroupData:%02x,%02x,%02x,%02x",read_info[0],read_info[1],read_info[2],read_info[3]);
    if(read_info[0] == EEPROM_RARDelayTim_Flag){
        read_len = read_info[2];
        read_len <<= 8;
        read_len |= read_info[1];

        if(read_len <= (EEPROM_INFO_Size - 4) ){
            FLASH_ROM_READ(EEPROM_INFO_SAVE_ADDR3+4,para_data,read_len);
            if(EEPROM_CheckSum(para_data,read_len) == read_info[3]){
                // 读取有人、无人条件组数量
                memcpy(RarInfo,para_data,sizeof(RAR_Info));
                // 读取成功
                Dbg_Println(DBG_BIT_DEVICE_STATUS,"RARDelayTim:%d,%d,%d,%d",RarInfo->Door_RARON_Time,RarInfo->Door_RARFF_TimeOut,RarInfo->BATH_RARON_Time,RarInfo->BATH_RARFF_TimeOut);

                if(RarInfo->Door_RARON_Time == 0)
                {

                }

                if(RarInfo->Door_RARFF_TimeOut < 5000)
                {
                    RarInfo->Door_RARFF_TimeOut = 5000;
                }

                if(RarInfo->BATH_RARON_Time == 0)
                {

                }

                if(RarInfo->BATH_RARFF_TimeOut < 5000)
                {
                    RarInfo->BATH_RARFF_TimeOut = 5000;
                }

                return 0x00;
            }
        }
    }else{
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "EEPROM_Read RARDelayTim Flag Fail3:%02x",read_info[0]);
    }

    return 0x01;
}


/***************************************************************
 * 函数名        : EEPROM_Write_RARDelayTim
 * 函数功能    : 保存延迟事件的延迟参数信息
 * 输入参数    : RarInfo : 结构体
 * 返回参数    :
 ****************************************************************/
uint8_t EEPROM_Write_RARDelayTim(RAR_Info *RarInfo)
{
    uint16_t data_len = sizeof(RAR_Info);      //加上条件组数量相关参数
    uint16_t WR_Len = (data_len/4 +1)*4 + 4 ;
    uint8_t data_buff[WR_Len];
    uint8_t rev = 0;

    if(WR_Len > EEPROM_INFO_Size) return 0x01;

    Dbg_Println(DBG_BIT_DEVICE_STATUS, "EEPROM_Save_RARDelayTim:%d",data_len);
    memset(data_buff,0x00,WR_Len);

    data_buff[0] = EEPROM_RARDelayTim_Flag;
    data_buff[1] = data_len & 0xFF;
    data_buff[2] = (data_len >> 8) & 0xFF;
    data_buff[3] = 0x00;    //Sum Check

    memcpy(&data_buff[4],(uint8_t *)RarInfo,data_len);     //减去条件组数量相关参数
    data_buff[3] = EEPROM_CheckSum(&data_buff[4],data_len);
    data_len += 4;

  //  Dbg_Println(DBG_BIT_PC_COMM, "GroupDataW22:%d,%d,%d,%d",data_buff[4],data_buff[5],data_buff[6],data_buff[7]);
  //  Dbg_Println(DBG_BIT_PC_COMM, "flag:%02x,len:%d,sum:%02x",EEPROM_SaveInfo_Flag,data_len,data_buff[3]);

    //擦除整页
    rev = FLASH_ROM_ERASE(EEPROM_INFO_SAVE_ADDR3,EEPROM_INFO_Size);
    if(rev == 0x00){
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "3擦除成功!");
    }else {
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "3擦除失败 %02X!",rev);
        return 0x01;
    }

    //写入
    rev = FLASH_ROM_WRITE(EEPROM_INFO_SAVE_ADDR3, data_buff, WR_Len);
    if(rev == 0x00){
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "3参数保存成功");
    }else {
        Dbg_Println(DBG_BIT_DEVICE_STATUS, "3参数保存失败 %02X!",rev);
        return 0x02;
    }

    return 0x00;
}



/***************************************************************
 * 函数名        : EEPROM_Default_RARDelayTim
 * 函数功能    : 延迟事件的延迟参数默认值
 * 输入参数    : RarInfo : 结构体
 * 返回参数    :
 ****************************************************************/
void EEPROM_Default_RARDelayTim(RAR_Info *RarInfo)
{
    RarInfo->Door_RARON_Time = 0;
    RarInfo->Door_RARFF_TimeOut = DOOR_RDR_DELAYTIME;
    RarInfo->BATH_RARON_Time = 0;
    RarInfo->BATH_RARFF_TimeOut = BATH_RDR_DELAYTIME;
}













