UDS_ISO14229_Services.c 49.4 KB
Newer Older
李俭双's avatar
李俭双 committed
1 2 3 4 5 6 7 8 9
/******************************************************************************
 �� �� ��;UDS_ISO14229_Services.c
 ��������;ISO 14229 �淶�涨����Ϸ���������˷���ʵ�ֺ������ļ�
 ��    ��;����
 ��    ��;V1.0
 ��    ��;2016.11.1
 ******************************************************************************/
#include "UDS_ISO14229_Services.h"
#include "flash.h"
10 11
#include "CAN_Signal\CAN_Lib.h"
#include "CAN_APP\CAN_CH0_CAN_Communication_Matrix.h"
李俭双's avatar
李俭双 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
typedef struct
{
    uint8_t ProjecName[16]; // 项目名
    uint8_t PartNumber[16]; // 零件号
    uint8_t PlatForm[16];   // 平台
    struct 
    {
      uint8_t type[16];     //标准*
      uint8_t ch[16];       //通道*
      uint8_t baudrate[16]; //速率*
      uint32_t diagID[4];   //诊断ID*
    }DiagCanCfg[1];         //(诊断CAN)通道配置*
} ProjectInfoStruct;

extern void UDS_Service_Response(uint8_t si, uint8_t RspType, uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);

/*诊断使用, 判断App一致性, 禁止修改(内部版本号除外)*/
29
#if(IC_Current == TY200_080000b)
李俭双's avatar
李俭双 committed
30 31
extern const ProjectInfoStruct ProjectInfo __attribute__((section(".ARM.__at_0x00007800"))) =
{
32
    "TianYing100",    // 天鹰100
33
    "TY200.080000b",      //零件号
34
    "BAT32G139GK64FB", // 中微芯片
李俭双's avatar
李俭双 committed
35 36 37 38
    {
        "CAN_STD",
        "CAN_CH_0",
        "CAN_500Kbps",
张明扬's avatar
张明扬 committed
39
        {DIAG_ID_Tx, DIAG_ID_Rx_PHY, DIAG_ID_Rx_FUN, 0}
李俭双's avatar
李俭双 committed
40 41
    }
};
42 43 44 45 46 47 48 49 50 51
#else
extern const ProjectInfoStruct ProjectInfo __attribute__((section(".ARM.__at_0x00007800"))) =
{
    "TianYing100",    // 天鹰100
    "TY200.080000b_ty",      //零件号
    "BAT32G139GK64FB", // 中微芯片
    {
        "CAN_STD",
        "CAN_CH_0",
        "CAN_500Kbps",
张明扬's avatar
张明扬 committed
52
        {DIAG_ID_Tx, DIAG_ID_Rx_PHY, DIAG_ID_Rx_FUN, 0}
53 54 55
    }
};
#endif
李俭双's avatar
李俭双 committed
56 57 58 59
DiagSendDataNeg NegRes;
uint8_t UDS_ISO14229_Transfer[250];

DiagDFlashData DiagDataForDFlash;
60
Ser2EDFlashData Ser2EDataForDFlash;
李俭双's avatar
李俭双 committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
DiagFlag DiagDataForFlag;
Ser27_FlowCtrlCntUnion Ser27_FlowCtrlCnt;
uint8_t                FlashDriverCheckSum = 0;
uint8_t                BlocKCnt            = 0;
/****************S3Timer********************/
uint8_t S3_ServerEN       = 0;
uint8_t S3_ServerCnt      = 0;
uint8_t S3_Server_refresh = 0;
/****************DiagnosticSession*****************/
uint8_t SessionType = 0x01;
/****************Ser28*********************/
uint8_t ControlType;
uint8_t CommunicationType;
/****************Ser27*********************/
uint8_t  wait10cnt    = 0;
uint8_t  Wait10sFlag  = 0;
uint8_t  DiagLockFlag = 0;
uint8_t  Seed [ 4 ];
uint32_t ValidSeedKey;
uint8_t  Services27_01_Requested;
/******************************************************************************
 The service access point of the diagnostics application layer provides a number
 of services that all have the same general structure. For each service, three
 service primitives are specified:

 - a service indication primitive, used by the diagnostics application layer, to
 pass data to the server function of the ECU diagnostic application;

 - a service response primitive, used by the server function in the ECU
 diagnostic application, to pass response data provided by the requested
 diagnostic service to the diagnostics application layer;

 - a service response-confirmation primitive, used by the server function in the
 ECU diagnostic application, to indicate that the data passed in the service
 response primitive is successfully sent on the vehicle communication bus the
 ECU received the diagnostic request on;
 ******************************************************************************/

