Simvik

Spot OK and ERROR in a modem stream

Feed received characters one at a time and report complete OK or ERROR lines. An easy embedded C problem (UART), run and graded in the browser.

easy · UART · uart · parsing · at-commands

GSM, Wi-Fi and Bluetooth modules answer AT commands with lines ending in \r\n, and finish with a line that is exactly OK or ERROR. Characters arrive one at a time from the UART.

at_feed(a, c) takes the next character. At the end of a line (\n) return 1 if the line was exactly OK, 2 if it was exactly ERROR, otherwise 0; then start a new line. Ignore \r. Return 0 for every other character.

Keep at most 15 characters of a line. A longer line can never be OK or ERROR, but it must not overflow the buffer.

Starter code

int at_feed(at_t *a, char c) {
  return 0;
}

Example tests

A command echo then OK

at_t a = { 0 };
feed(&a, "AT\r\nOK\r\n");

Expected output:

OK |

A report, a blank line, then ERROR

at_t a = { 0 };
feed(&a, "+CSQ: 20,0\r\n\r\nERROR\r\n");

Expected output:

ERROR |

Learn it first

  • UART · Baud rate, framing, errors, flow control and DMA reception.

More UART problems