Simvik

Report trouble codes by status mask

Build the responses to ReadDTCInformation's count and list sub-functions. A medium embedded C problem (UDS: data identifiers and the fault memory), run and…

medium · UDS: data identifiers and the fault memory · uds · dtc · automotive · diagnostics · bits

Write report_dtcs(dtcs, n, req, len, resp), the handler for two sub-functions of UDS service 0x19 ReadDTCInformation. Write the response to resp (it is large enough) and return its length.

RequestResponse
19 01 <mask>59 01 <availability mask> 01 <count high> <count low>
19 02 <mask>59 02 <availability mask>, then for each matching DTC its three code bytes (high first) and its status byte

A DTC matches when status & mask & AVAILABILITY_MASK is not zero. DTCs are reported in the order of the array. The status byte is reported with only the supported bits (status & AVAILABILITY_MASK). No matching DTC is still a positive response: a count of 0, or a list with nothing after the availability mask.

Negative responses: fewer than 2 bytes 7F 19 13; a sub-function other than 01 and 02 7F 19 12; then a length other than 3 7F 19 13.

Starter code

uint16_t report_dtcs(const dtc_t *dtcs, size_t n, const uint8_t *req, uint16_t len, uint8_t *resp) {
  (void)len;
  uint16_t at = 0;
  resp[at++] = 0x59;
  resp[at++] = req[1];
  resp[at++] = AVAILABILITY_MASK;
  for (size_t i = 0; i < n; i++) {
    if (dtcs[i].status == req[2]) {
      resp[at++] = (uint8_t)(dtcs[i].code >> 16);
      resp[at++] = (uint8_t)(dtcs[i].code >> 8);
      resp[at++] = (uint8_t)dtcs[i].code;
      resp[at++] = dtcs[i].status;
    }
  }
  return at;
}

Example tests

Confirmed trouble codes

const uint8_t req[] = { 0x19, 0x02, 0x08 };
ask(req, 3);

Expected output:

59 02 2F 9A 01 16 09 C1 00 87 28

How many are failing or pending?

const uint8_t req[] = { 0x19, 0x01, 0x05 };
ask(req, 3);

Expected output:

59 01 2F 01 00 02

Learn it first

More UDS: data identifiers and the fault memory problems