UDS: requests, sessions and timing
Service identifiers, negative response codes and their order, sessions, P2, P2, S3 and response pending.
One protocol for every ECU
A modern car has between 30 and 150 ECUs from dozens of suppliers. In the factory each must be programmed and configured; in the workshop each must report its faults; in development each must be measured and tweaked. UDS (Unified Diagnostic Services, ISO 14229-1) is the one protocol they all share for this. It defines about 25 services, each a request from a client (the tester) and a response from a server (the ECU).
UDS says nothing about wires. On CAN it rides on ISO-TP (ISO 14229-3, "UDSonCAN"); on Ethernet on DoIP (ISO 13400); it also runs over LIN, FlexRay and K-line. This lesson's examples use CAN, with each message shown as its bytes after ISO-TP has reassembled it.
The shape of a request and a response
A request starts with a service identifier (SID). A positive response starts with the SID + 0x40 and echoes enough of the request to be matched to it:
request 22 F1 90 ReadDataByIdentifier, DID 0xF190
positive response 62 F1 90 54 45 31 ... 0x22 + 0x40, the DID, the data
A refusal always has the same three bytes: 7F, the SID that was refused, and a negative response code (NRC) saying why.
request 22 12 34
negative response 7F 22 31 requestOutOfRange: no such identifier
The services you will meet most:
| SID | Service | Has a sub-function |
|---|---|---|
10 | DiagnosticSessionControl | yes |
11 | ECUReset | yes |
14 | ClearDiagnosticInformation | no |
19 | ReadDTCInformation | yes |
22 | ReadDataByIdentifier | no |
27 | SecurityAccess | yes |
28 | CommunicationControl | yes |
2E | WriteDataByIdentifier | no |
31 | RoutineControl | yes |
34 36 37 | RequestDownload, TransferData, RequestTransferExit | no |
3E | TesterPresent | yes |
85 | ControlDTCSetting | yes |
For a service with a sub-function, the byte after the SID selects the variant in its low 7 bits, and its top bit is the suppressPosRspMsgIndicationBit: when set, the server does the work but sends no positive response. 3E 00 is answered with 7E 00; 3E 80 is answered with silence. Negative responses are sent regardless, so a tester that suppresses responses still learns of failures.
Negative response codes
There are about forty NRCs. These are the ones an ECU engineer must know by number:
| NRC | Name | Means |
|---|---|---|
10 | generalReject | none of the others fits (a well-written server rarely needs it) |
11 | serviceNotSupported | this ECU does not implement the SID |
12 | subFunctionNotSupported | the SID yes, this sub-function no |
13 | incorrectMessageLengthOrInvalidFormat | the request has the wrong number of bytes |
21 | busyRepeatRequest | cannot now; ask again |
22 | conditionsNotCorrect | not in this state: vehicle moving, engine running, voltage too low |
24 | requestSequenceError | a step was skipped: a key without a seed, a transfer without a download request |
31 | requestOutOfRange | an identifier, address or value this ECU does not accept |
33 | securityAccessDenied | the ECU is locked for this |
35 36 37 | invalidKey, exceededNumberOfAttempts, requiredTimeDelayNotExpired | SecurityAccess, in a later lesson |
72 | generalProgrammingFailure | erasing or writing memory failed |
78 | requestCorrectlyReceived-ResponsePending | not a refusal: "working on it" (below) |
7E | subFunctionNotSupportedInActiveSession | the sub-function exists, but not in this session |
7F | serviceNotSupportedInActiveSession | the service exists, but not in this session |
The order of the checks is specified
A request can be wrong in several ways at once: an unknown identifier, in the wrong session, with a byte missing. Which NRC comes back is not the implementer's choice. ISO 14229-1 gives a decision sequence, and manufacturers' conformance tests check it request by request. The general order:
- Is the service supported at all? If not,
11. - Is it supported in the active session? If not,
7F. - Is the message at least the minimum length? If not,
13. - For a service with a sub-function: is the sub-function supported (
12), and supported in this session (7E)? - Is the total length right for this sub-function or identifier? If not,
13. - Is the security level sufficient? If not,
33. - Are the conditions right (
22), and is the sequence right (24)? - Are the parameters in range? If not,
31.
Each service's section of the standard refines this with its own flow chart. The reason for fixing the order is security and consistency: an ECU that answered 31 for an unknown identifier before checking security would let a locked-out tester map which identifiers exist.
#include <stdio.h>
#include <stdint.h>
#define SESSION_DEFAULT 1
#define SESSION_EXTENDED 3
/* A table of services: one row a SID, with what the checks need to know. */
typedef struct { uint8_t sid; uint8_t min_len; uint8_t sessions; } service_t; /* sessions: bit n set = allowed in session n */
static const service_t SERVICES[] = {
{ 0x10, 2, (1u << SESSION_DEFAULT) | (1u << SESSION_EXTENDED) },
{ 0x22, 3, (1u << SESSION_DEFAULT) | (1u << SESSION_EXTENDED) },
{ 0x2E, 4, (1u << SESSION_EXTENDED) },
{ 0x3E, 2, (1u << SESSION_DEFAULT) | (1u << SESSION_EXTENDED) },
};
/* The first NRC the general checks give, or 0 when the service's own handler may run. */
static uint8_t general_checks(const uint8_t *req, uint16_t len, uint8_t session) {
for (unsigned i = 0; i < sizeof SERVICES / sizeof SERVICES[0]; i++) {
if (SERVICES[i].sid != req[0]) continue;
if (!(SERVICES[i].sessions & (1u << session))) return 0x7F;
if (len < SERVICES[i].min_len) return 0x13;
return 0;
}
return 0x11;
}
static void try_request(const uint8_t *req, uint16_t len, uint8_t session) {
uint8_t nrc = general_checks(req, len, session);
if (nrc) printf("%02X in session %u: 7F %02X %02X\n", req[0], session, req[0], nrc);
else printf("%02X in session %u: passed to its handler\n", req[0], session);
}
int main(void) {
const uint8_t write[] = { 0x2E, 0x01 }; /* too short, and needs the extended session */
const uint8_t unknown[] = { 0xBA, 0x00 };
const uint8_t read[] = { 0x22, 0xF1, 0x90 };
try_request(write, sizeof write, SESSION_DEFAULT); /* the session is checked before the length */
try_request(write, sizeof write, SESSION_EXTENDED);
try_request(unknown, sizeof unknown, SESSION_DEFAULT);
try_request(read, sizeof read, SESSION_DEFAULT);
return 0;
}
A table like this is how production servers are built (in AUTOSAR it is generated from the diagnostic configuration). One generic function runs the common checks in the specified order for every service, so no handler can get them wrong.
Sessions
A diagnostic session is a mode of the server that decides which services and identifiers are available.
| Session | Value | For |
|---|---|---|
| Default | 01 | Always on after power-up. Reading only: identification, trouble codes, live data |
| Programming | 02 | Reflashing. In most ECUs entering it resets into the bootloader, which serves this session |
| Extended | 03 | Workshop and end-of-line work: writing configuration, actuator tests, routines |
| Safety system | 04 | Airbag deployment loops and similar |
40 to 5F | The vehicle manufacturer's own |
The tester changes session with 10 <session>. The positive response carries the server's timing for that session:
request 10 03
response 50 03 00 32 01 F4
| |
| +-- P2*: 0x01F4 = 500, in units of 10 ms = 5000 ms
+-------- P2: 0x0032 = 50 ms
Rules worth knowing exactly:
- A transition to a non-default session, or back to default, re-locks security and ends anything session-bound: periodic transmissions stop, and controls changed with CommunicationControl or ControlDTCSetting are restored.
- Asking for the session already active is allowed and answered positively; it re-initialises that session (security locks again).
- Each ECU has its own session. A functional
10 03on0x7DFmoves every ECU that supports it.
Timing: P2, P2* and response pending
| Parameter | Default | Meaning |
|---|---|---|
| P2 (P2server_max) | 50 ms | From the end of a request to the start of the server's response |
| P2\* (P2*server_max) | 5000 ms | The same, after the server has sent NRC 78 |
| S3 (S3server) | 5000 ms | How long a non-default session survives without a request |
P2 is a hard deadline. Some jobs cannot meet it: erasing a megabyte of flash takes seconds. For those the server sends 7F <SID> 78, requestCorrectlyReceived-ResponsePending, before P2 expires. It means "your request was valid; the real answer is coming". The tester then waits up to P2\. If the job needs longer still, the server sends `78` again before each P2\ expires, and finally the positive or negative response.
t = 0 ms tester 31 01 FF 00 erase memory
t = 40 ms ECU 7F 31 78 pending: tester now waits P2*
t = 4500 ms ECU 7F 31 78 still working
t = 6200 ms ECU 71 01 FF 00 done
Three things implementers get wrong:
- Sending
78too late. P2 is measured at the tester, which also has bus and gateway delays. Servers send the first78well inside P2 (40 ms is common for a 50 ms P2), and repeat it at around 70 to 80 percent of P2\*. - Blocking while the job runs. If the erase is one blocking call, nothing sends
78. The work must be split into steps the main loop calls between other duties, or the timer that sends78must run from an interrupt or another task. - Suppressing the final response. If a request had the suppress bit set and the server sent
78, the final response must be sent anyway: the tester has been told an answer is coming.
A manufacturer usually caps the number of 78 responses, so that a stuck ECU cannot hold a tester for ever.
S3 and TesterPresent
A tester that is unplugged in the middle of an extended session must not leave the ECU with its security unlocked and its normal messages switched off. The S3 timer does the cleanup: it runs whenever the server is in a non-default session, restarts on every request, and on expiry returns the server to the default session, as if 10 01 had been received.
A tester that wants to stay in a session while the technician reads the screen sends TesterPresent (3E) periodically, typically every 2 s, usually as 3E 80 on the functional address so that one frame keeps every ECU's session alive and none of them answers.
The details that conformance tests probe:
- S3 restarts on any complete request, also one that is answered negatively, and one whose response is suppressed.
- S3 does not run while the server is still producing a response (a
78sequence does not time the session out). - In the default session S3 does nothing.
Interview question: What are P2 and P2* in UDS, and how does a server use response pending?
Answer: P2 is the maximum time from the end of a request to the start of the server's response, 50 ms by default. P2* is the extended limit that applies after the server has sent negative response code 0x78, 5000 ms by default; both values are reported in the positive response to DiagnosticSessionControl. When a job cannot finish within P2, such as erasing flash, the server sends 7F, the service identifier and 0x78 before P2 expires, and repeats it before each P2* expires until it can send the final positive or negative response. The 0x78 response means the request was accepted and is being processed, so it must only be sent after the request has passed its checks.
Interview question: Why does UDS specify the order in which a server checks a request?
Answer: A request can be invalid in several ways at once, and the negative response code would otherwise depend on each supplier's code structure, so a tester could not rely on it and conformance could not be tested. A fixed order also prevents information leaking: checking the session and security level before the identifier's range means a locked or default-session tester cannot discover which protected identifiers exist by reading which code comes back. In practice the generic checks, service supported, supported in session, minimum length, sub-function, security, are done once in a table-driven dispatcher before any service handler runs.
Going deeper
Interview question: An ECU takes 300 ms to answer one ReadDataByIdentifier because the value comes from another processor over SPI. How should the diagnostic server handle it?
Answer: The handler must not block the main loop for 300 ms. It should start the SPI exchange and return "pending" to the dispatcher, which records that a request is in progress. A timer sends 7F 22 78 at about 40 ms, inside P2, and the main loop keeps running its other work, including the transport layer and the session timer. When the SPI answer arrives the server sends the positive response; if the exchange fails or exceeds its own limit, it sends a negative response such as conditionsNotCorrect. While the request is pending, a new request on the same connection is answered with busyRepeatRequest or ignored, according to the manufacturer's rules.
Interview question: Why are some negative responses not sent for functionally addressed requests?
Answer: A functional request reaches every ECU on the bus. If each ECU that did not support the service, sub-function or identifier answered "not supported", the tester would receive dozens of useless frames for every broadcast, and the answers of the ECUs that do support it would be delayed. ISO 14229-1 therefore says that for functionally addressed requests the server sends no response for the codes serviceNotSupported, subFunctionNotSupported, requestOutOfRange, and their in-active-session variants 0x7E and 0x7F. Positive responses and other negative responses, such as a length error, are still sent.
Interview question: What happens to a diagnostic session when the ECU resets?
Answer: The session is state in RAM, so after any reset the server is in the default session with security locked. The one designed exception is the jump between application and bootloader for reprogramming: the application, on receiving a request for the programming session, sets a flag that survives reset, in non-volatile memory or a RAM section that start-up code does not clear, and resets. The bootloader sees the flag, starts directly in the programming session and sends the positive response to the original request. The same mechanism brings the server back after the final ECUReset.
Explain it: In your own words, explain what the S3 timer is for and exactly when it restarts.
Answer: S3 is a watchdog on the tester's presence. While a server is in any session other than the default, it must receive a request at least every S3, 5 s by default; if it does not, it returns to the default session and locks security, so an unplugged or crashed tester cannot leave the ECU in a privileged state. The timer restarts whenever a complete request is received, whatever its service and whether its response is positive, negative or suppressed, which is why a tester sends TesterPresent with the suppress bit every couple of seconds. It does not run in the default session, and it is held while the server is still working on a response.