iso14229 0.10.0
ISO14229-1 (UDS) C Library
Loading...
Searching...
No Matches
UDS Server

Quickstart

UDSServer_t server;
UDSTp_t tp;
static UDSErr_t fn(UDSServer_t *srv, UDSEvent_t ev, void *arg)
{
// your server callbacks go in here.
return UDS_PositiveResponse;
}
int main() {
UDSServerTpIsoTpSockInit(&tp, "vcan0", 0x7E0, 0x7E8, 0x7DF); // initialize transport for linux; see \ref examples for more platforms
UDSServerInit(&server);
server.tp = tp;
server.fn = fn;
while (1) {
UDSServerPoll(&server); // call UDSServerPoll at an interval of 5ms or less.
}
}
void UDSServerPoll(UDSServer_t *srv)
Call this at <5ms intervals.
Definition iso14229.c:2575
UDSEvent_t
UDS events.
Definition iso14229.h:303
UDSErr_t UDSServerTpIsoTpSockInit(UDSTpIsoTpSock_t *tp, const char *ifname, uint32_t source_addr, uint32_t target_addr, uint32_t source_addr_func)
for UDSServer_t
Definition iso14229.c:3589
UDSErr_t
Error Codes, including NRCs defined by the standard.
Definition iso14229.h:343
UDSErr_t UDSServerInit(UDSServer_t *srv)
call this once
Definition iso14229.c:2558
UDS server structure.
Definition iso14229.h:920
UDSTp_t * tp
Definition iso14229.h:921
UDSErr_t(*) fn(struct UDSServer *srv, UDSEvent_t event, void *arg)
Definition iso14229.h:922
UDS Transport layer.
Definition iso14229.h:255

The UDS server API provides functionality for implementing diagnostic services that respond to UDS client requests. The server is event-driven. Incoming client requests are processed by your service handler function (called fn by convention).

Service Handler

The service handler function server.fn is called by UDSServerPoll when an event occurs. A listing of all events is available in UDSEvent_t.

The handler structure has five parts:

  1. Switch on the incoming event
  2. Case for a specific event
  3. Cast the arg pointer to the type specified by UDSEvent_t
  4. Optionally process the arguments
  5. Return a response

Example Handler:

UDSErr_t fn(UDSServer_t *srv, UDSEvent_t event, void *arg) {
// 1: Switch on the incoming event
switch (event) {
// 2: Case for a specific event: The client has called 0x10 DiagnosticSessionControl
// 3: Cast the arg pointer to the type specified by UDSEvent_t
// 4: Optionally process the arguments
// 5: Return a response
return UDS_OK;
}
// 2: Case for a specific event: The client has called 0x22 ReadDataByIdentifier
// 3: Cast the arg pointer to the type specified by UDSEvent_t
// 4: Check the requested data ID
switch (r->dataId) {
case 0x1234: {
uint8_t data[] = {0x01, 0x02, 0x03};
// 5: Return a response
return r->copy(srv, data, sizeof(data));
break;
}
default:
// 5: Return a response
return UDS_NRC_RequestOutOfRange;
}
}
// ... handle other services
default:
return UDS_NRC_ServiceNotSupported;
}
}
@ UDS_EVT_DiagSessCtrl
Definition iso14229.h:306
@ UDS_EVT_ReadDataByIdent
Definition iso14229.h:310
Diagnostic session control arguments.
Definition iso14229.h:975
Write data by identifier arguments.
Definition iso14229.h:1107
const uint16_t dataId
Definition iso14229.h:1108

Server Structure

The UDSServer_t structure contains:

Identifier Description How to Use
tp Pointer to ISO-TP transport layer Set during initialization: server.tp = transport;
fn Event handler callback function Set during initialization: server.fn = fn;
fn_data User data bound to server, accessible in fn Optional: server.fn_data = &my_data;
p2_ms P2 timeout in milliseconds Internal use only; Set default with UDS_SERVER_DEFAULT_P2_MS
p2_star_ms P2* timeout in milliseconds Internal use only; Set default with UDS_SERVER_DEFAULT_P2_STAR_MS
s3_ms S3 session timeout in milliseconds Internal use only; Set default with UDS_SERVER_DEFAULT_S3_MS
sessionType Current diagnostic session Read: if (server.sessionType == UDS_LEV_DS_EXTDS)
securityLevel Current security level Read/write: server.securityLevel = args->level;
xferIsActive Transfer operation active flag Read: if (server.xferIsActive)
xferBlockSequenceCounter Transfer block sequence counter Read only
r Current request/response buffers Internal use only

Server Events

See UDSEvent_t for the mapping from event to argument type.

Responding to Requests

Positive Response

Return UDS_OK or use the copy function to send data:

UDSRDBIArgs_t *args = (UDSRDBIArgs_t *)arg;
uint8_t vin[] = "WBADT43452G123456";
return args->copy(srv, vin, sizeof(vin) - 1);
}
Read data by identifier arguments.
Definition iso14229.h:1059
uint8_t(*) copy(UDSServer_t *srv, const void *src, uint16_t count)
Definition iso14229.h:1061

Negative Response

Return a Negative Response Code (NRC):

if (args->id != 0x1234) {
return UDS_NRC_RequestOutOfRange;
}
return UDS_OK;
}
@ UDS_EVT_RoutineCtrl
Definition iso14229.h:319
Routine control arguments.
Definition iso14229.h:1159
const uint16_t id
Definition iso14229.h:1161

Response Pending

Return UDS_NRC_RequestCorrectlyReceived_ResponsePending (0x78) to indicate that processing is taking longer than P2:

if (routine_still_running) {
return UDS_NRC_RequestCorrectlyReceived_ResponsePending;
}
return UDS_OK;
}

This is used to prevent the client from timing out during long-running server actions such as writing to flash memory.

To control long-running tasks asynchronously, consider using UDSSendRoutineCtrl .

Session Management

The server tracks the current diagnostic session:

// Extended diagnostic session is active
}
#define UDS_LEV_DS_EXTDS
Extended Diagnostic Session.
Definition iso14229.h:445
uint8_t sessionType
Definition iso14229.h:952

Sessions automatically timeout after S3 time of inactivity, returning to the default session.

Some configuration options are set at compile-time. See : Compile-Time Configuration.