Is this a valid single frame?
Return a single frame's message length, or reject a frame that is malformed. An easy embedded C problem (ISO-TP: frames and segmentation), run and graded in…
easy · ISO-TP: frames and segmentation · isotp · can · automotive · validation
Most diagnostic requests fit in one ISO-TP single frame: the first byte's high nibble is 0 and its low nibble is the message length, 1 to 7. The message follows, and the rest of the frame is padding.
A receiver must not trust that byte. Write isotp_sf_len(d, dlc), where d holds the dlc bytes of a CAN frame that arrived (dlc is 0 to 8). Return the message length, or -1 when the frame is not a valid single frame:
- the high nibble of
d[0]is not 0 (another frame type), - the length is 0, or more than 7,
- the length is more than the bytes that follow
d[0]in this frame, - the frame is empty.
Starter code
int isotp_sf_len(const uint8_t *d, uint8_t dlc) {
(void)dlc;
return d[0] & 0x0F;
}Example tests
A padded request and a full one
const uint8_t a[8] = { 0x02, 0x3E, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
const uint8_t b[8] = { 0x07, 1, 2, 3, 4, 5, 6, 7 };
show(a, 8);
show(b, 8);Expected output:
02 3E 00 CC CC CC CC CC -> 2
07 01 02 03 04 05 06 07 -> 7Other frame types
const uint8_t ff[8] = { 0x10, 0x14, 0x62, 0xF1, 0x90, 0x54, 0x45, 0x31 };
const uint8_t fc[8] = { 0x30, 0x00, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
show(ff, 8);
show(fc, 8);Expected output:
10 14 62 F1 90 54 45 31 -> -1
30 00 00 CC CC CC CC CC -> -1Learn it first
- ISO-TP: frames and segmentation · Single, first, consecutive and flow control frames, addressing, padding, and CAN FD.
More ISO-TP: frames and segmentation problems
- Split a message into ISO-TP frames · Produce the single frame, or the first frame and consecutive frames, that carry a message.