/**************************************************************************//**
 *
 *  \copyright  This software is the property of  Technologies. Any
 *              information contained in this doc should not be reproduced,
 *              or used, or disclosed without the written authorization from
 *               Technologies.
 *
 *  \brief      This is the source file of the adapter module based on uds
 *              platform.
 *
 *  \file       bl_adpt_uds_platform.c
 *  \ingroup    communication_adapter_module
 *  \author
 *
 *  \version    5.1.0
 *  \date       27/03/2018
 *
 *  \par        Changelist
 *      Version  | Date       | Authors          | CR# | Descriptions
 *      -------- | ---------- | ---------------- | --- | ------------
 *      01.00.00 | 25/03/2018 | xucong      | N/A | Boot010001

 *
 *****************************************************************************/
#include "bl_system_cfg.h"
#include "bl_common.h"
#include "bl_rte.h"
#include "bl_data.h"
#include "bl_can_tp.h"
#include "bl_can_if.h"
#include "bl_dcm.h"
#include "bl_dcm_cfg.h"
#include "bl_adapter.h"
#include "bl_security.h"
#include "bl_logical_block.h"


#include "bl_adpt_uds_platform.h"
#include "bl_adpt_uds_platform_cfg.h"
#include "bl_booting_cfg.h"
#include "bl_flash_if_cfg.h"
#include "bl_logical_block_cfg.h"
#include "bl_can_transceiver.h"
#include "bl_flash_if.h"
#include "bl_data_cfg.h"
#include "bl_flash.h"
#include "bl_memory.h"
#include "bl_memory_cfg.h"
//#include "eeprom.h"
#include "calibration.h"

#include "common_types.h"
#include "error.h"
#include "bl_system_cfg.h"
#include "crc.h"
#include "nvm.h"
#include "Ls_Nm.h"
#include "bl_access_udsdemo.h"
#include "wdog.h"

#ifdef UDS_DEBUG_EN
typedef struct
{
	u16 data[2];
	u16 time[8];
	u32 addr ;
	u32 size;
}usd_test_s;
usd_test_s g_usd_test = {0};
#endif

u8 check_date_valid(u8 *p_date);

/*****************************************************************************
 *  Verify The Configurations of Macro
 *****************************************************************************/
#if (DCM_BUFFER_SIZE < ADPT_MAXNUM_OF_BLOCKLENGTH)
#error "the buffer of the Dcm module is too small."
#endif

#if (RTE_FUN_GATEWAY == BL_FUN_ON)
#error "the function of gateway is not ok."
#endif
/*****************************************************************************
 *  Internal Macro Definitions
 *****************************************************************************/
/*lint -emacro(778,ADPT_SESSION_RESPONSE0,ADPT_SESSION_RESPONSE1)*/
/*lint -emacro(778,ADPT_SESSION_RESPONSE2,ADPT_SESSION_RESPONSE3)*/
/*lint -emacro(572,ADPT_SESSION_RESPONSE0,ADPT_SESSION_RESPONSE1)*/
/*lint -emacro(572,ADPT_SESSION_RESPONSE2,ADPT_SESSION_RESPONSE3)*/
#define ADPT_P2START_TIMER          (DCM_P2STARTIMEVALUE/DCM_P2STARTIMER_DIV)
#define ADPT_SESSION_RESPONSE0      (u8)(((u16)DCM_P2TIMEVALUE >> 8) & 0xFFu)
#define ADPT_SESSION_RESPONSE1      (u8)(((u16)DCM_P2TIMEVALUE >> 0) & 0xFFu)
#define ADPT_SESSION_RESPONSE2      (u8)(((u16)ADPT_P2START_TIMER >> 8) & 0xFFu)
#define ADPT_SESSION_RESPONSE3      (u8)(((u16)ADPT_P2START_TIMER >> 0) & 0xFFu)

#define ADPT_MEMORY_LEN_FORMAT          (0x04u)
#define ADPT_MEMORY_ADDR_FORMAT         (0x04u)

#define ADPT_MEMORY_LENANDADDR_FORMAT   ((ADPT_MEMORY_LEN_FORMAT << 4) \
										   | ADPT_MEMORY_ADDR_FORMAT)

#define ADPT_ERASE_DATA_LENGTH          (0x01u \
										   + ADPT_MEMORY_LEN_FORMAT \
										   + ADPT_MEMORY_ADDR_FORMAT)
#define ADPT_CHECKSUM_LENGTH            (0x04u)

#define ADPT_DATAFORMAT_IDEN            (0x00u)
#define ADPT_LENFORMAT_IDEN             (0x20u)
#define ADPT_IDEN_LEN_FORMAT            ((ADPT_LENFORMAT_IDEN >> 4) & 0x0Fu)
#define ADPT_REQDOWNLOAD_RESPLENGTH     (0x01u + ADPT_IDEN_LEN_FORMAT)

/** \brief Indicate the 31 service is ok.*/
#define ADPT_ROUTINE_CORRECT_RESULT     (0u)  //(0) // GL =  4	//31應答，FE-5HA是 4 和 5
/** \brief Indicate the 31 service is not ok.*/
#define ADPT_ROUTINE_INCORRECT_RESULT   (1u)  //(1) // GL = 5
#define ADPT_ROUTINE_INCORRECT_SIG_RESULT   (2u)  //(1) // GL = 5
#if 0  //目前沒有用到
#define ADPT_ROUTINE_NOT_ALL_MANDATORY_BLOCKS_PRESENT_RESULT   (6)  // 31  FF 01

#define ADPT_ROUTINE_HW_INCOMPATIBILITY_RESULT   (7)  // 31  FF 01
#define ADPT_ROUTINE_SW_INCOMPATIBILITY_RESULT   (8)  // 31  FF 01
#endif

#define ADPT_UDS_SYSTEM_STATUS_MASK     (RTE_SYSFLAG_WRITE_FINGPRINT \
											| ADPT_SYSTEM_STATUS_MASK)  //1c

#define ADPT_UDS_STATUS_MASK            (ADPT_STATUS_ALLOW_TRANS_DATA \
											| ADPT_STATUS_BLOCK_FIRST_DATA \
											| ADPT_STATUS_SEGMENT_FIRST_DATA)  //78000000

#define ADPT_UDS_36_SERVICE_ID              (0x36u)
#define ADPT_UDS_31_SERVICE_ID              (0x31u)
#define ADPT_UDS_3E_SERVICE_ID              (0x3Eu)
#define ADPT_UDS_11_SERVICE_ID              (0x11u)

#define ADPT_UDS_36_SERVICE_INDEX_POS       (0x00u)
#define ADPT_UDS_36_SERVICE_DATA_POS        (0x01u)

#define ADPT_UDS_ERASE_MEMORY_FORMAT_POS    (0)  // 44開始
#define ADPT_UDS_ERASE_MEMORY_ADDRESS_POS   (1)
#define ADPT_UDS_ERASE_MEMORY_SIZE_POS      (5)

#define ADPT_UDS_34_SERVICE_IDEN_POS        (0)
#define ADPT_UDS_34_SERVICE_ADDRANDSIZE_POS (1)
#define ADPT_UDS_34_SERVICE_ADDRESS_POS     (2)
#define ADPT_UDS_34_SERVICE_SIZE_POS        (ADPT_UDS_34_SERVICE_ADDRESS_POS \
												+ ADPT_MEMORY_ADDR_FORMAT)   //6

#define ADPT_UDS_34_RESPONSE_FORMAT_POS     (0)  // 如 20是格式
#define ADPT_UDS_34_RESPONSE_SIZE_POS       (1)		// 如 0402是長度


#define ADPT_STATUS_ALLOW_TRANS_DATA        DCM_MAKE_FLAG(0x20000000UL)  //
#define ADPT_STATUS_BLOCK_FIRST_DATA        DCM_MAKE_FLAG(0x10000000UL)
#define ADPT_STATUS_SEGMENT_FIRST_DATA      DCM_MAKE_FLAG(0x08000000UL)

#define ADPT_UDS_MAX_CAN_FRAME_SIZE         (CANTP_MAX_FRAME_SIZE)    //好好查一下該宏 
#define ADPT_UDS_CAN_FRAME_INDEX_0          (0)
#define ADPT_UDS_CAN_FRAME_INDEX_1          (1)
#define ADPT_UDS_CAN_FRAME_INDEX_2          (2)
#define ADPT_UDS_CAN_FRAME_INDEX_3          (3)
#define ADPT_UDS_CAN_FRAME_INDEX_4          (4)
#define ADPT_UDS_CAN_FRAME_INDEX_5          (5)
#define ADPT_UDS_CAN_FRAME_INDEX_6          (6)
#define ADPT_UDS_CAN_FRAME_INDEX_7          (7)
#define ADPT_UDS_CAN_FRAME_INDEX_8          (8)

/*****************************************************************************
 *  Internal Type Definitions
 *****************************************************************************/
/** \brief A alias of struct _tag_AdapterData.*/
typedef struct _tag_AdapterData bl_AdapterData_t;

#if (RTE_FUN_GATEWAY == BL_FUN_ON)
/** \brief A alias of struct _tag_GatewayTransmitList.*/
typedef struct _tag_GatewayTransmitList bl_GatewayTransmitList_t;
/** \brief A alias of struct _tag_GatewayConversion.*/
typedef struct _tag_GatewayConversion bl_GatewayConversion_t;
#endif

/*****************************************************************************
 *  Internal Structure Definitions
 *****************************************************************************/
/** \brief This is data of a adapter.*/
struct _tag_AdapterData
{
	struct _tag_SecurityDelay delay;    /**< security access delay*/
	bl_u8_t blockIndex;     /**< the block index of 36 service*/
	bl_Buffer_t serviceId;  /**< the id of processing service*/
	bl_Address_t address;   /**< the address of 36 service*/
	bl_Size_t remainSize;   /**< the remain size of 36 service*/
};

#if(RTE_FUN_GATEWAY == BL_FUN_ON)
/** \brief This is a list of gateway transmission.*/
struct _tag_GatewayTransmitList
{
	bl_u16_t handle;
	bl_u16_t number;
	struct _tag_GatewayConversion *data;
};

/**
 * \brief This is info of gateway conversion, it includes services
 *        needed to be transmitted.
 */
struct _tag_GatewayConversion
{
	bl_u8_t sid;
	bl_u16_t size;
	bl_Buffer_t *data;
};

#endif
/*****************************************************************************
 *  Internal Function Declarations
 *****************************************************************************/
#if(RTE_FUN_GATEWAY == BL_FUN_ON)
/** \brief Gateway transmits messages.*/
static void _Adpt_UdsGatewayTransmit(bl_u8_t id);
#endif
/** \brief Send 0x78 negative response.*/
static void _Adpt_UdsPendingResponse(bl_u8_t id);
/** \brief Timeout callback of sending 0x78.*/
static void _Adpt_TimeOutCallBack(bl_u8_t id);
/** \brief send a response by comif.*/
static void _Adpt_SendResponse(bl_BufferSize_t size, const bl_Buffer_t *buffer);
/** \brief Set response info of session.*/
static bl_BufferSize_t _Adpt_SetSessionResp(bl_Buffer_t *buffer);
/** \brief Goto the session.*/
static void _Adpt_UdsGotoSession(bl_DownStatus_t sessionMask);
/** \brief Reset the bootloader.*/
static void _Adpt_UdsResetSystem(bl_u8_t sid, bl_u16_t flagId);
/** \brief Clear the download information.*/
static void _Adpt_UdsClearDownInfo(bl_AdapterData_t *adpt);

/*****************************************************************************
 *  Internal Variable Definitions
 *****************************************************************************/
/** \brief Private data variable.*/
STATIC bl_AdapterData_t gs_UdsPrivateData;
#if 0
struct _tag_AdapterData
{
	struct _tag_SecurityDelay delay;    /**< security access delay*/
	bl_u8_t blockIndex;     /**< the block index of 36 service*/
	bl_Buffer_t serviceId;  /**< the id of processing service*/
	bl_Address_t address;   /**< the address of 36 service*/
	bl_Size_t remainSize;   /**< the remain size of 36 service*/
};
#endif

