/*! * @file uds_services.c * @brief UDS services definition * */ #include "uds_config.h" #if BOOTLOADER_CODE_FLAG uint32_t FBL_BaseAdrress = APP_FlashBaseAddress; uint32_t FBL_AppSize = 0; #endif extern uint16_t TP_Rx_Message_Length; /*! * Enum Type. Service State */ typedef enum { UDS_SERVICE_NOT_START, /*!< Service not start */ UDS_SERVICE_TRANSMITED, /*!< Service Request transmited */ UDS_SERVICE_POSRSP_RECEIVED, /*!< Positive Response Received */ UDS_SERVICE_NEGRSP_RECEIVED, /*!< Negtive Response Received */ UDS_SERVICE_PENDING_RESPONSE /*!< Pending Response Received */ } UDS_SERVICE_STATE_T; /*! * Struct Type. Service control */ typedef struct { uint8_t service; /*!< Service ID */ UDS_SERVICE_STATE_T state; /*!< Service state */ uint8_t sn; /*!< Sequence Number */ uint16_t timer; /*!< Timer */ } UDS_SERVICE_CTRL_T; #define SERVICE_TIMEOUT_THRESSHOLD 30 /*!< Timeout Time */ static UDS_SERVICE_CTRL_T UDS_CurService; /*!< Current service control */ static uint8_t service_buf[128]; /*! * @brief UDS_Service_Init * Initialization function for UDS service * */ void UDS_Service_Init(void) { #if BOOTLOADER_CODE_FLAG //memset(service_buf, 0x00, 10); if(GetUdsUpdataFlag() == true) { UdsService.SessionDiagModel = _UDS_PROGRAM_SESSION; UdsService.UpDataFlg=true; UdsService.SeedKeyIsOK = 0; UdsService.AlreadSendSeed = false; UdsService.SessionTimer=0; UdsService.NcmDisRxAndTx = 0; UdsService.NmcmDisRxAndTx = 0; UdsService.DisDTCRecord=0; UdsService.KeyErrLockTimer = 0; UdsService.KeyLockFlg = false; service_buf[0] = 0x50; service_buf[1] = UdsService.SessionDiagModel; service_buf[2] = (uint8_t)(UDS_P2Sever>>8); service_buf[3] = (uint8_t)(UDS_P2Sever); service_buf[4] = (uint8_t)(UDS_P2SeverMax>>8); service_buf[5] = (uint8_t)(UDS_P2SeverMax); HAL_UDS_Transmit_Physical_Request(6,service_buf); } #endif UDS_CurService.service = 0; UDS_CurService.state = UDS_SERVICE_POSRSP_RECEIVED; UDS_CurService.sn =1; UDS_CurService.timer = 0; //TP_Init(); } #if BOOTLOADER_CODE_FLAG extern void RAMFLASHInit(void); /*! * @brief UDS_Service_37 * Service function for service 37 * */ void UDS_Service_37(uint8_t *pdata,uint16_t length) { uint8_t ucErrCode=0; service_buf[0] = 0x77; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { if (true == UdsService.PhyFlg) { UDS_CurService.service= 0x77; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; UDS_CurService.state = UDS_SERVICE_NOT_START; if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,0)) { if(1==UdsService.SeedKeyIsOK) { /*if(true == CheckUdsDlcIsOk(UdsService.RxDlc,5)) { if(true == CheckVechileStatus()) { service_buf[0] = 0x77; UdsUpData.GetCrc = (pdata[0]<<24) + (pdata[1] << 16) |(pdata[2]<<8) + pdata[3]; HAL_UDS_Transmit_Physical_Request(1,service_buf); } else ucErrCode=0x22; } else */ if(true == CheckUdsDlcIsOk(UdsService.RxDlc,1)) { //if(true == CheckVechileStatus()) { if (UdsUpData.DownloadSeq == DOWNLOAD_SEQUENCE_36) { service_buf[0] = 0x77; service_buf[1] = ~((uint8_t)UdsUpData.checksum); UdsUpData.DownloadSeq = DOWNLOAD_SEQUENCE_37; HAL_UDS_Transmit_Physical_Request(2,service_buf); } else { ucErrCode=0x24; } //} else // ucErrCode=0x22; } else ucErrCode=0x13; } else ucErrCode=0x33; } else ucErrCode=0x7F; if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x37,ucErrCode); } UdsService.TxTimerOut = 10; } } } void UDS_Service_36_Ack(uint8_t* routine_buf, uint8_t bufLen) { uint8_t ucErrCode=0;//Rsq=0; uint16_t len = 0; //memset(service_buf, 0x00, 10); //(void)bufLen; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service= 0x36; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; if (true == UdsService.PhyFlg) { ucErrCode = Set0x36DiagData(routine_buf,bufLen,service_buf,(uint16_t*)&len); if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x36,ucErrCode); } UdsService.TxTimerOut = SERVICE_TIMEOUT_THRESSHOLD; } } } /*! * @brief UDS_Service_34 * Service function for service 34 * * @param dataformat data format * @param addrLenFormat address and length format * @param memBuf address data buffer pointer * @param sizeBuf size data buffer pointer */ void UDS_Service_34_Ack(uint8_t *pDst,uint16_t pLen) { uint8_t ErrCode=0x0/*,hlen=0,lLen=0,aLen=0*/; uint8_t dataFormatIdentifier, addressAndLengthFormatIdentifier; //(void)pLen; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { if (false == UdsService.PhyFlg) { return; } if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,0)) { if(1==UdsService.SeedKeyIsOK) { dataFormatIdentifier = pDst[0]; addressAndLengthFormatIdentifier = pDst[1]; // service_buf[0] = 0x74; // service_buf[1] = pDst[0]; // hlen = ((pDst[1]>>4)&0x0F); // lLen = (pDst[1]&0x0F); // aLen = hlen+lLen; //if(true == CheckUdsDlcIsOk(pLen,8)) if(pLen == (0x0B-1)) { //if(true == CheckVechileStatus()) { if((dataFormatIdentifier!=0x0) || (addressAndLengthFormatIdentifier != 0x44)) { ErrCode=0x31; } else { FBL_BaseAdrress = ((uint32_t)pDst[2]<<24) | ((uint32_t)pDst[3]<<16) | ((uint32_t)pDst[4]<<8) | ((uint32_t)pDst[5]<<0); FBL_AppSize = ((uint32_t)pDst[6]<<24) | ((uint32_t)pDst[7]<<16) | ((uint32_t)pDst[8]<<8) | ((uint32_t)pDst[9]<<0); service_buf[0] = 0x74; service_buf[1] = dataFormatIdentifier; service_buf[1] = 0x20; service_buf[2] = MSB(FMC_PAGE_SIZE+2); service_buf[3] = LSB((FMC_PAGE_SIZE+2)); UdsUpData.Sn = 1; UdsUpData.totalRecvLength = 0; UdsUpData.OffSetAdd = 0; if ((FBL_BaseAdrress == APP_FlashBaseAddress) && (FBL_AppSize < BOOTLOADER_APP_DATA_LEN_MAX)) { if (UdsUpData.RoutineEraseMemoryFlg) { HAL_UDS_Transmit_Physical_Request(4, service_buf); } else { ErrCode=0x22; } } else { ErrCode=0x70; } } //} else // ErrCode=0x22; } else { ErrCode=0x13; } } else { ErrCode=0x33; } } else { ErrCode=0x7F; } if(ErrCode) { HAL_UDS_Transmit_Pending_Response(0x34,ErrCode); } else { UdsUpData.DownloadSeq = DOWNLOAD_SEQUENCE_34; } UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; UdsService.TxTimerOut = 20; } } #endif /*! * @brief UDS_Service_31 * Service function for service 31 * * @param routine_buf routine buffer pointer * @param bufLen routine buffer length */ void UDS_Service_31(uint8_t* routine_buf, uint8_t bufLen) { uint8_t ucErrCode=0; uint16_t len = 0; // uint16_t ReqId = 0; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service= 0x31; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; if (true == UdsService.PhyFlg) { if (true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_DEFAULT_SESSION,1)) { ucErrCode = Set0x31DiagData(routine_buf,bufLen,service_buf,(uint16_t*)&len); } else { ucErrCode = 0x7F; } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x31,ucErrCode); // Debug("@0x31 NG Repose!!! \r\n"); } else { HAL_UDS_Transmit_Physical_Request(len, service_buf); } UdsService.TxTimerOut = 10; } } } void UDS_Service_28(uint8_t* requ, uint8_t bufLen) { uint8_t ucErrCode=0,ucRsp=0; uint8_t comCrtlType = 0; (void)bufLen; service_buf[0] = 0x68; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service= 0x28; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_DEFAULT_SESSION,1)) { if(true == CheckUdsDlcIsOk(UdsService.RxDlc,3)) { if(true == CheckVechileStatus()) { ucRsp=requ[0]&0x7F; comCrtlType = requ[1]; if ((0x0 == ucRsp) || (0x03 == ucRsp)) { if ((comCrtlType == 1) || (comCrtlType == 3)) { if (0x0 == ucRsp) { if (comCrtlType == 1) { UdsService.NcmDisRxAndTx = 0; // } else if (comCrtlType == 2) { // UdsService.NmcmDisRxAndTx = 0; } else { UdsService.NcmDisRxAndTx = 0; UdsService.NmcmDisRxAndTx = 0; } } else if(0x03 == ucRsp) { if (comCrtlType == 1) { UdsService.NcmDisRxAndTx = 1; // } else if (comCrtlType == 2) { // UdsService.NmcmDisRxAndTx = 1; } else { UdsService.NcmDisRxAndTx = 1; UdsService.NmcmDisRxAndTx = 1; } } if (true == CheckSubSidNeedPosResp(requ[0])) { service_buf[1] = requ[0]; HAL_UDS_Transmit_Physical_Request(2, service_buf); } } else { if (UdsService.CanID == UDS_PHY_TX_ID) { ucErrCode=0x31; } } } else { if(true== UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } } else ucErrCode = 0x22; } else ucErrCode = 0x13; } else { if (UdsService.CanID == UDS_PHY_TX_ID) { ucErrCode = 0x7F; } } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x28,ucErrCode); } UdsService.TxTimerOut = 10; } } void UDS_Service_85(uint8_t* routine_buf, uint8_t bufLen) { uint8_t ucErrCode=0,Rsq=0; (void)bufLen; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service= 0x85; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; //UdsService.SessionDiagModel=3; if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_DEFAULT_SESSION,1)) { if(true == CheckUdsDlcIsOk(UdsService.RxDlc,2)) { if(true != CheckVechileStatus()) ucErrCode = 0x22; else { Rsq = routine_buf[0]&0x7F; if((0x01 == Rsq)||(0x02 == Rsq)) { if(0x01 == Rsq) { UdsService.DisDTCRecord=0; } else { UdsService.DisDTCRecord=1; } if(true == CheckSubSidNeedPosResp(routine_buf[0])) { service_buf[0] = 0xC5; service_buf[1] = routine_buf[0]; HAL_UDS_Transmit_Physical_Request(2,service_buf); } //else //ucErrCode = 0x12; } else { if(true== UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } } } else ucErrCode=0x13; } else { if (UdsService.CanID == UDS_PHY_TX_ID) { ucErrCode = 0x7F; } } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x85,ucErrCode); } UdsService.TxTimerOut = 10; } } extern DIDs gDIDCache; void UDS_Nrc78PostProcessing(void) { uint8_t dlen = 0; #if BOOTLOADER_CODE_FLAG //uint32_t address = 0; //uint16_t len = 0; status_t ret = STATUS_ERROR; uint8_t result = 0; uint32_t dataLen = 0; uint32_t eraseSize = 0; static uint32_t eraseAddress = 0, eraseLength = 0; static uint32_t eraseAddrOffset = 0; static uint16_t erasePageCnt = 0; // const uint8_t app_flag[] = BCM_APP_FLAG; // uint8_t buffer[36] = {0}; // memset(service_buf, 0x00, 10); if (UdsUpData.RxEraseFlg == true) { UdsUpData.RxEraseFlg = false; if (UdsUpData.EraseMemoryFlg == false) { UdsUpData.EraseMemoryFlg = true; eraseAddress = UdsUpData.EraseAddress; eraseLength = UdsUpData.EraseLength; eraseAddrOffset = 0; erasePageCnt = 0; UdsUpData.EraseMemorySts = ERASE_MEMORY_STS_NONE; } else { UdsUpData.EraseMemorySts = ERASE_MEMORY_STS_FAILURE; UdsUpData.EraseMemoryFlg = false; } } else if (UdsUpData.EraseMemoryFlg == true) { UdsUpData.EraseMemorySts = ERASE_MEMORY_STS_IN_PROGRESS; //擦除一个页(2K)40ms if (eraseLength) { if (eraseLength > FMC_PAGE_SIZE) { eraseLength -= FMC_PAGE_SIZE; eraseSize = FMC_PAGE_SIZE; } else { eraseSize = eraseLength; eraseLength = 0; } } ret = EraseApplicationSoftwareCode(eraseAddress + eraseAddrOffset, eraseSize); eraseAddrOffset += eraseSize; erasePageCnt++; if (ret != STATUS_SUCCESS) { UdsUpData.EraseMemorySts = ERASE_MEMORY_STS_FAILURE; UdsUpData.EraseMemoryFlg = false; } else if (eraseLength == 0) { UdsUpData.EraseMemorySts = ERASE_MEMORY_STS_COMPLETED; UdsUpData.EraseMemoryFlg = false; service_buf[dlen++] = 0x71; service_buf[dlen++] = 0x01; service_buf[dlen++] = 0xFF; service_buf[dlen++] = 0x00; service_buf[dlen++] = 0x01; HAL_UDS_Transmit_Physical_Request(dlen,service_buf); } else if ((erasePageCnt % 20) == 0) { //20次发一次78NRC 20*40ms=800ms HAL_UDS_Transmit_Pending_Response(0x31,0x78); } } else if (UdsUpData.RxProgramFlag == true) { service_buf[dlen++] = 0x76; service_buf[dlen++] = UdsUpData.Sn; dataLen = UdsService.RxDlc-2; CalcCheckSum(&UdsUpData.checksum, UdsUpData.Buf, dataLen); if (UdsUpData.OffSetAdd == 0) { CRC16_CCITT_1021_Init(&UdsUpData.CalcCrc); CRC16_CCITT_1021_Update(&UdsUpData.CalcCrc, UdsUpData.Buf, 4); UdsUpData.StackTopAdr = (*(__IO uint32_t*)((uint32_t)UdsUpData.Buf));//保存栈顶地址 memset(UdsUpData.Buf, 0xFF, 4); } ret = ProgramApplicationSoftware(APP_FlashBaseAddress + UdsUpData.OffSetAdd, UdsUpData.Buf, dataLen); UdsUpData.OffSetAdd += dataLen; if (UdsUpData.totalRecvLength >= FBL_AppSize) { dataLen = FBL_AppSize - 4; //crc16校验 CRC16_CCITT_1021_Update(&UdsUpData.CalcCrc, (uint8_t*)(APP_FlashBaseAddress+4), dataLen); } if (ret == STATUS_SUCCESS) { HAL_UDS_Transmit_Physical_Request(dlen, service_buf); } else { UdsUpData.StackTopAdr = 0; HAL_UDS_Transmit_Pending_Response(0x36,0x72); } UdsUpData.Sn++; UdsUpData.RxProgramFlag = false; } #else #endif } ////////////////////////////////////////////////////////////////////// void UDS_Service_11(uint8_t* requ) { uint8_t ucErrCode=0,ucResp=0; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service= 0x11; UDS_CurService.state = UDS_SERVICE_NOT_START; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; if (true == UdsService.PhyFlg) { if(true == CheckUdsDlcIsOk(UdsService.RxDlc,2)) { ucResp=requ[0]&0x7F; if(CheckIsSupportSubFunc_Sid11(ucResp)) { if(true == CheckVechileStatus()) { if(true == CheckSubSidNeedPosResp(requ[0])) { service_buf[0] = 0x51; service_buf[1] = requ[0]; HAL_UDS_Transmit_Physical_Request(2,service_buf); } if(true == UdsUpData.UpModleFlg) { UdsUpData.UpModleFlg = false; service_buf[0]=0x00; service_buf[1]=0x00; //EEPROM_write(UDS_UPDATE_ADD,service_buf,2); //UdsUpData.UpCompFlg = true; } UdsService.ResetMcuFlg=TRUE; UdsService.RstTimer=200; //Debug("Mcu Rst of Uds\r\n"); } else { ucErrCode = 0x22; } } else { if(true== UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } } else { ucErrCode = 0x13; } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x11,ucErrCode); } UdsService.TxTimerOut = 10; } } } void UDS_Service_27(UDS_SERVICE_27_T in_service, uint8_t *requ, uint8_t* keyBuf) { uint8_t ucErrCode=0; uint8_t ret = 0xFF; uint16_t key; static uint8_t s_get_seed_flag; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { if (true == UdsService.PhyFlg) { if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_DEFAULT_SESSION,1)) { //memset(service_buf,0x0,18); #if BOOTLOADER_CODE_FLAG if(UDS_SERVICE_27_REQUEST_FBL_SEED==requ[0]) { #else if(UDS_SERVICE_27_REQUEST_SEED==requ[0]) { #endif s_get_seed_flag = 0; if(true == CheckUdsDlcIsOk(UdsService.RxDlc,2)) { if(UdsService.KeyLockFlg==true) ucErrCode = 0x37; else { if(true != CheckVechileStatus()) ucErrCode = 0x22; else { service_buf[0] = 0x67; service_buf[1] = requ[0]; ret = GenerateSID27SeedValue(seed_value); if( ret == 0) { if((1==UdsService.SeedKeyIsOK)&&(true==UdsService.AlreadSendSeed)) { memset(&service_buf[2],0x0,sizeof(seed_value)); //memcpy(&service_buf[2],seed_value,sizeof(seed_value)); //} else if (UdsService.SeedKeyLevel == UDS_SERVICE_27_SEND_KEY) { // memset(&service_buf[2],0x0,sizeof(seed_value)); } else { memcpy(&service_buf[2],seed_value,sizeof(seed_value)); } s_get_seed_flag = 1; UdsService.AlreadSendSeed = true; UDS_CurService.service= 0x67; UDS_CurService.state = UDS_SERVICE_NOT_START; HAL_UDS_Transmit_Physical_Request(2+2, service_buf); } } } } else ucErrCode = 0x13; #if BOOTLOADER_CODE_FLAG } else if(UDS_SERVICE_27_SEND_FBL_KEY == requ[0]) { #else } else if(UDS_SERVICE_27_SEND_KEY == requ[0]) { #endif if(CheckUdsDlcIsOk(UdsService.RxDlc,sizeof(key_value)+2)) { if(true != CheckVechileStatus()) ucErrCode = 0x22; else { if (/*(UdsService.KeyLockFlg == true) || */(UdsService.KeyErrCnt > UDS_27SERVICE_ERR_NUM_MAX) || (s_get_seed_flag == 0)) { if(UdsService.KeyErrCnt >= 3) ucErrCode = 0x36; if(s_get_seed_flag == 0) ucErrCode = 0x24; } else { if (s_get_seed_flag == 1) s_get_seed_flag = 0; memset(service_buf,0x0,sizeof(key_value)+2); if(memcmp(service_buf,seed_value,sizeof(seed_value))) { //Bug key = GenerateSID27KeyValue(WORD(seed_value[0], seed_value[1])); // if (ret == 0) { keyBuf[0] = requ[1]; keyBuf[1] = requ[2]; keyBuf[2] = requ[3]; keyBuf[3] = requ[4]; key_value[0] = MSB(key); key_value[1] = LSB(key); if(!memcmp(keyBuf,key_value,sizeof(key_value))) { UdsService.KeyErrCnt=0; service_buf[0] = 0x67; service_buf[1] = requ[0]; // UdsService.SeedKeyLevel = (UDS_SERVICE_27_SEND_KEY == requ[0])?SECURITY_LEVEL_1:SECURITY_LEVEL_FBL; UDS_CurService.service= 0x67; UDS_CurService.state = UDS_SERVICE_NOT_START; UdsService.SeedKeyIsOK = TRUE; HAL_UDS_Transmit_Physical_Request(2, service_buf); } else { // if(UdsService.KeyErrCnt<0xFF) UdsService.KeyErrCnt++; if((UDS_27SERVICE_ERR_NUM_MAX-1) >= UdsService.KeyErrCnt) ucErrCode = 0x35; else if(UDS_27SERVICE_ERR_NUM_MAX == UdsService.KeyErrCnt) { ucErrCode = 0x36; UdsService.KeyErrLockTimer=10; UdsService.KeyLockFlg=true; } } // } else { // ucErrCode = 0x12; // } } else { ucErrCode = 0x24; } memset(seed_value,0x0,sizeof(key_value)+2); } } } else ucErrCode = 0x13; } else { if(UdsService.RxDlc<2) { ucErrCode = 0x13; } else ucErrCode = 0x12; } } else ucErrCode = 0x7F; if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x27,ucErrCode); } UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; UdsService.TxTimerOut = SERVICE_TIMEOUT_THRESSHOLD; } } } void UDS_Service_10_Response(uint8_t in_session) { uint8_t Len=2,ucErrCode=0,ucResp=0; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service = 0x10; UDS_CurService.state = UDS_SERVICE_NOT_START; if( true == CheckUdsDlcIsOk(UdsService.RxDlc,2) ) { //memset(service_buf,0x0,8); ucResp = in_session&0x7F; if((_UDS_DEFAULT_SESSION == ucResp)||(_UDS_PROGRAM_SESSION == ucResp)||(_UDS_EXT_SESSION == ucResp)) { if(UdsService.SessionDiagModel == _UDS_EXT_SESSION && ucResp == _UDS_PROGRAM_SESSION) { if (CheckVechileStatus()) { UdsService.SessionDiagModel = ucResp; #if (BOOTLOADER_CODE_FLAG == 0) SetUdsUpdataFlag(); //Debug("Goto App1 Uds Updata\r\n"); UdsService.UpDataFlg=true; UdsService.ResetMcuFlg=TRUE; UdsService.RstTimer=200; ucErrCode = 0x78; #else UdsService.SessionDiagModel=ucResp; UdsService.SeedKeyIsOK = 0; UdsService.AlreadSendSeed = false; UdsService.SessionTimer=0; UdsService.NcmDisRxAndTx = 0; UdsService.NmcmDisRxAndTx = 0; UdsService.DisDTCRecord=0; if(true == CheckSubSidNeedPosResp(in_session)) { service_buf[0] = 0x50; service_buf[1] = (uint8_t)in_session; service_buf[2] = (uint8_t)(UDS_P2Sever>>8); service_buf[3] = (uint8_t)(UDS_P2Sever); service_buf[4] = (uint8_t)(UDS_P2SeverMax>>8); service_buf[5] = (uint8_t)(UDS_P2SeverMax); Len = 6; HAL_UDS_Transmit_Physical_Request(Len,service_buf); } UdsUpData.RoutineEraseMemoryFlg = false; UdsUpData.RoutineChkProgIntegrityFlg = false; UdsUpData.RoutineChkProgIntegrityPass = false; UdsUpData.RoutineEraseMemoryErr = false; UdsUpData.PrintFingerFlg = false; UdsUpData.DownloadSeq = DOWNLOAD_SEQUENCE_IDLE; #endif } else { ucErrCode = 0x22; } } else if ((UdsService.SessionDiagModel == _UDS_DEFAULT_SESSION && ucResp == _UDS_PROGRAM_SESSION) \ || (UdsService.SessionDiagModel == _UDS_PROGRAM_SESSION && ucResp == _UDS_EXT_SESSION)) { ucErrCode = 0x22; } else { if (CheckVechileStatus()) { if (ucResp == _UDS_EXT_SESSION) { UdsService.ChkProgPreconditionFlg = false; #if BOOTLOADER_CODE_FLAG == 0 } else if (ucResp == _UDS_DEFAULT_SESSION) { IO_InputOutputRelease(); #else } else if ((ucResp == _UDS_DEFAULT_SESSION) && (UdsService.SessionDiagModel == _UDS_PROGRAM_SESSION)) { // //在编程会话模式下,“0x10 0x01”会导致 ECU 重启 // UdsService.ResetMcuFlg=TRUE; // UdsService.RstTimer=200; #endif } UdsService.SessionDiagModel=ucResp; UdsService.SessionTimer=0; UdsService.SeedKeyIsOK = 0; UdsService.AlreadSendSeed = false; if (!(UdsService.SessionDiagModel == _UDS_EXT_SESSION && ucResp == _UDS_EXT_SESSION)) { UdsService.NcmDisRxAndTx = 0; UdsService.NmcmDisRxAndTx = 0; UdsService.DisDTCRecord=0; } if(true == CheckSubSidNeedPosResp(in_session)) { service_buf[0] = 0x50; service_buf[1] = (uint8_t)in_session; service_buf[2] = (uint8_t)(UDS_P2Sever>>8); service_buf[3] = (uint8_t)(UDS_P2Sever); service_buf[4] = (uint8_t)(UDS_P2SeverMax>>8); service_buf[5] = (uint8_t)(UDS_P2SeverMax); Len = 6; HAL_UDS_Transmit_Physical_Request(Len,service_buf); } } else { ucErrCode = 0x22; } } } else { if(true== UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } } else ucErrCode = 0x13; if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x10,ucErrCode); } UDS_CurService.timer = 10; UdsService.TxTimerOut = UDS_CurService.timer; } } void UDS_Service_0x22_Response(void) { uint8_t ucErrCode=0; uint16_t Len=0; if ((UDS_CurService.state > UDS_SERVICE_TRANSMITED) && (true == UdsService.PhyFlg)) { //if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,1)) { //if(true == CheckUdsDlcIsOk(UdsService.RxDlc,3)) if(UdsService.RxDlc>=3) { //if(true != CheckVechileStatus()) ucErrCode = 0x22; //else { if(1==(UdsService.RxDlc%2) && UdsService.RxDlc <= 3) { //memset(service_buf,0x0,128); Len = Get0x22SerDiagData(service_buf); if(Len>=3) { HAL_UDS_Transmit_Physical_Request(Len, service_buf); } else { if (UdsService.CanID == UDS_PHY_TX_ID) { ucErrCode = 0x31; } } } else ucErrCode = 0x13; //} //} else // ucErrCode = 0x13; } else { ucErrCode = 0x13; } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x22,ucErrCode); } //UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; UdsService.TxTimerOut = 30 ; } } void UDS_Service_0x2E_Response(INT8U *pIn,INT16U pLen) { uint8_t rst=0,ucErrCode=0; if ((UDS_CurService.state > UDS_SERVICE_TRANSMITED) && (true == UdsService.PhyFlg)) { #if BOOTLOADER_CODE_FLAG if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,0)) { #else if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_EXT_SESSION,0)) { #endif if( pLen < 3 ) { ucErrCode = 0x13; } else { rst = Set0x2EDidDiagData(pIn,pLen-2); if(rst == 0) { service_buf[0]=0x6E; service_buf[1]=pIn[0]; service_buf[2]=pIn[1]; HAL_UDS_Transmit_Physical_Request(3,service_buf); } else { ucErrCode = rst; } } } else ucErrCode = 0x7F; if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x2E,ucErrCode); } //UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; UdsService.TxTimerOut = 40; } } void UDS_Service_0x3E_Response(INT8U *pIn) { uint8_t ucErrCode=0,Rsp=0; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { if(true == CheckUdsDlcIsOk(UdsService.RxDlc,2)) { Rsp = pIn[0]&0x7F; if(0==Rsp) { if(true == CheckSubSidNeedPosResp(pIn[0])) { service_buf[0]=0x7E; service_buf[1]=pIn[0]; HAL_UDS_Transmit_Physical_Request(2,service_buf); } } else { // if(true== UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } } else ucErrCode = 0x13; if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x3E,ucErrCode); } UdsService.TxTimerOut = 10; } } #if BOOTLOADER_CODE_FLAG == 0 void UDS_Service_14(uint8_t* routine_buf, uint8_t bufLen) { uint8_t ucErrCode=0; uint32_t jj; (void)bufLen; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { UDS_CurService.service= 0x14; UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; jj = (routine_buf[0]<<16)+(routine_buf[1]<<8)+routine_buf[2]; if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,1)) { if(true == CheckUdsDlcIsOk(UdsService.RxDlc,4)) { if(true == CheckVechileStatus()) { if(0xFFFFFF==jj) { // ClearDtc(); service_buf[0] = 0x54; HAL_UDS_Transmit_Physical_Request(1,service_buf); } else { if (UdsService.CanID == UDS_PHY_TX_ID) { ucErrCode = 0x31; } } } else ucErrCode = 0x22; } else ucErrCode = 0x13; } else ucErrCode = 0x7F; if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x14,ucErrCode); } UdsService.TxTimerOut = 10; } } void UDS_Service_0x19_Response(void) { uint8_t Len=0,ucErrCode=0,Rsp=0; if ((UDS_CurService.state > UDS_SERVICE_TRANSMITED) && (true == UdsService.PhyFlg)) { UDS_CurService.service = 0x19; UDS_CurService.state = UDS_SERVICE_NOT_START; if(true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,1)) { if( UdsService.RxDlc >= 2 ) { Rsp = UdsService.ReqCmdBuf[0]&0x7F; switch(Rsp) { case 0x01: case 0x02: if (CheckIsSupportSubFunc_Sid19(Rsp)) { if (true == CheckUdsDlcIsOk(UdsService.RxDlc,3)) { Len = Get0x19SerDiagData(service_buf,Rsp,&UdsService.ReqCmdBuf[0]); // if(Len) { HAL_UDS_Transmit_Physical_Request(Len, service_buf); } else { ucErrCode = 0x31; } } else ucErrCode = 0x13; } else ucErrCode = 0x12; break; case 0x04: if (CheckIsSupportSubFunc_Sid19(Rsp)) { if (true == CheckUdsDlcIsOk(UdsService.RxDlc,6)) { Len = Get0x19SerDiagData(service_buf,Rsp,&UdsService.ReqCmdBuf[0]); // if(Len > 3) { //for(int i=0;i 3) { //for(int i=0;i UDS_SERVICE_TRANSMITED) { if (true == UdsService.PhyFlg) { if (true == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_EXT_SESSION,0)) { if (1 == UdsService.SeedKeyIsOK) { ucErrCode = Set0x2FDiagData(UdsService.ReqCmdBuf,UdsService.RxDlc,service_buf,&len); } else { ucErrCode = 0x33; } } else { ucErrCode = 0x7F; } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(0x2F,ucErrCode); } else { HAL_UDS_Transmit_Physical_Request(len,service_buf); } UdsService.TxTimerOut = 10; } } } #endif void UDS_Service_NonSupport_Response(INT8U pIn) { uint8_t ucErrCode=0; if(UDS_CurService.state > UDS_SERVICE_TRANSMITED) { #if BOOTLOADER_CODE_FLAG == 1 if((0x34==UdsService.Id)||(0x36==UdsService.Id)||(0x37==UdsService.Id)) { if(false == CheckIsBeyonSession(UdsService.SessionDiagModel,_UDS_PROGRAM_SESSION,0)) ucErrCode=0x7f; } else #endif { if(true== UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x11; } } if(ucErrCode) { HAL_UDS_Transmit_Pending_Response(pIn,ucErrCode); } //UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; UdsService.TxTimerOut = 10; } } /*! * @brief UDS_Service_PosRsp_Handler * Posive response handler * * @param length data length * @param rsp_buf response data buffer pointer */ static void UDS_Service_PosRsp_Handler(uint16_t length, uint8_t* rsp_buf) { (void)length; //demo here //define the handler behavior by customer. if(UDS_CurService.service == 0x27) { if(rsp_buf[1] == 0x01) { UDS_Demo_Calculate_Key(rsp_buf); } } //UDS_Pos_Response_Callback(); } /*! * @brief UDS_Service_NegRsp_Handler * Negtive response handler * * @param negRsp Negtive response code */ static void UDS_Service_NegRsp_Handler(uint8_t negRsp) { (void)negRsp; //define the handler behavior by customer. UDS_Neg_Response_Callback(); } /*! * @brief UDS_Service_Pending * Send pending request * */ void UDS_Service_Pending(void) { //define the handler behavior by customer. UDS_CurService.timer = SERVICE_TIMEOUT_THRESSHOLD; } /*! * @brief UDS_Service_Transmit_Confirmed * Callback function after the message is transmitted * */ void UDS_Service_Transmit_Confirmed(void) { UDS_CurService.state = UDS_SERVICE_TRANSMITED; } /*! * @brief UDS_Service_Response_Handler * UDS Service Response handler * * @param TPReceivexMessageLength TP Receive Message Length * @param N_Result TP Result * @param Rec_Bufer TP Receive Buffer pointer */ void UDS_Service_Response_Handler(uint16_t TPReceivexMessageLength, uint8_t N_Result, uint8_t* Rec_Bufer) { uint8_t i; if((0 == N_Result) &&(UDS_CurService.state == UDS_SERVICE_TRANSMITED)) { if((UDS_CurService.service+0x40) == Rec_Bufer[0]) { UDS_Service_PosRsp_Handler(TPReceivexMessageLength, Rec_Bufer); UDS_CurService.state = UDS_SERVICE_POSRSP_RECEIVED; } else if(Rec_Bufer[0] == 0x7F) { if(UDS_CurService.service == Rec_Bufer[1]) { if(Rec_Bufer[2] == 0x78) { UDS_Service_Pending(); UDS_CurService.state = UDS_SERVICE_PENDING_RESPONSE; } else { UDS_Service_NegRsp_Handler(Rec_Bufer[2]); UDS_CurService.state = UDS_SERVICE_NEGRSP_RECEIVED; } } } else { } } else if((0 == N_Result)&&(UDS_CurService.state == UDS_SERVICE_POSRSP_RECEIVED)) { if(0x10 == Rec_Bufer[0]) { //UDS_Service_PosRsp_Handler(TPReceivexMessageLength, Rec_Bufer); UDS_CurService.state = UDS_SERVICE_POSRSP_RECEIVED; UdsService.RxSessionDiagModel = Rec_Bufer[1]; } UdsService.Id = Rec_Bufer[0]; for (i=0; i