#include "AT24C128Opt.h"
#include "../network/downlink.h"
#include "../encode/encodeOpt.h"
/*------------------------------------------------
 *  清空EEPROM的存储空间
 * ------------------------------------------------*/
void fram_init(void)
{
    uint8_t buff[64] = {0};
    int i = 0;
    for(i=0;i<256;i++){
        AT24CXX_Write(0+i*64,buff,sizeof(buff));
    }
//    fram_write_eeprom_powerUpFlag();
}

/*----------------------------------------------
 *  是否第一次上电 的写入
 * ---------------------------------------------*/
void fram_write_eeprom_powerUpFlag(void)
{
    uint16_t version = 0x55AA;
    AT24CXX_Write(FRAM_FIRST_POWER_ON,(uint8_t*)&version,sizeof(uint16_t));
}
/*----------------------------------------------
 *   是否第一次上电 读取
 * ---------------------------------------------*/
uint16_t fram_read_eeprom_powerUpFlag(void)
{
    uint16_t version = 0;
    AT24CXX_Read(FRAM_FIRST_POWER_ON,(uint8_t*)&version,sizeof(uint16_t));

    return version;
}

/*----------------------------------------------
 *  EEPROM 版本号的写入
 * ---------------------------------------------*/
void fram_write_eeprom_version(void)
{
    uint8_t version = 0x01;
    AT24CXX_Write(FRAM_VERSION,&version,1);
}
/*----------------------------------------------
 *  EEPROM 版本号的读取
 * ---------------------------------------------*/
uint8_t fram_read_eeprom_version(void)
{
    uint8_t version = 0;
    AT24CXX_Read(FRAM_VERSION,&version,1);

    return version;
}
/*----------------------------------------------
 * 升级标志的写入
 * --------------------------------------------*/
void fram_write_update_flag(void)
{
    uint8_t flag = GATEWAY_UPDATE_FLAG;
    AT24CXX_Write(FRAM_UPDATE_FLAG,&flag,1);
}
/*----------------------------------------------
 *  配置信息标志的初始化
 * ---------------------------------------------*/
void fram_init_config_msg(void)
{
    GateConfigMsg *gconfigmsg = &(g_firmwareMsg.gatewayMsg.configmsg);

    gconfigmsg->bInitMsg.value = 0;
    gconfigmsg->coll_num = 0;
    gconfigmsg->time_interval = 0;
    AT24CXX_Write(FRAM_CONFIG_MSG,(uint8_t*)gconfigmsg,sizeof(GateConfigMsg));

}
/*-----------------------------------------------
 * 配置信息标志的写入
 * ----------------------------------------------*/
void fram_write_config_msg(uint16_t flag)
{
    GateConfigMsg *gconfigmsg = &(g_firmwareMsg.gatewayMsg.configmsg);
    if(flag==1){
        g_firmwareMsg.gatewayMsg.configmsg.bInitMsg.bInit.bConfig_tab = flag;
    }
    AT24CXX_Write(FRAM_CONFIG_MSG,(uint8_t*)gconfigmsg,sizeof(GateConfigMsg));

   
}
/*------------------------------------------------
 * 配置信息标志的读取
 * -----------------------------------------------*/
uint8_t fram_read_config_msg(void)
{
    GateConfigMsg *gconfigmsg = &(g_firmwareMsg.gatewayMsg.configmsg);

    downlink_config.config_flag = 0;
    downlink_config.collect_num = 0;

    AT24CXX_Read(FRAM_CONFIG_MSG,(uint8_t*)gconfigmsg,sizeof(GateConfigMsg));

    downlink_config.config_flag = gconfigmsg->bInitMsg.bInit.bConfig_tab;
    g_encoderDa.bhaveEncoder = gconfigmsg->bInitMsg.bInit.bConfig_qei;
    g_encodeOpt.benable = gconfigmsg->bInitMsg.bInit.benable_qei;
  //  if(gconfigmsg->coll_num==0xFF) gconfigmsg->coll_num = 0x00;
    downlink_config.collect_num = gconfigmsg->coll_num;
   // if(gconfigmsg->time_interval < 2) gconfigmsg->time_interval = 2;
	
    return downlink_config.config_flag;
}
/*-------------------------------------------------
 *  mqqtt ip prot的初始化
 * ----------------------------------------------------*/
void fram_init_mqtt_msg(void)
{
    MqttIdPort *mqttMsg = &(g_firmwareMsg.mqttidport);

    mqttMsg->flag = 0;
    memset(mqttMsg,0,sizeof(MqttIdPort));
    AT24CXX_Write(FRAM_MQTT_MSG,(uint8_t*)mqttMsg,sizeof(MqttIdPort));

}
/*-------------------------------------------------
 *  mqqtt ip prot的写 入
 * ----------------------------------------------------*/
void fram_write_mqtt_msg(void)
{
    MqttIdPort *mqttMsg = &(g_firmwareMsg.mqttidport);

    AT24CXX_Write(FRAM_MQTT_MSG,(uint8_t*)mqttMsg,sizeof(MqttIdPort));

}
/*-------------------------------------------------
 *  mqqtt ip prot的初始化
 * ----------------------------------------------------*/