#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8
STATIC const bl_Buffer_t gs_DefaultResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x06u,0x50u,0x01u,ADPT_SESSION_RESPONSE0,ADPT_SESSION_RESPONSE1,
	ADPT_SESSION_RESPONSE2,ADPT_SESSION_RESPONSE3,CANTP_FRAME_PADDING_VALUE
};
#else
STATIC const bl_Buffer_t gs_DefaultResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x00,0x06u,0x50u,0x01u,ADPT_SESSION_RESPONSE0,ADPT_SESSION_RESPONSE1,
	ADPT_SESSION_RESPONSE2,ADPT_SESSION_RESPONSE3
};
#endif

#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8
STATIC const bl_Buffer_t gs_ReprogrammingResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE]=
{
	0x06u,0x50u,0x02u,ADPT_SESSION_RESPONSE0,ADPT_SESSION_RESPONSE1,
	ADPT_SESSION_RESPONSE2,ADPT_SESSION_RESPONSE3,CANTP_FRAME_PADDING_VALUE
};
#else
STATIC const bl_Buffer_t gs_ReprogrammingResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE]=
{
	0x00,0x06u,0x50u,0x02u,ADPT_SESSION_RESPONSE0,ADPT_SESSION_RESPONSE1,
	ADPT_SESSION_RESPONSE2,ADPT_SESSION_RESPONSE3
};
#endif

#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8
STATIC const bl_Buffer_t gs_ResetResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x02u,0x51u,0x01u,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE
};
#else
STATIC const bl_Buffer_t gs_ResetResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x00,0x02u,0x51u,0x01u,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE
};
#endif

#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8
STATIC const bl_Buffer_t gs_ResetResponse_sub03[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x02u,0x51u,0x03u,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE
};
#else
STATIC const bl_Buffer_t gs_ResetResponse_sub03[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x00,0x02u,0x51u,0x03u,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE,
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE
};
#endif


#if (RTE_FUN_STAY_IN_BOOT == BL_FUN_ON)
#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8
STATIC const bl_Buffer_t gs_StayInBootResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x04u,0x71u,0x01u,ADPT_STAY_IN_BOOT_RESPONSE_HID,  //F5
	//18							AA
	ADPT_STAY_IN_BOOT_RESPONSE_LID,CANTP_FRAME_PADDING_VALUE,
	//AA 						AA
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE
};
#else
STATIC const bl_Buffer_t gs_StayInBootResponse[ADPT_UDS_MAX_CAN_FRAME_SIZE] =
{
	0x0,0x04u,0x71u,0x01u,ADPT_STAY_IN_BOOT_RESPONSE_HID,  //F5
	//18							AA
	ADPT_STAY_IN_BOOT_RESPONSE_LID,CANTP_FRAME_PADDING_VALUE,
	//AA 						AA
	CANTP_FRAME_PADDING_VALUE,CANTP_FRAME_PADDING_VALUE
};
#endif
#endif

#if(RTE_FUN_GATEWAY == BL_FUN_ON)
/** \details The data of gateway conversion. */
STATIC const bl_u8_t gs_UdsGatewayData[ADPT_UDS_CONVERSION_DATA_LENGTH] =  //2
{
	0x3Eu, 0x80u,
}

/** \details A list of gateway transmission. */
STATIC const bl_GatewayTransmitList_t gs_UdsGatewayList =
{
	ADPT_UDS_COMOPS_HANDLE,  //0XFFFF
	ADPT_UDS_NUMBER_OF_GATEWAY,  //1
	gs_UdsGateway,
};

/** \details The info of gateway conversion. */
STATIC const bl_GatewayConversion_t gs_UdsGateway[ADPT_UDS_NUMBER_OF_GATEWAY] =  //1
{
	{
		ADPT_UDS_3E_SERVICE_ID,   // SID= 3E
		ADPT_UDS_CONVERSION_DATA_LENGTH,  //SZIE =2
		gs_UdsGatewayData  // 內容指針 指向 gs_UdsGatewayData 3e 80
	},
};

#endif

/*****************************************************************************
 *  Global Variable Definitions
 *****************************************************************************/
/** \details The Uds platform adapter informations. */
const bl_Adapter_t g_UdsPlatformAdapter =
{
	//type=0  index=0 //Init      //Callback
	0x00u, 0x00u, &Adpt_UdsInit, &Adpt_UdsCallback,
	//PeriodCallback             //Timeout
	&Adpt_UdsPeriodicCallback, &Adpt_UdsTimeOutCallBack,
	//Sleep 目前沒有sleep
#if (RTE_FUN_SYSTEM_SLEEP == BL_FUN_ON)
	&Canif_Sleep,
#else
	BL_NULL_PTR, /*RTE_FUN_SYSTEM_SLEEP is OFF*/
#endif
};

///// 安全访问相关
static u8 security_access_mclf_cycle_counter = 0u ;   //security_access_get_mclf_cycle_counter

u8 security_access_get_mclf_cycle_counter(void)
{
	return security_access_mclf_cycle_counter;
}

void security_access_set_mclf_cycle_counter(u8 value)
{
	security_access_mclf_cycle_counter = value;
}



/*****************************************************************************
 *  Function Definitions
 *****************************************************************************/
/**************************************************************************//**
 *
 *  \details Initialize the uds module. This function mainly initialize
 *           CanTp, dcm and security access delay info.
 *
 *  \return If Initialization is successful return BL_ERR_OK,
 *          otherwise return BL_ERR_NOT_OK.
 *
 *  \retval BL_ERR_OK       - Initialize successfully.
 *  \retval BL_ERR_NOT_OK   - Initialize unsuccessfully.
 *
 *  \note This function is called by the process driver module.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_Return_t Adpt_UdsInit(void)
{
	bl_Return_t ret;

	//ret = Canif_Init(); //己在Rte_PreInit初始化過了
	Cantp_Init();
	Dcm_Init();
	ls_nm_Init();

	gs_UdsPrivateData.serviceId = 0;

	Adpt_InitDelayInfo(&gs_UdsPrivateData.delay);

	_Adpt_UdsGotoSession(DCM_STATUS_SESSION_DEFAULT);

	return ret;
}

/**************************************************************************//**
 *
 *  \details Periodic callback of function Rte_PeriodicSchedule. This function
 *           needs to be called periodically, and mainly handles timer of dcm,
 *           and CanTp module.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
void Adpt_UdsPeriodicCallback(void)
{
	#ifdef UDS_DEBUG_EN
	g_usd_test.time[0]++;
	if(g_usd_test.time[0]>=1000)  //果然是250us;後來改回1ms了
	{
		g_usd_test.time[0]=0;
		g_usd_test.time[1]++;
	}
	#endif
	wdog_task();
	//(void)CanTrcv_CheckError();
	Adpt_DelayTimeCallback(&gs_UdsPrivateData.delay);	//
	Dcm_TimerFunction();   // gs_DcmS3TimerStartFlag and  gs_DcmS3Timer
	Cantp_PeriodFunction();
	Dcm_MainFunction();
	#ifdef NVM_EEPROM_EXT_EN
	eeprom_task();
	#endif
}

/**************************************************************************//**
 *
 *  \details Callback of the adapter, it should be always running. This
 *           function will be called by process driver module, and performs
 *           tx confirmation and rx indication.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
void Adpt_UdsCallback(void)
{
	#ifdef UDS_DEBUG_EN
	g_usd_test.time[2]++;
	if(g_usd_test.time[2]>=1000)
	{
		g_usd_test.time[2]=0;
		g_usd_test.time[3]++;
	}
	#endif
	Canif_TxMainFunction();
	Canif_RxMainFunction();
	Canif_BusoffMainFunction();
}

/**************************************************************************//**
 *
 *  \details Callback of default session.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *   在數組 gs_Dcm10SubIdl 中
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback1001(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;
	bl_u8_t l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	bl_u8_t i;
	(void)size;

	/*programming session.*/
	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_PROGRAMMING);
	if(BL_ERR_OK == ret)
	{
	#if 1
		if (Dcm_IsSuspendedResponse() != BL_ERR_OK)  //gs_SupPosRespFlag==0
		{
			#ifdef DEFAULT_GOTO_APP_DEFAULT_SEESION_78
			//先修改ram标志，使app能判断
			(void)Mem_Write(MEM_MEMORY_ID_RAM,DM_DATA_3_LOCAL_ADDR,DM_DATA_3_SIZE,DM_DATA_3_DDP); //
			_Adpt_UdsPendingResponse(0x10);
			#else
			for(i=0;i<8;i++)
			{
				l_buf[i] = gs_DefaultResponse[i];
			}
			for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
			{
				l_buf[i] = CANTP_FRAME_PADDING_VALUE;
			}
			_Adpt_SendResponse((bl_BufferSize_t)sizeof(l_buf),l_buf);
			#endif
		}
		g_RteInfo.SystemReset();
	#else

		if (Dcm_IsSuspendedResponse() != BL_ERR_OK) //gs_SupPosRespFlag==0
		{
			_Adpt_SendResponse((bl_BufferSize_t)sizeof(gs_DefaultResponse),
								gs_DefaultResponse);
		}
		else
		{
			Dcm_StopS3Timer();
			 *respSize = _Adpt_SetSessionResp(buffer);
		}
		if(Lbm_IsAllValid() == BL_ERR_OK)
		{
			 Dcm_StopS3Timer();
			_Adpt_UdsGotoSession(DCM_STATUS_SESSION_DEFAULT);
			*respSize = _Adpt_SetSessionResp(buffer);
		}
		else
		{
			//(void)Dm_WriteDefaultData(BOOTM_REPROGRAM_FLAG_DID);
			g_RteInfo.SystemReset(); /// 需要注释  待调试确认是否要复位?????
		}
	#endif
	}
	else
	{

		/*extended session.*/
		ret = Rte_IsValidStatus(&g_DownContext,
									DCM_STATUS_SESSION_EXTENDED);
		if(BL_ERR_OK == ret)
		{
			#if 0
			Dcm_StopS3Timer();

			_Adpt_UdsGotoSession(DCM_STATUS_SESSION_DEFAULT);
			#else
			if (Dcm_IsSuspendedResponse() != BL_ERR_OK)  //gs_SupPosRespFlag==0
			{
				#ifdef DEFAULT_GOTO_APP_DEFAULT_SEESION_78
				//先修改ram标志，使app能判断
				(void)Mem_Write(MEM_MEMORY_ID_RAM,DM_DATA_3_LOCAL_ADDR,DM_DATA_3_SIZE,DM_DATA_3_DDP); //
				_Adpt_UdsPendingResponse(0x10);
				#else
				_Adpt_SendResponse((bl_BufferSize_t)sizeof(gs_DefaultResponse),
									gs_DefaultResponse);
				#endif
			}
			g_RteInfo.SystemReset();
			#endif
		}
		else
		{
			/*extended session.*/
			ret = Rte_IsValidStatus(&g_DownContext,
									DCM_STATUS_SESSION_SUPPLY);
			if(BL_ERR_OK == ret)
			{
				#if 0
				Dcm_StopS3Timer();

				_Adpt_UdsGotoSession(DCM_STATUS_SESSION_DEFAULT);
				#else
				if (Dcm_IsSuspendedResponse() != BL_ERR_OK)  //gs_SupPosRespFlag==0
				{
					#ifdef DEFAULT_GOTO_APP_DEFAULT_SEESION_78
					//先修改ram标志，使app能判断
					(void)Mem_Write(MEM_MEMORY_ID_RAM,DM_DATA_3_LOCAL_ADDR,DM_DATA_3_SIZE,DM_DATA_3_DDP); //
					_Adpt_UdsPendingResponse(0x10);
					#else
					_Adpt_SendResponse((bl_BufferSize_t)sizeof(gs_DefaultResponse),
										gs_DefaultResponse);
					#endif
				}
				g_RteInfo.SystemReset();
				#endif
			}
		}

		*respSize = _Adpt_SetSessionResp(buffer);
	}

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of programming session.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback1002(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_CONDITIONSNOTCORRECT;
	bl_Return_t ret;

	(void)size;

	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_DEFAULT);
	if (ret != BL_ERR_OK) /*the default can not go to programming.*/
	{
		/*write flag.*/
		if(Lbm_IsAllValid() == BL_ERR_OK)
		{
			Dcm_StartS3Timer();

			Lbm_ClearBlock(&g_DownContext);  //会知道当前是 flash app 还是 flash drv, 清除为 LBM_STATUS_INVALID

			Rte_ClearAddressInfo(&g_DownContext);  // dct->segIndex

			_Adpt_UdsGotoSession(DCM_STATUS_SESSION_PROGRAMMING);

			*respSize = _Adpt_SetSessionResp(buffer);
			resCode = DCM_E_POSITIVERESPONSE;
		}
		else
		{
			(void)Dm_WriteDefaultData(BOOTM_REPROGRAM_FLAG_DID); //BOOTM_REPROGRAM_FLAG_DID
			#ifdef BOOT_STATE_PROG_DEBUG_EN
			#else
			g_RteInfo.SystemReset();
			#endif
		}
	}
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of extended session.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback1003(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_CONDITIONSNOTCORRECT;
	bl_Return_t ret;

	(void)size;

	#if 0  //任何會話，直接進入03
	/*programming session.*/
	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_PROGRAMMING);

	///ret = BL_ERR_NOT_OK; ///// ???
	if (ret != BL_ERR_OK) /*the programming can not go to extended.*/
	#endif
	{
		Dcm_StartS3Timer();

		_Adpt_UdsGotoSession(DCM_STATUS_SESSION_EXTENDED);

		*respSize = _Adpt_SetSessionResp(buffer);

		resCode = DCM_E_POSITIVERESPONSE;
	}
	return resCode;
}


