#include "uds_config.h" #include "uds_nvm.h" #include "BSP_CAN.h" #if BOOTLOADER_CODE_FLAG == 0 #else #endif //INT32U g_ulRand=0; DIDs gDIDCache; //uint8_t g_testerprogramdateflag = false; //uint8_t g_testerserialnubnerflag = false; //extern INT8U strMcuAppst[4]; //extern INT8U strMcuAppDate[4]; extern INT32U ParseCanRxCnt; extern uint32_t FBL_AppSize; //_HSCANERROR HsCanError; uint32_t g_ulCanStatus; VehicleInfo2UdsType vehicleInfo2uds; UdsInfo2VehicleType udsInfo2Vehicle={true,true,true,true,true}; //UdsInfo2VehicleType udsInfo2Vehicle = { // .NcmRxEnable = true, // .NcmTxEnable = true, // .NmcmRxEnable = true, // .NmcmTxEnable = true, // .UdsOffLine = true, //}; uint8_t diagStart_InitTimer = 0; uint8_t diagStart_Timer = 0; DTC_STRUCT dtcComm; DTC_StatusType dtcStatusBak; DTC_Fault_Flags faultInfo; //故障信息 DTC_Options dtcOptions; const uint32_t DTC_LIST_TABLE[DTC_ID_NUM] = { DTC_FOREACH(GENERATE_DTC_VALUE_ARRAY) }; void DID_SignalInit(void) { uint8_t i; uint8_t index; uint16_t size; uint8_t *cachePtr, *didPtr; uint8_t *nvmDidPtr; DIDS_NvmType *dids; EE_Read(NVM_IMAGE_DID_ADDR, (uint8_t **)&dids); cachePtr = (uint8_t *)&gDIDCache; for (i = 0; i < DID_StoreInFlash_NUM; i++) { index = GetDIDIndexByID(DIDStoreInFlashId[i]); if (index != 0xff) { didPtr = cachePtr + DIDOffset[index]; size = DIDStoreInFlashSize[i]; nvmDidPtr = (uint8_t *)dids + DIDStoreInFlashOffset[i]; if (CRC16_DataCheck(nvmDidPtr, size) == 0) { memcpy(didPtr, nvmDidPtr, size); } } } #if BOOTLOADER_CODE_FLAG == 0 memcpy(gDIDCache.DID_BootSoftware, (uint8_t *)GetBootloaderSwVersionAddress(), \ DIDSize[BootSoftware]); #endif } void DTC_SignalInit(void) { #if BOOTLOADER_CODE_FLAG == 0 uint8_t index; uint8_t size; DTCS_NvmType *dtc, *dtcBak; EE_Read(NVM_IMAGE_DTC_ADDR, (uint8_t **)&dtc); size = sizeof(DTC_RecordDataBufType); for (index = 0; index < DTC_ID_NUM; index++) { if (CRC16_DataCheck((uint8_t *)&dtc->DTC_Status.recordData[index], size - 2) == 0) { memcpy((uint8_t *)&dtcComm.DTCStatus.recordData[index], (uint8_t *)&dtc->DTC_Status.recordData[index], size); memcpy((uint8_t *)&dtcStatusBak.recordData[index], (uint8_t *)&dtc->DTC_Status.recordData[index], size); } if (dtcComm.DTCStatus.recordData[index].status == ((1 << DTC_STATUS_Test_Failed) | (1 << DTC_STATUS_Confirmed_DTC))) { faultInfo.w |= 1 << index; } } diagStart_InitTimer = DIAG_START_INIT_TIME; diagStart_Timer = DIAG_START_TIME; dtcOptions.w = 0xffffffff; #endif } void UDS_ServiceInit(void) { //ECU 上电或复位后默认处于闭锁状态,且需启动 10s 的安全访问延时时间,同时错误计数器重置为零。 UdsService.KeyErrLockTimer = 10; UdsService.KeyLockFlg = true; UdsService.KeyErrCnt = 0; UdsService.SessionDiagModel = _UDS_DEFAULT_SESSION; UdsService.SeedKeyIsOK = 0; UdsService.AlreadSendSeed = false; UdsService.SessionTimer = 0; UdsService.NcmDisRxAndTx = 0; UdsService.NmcmDisRxAndTx = 0; UdsService.DisDTCRecord = 0; } void DTC_SignalRecover(void) { } void GenRand(void) { } #if BOOTLOADER_CODE_FLAG == 0 void SnapshotRecordLoad(DTC_RecordDataBufType *record) { record->snapshot = vehicleInfo2uds.Snapshot; } uint8_t SnapshotRecordFill(uint8_t *data, DTC_SnapshotType *snapshot) { uint8_t j = 0; data[j++] = (uint8_t)(0x4703 >> 8); data[j++] = (uint8_t)(0x4703 & 0x00ff); data[j++] = snapshot->PowerMode; data[j++] = (uint8_t)(0x4706 >> 8); data[j++] = (uint8_t)(0x4706 & 0x00ff); data[j++] = snapshot->TolOdometerH; data[j++] = snapshot->TolOdometerM; data[j++] = snapshot->TolOdometerL; data[j++] = (uint8_t)(0x470A >> 8); data[j++] = (uint8_t)(0x470A & 0x00ff); data[j++] = snapshot->DateSec & 0x3f; data[j++] = snapshot->DateMin & 0x3f; data[j++] = snapshot->DateHour & 0x1f; data[j++] = snapshot->DateDay & 0x1f; data[j++] = snapshot->DateMonth & 0x1f; data[j++] = snapshot->DateYear & 0x3f; data[j++] = (uint8_t)(0x470E >> 8); data[j++] = (uint8_t)(0x470E & 0x00ff); data[j++] = snapshot->VehSpeedH; data[j++] = snapshot->VehSpeedL; data[j++] = (uint8_t)(0x470F >> 8); data[j++] = (uint8_t)(0x470F & 0x00ff); data[j++] = snapshot->BatVolt; data[j++] = (uint8_t)(0x4710 >> 8); data[j++] = (uint8_t)(0x4710 & 0x00ff); data[j++] = (uint8_t)(snapshot->SOC >> 8); data[j++] = (uint8_t)(snapshot->SOC >> 0); return j; } uint8_t DiagDtcSnapshotRecordFill(uint8_t sub_func, DTC_MaskRecordTpye mask_record, uint8_t snap_record_num, DTC_RecordDataBufType *record_data, uint8_t *data_buf, uint8_t *length) { uint8_t ii = 0; uint8_t ret = 1; if (snap_record_num == 1 || snap_record_num == 0xff) { data_buf[(*length)++] = DIAG_SERVE_READ_DTC|0x40; data_buf[(*length)++] = sub_func; for (ii=0; ii= DTC_ID_NUM) { return; } if (dtc_record_data->status != status) { if (status == ((1<extended.FaultCounter != 0xff) { dtc_record_data->extended.FaultCounter ++; } } dtc_record_data->extended.FaultAgingCounter = 0; } dtc_record_data->status = status; } //电压正且非busoff、电源ON下 void DiagFaultsMonitor(void) { if (faultInfo.b.fECU_VOLTAGE_HIGH)return; if (faultInfo.b.fECU_VOLTAGE_LOW)return; if (CheckIsIgnON()) { if (vehicleInfo2uds.InOverVoltFault) { faultInfo.b.fINPUT_OVERVOLTAGE = 1; } else { faultInfo.b.fINPUT_OVERVOLTAGE = 0; } if (vehicleInfo2uds.InUnderVoltFault) { faultInfo.b.fINPUT_UNDERVOLTAGE = 1; } else { faultInfo.b.fINPUT_UNDERVOLTAGE = 0; } if (vehicleInfo2uds.OutOverVoltFault) { faultInfo.b.fOUTPUT_OVERVOLTAGE = 1; } else { faultInfo.b.fOUTPUT_OVERVOLTAGE = 0; } if (vehicleInfo2uds.OutUnderVoltFault) { faultInfo.b.fOUTPUT_UNDERVOLTAGE = 1; } else { faultInfo.b.fOUTPUT_UNDERVOLTAGE = 0; } if (vehicleInfo2uds.InOverCurFault) { faultInfo.b.fINPUT_OVERCURRENT = 1; } else { faultInfo.b.fINPUT_OVERCURRENT = 0; } if (vehicleInfo2uds.OutOverCurFault) { faultInfo.b.fOUTPUT_OVERCURRENT = 1; } else { faultInfo.b.fOUTPUT_OVERCURRENT = 0; } if (vehicleInfo2uds.OverTemperture) { faultInfo.b.fOVERTEMPERTURE = 1; } else { faultInfo.b.fOVERTEMPERTURE = 0; } if (CheckIsCanBusoff()) { if (vehicleInfo2uds.BusoffCnt >= 8) { faultInfo.b.fECU_ENTER_BUSOFF = 1; } } else { faultInfo.b.fECU_ENTER_BUSOFF = 0; } if (CheckIsCanBusoff())return; } } void GetBatteryStatus(void) { if (CheckIsBatteryHigh()) { faultInfo.b.fECU_VOLTAGE_HIGH = 1; } else { faultInfo.b.fECU_VOLTAGE_HIGH = 0; } if (CheckIsBatteryLow()) { faultInfo.b.fECU_VOLTAGE_LOW = 1; } else { faultInfo.b.fECU_VOLTAGE_LOW = 0; } } void DtcAging_Process(void) { uint8_t index; DTC_RecordDataBufType *dtc_record_data; static uint8_t ignStsBak = 1; if (ignStsBak != vehicleInfo2uds.IgnSts) { ignStsBak = vehicleInfo2uds.IgnSts; if (vehicleInfo2uds.IgnSts) { for (index = 0; index < DTC_ID_NUM; index++) { dtc_record_data = &dtcComm.DTCStatus.recordData[index]; if (dtc_record_data->status == (1 << DTC_STATUS_Confirmed_DTC)) { dtc_record_data->extended.FaultAgingCounter++; if (dtc_record_data->extended.FaultAgingCounter >= 40) { dtc_record_data->extended.FaultAgingCounter = 0; dtc_record_data->status = 0; if (dtc_record_data->extended.FaultCounterOfAge != 0xff) { dtc_record_data->extended.FaultCounterOfAge++; } } } } } } } void ClearHardwareFault(void) { UdsApi_HardwareFault_ForceClear(); } void GetFaultInfo(void) { uint8_t shift, status; uint8_t size; uint16_t crc16; DTC_Fault_Flags littleFault; DTC_RecordDataBufType *pDtcRecordData; static uint32_t timer_500ms = 0; GetBatteryStatus(); if (diagStart_InitTimer > 0) { //电压正常后才计数 diagStart_InitTimer --; return; } DiagFaultsMonitor(); if (UdsService.DisDTCRecord) { return; } DtcAging_Process(); if (DTC_ID_NUM <= 32) { //littleFault.w= BigLittleSwap32(faultInfo.w); littleFault.w = faultInfo.w; for (shift = 0; shift < DTC_ID_NUM; shift++) { if ((littleFault.w >> shift) & 0x00000001) { status = (1 << DTC_STATUS_Test_Failed) | (1 << DTC_STATUS_Confirmed_DTC); //当前故障 } else { status = dtcComm.DTCStatus.recordData[shift].status & (~(1 << DTC_STATUS_Test_Failed)); } DiagUpdateDtcValue(shift, status); } } else { //超出32个的,需要特殊处理 } if (++timer_500ms > 50) { timer_500ms = 0; size = sizeof(DTC_RecordDataBufType); for (shift = 0; shift < DTC_ID_NUM; shift++) { pDtcRecordData = &dtcComm.DTCStatus.recordData[shift]; if (pDtcRecordData->status != dtcStatusBak.recordData[shift].status \ /*|| dtcComm.DTCStatus.recordData[shift].extended.FaultCounterOfAge != dtcStatusBak.recordData[shift].extended.FaultCounterOfAge*/) { crc16 = CRC_Cal16_WithCfg((uint8_t *)pDtcRecordData, size - 2); pDtcRecordData->crc16[0] = (uint8_t)crc16; pDtcRecordData->crc16[1] = (uint8_t)(crc16 >> 8); dtcStatusBak.recordData[shift].status = pDtcRecordData->status; EE_Write(NVM_IMAGE_DTC_ADDR + (size * shift), (uint8_t *)pDtcRecordData, size, NULL); } } } } void ClearDtc(void) { uint8_t shift; uint16_t crc16; DTC_RecordDataBufType *pDtcRecordData; faultInfo.w = 0; for (shift = 0; shift < DTC_ID_NUM; shift++) { pDtcRecordData = &dtcComm.DTCStatus.recordData[shift]; memset((uint8_t *)pDtcRecordData, 0x00, sizeof(DTC_RecordDataBufType) - 2); crc16 = CRC_Cal16_WithCfg((uint8_t *)pDtcRecordData, sizeof(DTC_RecordDataBufType) - 2); pDtcRecordData->crc16[0] = (uint8_t)crc16; pDtcRecordData->crc16[1] = (uint8_t)(crc16 >> 8); } memcpy((uint8_t *)&dtcStatusBak, (uint8_t *)&dtcComm.DTCStatus, sizeof(DTC_StatusType)); EE_Write(NVM_IMAGE_DTC_ADDR, (uint8_t *)&dtcComm.DTCStatus, sizeof(DTC_StatusType), NULL); ClearHardwareFault(); } bool GetDtcStatusMask(uint8_t *pOut, uint8_t *len, uint8_t subFunc, uint8_t mask) { bool retValue = FALSE; uint8_t ii = 0; uint8_t j = 0; dtcComm.DTCcount.map = 0; pOut[j++] = DIAG_SERVE_READ_DTC | 0x40; pOut[j++] = subFunc; pOut[j++] = DTC_STATUS_Support_Bit_Status; //注意 因为没有ECU高低压DTC,故从index2开始读取 for (ii = 2; ii < DTC_ID_NUM; ii++) { /*if (((dtcComm.DTCStatus[ii]&DTC_STATUS_Support_Bit_Status) == (mask&DTC_STATUS_Support_Bit_Status))\ ||(subFunc == DTC_ReportSupportedDTCs))*/ if ((dtcComm.DTCStatus.recordData[ii].status & mask) \ || (dtcComm.DTCStatus.recordData[ii].status == mask)\ || (subFunc == DTC_ReportSupportedDTCs)) { if ((dtcOptions.w >> ii) & 0x00000001) { retValue = TRUE; dtcComm.DTCcount.map++; pOut[j++] = (uint8_t)(DTC_LIST_TABLE[ii] >> 16); pOut[j++] = (uint8_t)(DTC_LIST_TABLE[ii] >> 8); pOut[j++] = (uint8_t)(DTC_LIST_TABLE[ii] >> 0); pOut[j++] = dtcComm.DTCStatus.recordData[ii].status; } } } *len = j; return retValue; } #endif void DiagDIDStoreFlash(uint16_t id) { uint8_t didIndex, nvmDidIndex; //uint16_t offset; uint16_t size; uint8_t *cachePtr, *didPtr, *nvmDidPtr = NULL; uint16_t crc16; bool storage_flag = false; nvmDidIndex = GetDIDStoreInFlashIndexByID(id); if (nvmDidIndex != 0xFF) { didIndex = GetDIDIndexByID(id); if (didIndex != 0xFF) { size = DIDSize[didIndex]; cachePtr = (uint8_t *)&gDIDCache; didPtr = cachePtr + DIDOffset[didIndex]; crc16 = CRC_Cal16_WithCfg(didPtr, size); didPtr[size] = (uint8_t)crc16; size++; didPtr[size] = (uint8_t)(crc16 >> 8); size++; nvmDidPtr = (uint8_t *)NVM_IMAGE_DID_ADDR + DIDStoreInFlashOffset[nvmDidIndex]; if (nvmDidPtr != NULL) { EE_Write(nvmDidPtr, didPtr, size, NULL); } } } } void IgnChangeProcess(void) { static uint8_t ignStsBak = 1; if (ignStsBak != vehicleInfo2uds.IgnSts) { ignStsBak = vehicleInfo2uds.IgnSts; if (vehicleInfo2uds.IgnSts) { UdsService.KeyErrLockTimer = 10; UdsService.KeyLockFlg = true; UdsService.KeyErrCnt = 0; UdsService.SeedKeyIsOK = 0; } } } void CheckDtcHsCanError(void) { #if BOOTLOADER_CODE_FLAG == 0 static uint16_t Timer10msCnt = 0; #endif #if BOOTLOADER_CODE_FLAG == 0 if (++Timer10msCnt >= 10) { Timer10msCnt = 0; GetFaultInfo(); IgnChangeProcess(); } gDIDCache.DID_ActiveDiagnosticSession[0] = UdsService.SessionDiagModel; #endif if ((UdsService.UdsTxFlg == true) && (UdsService.UdsTxCompFlg == true)) { UdsService.UdsTxFlg = false; UdsService.UdsTxCompFlg = false; HAL_UDS_Tx_Callback(); //UdsQueueCanOut(); UdsService.TxCompFlg = true; //Debug("HAL_UDS_Tx_Callback \r\n"); // } } #if BOOTLOADER_CODE_FLAG == 0 INT8U Get0x19SerDiagData(INT8U *pOut, INT8U Type, INT8U *pdata) { INT8U RstLen = 0; // INT16U i=0; // INT32U dtc_code = 0; // INT8U num = 0; uint8_t dtcReportSubFunction; uint8_t dtcRecordNumber; uint8_t dtcStatusMask; DTC_MaskRecordTpye dtcMaskRecord; dtcReportSubFunction = Type; dtcStatusMask = pdata[1]; switch (dtcReportSubFunction) { case DTC_ReportNumberOfDTCByStatusMask: case DTC_ReportDTCByStatusMask: case DTC_ReportSupportedDTCs: if (GetDtcStatusMask(pOut, &RstLen, dtcReportSubFunction, dtcStatusMask) == FALSE) { RstLen = 0; pOut[RstLen++] = DIAG_SERVE_READ_DTC | 0x40; //0x59; pOut[RstLen++] = dtcReportSubFunction; pOut[RstLen++] = DTC_STATUS_Support_Bit_Status; } if (dtcReportSubFunction == DTC_ReportNumberOfDTCByStatusMask) { RstLen = 0; pOut[RstLen++] = DIAG_SERVE_READ_DTC | 0x40; //0x59; pOut[RstLen++] = dtcReportSubFunction; pOut[RstLen++] = DTC_STATUS_Support_Bit_Status; pOut[RstLen++] = ISO15031_6_DTCformat; pOut[RstLen++] = dtcComm.DTCcount.b.DTCcountHighByte; pOut[RstLen++] = dtcComm.DTCcount.b.DTCcountLowByte; } break; case DTC_ReportDTCSnapshotRecordByDTCNumber: dtcMaskRecord.DTCHightByte = pdata[1]; dtcMaskRecord.DTCMiddleByte = pdata[2]; dtcMaskRecord.DTCLowByte = pdata[3]; dtcRecordNumber = pdata[4]; if (DiagDtcSnapshotRecordFill(dtcReportSubFunction, dtcMaskRecord, dtcRecordNumber, dtcComm.DTCStatus.recordData, pOut, &RstLen) == 0) { } break; case DTC_ReportDTCExtendedDataRecordByDTCNumber: dtcMaskRecord.DTCHightByte = pdata[1]; dtcMaskRecord.DTCMiddleByte = pdata[2]; dtcMaskRecord.DTCLowByte = pdata[3]; dtcRecordNumber = pdata[4]; if (DiagDtcExtendedDataRecordFill(dtcReportSubFunction, dtcMaskRecord, dtcRecordNumber, dtcComm.DTCStatus.recordData, pOut, &RstLen) == 0) { } break; default: break; } return RstLen; } #endif INT16U Get0x22SerDiagData(INT8U *pOut) { INT16U RstLen = 0; INT16U ReqId = 0; // INT16U length = 0; uint8_t i; uint8_t *cachePtr; uint8_t size; ReqId = ((UdsService.ReqCmdBuf[0] << 8) + UdsService.ReqCmdBuf[1]); pOut[RstLen++] = 0x62; pOut[RstLen++] = ((ReqId >> 8) & 0xFF); pOut[RstLen++] = (ReqId & 0xFF); cachePtr = (uint8_t *)&gDIDCache; for (i = 0; i < DID_NUM; i++) { if (ReqId == DIDId[i]) { break; } } if (i == DID_NUM) { RstLen = 2; } else { #if BOOTLOADER_CODE_FLAG if (!CheckIsSupportBootloaderRead_Sid22(ReqId)) { RstLen = 2; } #endif } if (RstLen != 2) { size = DIDSize[i]; memcpy(&pOut[RstLen], (cachePtr + DIDOffset[i]), size); RstLen += size; } return RstLen; } #if BOOTLOADER_CODE_FLAG INT8U Set0x36DiagData(INT8U *pIn, INT16U PLen, INT8U *pOut, INT16U *RLen) { uint8_t /*i=0,*/ucErrCode = 0; uint8_t sn = 0; sn = pIn[0]; UdsUpData.totalRecvLength += (UdsService.RxDlc - 2); if (CheckIsBatteryHigh()) { ucErrCode = 0x92; } else if (CheckIsBatteryLow()) { ucErrCode = 0x93; } else if (UdsService.RxDlc > (FMC_PAGE_SIZE + 2)) { ucErrCode = 0x13; } else if ((UdsUpData.DownloadSeq != DOWNLOAD_SEQUENCE_34) && (UdsUpData.DownloadSeq != DOWNLOAD_SEQUENCE_36)) { ucErrCode = 0x24; } else if (sn != UdsUpData.Sn) { ucErrCode = 0x73; } else if (UdsUpData.totalRecvLength > FBL_AppSize) { ucErrCode = 0x71; } else { UdsUpData.RxProgramFlag = true; UdsUpData.DownloadSeq = DOWNLOAD_SEQUENCE_36; memcpy(UdsUpData.Buf, &TP_Buffer[2], (UdsService.RxDlc - 2)); ucErrCode = 0x78; } return ucErrCode; } #endif INT8U Set0x31DiagData(INT8U *pIn, INT16U PLen, INT8U *pOut, INT16U *RLen) { uint8_t service_buf[50]; uint8_t /*i=0,*/ucErrCode = 0, ucRsp = 0; uint16_t ReqId = 0; uint16_t dlen = 0; uint8_t result = 0; static bool routineStartFlg = false; if (UdsService.RxDlc < 4) { return 0x13; } ucRsp = pIn[0]; ReqId = pIn[1] << 8 | pIn[2]; service_buf[dlen++] = 0x71; service_buf[dlen++] = pIn[0]; service_buf[dlen++] = pIn[1]; service_buf[dlen++] = pIn[2]; #if BOOTLOADER_CODE_FLAG == 0 // static bool routineStartFlg = false; if ((0x01 == ucRsp) || (0x02 == ucRsp) || (0x03 == ucRsp)) { ucErrCode = 0x31; } else { if (true == UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } #else static uint32_t mem_addr, mem_size; static uint16_t checksum; if (0x01 == ucRsp) { if ((true == CheckUdsDlcIsOk(UdsService.RxDlc, 6) && (ReqId == 0xDA04))\ || (true == CheckUdsDlcIsOk(UdsService.RxDlc, 12) && (ReqId == 0xFF00)) \ || (true == CheckUdsDlcIsOk(UdsService.RxDlc, 14) && (ReqId == 0xFF01))) { if (UdsService.SeedKeyIsOK) { switch (ReqId) { case 0xDA04://Check application program validation if (UdsUpData.DownloadSeq == DOWNLOAD_SEQUENCE_37) { UdsUpData.GetCrc = (pIn[3] << 8) + pIn[4]; result = 0x01; service_buf[dlen++] = result; } else { ucErrCode = 0x22; } break; case 0xFF00://擦除内存 UdsUpData.EraseAddress = (pIn[3] << 24) + (pIn[4] << 16) + (pIn[5] << 8) + pIn[6]; UdsUpData.EraseLength = (pIn[7] << 24) + (pIn[8] << 16) + (pIn[9] << 8) + pIn[10]; if (UdsUpData.EraseAddress != APP_FlashBaseAddress) { ucErrCode = 0x72; UdsUpData.RoutineEraseMemoryErr = true; } else { ucErrCode = 0x78; UdsUpData.RxEraseFlg = true; UdsUpData.RoutineEraseMemoryFlg = true; } break; case 0xFF01://检查编程依赖性 mem_addr = (pIn[3] << 24) + (pIn[4] << 16) + (pIn[5] << 8) + pIn[6]; mem_size = (pIn[7] << 24) + (pIn[8] << 16) + (pIn[9] << 8) + pIn[10]; checksum = (pIn[11] << 8) + pIn[12]; result = 0x01; service_buf[dlen++] = result; break; default: break; } } else { ucErrCode = 0x33; } } else if ((ReqId == 0xDA04) || (ReqId == 0xFF00) || (ReqId == 0xFF01)) { ucErrCode = 0x13; } else { ucErrCode = 0x31; } } else if (0x03 == ucRsp) { if ((true == CheckUdsDlcIsOk(UdsService.RxDlc, 4) && (ReqId == 0xDA04))\ || (true == CheckUdsDlcIsOk(UdsService.RxDlc, 4) && (ReqId == 0xFF00)) \ || (true == CheckUdsDlcIsOk(UdsService.RxDlc, 4) && (ReqId == 0xFF01))) { if (UdsService.SeedKeyIsOK) { switch (ReqId) { case 0xDA04: case 0xFF01: result = 0x02; service_buf[dlen++] = result; break; case 0xFF00: if (UdsUpData.EraseMemorySts == ERASE_MEMORY_STS_COMPLETED) { result = 0x02; } else if (UdsUpData.EraseMemorySts == ERASE_MEMORY_STS_IN_PROGRESS) { result = 0x03; } else { result = 0x05; } service_buf[dlen++] = result; break; default: break; } } else { ucErrCode = 0x33; } } else if ((ReqId == 0xDA04) || (ReqId == 0xFF00) || (ReqId == 0xFF01)) { ucErrCode = 0x13; } else { ucErrCode = 0x31; } } else if (0x02 == ucRsp) { if ((true == CheckUdsDlcIsOk(UdsService.RxDlc, 4) && (ReqId == 0xDA04))\ || (true == CheckUdsDlcIsOk(UdsService.RxDlc, 4) && (ReqId == 0xFF00)) \ || (true == CheckUdsDlcIsOk(UdsService.RxDlc, 4) && (ReqId == 0xFF01))) { if (UdsService.SeedKeyIsOK) { switch (ReqId) { case 0xDA04: if(UdsUpData.CalcCrc==UdsUpData.GetCrc) { if ((UdsUpData.StackTopAdr & 0x2FFE0000 ) == MCU_RAM_StartAddress) { if (STATUS_SUCCESS == ProgramApplicationSoftware(APP_FlashBaseAddress, (uint8_t*)&UdsUpData.StackTopAdr, 4)) { result = 0x01; } else { result = 0x02; } } } service_buf[dlen++] = result; break; case 0xFF00: UdsUpData.EraseMemoryFlg = false; result = 0x04; service_buf[dlen++] = result; break; case 0xFF01: if ((UdsUpData.EraseAddress == mem_addr) \ || (UdsUpData.EraseLength == mem_size) \ || (UdsUpData.CalcCrc == checksum)) { result = 0x01; } else { result = 0x02; } service_buf[dlen++] = result; break; default: break; } } else { ucErrCode = 0x33; } } else if ((ReqId == 0xDA04) || (ReqId == 0xFF00) || (ReqId == 0xFF01)) { ucErrCode = 0x13; } else { ucErrCode = 0x31; } } else { if (true == UdsService.PhyFlg) { UdsService.PhyFlg = false; ucErrCode = 0x12; } } #endif *RLen = dlen; if (ucErrCode == 0) { memcpy(pOut, service_buf, dlen); } return ucErrCode; } INT8U Set0x2EDidDiagData(INT8U *pIn, INT16U PLen) { INT8U Rst = 0; INT16U ReqId = 0; ReqId = ((pIn[0] << 8) + pIn[1]); #if BOOTLOADER_CODE_FLAG == 0 if (ReqId == DIDId[VehicleIdentificationNumber]) { if(PLen == DIDSize[VehicleIdentificationNumber]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_VehicleIdentificationNumber,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[InputOvervoltageValue]) { if(PLen == DIDSize[InputOvervoltageValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_InputOvervoltageValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[InputUndervoltageValue]) { if(PLen == DIDSize[InputUndervoltageValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_InputUndervoltageValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[OutputOvervoltageValue]) { if(PLen == DIDSize[OutputOvervoltageValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_OutputOvervoltageValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[OutputUndervoltageValue]) { if(PLen == DIDSize[OutputUndervoltageValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_OutputUndervoltageValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[OutputOvercurrentFaultValue]) { if(PLen == DIDSize[OutputOvercurrentFaultValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_OutputOvercurrentFaultValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[InputOvercurrentFaultValue]) { if(PLen == DIDSize[InputOvercurrentFaultValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_InputOvercurrentFaultValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[OverTempertureProtectValue]) { if(PLen == DIDSize[OverTempertureProtectValue]) { if (UdsService.SeedKeyIsOK) { if (CheckVechileStatus()) { memcpy(gDIDCache.DID_OverTempertureProtectValue,&pIn[2],PLen); } else { Rst = 0x22; } } else { Rst = 0x33; } } else { Rst = 0x13; } } else { Rst = 0x31; } #else if (ReqId == DIDId[TesterSerialNumber]) { if(PLen == DIDSize[TesterSerialNumber]) { if (UdsService.SeedKeyIsOK) { memcpy(gDIDCache.DID_TesterSerialNumber,&pIn[2],PLen); } else { Rst = 0x33; } } else { Rst = 0x13; } } else if (ReqId == DIDId[ProgrammingDate]) { if(PLen == DIDSize[ProgrammingDate]) { if (UdsService.SeedKeyIsOK) { memcpy(gDIDCache.DID_ProgrammingDate,&pIn[2],PLen); } else { Rst = 0x33; } } else { Rst = 0x13; } } else { Rst = 0x31; } #endif if (Rst == 0) { DiagDIDStoreFlash(ReqId); } return Rst; } #if BOOTLOADER_CODE_FLAG == 0 INT8U Set0x2FDiagData(INT8U *pIn, INT16U PLen, INT8U *pOut, INT16U *RLen) { INT8U ucErrCode = 0; return ucErrCode; } #endif uint8_t seed_value[2] = {0}; uint8_t key_value[2] = {0}; void UdsQueueCanOut(void) { //if(Frame_Tx_Complete != false) return; //if(TP_Tx_Message_DLC) return; while ((UdsQueueData.In != UdsQueueData.Out)) { if (UdsQueueData.Out >= CAN0_MAX_RX) { UdsQueueData.Out = 0; } gUdsBusOffCanOutBak = UdsQueueData.Out; ParseUdsCan(UdsQueueData.CanMsg[UdsQueueData.Out]); UdsQueueData.Out++; ParseCanRxCnt++; // break; } //return; } uint8_t GenerateSID27SeedValue(uint8_t *seed) { uint32_t random; random *= UdsApi_GetRandom(); seed[0] = (uint8_t)(random >> 0); seed[1] = (uint8_t)(random >> 16); // seed[2] = (uint8_t)(random >> 8); // seed[3] = (uint8_t)(random >> 0); return 0; } #if BOOTLOADER_CODE_FLAG extern void RAMFLASHInit(void); #endif uint16_t GenerateSID27KeyValue(uint16_t seed) { #define TOPBIT 0x8000 #define POLYNOM_1 0x8408 #define POLYNOM_2 0x8025 #define BITMASK 0x0080 #define INITIAL_REMINDER 0xFFFE #define MSG_LEN 2 /* seed length in bytes */ uint8_t bSeed[2]; uint16_t remainder; uint8_t n; uint8_t i; bSeed[0] = (uint8_t)(seed >> 8); /* MSB */ bSeed[1] = (uint8_t)seed; /* LSB */ remainder = INITIAL_REMINDER; for (n = 0; n < MSG_LEN; n++) { /* Bring the next byte into the remainder. */ remainder ^= ((bSeed[n]) << 8); /* Perform modulo-2 division, a bit at a time. */ for (i = 0; i < 8; i++) { /* Try to divide the current data bit. */ if (remainder & TOPBIT) { if(remainder & BITMASK) { remainder = (remainder << 1) ^ POLYNOM_1; } else { remainder = (remainder << 1) ^ POLYNOM_2; } } else { remainder = (remainder << 1); } } } /* The final remainder is the key */ return remainder; } //extern EReturnMsg bsp_flash_erase_sector(INT32U nFlashAddr); //extern EReturnMsg bsp_flash_write_buff(INT32U nFlashAddr, INT8U *pBuff, INT32U nLen); //extern EReturnMsg bsp_flash_read_buff(INT32U nFlashAddr, INT8U *pBuff, INT32U nLen); uint8_t EE_Write(uint8_t *pImage, const uint8_t *pData, uint32_t Len, void (*pCbFn)(uint32_t)) { uds_service_nvm_write(pImage, pData, Len, pCbFn); return TRUE; } uint8_t EE_Read(uint8_t *pImage, uint8_t **pData) { if (NVM_IMAGE_DTC_ADDR == pImage) { *pData = (uint8_t *)NVM_IMAGE_DTC_ADDR; } else if (NVM_IMAGE_DID_ADDR == pImage) { *pData = (uint8_t *)NVM_IMAGE_DID_ADDR; } else { return FALSE; } return TRUE; } void SetUdsUpdataFlag(void) { #if (BOOTLOADER_CODE_FLAG == 0) SetExternProgrammingRequest(); #endif } #if BOOTLOADER_CODE_FLAG #if APP_DATA_ENCRYPT_ENABLE const unsigned char tea_key_table[]= { 0xF0,0xD6,0x28,0xCC,0xBA,0x39,0x4E,0x30, 0x8F,0x77,0x35,0xF6,0xCA,0x3E,0x99,0xBA, }; void UdsEncryptDate_Crc32Cal_Init(void) { CRC32_HwCal_04C11DB7_SegInit(); } void UdsEncryptDate_Crc32Cal_Update(const u8 *pData, u32 size) { CRC32_HwCal_04C11DB7_SegUpdata(pData, size); } uint32_t UdsEncryptDate_Crc32Cal_Final(void) { return CRC32_HwCal_04C11DB7_SegFinal(); } uint16_t UdsEncryptDate_Decrypt(uint8_t *src,uint16_t size_src) { return decrypt(src, size_src, (uint8_t*)tea_key_table); } void UdsEncryptDate_Xor(uint8_t *src,uint16_t size_src) { Xor_Opeartion(src, 0x20, size_src); } #endif #endif bool CheckVechileStatus(void) { bool cbRst = false; uint16_t speed = WORD(vehicleInfo2uds.Snapshot.VehSpeedH, vehicleInfo2uds.Snapshot.VehSpeedL); if (speed <= 80) { cbRst = true; } return cbRst; } bool CheckIsBatteryLow(void) { bool bRst = false; if (vehicleInfo2uds.BattLow) { bRst = true; } return bRst; } bool CheckIsBatteryHigh(void) { bool bRst = false; if (vehicleInfo2uds.BattHigh) { bRst = true; } return bRst; } bool CheckIsIgnON(void) { bool bRst = false; if (vehicleInfo2uds.IgnSts) { bRst = true; } return bRst; } //bool CheckIsIgnOff2ON(void) //{ // bool bRst=false; // // if (vehicleInfo2uds.IgnOff2On) { // bRst = true; // } // return bRst; //} #if BOOTLOADER_CODE_FLAG == 0 bool CheckIsCanBusoff(void) { bool bRst = false; if (vehicleInfo2uds.BusoffCnt) { bRst = true; } return bRst; } #endif