/**********************************************************************************
Copyright:  Science&Technology Co., Ltd. 2012-2016. All rights reserved.
File name: power_manage.c
Author: 
ID：
Version: V1.0.0
Date: 2019.09.05
Description:

Others:
History:
**********************************************************************************/
//#include "derivative.h"
#include "system_config.h"
#include "fault_detect.h"
#include "common_memory.h"
//#include "power_drive.h"
#include "crc.h"
#include "key_adc_mid_cfg.h"
#include "Demm.h"
#include "key_digital_driver_cfg.h"
#include "system_voltage_manage.h"

static void fault_detect_vin(void);
static void fault_detect_vehicle_cfginfo(void);
static u8 fault_detect_get_event(u8 event);
static void fault_detect_power_task(void);
static void fault_detect_ign_task(void);
static void fault_detect_his_task(void);

typedef struct
{
	u8 ign_state;
	u8 power_state;
	u16 ign_timecount;
	u16 power_timecount;
	u8 his_power_count;
}fault_detect_s;

static power_supply_history_detect_s g_power_supply_history_detect;
static fault_detect_s	g_fault_detect;
static vehicle_cfg_s g_vehicle_cfg;



/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
void fault_detect_init(void)
{
	#if 0
	common_memory_clear((u8*)&g_power_supply_history_detect,(u16)sizeof(power_supply_history_detect_s));
	common_memory_clear((u8*)&g_fault_detect,(u16)sizeof(fault_detect_s));
	common_memory_clear((u8*)&g_vehicle_cfg,(u16)sizeof(vehicle_cfg_s));
	g_power_supply_history_detect.count = 0;
	g_vehicle_cfg.cds = VEHICLE_CFG_CDS_VALUE_NONE;
	g_fault_detect.power_state=FAULT_DETECT_STATE_IDLE;
	g_fault_detect.ign_state=FAULT_DETECT_STATE_IDLE;
	#endif
}

//寫入一次記錄
void fault_detect_input_power_history(const power_supply_history_s *input_power)
{
	#if 0
	u8 i;
	g_power_supply_history_detect.count++;
	if(g_power_supply_history_detect.count>=POWER_SUPPLY_HIS_MAX)
	{
		g_power_supply_history_detect.count=0;
	}
	i=g_power_supply_history_detect.count;
	common_memory_copys((u8*)&g_power_supply_history_detect.his[i],(u8*)input_power,sizeof(power_supply_history_s));
	#endif
}

//將十次記錄都讀取
void fault_detect_get_power_history(power_supply_history_s *out_power,u8 *p_len)
{
	#if 0
	u8 i,j;
	power_supply_history_s *lp_his;
	//u8 l_buf[POWER_SUPPLY_HIS_MAX];
	*p_len = POWER_SUPPLY_HIS_MAX;
	lp_his = out_power;
	for(i=0;i<POWER_SUPPLY_HIS_MAX;i++)
	{
		if(g_power_supply_history_detect.count >=i)
		{
			j=g_power_supply_history_detect.count-i;
		}
		else
		{
			j=POWER_SUPPLY_HIS_MAX+g_power_supply_history_detect.count-i;
		}
		//l_buf[i] = j;
		common_memory_copys((u8*)lp_his,(const u8 *)&g_power_supply_history_detect.his[j],(u16)sizeof(power_supply_history_s));
		lp_his++;
	}
	#endif
}

static void fault_detect_vin(void)
{
	#if 0
	u8 l_crc;
	u8 l_buf[SYSDID_F190_VIN_CK_LEN];
	//读取vin
	(void)nvm_read_bytes(NVM_TYPE_EEPROM,SYSDID_F190_VIN_ADDR,(u8*)&l_buf[0],SYSDID_F190_VIN_CK_LEN);
	l_crc = crc8_make_by_table(CRC8_ID_FOR_E2E_L2,&l_buf[0],SYSDID_F190_VIN_LEN);
	if(l_crc == l_buf[SYSDID_F190_VIN_LEN])  //校验通过
	{
		dem_set_test_result(DTC_VIN_NOT_WRITE_ID, DTC_TEST_STATE_PASSED);
	}
	else
	{
		//不通过，表示未写入
		dem_set_test_result(DTC_VIN_NOT_WRITE_ID, DTC_TEST_STATE_FAILED);
	}
	#endif
}