/**************************************************************************//**
 *
 *  \details Callback of extended session.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback1004(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_CONDITIONSNOTCORRECT;
	bl_Return_t ret;

	(void)size;
	#if 0  //任何會話，直接進入60
	/*programming session.*/
	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_PROGRAMMING);

	///ret = BL_ERR_NOT_OK; ///// ???
	if (ret != BL_ERR_OK) /*the programming can not go to extended.*/
	#endif
	{
		Dcm_StartS3Timer();

		_Adpt_UdsGotoSession(DCM_STATUS_SESSION_SUPPLY);

		*respSize = _Adpt_SetSessionResp(buffer);

		resCode = DCM_E_POSITIVERESPONSE;
	}
	return resCode;
}


/**************************************************************************//**
 *
 *  \details Callback of reset.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *  gs_Dcm11SubId 數組中
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback1101(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_u8_t i;
	bl_u8_t l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	(void)respSize;
	(void)buffer;
	(void)size;

	if (Dcm_IsSuspendedResponse() != BL_ERR_OK)  //gs_SupPosRespFlag==0
	{
		for(i=0;i<8;i++)
		{
			l_buf[i] = gs_ResetResponse[i];
		}
		for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
		{
			l_buf[i] = CANTP_FRAME_PADDING_VALUE;
		}
		_Adpt_SendResponse((bl_BufferSize_t)sizeof(l_buf),l_buf);
	}

	g_RteInfo.SystemReset();
	/*never return*/
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of reset.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *  gs_Dcm11SubId 數組中
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback1103(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_u8_t i;
	bl_u8_t l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	(void)respSize;
	(void)buffer;
	(void)size;

	if (Dcm_IsSuspendedResponse() != BL_ERR_OK)  //gs_SupPosRespFlag==0
	{
		for(i=0;i<8;i++)
		{
			l_buf[i] = gs_ResetResponse_sub03[i];
		}
		for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
		{
			l_buf[i] = CANTP_FRAME_PADDING_VALUE;
		}
		_Adpt_SendResponse((bl_BufferSize_t)sizeof(l_buf),l_buf);
	}

	g_RteInfo.SystemReset();
	/*never return*/
	return resCode;
}

/*  22 F198 */
/**************************************************************************//**
 *
 *  \details Callback of reading fingerprint.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*  22 0201 */

bl_ResponseCode_t Adpt_UdsCallBackReadPgmAttmpCounter(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u16_t tmp=0;
	(void)size;
	resCode = DCM_E_SUBFUNCTIONNOTSUPPORTED;
	return resCode;
}

/*  22 0200 */

//(void)EEPROM_GetWord((EEPROM_TAddress)(EEPROM_DATA_BASE_ADDR+EEPROM_START_ADDRESS+0x10), &gs_LbmData.pgm_counter.dt.f_attmp);
//(void)EEPROM_GetWord((EEPROM_TAddress)(EEPROM_DATA_BASE_ADDR+EEPROM_START_ADDRESS+0x12), &gs_LbmData.pgm_counter.dt.success);
bl_ResponseCode_t Adpt_UdsCallBackReadPgmSuccCounter(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	u8 l_buf[SYSDID_ATTEMP_CK_LEN];
	u8 l_crc;
	(void)size;
	#if 0
	(void)cal_read_did(0xF100u,(u8*)&l_buf[0],SYSDID_ATTEMP_CK_LEN);
	l_crc = crc_8_make(&l_buf[0],4);
	if(l_buf[4]==l_crc)
	{
		if( (l_buf[0]==0xffu) && (l_buf[1]==0xFFu) )
		{
			l_buf[0]=0u;
			l_buf[1]=0u;
		}
	}
	else
	{
		l_buf[0]=0u;
		l_buf[1]=0u;
		#ifdef DEBUG_APP_DCM_EN
		g_debug_app_dcm.crc_error_count++;
		#endif
	}
	buffer[0] = l_buf[0];
	buffer[1] = l_buf[1];
	buffer[2] = (bl_u8_t)0xff;
	buffer[3] = (bl_u8_t)0xff;
	*respSize = 4;
	#else
	buffer[0] = 0;
	buffer[1] = 0;
	buffer[2] = (bl_u8_t)0xff;
	buffer[3] = (bl_u8_t)0xff;
	#endif
	return resCode;
}





/*  22 F180 */
bl_ResponseCode_t Adpt_UdsCallBackReadBootSW_ID(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u8_t i;
	(void)size;
	buffer[0] = g_sysdid_f180[0];  //这句话，不要去掉，目的是防止被优化掉
	(void)cal_read_did(0xF180u,(u8*)&buffer[0],SYSDID_F180_CK_LEN);
	*respSize = SYSDID_F180_LEN;

	return resCode;
}


/*  22 F181 */
/*  software detailed design ID: BSWDD110211*/
bl_ResponseCode_t Adpt_UdsCallBackRead_F181_vmapp_softid_ID(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u8_t i;
	(void)size;
	(void)cal_read_did(0xF181u,(u8*)&buffer[0],SYSDID_F181_CK_LEN);
	*respSize = SYSDID_F181_LEN;

	return resCode;
}

/*  22 F182 */
/*  software detailed design ID: BSWDD110212*/
bl_ResponseCode_t Adpt_UdsCallBackRead_F182_ecu_calnum(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u8_t i;
	(void)size;
	//buffer[0] = g_sysdid_f182[0];  //这句话，不要去掉，目的是防止被优化掉
	#if 0
	for(i = 0; i < SYSDID_F182_LEN; i++)
	{
		buffer[i] = g_sysdid_f182[i];
	}
	#else
	(void)cal_read_did(0xF182u,(u8*)&buffer[0],SYSDID_F182_CK_LEN);
	#endif
	*respSize = SYSDID_F182_LEN;

	return resCode;
}



/*  22 F186 */
bl_ResponseCode_t Adpt_UdsCallBackReadActiveSession(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;
	(void)size;
	buffer[0] = 0x0;
	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_DEFAULT);
	if(BL_ERR_OK == ret)
	{
		buffer[0] = 0x01;
	}
	else
	{
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_PROGRAMMING);
		if(BL_ERR_OK == ret)
		{
			buffer[0] = 0x02;
		}
		else
		{
			ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_EXTENDED);
			if(BL_ERR_OK == ret)
			{
				buffer[0] = 0x03;
			}
			else
			{
				ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_SUPPLY);
				if(BL_ERR_OK == ret)
				{
					buffer[0] = DCM_SESSION_SUPPLY_ID;
				}
			}
		}
	}
	*respSize = 1;
	return resCode;
}

/*  22 F187 */
/*  software detailed design ID: BSWDD110214*/
bl_ResponseCode_t Adpt_UdsCallBackReadSparaPartNumber(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u8_t i;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF187u,(u8*)&buffer[0],SYSDID_F187_CK_LEN);
	*respSize = SYSDID_F187_LEN;
	return resCode;
}

/*  22 F188 */
/*  software detailed design ID: BSWDD110215*/
bl_ResponseCode_t Adpt_UdsCallBackRead_F188_ECU_SW(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF188u,(u8*)&buffer[0],SYSDID_F188_CK_LEN);
	*respSize = SYSDID_F188_LEN;
	return resCode;
}






/*  22 F189 */
bl_ResponseCode_t Adpt_UdsCallBackReadfe02_count(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	u8 l_u16_temp[2];
	(void)size;
	//if(1)
	//if(0 == IntEeprom_ReadBytes(EEPROM_F180_START_ADDRESS+0x1AU, buffer, RTE_PGM_DATE_SIZE))
	{
		//resCode = DCM_E_SUBFUNCTIONNOTSUPPORTED;
		//resCode = DCM_E_SUBFUNCTIONNOTSUPPORTED;
		l_u16_temp[0] = gs_UdsPrivateData.delay.delayTime/1000;
		l_u16_temp[1] = 0u;
		buffer[0] = (u8)( l_u16_temp[0] >>8 );
		buffer[1] = (u8)(l_u16_temp[0] &0x00ffu);
		buffer[2] = (u8)( l_u16_temp[1] >>8 );
		buffer[3] = (u8)(l_u16_temp[1] &0x00ffu);
		*respSize = 4;
	}
	#if 0
	else
	{

		if( (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0 && buffer[3] == 0 &&
			buffer[4] == 0 && buffer[5] == 0 && buffer[6] == 0 && buffer[7] == 0 &&
			buffer[8] == 0 && buffer[9] == 0 && buffer[10] == 0 && buffer[11] == 0 &&
			buffer[12] == 0 && buffer[13] == 0 && buffer[14] == 0 && buffer[15] == 0 ) || (

			buffer[0] == 0xff && buffer[1] == 0xff && buffer[2] == 0xff && buffer[3] == 0xff &&
			buffer[4] == 0xff && buffer[5] == 0xff && buffer[6] == 0xff && buffer[7] == 0xff &&
			buffer[8] == 0xff && buffer[9] == 0xff && buffer[10] == 0xff && buffer[11] == 0xff &&
			buffer[12] == 0xff && buffer[13] == 0xff && buffer[14] == 0xff && buffer[15] == 0xff )) {

			buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0x20;
			buffer[4] = buffer[5] = buffer[6] = buffer[7] = 0x20;
			buffer[8] = buffer[9] = buffer[10] = buffer[11] = 0x20;
			buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0x20;
		}
		*respSize = 16;

		/**/
	}
	#endif
	return resCode;
}


