kwp2000_protocol.c 10.4 KB
Newer Older
hu's avatar
hu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
*********************************************************************
* Includes
*********************************************************************
*/

#include "kwp2000_tp.h"
#include "kwp2000_service.h"
#include "kwp2000_protocol.h"
#include "kwp2000_interface.h"

/*
*********************************************************************
* structure
*********************************************************************
*/

typedef unsigned char Kwp2000_Service_t(unsigned char *, unsigned char *);

typedef struct
{
    unsigned char sId;
    Kwp2000_Service_t *Kwp2000_Servie;
} Kwp2000_ServiceTab_Entry_t;

/*
*********************************************************************
* variable
*********************************************************************
*/

hu's avatar
hu committed
32
unsigned char Kwp2000_ComState_Flag;
hu's avatar
hu committed
33 34 35 36 37 38 39

/*
*********************************************************************
* function
*********************************************************************
*/
void Kwp2000_CallService(unsigned char Sid_u8);
hu's avatar
hu committed
40
unsigned char kwp2000_CalcSeriveTabSize(void);
hu's avatar
hu committed
41 42 43 44

/*-----------------------------------------------------------------------------------*/
const Kwp2000_ServiceTab_Entry_t Kwp2000_ServiceTab[] =
{
hu's avatar
hu committed
45 46 47 48 49 50 51 52 53 54 55 56
    {KWP2000_ECURESET_REQUEST,                              kwp_EcuReset                        }, /* 0x11 */
    {KWP2000_TESTERPRESENT_REQUEST,                         kwp_TesterPresent                   }, /* 0x3E */
    {KWP2000_STARTCOMMUNICATION_REQUEST,                    kwp_StartCommunication              }, /* 0x81 */
    {KWP2000_STOPCOMMUNICATION_REQUEST,                     kwp_StopCommunication               }, /* 0x82 */
    {KWP2000_READECUIDENTIFICATION_REQUEST,                 kwp_readEcuIdentification           }, /* 0x1A */
    {KWP2000_WRITEDATABYLOCALIDENTIFIER_REQUEST,            kwp_writeDataByLocalIdentifier      }, /* 0x3B */
    {KWP2000_READDATABYLOCALIDENTIFIER_REQUEST,             kwp_readDataByLocalIdentifier       }, /* 0x21 */
    {KWP2000_READDATABYCOMMONIDENTIFIER_REQUEST,            kwp_readDataByCommonIdentifier      }, /* 0x22 */
    {KWP2000_READDIAGNOSTICTROUBLECODESBYSTATUS_REQUEST,    kwp_readDataStatusofDTC             }, /* 0x18 */
    {KWP2000_CLEARDIAGNOSTICINFORMATION_REQUEST,            kwp_ClearDiagnosticInformation      }, /* 0x14 */
    {KWP2000_SECURITYACCESS_REQUEST,                        kwp_readDataAccessMode              }, /* 0x27 */
    {KWP2000_STARTROUTINEBYLOCALIDENTIFIER_REQUEST,         kwp_StartroutInebykocalIdentifier   }, /* 0x31 */
hu's avatar
hu committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
};

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_CallService
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_CallService(unsigned char Sid_u8)
{
    unsigned char serviceFound;
    unsigned char index;

    serviceFound = 0;
    Kwp2000_Negative.ResponseCode = 0;

    for (index = 0; index < kwp2000_CalcSeriveTabSize(); index++)
    {
        if (Kwp2000_ServiceTab[index].sId == Sid_u8)
        {
            serviceFound = 1;

hu's avatar
hu committed
81
            Kwp2000_ComState.Tx_len = Kwp2000_ServiceTab[index].Kwp2000_Servie((unsigned char *)&Kwp2000_ComState.TxBuffer[4],
hu's avatar
hu committed
82 83 84 85 86 87 88 89 90
                                      (unsigned char *)&Kwp2000_ComState.Request_Datas[1]);
            break;
        }
    }

    if (serviceFound == 0)
    {
        Kwp2000_ComState.Tx_len = kwp_NegativeResponse(Sid_u8, KWP2000_serviceNotSupported, (unsigned char *)&Kwp2000_ComState.TxBuffer[4]);
    }
hu's avatar
hu committed
91
    else if (Kwp2000_Negative.ResponseCode) /*其他负反馈处理 后添加原来没有*/
hu's avatar
hu committed
92 93 94 95 96 97 98 99 100 101 102
    {
        Kwp2000_ComState.Tx_len = kwp_NegativeResponse(Sid_u8, Kwp2000_Negative.ResponseCode, (unsigned char *)&Kwp2000_ComState.TxBuffer[4]);
        Kwp2000_Negative.ResponseCode = 0;
    }

    if (Kwp2000_ComState.Tx_len != 0)
    {
        Kwp2000_ComState.Tx_len += Kwp2000_PrepareHeader(Kwp2000_ComState.Tx_len);

        *(Kwp2000_ComState.Response_Datas + Kwp2000_ComState.Tx_len) = Kwp2000_ChecksumCalculate(Kwp2000_ComState.Response_Datas, Kwp2000_ComState.Tx_len);

hu's avatar
hu committed
103
        Kwp2000_ComState.Tx_len++;
hu's avatar
hu committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_Handle
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_Handle(void)
{
    if ((Kwp2000_ComMode & PROTOCOL) == KWP2000)
    {
hu's avatar
hu committed
119 120
        /*在FAST_INIT之后,如果在RX中断中接收数据,在转为几首模式,在FAST_INIT后加一个超时时间
        如果FAST_INIT后10ms没有接到新到来得数据就会回到等待空闲模式避免来个wakeup后仪表无法接收数据*/
hu's avatar
hu committed
121 122 123 124 125 126 127 128
        /*
        if(Kwp2000_ComMode == KWP2000_FAST_INIT)
        {
            Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
        }
        */
        switch (Kwp2000_ComMode)
        {
hu's avatar
hu committed
129
        case KWP2000_WAIT_RECEPTION: /* 0x22 */
hu's avatar
hu committed
130 131 132 133 134 135
            if (Kwp2000_ComState.Rx_len >= 1)
            {
                Kwp2000_ComState.headerSize = Kwp2000_FormatAnalyse();
                Kwp2000_ComMode = KWP2000_WAIT_HEADER;
            }
            break;
hu's avatar
hu committed
136
        case KWP2000_WAIT_HEADER: /* 0x23 */
hu's avatar
hu committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            if (Kwp2000_ComState.Rx_len >= Kwp2000_ComState.headerSize)
            {
                Kwp2000_HeaderAnalyse();

                /* Tests the Target address */
                if (kwp2000_AddressTest() != 1)
                {
                    /* This message is not for this ECU */
                    Kwp2000_CommuniationDown();
                    Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
                }
                else
                {
                    Kwp2000_ComMode = KWP2000_WAIT_DATAS;
                }
            }
            break;
hu's avatar
hu committed
154 155
        case KWP2000_COMMUNICATION: /* 0x25 */
        case KWP2000_WAIT_DATAS:    /* 0x24 */
hu's avatar
hu committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            if (Kwp2000_ComState.Rx_len > (Kwp2000_ComState.headerSize + Kwp2000_ComState.kwp2000_Len))
            {
                Kwp2000_ComState.kwp2000_Checksum = Kwp2000_ComState.RxBuffer[Kwp2000_ComState.headerSize + Kwp2000_ComState.kwp2000_Len];

                if (Kwp2000_VerifyChecksum())
                {
                    Kwp2000_ComMode = KWP2000_BUILD_RESPONSE;
                    break;
                }
                else
                {
                    Kwp2000_CommuniationDown();
                    Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
                    break;
                }
            }
            else
            {
            }
            break;
hu's avatar
hu committed
176
        case KWP2000_BUILD_RESPONSE: /* 0x26 */
hu's avatar
hu committed
177 178 179 180 181 182 183 184 185 186 187 188
            if (!Kwp2000_ComState.ResponsePending)
            {
                Kwp2000_ComState.SId = *(Kwp2000_ComState.Request_Datas);
            }
            else
            {
                /* do nothing */
            }

            Kwp2000_ComState_Flag++;
            if (Kwp2000_ComState_Flag > 7)
            {
hu's avatar
hu committed
189 190
#if 0
                if ()
hu's avatar
hu committed
191
                {
hu's avatar
hu committed
192 193 194 195 196 197 198 199 200
                    SeedKey = (unsigned long)RealTimeClock.RollingCounter;
                    SeedKey = SeedKey << 16;
                    SeedKey |= (unsigned long)(~RealTimeClock.RollingCounter);

                    Seed1 = (unsigned char)(SeedKey >> 24);
                    Seed2 = (unsigned char)(SeedKey >> 16);
                    Seed3 = (unsigned char)(SeedKey >> 8);
                    Seed4 = (unsigned char)(SeedKey);
                    App_Uds_Calc_Key(); /*计算密钥等待应答*/
hu's avatar
hu committed
201
                }
hu's avatar
hu committed
202
#endif
hu's avatar
hu committed
203 204 205 206 207 208
                Kwp2000_ComState_Flag = 0;
                Kwp2000_CallService(Kwp2000_ComState.SId);
            }

            if (Kwp2000_ComState.Tx_len != 0)
            {
hu's avatar
hu committed
209
                Kwp2000_AscTx(Kwp2000_ComState.Response_Datas); /*qitiancun 数据发送----20220303----*/
hu's avatar
hu committed
210 211 212 213 214 215 216
                Kwp2000_ComMode = KWP2000_SEND_RESPONSE;
            }
            else
            {
                Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
            }
            break;
hu's avatar
hu committed
217
        case KWP2000_SEND_RESPONSE: /* 0x27 */
hu's avatar
hu committed
218 219 220 221 222 223 224 225
            /* Wait when sending response message */
            if (Kwp2000_ComState.Tx_len)
            {
            }
            else
            {
            }
            break;
hu's avatar
hu committed
226
        case KWP2000_MODIFY_CONFIG: /* 0x28  */
hu's avatar
hu committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
            switch (Kwp2000_Modify.typeOfModif)
            {
            case KWP2000_MODIFY_NULL:
                Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
                Kwp2000_ComState.Rx_len = 0;
                break;

            case KWP2000_MODIFY_BAUDRATE:
                Kwp2000_ComMode = KWP2000_WAIT_RECEPTION;
                Kwp2000_AscSwitchBaudrate(Kwp2000_Modify.newBaudRate);
                Kwp2000_Modify.typeOfModif = KWP2000_MODIFY_NULL;
                Kwp2000_ComState.Rx_len = 0;
                break;

            case KWP2000_MODIFY_RESET:
                Kwp2000_ComState.Rx_len = 0;
                switch (Kwp2000_Modify.resetType)
                {
                case 0x01:
hu's avatar
hu committed
246
                    // Reset_SWResetRequest(SWRESET_GRP_POWERON_E, SWRESET_POWERON_E, 0x0);  qitiancun
hu's avatar
hu committed
247 248
                    break;
                case 0x6A:
hu's avatar
hu committed
249
                    // Reset_SWResetRequest(SWRESET_GRP_SB_E, SWRESET_RB_PROG_E, 0x0);  qitiancun
hu's avatar
hu committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                    break;
                default:
                    /*do nothing */
                    break;
                }
                break;

            case KWP2000_MODIFY_END:
                Kwp2000_ProtocolInit();
                break;

            default:
                break;
            }
            break;
        case KWP2000_END:
            break;
        default:
            break;
        }
    }
}

/*-------------------------------------------------------------------------
hu's avatar
hu committed
274
* Function Name  : kwp2000_CalcSeriveTabSize
hu's avatar
hu committed
275 276 277 278 279 280 281 282 283 284
* Description    : kwp2000 Calculate Serive Table Size
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
unsigned char kwp2000_CalcSeriveTabSize(void)
{
    return (unsigned char)(sizeof(Kwp2000_ServiceTab) / sizeof(Kwp2000_ServiceTab_Entry_t));
}