
/******************************************************************************
|  File Name :  Dcm.c
|  Description: 诊断服务入口
|------------------------------------------------------------------------------
| (c) This software is the proprietary of Wenzhou Changjiang AutoMobile (DAMING)
|     All rights are reserved by DAMING.
|------------------------------------------------------------------------------
| Initials      Name                   Company
| --------      --------------------   ----------------------------------------
|                				       DAMING
|------------------------------------------------------------------------------
|               R E V I S I O N   H I S T O R Y
|------------------------------------------------------------------------------
| Date         Version    Author       Description
| ----------   --------   ------       ----------------------------------------
| 2015-10-10   1.0.1     chenlianwen   初始版
 *****************************************************************************/

/******************************************************************************
 * Description:     : Include File Section 									
******************************************************************************/
#include "Dcm.h"
#include "can_drv.h"

/******************************************************************************
 * Description:     : Global Variable Definition Section 									
 ******************************************************************************/
const uint16 PeriodicDidServeTxTmMax[5]={0xFFFF,1000,500,100,0xFFFF};


/******************************************************************************
 * Description:     : Static Variable Definition Section 									
 ******************************************************************************/


/******************************************************************************
 * Description:     : Static Function Prototype Declaration									
 *****************************************************************************/

 static void MngDcm_ConditionStateControl(void);
/******************************************************************************
 * Description:     : Write User Function Code								
 *****************************************************************************/


