#include "UDS_27Service_Filter.h"

/**
  *27服务是否支持功能寻址
  *功能寻址下屏蔽该负反馈
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC11(void)
{
    uint8_t NRC = positiveResponse;
    if(DiagMSG.msgID == DIAG_ID_Rx_FUN)
    {
        NRC = serviceNotSupported;
    }
    return NRC;
}

/**
  *27服务会话是否支持验证
  *功能寻址下屏蔽该负反馈
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC7F(void)
{
    uint8_t NRC = positiveResponse;
    if(getSessionType() == DefaultSession)
    {
        NRC = serviceNotSupportedInActiveSession;
        if(DiagMSG.msgID == DIAG_ID_Rx_PHY)
        {
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
    }
    else
    {
        NRC = positiveResponse;
    }
    return NRC;
}

/**
  *27服务解锁等级验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC33(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}

/**
  *27服务最小长度验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC13_MinLengthCheck(void)
{
    uint8_t NRC = positiveResponse;
    if(DiagMSG.msgDLC < 1u)
    {
        NRC = incorrectMessageLength;
        UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
    }
    else
    {
        NRC = positiveResponse;
    }
    return NRC;
}

/**
  *27服务超出请求范围验证
  *功能寻址下屏蔽该负反馈
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC31(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}

/**
  *27服务总长度长度验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC13_TotalLengthCheck(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}

/**
  *27服务条件验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_NRC22(void)
{
    uint8_t NRC = positiveResponse;
    /*加判断条件*/
    /*if(!GetRequestSeedFlag()){
        NRC = conditionsNotCorrect;
        UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
    }*/
    return NRC;
}

/**
  *27服务子功能最小长度验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC13_MinLengthCheck(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}


  
/**
  *27服务子功能是否支持验证
  *功能寻址下屏蔽该负反馈
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC12(void)
{
    uint8_t NRC = positiveResponse;
    if((DiagMSG.msgData.SidDetail.SubID == requestSeed_LV1)|| (DiagMSG.msgData.SidDetail.SubID == sendKey_LV1)|| (DiagMSG.msgData.SidDetail.SubID == requestSeed_LV3)|| (DiagMSG.msgData.SidDetail.SubID == sendKey_LV3)|| (DiagMSG.msgData.SidDetail.SubID == requestSeed_LV7)|| (DiagMSG.msgData.SidDetail.SubID == sendKey_LV7))
    {
        if(DiagMSG.msgData.SidDetail.PositiveSuppression == 0u)
        {
            NRC = positiveResponse;
        }
        else
        {
            NRC = subFunctionNotSupported;
            if(DiagMSG.msgID == DIAG_ID_Rx_PHY)
            {
                UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
            }
        }
    }
    else
    {
        NRC = subFunctionNotSupported;
        if(DiagMSG.msgID == DIAG_ID_Rx_PHY)
        {
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
    }
    return NRC;
}

/**
  *27服务子功能会话是否支持验证
  *功能寻址下屏蔽该负反馈
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC7E(void)
{
    uint8_t NRC = positiveResponse;
    if((DiagMSG.msgData.SidDetail.SubID == requestSeed_LV7)|| (DiagMSG.msgData.SidDetail.SubID == sendKey_LV7))
    {
        NRC = subFunctionNotSupported;
        if(DiagMSG.msgID == DIAG_ID_Rx_PHY)
        {
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
    }
    else if((DiagMSG.msgData.SidDetail.SubID == requestSeed_LV1)|| (DiagMSG.msgData.SidDetail.SubID == sendKey_LV1)|| (DiagMSG.msgData.SidDetail.SubID == requestSeed_LV3)|| (DiagMSG.msgData.SidDetail.SubID == sendKey_LV3))
    {
        if(getSessionType() == DefaultSession)
        {
            NRC = subFunctionNotSupported;
            if(DiagMSG.msgID == DIAG_ID_Rx_PHY)
            {
                UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
            }
        }
    }
    else
    {
        NRC = positiveResponse;
    }
    return NRC;
}

/**
  *27服务子功能超出请求范围验证
  *功能寻址下屏蔽该负反馈
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC31(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}

/**
  *85服务子功能总长度长度验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC13_TotalLengthCheck(void)
{
    uint8_t NRC = positiveResponse;
    if((DiagMSG.msgData.SidDetail.SubID == requestSeed_LV1))
    {
        if(DiagMSG.msgDLC != 1u)
        {
            NRC = incorrectMessageLength;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
        else
        {
            NRC = positiveResponse;
        }
    }
    if((DiagMSG.msgData.SidDetail.SubID == sendKey_LV1))
    {
        if(DiagMSG.msgDLC != 3u)
        {
            NRC = incorrectMessageLength;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
        else
        {
            NRC = positiveResponse;
        }
    }
    if((DiagMSG.msgData.SidDetail.SubID == requestSeed_LV3))
    {
        if(DiagMSG.msgDLC != 1u)
        {
            NRC = incorrectMessageLength;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
        else
        {
            NRC = positiveResponse;
        }
    }
    if((DiagMSG.msgData.SidDetail.SubID == sendKey_LV3))
    {
        if(DiagMSG.msgDLC != 3u)
        {
            NRC = incorrectMessageLength;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);
        }
        else
        {
            NRC = positiveResponse;
        }
    }
   
    return NRC;
}

/**
  *27服务子功能请求顺序验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC24(void)
{
    uint8_t NRC = positiveResponse;
    if(DiagMSG.msgData.SidDetail.SubID == sendKey_LV1)
    {
        if(Ser27_FlowCtrl.LV1Ctrl == SER27_REQ_SEEDED)
        {
            NRC = positiveResponse;
        }
        else
        {
            NRC = requestSequenceError;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);  
        }
    }
    else if(DiagMSG.msgData.SidDetail.SubID == sendKey_LV3)
    {
        if(Ser27_FlowCtrl.LV3Ctrl == SER27_REQ_SEEDED)
        {
            NRC = positiveResponse;
        }
        else
        {
            NRC = requestSequenceError;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);  
        }
       
    }
    else{
        NRC = positiveResponse;
    }
 
    return NRC;
}

/**
  *27服务子功能条件验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC22(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}


/**
  *27服务子功能条件验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC37(void)
{
    uint8_t NRC;
    NRC = positiveResponse;
    if((DiagMSG.msgData.SidDetail.SubID == requestSeed_LV1) || (DiagMSG.msgData.SidDetail.SubID == requestSeed_LV3))
    {
        if((Ser27_FlowCtrl.LV1DelayCtrl == SER27_ERR_DELAY_TIMER_EN) || (Ser27_FlowCtrl.LV3DelayCtrl == SER27_ERR_DELAY_TIMER_EN))
        {
            NRC =requiredTimeDelayNotExpired;
            UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);  
        }
    }   
    return NRC;
}

/**
  *27服务子功能条件验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC36(void)
{
    uint8_t NRC = positiveResponse;
    NRC = exceedNumberOfAttempts  ;
    UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);  
    return NRC;
}


/**
  *27服务子功能条件验证
  *@return 负反馈编号
  */
uint8_t UDS_27Service_Sub_NRC35(void)
{
    uint8_t NRC = positiveResponse;
    NRC = invalidKey;
    UDS_Service_Response(0x27u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, (uint8_t*)&NRC);  
    return NRC;
}