/*  22 F18A */
/*  software detailed design ID: BSWDD110217*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f18A_supplier(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;
	(void)cal_read_did(0xF18au,(u8*)&buffer[0],SYSDID_F18A_CK_LEN);
	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	*respSize = SYSDID_F18A_LEN;
	return resCode;
}

/*  22 F18B */
/*  software detailed design ID: BSWDD110218*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f18B_ecu_manu_date(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF18bu,(u8*)&buffer[0],SYSDID_F18B_CK_LEN);
	*respSize = SYSDID_F18B_LEN;
	return resCode;
}


/*  22 F18C */
/*  software detailed design ID: BSWDD110219*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f18C_ecu_sn(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_u8_t l_crc;
	(void)size;
	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF18Cu,(u8*)&buffer[0],SYSDID_F18C_CK_LEN);
	l_crc = crc_8_make(buffer,SYSDID_F18C_LEN);
	if(l_crc == buffer[SYSDID_F18C_LEN]) //校验正确
	{
		//如果是正确的
	}
	else
	{
		//buffer[0]=0;
		//如果是错误的
		#ifdef DEBUG_APP_DCM_EN
		g_debug_app_dcm.crc_error_count++;
		#endif
	}
	*respSize = SYSDID_F18C_LEN;
	return resCode;
}


/*  22 F190 */
/*  software detailed design ID: BSWDD110220*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f190_vin(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	u8 l_crc;
	(void)size;
	(void)cal_read_did(0xF190u,(u8*)&buffer[0],SYSDID_F190_VIN_CK_LEN);
	l_crc = crc_8_make(buffer,SYSDID_F190_VIN_LEN);
	if(l_crc == buffer[SYSDID_F190_VIN_LEN]) //校验正确
	{
		//如果是正确的
	}
	else
	{
		//buffer[0]=0;
		//如果是错误的--也认为未写入
		#ifdef DEBUG_APP_DCM_EN
		g_debug_app_dcm.crc_error_count++;
		#endif
	}
	*respSize = RTE_F190_VIN_SIZE;
	return resCode;
}


/*  22 F191 */
/*  software detailed design ID: BSWDD110221*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f191_ecu_hw_num(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF191u,(u8*)&buffer[0],SYSDID_F191_CK_LEN);
	*respSize = SYSDID_F191_LEN;
	return resCode;
}


/*  22 F192 */
/*  software detailed design ID: BSWDD110222*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f192_hw_part_num(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF192u,(u8*)&buffer[0],SYSDID_F192_CK_LEN);
	*respSize = SYSDID_F192_LEN;
	return resCode;
}

/*  22 F193 */
/*  software detailed design ID: BSWDD110223*/
bl_ResponseCode_t Adpt_UdsCallBackReadECU_HW_Number(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	//u8 i;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;
	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF193u,(u8*)&buffer[0],SYSDID_F193_CK_LEN);
	*respSize = SYSDID_F193_LEN;
	return resCode;
}
//#define UDS_TEST_F194_CHECKSUM 
#ifdef UDS_TEST_F194_CHECKSUM
volatile static u8 g_test_f194[2] ={0u};
#endif
/*  22 F194 */
/*  software detailed design ID: BSWDD110224*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f194_ecu_sw_num(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	u8 i;
	u8 l_checksum=0u;

	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF194u,(u8*)&buffer[0],SYSDID_F194_CK_LEN);
	//判断checksum
	for(i = 0; i < SYSDID_F194_LEN; i++)
	{
		l_checksum += buffer[i];
	}
	//校验正确，说明有APP
	if(l_checksum == buffer[SYSDID_F194_LEN])
	{
		#ifdef UDS_TEST_F194_CHECKSUM
		g_test_f194[0]++;
		#endif
	}
	else
	{
		#ifdef UDS_TEST_F194_CHECKSUM
		g_test_f194[1]++;
		#endif
		for(i = 0; i < SYSDID_F194_LEN; i++)
		{
			buffer[i] = g_sysdid_f194[i];
		}
	}
	*respSize = SYSDID_F194_LEN;
	return resCode;
}

/*  22 F195 */
/*  software detailed design ID: BSWDD110225*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f195_sw_version_num(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF195u,(u8*)&buffer[0],SYSDID_F195_CK_LEN);
	*respSize = SYSDID_F195_LEN;
	return resCode;
}

/*  22 F198 */
/*  software detailed design ID: BSWDD110226*/
bl_ResponseCode_t Adpt_UdsCallBackReadFingerprint(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	//u8 i;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_Return_t ret = BL_ERR_OK;
	u8 l_crc;
	(void)size;
	/*read fingerprint.*/
	#ifdef FLASH_DRIVER_READ
	buffer[0] = g_boot_read_sx[0];
	#endif
	(void)cal_read_did(0xF198u,(u8*)&buffer[0],SYSDID_F198_REPAIR_SN_CK_LEN);
	l_crc = crc_8_make(buffer,SYSDID_F198_REPAIR_SN_LEN);
	if(l_crc == buffer[SYSDID_F198_REPAIR_SN_LEN]) //校验正确
	{
		//如果是正确的
	}
	else
	{
		//pu8Data[0]=0;
		//如果是错误的--也认为未写入
		#ifdef DEBUG_APP_DCM_EN
		g_debug_app_dcm.crc_error_count++;
		#endif
	}
	*respSize = SYSDID_F198_REPAIR_SN_LEN;
	return resCode;
}


/*  22 F199 */
/*  software detailed design ID: BSWDD110227*/
bl_ResponseCode_t Adpt_UdsCallBackReadProgrammingDate(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	u8 l_crc;
	(void)size;
	(void)cal_read_did(0xF199u,(u8*)&buffer[0],SYSDID_F199_DATE_CK_LEN);
	l_crc = crc_8_make(buffer,SYSDID_F199_DATE_LEN);
	if(l_crc == buffer[SYSDID_F199_DATE_LEN]) //校验正确
	{
		//如果是正确的
	}
	else
	{
		//pu8Data[0]=0;
		//如果是错误的--也认为未写入
		#ifdef DEBUG_APP_DCM_EN
		g_debug_app_dcm.crc_error_count++;
		#endif
	}
	*respSize = SYSDID_F199_DATE_LEN;
	return resCode;
}



/*  22 F19E */
/*  software detailed design ID: BSWDD110228*/
bl_ResponseCode_t Adpt_UdsCallBackRead_f19E_odx_id(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u8_t i;
	(void)size;
	(void)cal_read_did(0xF19eu,(u8*)&buffer[0],SYSDID_F19E_CK_LEN);
	*respSize = SYSDID_F19E_LEN;
	return resCode;
}


/*  22 F1F1 */
/*  software detailed design ID: BSWDD110229*/
bl_ResponseCode_t Adpt_UdsCallBackRead_F1F1_ecu_cal_num(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	(void)size;

	//resCode = DCM_E_CONDITIONSNOTCORRECT;
	(void)cal_read_did(0xF1f1u,(u8*)&buffer[0],SYSDID_F1F1_CK_LEN);
	*respSize = SYSDID_F1F1_LEN;
	return resCode;
}

/*  22 F1F2*/
/*  software detailed design ID: BSWDD110230*/
bl_ResponseCode_t Adpt_UdsCallBackRead_F1F2_boot_sw_id(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	//bl_u8_t i;
	(void)size;

	(void)cal_read_did(0xF1f2u,(u8*)&buffer[0],SYSDID_F1F2_CK_LEN);
	*respSize = SYSDID_F1F2_LEN;
	return resCode;
}

/*  22 01a0*/
/*  software detailed design ID: BSWDD110231*/
bl_ResponseCode_t Adpt_UdsCallBackRead_01a0_manulifecount(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	u8 l_crc;
	u8 l_buf[2];
	(void)size;
	(void)cal_read_did(0x01A0u,(u8*)&l_buf[0],SYSDID_MANUFAC_LIFECYCLE_COUNT_CK_LEN);
	l_buf[0] = security_access_get_mclf_cycle_counter();
	l_crc = crc_8_make(&l_buf[0],1);
	if(l_crc == l_buf[1])
	{
		buffer[0] = security_access_get_mclf_cycle_counter();;
	}
	else
	{
		buffer[0] = security_access_get_mclf_cycle_counter();;
	}
	*respSize = SYSDID_MANUFAC_LIFECYCLE_COUNT_LEN;
	return resCode;
}

/*  22 Fe01*/
/*  software detailed design ID: BSWDD110232*/
bl_ResponseCode_t Adpt_UdsCallBackRead_FE01_device_id(bl_BufferSize_t size,
												  bl_Buffer_t *buffer,
												  bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;//DCM_E_SUBFUNCTIONNOTSUPPORTED;
	(void)size;
	#if 1//8
	//(void)flash_read_bytes(0xFFFE08,buffer,8);
	//buffer[8] = FSEC;
	*respSize = 9;
	#else
	//flash_read_bytes(0xFFFE00,buffer,16);
	//buffer[16] = FSEC;
	*respSize = 17;
	#endif
	resCode = DCM_E_POSITIVERESPONSE;
	return resCode;
}


bl_u8_t g_cur_request_level =0;  //當前請求的等級

/**************************************************************************//**
 *
 *  \details Callback of getting seed.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallbackGetSeedForLevel1(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;
#if(ADPT_FUN_DELAY_WITH_CONTINUOUS_SEED == BL_FUN_ON)
	bl_u16_t remainCount = 0;
#endif

	(void)size;

	do
	{
	#if 0  //為什麼 FE-5HA把這個給去掉了
		/*judge wheter security access is available.*/
		ret = Adpt_IsAccessible(&gs_UdsPrivateData.delay);
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUIREDTIMEDELAYNOTEXPIRED;
			break;
		}
	#endif
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SECURITY_UNLOCK_1);
		if(BL_ERR_OK == ret)
		{
			/*when unlock, return seed 0x00u.*/
			Bl_MemSet(buffer,0,(bl_Size_t)SECM_ACCESS_SEED_SIZE);
			break;
		}
		/*If a seed is requested, return it.*/
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SECURITY_WAITKEY_1);
		if(BL_ERR_OK == ret)
		{
#if(ADPT_FUN_DELAY_WITH_CONTINUOUS_SEED == BL_FUN_ON)
			/*if always request seed, decrease attemptcount.*/
			remainCount = Adpt_DecreaseAttemptCount(&gs_UdsPrivateData.delay);
			if(remainCount == 0x00u)
			{
#if(ADPT_FUN_SAVE_DELAY_FLAG == BL_FUN_ON)
				/*set security access delay flag.*/
				(void)Dm_WriteDefaultData(gs_UdsPrivateData.delay.delayFlagId);
#endif

				Rte_SetMutexDownStatus(&g_DownContext,
										DCM_STATUS_SECURITY_LOCK_1,
										DCM_SECURITYACCESS_1_MASK);

				resCode = DCM_E_EXCEEDNUMBEROFATTEMPTS;
				break;
			}
#endif
			/*return the last seed.*/
			Secm_GetSeed(buffer);

			break;
		}

		/*request a new seed.*/
		Secm_MakeNewSeed();
		/*Copy the seed to buffer*/
		Secm_GetSeed(buffer);
		/*set wait key status.*/
		Rte_SetDownStatus(&g_DownContext, DCM_STATUS_SECURITY_WAITKEY_1);

		g_cur_request_level = 0u;  // 0就是mask數組中的第一等級

	}while(0);/*lint !e717*/

	*respSize = SECM_ACCESS_SEED_SIZE;

	return resCode;
}


/**************************************************************************//**
 *
 *  \details Callback of getting seed.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*  software detailed design ID: BSWDD110234*/
bl_ResponseCode_t Adpt_UdsCallbackGetSeedForLevel5(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;
#if(ADPT_FUN_DELAY_WITH_CONTINUOUS_SEED == BL_FUN_ON)
	bl_u16_t remainCount = 0;
#endif

	(void)size;

	do
	{
	#if 0  //為什麼 FE-5HA把這個給去掉了
		/*judge wheter security access is available.*/
		ret = Adpt_IsAccessible(&gs_UdsPrivateData.delay);
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUIREDTIMEDELAYNOTEXPIRED;
			break;
		}
	#endif
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SECURITY_UNLOCK_2);
		if(BL_ERR_OK == ret)
		{
			/*when unlock, return seed 0x00u.*/
			Bl_MemSet(buffer,0,(bl_Size_t)SECM_ACCESS_SEED_SIZE);
			break;
		}
		/*If a seed is requested, return it.*/
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SECURITY_WAITKEY_2);
		if(BL_ERR_OK == ret)
		{
#if(ADPT_FUN_DELAY_WITH_CONTINUOUS_SEED == BL_FUN_ON)
			/*if always request seed, decrease attemptcount.*/
			remainCount = Adpt_DecreaseAttemptCount(&gs_UdsPrivateData.delay);
			if(remainCount == 0x00u)
			{
#if(ADPT_FUN_SAVE_DELAY_FLAG == BL_FUN_ON)
				/*set security access delay flag.*/
				(void)Dm_WriteDefaultData(gs_UdsPrivateData.delay.delayFlagId);
#endif

				Rte_SetMutexDownStatus(&g_DownContext,
										DCM_STATUS_SECURITY_LOCK_2,
										DCM_SECURITYACCESS_2_MASK);

				resCode = DCM_E_EXCEEDNUMBEROFATTEMPTS;
				break;
			}
#endif
			/*return the last seed.*/
			Secm_GetSeed(buffer);

			break;
		}

		/*request a new seed.*/
		Secm_MakeNewSeed();
		/*Copy the seed to buffer*/
		Secm_GetSeed(buffer);
		/*set wait key status.*/
		Rte_SetDownStatus(&g_DownContext, DCM_STATUS_SECURITY_WAITKEY_2);

		g_cur_request_level = 2;  // 2就是mask數組中的第五等級

	}while(0);/*lint !e717*/

	*respSize = SECM_ACCESS_SEED_SIZE;

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of verifying key.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*lint -e{818}*/
bl_ResponseCode_t Adpt_UdsCallbackVerifyKeyForLevel1(bl_BufferSize_t size,
													 bl_Buffer_t *buffer,
													 bl_BufferSize_t *respSize)
{
	bl_u16_t remainCount;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	do
	{
		ret = Rte_IsValidStatus(&g_DownContext,
									DCM_STATUS_SECURITY_WAITKEY_1);
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUESTSEQUENCEERROR;
			break;
		}

		/*verify key.*/
		ret = Secm_VerifyKey((bl_Size_t)size,buffer);
		if (ret != BL_ERR_OK)
		{
			remainCount = Adpt_DecreaseAttemptCount(&gs_UdsPrivateData.delay);  //相當於  等 attemptCount  操作次數 Adpt_DecreaseAttemptCount
			if (0x00u == remainCount)
			{
#if(ADPT_FUN_SAVE_DELAY_FLAG == BL_FUN_ON)
				/*set security access delay flag.*/
				(void)Dm_WriteDefaultData(gs_UdsPrivateData.delay.delayFlagId);
#endif
				Rte_SetMutexDownStatus(&g_DownContext,
										DCM_STATUS_SECURITY_LOCK_1,
										DCM_SECURITYACCESS_1_MASK);

				resCode = DCM_E_EXCEEDNUMBEROFATTEMPTS;
			}
			else
			{
				resCode = DCM_E_INVALIDKEY;
			}

			break;
		}
		*respSize = 0x00u;
		Rte_SetMutexDownStatus(&g_DownContext,
								DCM_STATUS_SECURITY_UNLOCK_1,
								DCM_SECURITYACCESS_1_MASK);   //从0x512 变成 0x212

	}while(0);/*lint !e717*/

	return resCode;
}