static void fault_detect_vehicle_cfginfo(void)
{
	#if 0
	u8 l_crc;
	u8 l_buf[SYSDID_VEHICLE_CONFIG_CK_LEN];
	//读取车辆配置信息
	(void)nvm_read_bytes(NVM_TYPE_EEPROM,SYSDID_VEHICLE_CONFIG_ADDR,(u8*)&l_buf[0],SYSDID_VEHICLE_CONFIG_CK_LEN);
	//cal_read_did(0xF100u,(u8*)&l_buf[0],SYSDID_ATTEMP_LEN+SYSDID_ATTEMP_MAX_LEN);
	l_crc = crc8_make_by_table(CRC8_ID_FOR_E2E_L2,&l_buf[0],SYSDID_VEHICLE_CONFIG_LEN);
	if(l_crc == l_buf[SYSDID_VEHICLE_CONFIG_LEN])  //校验通过
	{
		if( (l_buf[0]==VEHICLE_CFG_CDS_VALUE_DEFAULT) || (l_buf[0]==VEHICLE_CFG_CDS_VALUE_NONE) )
		{
			dem_set_test_result(DTC_VCS_INVALID_ID, DTC_TEST_STATE_FAILED);
		}
		#if 1
		else if( (l_buf[0]==VEHICLE_CFG_CDS_VALUE_ADAS) || (l_buf[0]==VEHICLE_CFG_CDS_VALUE_CF) )
		{
			dem_set_test_result(DTC_VCS_INVALID_ID, DTC_TEST_STATE_PASSED);
		}
		#endif
		else
		{

		}
	}
	else
	{
		//不通过，表示未写入
		dem_set_test_result(DTC_VCS_INVALID_ID, DTC_TEST_STATE_FAILED);
	}
	#endif
}



static void fault_detect_power_task(void)
{
	#if 0
	switch(g_fault_detect.power_state)
	{
		case FAULT_DETECT_STATE_IDLE:
			g_fault_detect.power_timecount++;
			if(g_fault_detect.power_timecount>=FAULT_DETECT_POWER_TIME)
			{
				g_fault_detect.power_timecount=0;
				g_fault_detect.power_state = FAULT_DETECT_STATE_POWER;
			}
		break;
		case FAULT_DETECT_STATE_POWER:
		break;
		default:
			//为了 default而加，未验证
		break;
	}
	#endif
}

static void fault_detect_ign_task(void)
{
	#if 0
	switch(g_fault_detect.ign_state)
	{
		case FAULT_DETECT_STATE_IDLE:
			if(can_app_get_bcm_t15d()!=0u)
			{
				g_fault_detect.ign_timecount++;
				if(g_fault_detect.ign_timecount>=FAULT_DETECT_IGN_ON_TIME)
				{
					g_fault_detect.ign_timecount=0;
					fault_detect_vin();
					fault_detect_vehicle_cfginfo();
					g_fault_detect.ign_state = FAULT_DETECT_STATE_IGN_ON;
				}
			}
			else
			{
				g_fault_detect.ign_timecount=0u;
			}
		break;
		case FAULT_DETECT_STATE_IGN_ON:
			if(can_app_get_bcm_t15d()!=0u)
			{

			}
			else
			{
				g_fault_detect.ign_timecount=0;
				g_fault_detect.ign_state = FAULT_DETECT_STATE_IGN_OFF;
			}
		break;
		case FAULT_DETECT_STATE_IGN_OFF:
			if(can_app_get_bcm_t15d()!=0u)
			{
				g_fault_detect.ign_timecount++;
				if(g_fault_detect.ign_timecount>=FAULT_DETECT_IGN_ON_TIME)
				{
					g_fault_detect.ign_timecount=0;
					g_fault_detect.ign_state = FAULT_DETECT_STATE_IGN_ON;
				}
			}
			else
			{
				g_fault_detect.ign_timecount=0;
				g_fault_detect.ign_state = FAULT_DETECT_STATE_IGN_OFF;
			}
		break;
		default:
			//为了 default而加，未验证
		break;
	}
}


static void fault_detect_his_task(void)
{
	app_com_rx_tbox_time_s l_tbox_time;
	power_supply_history_s l_power_his;
	//对历史电源状态的记录---以最小的周期帧为基准
	g_fault_detect.his_power_count++;
	if(g_fault_detect.his_power_count>=FAULT_DETECT_HISTORY_POWER_TIME)
	{
		g_fault_detect.his_power_count=0u;
		(void)can_app_get_sig_tbox_time(&l_tbox_time);
		//power his
		l_power_his.poweroff = 0u;
		l_power_his.out30s = 0u;
		l_power_his.out30r = 0u;
		l_power_his.out15d = 0u;
		l_power_his.sigt30s = can_app_get_bcm_t30s();
		l_power_his.sigt30r = can_app_get_bcm_t30r();
		l_power_his.sigt15d = can_app_get_bcm_t15d();
		l_power_his.year = l_tbox_time.year;
		l_power_his.month = l_tbox_time.month;
		l_power_his.day = l_tbox_time.day;
		l_power_his.hour = l_tbox_time.hour;
		l_power_his.min = l_tbox_time.min;
		l_power_his.sec = l_tbox_time.sec;
		fault_detect_input_power_history(&l_power_his);
	}
	else
	{
		//code
	}
	#endif
}

