/***********************************************************************************
 * @Copyright (c)	:(C)2013, Qindao ieslab Co., Ltd
 * @FileName   	      :new_gsm_EEPROM_processor.c
 * @Author       	:Intelligent gas team
 * @Version      	:V1.0
 * @Date         	:2013-11-29
 * @Description	:the function of the entity of system processor
 ************************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif
/*--------------------------------Including-------------------------------------------*/
#include "UWater_sys.h"
#include "UWater_frame_app.h"
#include "UWater_eeprom_driver.h"
#include "UWater_encryption_driver.h"
#include "UWater_valve_driver.h"
#include "UWater_uart_driver.h"
#include "UWater_timer1uS_driver.h"
#include "UWater_valve_app.h"
#include "UWater_rtcc_app.h"
#include "UWater_gp22_app.h"
#include "SOE.h"
#include "nb_upload_monitor.h"
#include "eeprom_app.h"
/*------------------------------ Variable Define -------------------------------------*/

//static u8 ee_package_rd_num;		//读包号
//static u8 ee_package_wr_num;		//写包号
//static u8 ee_package_erase_num;		//擦除包号
static IMPORTANT_DATA_OP important_data_rd_state = IMPORTANT_DATA_OP_S0; 
//static IMPORTANT_DATA_OP important_data_wr_state = IMPORTANT_DATA_OP_S0;
//static IMPORTANT_DATA_OP important_data_erase_state = IMPORTANT_DATA_OP_S0;
static u8 important_data_rd_msg;
static TYPE_EE_INIT_STATE tmp_init_stat = EE_INIT_STATE_S0;//各类参数初始化状态 
static u8 task_init_complete = FALSE;//各类参数初始化任务完成标志
static u32 frame_para_buf[FRAME_PARA_BUF_LEN];//系统参数

Data_object Sys_data_object;//系统参数缓存
DateTime rtcc_tmp_ee_app;


static void SysData_Init(void);//系统参数初始化
static void Coefficient_Init(void); //多项式参数初始化
static void Flow_coef_Init(void);//流量系数初始化
static void UDP_Update_Init(void);//UDP ip初始化
static void SOE_Pint_Init(void);
static void RtccInitApp(void);

