Simvik

Parse a modem signal report

Validate a +CSQ reply and convert it to dBm. A medium embedded C problem (UART), run and graded in the browser.

medium · UART · uart · parsing · at-commands

A cellular modem answers AT+CSQ with a line such as +CSQ: 23,99. The first number is the signal strength index rssi; the second (bit error rate) does not matter here.

Return the signal strength in dBm: -113 + 2 * rssi for rssi from 0 to 31. Return 0 if the strength is unknown (rssi is 99) or the line is malformed. A well-formed line is exactly +CSQ: , then one or two digits, then a comma.

Starter code

int csq_to_dbm(const char *line) {
  int rssi = 0;
  for (const char *p = line + 6; *p >= '0' && *p <= '9'; p++)
    rssi = rssi * 10 + (*p - '0');
  return -113 + 2 * rssi;
}

Example tests

Normal reports

printf("%d %d\n", csq_to_dbm("+CSQ: 23,99"), csq_to_dbm("+CSQ: 0,0"));

Expected output:

-67 -113

Signal not known

printf("%d\n", csq_to_dbm("+CSQ: 99,99"));

Expected output:

0

Learn it first

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

More UART problems