void fram_read_mqtt_msg(void)
{
    MqttIdPort *mqttMsg = &(g_firmwareMsg.mqttidport);

    AT24CXX_Read(FRAM_MQTT_MSG,(uint8_t*)mqttMsg,sizeof(MqttIdPort));

}

/*-----------------------------------------------
 *  网关的硬件信息读取
 * -----------------------------------------------*/
void fram_read_gateway_hardware_msg(void)
{
    GateHareWareMsg *msg = &(g_firmwareMsg.gatewayMsg.hardwareMsg);

    downlink_config.gateway_id = 0x000000;

    AT24CXX_Read(FRAM_GATWAY_HARDWARE_MSG,(uint8_t*)msg,sizeof(GateHareWareMsg));

    downlink_config.gateway_id = msg->gateway_sn;
}
/*-----------------------------------------------
 *  网关的硬件信息写入
 * -----------------------------------------------*/
void fram_write_gateway_hardware_msg(void)
{
    GateHareWareMsg *msg = &(g_firmwareMsg.gatewayMsg.hardwareMsg);

    AT24CXX_Write(FRAM_GATWAY_HARDWARE_MSG,(uint8_t*)msg,sizeof(GateHareWareMsg));

}

/*----------------------------------------------
 *  网关信息的初始化
 * ---------------------------------------------*/
void fram_init_gateway_msg(void)
{
    g_firmwareMsg.gatewayMsg.configmsg.coll_num = 0;
    g_firmwareMsg.gatewayMsg.configmsg.bInitMsg.value = 0;
    g_firmwareMsg.gatewayMsg.gate_resetnum = 0;
    fram_write_config_msg(0);
    fram_init_gateway_resetnum();
}

/*----------------------------------------------------
 *  网关复位次数的初始化
 * ---------------------------------------------------*/
void fram_init_gateway_resetnum(void)
{
//    uint32_t num = 0;

    return;
//    g_firmwareMsg.gatewayMsg.gate_resetnum = 0;
//    AT24CXX_Write(FRAM_GATWAY_RESETNUM,(uint8_t*)&num,sizeof(uint32_t));

}
/*----------------------------------------------------
 *  网关复位次数的写入
 * ---------------------------------------------------*/
void fram_wirte_gateway_resetnum(void)
{
//    uint32_t num = 0;

    return;
//    num = g_firmwareMsg.gatewayMsg.gate_resetnum;
//   AT24CXX_Write(FRAM_GATWAY_RESETNUM,(uint8_t*)&num,sizeof(uint32_t));

}
/*----------------------------------------------------
 *  网关复位次数的读取
 * ---------------------------------------------------*/
void fram_read_gateway_resetnum(void)
{
    uint32_t num = 0;
    AT24CXX_Read(FRAM_GATWAY_RESETNUM,(uint8_t*)&num,sizeof(uint32_t));

    g_firmwareMsg.gatewayMsg.gate_resetnum = num;
}
/*------------------------------------------------------
 *  网关软件版本的信息初始化
 *  固件信息格式 ：DDDDFNNN:
 *  DDDD(设备类型)：0x0101:网关A 0x0102:网关B 0x0201:采集器1 0x0202：采集器2
 *  F(固件类型)：0x01:bootloader版本 0x02:pt版本 0x03:app版本 0xF:合成版本
 *  NNN:0x001 表示第1个版本 0x002:表示第2个版本
 * -----------------------------------------------------*/
void fram_init_gateway_version(void)
{
    GateVersionMsg *msg = NULL;

    g_firmwareExpl.dd = GATEWAY_DEVICE_TYPE; //网关A
    g_firmwareExpl.ff = GATEWAY_APP_TYPE; //app版本
    g_firmwareExpl.nn = GATEWAY_APP_VERSION; // 第一个版本

    fram_read_gateway_version();
     msg = &(g_firmwareMsg.gatewayMsg.verMsg);
	
		if(msg->gate_appVr != (((uint32_t)g_firmwareExpl.dd<<16)|((uint16_t)g_firmwareExpl.ff<<12)|g_firmwareExpl.nn)){
			 msg->gate_appVr = ((uint32_t)g_firmwareExpl.dd<<16)|((uint16_t)g_firmwareExpl.ff<<12)|g_firmwareExpl.nn;
			// msg->gate_appVr2 = msg->gate_appVr;
			// msg->gate_appVr3 = msg->gate_appVr;

			 fram_write_gateway_version();
		}
}

/*----------------------------------------------------
 *  网关 版本信息的读取
 * ---------------------------------------------------*/
void fram_write_gateway_version(void)
{
    GateVersionMsg *msg = NULL;

     msg = &(g_firmwareMsg.gatewayMsg.verMsg);
    AT24CXX_Write(FRAM_GATWAY_VER_MSG,(uint8_t*)msg,sizeof(GateVersionMsg));

}
/*----------------------------------------------------
 *  网关 版本信息的读取
 * ---------------------------------------------------*/
