/************************************************************
	|	文件名: DCM_DTC.c
	|	  描述：DTC相关服务函数 
	|	  作者：孙开瑞
	|	 
	| 版权声明
***********************************************************/
//DCM_DTC.c
#include "BranchTable.h"
#include "Dcm_App_Cfg.h"
#include "Dcm_App.h"
#include "can_drv.h"
//#include "can_task.h"
//#include "eel.h"
//#include "r_fdl_types.h"
#include "Dcm_extern.h"
#include "pfdl_user.h"

const TsDcm_h_DtcGroupType CaDcm_u_DtcTable[DCM_SUPDTC_COUNT]={
	{ 0,0xF0,0x06,0x16},
	{ 1,0xF0,0x06,0x17},
	{ 2,0x91,0x81,0x12},
	{ 3,0x91,0x81,0x14},
	{ 4,0x91,0x81,0x71},
	{ 5,0x91,0x83,0x12},
	{ 6,0x91,0x83,0x14},
	{ 7,0x91,0x83,0x71},
	{ 8,0x91,0x84,0x12},
	{ 9,0x91,0x84,0x14},
	{10,0x91,0x84,0x71},
	{11,0x91,0x86,0x11},
	{12,0x91,0x86,0x14},
	{13,0xC0,0x73,0x00},
};

DtcStutas_T DtcStutas[DCM_SUPDTC_COUNT];	// DTC 状态数组 
static uint16 tDiagStart;						//上电复位后网络诊断延迟时间 
uint16 tDiagStartVolt;					// 电源电压异常恢复后诊断延迟时间 

/********************************************************/

void DtcStutasInit(void)
{
	uint8 DtcIndex,checksum;
	uint8 LastConfirmedDTC[42];	
	
	DF_Read_All_DTC(LastConfirmedDTC,GetDataFlashAddress(INDEX_19_BASE),42);		
	
	for(DtcIndex=0;DtcIndex<DCM_SUPDTC_COUNT;DtcIndex++)
	{
		checksum = (LastConfirmedDTC[DtcIndex*3+1]+LastConfirmedDTC[DtcIndex*3])^0xFF + 1;
		if(LastConfirmedDTC[DtcIndex*3] != 0xFF && checksum == LastConfirmedDTC[DtcIndex*3+2])
		{
			DtcStutas[DtcIndex].DtcStutas = LastConfirmedDTC[DtcIndex*3];
			if(0!=(LastConfirmedDTC[DtcIndex*3] & ConfirmedDTC)){
				LastConfirmedDTC[DtcIndex*3+1]++;
				LastConfirmedDTC[DtcIndex*3+2] = (LastConfirmedDTC[DtcIndex*3+1]+LastConfirmedDTC[DtcIndex*3])^0xFF + 1;
				if(40<=LastConfirmedDTC[DtcIndex*3+1]){
					LastConfirmedDTC[DtcIndex*3] = 0;
					LastConfirmedDTC[DtcIndex*3+1] = 0xFF;
					LastConfirmedDTC[DtcIndex*3+2] = (LastConfirmedDTC[DtcIndex*3+1]+LastConfirmedDTC[DtcIndex*3])^0xFF + 1;
					ClearDtcStutas(DtcIndex);	
					DtcStutas[DtcIndex].EepWrtEnable = 1;				
				}
				else{
					DtcStutas[DtcIndex].EepWrtEnable = 0;
				}
			}
			else{
				DtcStutas[DtcIndex].EepWrtEnable = 1;				
			}
		}		
		else{
			DtcStutas[DtcIndex].EepWrtEnable = 1;	
		}		
	}
	
	DF_Write_All_DTC(LastConfirmedDTC,GetDataFlashAddress(INDEX_19_BASE),42);
}

void DtcInit(void)
{
	
	tDiagStart = 1500;
	tDiagStartVolt = 500;
	DtcStutasInit();
}

void DtcDiagConditionChk(void)
{
	if(tDiagStart > 0)
		tDiagStart --;
	if(GetIgnVoltStatus() == 0)
	{
		if(tDiagStartVolt > 0)
			tDiagStartVolt--;
	}
	else
		tDiagStartVolt = 500;
}

static bool1 ChkDiagVolt(void)
{
	return (tDiagStartVolt == 0);
}

static bool1 ChkNetDiagCondition(void)
{
	return ((tDiagStart == 0)&&(tDiagStartVolt == 0));
}