//根据要写的变量地址，计算ee地址，主要用于Sys_data_object结构体变量
u16 ee_addr_compute(u8 *data_addr)
{
    u16 addr_result;
    if(data_addr >= (u8*)(&Sys_data_object.P1))
    {
        if( data_addr < ((u8*)(&Sys_data_object.P1) + sizeof(Sys_data_object.P1)) )
        {
            addr_result = EEP_FRAME_BASE_ADDR + data_addr - (u8*)(&Sys_data_object.P1);
            return addr_result;	
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}

//首次上电初始化部分变量
void FirstPowerOnInit(void)
{
  	//Sys_data_object.P1.first_power_flg = FRAME_FIRST_POWER_FLG;//u8 first_power_flg---第一次上电标志,置起
	Sys_data_object.P1.start_using_flg = 0x00; //0x00,//u8 start_using_flg---出厂启用标志，默认不出厂启用
	/*上报频率u8 report_freq_val[5]*/
	Sys_data_object.P1.Time_upload_2006.s.type   = 0x02; //0x02,//report_freq_val[0]---天模式
	Sys_data_object.P1.Time_upload_2006.s.period = 0x00; //0x00,//report_freq_val[1]---天模式无效
	Sys_data_object.P1.Time_upload_2006.s.hour   = 0x06; //0x06,//report_freq_val[2]---6点
	Sys_data_object.P1.Time_upload_2006.s.minute = 0x00; //0x00,//report_freq_val[3]---0分
	Sys_data_object.P1.Time_upload_2006.s.second = 0x00; //0x00,//report_freq_val[4]---0秒
    /*ip地址:117.60.157.137*/
	Sys_data_object.P1.server_ip[0] =221;//117; //117,//server_ip[0]
	Sys_data_object.P1.server_ip[1] = 229;//60;  //60,//server_ip[1]
	Sys_data_object.P1.server_ip[2] =214;//157;  //157,//server_ip[2]
	Sys_data_object.P1.server_ip[3] =202;//137;  //137,//server_ip[3]
	/*端口号:5683=0x1633*/
	Sys_data_object.P1.server_port.datas = 5683; 
	//0x33,//server_port.datas_map.data0
	//0x16,//server_port.datas_map.data1      
	/*周期上报起始时间:19-7-1-00-00-00*/
	Sys_data_object.P1.report_period_start[0]=0x00; //0x00,//report_period_start[0]---秒
	Sys_data_object.P1.report_period_start[1]=0x00; //0x00,//report_period_start[1]---分
	Sys_data_object.P1.report_period_start[2]=0x00; //0x00,//report_period_start[2]---时
	Sys_data_object.P1.report_period_start[3]=0x01; //0x01,//report_period_start[3]---日
	Sys_data_object.P1.report_period_start[4]=0x07; //0x07,//report_period_start[4]---月
	Sys_data_object.P1.report_period_start[5]=0x19; //0x19,//report_period_start[5]---年
	/*周期上报结束时间:29-7-1-00-00-00*/
	Sys_data_object.P1.report_period_end[0]=0x00; //0x00,//report_period_end[0]---秒
	Sys_data_object.P1.report_period_end[1]=0x00; //0x00,//report_period_end[1]---分
	Sys_data_object.P1.report_period_end[2]=0x00; //0x00,//report_period_end[2]---时
	Sys_data_object.P1.report_period_end[3]=0x01; //0x01,//report_period_end[3]---日
	Sys_data_object.P1.report_period_end[4]=0x07; //0x07,//report_period_end[4]---月
	Sys_data_object.P1.report_period_end[5]=0x99; //0x99,//report_period_end[5]---年
	/*周期上报最小估长值:15*/
	Sys_data_object.P1.report_step_len.datas =15;// 15,//report_step_len.datas_map.data0 //估长值改为15s
	/*重试次数*/
	Sys_data_object.P1.report_retry_times =0; //默认重试次数改为0 
	/*密集上报开始时间*/
	Sys_data_object.P1.report_start_time[0] = 0x01; //0x01,//report_start_time[0]---起始日
	Sys_data_object.P1.report_start_time[1] = 0x07; //0x07,//report_start_time[1]---起始月
	Sys_data_object.P1.report_start_time[2] = 0x19; //0x19,//report_start_time[2]---起始年
	Sys_data_object.P1.report_start_time[3] = 0x00; //0x00,//report_start_time[3]---结束日
	Sys_data_object.P1.report_start_time[4] = 0x30; //0x30,//report_start_time[4]---结束月
	Sys_data_object.P1.report_start_time[5] = 0x01; //0x01,//report_start_time[5]---结束年
	/*密集上报结束时间*/
	Sys_data_object.P1.report_start_time[6] = 0x02; //0x02,//report_start_time[6]---开始采集时间秒
	Sys_data_object.P1.report_start_time[7] = 0x07; //0x07,//report_start_time[7]---开始采集时间分
	Sys_data_object.P1.report_start_time[8] = 0x19; //0x19,//report_start_time[8]---开始采集时间时
	/*大流量报警门限值：3000L/h=0x00000BB8*/
	Sys_data_object.P1.high_flow_alarm.datas = 3000;
	//0xB8,//high_flow_alarm.datas_map.data0
	//0x0B,//high_flow_alarm.datas_map.data1
	//0x00,//high_flow_alarm.datas_map.data2
	//0x00,//high_flow_alarm.datas_map.data3
	/*阀控参数 6个字节*/
	Sys_data_object.P1.ies_valve_paras.valve_state = VALVE_STATE_OPEN;//阀门状态
	Sys_data_object.P1.ies_valve_paras.valve_err = 0;                 //阀门是否异常
	Sys_data_object.P1.ies_valve_paras.valve_total_time = 20;          //阀控最大时间
	Sys_data_object.P1.ies_valve_paras.valve_AD = VALVE_HOGH_ADC_VALUE;//阀控AD默认值
	Sys_data_object.P1.ies_pulse_emergency.datas = 5;	//应急用水量 5方
	/*通信频点:850=0x352*/
	Sys_data_object.P1.report_fre.datas =850; //0x52,//report_fre.datas_map.data0
	//0x03,//report_fre.datas_map.data1
	/*APN20字节:ctnb*/	
	Sys_data_object.P1.apn[0] = 0x01; //APN使能
	Sys_data_object.P1.apn[1] = 0x04; //0x05,//apn[1]APN长度
	Sys_data_object.P1.apn[2] = 'c';
	Sys_data_object.P1.apn[3] = 't';
	Sys_data_object.P1.apn[4] = 'n';
	Sys_data_object.P1.apn[5] = 'b';
	/*普通密钥---16字节:QINGDAOJICHE-GNB*/
	Sys_data_object.P1.key[0] = 81;								
	Sys_data_object.P1.key[1] = 73;
	Sys_data_object.P1.key[2] = 78;
	Sys_data_object.P1.key[3] = 71;
	Sys_data_object.P1.key[4] = 68;
	Sys_data_object.P1.key[5] = 65;
	Sys_data_object.P1.key[6] = 79;
	Sys_data_object.P1.key[7] = 74;
	Sys_data_object.P1.key[8] = 73;
	Sys_data_object.P1.key[9] = 67;
	Sys_data_object.P1.key[10] = 72;
	Sys_data_object.P1.key[11] = 45;
	Sys_data_object.P1.key[12] = 78;
	Sys_data_object.P1.key[13] = 71;
	Sys_data_object.P1.key[14] = 78;
	Sys_data_object.P1.key[15] = 66;
	/*物理地址---7字节*/
	Sys_data_object.P1.mac_addr[0] = 0;
	Sys_data_object.P1.mac_addr[1] = 0;
	Sys_data_object.P1.mac_addr[2] = 0;
	Sys_data_object.P1.mac_addr[3] = 0;
	Sys_data_object.P1.mac_addr[4] = 0;
	Sys_data_object.P1.mac_addr[5] = 0;
	Sys_data_object.P1.mac_addr[6] = 0;
	/*序列号---7字节*/
	Sys_data_object.P1.ser_num[0] =0;
	Sys_data_object.P1.ser_num[1] =0;
	Sys_data_object.P1.ser_num[2] =0;
	Sys_data_object.P1.ser_num[3] =0;
	Sys_data_object.P1.ser_num[4] =0;
	Sys_data_object.P1.ser_num[5] =0;
	Sys_data_object.P1.ser_num[6] =0;
	/*通信地址---7字节*/
	Sys_data_object.P1.met_addr[0] =0x01;
	Sys_data_object.P1.met_addr[1] =0;
	Sys_data_object.P1.met_addr[2] =0;
	Sys_data_object.P1.met_addr[3] =0;
	Sys_data_object.P1.met_addr[4] =0;
	Sys_data_object.P1.met_addr[5] =0x33;
	Sys_data_object.P1.met_addr[6] =0x78;
	/*固化物理地址标识---1字节*/
	Sys_data_object.P1.solidify_mac_addr_flg = 0x00;
	/*关阀后，上报参数---5字节,*/
	Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre = 4;	//关阀后上报间隔(4*30)
	Sys_data_object.P1.ies_valve_up_times.valve_up_times_total = 24;
	Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime = 0;	
	Sys_data_object.P1.ies_valve_up_times.valve_auto_check = VALVE_AUTO_CHECK_OPEN;//VALVE_AUTO_CHECK_CLOSE;//阀门每月初自检是否开启，=0xAA表示开启，默认关闭
	Sys_data_object.P1.ies_valve_up_times.reserv = 0x00;
	/*启用备份密钥---1字节*/
	Sys_data_object.P1.enable_bak_key = 0x00; 
	/*下次通讯的密钥num---1字节*/
	Sys_data_object.P1.last_key_num = 0x00;
	/*温度系数0---4字节*/
	Sys_data_object.P1.temperature_coefficient0.datas = 0x00000000;//0x00,0x00,0x00,0x00,//temperature_coefficient0
	/*温度系数1---4字节*/
	Sys_data_object.P1.temperature_coefficient1.datas = 0x00000000;//0x00,0x00,0x00,0x00,//temperature_coefficient1
	/*小流量系数---4字节*/
	Sys_data_object.P1.flow_coefficient_small.datas = 0x00010000;//0x00,0x00,0x01,0x00,//flow_coefficient_small   0x00010000
	/*中流量系数0---4字节*/
	Sys_data_object.P1.flow_coefficient_middle0.datas = 0x00010000;//0x00,0x00,0x01,0x00,//flow_coefficient_middle0 0x00010000
	/*中流量系数1---4字节*/
	Sys_data_object.P1.flow_coefficient_middle1.datas = 0x00010000;//0x00,0x00,0x01,0x00,//flow_coefficient_middle1 0x00010000
	/*中流量系数2---4字节*/
	Sys_data_object.P1.flow_coefficient_middle2.datas = 0x00010000;//0x00,0x00,0x01,0x00,//flow_coefficient_middle2 0x00010000
	/*中流量系数3---4字节*/
	Sys_data_object.P1.flow_coefficient_middle3.datas = 0x00010000;//0x00,0x00,0x01,0x00,//flow_coefficient_middle3 0x00010000  
	/*大流量系数---4字节*/
	Sys_data_object.P1.flow_coefficient_large.datas = 0x00010000;//0x00,0x00,0x01,0x00,//flow_coefficient_large   0x00010000
	/*启用检定功能---1字节*/
	Sys_data_object.P1.enable_verification_function =0x5A;// 0x5A,//enable_verification_function 
	/*启用红外功能---1字节*/
	Sys_data_object.P1.enable_infrared_function = 0x5A;// 0x5A,//enable_infrared_function
	/*功能配置字---32字节*/
	for(int i=0;i<32;i++)
	{
		Sys_data_object.P1.func_cfg_bytes[i] = 0x00;
	}
	Sys_data_object.P1.func_cfg_bytes[0]=15;//func_cfg_bytes[0]---厂区工作时间,默认15天
	//0x00,//func_cfg_bytes[1]---红外按键阈值:0x10=16 ---单板删除
	//0x00,//func_cfg_bytes[2]远红外调试功能默认关闭,(0xAA打开)
	//0x00,//func_cfg_bytes[3]触摸按键功能默认关闭(0xAA打开)
	////func_cfg_bytes[4~5]原-阀控过流ADC默认值 
	//0x00,//[6]原---关阀上报次数
    //0x00,//[7]0x55需要补报,默认不补报
	Sys_data_object.P1.func_cfg_bytes[8]=0xAA,//[8]短帧上报标志(0xAA为短帧)
	//Sys_data_object.P1.func_cfg_bytes[9]=0xAA,//[9]0XAA新补报规约
	Sys_data_object.P1.func_cfg_bytes[10]=0x55;//[10]0x55开启ACK判断上报成功
	//Sys_data_object.P1.func_cfg_bytes[11]=0x55;//[11]0x55,MN316降低发射功率
	//Sys_data_object.P1.func_cfg_bytes[12]=0x80,//[12]使用PSM模式进行上报,0x80关阀使用PSM,0x82一直使用PSM
	Sys_data_object.P1.func_cfg_bytes[13]=0x11;//剩余电量对应上报频率设置1
	Sys_data_object.P1.func_cfg_bytes[14]=0x23;//剩余电量对应上报频率设置2
	Sys_data_object.P1.func_cfg_bytes[15]=0x7F;//剩余电量对应上报频率设置3 	//func_cfg_bytes[15]---原---定时活动阀门默认开启
	Sys_data_object.P1.func_cfg_bytes[16]=2;//[16]蓝牙:广播间隔2s,单位秒,
	Sys_data_object.P1.func_cfg_bytes[17]=50;//[17]蓝牙:连接间隔50ms,单位ms
	Sys_data_object.P1.func_cfg_bytes[18]=0x00;//[18]蓝牙:发射功率,0:2.5dbm;1:0dbm;2:-5dbm;3:-10dbm
	Sys_data_object.P1.func_cfg_bytes[19]=5;//[19]蓝牙:扫描次数5
	Sys_data_object.P1.func_cfg_bytes[20]=5;//应急用水量默认5方
	Sys_data_object.P1.func_cfg_bytes[21]=0;//密码配对功能,默认0关闭
	Sys_data_object.P1.func_cfg_bytes[22]=7;//蓝牙复位间隔,默认7天
	Sys_data_object.P1.func_cfg_bytes[23]=20;//蓝牙每天允许复位次数,默认20
	/*使动流量---2字节:默认600ps=0x0258*/
	//(DEFAULT_START_FLOW&0xFF),((DEFAULT_START_FLOW>>8)&0xFF), //#define   DEFAULT_START_FLOW   0x00D1 //DN20铜63KPA时的使动流量数值   
	Sys_data_object.P1.start_flow.datas = DEFAULT_START_FLOW;
	/*逆流计量方式---2字节:默认方式1*/
	Sys_data_object.P1.counter_current_flow.datas = 0x2B1A;//0x1A,0x2B,
	/*采样频率---1字节:默认8*/
	Sys_data_object.P1.sampling_frequency = 0x08;// 0x08,
	/*采样缓冲区大小---1字节:默认16*/
	Sys_data_object.P1.buf_length = 0x10;// 0x10,
	/*默认密集存储间隔0:30s*/
	Sys_data_object.P1.storage_frequency = 15;//江西改为默认15分钟存储间隔
	/*默认最低采样频率4(8Hz),最高采样频率5(16Hz)*/
	Sys_data_object.P1.jx_sampling_frequency[0] = 0x04; //0x04,0x05,
	Sys_data_object.P1.jx_sampling_frequency[1] = 0x05;
	/*默认NB工作模式为断电模式*/
	Sys_data_object.P1.jx_psm_pwdown =0x01; //0x01,
	/*默认NB上报失败后4小时重试,符网时间查询cereg 40次*/
	//0x40,0x38,60,0x00
	#if PRODUCT_SY
	    /*沈阳规约默认NB上报失败后30分钟重试,符网时间查询cereg 60次*/
		Sys_data_object.P1.Upload_again_interval_time.datas = 1800;//1800=30x60
	#else
		Sys_data_object.P1.Upload_again_interval_time.datas = 14400;//14400=4x3600
	#endif
	Sys_data_object.P1.nb_para_login_ask_times.datas = 40;//CGATT查询次数
	//补报相关参数
	Sys_data_object.P1.Supplemental_send_flag.datas = 0;
	Sys_data_object.P1.histroy_r = 0;
	Sys_data_object.P1.histroy_w = 0;
	//
	Sys_data_object.P1.ies_pulse_remain.datas = 0;//已使用量的应急用水量
	//电池电量相关参数
	//#if UPLOAD_MODE_1
	Sys_data_object.P1.run_information_object.total_use_sum_times_sec.to_uint32 = 0;//表计工作总时间
	Sys_data_object.P1.run_information_object.bat_surplus.to_uint32 = ENERGY_OF_26500;//电池电量uAH
	Sys_data_object.P1.run_information_object.use_sum_times_sec.to_uint32 = 0;//红外累计工作时间
	Sys_data_object.P1.run_information_object.nb_use_sum_times_sec.to_uint32 = 0;//nb累计工作时间
	Sys_data_object.P1.run_information_object.nb_upload_happen_num = 0;//nb激活次数
	Sys_data_object.P1.run_information_object.valve_use_sum_times_sec = 0;//阀门工作时间
	Sys_data_object.P1.run_information_object.ee_remain_para_energy_life = 3;//比值
	//#endif
	//蓝牙参数
	Sys_data_object.P3.function_switch.ble_adv_fre = Sys_data_object.P1.func_cfg_bytes[16];//广播间隔2秒
	Sys_data_object.P3.function_switch.ble_con_para = Sys_data_object.P1.func_cfg_bytes[17];//连接间隔50ms
	Sys_data_object.P3.function_switch.ble_tx_power = Sys_data_object.P1.func_cfg_bytes[18];//发射功率
	Sys_data_object.P3.function_switch.ble_scan_times = Sys_data_object.P1.func_cfg_bytes[19];//扫描次数
	Sys_data_object.P1.ies_pulse_emergency.datas =  Sys_data_object.P1.func_cfg_bytes[20];//应急用水量
	Sys_data_object.P3.function_switch.password_enable = Sys_data_object.P1.func_cfg_bytes[21];//使能密码配对
}

//从EE中读重要数据
void set_important_data_rd_state (void)
{
    important_data_rd_state = IMPORTANT_DATA_OP_S1;
}
u8 get_important_data_rd_state (void)
{
    return important_data_rd_state;
}
void LoadEepromImportantData(void)
{
    set_important_data_rd_state();
    while(get_important_data_rd_state())
    {
        EeReadImportantDataMachine();
        EepMachineDriver();
    }
	Meter_BootVersionInit();//获取boot版本号
	Sys_data_object.P3.NBSleepMode = 0;//非PSM模式上报
}
//读取重要参数
void EeReadImportantDataMachine(void)
{
    switch(important_data_rd_state)
    {
        case IMPORTANT_DATA_OP_S0:
		{
		}
        break;

        case IMPORTANT_DATA_OP_S1:
        {
			task_init_complete = FALSE;
			tmp_init_stat = EE_INIT_STATE_S1;
			important_data_rd_state=IMPORTANT_DATA_OP_S2;
        }
        break;

        case IMPORTANT_DATA_OP_S2://系统参数初始化
        {
            SysData_Init();
			if(task_init_complete == TRUE)
			{
				task_init_complete = FALSE;
				tmp_init_stat = EE_INIT_STATE_S1;
				important_data_rd_state=IMPORTANT_DATA_OP_S3;
			}
        }
        break;
		
		case IMPORTANT_DATA_OP_S3://多项式系数初始化
		{
			Coefficient_Init();
			if(task_init_complete == TRUE)
			{
				task_init_complete = FALSE;
				tmp_init_stat = EE_INIT_STATE_S1;
				important_data_rd_state=IMPORTANT_DATA_OP_S4;
			}
		}
		break;
		
		case IMPORTANT_DATA_OP_S4://流量系数初始化
		{
			Flow_coef_Init();
			if(task_init_complete == TRUE)
			{
				task_init_complete = FALSE;
				tmp_init_stat = EE_INIT_STATE_S1;
				important_data_rd_state=IMPORTANT_DATA_OP_S5;
			}
		}
		break;
		
		case IMPORTANT_DATA_OP_S5://UDP ip初始化
		{
			UDP_Update_Init();
			if(task_init_complete == TRUE)
			{
				task_init_complete = FALSE;
				tmp_init_stat = EE_INIT_STATE_S1;
				important_data_rd_state=IMPORTANT_DATA_OP_S6;
			}
		}
		break;
		case IMPORTANT_DATA_OP_S6://SOE指针初始化
		{
			SOE_Pint_Init();
			if(task_init_complete == TRUE)
			{
				task_init_complete = FALSE;
				tmp_init_stat = EE_INIT_STATE_S1;
				important_data_rd_state=IMPORTANT_DATA_OP_S7;
			}
		}
		break;
		
		case IMPORTANT_DATA_OP_S7://RTCC初始化
		{
			RtccInitApp();
			if(task_init_complete == TRUE)
			{
				task_init_complete = FALSE;
				tmp_init_stat = EE_INIT_STATE_S1;
				important_data_rd_state=IMPORTANT_DATA_OP_S8;
			}
		}
		break;
		
		case IMPORTANT_DATA_OP_S8://其他参数保存完成,再保存第一次上电标志
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if(FRAME_FIRST_POWER_FLG != Sys_data_object.P1.first_power_flg)//是第一次上电
				{
					Sys_data_object.P1.first_power_flg = FRAME_FIRST_POWER_FLG;//保存第一次上电标志
					EepWrite(&Sys_data_object.P1.first_power_flg, EEP_FRAME_BASE_ADDR, 1, &important_data_rd_msg);
				}
				important_data_rd_state = IMPORTANT_DATA_OP_S9;
			}
		}
		break;

		case IMPORTANT_DATA_OP_S9:
		{
			if(TASK_IDLE == EepIfIdle())
			{
                important_data_rd_state = IMPORTANT_DATA_OP_S0;			
			}
		}
		break;

        default:
        {
            important_data_rd_state = IMPORTANT_DATA_OP_S0;
        }
        break;
    }
}
//读状态机是否空闲
u8 EeReadImportantDataIfIdle(void)
{
    if(TASK_IDLE == EepIfIdle())
    {
        if(IMPORTANT_DATA_OP_S0 == important_data_rd_state)
        {
            return TASK_IDLE;
        }
        else
        {
            return TASK_BUSY;
        }
    }
    else
    {
        return TASK_BUSY;
    }
}
void StartEeReadImportantData(void)
{
    important_data_rd_state = IMPORTANT_DATA_OP_S1;
}
/*******************************************************************************
 * @fun_name:	SysData_Init
 * @brief   :   系统参数初始化函数
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
static void SysData_Init(void)
{
	switch (tmp_init_stat)
	{
		case EE_INIT_STATE_S0:
		{
			break;
		}
		case EE_INIT_STATE_S1://读取第一次上电标志
		{
			if (TASK_IDLE == EepIfIdle())
			{
				EepRead(EEP_FRAME_FIRST_POWER_ADDR, &Sys_data_object.P1.first_power_flg, sizeof(Sys_data_object.P1), &important_data_rd_msg);
				tmp_init_stat = EE_INIT_STATE_S2;
			}
			break;
		}
		case EE_INIT_STATE_S2://判断是否第一次上电
		{
			if (TASK_IDLE == EepIfIdle())
			{
				if(FRAME_FIRST_POWER_FLG != Sys_data_object.P1.first_power_flg)//是第一次上电,其他参数初始化成功再保存第一次上电标志
				{
					Sys_data_object.P3.FirstPowerOnFlag = FIRST_POWER_ON_FLAG;
					FirstPowerOnInit();
					EepWrite(&(Sys_data_object.P1.start_using_flg), (EEP_FRAME_FIRST_POWER_ADDR+1), (sizeof(Sys_data_object.P1)-1), &important_data_rd_msg);
				}
				else
				{
					Sys_data_object.P3.FirstPowerOnFlag = 0;
				}
				Sys_data_object.P3.ProductUpoadFailFlag =0;
				tmp_init_stat = EE_INIT_STATE_S3;
			}
			break;
		}
		case EE_INIT_STATE_S3://重要参数合法判断:反向计量标志
		{
			if (TASK_IDLE == EepIfIdle())
			{
				if((0x2B1A !=Sys_data_object.P1.counter_current_flow.datas) &&(0x4D3C !=Sys_data_object.P1.counter_current_flow.datas))
				{
					Sys_data_object.P1.counter_current_flow.datas = 0x2B1A;
					EepWrite(&Sys_data_object.P1.counter_current_flow.datas_map.data0,EEP_FRAME_COUNTER_CURRENT_FLOW_ADDR, sizeof (Sys_data_object.P1.counter_current_flow.datas), &important_data_rd_msg);
				}
				tmp_init_stat = EE_INIT_STATE_S4;
			}
			break;
		}
		case EE_INIT_STATE_S4://重要参数合法判断:始动流量
		{
			if (TASK_IDLE == EepIfIdle())
			{
				if((Sys_data_object.P1.start_flow.datas < MIN_START_FLOW)||(Sys_data_object.P1.start_flow.datas > MAX_START_FLOW))  
                {
                    Sys_data_object.P1.start_flow.datas = DEFAULT_START_FLOW;//默认值
                    EepWrite(&Sys_data_object.P1.start_flow.datas_map.data0, EEP_FRAME_START_FLOW_ADDR, 2, &important_data_rd_msg);
                }
				tmp_init_stat = EE_INIT_STATE_S5;
			}
			break;
		}
		case EE_INIT_STATE_S5:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				Frame_Para_Init();//系统参数初始化 
				task_init_complete = TRUE;
				tmp_init_stat = EE_INIT_STATE_S0;
			}
			break;
		}
		default:
		{
			tmp_init_stat = EE_INIT_STATE_S0;
			break;
		}
	}
}
/*******************************************************************************
 * @fun_name:	Coefficient_Init
 * @brief   :   多项式系数上电初始化函数
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
static void Coefficient_Init(void)
{
      switch (tmp_init_stat)
      {
        case EE_INIT_STATE_S0:
        {
        }
		break;
		case EE_INIT_STATE_S1:
        {
			if (TASK_IDLE == EepIfIdle())
			{
				if(FRAME_FIRST_POWER_FLG != Sys_data_object.P1.first_power_flg)//是第一次上电
				{
					memcpy(&cal_coefficient[0].flt, default_coefficient,sizeof(default_coefficient));//多项式建模系数初始化
					EepWrite(&cal_coefficient[0].value[0], FRAME_FLOW_COEFFICIENT_ADDR, sizeof (cal_coefficient), &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S3;
				}
				else//非第一次上电
				{
					EepRead(FRAME_FLOW_COEFFICIENT_ADDR, &cal_coefficient[0].value[0], sizeof (cal_coefficient), &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S20;
				}
			} 
        } 
		break;
		case EE_INIT_STATE_S20:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				u8 flow_coef_ok_flg = 0; //多项式系数异常flag
				for(u8 i=0;i<MULT_COEFFICIENT_BUF_NUM;i++)//判断系数合法性，不合法置默认
				{
					if(0xFFFFFFFF==cal_coefficient[i].value_u32)//系数默认为0xFFFFFFFF属于异常
					{
						flow_coef_ok_flg=0;							
						break;
					}
					if((COEFFICIENT_MAX < cal_coefficient[i].flt)||(cal_coefficient[i].flt <COEFFICIENT_MIN))//判断范围有待确认-20191112
					{
						flow_coef_ok_flg=0;							
						break;
					}
					else if(cal_coefficient[i].flt != 0)
					{
						flow_coef_ok_flg=1;//如果系数中有一个不为0。则flag置1
					}
				}
				if(0 == flow_coef_ok_flg)//判断异常系数,则写默认系数
				{
					memcpy(&cal_coefficient[0].flt, default_coefficient,sizeof(default_coefficient));
					EepWrite(&cal_coefficient[0].value[0], FRAME_FLOW_COEFFICIENT_ADDR, sizeof (cal_coefficient), &important_data_rd_msg);
				}
				tmp_init_stat = EE_INIT_STATE_S3;
			}                    
		}
		break;
		case EE_INIT_STATE_S3:
        {
            if (TASK_IDLE == EepIfIdle())
            {
				task_init_complete = TRUE;
                tmp_init_stat = EE_INIT_STATE_S0;
            }
		}
		break;
		default:
		{
			tmp_init_stat = EE_INIT_STATE_S0;
		    break;
		}
      }
}
/*******************************************************************************
 * @fun_name:	Flow_coef_Init
 * @brief   :   流量系数上电初始化函数
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
static void Flow_coef_Init(void)
{
      switch (tmp_init_stat)
      {
        case EE_INIT_STATE_S0:
        {
            break;
        }
		case EE_INIT_STATE_S1://第一次上电流程
        {
			if (TASK_IDLE == EepIfIdle())
			{
				if(FRAME_FIRST_POWER_FLG != Sys_data_object.P1.first_power_flg)//是第一次上电
				{
					memcpy(&(cal_flow_coefficient[0].datas_map.data0), default_flow_coefficient, sizeof (default_flow_coefficient));//流量系数初始化
					EepWrite(&cal_flow_coefficient[0].datas_map.data0, FRAME_FLOW_LOW_COEFFICIENT_ADDR, sizeof (cal_flow_coefficient), &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S3;
				}
				else//非第一次上电
				{
					EepRead(FRAME_FLOW_LOW_COEFFICIENT_ADDR, &cal_flow_coefficient[0].datas_map.data0, sizeof (default_flow_coefficient), &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S20;
				}
			}
            break;
        }
        case EE_INIT_STATE_S20:
        {
            if (TASK_IDLE == EepIfIdle())
            {
                u8 flow_coef_ok_flg = 1;//流量系数异常标志  1：正常为   0：异常为

                for(u8 i=0;i<FLOW_COEFFICIENT_BUF_NUM;i++)//后18个是流量系数的
                {
                    if(0xFFFFFFFF==cal_flow_coefficient[i].datas)//系数默认为0xFFFFFFFF属于异常
                    {
                        flow_coef_ok_flg=0;							
                        break;
                    }
                    if((FLOW_COEFFICIENT_MAX < cal_flow_coefficient[i].datas)||(cal_flow_coefficient[i].datas <FLOW_COEFFICIENT_MIN))//判断范围有待确认-20191112
                    {
                        flow_coef_ok_flg=0;							
                        break;
                    }
                }
                if(0 == flow_coef_ok_flg)//判定流量系数存在异常 若异常了就将默认流量系数写入到EEPROM
                {
                    memcpy(&(cal_flow_coefficient[0].datas_map.data0), default_flow_coefficient, sizeof (default_flow_coefficient));//流量系数初始化
					EepWrite(&cal_flow_coefficient[0].datas_map.data0, FRAME_FLOW_LOW_COEFFICIENT_ADDR, sizeof (cal_flow_coefficient), &important_data_rd_msg);
                } 
				tmp_init_stat = EE_INIT_STATE_S3;					
            }                    
            break;
        }
		case EE_INIT_STATE_S3:
        {
            if (TASK_IDLE == EepIfIdle())
            {
				task_init_complete = TRUE;
                tmp_init_stat = EE_INIT_STATE_S0;
            }
			break;
		}
		default:
		{
			tmp_init_stat = EE_INIT_STATE_S0;
		    break;
		}		
     }
}
/*******************************************************************************
 * @fun_name:	UDP_Update_Init
 * @brief   :   UDP参数上电初始化函数
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
static void UDP_Update_Init(void)
{
	switch (tmp_init_stat)
	{
		case EE_INIT_STATE_S0:
		{
			break;
		}
		case EE_INIT_STATE_S1:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				if(FRAME_FIRST_POWER_FLG != Sys_data_object.P1.first_power_flg)//是第一次上电
				{
					#if PRODUCT_SY
					Sys_data_object.P2.server_ip[0] = 223;
					Sys_data_object.P2.server_ip[1] = 100;
					Sys_data_object.P2.server_ip[2] = 22;
					Sys_data_object.P2.server_ip[3] = 125;
					Sys_data_object.P2.server_port.datas = 3031;
					#else
					Sys_data_object.P2.server_ip[0] = 60;
					Sys_data_object.P2.server_ip[1] = 208;
					Sys_data_object.P2.server_ip[2] = 16;
					Sys_data_object.P2.server_ip[3] = 58;
					Sys_data_object.P2.server_port.datas = 7833;
					#endif
					EepWrite(&Sys_data_object.P2.server_ip[0], EEP_UPDATE_UDP_IP, 6, &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S3;
				}
				else//非第一次上电
				{
					EepRead(EEP_UPDATE_UDP_IP, &Sys_data_object.P2.server_ip[0], 6, &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S3;
				}
			}
			break;			
		}
		case EE_INIT_STATE_S3:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				task_init_complete = TRUE;
				tmp_init_stat = EE_INIT_STATE_S0;				
			}
			break;
		}
		default:
		{
			tmp_init_stat = EE_INIT_STATE_S0;
			break;
		}
	}
}
/*******************************************************************************
 * @fun_name:	SOE_Pint_Init
 * @brief   :   SOE存储指针上电初始化函数
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
static void SOE_Pint_Init(void)
{
	switch (tmp_init_stat)
	{
		case EE_INIT_STATE_S0:
		{
			break;
		}
		case EE_INIT_STATE_S1:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				if(FRAME_FIRST_POWER_FLG != Sys_data_object.P1.first_power_flg)//是第一次上电
				{
					Sys_data_object.P4.Point_SOE_write = 0;
					EepWrite(&Sys_data_object.P4.Point_SOE_write, EEP_ADDR_SOE_WR_POINT, 1, &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S3;
				}
				else//非第一次上电
				{
					EepRead(EEP_ADDR_SOE_WR_POINT, &Sys_data_object.P4.Point_SOE_write, 1, &important_data_rd_msg);
					tmp_init_stat = EE_INIT_STATE_S3;
				}
			}
			break;			
		}
		case EE_INIT_STATE_S3:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				task_init_complete = TRUE;
				tmp_init_stat = EE_INIT_STATE_S0;				
			}
			break;
		}
		default:
		{
			tmp_init_stat = EE_INIT_STATE_S0;
			break;
		}
	}
}
static void RtccInitApp(void)
{
	switch (tmp_init_stat)
	{
		case EE_INIT_STATE_S0:
		{
			break;
		}
		case EE_INIT_STATE_S1:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				EepRead(EE_FIRST_USE_ADDR, Sys_data_object.ee_data.byte, sizeof (RTCC_EE_DATA), &important_data_rd_msg); //读EE永久性数据
				tmp_init_stat = EE_INIT_STATE_S2;
			}
			break;			
		}
		case EE_INIT_STATE_S2:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				 if (Sys_data_object.ee_data.data.first_flag != RTCC_FIRST_FLAG)
                    {
                        //第一次上电
                        if (EepIfIdle())
                        {
                            Sys_data_object.ee_data.data.first_flag = RTCC_FIRST_FLAG; //改写上电标志位
                            Sys_data_object.ee_data.data.work_hour = 0; //清累计工作时间
                            Sys_data_object.ee_data.data.day_data = 0xFFFFFFFF; //结算日累计值
                            RtccGetDateTime(&Sys_data_object.ee_data.data.start_time_up); //记录表计出厂时间
                            EepWrite(Sys_data_object.ee_data.byte, EE_FIRST_USE_ADDR, sizeof (RTCC_EE_DATA), &important_data_rd_msg);
                            tmp_init_stat = EE_INIT_STATE_S3;
                        }
                    }
                    else
                    {							
                        //计算时间差，监控RTCC是否跑飞
						DateTime init_time;
                        if (ERROR == RtccGetDateTime(&init_time)) //上电的时间不合法
                        {
                            init_time.s.year = 0x22;
                            init_time.s.month = 0x01;
                            init_time.s.day = 0x01;
                            init_time.s.hour = 0x00;
                            init_time.s.minute = 0x00;
                            init_time.s.second = 0x00;
                            RtccSetDateTime(&init_time);
                        }
						task_init_complete = TRUE;
						tmp_init_stat = EE_INIT_STATE_S0;
                    }
				
			}
			break;
		}
		case EE_INIT_STATE_S3:
		{
			if (TASK_IDLE == EepIfIdle())
			{
				task_init_complete = TRUE;
				tmp_init_stat = EE_INIT_STATE_S0;				
			}
			break;
		}
		default:
		{
			tmp_init_stat = EE_INIT_STATE_S0;
			break;
		}
	}
}
/*******************************************************************************
 * @fun_name:	Frame_Para_Init
 * @brief   :   系统当前参数刷新
 * @param[in] :
 * @param[out]: u32
 * @retval    : None
 * @other     :
 ******************************************************************************/