void fram_read_gateway_version(void)
{
    GateVersionMsg *msg = NULL;

    msg = &(g_firmwareMsg.gatewayMsg.verMsg);

    AT24CXX_Read(FRAM_GATWAY_VER_MSG,(uint8_t*)msg,sizeof(GateVersionMsg));
			
	//	fram_init_gateway_version();

}


/*----------------------------------------------
 *  采集器信息的初始化
 * ---------------------------------------------*/
void fram_init_collect_msg(void)
{
    uint8_t i = 0;
    uint16_t len = 0;
    uint8_t buff[1200] = {0};

   // collect_conf_t *collMsg = NULL;

    len = sizeof(collect_conf_t)*16;
    memset(buff,0,len);


    AT24CXX_Write(FRAM_COLLECT_MSG+i,buff,len);

}
/*----------------------------------------------
 *  采集器信息的写入
 * ---------------------------------------------*/
void fram_write_collect_msg(void)
{
    uint8_t i = 0;
    uint16_t len = 0;

    collect_conf_t *collMsg = NULL;

    len = sizeof(collect_conf_t);
    for(i = 0;i < 16;i++){
        collMsg = &(downlink_config.collect_conf[i]);
        AT24CXX_Write(FRAM_COLLECT_MSG+i*len,(uint8_t *)collMsg,len);

    }
}
/*----------------------------------------------
 *  采集器信息的读取
 * ---------------------------------------------*/
void fram_read_collect_msg(void)
{
    uint8_t i = 0;
    uint16_t len = 0;

    collect_conf_t *collMsg = NULL;

		
    len = sizeof(collect_conf_t);
    for(i = 0;i < 16;i++){
        collMsg = &(downlink_config.collect_conf[i]);
        AT24CXX_Read(FRAM_COLLECT_MSG+i*len,(uint8_t*)collMsg,len);
        collMsg->collect_status = 0x00;
        //memset(downlink_config.collect_conf[i],collMsg,len);
    }
}
/*----------------------------------------------
 *  写 lora 编号
 * ---------------------------------------------*/
void fram_write_lora_index(uint8_t index)
{
    uint16_t value = 0;
    if(index>3 ) return;//&& index <0

    value = (~index);
    value = value<<8;
    value = (value | index);
    AT24CXX_Write(FRAM_LORA_INDEX,(uint8_t*)&value,2);

}

/*----------------------------------------------
 *  读取 lora 编号
 * ---------------------------------------------*/
uint8_t fram_read_lora_index(void)
{
    uint16_t value = 0;
    uint8_t index = 0, nindex=0;

    AT24CXX_Read(FRAM_LORA_INDEX,(uint8_t*)&value,2);

    index = value &0xFF;
    nindex  = (~(value >>8))&0xFF;
    if(index != nindex) return 0;
    else return index;
}
/*-----------------------------------------------------
 *  编码器的配置表的存储
 * ----------------------------------------------------*/
void fram_init_encoder_configMsg(void)
{
    uint8_t value = 0;
    AT24CXX_Write(FRAM_ENCODEINIT,(uint8_t *)&value,1);
}

void fram_write_encoder_configMsg(void)
{
    uint8_t value = 0;
    value = g_encodeOpt.encodenum;
    AT24CXX_Write(FRAM_ENCODEINIT,(uint8_t *)&value,1);


}
/*-----------------------------------------------------
 *  编码器的配置表读取
 * ----------------------------------------------------*/
void fram_read_encoder_configMsg(void)
{
    uint8_t value = 0;

    AT24CXX_Read(FRAM_ENCODEINIT,(uint8_t*)&value,1);
    g_encodeOpt.encodenum = value;
}

/*------------------------------------------------------
 *  编码器信息表存储
 * ------------------------------------------------------*/
void fram_init_encoder_msg(void)
{
    uint8_t i=0;
    uint16_t len = 0;

    table_msg *msg = NULL;
    len = sizeof(table_msg);
    for(i=0;i<64;i++){
        msg = &(g_encodeOpt.tableDa[i]);
        memset((char*)msg,0x00,len);
        AT24CXX_Write(FRAM_ENCODEMSG+i*len,(uint8_t *)msg,len);
    }
}

void fram_write_encoder_msg(void)
{
    uint8_t i=0;
    uint16_t len = 0;

    table_msg *msg = NULL;
    len = sizeof(table_msg);
    for(i=0;i<64;i++){
        msg = &(g_encodeOpt.tableDa[i]);
        AT24CXX_Write(FRAM_ENCODEMSG+i*len,(uint8_t *)msg,len);
    }
}
/*---------------------------------------------------------
 *  编码器信息表的读取
 * -------------------------------------------------------*/
void fram_read_encoder_msg(void)
{
    uint8_t i = 0;
    uint16_t len = 0;

    table_msg *msg = NULL;


    len = sizeof(table_msg);
    for(i = 0;i < 64;i++){
        msg = &(g_encodeOpt.tableDa[i]);
        AT24CXX_Read(FRAM_ENCODEMSG+i*len,(uint8_t*)msg,len);
    }
}
