Diag day: Route A+B verified live, DTCs read, donor identified as E60 530d

- diag probe: add mode 03 (diag dtc), BMW KWP on 0x6F1 with extended
  addressing (diag ident, diag bmw), read-only service whitelist
  (1A/18/21/22), MAX_PAYLOAD 64->384, full hex+ascii dump of BMW replies
- tools/diag_session.py: one-session probe runner, logs into wiki/raw/
- wiki: Route A results (11 mode-01 PIDs, rpm/coolant cross-checked, mode
  09 silent), Route B verified (0x6F1 [12] -> 0x612), 1A 86 ident with
  VIN prefix WBANC92090 (E60 530d), DTCs P0101/P0113/P0500/P0545/P0030
  (disconnected-sensor swap leftovers), open-questions #2/#7 updated

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-23 12:54:24 +02:00
parent e9fc24778a
commit 7a7398243b
20 changed files with 87140 additions and 51 deletions
+196 -36
View File
@@ -25,13 +25,15 @@ static const uint32_t OBD_RESP_LAST = 0x7EF; // physical request = response
static const uint32_t OBD_REQ_FIRST = 0x7E0;
static const uint32_t OBD_REQ_LAST = 0x7E7;
static const uint32_t BMW_TESTER_ID = 0x6F1; // BMW KWP/UDS tester (Launch, INPA)
static const uint32_t BMW_RX_BASE = 0x600; // an ECU replies on 0x600 + its address
static const uint8_t BMW_ADDR_DDE = 0x12; // engine ECU address (reply on 0x612)
static const uint32_t REPLY_TIMEOUT_MS = 500; // P2 is 50 ms; be generous
static const uint32_t LINGER_MS = 150; // after the first reply, wait for other ECUs
static const uint32_t PENDING_EXTRA_MS = 2000; // NRC 0x78 "response pending"
static const uint32_t TESTER_GUARD_MS = 3000; // refuse to send this soon after a foreign tester
static const uint8_t PAD_BYTE = 0x00;
static const size_t MAX_PAYLOAD = 64;
static const size_t MAX_PAYLOAD = 384; // KWP ident replies can be long
static const int MAX_RESPONDERS = 4;
static const int SCAN_BLOCKS = 8; // 0x00, 0x20, ... 0xE0
@@ -50,7 +52,10 @@ struct responder_t {
static SemaphoreHandle_t g_mutex = NULL;
static state_t g_state = ST_IDLE;
static uint8_t g_req_mode = 0, g_req_pid = 0;
static uint8_t g_req_mode = 0;
static int g_req_pid = 0; // -1 = request had no PID/sub byte (e.g. mode 03)
static bool g_bmw = false; // current request is BMW-addressed on 0x6F1
static uint8_t g_bmw_target = 0; // its target ECU address (reply on 0x600+addr)
static uint32_t g_deadline_ms = 0;
static uint8_t g_replies_this_req = 0;
@@ -190,8 +195,8 @@ static responder_t* findResponder(uint32_t id, bool create) {
// Transmit
// ----------------------------------------------------------------------------
/** Build and send one single-frame request. Caller holds no lock. */
static bool sendRequest(uint8_t mode, uint8_t pid) {
/** Common preconditions for any transmit; prints the refusal reason. */
static bool clearToSend() {
if (!CANDriver::isActive()) {
print("[diag] refused: CAN is not running");
return false;
@@ -212,23 +217,41 @@ static bool sendRequest(uint8_t mode, uint8_t pid) {
(unsigned long)tester_id, (unsigned long)since_tester);
return false;
}
return true;
}
uint8_t f[8];
memset(f, PAD_BYTE, sizeof(f));
f[0] = 0x02; // ISO-TP single frame, 2 bytes
f[1] = mode;
f[2] = pid;
/** Arm the waiting state. Caller sends the frame right after. */
static void armRequest(uint8_t mode, int pid, bool bmw, uint8_t target) {
lock();
g_state = ST_WAITING;
g_bmw = bmw;
g_bmw_target = target;
g_req_mode = mode;
g_req_pid = pid;
g_deadline_ms = now + REPLY_TIMEOUT_MS;
g_deadline_ms = millis() + REPLY_TIMEOUT_MS;
g_replies_this_req = 0;
g_rx_active = false;
g_requests++;
g_tx_fail_at_send = CANDriver::getStats().tx_failures;
unlock();
}
/** Build and send one single-frame OBD request on 0x7DF. pid < 0 = mode only. */
static bool sendRequest(uint8_t mode, int pid) {
if (!clearToSend()) return false;
uint8_t f[8];
memset(f, PAD_BYTE, sizeof(f));
if (pid < 0) {
f[0] = 0x01; // ISO-TP single frame, 1 byte (e.g. mode 03)
f[1] = mode;
} else {
f[0] = 0x02; // ISO-TP single frame, 2 bytes
f[1] = mode;
f[2] = (uint8_t)pid;
}
armRequest(mode, pid, false, 0);
if (!CANDriver::transmit(OBD_FUNCTIONAL_ID, f, sizeof(f))) {
lock();
@@ -237,7 +260,52 @@ static bool sendRequest(uint8_t mode, uint8_t pid) {
print("[diag] transmit failed (TWAI queue)");
return false;
}
print("[diag] -> 0x7DF %02X %02X", mode, pid);
if (pid < 0) print("[diag] -> 0x7DF %02X", mode);
else print("[diag] -> 0x7DF %02X %02X", mode, pid);
return true;
}
/**
* One single-frame BMW-addressed request on 0x6F1 (ISO 15765 extended
* addressing: byte 0 = target ECU address, then normal ISO-TP). Read-only
* services only - enforced here, not just at the console.
*/
static bool sendBmwRequest(uint8_t target, const uint8_t* req, size_t n) {
static const uint8_t OK_SERVICES[] = {0x1A, 0x18, 0x21, 0x22};
if (n < 1 || n > 6) {
print("[diag] BMW request must be 1..6 bytes (single frame)");
return false;
}
bool allowed = false;
for (size_t i = 0; i < sizeof(OK_SERVICES); i++) {
if (req[0] == OK_SERVICES[i]) allowed = true;
}
if (!allowed) {
print("[diag] service 0x%02X refused - read-only services only (1A ident, 18 DTCs, 21/22 read data)",
req[0]);
return false;
}
if (!clearToSend()) return false;
uint8_t f[8];
memset(f, PAD_BYTE, sizeof(f));
f[0] = target;
f[1] = (uint8_t)n;
memcpy(f + 2, req, n);
armRequest(req[0], (n >= 2) ? (int)req[1] : -1, true, target);
if (!CANDriver::transmit(BMW_TESTER_ID, f, sizeof(f))) {
lock();
g_state = ST_IDLE;
unlock();
print("[diag] transmit failed (TWAI queue)");
return false;
}
char hex[3 * 6 + 1];
hexdump(hex, sizeof(hex), req, n);
print("[diag] -> 0x6F1 [%02X] %s (reply expected on 0x%03lX)", target, hex,
(unsigned long)(BMW_RX_BASE + target));
return true;
}
@@ -245,9 +313,17 @@ static bool sendRequest(uint8_t mode, uint8_t pid) {
static void sendFlowControl(uint32_t responder_id) {
uint8_t f[8];
memset(f, PAD_BYTE, sizeof(f));
f[0] = 0x30; // FC, ContinueToSend
f[1] = 0x00; // block size: all
f[2] = 0x00; // STmin: 0 ms
if (g_bmw) {
f[0] = g_bmw_target; // extended addressing: target first, then the FC
f[1] = 0x30; // FC, ContinueToSend
f[2] = 0x00; // block size: all
f[3] = 0x00; // STmin: 0 ms
CANDriver::transmit(BMW_TESTER_ID, f, sizeof(f));
return;
}
f[0] = 0x30;
f[1] = 0x00;
f[2] = 0x00;
CANDriver::transmit(responder_id - 8, f, sizeof(f));
}
@@ -259,8 +335,8 @@ static void finishScanBlockIfPrimary(uint32_t id, const uint8_t* p, size_t n);
/** A complete ISO-TP payload from `id`. Caller holds g_mutex. */
static void handleReply(uint32_t id, const uint8_t* p, size_t n) {
char hex[3 * 24 + 1];
hexdump(hex, sizeof(hex), p, n > 24 ? 24 : n);
char hex[3 * 48 + 1];
hexdump(hex, sizeof(hex), p, n > 48 ? 48 : n);
g_replies++;
if (n >= 3 && p[0] == 0x7F) {
@@ -284,10 +360,40 @@ static void handleReply(uint32_t id, const uint8_t* p, size_t n) {
g_replies_this_req++;
if (g_deadline_ms > millis() + LINGER_MS) g_deadline_ms = millis() + LINGER_MS;
print("[diag] <- 0x%03lX %s%s", (unsigned long)id, hex, n > 24 ? " ..." : "");
print("[diag] <- 0x%03lX %s%s", (unsigned long)id, hex, n > 48 ? " ..." : "");
if (n < 2 || p[0] != (uint8_t)(g_req_mode + 0x40) || p[1] != g_req_pid) {
print("[diag] (unexpected payload for request %02X %02X)", g_req_mode, g_req_pid);
if (n < 1 || p[0] != (uint8_t)(g_req_mode + 0x40) ||
(!g_bmw && g_req_pid >= 0 && (n < 2 || p[1] != (uint8_t)g_req_pid))) {
print("[diag] (unexpected payload for request %02X)", g_req_mode);
return;
}
if (g_bmw) {
// KWP ident replies are long and the header line clips at 48 bytes:
// dump the whole payload, 16 bytes per line, hex + ascii.
for (size_t i = 1; i < n; i += 16) {
size_t k = (n - i < 16) ? (n - i) : 16;
char hx[3 * 16 + 1];
hexdump(hx, sizeof(hx), p + i, k);
char as[17];
for (size_t j = 0; j < k; j++) {
as[j] = (p[i + j] >= 0x20 && p[i + j] < 0x7F) ? (char)p[i + j] : '.';
}
as[k] = 0;
print("[diag] +%03u %-48s |%s|", (unsigned)(i - 1), hx, as);
}
return;
}
if (g_req_mode == 0x03) {
// 43 <count> <2 bytes per DTC>
uint8_t cnt = (n >= 2) ? p[1] : 0;
print("[diag] %u stored DTC(s):", cnt);
for (size_t i = 2; i + 1 < n; i += 2) {
uint8_t a = p[i], b = p[i + 1];
if (a == 0 && b == 0) continue;
print("[diag] %c%u%X%02X", "PCBU"[a >> 6], (a >> 4) & 3, a & 0x0F, b);
}
return;
}
@@ -340,27 +446,40 @@ void onFrame(const twai_message_t& m) {
return;
}
if (id < OBD_RESP_FIRST || id > OBD_RESP_LAST) return;
if (m.data_length_code == 0) return;
bool maybe_obd = (id >= OBD_RESP_FIRST && id <= OBD_RESP_LAST);
bool maybe_bmw = (id >= BMW_RX_BASE && id < BMW_RX_BASE + 0x100);
if (!maybe_obd && !maybe_bmw) return;
lock();
if (g_state != ST_WAITING) { unlock(); return; }
uint8_t pci = m.data[0] >> 4;
const uint8_t* d = m.data;
uint8_t dlc = m.data_length_code;
uint8_t off = 0; // index of the ISO-TP PCI byte
if (g_bmw) {
// extended addressing: reply on 0x600+target, byte 0 = tester addr 0xF1
if (id != BMW_RX_BASE + g_bmw_target || dlc < 2 || d[0] != 0xF1) { unlock(); return; }
off = 1;
} else if (!maybe_obd) {
unlock();
return;
}
uint8_t pci = d[off] >> 4;
if (pci == 0x0) {
// single frame
size_t len = d[0] & 0x0F;
if (len > 0 && len + 1 <= dlc) handleReply(id, d + 1, len);
size_t len = d[off] & 0x0F;
if (len > 0 && off + 1 + len <= dlc) handleReply(id, d + off + 1, len);
} else if (pci == 0x1) {
// first frame: 1L LL data[6]
if (!g_rx_active && dlc >= 2) {
g_rx_expected = ((size_t)(d[0] & 0x0F) << 8) | d[1];
// first frame: 1L LL data[...]
if (!g_rx_active && dlc >= (uint8_t)(off + 2)) {
g_rx_expected = ((size_t)(d[off] & 0x0F) << 8) | d[off + 1];
if (g_rx_expected > MAX_PAYLOAD) g_rx_expected = MAX_PAYLOAD;
g_rx_len = 0;
for (uint8_t i = 2; i < dlc && g_rx_len < g_rx_expected; i++) g_rx_buf[g_rx_len++] = d[i];
for (uint8_t i = off + 2; i < dlc && g_rx_len < g_rx_expected; i++) g_rx_buf[g_rx_len++] = d[i];
g_rx_id = id;
g_rx_seq = 1;
g_rx_active = true;
@@ -370,9 +489,9 @@ void onFrame(const twai_message_t& m) {
return;
}
} else if (pci == 0x2) {
// consecutive frame: 2N data[7]
if (g_rx_active && id == g_rx_id && (d[0] & 0x0F) == (g_rx_seq & 0x0F)) {
for (uint8_t i = 1; i < dlc && g_rx_len < g_rx_expected; i++) g_rx_buf[g_rx_len++] = d[i];
// consecutive frame: 2N data[...]
if (g_rx_active && id == g_rx_id && (d[off] & 0x0F) == (g_rx_seq & 0x0F)) {
for (uint8_t i = off + 1; i < dlc && g_rx_len < g_rx_expected; i++) g_rx_buf[g_rx_len++] = d[i];
g_rx_seq++;
g_deadline_ms = millis() + REPLY_TIMEOUT_MS;
if (g_rx_len >= g_rx_expected) {
@@ -455,8 +574,13 @@ void tick() {
unlock();
if (!got_reply) {
print("[diag] no reply within %lu ms (request %02X %02X)%s", (unsigned long)REPLY_TIMEOUT_MS,
g_req_mode, g_req_pid, unacked ? " - frame was not ACKed by any node" : " - frame was ACKed, ECU stayed silent");
if (g_req_pid < 0) {
print("[diag] no reply within %lu ms (request %02X)%s", (unsigned long)REPLY_TIMEOUT_MS,
g_req_mode, unacked ? " - frame was not ACKed by any node" : " - frame was ACKed, ECU stayed silent");
} else {
print("[diag] no reply within %lu ms (request %02X %02X)%s", (unsigned long)REPLY_TIMEOUT_MS,
g_req_mode, g_req_pid, unacked ? " - frame was not ACKed by any node" : " - frame was ACKed, ECU stayed silent");
}
}
if (scan) {
if (send_next) {
@@ -474,10 +598,13 @@ void tick() {
static void help() {
print("diag pids scan mode-01 supported-PID bitmaps (the Route A probe)");
print("diag pid <hex> one mode-01 request, e.g. `diag pid 0C` (rpm), `diag pid 70` (boost)");
print("diag dtc mode 03 - read stored generic DTCs");
print("diag vin mode 09 PID 02 - VIN");
print("diag ecu mode 09 PID 0A - ECU name");
print("diag ident [addr] BMW KWP `1A 80` ECU identification (default addr 12 = DDE, reply 0x612)");
print("diag bmw <addr> <hex...> raw BMW request on 0x6F1, read-only services only (1A/18/21/22)");
print("diag status counters, responders, tester guard");
print("Sends on 0x7DF only, one request at a time; refused while a scan tool is polling.");
print("Sends on 0x7DF / 0x6F1 only, one request at a time; refused while a scan tool is polling.");
}
static void status() {
@@ -507,9 +634,9 @@ bool handleCommand(const char* line) {
strncpy(buf, line, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = 0;
char* argv[4] = {0};
char* argv[10] = {0};
int argc = 0;
for (char* tok = strtok(buf, " \t"); tok && argc < 4; tok = strtok(NULL, " \t")) {
for (char* tok = strtok(buf, " \t"); tok && argc < 10; tok = strtok(NULL, " \t")) {
argv[argc++] = tok;
}
if (argc == 0 || strcmp(argv[0], "diag") != 0) return false;
@@ -544,6 +671,39 @@ bool handleCommand(const char* line) {
if (!strcmp(argv[1], "vin")) { sendRequest(0x09, 0x02); return true; }
if (!strcmp(argv[1], "ecu")) { sendRequest(0x09, 0x0A); return true; }
if (!strcmp(argv[1], "dtc")) { sendRequest(0x03, -1); return true; }
if (!strcmp(argv[1], "ident")) {
uint8_t target = BMW_ADDR_DDE;
if (argc >= 3) target = (uint8_t)strtol(argv[2], NULL, 16);
uint8_t req[2] = {0x1A, 0x80};
sendBmwRequest(target, req, sizeof(req));
return true;
}
if (!strcmp(argv[1], "bmw") && argc >= 4) {
uint8_t target = (uint8_t)strtol(argv[2], NULL, 16);
char hexs[13] = {0}; // up to 6 bytes = 12 hex chars
size_t hl = 0;
for (int i = 3; i < argc; i++) {
for (const char* c = argv[i]; *c; c++) {
if (hl >= sizeof(hexs) - 1) { print("[diag] too many bytes (max 6)"); return true; }
hexs[hl++] = *c;
}
}
if (hl == 0 || (hl % 2) != 0) {
print("[diag] give whole hex bytes, e.g. `diag bmw 12 1A 80`");
return true;
}
uint8_t req[6];
size_t n = 0;
for (size_t i = 0; i < hl; i += 2) {
char b[3] = {hexs[i], hexs[i + 1], 0};
req[n++] = (uint8_t)strtol(b, NULL, 16);
}
sendBmwRequest(target, req, n);
return true;
}
help();
return true;
+8
View File
@@ -15,11 +15,19 @@
* Console (USB serial, same line reader as `cfg`):
* diag pids scan the mode-01 supported-PID bitmaps (00, 20, 40, ...)
* diag pid <hex> one mode-01 request, e.g. `diag pid 0C` (rpm)
* diag dtc mode 03 — stored generic DTCs
* diag vin mode 09 PID 02 — VIN (identifies the donor)
* diag ecu mode 09 PID 0A — ECU name string
* diag ident [addr] BMW KWP `1A 80` ECU identification on 0x6F1 (default 12 = DDE)
* diag bmw <addr> <hex...> raw BMW-addressed request; read-only services only
* diag status counters, responders seen, tester-guard timer
* diag help
*
* BMW requests use ISO 15765 extended addressing: TX on 0x6F1 with byte 0 =
* target ECU address, reply on 0x600+address with byte 0 = 0xF1. Allowed
* services are whitelisted in sendBmwRequest(): 1A (ident), 18 (DTCs),
* 21/22 (read data) — never session control, resets, or writes.
*
* Every frame we send is echoed on the console as
* CANTX,<ts_ms>,<id_hex>,0,<dlc>,<data_hex>
* — the same shape as CANRAW — so a capture holds both halves of every exchange.