void Frame_Para_Init(void)
{
	MET_STATE temp_state; //状态字
	frame_para_buf[FramePara_DNx] = GP30_APP_DNX;//DNx
	frame_para_buf[FramePara_Tc0] = Sys_data_object.P1.temperature_coefficient0.datas;
	frame_para_buf[FramePara_Tc1] = Sys_data_object.P1.temperature_coefficient1.datas;
	frame_para_buf[FramePara_FcSmall] = Sys_data_object.P1.flow_coefficient_small.datas;
	frame_para_buf[FramePara_FcMiddle0] = Sys_data_object.P1.flow_coefficient_middle0.datas;
	frame_para_buf[FramePara_FcMiddle1] = Sys_data_object.P1.flow_coefficient_middle1.datas;
	frame_para_buf[FramePara_FcMiddle2] = Sys_data_object.P1.flow_coefficient_middle2.datas;
	frame_para_buf[FramePara_FcLarge] = Sys_data_object.P1.flow_coefficient_large.datas;
	frame_para_buf[FramePara_ReportStartDMY] = Sys_data_object.P1.report_start_time[0]|(Sys_data_object.P1.report_start_time[1]<<8)|(Sys_data_object.P1.report_start_time[2]<<16);
	frame_para_buf[FramePara_ReportStartSMH] = Sys_data_object.P1.report_start_time[8]|(Sys_data_object.P1.report_start_time[7]<<8)|(Sys_data_object.P1.report_start_time[6]<<16);
	frame_para_buf[FramePara_ProgVersion] = PROGRAME_VERSION;
	frame_para_buf[FramePara_MetAddr0_3] = Sys_data_object.P1.met_addr[0]|(Sys_data_object.P1.met_addr[1]<<8)|(Sys_data_object.P1.met_addr[2]<<16)|(Sys_data_object.P1.met_addr[3]<<24);
	frame_para_buf[FramePara_MetAddr4] = Sys_data_object.P1.met_addr[4]|(Sys_data_object.P1.met_addr[5]<<8)|(Sys_data_object.P1.met_addr[6]<<16);
	frame_para_buf[FramePara_FuncCfg0_3] =  Sys_data_object.P1.func_cfg_bytes[0]|(Sys_data_object.P1.func_cfg_bytes[1]<<8)|(Sys_data_object.P1.func_cfg_bytes[2]<<16)|(Sys_data_object.P1.func_cfg_bytes[3]<<24);
	frame_para_buf[FramePara_FuncCfg4_7] =  Sys_data_object.P1.func_cfg_bytes[4]|(Sys_data_object.P1.func_cfg_bytes[5]<<8)|(Sys_data_object.P1.func_cfg_bytes[6]<<16)|(Sys_data_object.P1.func_cfg_bytes[7]<<24);
	frame_para_buf[FramePara_FuncCfg8_11] = Sys_data_object.P1.func_cfg_bytes[8]|(Sys_data_object.P1.func_cfg_bytes[9]<<8)|(Sys_data_object.P1.func_cfg_bytes[10]<<16)|(Sys_data_object.P1.func_cfg_bytes[11]<<24);
	frame_para_buf[FramePara_FuncCfg12_15]= Sys_data_object.P1.func_cfg_bytes[12]|(Sys_data_object.P1.func_cfg_bytes[13]<<8)|(Sys_data_object.P1.func_cfg_bytes[14]<<16)|(Sys_data_object.P1.func_cfg_bytes[15]<<24);
	frame_para_buf[FramePara_ReportEndSMH] = Sys_data_object.P1.report_start_time[3]|(Sys_data_object.P1.report_start_time[4]<<8)|(Sys_data_object.P1.report_start_time[5]<<16);
	frame_para_buf[FramePara_FcMiddle3] = Sys_data_object.P1.flow_coefficient_middle3.datas;
	temp_state = FrameGetByte();
	frame_para_buf[FramePara_Sta1_2] = temp_state.stat1|(temp_state.stat2<<8);//-------------------------------------------------------------
	frame_para_buf[FramePara_CCFlow] = Sys_data_object.P1.counter_current_flow.datas_map.data0|(Sys_data_object.P1.counter_current_flow.datas_map.data1<<8);
	frame_para_buf[FramePara_StartFlow] = Sys_data_object.P1.start_flow.datas;
	/********************/
	frame_para_buf[FramePara_GP30Freq] = Sys_data_object.P1.sampling_frequency;//----------------------------------------------------------------------
	frame_para_buf[FramePara_GP30BufLen] = Sys_data_object.P1.buf_length;//----------------------------------------------------------------------------
	frame_para_buf[FramePara_ReportFreq] = Sys_data_object.P1.Time_upload_2006.buf[0]|(Sys_data_object.P1.Time_upload_2006.buf[1]<<8)|(Sys_data_object.P1.Time_upload_2006.buf[2]<<16)|(Sys_data_object.P1.Time_upload_2006.buf[3]<<24);
    frame_para_buf[FramePara_EnableTestFunc] = Sys_data_object.P1.enable_verification_function;
	frame_para_buf[FramePara_EnableInfrareFunc] = Sys_data_object.P1.enable_infrared_function;
	frame_para_buf[FramePara_StartUsingFunc] = Sys_data_object.P1.start_using_flg;
	frame_para_buf[FramePara_StoreFrequency] = Sys_data_object.P1.storage_frequency;
	frame_para_buf[FramePara_NbParaRetryDelay] = Sys_data_object.P1.Upload_again_interval_time.datas;
	frame_para_buf[FramePara_NbParaLoginAckTimes] = Sys_data_object.P1.nb_para_login_ask_times.datas;
	frame_para_buf[FramePara_BootVersion] = Meter_GetBootVersion();
	//蓝牙参数更新
	Sys_data_object.P3.function_switch.ble_adv_fre = Sys_data_object.P1.func_cfg_bytes[16];//广播间隔3秒
	Sys_data_object.P3.function_switch.ble_con_para = Sys_data_object.P1.func_cfg_bytes[17];//连接间隔50ms
	Sys_data_object.P3.function_switch.ble_tx_power = Sys_data_object.P1.func_cfg_bytes[18];//发射功率
	Sys_data_object.P3.function_switch.ble_scan_times = Sys_data_object.P1.func_cfg_bytes[19];//扫描次数
	Sys_data_object.P1.ies_pulse_emergency.datas =  Sys_data_object.P1.func_cfg_bytes[20];//应急用水量
	Sys_data_object.P3.function_switch.password_enable = Sys_data_object.P1.func_cfg_bytes[21];//使能密码配对
}
/*******************************************************************************
 * @fun_name:	FrameCheckParaApp
 * @brief   :   查询系统当前参数
 * @param[in] :	parameter Frame_CheckParaTypeDef格式枚举类型
 * @param[out]: u32
 * @retval    : None
 * @other     :
 ******************************************************************************/
u32 FrameCheckParaApp(Frame_CheckParaTypeDef parameter)
{
    return frame_para_buf[parameter];
}

void EeSysMachine(void)
{
    EeReadImportantDataMachine();
    EepMachineDriver();
}

#ifdef __cplusplus
}
#endif		//#ifdef __cplusplus



