/* * canbus_uds.h * UDS (ISO 14229) diagnostic services over ISO-TP. */ #pragma once #include #include #ifdef __cplusplus extern "C" { #endif /* ============================================================ * UDS Service IDs * ============================================================ */ #define UDS_DIAG_SESSION_CTRL 0x10 #define UDS_ECU_RESET 0x11 #define UDS_CLEAR_DTC 0x14 #define UDS_READ_DTC_INFO 0x19 #define UDS_READ_DATA_BY_ID 0x22 #define UDS_READ_MEM_BY_ADDR 0x23 #define UDS_SECURITY_ACCESS 0x27 #define UDS_COMM_CTRL 0x28 #define UDS_WRITE_DATA_BY_ID 0x2E #define UDS_IO_CTRL 0x2F #define UDS_ROUTINE_CTRL 0x31 #define UDS_REQUEST_DOWNLOAD 0x34 #define UDS_REQUEST_UPLOAD 0x35 #define UDS_TRANSFER_DATA 0x36 #define UDS_TRANSFER_EXIT 0x37 #define UDS_TESTER_PRESENT 0x3E /* Session types */ #define UDS_SESSION_DEFAULT 0x01 #define UDS_SESSION_PROGRAMMING 0x02 #define UDS_SESSION_EXTENDED 0x03 /* Negative Response Codes */ #define UDS_NRC_GENERAL_REJECT 0x10 #define UDS_NRC_SERVICE_NOT_SUPPORTED 0x11 #define UDS_NRC_SUBFUNCTION_NOT_SUPPORTED 0x12 #define UDS_NRC_INCORRECT_LENGTH 0x13 #define UDS_NRC_RESPONSE_PENDING 0x78 #define UDS_NRC_SECURITY_ACCESS_DENIED 0x33 #define UDS_NRC_INVALID_KEY 0x35 #define UDS_NRC_EXCEEDED_ATTEMPTS 0x36 #define UDS_NRC_CONDITIONS_NOT_MET 0x22 /* ============================================================ * UDS Context * ============================================================ */ typedef struct { uint32_t tx_id; /* Request CAN ID (e.g. 0x7E0) */ uint32_t rx_id; /* Response CAN ID (e.g. 0x7E8) */ int timeout_ms; /* Response timeout */ uint8_t session; /* Current session type */ bool security_unlocked; } uds_ctx_t; /* ============================================================ * High-Level UDS API * ============================================================ */ /* DiagnosticSessionControl (0x10) */ int uds_diagnostic_session(uds_ctx_t *ctx, uint8_t session_type); /* TesterPresent (0x3E) — keep-alive */ int uds_tester_present(uds_ctx_t *ctx); /* ReadDataByIdentifier (0x22) — returns data length or -1 */ int uds_read_data_by_id(uds_ctx_t *ctx, uint16_t did, uint8_t *out, size_t cap); /* SecurityAccess (0x27) — request seed */ int uds_security_access_seed(uds_ctx_t *ctx, uint8_t level, uint8_t *seed, size_t *seed_len); /* SecurityAccess (0x27) — send key */ int uds_security_access_key(uds_ctx_t *ctx, uint8_t level, const uint8_t *key, size_t key_len); /* ReadMemoryByAddress (0x23) — returns data length or -1 */ int uds_read_memory(uds_ctx_t *ctx, uint32_t addr, uint16_t size, uint8_t *out); /* Raw UDS request — returns response length or -1 */ int uds_raw_request(uds_ctx_t *ctx, const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_cap, size_t *resp_len); /* ECU discovery: send TesterPresent to 0x7E0-0x7EF, report responders */ int uds_scan_ecus(uint32_t *found_ids, int max_ecus); /* Get human-readable NRC name */ const char *uds_nrc_name(uint8_t nrc); #ifdef __cplusplus } #endif