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

#ifndef __FAULT_DETECT_H
#define __FAULT_DETECT_H

#include "common_types.h"

#define FAULT_DETECT_TIME_UNIT  APP_TASK_10MS
#define FAULT_DETECT_HISTORY_POWER_TIME  (20u/FAULT_DETECT_TIME_UNIT)

#define FAULT_DETECT_IGN_ON_TIME  (2000u/FAULT_DETECT_TIME_UNIT)
#define FAULT_DETECT_POWER_TIME  (5500u/FAULT_DETECT_TIME_UNIT)

#define FAULT_DETECT_STATE_IDLE  0u
#define FAULT_DETECT_STATE_IGN_ON  1u
#define FAULT_DETECT_STATE_IGN_OFF  2u
#define FAULT_DETECT_STATE_POWER  3u


#define VEHICLE_CFG_CDS_VALUE_DEFAULT 0XFFU
#define VEHICLE_CFG_CDS_VALUE_CF 0X01U
#define VEHICLE_CFG_CDS_VALUE_ADAS 0X03U
#define VEHICLE_CFG_CDS_VALUE_NONE 0XFEU

#define FAULT_DETECT_JUDGE_NODE_LOSS_NOTOK  0u
#define FAULT_DETECT_JUDGE_NODE_LOSS_OK  1u

//0xAA表示busoff了，0x55表示正常
#define FAULT_DETECT_COM_BUSOFF_STATE  0XAAu
#define FAULT_DETECT_COM_NORMAL_STATE  0X55u

typedef struct
{
	u8 cds;
}vehicle_cfg_s;


#define POWER_SUPPLY_HIS_DATA_SIZE 8u
#define POWER_SUPPLY_HIS_MAX 10u
typedef struct
{
	u8 poweroff;
	u8 out30s;
	u8 out30r;
	u8 out15d;
	u8 sigt30s;
	u8 sigt30r;
	u8 sigt15d;
	u8 year;
	u8 month;
	u8 day;
	u8 hour;
	u8 min;
	u8 sec;
}power_supply_history_s;


typedef struct
{
	power_supply_history_s his[POWER_SUPPLY_HIS_MAX];
	u8 count;	//計數
	u8 p_cur;  //當前的指針
}power_supply_history_detect_s;


void fault_detect_input_power_history(const power_supply_history_s *input_power);
void fault_detect_get_power_history(power_supply_history_s *out_power,u8 *p_len);
void fault_detect_init(void);
void fault_detect_task(void);

void vehicle_cfg_set_cds(u8 cds);
u8 vehicle_cfg_get_cds(void);
u8 fault_detect_get_power_state(void);
u8 fault_detect_get_ign_state(void);
u8 fault_detect_judge_node_bcmstate_loss(void);
u8 fault_detect_judge_key_stuck(void);
void fault_detect_iokey_dtc(u8 key_id,u8 event);
void fault_detect_adckey_dtc(u8 key_id,u8 event);


#endif
