// // Created by cfif on 20.01.2026. // #include "CanUds.h" #include "CanPorts.h" #include "StatusData.h" #include "memory.h" #include "TesterPresent_3e.h" #include "DiagnosticSessionControl_10.h" #define LOG_SIGN "CAN_UDS" #define LOGGER env->logger bool ReceivedCan_func(void *arg, can_rx_message_type *canFrame) { tCanUds *env = arg; return false; } void ReceivedTP_func(void *arg, tCanTP_data *data) { tCanUds *env = arg; if (data->data[0] == UDS_TesterPresent) { tTesterPresent testerPresent; testerPresent.ServiceId = data->data[0] | 0b1000000; testerPresent.zeroSubFunction = data->data[1] & 0b1111111; CanSerialPortFrameTpTransmit(&env->canSerialPortFrameTp, (uint8_t *) &testerPresent, sizeof(tTesterPresent), PROTOCOL_CAN_ADR_UDS, WAIT_FRAME_WRITE); return; } osStatus_t status = osMessageQueuePut(env->queue, data, 0, 0U); if (status != osOK) { LoggerInfoStatic(LOGGER, LOG_SIGN, "Ошибка добавления в очередь addCommandQueue") } } uint8_t setResponseError(tCanUds *env, eUdsServices service, eUdsResponseError error) { env->dataResponse[0] = 0x7F; env->dataResponse[1] = service; env->dataResponse[2] = error; return 3; } // начало ----------------------------- Сессия ------------------------------------------------------------- // начало ----------------------------- Сессия ------------------------------------------------------------- // начало ----------------------------- Сессия ------------------------------------------------------------- static uint8_t DiagnosticSessionControl_10(tCanUds *env) { if ((env->data.len != 2) || (env->data.data[1] != UDS_session_defaultSession) || (env->data.data[1] != UDS_session_programmingSession) || (env->data.data[1] != UDS_session_extendedDiagnosticSession)) { return setResponseError(env, UDS_DiagnosticSessionControl, UDS_error_incorrectMessageLengthOrInvalidFormat); } tDiagnosticSessionsType *diagnosticSessionsType = (tDiagnosticSessionsType *) env->dataResponse; diagnosticSessionsType->ServiceId = UDS_DiagnosticSessionControl | 0b1000000; diagnosticSessionsType->diagnosticSessionType = env->data.data[1]; diagnosticSessionsType->sessionParameterRecord[0] = 0; diagnosticSessionsType->sessionParameterRecord[1] = 0x32; diagnosticSessionsType->sessionParameterRecord[2] = 0x01; diagnosticSessionsType->sessionParameterRecord[3] = 0xF4; return sizeof(tDiagnosticSessionsType); } // конец ----------------------------- Сессия -------------------------------------------------------------- // конец ----------------------------- Сессия -------------------------------------------------------------- // конец ----------------------------- Сессия -------------------------------------------------------------- // начало --------------------------- Чтение --------------------------------------------------------- // начало --------------------------- Чтение --------------------------------------------------------- // начало --------------------------- Чтение --------------------------------------------------------- static uint8_t ReadDataByIdentifier_22(tCanUds *env) { if (env->data.len != 3) { return setResponseError(env, UDS_ReadDataByIdentifier, UDS_error_incorrectMessageLengthOrInvalidFormat); } uint8_t dataIdentifier_hi = env->data.data[1]; uint8_t dataIdentifier_lo = env->data.data[2]; // uint16_t dataIdentifier = (dataIdentifier_hi << 8) | dataIdentifier_lo; if (dataIdentifier_hi == 0xCF) { if (uds_ReadDataByIdentifier_22_com_CF[dataIdentifier_lo].data != NULL) { uint8_t response_size = uds_ReadDataByIdentifier_22_com_CF[dataIdentifier_lo].size; env->dataResponse[0] = UDS_ReadDataByIdentifier | 0b1000000; env->dataResponse[1] = dataIdentifier_hi; env->dataResponse[2] = dataIdentifier_lo; memcpy(&env->dataResponse[3], uds_ReadDataByIdentifier_22_com_CF[dataIdentifier_lo].data, response_size); return response_size; } } if (dataIdentifier_hi == 0xF1) { if (uds_ReadDataByIdentifier_22_com_F1[dataIdentifier_lo].data != NULL) { uint8_t response_size = uds_ReadDataByIdentifier_22_com_F1[dataIdentifier_lo].size; env->dataResponse[0] = UDS_ReadDataByIdentifier | 0b1000000; env->dataResponse[1] = dataIdentifier_hi; env->dataResponse[2] = dataIdentifier_lo; memcpy(&env->dataResponse[3], uds_ReadDataByIdentifier_22_com_F1[dataIdentifier_lo].data, response_size); return response_size; } } return setResponseError(env, UDS_ReadDataByIdentifier, UDS_error_requestOutOfRange); } // конец --------------------------- Чтение --------------------------------------------------------- // конец --------------------------- Чтение --------------------------------------------------------- // конец --------------------------- Чтение --------------------------------------------------------- // начало --------------------------- Запись --------------------------------------------------------- // начало --------------------------- Запись --------------------------------------------------------- // начало --------------------------- Запись --------------------------------------------------------- static uint8_t WriteDataByIdentifier_2E(tCanUds *env) { } // конец --------------------------- Запись --------------------------------------------------------- // конец --------------------------- Запись --------------------------------------------------------- // конец --------------------------- Запись --------------------------------------------------------- const eUds_com uds_com[256] = { {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {DiagnosticSessionControl_10, "DiagnosticSessionControl_10"}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {ReadDataByIdentifier_22, "ReadDataByIdentifier_22"}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {WriteDataByIdentifier_2E, "WriteDataByIdentifier_2E"}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""}, {NULL, ""} }; void CanUds(tCanUds *env) { for (;;) { osStatus_t status = osMessageQueueGet(env->queue, &env->data, 0, 1000); if (status == osOK) { uint8_t com = env->data.data[0]; if (uds_com[com].func != NULL) { uint8_t response_size = uds_com[com].func(env); if (response_size) { CanSerialPortFrameTpTransmit(&env->canSerialPortFrameTp, env->dataResponse, response_size, PROTOCOL_CAN_ADR_UDS, WAIT_FRAME_WRITE); } } else { asm("nop"); } } } } void CanSerialPortCanUds_Start(tCanUds *env) { ThreadBlock_Start(env->T_can_Uds, env, CanUds); CanSerialPortFrameTp_Start(&env->canSerialPortFrameTp); } void CanUds_Init( tCanUds *env, tSerialPortFrameIO *CanIO, tDeviceStorage *deviceStorage, tLoggerInterface *logger) { env->logger = logger; env->CanIO = CanIO; env->deviceStorage = deviceStorage; env->filterIdCount = 0; env->queue = osMessageQueueNew(CAN_US_QUEUE_SIZE, sizeof(tCanTP_data), NULL); env->filterReqId[0] = 0; env->filterReqId[1] = 0; env->filterReqId[2] = 0; env->filterReqId[3] = 0; env->filterReqId[4] = 0; env->filterReqId[5] = 0; env->filterReqId[6] = 0; env->filterRespId[0] = 0; env->filterRespId[1] = 0; env->filterRespId[2] = 0; env->filterRespId[3] = 0; env->filterRespId[4] = 0; env->filterRespId[5] = 0; env->filterRespId[6] = 0; env->filterDirReq[0] = 0; env->filterDirReq[1] = 0; env->filterDirReq[2] = 0; env->filterDirReq[3] = 0; env->filterDirReq[4] = 0; env->filterDirReq[5] = 1; env->filterDirReq[6] = 0; CanSerialPortFrameTpInit( &env->canSerialPortFrameTp, ReceivedCan_func, env, ReceivedTP_func, env, env->CanIO, (tCanTP_data *) &env->canTP_Ext_data, sizeof(env->canTP_Ext_data.data), env->logger, env->filterIdCount, env->filterReqId, env->filterRespId, env->filterDirReq ); InitThreadBlock(env->T_can_Uds, "CanUds", osPriorityNormal); };