Simvik

A session that times out

Track the diagnostic session and the S3 timer that returns an ECU to the default session. A medium embedded C problem (UDS: requests, sessions and timing),…

medium · UDS: requests, sessions and timing · uds · automotive · diagnostics · state-machine · timers · rollover

An ECU leaves the extended session by itself when the tester goes quiet: that is the S3 timer. Write three functions on diag_t. Times are a free-running millisecond counter that wraps around after 2^32 ms (49.7 days).

  • diag_request(d, now): called for every complete request, whatever it is. Restarts S3.
  • diag_set_session(d, now, session): a successful DiagnosticSessionControl. Sets the session and restarts S3. Every session change, also to the session already active, locks security (unlocked = false).
  • diag_tick(d, now): called periodically. In a session other than the default, once S3_MS or more has passed since the last request, the ECU returns to the default session and locks.

The default session never times out. d starts as { SESSION_DEFAULT, false, 0 }.

Starter code

void diag_request(diag_t *d, uint32_t now) {
  (void)d; (void)now;
}

void diag_set_session(diag_t *d, uint32_t now, uint8_t session) {
  d->session = session;
  d->last_request_ms = now;
}

void diag_tick(diag_t *d, uint32_t now) {
  if (now > d->last_request_ms + S3_MS) d->session = SESSION_DEFAULT;
}

Example tests

Silence ends the extended session

diag_t d = { SESSION_DEFAULT, false, 0 };
diag_set_session(&d, 1000, SESSION_EXTENDED);
d.unlocked = true;
diag_tick(&d, 5999);
show(&d, 5999);
diag_tick(&d, 6000);
show(&d, 6000);

Expected output:

t=5999: session 3, unlocked
t=6000: session 1, locked

Requests keep it alive

diag_t d = { SESSION_DEFAULT, false, 0 };
diag_set_session(&d, 0, SESSION_EXTENDED);
d.unlocked = true;
for (uint32_t t = 2000; t <= 10000; t += 2000) {
  diag_request(&d, t);
  diag_tick(&d, t + 1999);
}
show(&d, 11999);
diag_tick(&d, 15000);
show(&d, 15000);

Expected output:

t=11999: session 3, unlocked
t=15000: session 1, locked

Learn it first

More UDS: requests, sessions and timing problems