Byte-stream frame parser
Reassemble frames from a byte stream one byte at a time. A hard embedded C problem (Protocols, framing and checksums), run and graded in the browser.
hard · Protocols, framing and checksums · state-machine · framing · uart
Bytes arrive one at a time from a UART interrupt, so the receiver must remember where it is in a frame. Frames are 0x7E, a length, the payload, and a checksum equal to the XOR of the length and payload bytes. A zero-initialised parser_t is waiting for a start byte.
Process one received byte and return true only when it completes a frame with a correct checksum; the payload and its length are then in p->payload and p->len.
WAIT_START: ignore everything exceptFRAME_START.WAIT_LEN: a length aboveMAX_PAYLOADis invalid; go back toWAIT_START. A length of 0 goes straight toWAIT_CHECKSUM.READ_PAYLOAD: store bytes untillenhave arrived.WAIT_CHECKSUM: compare, then return toWAIT_STARTwhatever the result.
Starter code
bool parser_feed(parser_t *p, uint8_t byte) {
switch (p->state) {
case WAIT_START:
break;
case WAIT_LEN:
break;
case READ_PAYLOAD:
break;
case WAIT_CHECKSUM:
break;
}
return false;
}Example tests
One complete frame
parser_t p = { 0 };
const uint8_t rx[] = { 0x7E, 0x02, 0xAA, 0xBB, 0x13 };
printf("%d\n", feed_all(&p, rx, sizeof rx));Expected output:
frame 2: AA BB
1Noise before the start byte
parser_t p = { 0 };
const uint8_t rx[] = { 0x00, 0xFF, 0x7E, 0x01, 0x55, 0x54 };
printf("%d\n", feed_all(&p, rx, sizeof rx));Expected output:
frame 1: 55
1Learn it first
- Protocols, framing and checksums · Checksums, CRCs, byte order, framing and parsing a byte stream.
More Protocols, framing and checksums problems
- XOR checksum · Compute the XOR of every byte in a buffer.
- Two's-complement checksum · Compute the checksum byte of an Intel HEX record.
- CRC-8 · Implement a bitwise CRC-8 with polynomial 0x07.
- CRC-16 for Modbus · Implement the reflected CRC-16 used by Modbus RTU.
- Read a big-endian word · Assemble a 32-bit value from four bytes of a packet.
- I2C address byte · Build the first byte of an I2C transfer.
- BCD conversion · Convert between binary-coded decimal and binary.
- Build a UART frame · Assemble the bits a UART sends for one byte with parity.
- Build a framed message · Wrap a payload with a start byte, a length and a checksum.
- SLIP byte stuffing · Escape reserved bytes so a frame delimiter is unambiguous.
- Variable-length integer · Encode a 32-bit value in as few bytes as it needs.
- Validate an NMEA sentence · Check the checksum of a GPS sentence.
- COBS encoding · Remove every zero byte from a packet with one byte of overhead.
- Fix: the bootloader hangs on large chunks · A loop counter is too narrow for the length it counts to.
- Fix: frame lengths come out 256 times too big · A big-endian length field is decoded as little-endian.
- Reliable delivery over a lossy link · Stop-and-wait with a sequence bit, acknowledgements, a retransmit timer and duplicate detection.
- Solar inverter: decode a status frame · Check a frame from the inverter and decode its power, voltage and temperature.