/**
 * @brief  放在10ms定時器
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
void fault_detect_task(void)
{
	#if 0
	fault_detect_ign_task();
	fault_detect_power_task();
	fault_detect_his_task();
	#endif
}



/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
void vehicle_cfg_set_cds(u8 cds)
{
	g_vehicle_cfg.cds = cds;
}

/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
u8 vehicle_cfg_get_cds(void)
{
	return g_vehicle_cfg.cds;
}

/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
u8 fault_detect_get_power_state(void)
{
	return g_fault_detect.power_state;
}

/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
u8 fault_detect_get_ign_state(void)
{
	return g_fault_detect.ign_state;
}



u8 fault_detect_judge_node_bcmstate_loss(void)
{
	#if 0
	u8 l_result=FAULT_DETECT_JUDGE_NODE_LOSS_OK;
	u8 l_busoff_state ;
	l_busoff_state = net_app_get_busoff();  /*PRQA S 3426*/ 
	if((system_voltage_manage_mode_get(POWER_GRADE_ID_DIAG)==POWER_GRADE_FUNC_NORMAL)&&(l_busoff_state==0u))
	{
		l_result=FAULT_DETECT_JUDGE_NODE_LOSS_OK;
	}
	else
	{
		l_result=FAULT_DETECT_JUDGE_NODE_LOSS_NOTOK;
	}
	return l_result;
	#else
	return 0u;
	#endif
}





u8 fault_detect_judge_key_stuck(void)
{
	#if 0
	u8 l_result=FAULT_DETECT_JUDGE_NODE_LOSS_OK;

	if((system_voltage_manage_mode_get(DIG_SYSTEM_VOLTAGE_MANAGE_ID)==POWER_GRADE_FUNC_NORMAL)  \
		&& (fault_detect_get_ign_state()==FAULT_DETECT_STATE_IGN_ON)  \
		)
	{
		l_result=FAULT_DETECT_JUDGE_NODE_LOSS_OK;
	}
	else
	{
		l_result=FAULT_DETECT_JUDGE_NODE_LOSS_NOTOK;
	}
	return l_result;
	#else
	return 0u;
	#endif
}

static u8 fault_detect_get_event(u8 event)
{
	#if 0
	u8 l_event;
	if(event==DTC_TEST_STATE_FAILED)
	{
		l_event=DTC_TEST_STATE_FAILED;
	}
	else if(event==DTC_TEST_STATE_PASSED)
	{
		l_event=DTC_TEST_STATE_PASSED;
	}
	else
	{
		l_event=DTC_TEST_STATE_PASSED;
	}
	return l_event;
	#else
	return 0u;
	#endif
}


void fault_detect_iokey_dtc(u8 key_id,u8 event)
{
	#if 0
	u8 l_event;
	u8 l_result;
	l_result = fault_detect_judge_key_stuck();
	if(l_result ==FAULT_DETECT_JUDGE_NODE_LOSS_OK)
	{
		l_event=fault_detect_get_event(event);
		switch(key_id)
		{
			case KEY_ID_GAP1:
				dem_set_test_result(DTC_DSPA_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_GAP2:
				dem_set_test_result(DTC_DSPD_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_ACC:
				dem_set_test_result(DTC_ACC_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_CANCEL:
				dem_set_test_result(DTC_CANCEL_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_RES1:
				dem_set_test_result(DTC_SPDA_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_SET2:
				dem_set_test_result(DTC_SPDD_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_UP:
				dem_set_test_result(DTC_UP_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ID_DOWN:
				dem_set_test_result(DTC_DOWN_BUTTON_STUCK_ID, l_event);
			break;
			default:
				//为了 default而加，未验证
			break;
		}
	}
	else
	{
		//
	}
	#endif
}


void fault_detect_adckey_dtc(u8 key_id,u8 event)
{
	#if 0
	u8 l_event;
	u8 l_result;
	l_result = fault_detect_judge_key_stuck();
	if(l_result ==FAULT_DETECT_JUDGE_NODE_LOSS_OK)
	{
		l_event=fault_detect_get_event(event);
		switch(key_id)
		{
			case KEY_ADC_ID_PHONE:
				dem_set_test_result(DTC_BT_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_SOUNDCTRL:
				dem_set_test_result(DTC_SOUND_CTRL_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_VOL_INC:
				dem_set_test_result(DTC_VOLA_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_VOL_DEC:
				dem_set_test_result(DTC_VOLD_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_PREVIOUS:
				dem_set_test_result(DTC_PREV_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_NEXT:
				dem_set_test_result(DTC_NEXT_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_MODE:
				dem_set_test_result(DTC_MODE_BUTTON_STUCK_ID, l_event);
			break;
			case KEY_ADC_ID_CUSTOM:
				dem_set_test_result(DTC_CUSTOM_BUTTON_STUCK_ID, l_event);
			break;
			default:
				//为了 default而加，未验证
			break;
		}
	}
	else
	{
		//
	}
	#endif
}


/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
void fault_detect_goto_sleep(void)
{

}


/**
 * @brief
 * @param  input:
 * @param  output:
 * @return   none:
 * @calls
 * @calls by
 * @others
 */
void fault_detect_goto_wakeup(void)
{

}