static uint8 GetLastSnapshotRecordNum(uint8 DtcIndex)
{	
	uint8 temp,SnapshotRecordNum = 0;
	uint32	DtcSnapshotRecord[32]; 
	DF_Read(DtcSnapshotRecord,DtcIndex<<1,128);
	for(temp=0;temp<=7;temp++)
	{
		if(DtcSnapshotRecord[(temp<<2)+3] != 0xFFFFFFFF)
			SnapshotRecordNum |= 1<<temp;
	}
	switch(SnapshotRecordNum)
	{
		case 1:
		case 0xF1:return 0;
		case 3:
		case 0xF3:return 1;
		case 7:
		case 0xF7:return 2;
		case 15:return 3;
		case 31:return 4;
		case 63:return 5;
		case 127:return 6;
		case 255:return 7;
		default:return 7;
	}
}

#if 0
void GetSnapshotRecord(uint8 * Data)
{
	uint16 Temp16;
	(void *) Data;
	Temp16 = GetModeFeedbackVoltage();
	*Data++ = (uint8)(Temp16>>8);
	*Data++ = (uint8)Temp16&0xFF;
	Temp16 = GetRecFeedbackVoltage();
	*Data++ = (uint8)(Temp16>>8);
	*Data++ = (uint8)Temp16&0xFF;
	Temp16 = EvaSensorFeedbackVoltage();
	*Data++ = (uint8)(Temp16>>8);
	*Data++ = (uint8)Temp16&0xFF;	
	Temp16 = GetPtcTmpSnsVolt();
	*Data++ = (uint8)(Temp16>>8);
	*Data++ = (uint8)Temp16&0xFF;
	Temp16 = GetFanFeedBackVolt();
	*Data++ = (uint8)(Temp16>>8);
	*Data++ = (uint8)Temp16&0xFF;
	Temp16 = GetCompSnsVolt();
	*Data++ = (uint8)(Temp16>>8);
	*Data++ = (uint8)Temp16&0xFF;	
}
#endif

static void WriteDtcSnapshot(uint8 DtcIndex)
{
	uint8 temp[2];
#if (defined (DTC_ONE_SNAPSHOT) && (DTC_ONE_SNAPSHOT == NO))
	uint32	DtcSnapshotRecord[32]; 
#else
	uint32	DtcSnapshotRecord[4];
#endif
	DtcStutas[DtcIndex].DtcStutas |= (TestFailed | ConfirmedDTC);	
//	DtcStutas[DtcIndex].DtcStutas &= ~TestNotCompletedThisOperationCycle;	//2017/7/2 11:52:01 spark del
	if(DtcStutas[DtcIndex].EepWrtEnable)
	{	
		DtcStutas[DtcIndex].EepWrtEnable = 0;
		DF_Read(temp,(DtcIndex+INDEX_19_BASE),3);
		temp[0] = DtcStutas[DtcIndex].DtcStutas & ConfirmedDTC;
		DF_Write(&temp,(DtcIndex+INDEX_19_BASE),3);
	}
}

void ClearDtcStutas(uint8 DtcIndex)
{
//	DtcStutas[DtcIndex].EepWrtEnable = 1;		//2017/7/4 15:19:34 spark del 
//	DtcStutas[DtcIndex].DtcStutas &= ~(TestNotCompletedThisOperationCycle | TestFailed); //2017/7/2 11:52:29 del
	DtcStutas[DtcIndex].DtcStutas &= ~TestFailed;	//2017/7/2 11:52:56 spark add
}