/*lint -e{818}*/
/*  software detailed design ID: BSWDD110236*/
bl_ResponseCode_t Adpt_UdsCallbackVerifyKeyForLevel5(bl_BufferSize_t size,
													 bl_Buffer_t *buffer,
													 bl_BufferSize_t *respSize)
{
	bl_u16_t remainCount;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	do
	{
		ret = Rte_IsValidStatus(&g_DownContext,
									DCM_STATUS_SECURITY_WAITKEY_2);
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUESTSEQUENCEERROR;
			break;
		}

		/*verify key.*/
		ret = Secm_VerifyKey((bl_Size_t)size,buffer);
		if (ret != BL_ERR_OK)
		{
			remainCount = Adpt_DecreaseAttemptCount(&gs_UdsPrivateData.delay);  //相當於  等 attemptCount  操作次數 Adpt_DecreaseAttemptCount
			if (0x00u == remainCount)
			{
#if(ADPT_FUN_SAVE_DELAY_FLAG == BL_FUN_ON)
				/*set security access delay flag.*/
				(void)Dm_WriteDefaultData(gs_UdsPrivateData.delay.delayFlagId);
#endif
				Rte_SetMutexDownStatus(&g_DownContext,
										DCM_STATUS_SECURITY_LOCK_2,
										DCM_SECURITYACCESS_2_MASK);

				resCode = DCM_E_EXCEEDNUMBEROFATTEMPTS;
			}
			else
			{
				resCode = DCM_E_INVALIDKEY;
			}

			break;
		}
		*respSize = 0x00u;
		Rte_SetMutexDownStatus(&g_DownContext,
								DCM_STATUS_SECURITY_UNLOCK_2,
								DCM_SECURITYACCESS_2_MASK);
	}while(0);/*lint !e717*/

	return resCode;
}



/**************************************************************************//**
 *
 *  \details Callback of getting seed.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*  software detailed design ID: BSWDD110233*/
bl_ResponseCode_t Adpt_UdsCallbackGetSeedForLevel23(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;
#if(ADPT_FUN_DELAY_WITH_CONTINUOUS_SEED == BL_FUN_ON)
	bl_u16_t remainCount = 0;
#endif

	(void)size;
	#if 0
	do
	{
	#if 1  //為什麼 FE-5HA把這個給去掉了
		/*judge wheter security access is available.*/
		ret = bl_access_supply_get_lock();
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUIREDTIMEDELAYNOTEXPIRED;
			break;
		}
	#endif
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SECURITY_UNLOCK_3);
		if(BL_ERR_OK == ret)
		{
			/*when unlock, return seed 0x00u.*/
			Bl_MemSet(buffer,0,(bl_Size_t)SECM_ACCESS_SEED_SIZE);
			break;
		}
		/*If a seed is requested, return it.*/
		ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SECURITY_WAITKEY_3);
		if(BL_ERR_OK == ret)
		{
#if(ADPT_FUN_DELAY_WITH_CONTINUOUS_SEED == BL_FUN_ON)
			/*if always request seed, decrease attemptcount.*/
			remainCount = bl_access_supply_inc_attemptCount();
			if(remainCount == 0x00u)
			{
#if(ADPT_FUN_SAVE_DELAY_FLAG == BL_FUN_ON)
				/*set security access delay flag.*/
				(void)Dm_WriteDefaultData(gs_UdsPrivateData.delay.delayFlagId);
#endif

				Rte_SetMutexDownStatus(&g_DownContext,
										DCM_STATUS_SECURITY_LOCK_3,
										DCM_SECURITYACCESS_3_MASK);

				resCode = DCM_E_EXCEEDNUMBEROFATTEMPTS;
				break;
			}
#endif
			/*return the last seed.*/
			//Secm_GetSeed(buffer);
			Acc_UdsDemoGetSeed_supply(buffer);
			break;
		}

		/*request a new seed.*/
		//Secm_MakeNewSeed();
		/*Copy the seed to buffer*/
		//Secm_GetSeed(buffer);
		Acc_UdsDemoGetSeed_supply(buffer);
		/*set wait key status.*/
		Rte_SetDownStatus(&g_DownContext, DCM_STATUS_SECURITY_WAITKEY_3);

		g_cur_request_level = 0x23;  // 0就是mask數組中的第一等級

	}while(0);/*lint !e717*/
	*respSize = SECM_ACCESS_SEED_SIZE;
	#else
	resCode = DCM_E_SUBFUNCTIONNOTSUPPORTEDINACTIVESESSION;
	#endif
	return resCode;
}

/*lint -e{818}*/
/*  software detailed design ID: BSWDD110235*/
bl_ResponseCode_t Adpt_UdsCallbackVerifyKeyForLevel24(bl_BufferSize_t size,
													 bl_Buffer_t *buffer,
													 bl_BufferSize_t *respSize)
{
	bl_u16_t remainCount;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;
	#if 0
	do
	{
		ret = Rte_IsValidStatus(&g_DownContext,
									DCM_STATUS_SECURITY_WAITKEY_3);
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUESTSEQUENCEERROR;
			break;
		}

		/*verify key.*/
		ret = Acc_UdsDemoVerifyKey_for_supply((bl_Size_t)size,buffer);  //   Secm_VerifyKey
		if (ret != BL_ERR_OK)
		{
			remainCount = bl_access_supply_get_attemptCount();//相當於  等 attemptCount  操作次數
			if (0x00u == remainCount)
			{
#if(ADPT_FUN_SAVE_DELAY_FLAG == BL_FUN_ON)
				/*set security access delay flag.*/
				(void)Dm_WriteDefaultData(gs_UdsPrivateData.delay.delayFlagId);
#endif
				Rte_SetMutexDownStatus(&g_DownContext,
										DCM_STATUS_SECURITY_LOCK_1,
										DCM_SECURITYACCESS_1_MASK);

				resCode = DCM_E_EXCEEDNUMBEROFATTEMPTS;
			}
			else
			{
				resCode = DCM_E_INVALIDKEY;
			}

			break;
		}
		bl_access_supply_clear_attemptCount();  //校驗成功，清除次數
		*respSize = 0x00u;
		Rte_SetMutexDownStatus(&g_DownContext,
								DCM_STATUS_SECURITY_UNLOCK_3,
								DCM_SECURITYACCESS_3_MASK);   //从0x512 变成 0x212

	}while(0);/*lint !e717*/
	#endif
	resCode = DCM_E_SUBFUNCTIONNOTSUPPORTEDINACTIVESESSION;
	return resCode;
}


/**************************************************************************//**
 *
 *  \details Callback of enable or disable communication.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval DCM_E_POSITIVERESPONSE   - Positive response code.
 *  \retval DCM_E_REQUESTOUTOFRANGE - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*lint -e{818}*/
