16#define UDS_LIB_VERSION "0.9.0"
20#define UDS_SYS_CUSTOM 0
22#define UDS_SYS_WINDOWS 2
23#define UDS_SYS_ARDUINO 3
24#define UDS_SYS_ESP32 4
28#if defined(__unix__) || defined(__APPLE__)
29#define UDS_SYS UDS_SYS_UNIX
31#define UDS_SYS UDS_SYS_WINDOWS
33#define UDS_SYS UDS_SYS_ARDUINO
34#elif defined(ESP_PLATFORM)
35#define UDS_SYS UDS_SYS_ESP32
37#define UDS_SYS UDS_SYS_CUSTOM
45#if UDS_SYS == UDS_SYS_ARDUINO
63#if UDS_SYS == UDS_SYS_UNIX
80#if UDS_SYS == UDS_SYS_WINDOWS
90typedef SSIZE_T ssize_t;
93#define strncasecmp _strnicmp
94#define strcasecmp _stricmp
101#if UDS_SYS == UDS_SYS_ESP32
105#include <esp_timer.h>
107#define UDS_TP_ISOTP_C 1
114#define UDS_ISOTP_MTU (4095)
117#define UDS_TP_MTU UDS_ISOTP_MTU
120#ifndef UDS_SERVER_SEND_BUF_SIZE
121#define UDS_SERVER_SEND_BUF_SIZE (UDS_TP_MTU)
124#ifndef UDS_SERVER_RECV_BUF_SIZE
125#define UDS_SERVER_RECV_BUF_SIZE (UDS_TP_MTU)
128#ifndef UDS_CLIENT_SEND_BUF_SIZE
129#define UDS_CLIENT_SEND_BUF_SIZE (UDS_TP_MTU)
132#ifndef UDS_CLIENT_RECV_BUF_SIZE
133#define UDS_CLIENT_RECV_BUF_SIZE (UDS_TP_MTU)
136#ifndef UDS_CLIENT_DEFAULT_P2_MS
137#define UDS_CLIENT_DEFAULT_P2_MS (150U)
140#ifndef UDS_CLIENT_DEFAULT_P2_STAR_MS
141#define UDS_CLIENT_DEFAULT_P2_STAR_MS (1500U)
145#ifndef UDS_CLIENT_DEFAULT_S3_MS
146#define UDS_CLIENT_DEFAULT_S3_MS (2000)
149static_assert(UDS_CLIENT_DEFAULT_P2_STAR_MS > UDS_CLIENT_DEFAULT_P2_MS,
"");
152#ifndef UDS_SERVER_DEFAULT_P2_MS
153#define UDS_SERVER_DEFAULT_P2_MS (50)
157#ifndef UDS_SERVER_DEFAULT_P2_STAR_MS
158#define UDS_SERVER_DEFAULT_P2_STAR_MS (5000)
162#ifndef UDS_SERVER_DEFAULT_S3_MS
163#define UDS_SERVER_DEFAULT_S3_MS (5100)
166static_assert((0 < UDS_SERVER_DEFAULT_P2_MS) &&
167 (UDS_SERVER_DEFAULT_P2_MS < UDS_SERVER_DEFAULT_P2_STAR_MS) &&
168 (UDS_SERVER_DEFAULT_P2_STAR_MS < UDS_SERVER_DEFAULT_S3_MS),
174#ifndef UDS_SERVER_DEFAULT_POWER_DOWN_TIME_MS
175#define UDS_SERVER_DEFAULT_POWER_DOWN_TIME_MS (60)
178#if (UDS_SERVER_DEFAULT_POWER_DOWN_TIME_MS < UDS_SERVER_DEFAULT_P2_MS)
179#error "The server shall have adequate time to respond before reset"
183#ifndef UDS_SERVER_0x27_BRUTE_FORCE_MITIGATION_BOOT_DELAY_MS
184#define UDS_SERVER_0x27_BRUTE_FORCE_MITIGATION_BOOT_DELAY_MS (1000)
188#ifndef UDS_SERVER_0x27_BRUTE_FORCE_MITIGATION_AUTH_FAIL_DELAY_MS
189#define UDS_SERVER_0x27_BRUTE_FORCE_MITIGATION_AUTH_FAIL_DELAY_MS (1000)
192#ifndef UDS_SERVER_DEFAULT_XFER_DATA_MAX_BLOCKLENGTH
196#define UDS_SERVER_DEFAULT_XFER_DATA_MAX_BLOCKLENGTH (UDS_TP_MTU)
199#ifndef UDS_CUSTOM_MILLIS
200#define UDS_CUSTOM_MILLIS 0
206#if defined UDS_TP_ISOTP_C_SOCKETCAN
207#ifndef UDS_TP_ISOTP_C
208#define UDS_TP_ISOTP_C
212enum UDSTpStatusFlags {
213 UDS_TP_IDLE = 0x0000,
214 UDS_TP_SEND_IN_PROGRESS = 0x0001,
215 UDS_TP_RECV_COMPLETE = 0x0002,
219typedef uint32_t UDSTpStatus_t;
222 UDS_A_MTYPE_DIAG = 0,
223 UDS_A_MTYPE_REMOTE_DIAG,
224 UDS_A_MTYPE_SECURE_DIAG,
225 UDS_A_MTYPE_SECURE_REMOTE_DIAG,
229 UDS_A_TA_TYPE_PHYSICAL = 0,
230 UDS_A_TA_TYPE_FUNCTIONAL,
233typedef uint8_t UDSTpAddr_t;
248#define UDS_TP_NOOP_ADDR (0xFFFFFFFF)
254typedef struct UDSTp {
263 ssize_t (*send)(
struct UDSTp *hdl, uint8_t *buf,
size_t len,
UDSSDU_t *info);
273 ssize_t (*recv)(
struct UDSTp *hdl, uint8_t *buf,
size_t bufsize,
UDSSDU_t *info);
282 UDSTpStatus_t (*poll)(
struct UDSTp *hdl);
285ssize_t UDSTpSend(
UDSTp_t *hdl,
const uint8_t *buf, ssize_t len,
UDSSDU_t *info);
286ssize_t UDSTpRecv(
UDSTp_t *hdl, uint8_t *buf,
size_t bufsize,
UDSSDU_t *info);
287UDSTpStatus_t UDSTpPoll(
UDSTp_t *hdl);
298typedef enum UDSEvent {
340 UDS_PositiveResponse = 0,
342 UDS_NRC_GeneralReject = 0x10,
343 UDS_NRC_ServiceNotSupported = 0x11,
344 UDS_NRC_SubFunctionNotSupported = 0x12,
345 UDS_NRC_IncorrectMessageLengthOrInvalidFormat = 0x13,
346 UDS_NRC_ResponseTooLong = 0x14,
348 UDS_NRC_BusyRepeatRequest = 0x21,
349 UDS_NRC_ConditionsNotCorrect = 0x22,
350 UDS_NRC_RequestSequenceError = 0x24,
351 UDS_NRC_NoResponseFromSubnetComponent = 0x25,
352 UDS_NRC_FailurePreventsExecutionOfRequestedAction = 0x26,
354 UDS_NRC_RequestOutOfRange = 0x31,
356 UDS_NRC_SecurityAccessDenied = 0x33,
357 UDS_NRC_AuthenticationRequired = 0x34,
358 UDS_NRC_InvalidKey = 0x35,
359 UDS_NRC_ExceedNumberOfAttempts = 0x36,
360 UDS_NRC_RequiredTimeDelayNotExpired = 0x37,
361 UDS_NRC_SecureDataTransmissionRequired = 0x38,
362 UDS_NRC_SecureDataTransmissionNotAllowed = 0x39,
363 UDS_NRC_SecureDataVerificationFailed = 0x3A,
365 UDS_NRC_CertficateVerificationFailedInvalidTimePeriod = 0x50,
366 UDS_NRC_CertficateVerificationFailedInvalidSignature = 0x51,
367 UDS_NRC_CertficateVerificationFailedInvalidChainOfTrust = 0x52,
368 UDS_NRC_CertficateVerificationFailedInvalidType = 0x53,
369 UDS_NRC_CertficateVerificationFailedInvalidFormat = 0x54,
370 UDS_NRC_CertficateVerificationFailedInvalidContent = 0x55,
371 UDS_NRC_CertficateVerificationFailedInvalidScope = 0x56,
372 UDS_NRC_CertficateVerificationFailedInvalidCertificate = 0x57,
373 UDS_NRC_OwnershipVerificationFailed = 0x58,
374 UDS_NRC_ChallengeCalculationFailed = 0x59,
375 UDS_NRC_SettingAccessRightsFailed = 0x5A,
376 UDS_NRC_SessionKeyCreationOrDerivationFailed = 0x5B,
377 UDS_NRC_ConfigurationDataUsageFailed = 0x5C,
378 UDS_NRC_DeAuthenticationFailed = 0x5D,
380 UDS_NRC_UploadDownloadNotAccepted = 0x70,
381 UDS_NRC_TransferDataSuspended = 0x71,
382 UDS_NRC_GeneralProgrammingFailure = 0x72,
383 UDS_NRC_WrongBlockSequenceCounter = 0x73,
385 UDS_NRC_RequestCorrectlyReceived_ResponsePending = 0x78,
387 UDS_NRC_SubFunctionNotSupportedInActiveSession = 0x7E,
388 UDS_NRC_ServiceNotSupportedInActiveSession = 0x7F,
390 UDS_NRC_RpmTooHigh = 0x81,
391 UDS_NRC_RpmTooLow = 0x82,
392 UDS_NRC_EngineIsRunning = 0x83,
393 UDS_NRC_EngineIsNotRunning = 0x84,
394 UDS_NRC_EngineRunTimeTooLow = 0x85,
395 UDS_NRC_TemperatureTooHigh = 0x86,
396 UDS_NRC_TemperatureTooLow = 0x87,
397 UDS_NRC_VehicleSpeedTooHigh = 0x88,
398 UDS_NRC_VehicleSpeedTooLow = 0x89,
399 UDS_NRC_ThrottlePedalTooHigh = 0x8A,
400 UDS_NRC_ThrottlePedalTooLow = 0x8B,
401 UDS_NRC_TransmissionRangeNotInNeutral = 0x8C,
402 UDS_NRC_TransmissionRangeNotInGear = 0x8D,
404 UDS_NRC_BrakeSwitchNotClosed = 0x8F,
405 UDS_NRC_ShifterLeverNotInPark = 0x90,
406 UDS_NRC_TorqueConverterClutchLocked = 0x91,
407 UDS_NRC_VoltageTooHigh = 0x92,
408 UDS_NRC_VoltageTooLow = 0x93,
409 UDS_NRC_ResourceTemporarilyNotAvailable = 0x94,
416 UDS_ERR_TIMEOUT = 0x100,
417 UDS_ERR_DID_MISMATCH,
418 UDS_ERR_SID_MISMATCH,
419 UDS_ERR_SUBFUNCTION_MISMATCH,
421 UDS_ERR_RESP_TOO_SHORT,
430#define UDS_LEV_DS_DS 1
431#define UDS_LEV_DS_PRGS 2
432#define UDS_LEV_DS_EXTDS 3
433#define UDS_LEV_DS_SSDS 4
440#define UDS_LEV_RT_HR 1
441#define UDS_LEV_RT_KOFFONR 2
442#define UDS_LEV_RT_SR 3
443#define UDS_LEV_RT_ERPSD 4
444#define UDS_LEV_RT_DRPSD 5
451#define UDS_LEV_CTRLTP_ERXTX 0
452#define UDS_LEV_CTRLTP_ERXDTX 1
453#define UDS_LEV_CTRLTP_DRXETX 2
454#define UDS_LEV_CTRLTP_DRXTX 3
461#define UDS_CTP_NWMCM 2
462#define UDS_CTP_NWMCM_NCM 3
468#define UDS_LEV_RCTP_STR 1
469#define UDS_LEV_RCTP_STPR 2
470#define UDS_LEV_RCTP_RRR 3
476#define UDS_MOOP_ADDFILE 1
477#define UDS_MOOP_DELFILE 2
478#define UDS_MOOP_REPLFILE 3
479#define UDS_MOOP_RDFILE 4
480#define UDS_MOOP_RDDIR 5
481#define UDS_MOOP_RSFILE 6
487#define UDS_LEV_DTCSTP_ON 1
488#define UDS_LEV_DTCSTP_OFF 2
494#define UDS_LEV_LCTP_VMTWFP 1
495#define UDS_LEV_LCTP_VMTWSP 2
496#define UDS_LEV_LCTP_TM 3
499#define UDS_MAX_DIAGNOSTIC_SERVICES 0x7F
501#define UDS_RESPONSE_SID_OF(request_sid) ((request_sid) + 0x40)
502#define UDS_REQUEST_SID_OF(response_sid) ((response_sid) - 0x40)
504#define UDS_NEG_RESP_LEN 3U
505#define UDS_0X10_REQ_LEN 2U
506#define UDS_0X10_RESP_LEN 6U
507#define UDS_0X11_REQ_MIN_LEN 2U
508#define UDS_0X11_RESP_BASE_LEN 2U
509#define UDS_0X14_REQ_MIN_LEN 4U
510#define UDS_0X14_RESP_BASE_LEN 1U
511#define UDS_0X19_REQ_MIN_LEN 2U
512#define UDS_0X19_RESP_BASE_LEN 2U
513#define UDS_0X23_REQ_MIN_LEN 4U
514#define UDS_0X23_RESP_BASE_LEN 1U
515#define UDS_0X22_RESP_BASE_LEN 1U
516#define UDS_0X27_REQ_BASE_LEN 2U
517#define UDS_0X27_RESP_BASE_LEN 2U
518#define UDS_0X28_REQ_BASE_LEN 3U
519#define UDS_0X28_RESP_LEN 2U
520#define UDS_0X2C_REQ_MIN_LEN 2U
521#define UDS_0X2C_RESP_BASE_LEN 2U
522#define UDS_0X2E_REQ_BASE_LEN 3U
523#define UDS_0X2E_REQ_MIN_LEN 4U
524#define UDS_0X2E_RESP_LEN 3U
525#define UDS_0X2F_REQ_MIN_LEN 4U
526#define UDS_0X2F_RESP_BASE_LEN 4U
527#define UDS_0X31_REQ_MIN_LEN 4U
528#define UDS_0X31_RESP_MIN_LEN 4U
529#define UDS_0X34_REQ_BASE_LEN 3U
530#define UDS_0X34_RESP_BASE_LEN 2U
531#define UDS_0X35_REQ_BASE_LEN 3U
532#define UDS_0X35_RESP_BASE_LEN 2U
533#define UDS_0X36_REQ_BASE_LEN 2U
534#define UDS_0X36_RESP_BASE_LEN 2U
535#define UDS_0X37_REQ_BASE_LEN 1U
536#define UDS_0X37_RESP_BASE_LEN 1U
537#define UDS_0X38_REQ_BASE_LEN 9U
538#define UDS_0X38_RESP_BASE_LEN 3U
539#define UDS_0X3D_REQ_MIN_LEN 5U
540#define UDS_0X3D_RESP_BASE_LEN 2U
541#define UDS_0X3E_REQ_MIN_LEN 2U
542#define UDS_0X3E_REQ_MAX_LEN 2U
543#define UDS_0X3E_RESP_LEN 2U
544#define UDS_0X85_REQ_BASE_LEN 2U
545#define UDS_0X85_RESP_LEN 2U
546#define UDS_0X87_REQ_BASE_LEN 2U
547#define UDS_0X87_RESP_LEN 2U
549enum UDSDiagnosticServiceId {
550 kSID_DIAGNOSTIC_SESSION_CONTROL = 0x10,
551 kSID_ECU_RESET = 0x11,
552 kSID_CLEAR_DIAGNOSTIC_INFORMATION = 0x14,
553 kSID_READ_DTC_INFORMATION = 0x19,
554 kSID_READ_DATA_BY_IDENTIFIER = 0x22,
555 kSID_READ_MEMORY_BY_ADDRESS = 0x23,
556 kSID_READ_SCALING_DATA_BY_IDENTIFIER = 0x24,
557 kSID_SECURITY_ACCESS = 0x27,
558 kSID_COMMUNICATION_CONTROL = 0x28,
559 kSID_READ_PERIODIC_DATA_BY_IDENTIFIER = 0x2A,
560 kSID_DYNAMICALLY_DEFINE_DATA_IDENTIFIER = 0x2C,
561 kSID_WRITE_DATA_BY_IDENTIFIER = 0x2E,
562 kSID_IO_CONTROL_BY_IDENTIFIER = 0x2F,
563 kSID_ROUTINE_CONTROL = 0x31,
564 kSID_REQUEST_DOWNLOAD = 0x34,
565 kSID_REQUEST_UPLOAD = 0x35,
566 kSID_TRANSFER_DATA = 0x36,
567 kSID_REQUEST_TRANSFER_EXIT = 0x37,
568 kSID_REQUEST_FILE_TRANSFER = 0x38,
569 kSID_WRITE_MEMORY_BY_ADDRESS = 0x3D,
570 kSID_TESTER_PRESENT = 0x3E,
571 kSID_ACCESS_TIMING_PARAMETER = 0x83,
572 kSID_SECURED_DATA_TRANSMISSION = 0x84,
573 kSID_CONTROL_DTC_SETTING = 0x85,
574 kSID_RESPONSE_ON_EVENT = 0x86,
575 kSID_LINK_CONTROL = 0x87,
583#define UDS_ASSERT(x) assert(x)
587static inline bool UDSTimeAfter(uint32_t a, uint32_t b) {
return (int32_t)(a - b) > 0; }
596bool UDSErrIsNRC(UDSErr_t err);
598const char *UDSErrToStr(UDSErr_t err);
609#define UDS_LOG_NONE 0
610#define UDS_LOG_ERROR 1
611#define UDS_LOG_WARN 2
612#define UDS_LOG_INFO 3
613#define UDS_LOG_DEBUG 4
614#define UDS_LOG_VERBOSE 5
616typedef int UDS_LogLevel_t;
619#define UDS_LOG_LEVEL UDS_LOG_NONE
622#if UDS_CONFIG_LOG_COLORS
623#define UDS_LOG_COLOR_BLACK "30"
624#define UDS_LOG_COLOR_RED "31"
625#define UDS_LOG_COLOR_GREEN "32"
626#define UDS_LOG_COLOR_BROWN "33"
627#define UDS_LOG_COLOR_BLUE "34"
628#define UDS_LOG_COLOR_PURPLE "35"
629#define UDS_LOG_COLOR_CYAN "36"
630#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
631#define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
632#define UDS_LOG_RESET_COLOR "\033[0m"
633#define UDS_LOG_COLOR_E LOG_COLOR(UDS_LOG_COLOR_RED)
634#define UDS_LOG_COLOR_W LOG_COLOR(UDS_LOG_COLOR_BROWN)
635#define UDS_LOG_COLOR_I LOG_COLOR(UDS_LOG_COLOR_GREEN)
636#define UDS_LOG_COLOR_D
637#define UDS_LOG_COLOR_V
639#define UDS_LOG_COLOR_E
640#define UDS_LOG_COLOR_W
641#define UDS_LOG_COLOR_I
642#define UDS_LOG_COLOR_D
643#define UDS_LOG_COLOR_V
644#define UDS_LOG_RESET_COLOR
647#define UDS_LOG_FORMAT(letter, format) \
648 UDS_LOG_COLOR_##letter #letter " (%" PRIu32 ") %s: " format UDS_LOG_RESET_COLOR "\n"
650#if (UDS_LOG_LEVEL >= UDS_LOG_ERROR) && (UDS_LOG_LEVEL > UDS_LOG_NONE)
651#define UDS_LOGE(tag, format, ...) \
652 UDS_LogWrite(UDS_LOG_ERROR, tag, UDS_LOG_FORMAT(E, format), UDSMillis(), tag, ##__VA_ARGS__)
654#define UDS_LOGE(tag, format, ...) UDS_LogDummy(tag, format, ##__VA_ARGS__)
657#if UDS_LOG_LEVEL >= UDS_LOG_WARN && UDS_LOG_LEVEL > UDS_LOG_NONE
658#define UDS_LOGW(tag, format, ...) \
659 UDS_LogWrite(UDS_LOG_WARN, tag, UDS_LOG_FORMAT(W, format), UDSMillis(), tag, ##__VA_ARGS__)
661#define UDS_LOGW(tag, format, ...) UDS_LogDummy(tag, format, ##__VA_ARGS__)
664#if UDS_LOG_LEVEL >= UDS_LOG_INFO && UDS_LOG_LEVEL > UDS_LOG_NONE
665#define UDS_LOGI(tag, format, ...) \
666 UDS_LogWrite(UDS_LOG_INFO, tag, UDS_LOG_FORMAT(I, format), UDSMillis(), tag, ##__VA_ARGS__)
668#define UDS_LOGI(tag, format, ...) UDS_LogDummy(tag, format, ##__VA_ARGS__)
671#if UDS_LOG_LEVEL >= UDS_LOG_DEBUG && UDS_LOG_LEVEL > UDS_LOG_NONE
672#define UDS_LOGD(tag, format, ...) \
673 UDS_LogWrite(UDS_LOG_DEBUG, tag, UDS_LOG_FORMAT(D, format), UDSMillis(), tag, ##__VA_ARGS__)
675#define UDS_LOGD(tag, format, ...) UDS_LogDummy(tag, format, ##__VA_ARGS__)
678#if UDS_LOG_LEVEL >= UDS_LOG_VERBOSE && UDS_LOG_LEVEL > UDS_LOG_NONE
679#define UDS_LOGV(tag, format, ...) \
680 UDS_LogWrite(UDS_LOG_VERBOSE, tag, UDS_LOG_FORMAT(V, format), UDSMillis(), tag, ##__VA_ARGS__)
682#define UDS_LOGV(tag, format, ...) UDS_LogDummy(tag, format, ##__VA_ARGS__)
685#if UDS_LOG_LEVEL >= UDS_LOG_DEBUG && UDS_LOG_LEVEL > UDS_LOG_NONE
686#define UDS_LOG_SDU(tag, buffer, buff_len, info) \
687 UDS_LogSDUInternal(UDS_LOG_DEBUG, tag, buffer, buff_len, info)
689#define UDS_LOG_SDU(tag, buffer, buff_len, info) UDS_LogSDUDummy(tag, buffer, buff_len, info)
692#if defined(__GNUC__) || defined(__clang__)
693#define UDS_PRINTF_FORMAT(fmt_index, first_arg) \
694 __attribute__((format(printf, fmt_index, first_arg)))
696#define UDS_PRINTF_FORMAT(fmt_index, first_arg)
699#if UDS_LOG_LEVEL > UDS_LOG_NONE
700void UDS_LogWrite(UDS_LogLevel_t level,
const char *tag,
const char *format, ...)
701 UDS_PRINTF_FORMAT(3, 4);
702void UDS_LogSDUInternal(UDS_LogLevel_t level, const
char *tag, const uint8_t *buffer,
707static inline void UDS_LogDummy(
const char *tag,
const char *format, ...) {
711static inline void UDS_LogSDUDummy(
const char *tag,
const uint8_t *buffer,
size_t buff_len,
722#define UDS_SUPPRESS_POS_RESP 0x1
723#define UDS_FUNCTIONAL 0x2
724#define UDS_IGNORE_SRV_TIMINGS 0x8
729typedef struct UDSClient {
741 int (*fn)(
struct UDSClient *client,
UDSEvent_t evt,
void *ev_data);
746 uint8_t recv_buf[UDS_CLIENT_RECV_BUF_SIZE];
747 uint8_t send_buf[UDS_CLIENT_SEND_BUF_SIZE];
783 void *(*UnpackFn)(
void *dst,
const void *src,
size_t n);
788UDSErr_t UDSSendBytes(
UDSClient_t *client,
const uint8_t *data, uint16_t size);
789UDSErr_t UDSSendECUReset(
UDSClient_t *client, uint8_t type);
790UDSErr_t UDSSendDiagSessCtrl(
UDSClient_t *client, uint8_t mode);
791UDSErr_t UDSSendSecurityAccess(
UDSClient_t *client, uint8_t level, uint8_t *data, uint16_t size);
792UDSErr_t UDSSendCommCtrl(
UDSClient_t *client, uint8_t ctrl, uint8_t comm);
793UDSErr_t UDSSendRDBI(
UDSClient_t *client,
const uint16_t *didList,
794 const uint16_t numDataIdentifiers);
795UDSErr_t UDSSendWDBI(
UDSClient_t *client, uint16_t dataIdentifier,
const uint8_t *data,
798UDSErr_t UDSSendRoutineCtrl(
UDSClient_t *client, uint8_t type, uint16_t routineIdentifier,
799 const uint8_t *data, uint16_t size);
801UDSErr_t UDSSendRequestDownload(
UDSClient_t *client, uint8_t dataFormatIdentifier,
802 uint8_t addressAndLengthFormatIdentifier,
size_t memoryAddress,
805UDSErr_t UDSSendRequestUpload(
UDSClient_t *client, uint8_t dataFormatIdentifier,
806 uint8_t addressAndLengthFormatIdentifier,
size_t memoryAddress,
808UDSErr_t UDSSendTransferData(
UDSClient_t *client, uint8_t blockSequenceCounter,
809 const uint16_t blockLength,
const uint8_t *data, uint16_t size);
810UDSErr_t UDSSendTransferDataStream(
UDSClient_t *client, uint8_t blockSequenceCounter,
811 const uint16_t blockLength, FILE *fd);
812UDSErr_t UDSSendRequestTransferExit(
UDSClient_t *client);
814UDSErr_t UDSSendRequestFileTransfer(
UDSClient_t *client, uint8_t mode,
const char *filePath,
815 uint8_t dataFormatIdentifier, uint8_t fileSizeParameterLength,
816 size_t fileSizeUncompressed,
size_t fileSizeCompressed);
818UDSErr_t UDSCtrlDTCSetting(
UDSClient_t *client, uint8_t dtcSettingType,
819 uint8_t *dtcSettingControlOptionRecord, uint16_t len);
821UDSErr_t UDSUnpackSecurityAccessResponse(
const UDSClient_t *client,
823UDSErr_t UDSUnpackRequestDownloadResponse(
const UDSClient_t *client,
825UDSErr_t UDSUnpackRoutineControlResponse(
const UDSClient_t *client,
828UDSErr_t UDSConfigDownload(
UDSClient_t *client, uint8_t dataFormatIdentifier,
829 uint8_t addressAndLengthFormatIdentifier,
size_t memoryAddress,
830 size_t memorySize, FILE *fd);
839 uint8_t recv_buf[UDS_SERVER_RECV_BUF_SIZE];
840 uint8_t send_buf[UDS_SERVER_SEND_BUF_SIZE];
850typedef struct UDSServer {
852 UDSErr_t (*fn)(
struct UDSServer *srv,
UDSEvent_t event,
void *arg);
924 const uint32_t groupOfDTC;
940 } numOfDTCByStatusMaskArgs, dtcStatusByMaskArgs;
945 } dtcSnapshotRecordbyDTCNumArgs, userDefMemDTCSnapshotRecordByDTCNumArgs;
948 } dtcStoredDataByRecordNumArgs, dtcExtDataRecordByRecordNumArgs, dtcExtDataRecordIdArgs;
953 } dtcExtDtaRecordByDTCNumArgs, userDefMemDTCExtDataRecordByDTCNumArgs;
959 } numOfDTCBySeverityMaskArgs, dtcBySeverityMaskArgs, wwhobdDTCByMaskArgs;
962 } severityInfoOfDTCArgs;
966 } userDefMemoryDTCByStatusMaskArgs;
968 uint8_t functionalGroup;
971 } wwhobdDTCWithPermStatusArgs, dtcInfoByDTCReadinessGroupIdArgs;
979 const uint16_t dataId;
989 const size_t memSize;
1007 const uint8_t level;
1018 const uint8_t level;
1027 const uint16_t dataId;
1036 const void *memAddr;
1052 uint16_t sourceDataId;
1059 } defineByMemAddress;
1067 const uint16_t dataId;
1079 const uint8_t ctrlType;
1113 const uint8_t *
const data;
1116 uint8_t (*copyResponse)(
1125 const uint8_t *
const data;
1135 const uint8_t modeOfOperation;
1179#if defined(UDS_TP_ISOTP_C)
1180#define ISO_TP_USER_SEND_CAN_ARG 1
1181#ifndef ISOTPC_CONFIG_H
1182#define ISOTPC_CONFIG_H
1187#define ISO_TP_DEFAULT_BLOCK_SIZE 8
1192#define ISO_TP_DEFAULT_ST_MIN_US 0
1197#define ISO_TP_MAX_WFT_NUMBER 1
1202#define ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US 100000
1210#ifndef ISO_TP_FRAME_PADDING_VALUE
1211#define ISO_TP_FRAME_PADDING_VALUE 0xAA
1221#ifndef ISOTPC_USER_DEFINITIONS_H
1222#define ISOTPC_USER_DEFINITIONS_H
1230#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1231#define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
1232#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1234#error "unsupported byte ordering"
1242#define snprintf _snprintf
1247#define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
1248#define __builtin_bswap8 _byteswap_uint8
1249#define __builtin_bswap16 _byteswap_uint16
1250#define __builtin_bswap32 _byteswap_uint32
1251#define __builtin_bswap64 _byteswap_uint64
1257#define ISOTP_RET_OK 0
1258#define ISOTP_RET_ERROR -1
1259#define ISOTP_RET_INPROGRESS -2
1260#define ISOTP_RET_OVERFLOW -3
1261#define ISOTP_RET_WRONG_SN -4
1262#define ISOTP_RET_NO_DATA -5
1263#define ISOTP_RET_TIMEOUT -6
1264#define ISOTP_RET_LENGTH -7
1265#define ISOTP_RET_NOSPACE -8
1268#define IsoTpTimeAfter(a,b) ((int32_t)((int32_t)(b) - (int32_t)(a)) < 0)
1271#define ISOTP_INVALID_BS 0xFFFF
1275 ISOTP_SEND_STATUS_IDLE,
1276 ISOTP_SEND_STATUS_INPROGRESS,
1277 ISOTP_SEND_STATUS_ERROR,
1278} IsoTpSendStatusTypes;
1282 ISOTP_RECEIVE_STATUS_IDLE,
1283 ISOTP_RECEIVE_STATUS_INPROGRESS,
1284 ISOTP_RECEIVE_STATUS_FULL,
1285} IsoTpReceiveStatusTypes;
1288#if defined(ISOTP_BYTE_ORDER_LITTLE_ENDIAN)
1290 uint8_t reserve_1:4;
1292 uint8_t reserve_2[7];
1302 uint8_t FF_DL_high:4;
1326 uint8_t reserve_1:4;
1327 uint8_t reserve_2[7];
1358 uint8_t FF_DL_high:4;
1421 ISOTP_PCI_TYPE_SINGLE = 0x0,
1422 ISOTP_PCI_TYPE_FIRST_FRAME = 0x1,
1423 TSOTP_PCI_TYPE_CONSECUTIVE_FRAME = 0x2,
1424 ISOTP_PCI_TYPE_FLOW_CONTROL_FRAME = 0x3
1425} IsoTpProtocolControlInformation;
1430 PCI_FLOW_STATUS_CONTINUE = 0x0,
1431 PCI_FLOW_STATUS_WAIT = 0x1,
1432 PCI_FLOW_STATUS_OVERFLOW = 0x2
1437#define ISOTP_PROTOCOL_RESULT_OK 0
1438#define ISOTP_PROTOCOL_RESULT_TIMEOUT_A -1
1439#define ISOTP_PROTOCOL_RESULT_TIMEOUT_BS -2
1440#define ISOTP_PROTOCOL_RESULT_TIMEOUT_CR -3
1441#define ISOTP_PROTOCOL_RESULT_WRONG_SN -4
1442#define ISOTP_PROTOCOL_RESULT_INVALID_FS -5
1443#define ISOTP_PROTOCOL_RESULT_UNEXP_PDU -6
1444#define ISOTP_PROTOCOL_RESULT_WFT_OVRN -7
1445#define ISOTP_PROTOCOL_RESULT_BUFFER_OVFLW -8
1446#define ISOTP_PROTOCOL_RESULT_ERROR -9
1449#ifndef ISOTPC_USER_H
1450#define ISOTPC_USER_H
1468 const uint8_t* data,
const uint8_t size
1469#
if ISO_TP_USER_SEND_CAN_ARG
1506 uint32_t send_arbitration_id;
1508 uint8_t* send_buffer;
1509 uint16_t send_buf_size;
1511 uint16_t send_offset;
1514 uint16_t send_bs_remain;
1515 uint32_t send_st_min_us;
1516 uint8_t send_wtf_count;
1517 uint32_t send_timer_st;
1518 uint32_t send_timer_bs;
1521 int send_protocol_result;
1522 uint8_t send_status;
1524 uint32_t receive_arbitration_id;
1526 uint8_t* receive_buffer;
1527 uint16_t receive_buf_size;
1528 uint16_t receive_size;
1529 uint16_t receive_offset;
1532 uint8_t receive_bs_count;
1533 uint32_t receive_timer_cr;
1536 int receive_protocol_result;
1537 uint8_t receive_status;
1539#if defined(ISO_TP_USER_SEND_CAN_ARG)
1540 void* user_send_can_arg;
1555 uint8_t *sendbuf, uint16_t sendbufsize,
1556 uint8_t *recvbuf, uint16_t recvbufsize);
1619#if defined(UDS_TP_ISOTP_C)
1628 uint32_t phys_sa, phys_ta;
1629 uint32_t func_sa, func_ta;
1633 uint32_t source_addr;
1634 uint32_t target_addr;
1635 uint32_t source_addr_func;
1636 uint32_t target_addr_func;
1647#if defined(UDS_TP_ISOTP_C_SOCKETCAN)
1657 uint32_t phys_sa, phys_ta;
1658 uint32_t func_sa, func_ta;
1662UDSErr_t UDSTpISOTpCInit(
UDSTpISOTpC_t *tp,
const char *ifname, uint32_t source_addr,
1663 uint32_t target_addr, uint32_t source_addr_func,
1664 uint32_t target_addr_func);
1670#if defined(UDS_TP_ISOTP_SOCK)
1681 uint32_t phys_sa, phys_ta;
1682 uint32_t func_sa, func_ta;
1686UDSErr_t UDSTpIsoTpSockInitServer(
UDSTpIsoTpSock_t *tp,
const char *ifname, uint32_t source_addr,
1687 uint32_t target_addr, uint32_t source_addr_func);
1688UDSErr_t UDSTpIsoTpSockInitClient(
UDSTpIsoTpSock_t *tp,
const char *ifname, uint32_t source_addr,
1689 uint32_t target_addr, uint32_t target_addr_func);
1701#if defined(UDS_TP_ISOTP_MOCK)
1705typedef struct ISOTPMock {
1707 uint8_t recv_buf[UDS_TP_MTU];
1708 uint8_t send_buf[UDS_TP_MTU];
1715 uint32_t send_tx_delay_ms;
1716 uint32_t send_buf_size;
1734void ISOTPMockFree(
UDSTp_t *tp);
1742void ISOTPMockLogToStdout(
void);
@ UDS_EVT_RequestFileTransfer
@ UDS_EVT_ReadDTCInformation
@ UDS_EVT_ClearDiagnosticInfo
@ UDS_EVT_DynamicDefineDataId
@ UDS_EVT_SecAccessRequestSeed
@ UDS_EVT_SecAccessValidateKey
@ UDS_EVT_RequestDownload
@ UDS_EVT_WriteDataByIdent
@ UDS_EVT_RequestTransferExit
@ UDS_EVT_ResponseReceived
@ UDS_EVT_ControlDTCSetting
@ UDS_EVT_DoScheduledReset
@ UDS_EVT_ReadDataByIdent
void isotp_user_debug(const char *message,...)
user implemented, print debug message
uint32_t isotp_user_get_us(void)
user implemented, gets the amount of time passed since the last call in microseconds
UDSTp_t * ISOTPMockNew(const char *name, ISOTPMockArgs_t *args)
Create a mock transport. It is connected by default to a broadcast network of all other mock transpor...
uint32_t UDSMillis(void)
Get time in milliseconds.
int isotp_receive(IsoTpLink *link, uint8_t *payload, const uint16_t payload_size, uint16_t *out_size)
Receives and parses the received data and copies the parsed data in to the internal buffer.
void isotp_on_can_message(IsoTpLink *link, const uint8_t *data, uint8_t len)
Handles incoming CAN messages. Determines whether an incoming message is a valid ISO-TP frame or not ...
void isotp_init_link(IsoTpLink *link, uint32_t sendid, uint8_t *sendbuf, uint16_t sendbufsize, uint8_t *recvbuf, uint16_t recvbufsize)
Initialises the ISO-TP library.
void isotp_poll(IsoTpLink *link)
Polling function; call this function periodically to handle timeouts, send consecutive frames,...
int isotp_user_send_can(const uint32_t arbitration_id, const uint8_t *data, const uint8_t size, void *arg)
user implemented, send can message. should return ISOTP_RET_OK when success.
void ISOTPMockReset(void)
clear all transports and close the log file
bool UDSSecurityAccessLevelIsReserved(uint8_t securityLevel)
Check if a security level is reserved per ISO14229-1:2020 Table 42.
int isotp_send_with_id(IsoTpLink *link, uint32_t id, const uint8_t payload[], uint16_t size)
See isotp_send, with the exception that this function is used only for functional addressing.
int isotp_send(IsoTpLink *link, const uint8_t payload[], uint16_t size)
Sends ISO-TP frames via CAN, using the ID set in the initialising function.
void ISOTPMockLogToFile(const char *filename)
write all messages to a file
Struct containing the data for linking an application to a CAN instance. The data stored in this stru...
Request download response structure.
size_t maxNumberOfBlockLength
Routine control response structure.
const uint8_t * routineStatusRecord
uint16_t routineIdentifier
uint8_t routineControlType
uint16_t routineStatusRecordLength
Security access response structure.
uint8_t securityAccessType
uint16_t securitySeedLength
const uint8_t * securitySeed
Clear diagnostic information arguments.
const uint8_t memorySelection
const bool hasMemorySelection
Communication control arguments.
Control DTC setting arguments.
Custom service arguments.
const uint8_t * optionRecord
Dynamically define data identifier arguments.
Diagnostic session control arguments.
uint32_t powerDownTimeMillis
Input/output control by identifier arguments.
const uint8_t ioCtrlParam
const size_t ctrlStateAndMaskLen
const void *const ctrlStateAndMask
Read data by identifier arguments.
Read data by identifier variable structure.
Read DTC information arguments.
Read memory by address arguments.
Request download arguments.
const uint8_t dataFormatIdentifier
uint16_t maxNumberOfBlockLength
Request file transfer arguments.
const size_t fileSizeUnCompressed
uint16_t maxNumberOfBlockLength
const size_t fileSizeCompressed
const uint16_t filePathLen
const uint8_t dataFormatIdentifier
Request transfer exit arguments.
Request upload arguments.
uint16_t maxNumberOfBlockLength
const uint8_t dataFormatIdentifier
Routine control arguments.
const uint8_t * optionRecord
UDS_A_TA_Type_t A_TA_Type
Security access request seed arguments.
const uint8_t *const dataRecord
Security access validate key arguments.
uint32_t sec_access_auth_fail_timer
uint16_t p2_ms
Server time constants (milliseconds)
bool notReadyToReceive
UDS-1 2013 defines the following conditions under which the server does not process incoming requests...
uint32_t sec_access_boot_delay_timer
uint32_t s3_session_timeout_timer
uint8_t ecuResetScheduled
uint8_t xferBlockSequenceCounter
bool xferIsActive
UDS-1-2013: Table 407 - 0x36 TransferData Supported negative response codes requires that the server ...
const uint16_t maxRespLen
Write data by identifier arguments.
const uint8_t *const data
Write memory by address arguments.
const uint8_t *const data