Reliable delivery over a lossy link
Stop-and-wait with a sequence bit, acknowledgements, a retransmit timer and duplicate detection. A hard embedded C problem (Protocols, framing and…
hard · Protocols, framing and checksums · protocol · arq · retransmission · state-machine
A radio link loses frames and acknowledgements. Stop-and-wait ARQ makes it reliable: send one frame, wait for its ACK, and send it again if the ACK does not come. A one-bit sequence number lets the receiver spot a frame it has already seen, which happens when the ACK, not the frame, was lost.
Sender:
arq_send(a, data, now): if a frame is in flight, returnfalse. Otherwise senddatawith the currentsequsinglink_send, record it, and returntrue.arq_on_ack(a, ack_seq): an ACK carries the sequence bit of the frame it acknowledges. If a frame is in flight andack_seqmatches it, the frame is done: stop waiting and flipseqfor the next frame. Ignore any other ACK; it is a late duplicate.arq_tick(a, now): if a frame is in flight andARQ_TIMEOUTor more ticks have passed since it was last sent: send it again (sameseq) and returnARQ_RETRIED, unless it has already been sentARQ_MAX_TRIEStimes, in which case abandon it (stop waiting, keepseq) and returnARQ_GAVE_UP. Otherwise returnARQ_IDLE.
Receiver:
arq_receive(r, seq, &ack): always acknowledge with the frame's ownseq. Returntrueif it is the frame the receiver expected (then expect the other bit next), orfalseif it is a duplicate to throw away.
Starter code
bool arq_send(arq_tx_t *a, uint8_t data, uint32_t now) {
a->data = data;
a->waiting = true;
link_send(a->seq, data);
return true;
}
void arq_on_ack(arq_tx_t *a, uint8_t ack_seq) {
a->waiting = false;
a->seq ^= 1;
}
int arq_tick(arq_tx_t *a, uint32_t now) {
return ARQ_IDLE;
}
bool arq_receive(arq_rx_t *r, uint8_t seq, uint8_t *ack) {
*ack = seq;
return true;
}Example tests
A lost frame is sent again
arq_send(&tx, 'A', 0);
deliver(false);
arq_send(&tx, 'B', 1);
lose();
tick(2, 6);
deliver(false);
printf("\n");Expected output:
tx 0:A got A ack 0 tx 1:B (lost) tx 1:B @t6 got B ack 1A lost ACK makes a duplicate
arq_send(&tx, 'A', 0);
deliver(true);
tick(1, 5);
deliver(false);
arq_send(&tx, 'B', 6);
deliver(false);
printf("\n");Expected output:
tx 0:A got A (ack lost) tx 0:A @t5 dup ack 0 tx 1:B got B ack 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.
- Byte-stream frame parser · Reassemble frames from a byte stream one byte at a time.
- 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.
- Solar inverter: decode a status frame · Check a frame from the inverter and decode its power, voltage and temperature.