bl_ResponseCode_t Adpt_UdsCallback28(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;

	(void)size;
	/*if(ADPT_COMMCONTROLTYPE_DATA != buffer[0])*/
	if((ADPT_COMMCONTROLTYPE_DATA != buffer[0]) && (0x3u != buffer[0]))   // buffer[0]！=ADPT_COMMCONTROLTYPE_DATA是01 且不等於03
	{
		resCode = DCM_E_REQUESTOUTOFRANGE;  //回復 31
	}
	else
	{
		*respSize = 0x00u;
	}


	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of writting fingerprint.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*lint -e{818}*/  //F198
/*  software detailed design ID: BSWDD110238*/
bl_ResponseCode_t Adpt_UdsCallBackWriteFingerprint(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
	//bl_Return_t ret;
	u8 l_crc;
	(void)size;
	/*write fingerprint data.*/
	#if (LBM_FUN_SAVE_FINGERPRINT == BL_FUN_ON)
	//(void)IntEeprom_WriteBytes(EEPROM_START_ADDRESS, buffer, RTE_FINGERPRINT_SIZE);
	#endif
	l_crc = crc_8_make(buffer,SYSDID_F198_REPAIR_SN_LEN);
	buffer[SYSDID_F198_REPAIR_SN_LEN] = l_crc;
	(void)cal_write_did(0xf198u,(u8*)&buffer[0],SYSDID_F198_REPAIR_SN_CK_LEN);
	//eeprom_write_u8(SYSDID_F198_REPAIR_SN_ADDR,buffer,SYSDID_F198_REPAIR_SN_LEN);
	Rte_SetDownStatus(&g_DownContext, RTE_SYSFLAG_WRITE_FINGPRINT);
	*respSize = 0u;
	resCode = DCM_E_POSITIVERESPONSE;
	return resCode;
}


bl_ResponseCode_t Adpt_UdsCallBackWriteProgrammingDate(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
//    bl_Return_t ret;
	u8 l_crc;
	bl_u8_t l_result;
	(void)size;
	/*write PGM date TO RAM  */
	#if (LBM_FUN_SAVE_PROGRAMMING_DATE == BL_FUN_ON)
	//(void)IntEeprom_WriteBytes(EEPROM_START_ADDRESS+0x14, buffer, RTE_PGM_DATE_SIZE);
	//(void)EEPROM_SetWord((EEPROM_TAddress)(EEPROM_DATA_BASE_ADDR+EEPROM_START_ADDRESS+0x14), ((bl_u16_t)(buffer[0]<<8))|buffer[0]);
	//(void)EEPROM_SetWord((EEPROM_TAddress)(EEPROM_DATA_BASE_ADDR+EEPROM_START_ADDRESS+0x16), ((bl_u16_t)(buffer[2]<<8))|buffer[3]);
	#endif
	l_crc = crc_8_make(buffer,SYSDID_F199_DATE_LEN);
	buffer[SYSDID_F199_DATE_LEN] = l_crc;
	(void)cal_write_did(0xf199u,(u8*)&buffer[0],SYSDID_F199_DATE_CK_LEN);
	//eeprom_write_u8(SYSDID_F199_DATE_ADDR,buffer,SYSDID_F199_DATE_LEN);
	l_result = check_date_valid(buffer);
	if(l_result == FALSE)
	{
		resCode = DCM_E_REQUESTOUTOFRANGE;
		//*respSize = 0u;
	}
	else
	{
		Rte_SetDownStatus(&g_DownContext, RTE_SYSFLAG_WRITE_OTHER);
		*respSize = 0u;
		resCode = DCM_E_POSITIVERESPONSE;
	}
	return resCode;
}



/**************************************************************************//**
 *
 *  \details Callback of writting fingerprint.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*  software detailed design ID: BSWDD110240*/
bl_ResponseCode_t Adpt_UdsCallBackWrite_F18C(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
//    bl_Return_t ret;
	u8 l_crc;
	(void)size;
	/*write fingerprint data.*/
	#if (LBM_FUN_SAVE_FINGERPRINT == BL_FUN_ON)
	//(void)IntEeprom_WriteBytes(EEPROM_START_ADDRESS, buffer, RTE_FINGERPRINT_SIZE);
	#endif
	l_crc = crc_8_make(buffer,SYSDID_F18C_LEN);
	buffer[SYSDID_F18C_LEN] = l_crc;
	(void)cal_write_did(0xf18cu,(u8*)&buffer[0],SYSDID_F18C_CK_LEN);
	//Rte_SetDownStatus(&g_DownContext, RTE_SYSFLAG_WRITE_FINGPRINT);
	*respSize = 0u;
	resCode = DCM_E_POSITIVERESPONSE;
	return resCode;
}



/**************************************************************************//**
 *
 *  \details Callback of writting fingerprint.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*  software detailed design ID: BSWDD110241*/
bl_ResponseCode_t Adpt_UdsCallBackWrite_F190(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
//    bl_Return_t ret;
	u8 l_crc;
	(void)size;
	/*write fingerprint data.*/
	#if (LBM_FUN_SAVE_FINGERPRINT == BL_FUN_ON)
	//(void)IntEeprom_WriteBytes(EEPROM_START_ADDRESS, buffer, RTE_FINGERPRINT_SIZE);
	#endif
	l_crc = crc_8_make(buffer,SYSDID_F190_VIN_LEN);
	buffer[SYSDID_F190_VIN_LEN] = l_crc;
	(void)cal_write_did(0xf190u,(u8*)&buffer[0],SYSDID_F190_VIN_CK_LEN);
	//eeprom_write_u8(SYSDID_F190_VIN_ADDR,buffer,SYSDID_F190_VIN_LEN);
	*respSize = 0u;
	resCode = DCM_E_POSITIVERESPONSE;
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of writting fingerprint.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*  software detailed design ID: BSWDD110242*/
bl_ResponseCode_t Adpt_UdsCallBackWrite_01a0_manulifecount(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
    	bl_Return_t ret;
	u8 l_buf[2];
	(void)size;
	/*write fingerprint data.*/
	#if (LBM_FUN_SAVE_FINGERPRINT == BL_FUN_ON)
	//(void)IntEeprom_WriteBytes(EEPROM_START_ADDRESS, buffer, RTE_FINGERPRINT_SIZE);
	#endif
	#if 0
	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_SUPPLY);
	if(BL_ERR_OK == ret)
	{
		l_buf[0]=buffer[0];
		l_buf[1] = crc_8_make(&l_buf[0],1);  //crc
		(void)cal_write_did(0x01a0u,(u8*)&l_buf[0],SYSDID_MANUFAC_LIFECYCLE_COUNT_CK_LEN);
	 	security_access_set_mclf_cycle_counter(l_buf[0]);
		*respSize = 0u;
		resCode = DCM_E_POSITIVERESPONSE;
	}
	else
	{
		if(buffer[0] ==SERVICE_27_MANULF_CYC_COUNT_VALID)  //只能写入0
		{
			l_buf[0]=buffer[0];
			l_buf[1] = crc_8_make(&l_buf[0],1);  //crc
			(void)cal_write_did(0x01a0u,(u8*)&l_buf[0],SYSDID_MANUFAC_LIFECYCLE_COUNT_CK_LEN);
		 	security_access_set_mclf_cycle_counter(l_buf[0]);
			*respSize = 0u;
			resCode = DCM_E_POSITIVERESPONSE;
		}
		else
		{
			*respSize = 0u;
			resCode = DCM_E_REQUESTOUTOFRANGE;
		}
	}
	#endif
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of writting fingerprint.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
//static u8 g_buf_backdoo[16];
/*  software detailed design ID: BSWDD110242*/
bl_ResponseCode_t Adpt_UdsCallBackWrite_fe01_backdoo_access_key(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
    bl_Return_t ret;
	//u8 l_buf[8];
	//u8 i;
	bl_FlashDrvParam_t l_bl_flash_drv1;
	(void)size;

	/*write fingerprint data.*/
	#if (LBM_FUN_SAVE_FINGERPRINT == BL_FUN_ON)
	//(void)IntEeprom_WriteBytes(EEPROM_START_ADDRESS, buffer, RTE_FINGERPRINT_SIZE);
	#endif
	ret = Rte_IsValidStatus(&g_DownContext, DCM_STATUS_SESSION_SUPPLY);
	if(BL_ERR_OK == ret)
	{
		l_bl_flash_drv1.size = 8u;
		l_bl_flash_drv1.FeedDog = g_RteInfo.wdgOps->Feed;//(void*)0;
		l_bl_flash_drv1.buffer = buffer;
		#if 0
		Flash_backdoor(&l_bl_flash_drv1);
		if(l_bl_flash_drv1.errCode == BL_ERR_OK)
		{
			*respSize = 0u;
			resCode = DCM_E_POSITIVERESPONSE;
		}
		else
		{
			#if 0
			l_bl_flash_drv1.size = 0;
			Flash_unsecure(&l_bl_flash_drv1);
			if(l_bl_flash_drv1.errCode == BL_ERR_OK)
			{
				*respSize = 0u;
				resCode = DCM_E_INVALIDKEY;  //DCM_E_INVALIDKEY
			}
			else
			{
				*respSize = 0u;
				resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
			}
			#else
			*respSize = 0u;
			resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
			#endif
		}
		#else
			*respSize = 0u;
			resCode = DCM_E_POSITIVERESPONSE;
		#endif
	}
	else
	{
			*respSize = 0u;
			resCode = DCM_E_SECURITYACCESSDENIED;
	}

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of checking programming contiditions.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/

bl_ResponseCode_t Adpt_UdsCallbackCheckProgramCond(bl_BufferSize_t size,
												   bl_Buffer_t *buffer,
												   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;

	(void)size;

	*respSize = 0x01u;
	buffer[0] = ADPT_ROUTINE_CORRECT_RESULT;

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of erasing memory.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallbackEraseMemory(bl_BufferSize_t size,
											  bl_Buffer_t *buffer,
											  bl_BufferSize_t *respSize)
{
	bl_Size_t eraseSize;
	bl_Address_t address;
	bl_ResponseCode_t resCode = DCM_E_CONDITIONSNOTCORRECT;
	bl_Return_t ret;

	(void)size;

	if((bl_Buffer_t)ADPT_MEMORY_LENANDADDR_FORMAT
		== buffer[ADPT_UDS_ERASE_MEMORY_FORMAT_POS])
	{
		#if 1
        address = BL_BE32_TO_MCU(&buffer[ADPT_UDS_ERASE_MEMORY_ADDRESS_POS]);
        eraseSize = BL_BE32_TO_MCU(&buffer[ADPT_UDS_ERASE_MEMORY_SIZE_POS]);
		#else
		address = LBM_LB1_ADDRESS;
		eraseSize = LBM_LB1_SIZE;
		#endif
		gs_UdsPrivateData.serviceId = ADPT_UDS_31_SERVICE_ID;
		_Adpt_TimeOutCallBack(ADPT_UDS_31_SERVICE_ID);  //負響應78

		ret = Adpt_EraseLB(&g_DownContext, address, eraseSize);
		if(BL_ERR_OK == ret)
		{
			buffer[0] = ADPT_ROUTINE_CORRECT_RESULT;
		}
		else
		{
			Rte_ClearDownStatus(&g_DownContext, ADPT_UDS_STATUS_MASK);
            buffer[0] = ADPT_ROUTINE_INCORRECT_RESULT; //*buffer = ADPT_ROUTINE_INCORRECT_RESULT;  //
		}
		*respSize = 0x01u;
		resCode = DCM_E_POSITIVERESPONSE;
	}
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of checksum.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallbackCheckSum(bl_BufferSize_t size,
										   bl_Buffer_t *buffer,
										   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	gs_UdsPrivateData.serviceId = ADPT_UDS_31_SERVICE_ID;
	_Adpt_TimeOutCallBack(ADPT_UDS_31_SERVICE_ID);

	ret = Adpt_VerifyData(&g_DownContext, (bl_Size_t)size, buffer);   //这里需要改动，因为接口变了。
	if(BL_ERR_OK == ret)
	{
		buffer[0] = ADPT_ROUTINE_CORRECT_RESULT;  //正确
	}
	else if(BL_ERR_USER_CODE_01 == ret)
	{
		buffer[0] = ADPT_ROUTINE_INCORRECT_SIG_RESULT;	//数字签名有错 2
	}
	else
	{
		buffer[0] = ADPT_ROUTINE_INCORRECT_RESULT;  //校验有错  1
	}

	Rte_ClearDownStatus(&g_DownContext, ADPT_UDS_STATUS_MASK);

	*respSize = 0x01u;

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of checking compatibility.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallbackCheckCompatibility(bl_BufferSize_t size,
													 bl_Buffer_t *buffer,
													 bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	(void)size;

	ret = Adpt_CheckCompatibility();
	if(BL_ERR_OK == ret)
	{
		buffer[0] = ADPT_ROUTINE_CORRECT_RESULT;
	}
	else
	{
		buffer[0] = ADPT_ROUTINE_INCORRECT_RESULT;
	}

	_Adpt_UdsClearDownInfo(&gs_UdsPrivateData);

	*respSize = 0x01u;

	return resCode;
}

#if (RTE_FUN_STAY_IN_BOOT == BL_FUN_ON)
/**************************************************************************//**
 *
 *  \details Callback of stayinboot.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallbackStayInBoot(bl_BufferSize_t size,
											 bl_Buffer_t *buffer,
											 bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;

	(void)buffer;
	(void)size;

	*respSize = 0x00u;

	return resCode;
}
#endif

/**************************************************************************//**
 *
 *  \details Callback of requesting download.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback34(bl_BufferSize_t size,
									 bl_Buffer_t *buffer,
									 bl_BufferSize_t *respSize)
{
	bl_Size_t reqSize;
	bl_Address_t addr;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	(void)size;
	tFlashOptInfo *l_pstFlashOptInfo = (void *)0;
	l_pstFlashOptInfo = (tFlashOptInfo *)flashDriverStartAdd;

	do
	{
		#ifdef FLASH_DRIVER_FAILED_RES_NES_FUNC
		if(g_DownContext.lbId == /*LBM_LB1_ID_APP*/0)
		{
			if( (l_pstFlashOptInfo->magic != FLASH_DRIVER_VERSION_MAGIC ) || (l_pstFlashOptInfo->version != FLASH_DRIVER_VERSION_VERSION) )
			{
				resCode = DCM_E_GENERALREJECT;
				break;
			}
		}
		#endif
		ret = Rte_IsValidStatus(&g_DownContext, ADPT_STATUS_ALLOW_TRANS_DATA);
		if(BL_ERR_OK == ret)
		{
			resCode = DCM_E_REQUESTSEQUENCEERROR;
			break;
		}

		if((ADPT_DATAFORMAT_IDEN != buffer[ADPT_UDS_34_SERVICE_IDEN_POS])
			|| (ADPT_MEMORY_LENANDADDR_FORMAT
					!= buffer[ADPT_UDS_34_SERVICE_ADDRANDSIZE_POS]))
		{
			resCode = DCM_E_CONDITIONSNOTCORRECT;
			break;
		}

		addr = BL_BE32_TO_MCU(&buffer[ADPT_UDS_34_SERVICE_ADDRESS_POS]);
		reqSize = BL_BE32_TO_MCU(&buffer[ADPT_UDS_34_SERVICE_SIZE_POS]);  /*PRQA S 1840*/

		ret = Adpt_ActivateDownload(&g_DownContext, addr, reqSize);
		if(BL_ERR_OK == ret)
		{
			buffer[ADPT_UDS_34_RESPONSE_FORMAT_POS] = ADPT_LENFORMAT_IDEN;

#if(1u == ADPT_IDEN_LEN_FORMAT)
			/*get maxnum of block length.*/
			buffer[ADPT_UDS_34_RESPONSE_SIZE_POS] = ADPT_MAXNUM_OF_BLOCKLENGTH;
#elif (2u == ADPT_IDEN_LEN_FORMAT)
			BL_MCU_TO_BE16(&buffer[ADPT_UDS_34_RESPONSE_SIZE_POS],
							ADPT_MAXNUM_OF_BLOCKLENGTH);
#else
#error "ADPT_IDEN_LEN_FORMAT is error."
#endif

			gs_UdsPrivateData.address = addr;
			gs_UdsPrivateData.remainSize = reqSize;
			gs_UdsPrivateData.blockIndex = 0x01u;

			*respSize = ADPT_REQDOWNLOAD_RESPLENGTH;

			Rte_SetDownStatus(&g_DownContext, ADPT_STATUS_ALLOW_TRANS_DATA);
		}
		else
		{
			resCode = DCM_E_GENERALREJECT;
		}
	}while(0);/*lint !e717*/

	if (resCode != DCM_E_POSITIVERESPONSE)
	{
		_Adpt_UdsClearDownInfo(&gs_UdsPrivateData);
	}
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of transferring data.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
/*lint -e{818}*/
bl_ResponseCode_t Adpt_UdsCallback36(bl_BufferSize_t size,
									 bl_Buffer_t *buffer,	//這裡的數據，己經去掉了sid 或者 sudid did等
									 bl_BufferSize_t *respSize)
{
	bl_u8_t expendIndex;
	bl_Size_t dataSize;
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	dataSize = (bl_Size_t)((bl_Size_t)size - 1u); /*overflow checking is not required.*/

	do
	{
		ret = Rte_IsValidStatus(&g_DownContext, ADPT_STATUS_ALLOW_TRANS_DATA);
		if (ret != BL_ERR_OK)
		{
			resCode = DCM_E_REQUESTSEQUENCEERROR;
			break;
		}

		expendIndex = gs_UdsPrivateData.blockIndex;

		ret = Rte_IsValidStatus(&g_DownContext,ADPT_STATUS_SEGMENT_FIRST_DATA);
		if(BL_ERR_NOT_OK == ret)
		{
			if (buffer[ADPT_UDS_36_SERVICE_INDEX_POS] != expendIndex)
			{
				resCode = DCM_E_WRONGBLOCKSEQUENCECOUNTER;  //顺序
				break;
			}
			/*set first 0x36 service of a download segment.*/
			Rte_SetDownStatus(&g_DownContext, ADPT_STATUS_SEGMENT_FIRST_DATA);
		}

#if(ADPT_FUN_FIRST_DATA_PREPROCESS == BL_FUN_ON)
		ret = Rte_IsValidStatus(&g_DownContext, ADPT_STATUS_BLOCK_FIRST_DATA);
		if(BL_ERR_NOT_OK == ret)
		{

			/*set first 0x36 service of a logical block.*/
			Rte_SetDownStatus(&g_DownContext, ADPT_STATUS_BLOCK_FIRST_DATA);

			App_PreprocessData();
		}
#endif

		/*check whether the SN is valid.*/
		if(buffer[ADPT_UDS_36_SERVICE_INDEX_POS] != expendIndex)
		{
			if (buffer[ADPT_UDS_36_SERVICE_INDEX_POS] != (expendIndex - 1u))
			{
				resCode = DCM_E_WRONGBLOCKSEQUENCECOUNTER;
			}
			else
			{
				/*repeat request*/
				resCode = DCM_E_POSITIVERESPONSE;
			}
			break;
		}

		/*Check the size of received data.*/
		if(gs_UdsPrivateData.remainSize > ADPT_MAXNUM_OF_TRANS_DATA)
		{
			/*It is not the last 0x36 service.*/
			if(ADPT_MAXNUM_OF_TRANS_DATA != dataSize)
			{
				resCode = DCM_E_INCORRECTMESSAGELENGTHORINVALIDFORMAT;
				break;
			}
		}
		else if(gs_UdsPrivateData.remainSize != dataSize)
		{
			resCode = DCM_E_INCORRECTMESSAGELENGTHORINVALIDFORMAT;
			break;
		}
		else
		{
			/*last service and data size is ok.*/
		}

		/*send 0x78.*/
		gs_UdsPrivateData.serviceId = ADPT_UDS_36_SERVICE_ID;
		_Adpt_TimeOutCallBack(ADPT_UDS_36_SERVICE_ID);
		#ifdef UDS_DEBUG_EN
		g_usd_test.data[0]++;
		#endif
		ret = Adpt_DownloadData(&g_DownContext,
									gs_UdsPrivateData.address,
									dataSize,
									&buffer[ADPT_UDS_36_SERVICE_DATA_POS]);
		if(BL_ERR_OK != ret)
		{
			resCode = DCM_E_GENERALPROGRAMMINGFAILURE;
			break;
		}
		else
		{
			*respSize = 0x01u;
			gs_UdsPrivateData.blockIndex++;
			gs_UdsPrivateData.address += dataSize;
			gs_UdsPrivateData.remainSize -= dataSize;
		}
	}while(0);/*lint !e717*/

	if (resCode != DCM_E_POSITIVERESPONSE)
	{
		_Adpt_UdsClearDownInfo(&gs_UdsPrivateData);
	}

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of requesting transfer exit.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback37(bl_BufferSize_t size,
									 bl_Buffer_t *buffer,
									 bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;
	bl_Return_t ret;

	(void)buffer;
	(void)size;

	ret = Rte_IsValidStatus(&g_DownContext,
								ADPT_STATUS_ALLOW_TRANS_DATA);
	if(BL_ERR_OK != ret)
	{
		_Adpt_UdsClearDownInfo(&gs_UdsPrivateData);

		resCode = DCM_E_REQUESTSEQUENCEERROR;
	}
	else
	{
		Rte_ClearDownStatus(&g_DownContext,
								(ADPT_STATUS_SEGMENT_FIRST_DATA
								| ADPT_STATUS_ALLOW_TRANS_DATA));
		*respSize = 0x00u;
	}
	#ifdef UDS_DEBUG_EN
	g_usd_test.data[1]++;
	#endif
	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of keeping session.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval 0x00   - Positive response code.
 *  \retval others - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback3E(bl_BufferSize_t size,
									 bl_Buffer_t *buffer,
									 bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;

	(void)size;
	(void)buffer;

#if(RTE_FUN_GATEWAY == BL_FUN_ON)
		_Adpt_UdsGatewayTransmit(ADPT_UDS_3E_SERVICE_ID);
#endif

	*respSize = 0x00u;

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Callback of switching on of off dtc.
 *
 *  \param[in]     size     - the data size, not include id, subid and did.
 *  \param[in/out] buffer   - the pointer of data.
 *  \param[out]    respSize - the size of response data.
 *
 *  \return Return response code.
 *
 *  \retval DCM_E_POSITIVERESPONSE   - Positive response code.
 *  \retval DCM_E_SUBFUNCTIONNOTSUPPORTED - Negative response code.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
bl_ResponseCode_t Adpt_UdsCallback85(bl_BufferSize_t size,
									   bl_Buffer_t *buffer,
									   bl_BufferSize_t *respSize)
{
	bl_ResponseCode_t resCode = DCM_E_POSITIVERESPONSE;

	(void)size;
	(void)buffer;

	*respSize = 0x00u;

	return resCode;
}

/**************************************************************************//**
 *
 *  \details Timeout callback of sending 0x78.
 *
 *  \param[in]  id  - service id.
 *
 *  \since  V3.0.0
 *  是靠 Rte_ReportEvent(RTE_EVENT_TYPE_MEMOPS_TIMEOUT);  //要發送 78負響應 調用 的
 *****************************************************************************/
void Adpt_UdsTimeOutCallBack(void)
{
	#ifdef UDS_DEBUG_EN
	g_usd_test.time[4]++;
	if(g_usd_test.time[4]>=1000)
	{
		g_usd_test.time[4]=0;
		g_usd_test.time[5]++;
	}
	#endif
	_Adpt_TimeOutCallBack(gs_UdsPrivateData.serviceId);

	return ;
}


//
void Adpt_Res_default_ValidProcess(void)
{
	bl_u8_t i;
	bl_u8_t l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	Dcm_StartS3Timer();

	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SESSION_DEFAULT,
							DCM_SESSION_MASK);

	for(i=0;i<8;i++)
	{
		l_buf[i] = gs_DefaultResponse[i];
	}
	for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
	{
		l_buf[i] = CANTP_FRAME_PADDING_VALUE;
	}
	_Adpt_SendResponse((bl_BufferSize_t)sizeof(l_buf),l_buf);

	return ;
}

/**************************************************************************//**
 *
 *  \details When the reprogram flag is valid, the RTE module call this
 *           function.
 *
 *  \since  V3.0.0
 *  好像只被用到一次；如果從app 跳轉到 boot 則要響應 0x06u,0x50u,0x02u,
 *****************************************************************************/
void Adpt_ReprogramValidProcess(void)
{
	bl_u8_t i;
	bl_u8_t l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	Dcm_StartS3Timer();

	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SESSION_PROGRAMMING,
							DCM_SESSION_MASK);

	for(i=0;i<8;i++)
	{
		l_buf[i] = gs_ReprogrammingResponse[i];
	}
	for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
	{
		l_buf[i] = CANTP_FRAME_PADDING_VALUE;
	}
	_Adpt_SendResponse((bl_BufferSize_t)sizeof(l_buf),l_buf);

	return ;
}

/**************************************************************************//**
 *
 *  \details When the reprogram flag is valid, the RTE module call this
 *           function.
 *
 *  \since  V3.0.0
 *  好像只被用到一次；如果從app 跳轉到 boot 則要響應 0x06u,0x50u,0x02u,
 *****************************************************************************/
void Adpt_ReprogramValid_nores_Process(void)
{
	#ifdef SERVICE_36_BLOCK_REPEATE
	g_36_block_repeat_count=0;
	#endif

	Dcm_StartS3Timer();

	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SESSION_PROGRAMMING,
							DCM_SESSION_MASK);
	#if 0
	_Adpt_SendResponse((bl_BufferSize_t)sizeof(gs_ReprogrammingResponse),
						gs_ReprogrammingResponse);
	#endif
	return ;
}

/**************************************************************************//**
 *
 *  \details When the reset flag is valid, the RTE module call this function.
 *
 *  \since  V3.0.0
 *  0x02u,0x51u,0x01u  好像目前沒有用到
 *****************************************************************************/
void Adpt_SystemResetProcess(void)
{
	u8 i;
	u8 l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	for(i=0;i<8;i++)
	{
		l_buf[i] = gs_ResetResponse[i];
	}
	for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
	{
		l_buf[i] = CANTP_FRAME_PADDING_VALUE;
	}
	_Adpt_SendResponse((bl_BufferSize_t)sizeof(gs_ResetResponse),gs_ResetResponse);

	return ;
}

#if (RTE_FUN_STAY_IN_BOOT == BL_FUN_ON)
/**************************************************************************//**
 *
 *  \details When the application is valid and the stay in boot is enabled,
 *           the RTE module call this function.
 *
 *  \since  V3.0.0
 *  0x04u,0x71u,0x01u F5 18
 *****************************************************************************/
void Adpt_AppValidProcess(void)
{
	bl_u8_t i;
	bl_u8_t l_buf[ADPT_UDS_MAX_CAN_FRAME_SIZE];
	for(i=0;i<8;i++)
	{
		l_buf[i] = gs_StayInBootResponse[i];
	}
	for(i=8;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
	{
		l_buf[i] = CANTP_FRAME_PADDING_VALUE;
	}
	_Adpt_SendResponse((bl_BufferSize_t)sizeof(l_buf),l_buf);

	return ;
}
#endif

#if(RTE_FUN_GATEWAY == BL_FUN_ON)
/**************************************************************************//**
 *
 *  \details Gateway transmits messages by id.
 *
 *  \param[in]  id  - service id.
 *
 *  \since  V5.0.0
 *
 *****************************************************************************/
static void _Adpt_UdsGatewayTransmit(bl_u8_t id)
{
	bl_u8_t i;
	bl_u16_t handle;
	bl_u16_t number;
	const bl_GatewayConversion_t *gateway;

	number = gs_UdsGatewayList.number;
	gateway = gs_UdsGatewayList.data;
	handle = gs_UdsGatewayList.handle;

	for(i = 0u; i < number; i++)
	{
		if(id == gateway[i].sid)
		{
			(void)Canif_Write(handle, gateway[i].size, gateway[i].data);
			break;
		}
	}

	return ;
}
#endif

/**************************************************************************//**
 *
 *  \details Send 0x78 negative response.
 *
 *  \param[in]  id  - service id.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
static void _Adpt_UdsPendingResponse(bl_u8_t id)
{
	u8 i;
	#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8

		bl_Buffer_t buffer[ADPT_UDS_MAX_CAN_FRAME_SIZE];

		buffer[ADPT_UDS_CAN_FRAME_INDEX_0] = DCM_NEGATIVE_RESPONSE_SIZE;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_1] = DCM_NEGATIVE_RESPONSE_CODE;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_2] = id;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_3] =
									DCM_E_REQUESTCORRECTLYRECEIVED_RESPONSEPENDING;
		#if(CANTP_FUN_FRAME_PADDING == BL_FUN_ON)
			for(i=ADPT_UDS_CAN_FRAME_INDEX_4;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
			{
				buffer[i] = CANTP_FRAME_PADDING_VALUE;
			}
		#endif
	#else

		bl_Buffer_t buffer[ADPT_UDS_MAX_CAN_FRAME_SIZE];

		buffer[ADPT_UDS_CAN_FRAME_INDEX_0] = 0x00;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_1] = DCM_NEGATIVE_RESPONSE_SIZE;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_2] = DCM_NEGATIVE_RESPONSE_CODE;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_3] = id;
		buffer[ADPT_UDS_CAN_FRAME_INDEX_4] =
									DCM_E_REQUESTCORRECTLYRECEIVED_RESPONSEPENDING;
		#if(CANTP_FUN_FRAME_PADDING == BL_FUN_ON)
			for(i=ADPT_UDS_CAN_FRAME_INDEX_5;i<ADPT_UDS_MAX_CAN_FRAME_SIZE;i++)
			{
				buffer[i] = CANTP_FRAME_PADDING_VALUE;
			}
		#endif

	#endif

	_Adpt_SendResponse((bl_BufferSize_t)sizeof(buffer), buffer);

	return ;
}

/**************************************************************************//**
 *
 *  \details Send a response by ComOps in the g_RteInfo. When the transmitter
 *           is activated, This function will wait for some time configurated
 *           by ADPT_UDS_TX_TIMEOUT. If the response is not send within this
 *           time, this function will still return.
 *
 *  \param[in]  size    - the size of buffer.
 *  \param[in]  buffer  - the data will be send.
 *
 *  \since  V3.0.0
 *
 *****************************************************************************/
static void _Adpt_SendResponse(bl_BufferSize_t size, const bl_Buffer_t *buffer)
{
	#if ADPT_UDS_MAX_CAN_FRAME_SIZE <=8
	bl_u32_t timeout = ADPT_UDS_TX_TIMEOUT;
	#else
	bl_u32_t timeout = ADPT_UDS_CANFD_TX_TIMEOUT;
	#endif

	/*whether return value is necessary*/
	(void)Canif_Write(ADPT_UDS_TX_HANDLE, size, buffer);

	BL_WAITFOR_TIMEOUT(timeout,
						Canif_IsWritten(ADPT_UDS_TX_HANDLE) == BL_ERR_OK)

	return ;
}
/**************************************************************************//**
 *
 *  \details Timeout callback of sending 0x78.
 *
 *  \param[in]  id  - service id.
 *
 *  \since  V2.0.0
 *  負響應  78
 *****************************************************************************/
static void _Adpt_TimeOutCallBack(bl_u8_t id)
{
	_Adpt_UdsPendingResponse(id);

#if(RTE_FUN_GATEWAY == BL_FUN_ON)
	_Adpt_UdsGatewayTransmit(ADPT_UDS_3E_SERVICE_ID);
#endif
}

/**************************************************************************//**
 *
 *  \details Set response info of session.
 *
 *  \param[out]  buffer  - the pointer of response data.
 *
 *  \return Return the size of response data.
 *
 *  \since  V4.0.0
 *  會話的應答時間參數
 *****************************************************************************/
static bl_BufferSize_t _Adpt_SetSessionResp(bl_Buffer_t *buffer)
{
	bl_BufferSize_t size;

	size = ADPT_SESSION_RESPONSE_LEN;

	buffer[ADPT_UDS_CAN_FRAME_INDEX_0] = ADPT_SESSION_RESPONSE0;
	buffer[ADPT_UDS_CAN_FRAME_INDEX_1] = ADPT_SESSION_RESPONSE1;
	buffer[ADPT_UDS_CAN_FRAME_INDEX_2] = ADPT_SESSION_RESPONSE2;
	buffer[ADPT_UDS_CAN_FRAME_INDEX_3] = ADPT_SESSION_RESPONSE3;

	return size;
}

/**************************************************************************//**
 *
 *  \details Go to the session
 *
 *  \param[in] sessionMask - the session will be gone.
 *
 *  \since  V5.0.0
 *
 *****************************************************************************/
static void _Adpt_UdsGotoSession(bl_DownStatus_t sessionMask)
{
	_Adpt_UdsClearDownInfo(&gs_UdsPrivateData);  /*
													adpt->address = 0;
													adpt->remainSize = 0;
													adpt->blockIndex = 0;
													再清 g_DownContext.state[0-1]
													state[0]一般指#define RTE_SYSFLAG_SECURITY_ACCESS     (0x00000001UL)
														#define RTE_SYSFLAG_CHECK_SIGNATURE     (0x00000002UL)
														#define RTE_SYSFLAG_ERASE_BLOCK         (0x00000004UL)
														#define RTE_SYSFLAG_WRITE_DATA          (0x00000008UL)
														#define RTE_SYSFLAG_WRITE_FINGPRINT     (0x00000010UL)  //寫入金手指，具體是什麼？
														#define RTE_SYSFLAG_WRITE_OTHER         (0x00000020UL)
													state[1]一般指 RTE_USER_FLAG_GROUP_1
														下面包含 ALLOW_TRANS_DATA ；BLOCK_FIRST_DATA ； SEGMENT_FIRST_DATA ; SEGMENT_FIRST_DATA
														所有 DCM診斷相關
													*/

#if (DCM_FUN_SECURITY_ACCESS == BL_FUN_ON)
	/*lock the level 1.*/
	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SECURITY_LOCK_1,  //40000100
							DCM_SECURITYACCESS_1_MASK);  //40000700  相當於清除其余兩個 再置 DCM_STATUS_SECURITY_LOCK_1
	/*lock the level 1.*/
	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SECURITY_LOCK_2,  //40000100
							DCM_SECURITYACCESS_2_MASK);  //40000700  相當於清除其余兩個 再置 DCM_STATUS_SECURITY_LOCK_1
	/*lock the level 1.*/
	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SECURITY_LOCK_3,  //40000100
							DCM_SECURITYACCESS_3_MASK);  //40000700  相當於清除其余兩個 再置 DCM_STATUS_SECURITY_LOCK_1
#else
	/*unlock the level 1.*/
	Rte_SetMutexDownStatus(&g_DownContext,
							DCM_STATUS_SECURITY_UNLOCK_1,  //40000200
							DCM_SECURITYACCESS_1_MASK);  //40000700  相當於清除其余兩個 再置 DCM_SECURITYACCESS_1_MASK
#endif
	Rte_SetMutexDownStatus(&g_DownContext,
							sessionMask,    //
							DCM_SESSION_MASK);   //40000007 相當於清除其余兩個  再置sessionMask

	return ;
}

/**************************************************************************//**
 *
 *  \details Clear the download information.
 *
 *  \since  V5.1.0
 *
 *****************************************************************************/
static void _Adpt_UdsClearDownInfo(bl_AdapterData_t *adpt)
{
	adpt->address = 0;
	adpt->remainSize = 0;
	adpt->blockIndex = 0;

	Rte_ClearDownStatus(&g_DownContext, ADPT_UDS_SYSTEM_STATUS_MASK);  //1c  state[0]
																		/*
																		 (RTE_SYSFLAG_ERASE_BLOCK \  4
                                            								| RTE_SYSFLAG_WRITE_DATA)  //8
                                            								| RTE_SYSFLAG_WRITE_FINGPRINT 10
																		 */
	Rte_ClearDownStatus(&g_DownContext, ADPT_UDS_STATUS_MASK);  //78000000  state[1]
																/*
																	(ADPT_STATUS_ALLOW_TRANS_DATA \
																	| ADPT_STATUS_BLOCK_FIRST_DATA \
																	| ADPT_STATUS_SEGMENT_FIRST_DATA)  //78000000
																 */
	return ;
}

/**************************************************************************//**
 *
 *  \details Reset the system.
 *
 *  \since  V5.1.0
 *  好像沒有用到
 *****************************************************************************/
static void _Adpt_UdsResetSystem(bl_u8_t sid, bl_u16_t flagId)
{
	bl_Return_t ret;

	ret = Dcm_IsSuspendedResponse(); //gs_SupPosRespFlag==0
	if (ret != BL_ERR_OK)
	{
		_Adpt_TimeOutCallBack(sid);
		/*write flag.*/
		(void)Dm_WriteDefaultData(flagId);
	}

	g_RteInfo.SystemReset();
}








void  adpt_test_init(void)
{
	(void)Dm_WriteDefaultData(BOOTM_REPROGRAM_FLAG_DID);
}


void adpt_secutiry_valid_all(void)
{
	Rte_SetMutexDownStatus(&g_DownContext,
								DCM_STATUS_SECURITY_UNLOCK_1,
								DCM_SECURITYACCESS_1_MASK);   //从0x512 变成 0x212
	Rte_SetMutexDownStatus(&g_DownContext,
								DCM_STATUS_SECURITY_UNLOCK_2,
								DCM_SECURITYACCESS_2_MASK);   //从0x512 变成 0x212
}


u8 check_date_valid(u8 *p_date)
{
	u16 l_year;
	u8 l_result = TRUE;
	u8 l_data[4];
	u16 l_temp;

	#if 1

	l_data[0] = p_date[0];
	l_data[1] = p_date[1];
	l_data[2] = p_date[2];
	l_data[3] = p_date[3];

	/*日期合法的判斷*/
	if( (l_data[0]>0x99u) || (l_data[1]>0x99u) || (l_data[0]==0x00u) || (l_data[2]==0x00u) || (l_data[3]==0x00u) || (l_data[2]>0x12u) || (l_data[3]>0x31u) )
	{
		l_result = FALSE;
	}
	else if( ( (l_data[2]==0x04u) || (l_data[2]==0x06u)  || (l_data[2]==0x09u) || (l_data[2]==0x11u ) ) && (l_data[3]>0x30u) )
	{
		l_result = FALSE;
	}
	else if( ( (l_data[2]==0x01u) || (l_data[2]==0x03u)  || (l_data[2]==0x05u) || (l_data[2]==0x07u ) || (l_data[2]==0x08u )|| (l_data[2]==0x10u ) || (l_data[2]==0x12u )) && (l_data[3]>0x31u) )
	{
		l_result = FALSE;
	}
	else if( l_data[2]==0x02u)
	{

		l_temp = (u16)((u16)l_data[0] >> 4) ;
		l_temp *= 1000u;
		l_year = l_temp;
		l_temp =(u16)((u16)l_data[0] & 0x0fu);
		l_temp *= 100u;
		l_year += l_temp;
		l_temp =(u16)((u16)l_data[1] >> 4);
		l_temp *= 10u;
		l_year += l_temp;
		l_temp =(u16)((u16)l_data[1] & 0x0fu) ;
		l_year += l_temp;

		//l_year = (u16)(l_data[0] >> 4) * 1000 +  (u16)(l_data[0] & 0x0f) * 100 + (u16)(l_data[1] >> 4) * 10 +  (u16)(l_data[1] & 0x0f) ;
		if( ( (l_year%4u) ==0u) && ( (l_year%100u) != 0u) || ( (l_year%400u) ==0u) )    /*PRQA S 3397*/
		{
			l_temp = 0x29u;
		}
		else
		{
			l_temp = 0x28u;
		}

		if(l_data[3]>l_temp)
		{
			l_result = FALSE;
		}
		else
		{
			l_result = TRUE;
		}


	}
	else
	{
		//code
	}
	#endif

	return l_result;
}
