Simvik

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, return false. Otherwise send data with the current seq using link_send, record it, and return true.
  • arq_on_ack(a, ack_seq): an ACK carries the sequence bit of the frame it acknowledges. If a frame is in flight and ack_seq matches it, the frame is done: stop waiting and flip seq for the next frame. Ignore any other ACK; it is a late duplicate.
  • arq_tick(a, now): if a frame is in flight and ARQ_TIMEOUT or more ticks have passed since it was last sent: send it again (same seq) and return ARQ_RETRIED, unless it has already been sent ARQ_MAX_TRIES times, in which case abandon it (stop waiting, keep seq) and return ARQ_GAVE_UP. Otherwise return ARQ_IDLE.

Receiver:

  • arq_receive(r, seq, &ack): always acknowledge with the frame's own seq. Return true if it is the frame the receiver expected (then expect the other bit next), or false if 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 1

A 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 1

Learn it first

More Protocols, framing and checksums problems