static void CanDTC_AcBusOffDetected(void)
{
//	uint8 temp;
//	uint32	DtcSnapshotRecord[32]; 
	if(ChkNetDiagCondition() == 0) return;
		
	if(0!=GetCanBusOffErr())	{
		WriteDtcSnapshot(AC_BusOff);
	}
	else{
		ClearDtcStutas(AC_BusOff);
	}
		
#if 0
	if((BusOffCnt < 10) && (BusOffCnt > 0))
		DtcStutas[AC_BusOff].DtcStutas |= TestNotCompletedThisOperationCycle;
	else if(BusOffCnt >= 10)
	{
		if(DtcStutas[AC_BusOff].EepWrtEnable)
		{		
			WriteDtcSnapshot(AC_BusOff);
		}
	}
	else if(BusOffCnt == 0)
	{
		ClearDtcStutas(AC_BusOff);
	}
#endif
}
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
static void PowerVoltageChk(void)
{
	static uint8 PowerOverVoltCnt,PowerUderVoltCnt;
	if(GetVoltage(KL30_VOLTAGE) > 160)
	{
		if(PowerOverVoltCnt < 50)			/* 50*40ms = 2s */
			PowerOverVoltCnt++;
		else
			WriteDtcSnapshot(AC_OVER_VOLTAGE);
	}
	else
	{
		if(PowerOverVoltCnt > 0){
			PowerOverVoltCnt--;
			if(PowerOverVoltCnt>37){		/* 37*40ms = 1.48s */
				PowerOverVoltCnt = 37;
			}
		}
		else{
			ClearDtcStutas(AC_OVER_VOLTAGE);
		}
	}
	
	if(GetVoltage(KL30_VOLTAGE) < 90)
	{
		if(PowerUderVoltCnt < 50)			/* 50*40ms = 2s */
			PowerUderVoltCnt++;
		else
			WriteDtcSnapshot(AC_LOW_VOLTAGE);
	}
	else
	{
		if(PowerUderVoltCnt > 0){
			PowerUderVoltCnt--;
			if(PowerUderVoltCnt>37){		/* 37*40ms = 1.48s */
				PowerUderVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_LOW_VOLTAGE);
	}
}

/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
static void TempMotorFeedbackChk(void)
{
	static uint8 TempOverVoltCnt,TempUderVoltCnt;
	if(GetVoltage(TEMP_MOTOR) >= 49)
	{
		if(TempOverVoltCnt < 50)			/* 50*40ms = 2s */
			TempOverVoltCnt++;
		else
			WriteDtcSnapshot(AC_TEMP_MOTOR_SHORT_CUT_TO_VCC);
	}
	else
	{
		if(TempOverVoltCnt > 0){
			TempOverVoltCnt--;
			if(TempOverVoltCnt>37){		/* 37*40ms = 1.48s */
				TempOverVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_TEMP_MOTOR_SHORT_CUT_TO_VCC);
	}
	
	if(GetVoltage(TEMP_MOTOR) <= 1)
	{
		if(TempUderVoltCnt < 50)			/* 50*40ms = 2s */
			TempUderVoltCnt++;
		else
			WriteDtcSnapshot(AC_TEMP_MOTOR_SHORT_CUT_TO_GND);
	}
	else
	{
		if(TempUderVoltCnt > 0){
			TempUderVoltCnt--;
			if(TempUderVoltCnt>37){		/* 37*40ms = 1.48s */
				TempUderVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_TEMP_MOTOR_SHORT_CUT_TO_GND);
	}
}

/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
static void ModeMotorFeedbackChk(void)
{
	static uint8 ModeOverVoltCnt,ModeUderVoltCnt;
	if(GetVoltage(MODE_MOTOR) >= 49)
	{
		if(ModeOverVoltCnt < 50)			/* 50*40ms = 2s */
			ModeOverVoltCnt++;
		else
			WriteDtcSnapshot(AC_MODE_MOTOR_SHORT_CUT_TO_VCC);
	}
	else
	{
		if(ModeOverVoltCnt > 0){
			ModeOverVoltCnt--;
			if(ModeOverVoltCnt>37){		/* 37*40ms = 1.48s */
				ModeOverVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_MODE_MOTOR_SHORT_CUT_TO_VCC);
	}
	
	if(GetVoltage(MODE_MOTOR) <= 1)
	{
		if(ModeUderVoltCnt < 50)			/* 50*40ms = 2s */
			ModeUderVoltCnt++;
		else
			WriteDtcSnapshot(AC_MODE_MOTOR_SHORT_CUT_TO_GND);
	}
	else
	{
		if(ModeUderVoltCnt > 0){
			ModeUderVoltCnt--;
			if(ModeUderVoltCnt>37){		/* 37*40ms = 1.48s */
				ModeUderVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_MODE_MOTOR_SHORT_CUT_TO_GND);
	}
}

/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
static void RecMotorFeedbackChk(void)
{
	static uint8 RecOverVoltCnt,RecUderVoltCnt;
	if(GetVoltage(REC_MOTOR) >= 49)
	{
		if(RecOverVoltCnt < 50)			/* 50*40ms = 2s */
			RecOverVoltCnt++;
		else
			WriteDtcSnapshot(AC_REC_MOTOR_SHORT_CUT_TO_VCC);
	}
	else
	{
		if(RecOverVoltCnt > 0){
			RecOverVoltCnt--;
			if(RecOverVoltCnt>37){		/* 37*40ms = 1.48s */
				RecOverVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_REC_MOTOR_SHORT_CUT_TO_VCC);
	}
	
	if(GetVoltage(REC_MOTOR) <= 1)
	{
		if(RecUderVoltCnt < 50)			/* 50*40ms = 2s */
			RecUderVoltCnt++;
		else
			WriteDtcSnapshot(AC_REC_MOTOR_SHORT_CUT_TO_GND);
	}
	else
	{
		if(RecUderVoltCnt > 0){
			RecUderVoltCnt--;
			if(RecUderVoltCnt>37){		/* 37*40ms = 1.48s */
				RecUderVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_REC_MOTOR_SHORT_CUT_TO_GND);
	}
}

/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
static void EvaSensorChk(void)
{
	static uint8 EvaOverVoltCnt,EvaUderVoltCnt;
	if(GetVoltage(EVA_SENSOR) >= 49)
	{
		if(EvaOverVoltCnt < 50)			/* 50*40ms = 2s */
			EvaOverVoltCnt++;
		else
			WriteDtcSnapshot(AC_EVA_SENSOR_SHORT_CUT_TO_VCC);
	}
	else
	{
		if(EvaOverVoltCnt > 0){
			EvaOverVoltCnt--;
			if(EvaOverVoltCnt>37){		/* 37*40ms = 1.48s */
				EvaOverVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_EVA_SENSOR_SHORT_CUT_TO_VCC);
	}
	
	if(GetVoltage(EVA_SENSOR) <= 1)
	{
		if(EvaUderVoltCnt < 50)			/* 50*40ms = 2s */
			EvaUderVoltCnt++;
		else
			WriteDtcSnapshot(AC_EVA_SENSOR_SHORT_CUT_TO_GND);
	}
	else
	{
		if(EvaUderVoltCnt > 0){
			EvaUderVoltCnt--;
			if(EvaUderVoltCnt>37){		/* 37*40ms = 1.48s */
				EvaUderVoltCnt = 37;
			}
		}
		else
			ClearDtcStutas(AC_EVA_SENSOR_SHORT_CUT_TO_GND);
	}
}

/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
/**************************************************************************************/
static void MotorLockChk(void)
{
	if(GetMotorLockStatus(TEMP_MOTOR) != 0)
	{
		WriteDtcSnapshot(AC_TEMP_MOTOR_LOCK);
	}
	else
	{
		ClearDtcStutas(AC_TEMP_MOTOR_LOCK);
	}
	
	if(GetMotorLockStatus(MODE_MOTOR) != 0)
	{
		WriteDtcSnapshot(AC_MODE_MOTOR_LOCK);
	}
	else
	{
		ClearDtcStutas(AC_MODE_MOTOR_LOCK);
	}
	
	if(GetMotorLockStatus(REC_MOTOR) != 0)
	{
		WriteDtcSnapshot(AC_REC_MOTOR_LOCK);
	}
	else
	{
		ClearDtcStutas(AC_REC_MOTOR_LOCK);
	}
}

static void CanDTC_CycChk(void)
{
	PowerVoltageChk();
	TempMotorFeedbackChk();
	ModeMotorFeedbackChk();
	RecMotorFeedbackChk();
	EvaSensorChk();
}

extern uint8 GetNormalMsgSts(void);
void CanDTC_Task(void)
{
	static uint8 DtcTestCycleTim;
	//if(ChkDiagVolt()==0) return;
	
	CanDTC_AcBusOffDetected();	
	if(GetNormalMsgSts() & 0x02)
	{
		//CanDTC_CanCommunicationLostDetected(HMI_ControlCommand_ID);
		//CanDTC_CanCommunicationLostDetected(VCU_DisplayInfo_ID);
		//CanDTC_CanCommunicationLostDetected(COMP_Information_ID);
		//CanDTC_CompErrChk();
		//CanDTC_MotorRunTimeChk();
		
		MotorLockChk();
	
		if(DtcTestCycleTim < DTC_TEST_CYCLE_TIM)
		{
			DtcTestCycleTim++;
		}
		else{
			DtcTestCycleTim = 0;
			CanDTC_CycChk();
		}
	}
}

uint8 GetDtcStutas(uint8 DtcIndex)
{
return DtcStutas[DtcIndex].DtcStutas;
}

uint8 GetDtcSnapshotRecord(uint8 Index,uint8 * Buf,uint8 * Length ,uint8 DtcSnapshotRecordNum)
{
	uint8 SnapshotRecordBuf[16];
	DF_Read(SnapshotRecordBuf,Index,16);
	if((DtcSnapshotRecordNum >= 1)&&(DtcSnapshotRecordNum <=6))
	{
		*Buf++ = DtcSnapshotRecordNum;
		*Buf++ = 1;
		*Buf++ = 0x0A;
		*Buf++ = DtcSnapshotRecordNum - 1;
		*Buf++ = SnapshotRecordBuf[(DtcSnapshotRecordNum - 1)<<1];
		*Buf++ =  SnapshotRecordBuf[((DtcSnapshotRecordNum - 1)<<1)+1];
		*Length += 6;
	}
	else if(DtcSnapshotRecordNum == 0xFF)
	{
		for(DtcSnapshotRecordNum = 1;DtcSnapshotRecordNum<=6;DtcSnapshotRecordNum++)
		{
			*Buf++ = DtcSnapshotRecordNum;
			*Buf++ = 1;
			*Buf++ = 0x0A;
			*Buf++ = DtcSnapshotRecordNum - 1;
			*Buf++ = SnapshotRecordBuf[(DtcSnapshotRecordNum - 1)<<1];
			*Buf++ =  SnapshotRecordBuf[((DtcSnapshotRecordNum - 1)<<1)+1];
			*Length += 6;
		}
	}
	else
		return DCM_NORESULT;
	
	return 0;
}

// 19 01
uint8 MngDcm_ReportNumberOfDTCByStatusMask(void)
{
	uint8 LaDcm_u_TempBuffer[6];	
	TeDcm_h_ResultType LeDcm_u_Result;
	uint8 DtcCnt,Index;
	
	VeDcm_h_RqustInfo.StateMask = VeDcm_P_DataAddress[2];
	if(VeDcm_h_RqustInfo.StateMask & DTC_STATUS_AVAILABILITY_MASK)
	{
		LaDcm_u_TempBuffer[0] = VeDcm_h_RqustInfo.Sid + 0x40;
		LaDcm_u_TempBuffer[1] = VeDcm_h_RqustInfo.Sub;
		LaDcm_u_TempBuffer[2] = DTC_STATUS_AVAILABILITY_MASK;
		for(Index=0;Index<DCM_SUPDTC_COUNT;Index++)
		{
			if(GetDtcStutas(Index) & VeDcm_h_RqustInfo.StateMask)
				DtcCnt++;
		}
		LaDcm_u_TempBuffer[3] = ISO_14229_1_DTC_FORMAT;
		LaDcm_u_TempBuffer[4] = 0;
		LaDcm_u_TempBuffer[5] = DtcCnt;
		SetCanTp_TxBuffer(6U,LaDcm_u_TempBuffer);      /*调用接口发送响应数据*/
		LeDcm_u_Result = CeDcm_e_ResultPosRsp;
	}
	else
	{
		/*设置NRC31*/
        MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC31]);    /*整理负响应数据*/
        SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
        LeDcm_u_Result = CeDcm_e_ResultNegRsp;
	}
	return LeDcm_u_Result;
}


// 19 02
uint8 MngDcm_ReportDTCByStatusMask(void)
{
	uint8 LaDcm_u_TempBuffer[DCM_TXBUFFER_LENGTH];	
	TeDcm_h_ResultType LeDcm_u_Result;
	uint8 DtcIndex;
	uint8 DtcReporLength;	
	
	VeDcm_h_RqustInfo.StateMask = VeDcm_P_DataAddress[2];
	if(VeDcm_h_RqustInfo.StateMask & DTC_STATUS_AVAILABILITY_MASK)
	{
		LaDcm_u_TempBuffer[0] = VeDcm_h_RqustInfo.Sid + 0x40;
		LaDcm_u_TempBuffer[1] = VeDcm_h_RqustInfo.Sub;
		LaDcm_u_TempBuffer[2] = DTC_STATUS_AVAILABILITY_MASK;
		DtcReporLength = 3;
		for(DtcIndex=0;DtcIndex<DCM_SUPDTC_COUNT;DtcIndex++)
		{
			if(GetDtcStutas(DtcIndex) & VeDcm_h_RqustInfo.StateMask)
			{
				LaDcm_u_TempBuffer[DtcReporLength++] = CaDcm_u_DtcTable[DtcIndex].DtcHigh;
				LaDcm_u_TempBuffer[DtcReporLength++] = CaDcm_u_DtcTable[DtcIndex].DtcMiddle;
				LaDcm_u_TempBuffer[DtcReporLength++] = CaDcm_u_DtcTable[DtcIndex].DtcLow;
				LaDcm_u_TempBuffer[DtcReporLength++] = GetDtcStutas(DtcIndex);
			}
		}
		SetCanTp_TxBuffer(DtcReporLength,LaDcm_u_TempBuffer);      /*调用接口发送响应数据*/
		LeDcm_u_Result = CeDcm_e_ResultPosRsp;
	}
	else
	{
		/*设置NRC31*/
        MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC31]);    /*整理负响应数据*/
        SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
        LeDcm_u_Result = CeDcm_e_ResultNegRsp;
	}
	return LeDcm_u_Result;
}

#if 0
// 19 04
uint8 MngDcm_ReportDTCSnapshotRecordByDTCNumber(void)
{	
	uint8 LaDcm_u_TempBuffer[DCM_TXBUFFER_LENGTH];
	TeDcm_h_ResultType LeDcm_u_Result;
	uint8 DtcIndex;
	uint8 DtcSnapshotRecordNum;
	uint8 DtcReporLength = 0;	
	
	DtcSnapshotRecordNum = VeDcm_P_DataAddress[5];
	for(DtcIndex=0;DtcIndex<DCM_SUPDTC_COUNT;DtcIndex++)
	{
		if((CaDcm_u_DtcTable[DtcIndex].DtcHigh == VeDcm_P_DataAddress[2]) && (CaDcm_u_DtcTable[DtcIndex].DtcMiddle == VeDcm_P_DataAddress[3]) && (CaDcm_u_DtcTable[DtcIndex].DtcLow == VeDcm_P_DataAddress[4]))
		{
			LaDcm_u_TempBuffer[0] = VeDcm_h_RqustInfo.Sid + 0x40;
			LaDcm_u_TempBuffer[1] = VeDcm_h_RqustInfo.Sub;
			LaDcm_u_TempBuffer[2] = VeDcm_P_DataAddress[2];
			LaDcm_u_TempBuffer[3] = VeDcm_P_DataAddress[3];
			LaDcm_u_TempBuffer[4] = VeDcm_P_DataAddress[4];
			LaDcm_u_TempBuffer[5] = GetDtcStutas(DtcIndex);
			DtcReporLength = 6;
			if(GetDtcSnapshotRecord(DtcIndex,LaDcm_u_TempBuffer+6,&DtcReporLength,DtcSnapshotRecordNum) != DCM_NORESULT)
			{
				SetCanTp_TxBuffer(DtcReporLength,LaDcm_u_TempBuffer);      /*调用接口发送响应数据*/
				LeDcm_u_Result = CeDcm_e_ResultPosRsp;
			}
			else
			{
				/*设置NRC31*/
				MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC31]);    /*整理负响应数据*/
				SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
				LeDcm_u_Result = CeDcm_e_ResultNegRsp;
			}
			break;
		}
	}
	if((DtcIndex == DCM_SUPDTC_COUNT) && (DtcReporLength == 0))
	{
		/*设置NRC31*/
		MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC31]);    /*整理负响应数据*/
		SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
		LeDcm_u_Result = CeDcm_e_ResultNegRsp;
	}
	
	return LeDcm_u_Result;
}
// 19 06
uint8 MngDcm_ReportDTCExtendedDataRecordByDTCNumber(void)
{
	uint8 LaDcm_u_TempBuffer[DCM_TXBUFFER_LENGTH];
	TeDcm_h_ResultType LeDcm_u_Result;
	uint8 DtcIndex;
	uint8 DtcReporLength;	
	
	for(DtcIndex=0;DtcIndex<DCM_SUPDTC_COUNT;DtcIndex++)
	{
		if((CaDcm_u_DtcTable[DtcIndex].DtcHigh == VeDcm_P_DataAddress[2]) && (CaDcm_u_DtcTable[DtcIndex].DtcMiddle == VeDcm_P_DataAddress[3]) && (CaDcm_u_DtcTable[DtcIndex].DtcLow == VeDcm_P_DataAddress[4]))
		{
			LaDcm_u_TempBuffer[0] = VeDcm_h_RqustInfo.Sid + 0x40;
			LaDcm_u_TempBuffer[1] = VeDcm_h_RqustInfo.Sub;
			LaDcm_u_TempBuffer[2] = VeDcm_P_DataAddress[3];
			LaDcm_u_TempBuffer[3] = VeDcm_P_DataAddress[4];
			LaDcm_u_TempBuffer[4] = VeDcm_P_DataAddress[5];
			LaDcm_u_TempBuffer[5] = GetDtcStutas(DtcIndex);
			LaDcm_u_TempBuffer[6] = 1;
			LaDcm_u_TempBuffer[7] = 0xFF;
			LaDcm_u_TempBuffer[8] = 0xFF;
			LaDcm_u_TempBuffer[9] = 0xFF;
			LaDcm_u_TempBuffer[10] = 0xFF;
			LaDcm_u_TempBuffer[11] = 2;
			LaDcm_u_TempBuffer[12] = 0xFF;
			LaDcm_u_TempBuffer[13] = 0xFF;
			LaDcm_u_TempBuffer[14] = 0xFF;
			LaDcm_u_TempBuffer[15] = 0xFF;
			LaDcm_u_TempBuffer[16] = 3;
			LaDcm_u_TempBuffer[17] = 0xFF;
			LaDcm_u_TempBuffer[18] = 0xFF;
			LaDcm_u_TempBuffer[19] = 0xFF;
			LaDcm_u_TempBuffer[20] = 0xFF;
			LaDcm_u_TempBuffer[21] = 4;
			LaDcm_u_TempBuffer[22] = 0xFF;
			LaDcm_u_TempBuffer[23] = 0xFF;
			LaDcm_u_TempBuffer[24] = 0xFF;
			LaDcm_u_TempBuffer[25] = 0xFF;
			SetCanTp_TxBuffer(25,LaDcm_u_TempBuffer);      /*调用接口发送响应数据*/
			LeDcm_u_Result = CeDcm_e_ResultPosRsp;	
			break;
			}
		}	
	return LeDcm_u_Result;
}
#endif

