	
#ifndef DCM_APP_H
#define DCM_APP_H

/******************************************************************************
 * Description:     : Include File Section 									
******************************************************************************/

#include "Dcm_App_Cfg.h"
/******************************************************************************
 * Description:     : Macro Description Section
******************************************************************************/
#define SetMcu_SoftReset() 	do{WDTE = 0x00;}while(1)


#define DCM_PHYSICAL_ADDRESS                      0x01          /* 宏定寻址方式 */
#define DCM_FUNCTIONAL_ADDRESS                    0x02

#define DCM_DEFAULT_SESSION                       0x01       /* 默认模式 */
#define DCM_PROGRAMME_SESSION                     0x02       /* 编程模式 */
#define DCM_EXTEND_SESSION                        0x04       /* 扩展模式 */
#define DCM_DIAGNOSTIC_SESSION_DEFAULT             DCM_DEFAULT_SESSION             /* 上电默认诊断会话模式 */

#define DCM_SECURITYACCESS_L2_UNLOCK              0x07       /*解锁状态*/
#define DCM_SECURITYACCESS_L1_UNLOCK              0x06        /*L1解锁状态*/
#define DCM_SECURITYACCESS_DEFAULT                0x04        /*默认安全状态状态*/

#define DCM_SECURITY_LEVEL_DEFAULT				0x07
#define DCM_SECURITYACCESS_LEVEL_L1				0x03
#define DCM_SECURITYACCESS_LEVEL_L2				0x01
#define DCM_SECURITYACCESS_LEVEL_L11			0x03
#define DCM_SECURITYACCESS_LEVEL_L12			0x01

#define DCM_19SUPMASK                            0x09           /*19服务的有效掩码*/
#define DCM_15031_6_DTCFORMAT                     0x00           /*DTC格式*/

#define DCM_DTCSET_ENABLE                         1U        /*DTC设置使能*/
#define DCM_DTCSET_DISENABLE                      0U        /*DTC设置关闭*/

#define DCM_MSGTXRX_ENABLE                        1U        /*报文收发状态 使能*/
#define DCM_MSGTXRX_DISENABLE                     0U        /*报文收发状态 关闭*/

#define DCM_SASERVERSTS_DEFAULT                  0U       /*27服务状态机 默认状态 初始状态*/
#define DCM_SASERVERSTS_REQUESTKEY               (0x80)//1U       /*27服务状态机 已请求种子*/ 
#define DCM_SASERVERSTS_UNLOCK                   2U       /*27服务状态机 已解锁*/ 
#define DCM_SASERVERSTS_ERROR                    4U       /*27服务状态机 密钥错误延时*/ 


#define DCM_S3SEVERTIMER_STOP                     0U           /*S3ServerTime 计时停止*/
#define DCM_S3SEVERTIMER_CONTINUE                 1U           /*S3ServerTime 计时继续*/

#define DCM_DTCDATA_MAXLENGTH                     36           /*DTC列表最大数据量*/

#define DCM_TXBUFFER_LENGTH                       105U      /*发送buffer的长度*/          

#define DCM_SASERVER_KEYLENGTH                   4U      /*密钥长度*/
#define DCM_SASERVER_KEYERRORCOUNT               3U      /*允许密钥错误次数*/

#define DCM_RIDRESULT_SUCCESS                    0x04      /*历程处理成功*/
#define DCM_RIDRESULT_FAILURE                    0x05      /*历程处理失败*/

#define DCM_RIDRUNSTS_FINISH					 0x01
#define DCM_RIDRUNSTS_IDLE					 	 0x00

#define DCM_READDID_MAXNUMBER                     5U        /*22 一次性读取的DID 最大个数*/ 
#define DCM_WRITEDATA_MAXLENGTH                   20U        /*2E服务写入数据最大长度*/



#define DCM_SASERVERSTS_ERRORDELAY_COUNT          10000U   /*密钥错误延时*/ 
#define DCM_SASERVERSTS_ERRORDELAY_COUNT_DEFAULT  10000U   /*密钥错误延时 上电默认值*/  

#define DCM_S3SEVERTIMEOUT                       5000U       /*S3ServerTime计时超时计数量标定*/ 


#define DCM_DATAFOMATLDENTIFER                         0x00           /*34 服务参数  数据格式*/ 
#define DCM_ADDRESSANDLENGTHFOMATLDENTIFER             0x44           /*34 服务参数  地址和数据长度格式*/ 
#define DCM_DOWNLOAD_MINADDRESS                        0x00000000     /*34 服务参数  最小地址*/
#define DCM_DOWNLOAD_MAXADDRESS                        0xFFFFFFFF     /*34 服务参数  最大地址*/
#define DCM_DOWNLOAD_MAXDATALENGTH                     0xFFF          /*34 服务参数  最长数据长度*/
#define DCM_LENGTHFOMATLDENTIFER                       0x20           /*34 服务参数  正响应里的 数据长度格式*/ 
#define DCM_DOWNLOAD_RECEIVEDATALENGTH                 0x0400         /*36 服务 接收数据缓存数组长度*/


/***************************************************/
#define DCM_DS_FLASHDOWNLOAD_COMPLETE          0x02
#define DCM_DS_FLASHCRC_COMPLETE               0x03
#define DCM_DS_ERASE_COMPLETE                  0x04
#define DCM_DS_APPDOWNLOAD_REQUEST             0x05
#define DCM_DS_APPCRC_COMPLETE                 0x06


/******************************************************************************
 * Description:     : Structure Type Declaration Section
******************************************************************************/
/*@结构类型typedef定义*/
/*34 36 37  数据下载相关服务处理状态*/
typedef enum
{
  CeDcm_e_DownLoadRelax =0,
  CeDcm_e_DownLoadRequested,
  CeDcm_e_DownLoadDuringTransmit,
  CeDcm_e_DownLoadTransmited
}TeDcm_e_DownLoadStsType;

