#include "UDS_22Service_Filter.h"

const uint8_t  S22DLC [] = {2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
                            2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u};
const uint16_t S22DID [] = {
                                0xF187u, 
                                0xF18Au,
                                0xF18Bu,
                                0xF18Cu,
                                0xF190u,
                                0xF192u,
                                0xF193u,
                                0xF194u,
                                0xF195u,
                                0xF197u,
                                0xF1B0u,
                                0xF1B1u,
                                0xF1B7u,
                                0xF1B8u,
                                0xF1BFu,
                                0xF1C5u,
                                0xF1C6u,
                                0xF186u,
                                0xF180u,
                                0xF010u,
                                0xF15Bu,
                                0xCF00u,
                                0xCF01u,
                                0xCF02u,
                                0xCF03u,
                                0xCF04u,
                                0x1E00u,
                                0x1E01u,
                            };

/**
 *22服务是否支持功能寻址
 *功能寻址下屏蔽该负反馈
 *@return 负反馈编号
 */
uint8_t UDS_22Service_NRC11(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}

/**
 *22服务会话是否支持验证
 *功能寻址下屏蔽该负反馈
 *@return 负反馈编号
 */
uint8_t UDS_22Service_NRC7F(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}

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

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

/**
 *22服务超出请求范围验证
 *功能寻址下屏蔽该负反馈
 *@return 负反馈编号
 */
uint8_t UDS_22Service_NRC31(void)
{
    uint8_t i;
    uint8_t NRC   = requestOutOfRange;
    uint8_t count = 0u;

    count = DIM(S22DID);

    for ( i = 0u; i < count; ++i )
    {
        if ( getDID(DiagMSG.msgData.DidDetail.DID_H, DiagMSG.msgData.DidDetail.DID_L) == S22DID [ i ] )
        {
            NRC = positiveResponse;
            break;
        }
    }
    if ( (NRC == requestOutOfRange) && (DiagMSG.msgID == DIAG_ID_Rx_PHY) )
    {
        UDS_Service_Response(0x22u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, ( uint8_t * )&NRC);
    }
    return NRC;
}

/**
 *22服务总长度长度验证
 *@return 负反馈编号
 */
uint8_t UDS_22Service_NRC13_TotalLengthCheck(void)
{
    uint8_t i;
    uint8_t NRC = positiveResponse;
    for ( i = 0u; i < DIM(S22DID); ++i )
    {
        if ( getDID(DiagMSG.msgData.DidDetail.DID_H, DiagMSG.msgData.DidDetail.DID_L)
             == S22DID [ i ] )
        {
            if ( DiagMSG.msgDLC != S22DLC [ i ] )
            {
                NRC = incorrectMessageLength;
                UDS_Service_Response(0x22u, NEGATIVE_RSP, DIAG_ID_Tx, 1u, ( uint8_t * )&NRC);
            }
            else
            {
                NRC = positiveResponse;
            }
            break;
        }
    }
    return NRC;
}

/**
 *22服务条件验证
 *@return 负反馈编号
 */
uint8_t UDS_22Service_NRC22(void)
{
    uint8_t NRC = positiveResponse;
    return NRC;
}