//19 0A
uint8 MngDcm_ReportSupportedDTC(void)
{
	uint8 LaDcm_u_TempBuffer[DCM_TXBUFFER_LENGTH];
	TeDcm_h_ResultType LeDcm_u_Result;
	uint8 DtcIndex;
	uint8 DtcReporLength;	
	
	LaDcm_u_TempBuffer[0] = VeDcm_h_RqustInfo.Sid + 0x40;
	LaDcm_u_TempBuffer[1] = VeDcm_h_RqustInfo.Sub;
	LaDcm_u_TempBuffer[2] = DTC_STATUS_AVAILABILITY_MASK;
	DtcReporLength = 3;
	
	for(DtcIndex=0;DtcIndex<DCM_SUPDTC_COUNT;DtcIndex++)
	{
		LaDcm_u_TempBuffer[DtcReporLength++] = CaDcm_u_DtcTable[DtcIndex].DtcHigh;
		LaDcm_u_TempBuffer[DtcReporLength++] = CaDcm_u_DtcTable[DtcIndex].DtcMiddle;
		LaDcm_u_TempBuffer[DtcReporLength++] = CaDcm_u_DtcTable[DtcIndex].DtcLow;
		LaDcm_u_TempBuffer[DtcReporLength++] = DTC_STATUS_AVAILABILITY_MASK;
	}
	SetCanTp_TxBuffer(DtcReporLength,LaDcm_u_TempBuffer);      /*调用接口发送响应数据*/
	LeDcm_u_Result = CeDcm_e_ResultPosRsp;
	return LeDcm_u_Result;
}

bool1 MngDcm_CleanDtc(void)
{
	uint8 DtcIndex;
	uint8 temp[42]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
	uint8 t_we = 0;
		 
	for(DtcIndex=0;DtcIndex<DCM_SUPDTC_COUNT;DtcIndex++)
	{
		DtcStutas[DtcIndex].DtcStutas = 0;		
	}
	
	if(0!=DF_Write_All_DTC(&temp,GetDataFlashAddress(INDEX_19_BASE),42)){
		t_we++;
	}
	
	if(0 == t_we)
		return _ACTIVE;
	else
		return _FALSE;
}