99
#if(IC_Current == TY200_080000b)
李俭双's avatar
李俭双 committed
100 101 102 103 104 105 106 107 108 109
//软件版本号 F195
uint8_t MCU_SWversion[6] = {'S', 'V', (uint8_t)((SWV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((SWV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(SWV & 0x0Fu) + 0x30u};

//硬件版本号 F193
uint8_t MCU_HWversion[6] = {'H', 'V', (uint8_t)((HWV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((HWV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(HWV & 0x0Fu) + 0x30u};

//引导程序版本信息 F180
uint8_t MCU_FBLversion[6] = {'B', 'V', (uint8_t)((BTV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((BTV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(BTV & 0x0Fu) + 0x30u};

//零件号信息 F187
110
uint8_t MCU_PartNumber[13] = {'T', 'Y', '2', '0', '0', '.', '0', '8', '0', '0', '0', '0', 'b'};
111 112 113 114 115 116 117 118 119 120 121
#else
//软件版本号 F195
uint8_t MCU_SWversion[6] = {'S', 'V', (uint8_t)((SWV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((SWV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(SWV & 0x0Fu) + 0x30u};

//硬件版本号 F193
uint8_t MCU_HWversion[6] = {'H', 'V', (uint8_t)((HWV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((HWV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(HWV & 0x0Fu) + 0x30u};

//引导程序版本信息 F180
uint8_t MCU_FBLversion[6] = {'B', 'V', (uint8_t)((BTV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((BTV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(BTV & 0x0Fu) + 0x30u};

//零件号信息 F187
122
uint8_t MCU_PartNumber[18] = {'T', 'Y', '2', '0', '0', '.', '0', '8', '0', '0', '0', '0', '0', 'b', '(', 't', 'y', ')'};
123
#endif
李俭双's avatar
李俭双 committed
124 125 126 127

void Data_Set_DiagPara(void)
{
    uint8_t i;
128
    uint8_t length = 0;
129
    uint8_t Filldata_length = 0;
李俭双's avatar
李俭双 committed
130 131 132 133 134 135

    //获取DID的值,等待写入DFlash
    for(i = 0; i < 6; i++)
    {
        DiagDataForDFlash.DID_F180[i] = MCU_FBLversion[i];
    }
136
    length = (IC_Current == TY200_080000b) ? 13 : 18;
137
    for( i = 0; i < length; i++)
李俭双's avatar
李俭双 committed
138 139 140 141 142 143 144 145 146 147 148
    {
        DiagDataForDFlash.DID_F187[i] = MCU_PartNumber[i];
    }
    for(i = 0; i < 6; i++)
    {
        DiagDataForDFlash.DID_F193[i] = MCU_HWversion[i];
    }
    for(i = 0; i < 6; i++)
    {
        DiagDataForDFlash.DID_F195[i] = MCU_SWversion[i];
    }
149 150
    Filldata_length = (IC_Current == TY200_080000b) ? 1 : 4;
    for(i = 0; i < Filldata_length; i++)
李俭双's avatar
李俭双 committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    {
        DiagDataForDFlash.Filldata[i] = 0u;
    }
}

/*写App有效性标志*/
void Write_App_InValid(uint32_t m32)
{
	//设置App状态值
    DiagDataForFlag.Flag = 0xA77A5AA5u;
    DiagDataForFlag.APP_STATUS = m32;
    //擦除扇区
	EraseSector (APP_STATUS_ADDR);
    //App程序状态和22服务数据全写进去
    ProgramPage (APP_STATUS_ADDR, 8U, (uint8_t *)(&DiagDataForFlag));
}

/*写22服务数据*/
void DFlash_init(void)
{
171
    DiagDFlashData* ReadDiagDataForDFlash =(DiagDFlashData*)APP_DATA_INFO; 
李俭双's avatar
李俭双 committed
172 173 174
    
    if( (ReadDiagDataForDFlash->Flag != 0x5AA5A77Au)||
        (0!=memcmp(ReadDiagDataForDFlash->DID_F180 , MCU_FBLversion,sizeof(MCU_FBLversion)))||
175
        (0!=memcmp(ReadDiagDataForDFlash->DID_F187 , MCU_PartNumber,sizeof(MCU_PartNumber)))||
李俭双's avatar
李俭双 committed
176 177 178 179 180 181 182
        (0!=memcmp(ReadDiagDataForDFlash->DID_F193 , MCU_HWversion ,sizeof(MCU_HWversion )))||
        (0!=memcmp(ReadDiagDataForDFlash->DID_F195 , MCU_SWversion ,sizeof(MCU_SWversion )))
      )
    {
        Data_Set_DiagPara();
        DiagDataForDFlash.Flag = 0x5AA5A77Au;
        //擦除扇区
183
        EraseSector (APP_DATA_INFO);
李俭双's avatar
李俭双 committed
184
        //App程序状态和22服务数据全写进去
185
        ProgramPage (APP_DATA_INFO, sizeof(DiagDataForDFlash), (uint8_t *)(&DiagDataForDFlash));
李俭双's avatar
李俭双 committed
186 187 188 189 190 191 192
    }
    else
    {
        memcpy(&DiagDataForDFlash,ReadDiagDataForDFlash,sizeof(DiagDFlashData));
    }

}
193 194 195 196
void Data_Read_DiagPara(void)
{
    uint8_t i;
    Ser2EDFlashData* ReadSer2EDataForDFlash = (Ser2EDFlashData*)0x500600ul; 
197
    for ( i = 0; i < 32; i++)
198 199 200 201
    {
        Ser2EDataForDFlash.DID_F184[i] = ReadSer2EDataForDFlash->DID_F184[i];
    }
}
李俭双's avatar
李俭双 committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
void S3_ServerCNTT(void)
{
    if ( Wait10sFlag == 1 )
    {
        if ( wait10cnt >= 100 )
        {
            Ser27_FlowCtrlCnt.RequestSeedCnt = 2;
            Ser27_FlowCtrlCnt.Attemptcnt     = 2;
            // WriteDFlashData(0x02u, ( uint32_t * )&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NoNeedWait);
            wait10cnt   = 0;
            Wait10sFlag = 0;
        }
        else
        {
            ++wait10cnt;
        }
    }

    if ( S3_ServerEN == 1 )
    {
        if ( S3_Server_refresh == 1 )
        {
            S3_Server_refresh = 0;
            S3_ServerCnt      = 0;
        }

        if ( S3_ServerCnt >= S3_SERVER )
        {
            DIAG_InitParameter( );
231 232
            CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
            CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);//S3超时解除
李俭双's avatar
李俭双 committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        }
        else
        {
            ++S3_ServerCnt;
        }
    }
}

uint32_t RANDOM = 0xA77A;
void Randomcnt(void)
{
    RANDOM++;
    if (RANDOM >= 0xFF0F00F0)
    {
        RANDOM = 0x363;
    }
}

uint8_t SerXXNRC12Filter(uint8_t insub, const uint8_t *supportsub, uint8_t size, uint8_t *index)
{
    uint8_t sub     = insub;
    uint8_t i       = 0;
    uint8_t nrccode = 0;

    if ( size > 0 )
    {
        nrccode = subFunctionNotSupported;
        while ( i < size )
        {
            if ( supportsub [ i ] == sub )
            {
                nrccode = 0;
                *index  = i;
                break;
            }
            i++;
        }
    }
    else
    {
        *index = 0;
    }

    return nrccode;
}

279
uint8_t SerXXNRC13Filter(uint8_t type, uint16_t serlength, const uint8_t *length, uint8_t index)
李俭双's avatar
李俭双 committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
{
    uint8_t nrccode;
    nrccode = incorrectMessageLength;

    if ( 0 == type )
    {
        if ( length != 0 )
        {
            if ( length [ 0 ] <= serlength )
            {
                nrccode = 0;
            }
        }
        else
        {
            nrccode = incorrectMessageLength;
        }
    }
    else
    {
        if ( length != 0 )
        {
            if ( length [ index ] == serlength )
            {
                nrccode = 0;
            }
        }
        else
        {
            nrccode = incorrectMessageLength;
        }
    }
    return nrccode;
}

uint8_t SerXXNRC22Filter(void)
{
    uint8_t nrccode = 0;
    nrccode         = 0;

    return nrccode;
}

uint8_t SerXXNRC31DIDFilter(uint16_t inDID, const uint16_t *DIDList, uint8_t size, uint8_t *index)
{
    uint8_t nrccode = 0;
    uint8_t i       = 0;

    if ( size > 0 )
    {
        nrccode = requestOutOfRange;
        while ( i < size )
        {
            if ( inDID == DIDList [ i ] )
            {
                nrccode = 0;
                *index  = i;
                break;
            }
            else
            {
                ++i;
            }
        }
    }
    return nrccode;
}

uint8_t SerXXNRC33Filter(const uint8_t *levelList, uint8_t index)
{
    uint8_t nrccode;
    nrccode = 0;

    if ( NoNeed != levelList [ index ] )
    {
        if ( DiagLockFlag != levelList [ index ] )
        {
            nrccode = securityAccessDenied;
        }
    }
    return nrccode;
}

uint8_t SerXXNRC7EFilter(const uint8_t *SubSupportSession, uint8_t index)
{
    uint8_t nrccode;
    nrccode = 0;

    if ( 0 == (GetCurrentSession( ) & SubSupportSession [ index ]) )
    {
        nrccode = subfunctionNotSupportinActiveSession;
    }
    return nrccode;
}

uint8_t SerXXNRC7FFilter(uint8_t SupportSession)
{
    uint8_t nrccode;
    nrccode = 0;

    if ( 0 == (GetCurrentSession( ) & SupportSession) )
    {
        nrccode = serviceNotSupportedInActiveSession;
    }
    return nrccode;
}

uint8_t GetCurrentSession(void)
{
    uint8_t SessionMode;
    if ( DefaultSession == SessionType )
    {
        SessionMode = DefaultMode;
    }
    else if ( ProgrammingSession == SessionType )
    {
        SessionMode = ProgrammingMode;
    }
    else if ( ExtendedDiagnosticSession == SessionType )
    {
        SessionMode = ExtendedDiagnosticMode;
    }
    else
    {
        SessionType = DefaultSession;
        SessionMode = DefaultMode;
    }
    return SessionMode;
}

/******************************************************************************
 10# - DiagnosticSessionControl
 ******************************************************************************/
static const uint8_t Ser10BanResponse          = AbleResponseType;
static const uint8_t Ser10Sub []               = {DefaultSession, ProgrammingSession, ExtendedDiagnosticSession};
static const uint8_t Ser10MinLen []            = {1};
static const uint8_t Ser10SubLen []            = {1, 1, 1};
static const uint8_t Ser10UnlockLevel []       = {NoNeed, NoNeed, NoNeed};
static const uint8_t Ser10SupportSession       = DefaultMode | ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser10SubSupportSession [] = {DefaultMode | ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode,
                                                  DefaultMode | ExtendedDiagnosticMode};
void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t i;
    uint8_t si = DiagnosticSessionControl;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t Service10DiagDataLength = A_Length;
    uint16_t FunorPhy = A_TA_type;

    for (i = 0; i < Service10DiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser10SupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service10DiagDataLength, Ser10MinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (Ser10BanResponse == AbleResponseType)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser10Sub, sizeof(Ser10Sub), &Index);
    }

    if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy) && (ProgrammingSession == Ser10Sub[Index]))
    {
        NrcCode = subFunctionNotSupported;
    }

    if (0 == NrcCode && Index < sizeof(Ser10UnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser10UnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser10SubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser10SubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser10SubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service10DiagDataLength, Ser10SubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

    if (0 == NrcCode)
    {
        UDS_ISO14229_Transfer[1] = 0;
        UDS_ISO14229_Transfer[2] = 50;
        UDS_ISO14229_Transfer[3] = 0;
        UDS_ISO14229_Transfer[4] = 200;
        if (DefaultSession == SubFunction)
        {
            S3_ServerEN = 0;
            SessionType = DefaultSession;
            DIAG_InitParameter();
495 496 497
            CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
            CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);//切会话解除

李俭双's avatar
李俭双 committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

            if (AbleResponseType == Ser10BanResponse)
            {
                if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
                }
            }
            else
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
            }
        }
        else if (ProgrammingSession == SubFunction)
        {
            S3_ServerEN = 1;
            S3_Server_refresh = 1;
            SessionType = ProgrammingSession;
            Services27_01_Requested = 0;
            DiagLockFlag = 0;

            if (AbleResponseType == Ser10BanResponse)
            {
                if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                {
                    NegRes.code = requestCorrectlyReceivedResponsePending;
                    UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));

                    Write_App_InValid(Jump_To_Boot_Need_Answer);

                    for (i = 0; i < 250; ++i)
                    {
                        __NOP();
                    }
                    __NVIC_SystemReset();
                }
                else
                {
                    Write_App_InValid(Jump_To_Boot_No_Answer);
                    for (i = 0; i < 250; ++i)
                    {
                        __NOP();
                    }
                    __NVIC_SystemReset();
                }
            }
            else
            {
                NegRes.code = requestCorrectlyReceivedResponsePending;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                Write_App_InValid(Jump_To_Boot_Need_Answer);
                for (i = 0; i < 250; ++i)
                {
                    __NOP();
                }
                __NVIC_SystemReset();
            }
        }
        else if (ExtendedDiagnosticSession == SubFunction)
        {
            S3_ServerEN = 1;
            S3_Server_refresh = 1;
            SessionType = ExtendedDiagnosticSession;
            Services27_01_Requested = 0;
            DiagLockFlag = 0;

            if (AbleResponseType == Ser10BanResponse)
            {
                if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
                }
            }
            else
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
            }
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

/******************************************************************************
 11# - ECUReset
 ******************************************************************************/
static const uint8_t Ser11BanResponse          = AbleResponseType;
static const uint8_t Ser11Sub []               = {HardReset};
static const uint8_t Ser11MinLen []            = {1};
static const uint8_t Ser11SubLen []            = {1};
static const uint8_t Ser11UnlockLevel []       = {NoNeed};
static const uint8_t Ser11SupportSession       = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser11SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_11_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t si = ECUReset;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t i;
    uint16_t Service11DiagDataLength = A_Length;
    uint16_t FunorPhy = A_TA_type;

    for (i = 0; i < Service11DiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
    {
        NrcCode = serviceNotSupported;
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser11SupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service11DiagDataLength, Ser11MinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (AbleResponseType == Ser11BanResponse)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser11Sub, sizeof(Ser11Sub), &Index);
    }

    if (0 == NrcCode && Index < sizeof(Ser11UnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser11UnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser11SubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser11SubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser11SubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service11DiagDataLength, Ser11SubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

    if (0 == NrcCode)
    {
        if (HardReset == SubFunction)
        {
            if (AbleResponseType == Ser11BanResponse)
            {
                if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
                }
            }
            else
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
            }
            i = 900;
            while (--i)
            {
                __NOP();
            }
            __NVIC_SystemReset();
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

/******************************************************************************
 22# - ReadDataByIdentifier
 ******************************************************************************/
static const uint8_t Ser22BanResponse = DisableResponseType;
static const uint8_t Ser22Sub []      = {0xFF};
static const uint8_t Ser22MinLen []   = {2};
static const uint8_t Ser22SubLen []   = {2};
720
static const uint16_t Ser22DIDList [] = {0xF195, 0xF193, 0xF180, 0xF187, 0x1024, 0x1028, 0xF184,0x2024 };
李俭双's avatar
李俭双 committed
721 722 723 724 725 726 727 728 729 730 731 732 733
static const uint8_t Ser22UnlockLevel []       = {NoNeed};
static const uint8_t Ser22SupportSession       = DefaultMode | ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser22SubSupportSession [] = {DefaultMode | ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t i;
    uint8_t si = ReadDataByIdentifier;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t DID;
    uint16_t Service22DiagDataLength = A_Length;
    uint16_t FunorPhy = A_TA_type;
734
    uint8_t length = 0;
李俭双's avatar
李俭双 committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

    for (i = 0; i < Service22DiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
    {
        NrcCode = serviceNotSupported;
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser22SupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service22DiagDataLength, Ser22MinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (AbleResponseType == Ser22BanResponse)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser22Sub, 0, &Index);
    }

    if (0 == NrcCode && Index < sizeof(Ser22UnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser22UnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser22SubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser22SubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser22SubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service22DiagDataLength, Ser22SubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

    if (0 == NrcCode)
    {
        DID = ((uint16_t)UDS_ISO14229_Transfer[0] << 8) | UDS_ISO14229_Transfer[1];
792
        NrcCode = SerXXNRC31DIDFilter(DID, Ser22DIDList, (uint8_t)(sizeof(Ser22DIDList) / 2), &Index);
李俭双's avatar
李俭双 committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    }

    if (0 == NrcCode)
    {
        switch (DID)
        {
        case 0xF195: // 读软件版本号
            for (i = 0; i < 6; i++)
            {
                UDS_ISO14229_Transfer[i + 2] = MCU_SWversion[i];
            }
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 6, UDS_ISO14229_Transfer);
            break;

        case 0xF193: // 读硬件版本号
            for (i = 0; i < 6; i++)
            {
                UDS_ISO14229_Transfer[i + 2] = MCU_HWversion[i];
            }
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 6, UDS_ISO14229_Transfer);
            break;
814
        case 0xF180: // 引导版本信息
李俭双's avatar
李俭双 committed
815 816 817 818 819 820 821 822
            for (i = 0; i < 6; i++)
            {
                UDS_ISO14229_Transfer[i + 2] = MCU_FBLversion[i];
            }
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 6, UDS_ISO14229_Transfer);
            break;

        case 0xF187: // 读零件号
823
            length = (IC_Current == TY200_080000b) ? 13 : 18;
824
            for (uint8_t i = 0; i < length; i++) 
李俭双's avatar
李俭双 committed
825 826 827
            {
                UDS_ISO14229_Transfer[i + 2] = MCU_PartNumber[i];
            }
828 829
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + length, UDS_ISO14229_Transfer);
            
李俭双's avatar
李俭双 committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
            break;

        case 0x1024: // 读内部版本号
            UDS_ISO14229_Transfer[0] = 0x10u;
            UDS_ISO14229_Transfer[1] = 0x24u;
            UDS_ISO14229_Transfer[2] = 'A';
            UDS_ISO14229_Transfer[3] = '_';
            UDS_ISO14229_Transfer[4] = 'V';
            UDS_ISO14229_Transfer[5] = (uint8_t)((INTLV >> 8u) & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[6] = '.';
            UDS_ISO14229_Transfer[7] = (uint8_t)((INTLV >> 4u) & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[8] = (uint8_t)(INTLV & 0x0Fu) + 0x30u;
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 7, UDS_ISO14229_Transfer);
            break;

        case 0x1028: // 读内部版本号
            UDS_ISO14229_Transfer[0] = 0x10u;
            UDS_ISO14229_Transfer[1] = 0x28u;
            UDS_ISO14229_Transfer[2] = '2';
            UDS_ISO14229_Transfer[3] = '0';
            UDS_ISO14229_Transfer[4] = (uint8_t)((PROG_Y >> 4u) & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[5] = (uint8_t)(PROG_Y & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[6] = (uint8_t)((PROG_M >> 4u) & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[7] = (uint8_t)(PROG_M & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[8] = (uint8_t)((PROG_D >> 4u) & 0x0Fu) + 0x30u;
            UDS_ISO14229_Transfer[9] = (uint8_t)(PROG_D & 0x0Fu) + 0x30u;
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 8, UDS_ISO14229_Transfer);
            break;

859
        case 0xF184: // UUID
860
            for (i = 0; i < 32; i++)
861 862 863
            {
                UDS_ISO14229_Transfer[i + 2] = Ser2EDataForDFlash.DID_F184[i];
            }
864
            UDS_Service_Response(0x22, POSITIVE_RSP, DIAG_ID_Tx, 2 + 32, UDS_ISO14229_Transfer);
865 866
            break;

867
        case 0x2024: // ESP32内部版本号
张明扬's avatar
张明扬 committed
868
            UDS_ISO14229_Transfer[2] = BlueTooth.ESP32_SWV;
869 870 871
            UDS_Service_Response(0x22, POSITIVE_RSP, DIAG_ID_Tx, 2 + 1, UDS_ISO14229_Transfer);
            break;

李俭双's avatar
李俭双 committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
        default:
            if (FunorPhy == DIAG_ID_Rx_FUN)
            {
                return;
            }
            NegRes.code = requestOutOfRange;
            UDS_Service_Response ( 0x22, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
            break;
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

902 903 904 905
void UDS_Service_2E_Indication ( uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data )
{
    uint16_t i;
    uint16_t DID;
906
    uint16_t  Service2EDiagDataLength;
907 908 909 910 911 912 913 914 915 916
    uint16_t FunorPhy = 0;
    Service2EDiagDataLength = A_Length;
    S3_ServerCnt = 0;
    FunorPhy = A_TA_type;

    for (i = 0; i < Service2EDiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }
    if ( FunorPhy == 0X7DF )
917
    {
918
        return;
919 920 921
    }
    
    if( (SessionType == DefaultSession) || (SessionType == ProgrammingSession))
922
    {
923
        NegRes.code = serviceNotSupportedInActiveSession; //NRC7F
924 925 926
        UDS_Service_Response ( 0x2E, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
        return;
    }
927

928 929 930 931 932 933
    if ( Service2EDiagDataLength < 3 )
    {
        NegRes.code = incorrectMessageLength; //NRC13
        UDS_Service_Response ( 0x2E, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
        return;
    }
934 935

    if ( DiagLockFlag == 0x00 )
936
    {
937
        NegRes.code = securityAccessDenied; //NRC33
938 939 940 941 942 943
        UDS_Service_Response ( 0x2E, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
        return;
    }
    DID = ((uint16_t)UDS_ISO14229_Transfer[0] << 8) | ((uint16_t)UDS_ISO14229_Transfer[1]);
    switch ( DID )
    {
944
        case 0xF184: // 写入UUID
945
            if ( Service2EDiagDataLength == 34 ) //限制最大长度
946
            {
947 948
                for (i = 0; i < (sizeof(Ser2EDataForDFlash.DID_F184)); i++)
                {
949
                    Ser2EDataForDFlash.DID_F184[i] = UDS_ISO14229_Transfer[i + 2];
950 951 952 953 954 955 956 957 958
                }
                Ser2EDataForDFlash.Flag = 0x5AA5A77Au;
                // 擦除扇区
                EraseSector(0x500600ul);
                // App程序状态和22服务数据全写进去
                ProgramPage(0x500600ul, sizeof(Ser2EDataForDFlash), (uint8_t *)(&Ser2EDataForDFlash));
                UDS_Service_Response(0x2E, POSITIVE_RSP, DIAG_ID_Tx, 2, UDS_ISO14229_Transfer);
            }
            else
959
            {
960 961
                NegRes.code = incorrectMessageLength; //NRC13
                UDS_Service_Response ( 0x2E, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
962 963 964 965 966 967 968 969 970 971 972 973 974 975
            }
            break;
        default:
            if ( FunorPhy == 0x7df )
            {
                return;
            }
            NegRes.code = requestOutOfRange;
            UDS_Service_Response ( 0x2E, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
            break;
    }
}


李俭双's avatar
李俭双 committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
void GetSeed(void)
{
    uint8_t SeedHigh;
    uint8_t SeedLow;
    uint16_t tempbuffer;

    tempbuffer = (uint16_t)(RANDOM);
    SeedHigh = (uint8_t)(tempbuffer >> 8);
    SeedLow = (uint8_t)(tempbuffer & 0xff);
    Seed[0] = 0x31 + ~SeedHigh;
    Seed[1] = 0x23 + ~SeedLow;
    Seed[2] = 0x56 + SeedHigh;
    Seed[3] = 0x71 + SeedLow;
}

const uint32_t KeyK = 0x00000201;
void CalculateKey(void)
{
    uint32_t SaccSeed;
    uint32_t KeyResult;

    SaccSeed = ((uint32_t)Seed[0]) << 24 | ((uint32_t)Seed[1]) << 16 | ((uint32_t)Seed[2]) << 8 | ((uint32_t)Seed[3]);

    KeyResult = (((SaccSeed >> 1) ^ SaccSeed) << 3) ^ (SaccSeed >> 2);
    KeyResult = KeyResult ^ KeyK;

    ValidSeedKey = KeyResult;
}

void CalculateKeyLv11(void)
{
    uint32_t SaccSeed;
    uint32_t KeyResult;

    SaccSeed = (( uint32_t )Seed [ 0 ]) << 24 | (( uint32_t )Seed [ 1 ]) << 16 | (( uint32_t )Seed [ 2 ]) << 8 | (( uint32_t )Seed [ 3 ]);

    KeyResult = (((SaccSeed >> 1) ^ SaccSeed) << 3) ^ (SaccSeed >> 2);
    KeyResult = KeyResult ^ KeyK;

    ValidSeedKey = KeyResult;
}

/******************************************************************************
 27# - SecurityAccess
 ******************************************************************************/
static const uint8_t Ser27BanResponse          = DisableResponseType;
static const uint8_t Ser27Sub []               = {requestSeed_LV1, sendKey_LV1};
static const uint8_t Ser27MinLen []            = {1};
static const uint8_t Ser27SubLen []            = {1, 5};
static const uint8_t Ser27UnlockLevel []       = {NoNeed, NoNeed};
static const uint8_t Ser27SupportSession       = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser27SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_27_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t i;
    uint8_t si = SecurityAccess;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t FunorPhy = A_TA_type;
    uint16_t Service27DiagDataLength = A_Length;
    uint32_t KeyReceive;

    for (i = 0; i < Service27DiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
    {
        NrcCode = serviceNotSupported;
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser27SupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service27DiagDataLength, Ser27MinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (AbleResponseType == Ser27BanResponse)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser27Sub, sizeof(Ser27Sub), &Index);
    }

    if (0 == NrcCode && Index < sizeof(Ser27UnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser27UnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser27SubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser27SubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser27SubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service27DiagDataLength, Ser27SubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

    if (0 == NrcCode)
    {
        if (1 == Wait10sFlag)
        {
            NrcCode = requiredTimeDelayNotExpired;
        }
    }

    if (0 == NrcCode)
    {
        if (0 == Services27_01_Requested && sendKey_LV1 == SubFunction)
        {
            NrcCode = requestSequenceError;
        }
    }

    if (0 == NrcCode)
    {
        KeyReceive = ((uint32_t)UDS_ISO14229_Transfer[1] << 24) | ((uint32_t)UDS_ISO14229_Transfer[2] << 16) | ((uint16_t)UDS_ISO14229_Transfer[3] << 8) | UDS_ISO14229_Transfer[4];
        if (sendKey_LV1 == SubFunction && (KeyReceive == 0x00000000 || KeyReceive == 0xFFFFFFFF))
        {
            NrcCode = requestOutOfRange;
        }
    }

    if (0 == NrcCode)
    {
        if (sendKey_LV1 == SubFunction && KeyReceive != ValidSeedKey)
        {
            Services27_01_Requested = 0;
            Ser27_FlowCtrlCnt.Attemptcnt++;
            // WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NeedWait);
            NrcCode = invalidKey;
        }
    }

    if (0 == NrcCode || invalidKey == NrcCode)
    {
        if (sendKey_LV1 == SubFunction)
        {
            if (AttemptMaxCnt <= Ser27_FlowCtrlCnt.Attemptcnt)
            {
                Wait10sFlag = 1;
                NrcCode = exceedNumberOfAttempts;
            }
        }
        if (requestSeed_LV1 == SubFunction)
        {
            Ser27_FlowCtrlCnt.Attemptcnt = Ser27_FlowCtrlCnt.RequestSeedCnt;
            Ser27_FlowCtrlCnt.RequestSeedCnt++;
            // WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NeedWait);
            if (RequestSeedMaxCnt <= Ser27_FlowCtrlCnt.RequestSeedCnt)
            {
                Wait10sFlag = 1;
                NrcCode = exceedNumberOfAttempts;
            }
        }
    }

    if (0 == NrcCode)
    {
        if (requestSeed_LV1 == SubFunction)
        {
            if (NormalKeyLock == DiagLockFlag)
            {
                Services27_01_Requested = 1;
                do
                {
                    GetSeed();
                } while (Seed[0] == 0 && Seed[1] == 0 && Seed[2] == 0 && Seed[3] == 0);
                UDS_ISO14229_Transfer[1] = Seed[0];
                UDS_ISO14229_Transfer[2] = Seed[1];
                UDS_ISO14229_Transfer[3] = Seed[2];
                UDS_ISO14229_Transfer[4] = Seed[3];
                if (AbleResponseType == Ser27BanResponse)
                {
                    if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                    {
                        UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
                    }
                }
                else
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
                }
                CalculateKey();
            }
            else if (NormalKeyUnlock == DiagLockFlag)
            {
                UDS_ISO14229_Transfer[1] = 0;
                UDS_ISO14229_Transfer[2] = 0;
                UDS_ISO14229_Transfer[3] = 0;
                UDS_ISO14229_Transfer[4] = 0;
                if (AbleResponseType == Ser27BanResponse)
                {
                    if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                    {
                        UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
                    }
                }
                else
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
                }
            }
        }
        else if (sendKey_LV1 == SubFunction)
        {
            Services27_01_Requested = 0;
            Ser27_FlowCtrlCnt.RequestSeedCnt = 0;
            Ser27_FlowCtrlCnt.Attemptcnt = 0;
            DiagLockFlag = NormalKeyUnlock;
            if (AbleResponseType == Ser27BanResponse)
            {
                if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
                }
            }
            else
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
            }
            // WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NeedWait);
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

/******************************************************************************
 28# - CommunicationControl
 ******************************************************************************/
static const uint8_t Ser28BanResponse          = AbleResponseType;
1242
static const uint8_t Ser28Sub []               = {enableRxAndTx,/*enableRxAndDisableTx, disableRxAndEnableTx, */disableRxAndTx};
李俭双's avatar
李俭双 committed
1243
static const uint8_t Ser28MinLen []            = {2};
1244
static const uint8_t Ser28SubLen []            = {2, /*2, 2,*/ 2};
李俭双's avatar
李俭双 committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
static const uint8_t Ser28UnlockLevel []       = {NoNeed, NoNeed, NoNeed, NoNeed};
static const uint8_t Ser28SupportSession       = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser28SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_28_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t i;
    uint8_t si = CommunicationControl;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t FunorPhy = A_TA_type;
    uint16_t Service28DiagDataLength = A_Length;

    for (i = 0; i < Service28DiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser28SupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service28DiagDataLength, Ser28MinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (AbleResponseType == Ser28BanResponse)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser28Sub, sizeof(Ser28Sub), &Index);
    }

    if (0 == NrcCode && Index < sizeof(Ser28UnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser28UnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser28SubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser28SubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser28SubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service28DiagDataLength, Ser28SubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

1306 1307 1308 1309 1310 1311 1312
    // if (0 == NrcCode)
    // {
    //     if (normalCommunicationMessages != UDS_ISO14229_Transfer[1])
    //     {
    //         NrcCode = requestOutOfRange;
    //     }
    // }
李俭双's avatar
李俭双 committed
1313 1314 1315 1316 1317 1318 1319

    if (0 == NrcCode)
    {
        if (enableRxAndTx == SubFunction)
        {
            if (UDS_ISO14229_Transfer[1] & normalCommunicationMessages)
            {
1320 1321
                CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
                CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);
李俭双's avatar
李俭双 committed
1322 1323 1324 1325 1326 1327 1328 1329 1330
            }
            if (UDS_ISO14229_Transfer[1] & networkManagementCommunicationMessages)
            {
            }
        }
        else if (disableRxAndTx == SubFunction)
        {
            if (UDS_ISO14229_Transfer[1] & normalCommunicationMessages)
            {
1331 1332
                CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Disable);
                CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
李俭双's avatar
李俭双 committed
1333 1334 1335 1336
            }
            if (UDS_ISO14229_Transfer[1] & networkManagementCommunicationMessages)
            {
            }
1337 1338 1339
            if (UDS_ISO14229_Transfer[1] & networkManagementCommunicationMessagesnormalCommunicationMessages)
            {
            }
李俭双's avatar
李俭双 committed
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
        }

        if (AbleResponseType == Ser28BanResponse)
        {
            if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
            }
        }
        else
        {
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

/******************************************************************************
 3E# - TesterPresent
 ******************************************************************************/
static const uint8_t Ser3EBanResponse          = AbleResponseType;
static const uint8_t Ser3ESub []               = {0x00};
static const uint8_t Ser3EMinLen []            = {1};
static const uint8_t Ser3ESubLen []            = {1};
static const uint8_t Ser3EUnlockLevel []       = {NoNeed};
static const uint8_t Ser3ESupportSession       = DefaultMode | ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser3ESubSupportSession [] = {DefaultMode | ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t i;
    uint8_t si = TesterPresent;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t Service3EDiagDataLength = A_Length;
    uint16_t FunorPhy = A_TA_type;

    for (i = 0; i < Service3EDiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser3ESupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service3EDiagDataLength, Ser3EMinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (AbleResponseType == Ser3EBanResponse)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser3ESub, sizeof(Ser3ESub), &Index);
    }

    if (0 == NrcCode && Index < sizeof(Ser3EUnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser3EUnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser3ESubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser3ESubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser3ESubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service3EDiagDataLength, Ser3ESubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

    if (0 == NrcCode)
    {
        if (0x00 == SubFunction)
        {
            S3_Server_refresh = 1;
            if (AbleResponseType == Ser3EBanResponse)
            {
                if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
                {
                    UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
                }
            }
            else
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
            }
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

/******************************************************************************
 85# - ControlDTCSetting
 ******************************************************************************/
static const uint8_t Ser85BanResponse          = AbleResponseType;
static const uint8_t Ser85Sub []               = {DTCSettingOn, DTCSettingOff};
static const uint8_t Ser85MinLen []            = {1};
static const uint8_t Ser85SubLen []            = {1, 1};
static const uint8_t Ser85UnlockLevel []       = {NoNeed, NoNeed};
static const uint8_t Ser85SupportSession       = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser85SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_85_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
    uint8_t i;
    uint8_t si = ControlDTCSetting;
    uint8_t NrcCode = 0;
    uint8_t Index = 0;
    uint8_t SubFunction = 0;
    uint16_t Service85DiagDataLength = A_Length;
    uint16_t FunorPhy = A_TA_type;

    for (i = 0; i < Service85DiagDataLength; ++i)
    {
        UDS_ISO14229_Transfer[i] = *(A_Data + i);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC7FFilter(Ser85SupportSession);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC13Filter(MinLengthType, Service85DiagDataLength, Ser85MinLen, Index);
    }

    if (0 == NrcCode)
    {
        if (AbleResponseType == Ser85BanResponse)
        {
            SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
        }
        else
        {
            SubFunction = UDS_ISO14229_Transfer[0];
        }
        NrcCode = SerXXNRC12Filter(SubFunction, Ser85Sub, sizeof(Ser85Sub), &Index);
    }

    if (0 == NrcCode && Index < sizeof(Ser85UnlockLevel))
    {
        NrcCode = SerXXNRC33Filter(Ser85UnlockLevel, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser85SubSupportSession)))
    {
        NrcCode = SerXXNRC7EFilter(Ser85SubSupportSession, Index);
    }

    if (0 == NrcCode && (Index < sizeof(Ser85SubLen)))
    {
        NrcCode = SerXXNRC13Filter(SubLengthType, Service85DiagDataLength, Ser85SubLen, Index);
    }

    if (0 == NrcCode)
    {
        NrcCode = SerXXNRC22Filter();
    }

    if (0 == NrcCode)
    {
        if (DTCSettingOn == SubFunction)
        {
            // CAN_DTC_OFF = 0;
        }
        else if (DTCSettingOff == SubFunction)
        {
            // CAN_DTC_OFF = 1;
        }

        if (AbleResponseType == Ser85BanResponse)
        {
            if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
            {
                UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
            }
        }
        else
        {
            UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
        }
    }
    else
    {
        if (DIAG_ID_Rx_FUN == FunorPhy)
        {
            if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
            {
                NegRes.code = NrcCode;
                UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
                return;
            }
        }
        else
        {
            NegRes.code = NrcCode;
            UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
            return;
        }
    }
}

/*************IGN on,S3 Timeout****************/
void DIAG_InitParameter(void)
{
    S3_ServerCnt = 0;
    S3_ServerEN  = 0;
    SessionType  = DefaultSession;

    DiagLockFlag            = 0;
    RANDOM                  = 1573;
    Services27_01_Requested = 0;
}