/*例程控制状态*/
typedef enum
{
  CeDcm_e_RidStart =0,
  CeDcm_e_RidStop,
  CeDcm_e_RidGetResult
}TeDcm_e_RidStsType;

typedef enum
{
	Rid_Idle,
	Rid_Finish
}RidRunSts_T;

/*TP数据分拣出的信息 完整信息*/
typedef struct
{
  uint16 DL;               						/*有效字节长度*/
  uint8  Sid;              						/*SID*/
  uint8  RequstSty;        						/*请求方式*/
  uint8 Sub;               						/*子服务*/
  uint8 DtcGroup[3];                  			/*DTC组*/
  uint8 StateMask;                   			/*请求的 状态掩码*/
  uint8 Key[DCM_SASERVER_KEYLENGTH];            /*密钥信息*/
  uint8 CommType;                    			/*通讯类型*/
  uint8 Did[DCM_READDID_MAXNUMBER*2];      		/*具体DID*/
  uint8 DidCount;                    			/*DID个数*/
  uint16 Rid;                     				/*RID*/
  uint8 WriteData[DCM_WRITEDATA_MAXLENGTH];     /*写入的数据*/
}TsDcm_h_RquestInfo;     

typedef enum{
	DCM_RESET_NOTREADY,
	DCM_RESET_READY
}DcmReset_T;

/******************************************************************************
 * Description:     : Global Variable Declaration Section
******************************************************************************/

/*调试暂用*/
extern uint8 TempTP[];

/*@全局变量声明extern*/
extern uint8* VeDcm_P_DataAddress;                                                          /*TP层数据地址*/
extern uint8 VeDcm_u_DL;                                                                    /*TP层数据长度*/
extern uint8 VeDcm_u_RequstSty;                                                             /*TP层数据请求方式*/
extern uint8 VaDcm_u_NegRspTxBuffer[3];
 
extern TsDcm_h_RquestInfo VeDcm_h_RqustInfo;



extern uint16 VeDcm_w_S3ServerTime;                            /*S3ServerTime计数*/
extern uint8 VeDcm_u_S3ServerTimeSts; 
extern uint8 VeDcm_u_DiagnosticSession;                        /*诊断会话模式*/
extern uint8 VeDcm_u_SecurityAccessState;                      /*安全权限状态*/



/******************************************************************************
 * Description:     : Global Function Prototype Declaration	
******************************************************************************/


/*@接口函数声明extern*/
extern uint8 MngDcm_CollectRequstInfo(void);

extern TeDcm_h_ResultType MngDcm_DefaultSessionDeal(void);
extern TeDcm_h_ResultType MngDcm_ProgramSessionDeal(void);
extern TeDcm_h_ResultType MngDcm_ExtendedSessionDeal(void);

extern TeDcm_h_ResultType MngDcm_SoftResetDeal(void);

extern TeDcm_h_ResultType MngDcm_CleanDTC(uint32 Group);

extern  TeDcm_h_ResultType MngDcm_DynamicaDefineNewDid(void);
extern   TeDcm_h_ResultType MngDcm_ClearDynamicaDid(void);

extern TeDcm_h_ResultType MngDcm_RequestSeekDeal(uint8 SecurityLevel);
extern TeDcm_h_ResultType MngDcm_KeyCkeckDeal(uint8 dummy);

extern  TeDcm_h_ResultType MngDcm_CtrlCommunication(uint8  CommunicationType);
extern TeDcm_h_ResultType MngDcm_DisenableCommunication(void);

extern TeDcm_h_ResultType MngDcm_RoutineFF02Start(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF02Stop(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF02Result(void);

extern TeDcm_h_ResultType MngDcm_Routine0202Start(void) ; 
extern TeDcm_h_ResultType MngDcm_Routine0202Stop(void)  ;   
extern TeDcm_h_ResultType MngDcm_Routine0202Result(void); 


extern TeDcm_h_ResultType MngDcm_TesterPresentDeal(void);

extern TeDcm_h_ResultType MngDcm_DTCSettingEnable(void);
extern TeDcm_h_ResultType MngDcm_DTCSettingDisenable(void);

extern void MngDcm_ChangeToDefaltSession(void);

extern void MngDcmS3ServerCountCutDown(void);

extern void MngDcmKeyErrorDelayCountCutDown(void);

extern void MngDcm_PrepareNegRsp(uint8 Sid,uint8 Nrc);

extern void MngDcm_DataMoveDeal(uint8* TargetAddress,uint8 TargetPost,uint8* SourceAddress,uint8 SourcePost,uint8 DataLength);

extern TeDcm_h_ResultType MngDcm_RequestDownloadService(void);
extern TeDcm_h_ResultType MngDcm_TransmitDataService(void);
extern TeDcm_h_ResultType MngDcm_TransmitExitService(void);

extern void InitDcm_Parameter (void);

extern uint16 MngDcm_GetS3serverTime(void);
extern uint8 MngDcm_GetS3serverTimeSts(void);
extern uint8 MngDcm_GetDiagnosticSession(void);
extern uint8 MngDcm_GetSecurityAccessSts(void);

extern TeDcm_h_ResultType MngDcm_RoutineFF00Start(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF01Start(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF00Stop(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF01Stop(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF00Result(void);
extern TeDcm_h_ResultType MngDcm_RoutineFF01Result(void);

extern  uint8 GetDtcEnable(void);

extern void MngDcm_ResetS3serverTime(void);

extern void BootProgramSessionDeal(void);
extern void BootDefaultSessionDeal(void);
extern void resetS3Server(void);

#endif