/******************************************************************************
 *函数名称    : void MngDcm_MainFunction(void)

 *参数        : void

 *返回值      : void

 *描述        : 诊断总入口函数

 *编辑时间    : 2015-10-10

 *备注        : 
 *****************************************************************************/
 void MngDcm_MainFunction(void)
{
    uint8 LeDcm_u_SidIndex;
    uint8 LeDcm_u_CheckSessionResult;
    uint8 LeDcm_u_CheckSecurityAccessResult;
    uint8 LeDcm_u_CheckAddressResult;
    uint8 LaDcm_u_TempBuffer[6];
    uint8 LeDcm_u_CheckMinDLResult;
    uint8 LeDcm_u_CollectInfoResult;
    TeDcm_h_ResultType LeDcm_u_FinallyResult;
    /*根据条件调整参数*/
    MngDcm_ConditionStateControl();
    
    /*获取TP层数据接口 DL SID 请求类型*/
    LeDcm_u_CollectInfoResult = MngDcm_CollectRequstInfo();
    
    if(DCM_CHECK_OK == LeDcm_u_CollectInfoResult)
    {
		MngDcm_ResetS3serverTime();             /*S3server 计时计数值重新装载*/
        LeDcm_u_SidIndex = MngDcm_SIDCheckAvailable(VeDcm_h_RqustInfo.Sid); /*检查SID是否支持*/
        if( DCM_NORESULT!=LeDcm_u_SidIndex)
        {
    
           LeDcm_u_CheckSessionResult = MngDcm_SIDCheckSession(LeDcm_u_SidIndex);   /*检查模式是否支持*/ 
           if(DCM_CHECK_OK == LeDcm_u_CheckSessionResult)
           {
       
              LeDcm_u_CheckSecurityAccessResult = MngDcm_SIDCheckSecurityAccess(LeDcm_u_SidIndex);  /*检查安全权限是否满足*/ 
              if(DCM_CHECK_OK == LeDcm_u_CheckSecurityAccessResult )
              {
                  if(DCM_FUNCTIONAL_ADDRESS == VeDcm_h_RqustInfo.RequstSty)
                  {
                       LeDcm_u_CheckAddressResult = MngDcm_SIDFuncAddressAvailable(LeDcm_u_SidIndex);  /*是否支持功能寻址*/
                       if(DCM_CHECK_NOK == LeDcm_u_CheckAddressResult) /*不支持*/
                       {
                           SetCanTp_TxBuffer(0U,LaDcm_u_TempBuffer);      /*调用接口发送响应数据 0数据为不响应*/
                           LeDcm_u_FinallyResult = CeDcm_e_ResultNoRsp; 
                       } 
                       else
                       {
                           LeDcm_u_CheckMinDLResult = MngDcm_SIDCheckMinDL(LeDcm_u_SidIndex,VeDcm_h_RqustInfo.DL);                    /*是否满足最小DL*/
                           if( DCM_CHECK_OK == LeDcm_u_CheckMinDLResult)
                          {
                             LeDcm_u_FinallyResult = CaDcm_h_SidCheckInfoTable[LeDcm_u_SidIndex].SidFunction();   /*调用SID入口函数*/
                           
                          }    
                          else
                          {
                         
                              //设置NRC13
                              MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC13]);    /*整理负响应数据*/
                              SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
                              LeDcm_u_FinallyResult = CeDcm_e_ResultNegRsp;
                         
                          }
                       } 
                  }
                  else
                  {
              
                      LeDcm_u_CheckMinDLResult = MngDcm_SIDCheckMinDL(LeDcm_u_SidIndex,VeDcm_h_RqustInfo.DL);                    /*是否满足最小DL*/
                      if( DCM_CHECK_OK == LeDcm_u_CheckMinDLResult)
                      {
                          LeDcm_u_FinallyResult = CaDcm_h_SidCheckInfoTable[LeDcm_u_SidIndex].SidFunction();   /*调用SID入口函数*/
                      }
                      else
                      {
                          //设置NRC13
                          MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC13]);    /*整理负响应数据*/
                          SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
                          LeDcm_u_FinallyResult = CeDcm_e_ResultNegRsp;
                      
                     
                      }
                  }
          
              }
              else
              {
                  MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC33]);    /*整理负响应数据*/
                  SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
                  LeDcm_u_FinallyResult = CeDcm_e_ResultNegRsp;
              }
          
           }
           else
           {
           
               //设置NRC7F
               MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC7F]);    /*整理负响应数据*/
               SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
               LeDcm_u_FinallyResult = CeDcm_e_ResultNegRsp;
           
           }
     
        }
        else
        {
            if(DCM_FUNCTIONAL_ADDRESS == VeDcm_h_RqustInfo.RequstSty)
            {
                SetCanTp_TxBuffer(0U,LaDcm_u_TempBuffer);      /*调用接口发送响应数据 0数据为不响应*/
                LeDcm_u_FinallyResult = CeDcm_e_ResultNoRsp; 
            
            }else
            {//设置NRC11
                MngDcm_PrepareNegRsp(VeDcm_h_RqustInfo.Sid,CaDcm_u_NRCData[DCM_NRC11]);    /*整理负响应数据*/
                SetCanTp_TxBuffer(3U,VaDcm_u_NegRspTxBuffer);      /*调用接口发送响应数据*/
                LeDcm_u_FinallyResult = CeDcm_e_ResultNegRsp;
            }
        }
    }else
    {
        
    }
   
    if(DCM_S3SEVERTIMER_CONTINUE == GetCanTp_u_S3Sts())
    {
        MngDcmS3ServerCountCutDown();        /*S3server 计时 和 密钥错误延时计时*/
    }
    
    MngDcmKeyErrorDelayCountCutDown();                 /*27 服务密钥错误次数到达后延时*/
	
	DtcDiagConditionChk();
	if(GetDtcEnable())
		CanDTC_Task();
}






/******************************************************************************
 *函数名称    : void MngDcm_ConditionStateControl(void)
 *参数        : void

 *返回值      : void

 *描述        : 根据当前条件(S3severtime等)调整状态参数

 *编辑时间    : 2015-10-10

 *备注        : 
 *****************************************************************************/
void MngDcm_ConditionStateControl(void)
{
    if(0U == MngDcm_GetS3serverTime())
    {
       MngDcm_ChangeToDefaltSession();      
    }
    
}


	
