From a1ea02771c7b61929948bb7339e4476818aabfed Mon Sep 17 00:00:00 2001 From: portersky <24420859+portersky@users.noreply.github.com> Date: Sun, 14 Jun 2026 20:29:47 +0200 Subject: [PATCH] refactor: make cel_serial_read non-blocking Drop timeout_ms; read now returns immediately with whatever data is available (0 if none), so callers don't block the rest of their loop waiting on serial I/O. telemetry's poll loop now sleeps interval_ms itself between empty reads via a small sleep_ms helper. --- celrs/serial.c | 5 ++--- celrs/serial.h | 5 +++-- tests/test_serial.c | 2 +- tools/telemetry.c | 22 ++++++++++++++++++++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/celrs/serial.c b/celrs/serial.c index ad19663..da0b8e8 100644 --- a/celrs/serial.c +++ b/celrs/serial.c @@ -37,12 +37,11 @@ void cel_serial_close(cel_serial_port* port) { free(port); } -size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len, int timeout_ms) { +size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len) { (void)port; (void)buf; (void)len; - (void)timeout_ms; - /* TODO: platform-specific read */ + /* TODO: platform-specific non-blocking read */ return 0; } diff --git a/celrs/serial.h b/celrs/serial.h index 2839542..43dea2a 100644 --- a/celrs/serial.h +++ b/celrs/serial.h @@ -15,8 +15,9 @@ cel_serial_port* cel_serial_open(char const* path, int baud_rate); /* Close and free the serial port */ void cel_serial_close(cel_serial_port* port); -/* Read up to len bytes. Returns bytes read, 0 on timeout/error. */ -size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len, int timeout_ms); +/* Read up to len bytes without blocking. + * Returns bytes read immediately available, or 0 if none/error. */ +size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len); /* Write data. Returns bytes written, 0 on error. */ size_t cel_serial_write(cel_serial_port* port, uint8_t const* buf, size_t len); diff --git a/tests/test_serial.c b/tests/test_serial.c index b86a181..84434f4 100644 --- a/tests/test_serial.c +++ b/tests/test_serial.c @@ -34,7 +34,7 @@ void test_read_returns_zero_stub(void) { TEST_ASSERT_NOT_NULL(port); uint8_t buf[16]; /* Stub implementation returns 0 */ - size_t n = cel_serial_read(port, buf, sizeof(buf), 100); + size_t n = cel_serial_read(port, buf, sizeof(buf)); TEST_ASSERT_EQUAL_UINT(0, n); cel_serial_close(port); } diff --git a/tools/telemetry.c b/tools/telemetry.c index 9cadc23..f210696 100644 --- a/tools/telemetry.c +++ b/tools/telemetry.c @@ -5,6 +5,12 @@ #include #include +#ifdef _WIN32 +#include +#else +#include +#endif + /* TX power index to dBm mapping (ELRS standard) */ static int const s_tx_power_dbm[] = { 0, 20, 26, 30, 32, 34, 36, 38, @@ -31,6 +37,15 @@ static int telemetry_parse_link(int16_t* rssi, uint8_t* link_quality, return 0; } +static void sleep_ms(int ms) { +#ifdef _WIN32 + Sleep((DWORD)ms); +#else + struct timespec ts = {.tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000L}; + nanosleep(&ts, NULL); +#endif +} + static void print_usage(char const* prog) { printf("Usage: %s --port [--baudrate ] [interval_ms]\n", prog); printf(" %s --list\n", prog); @@ -106,8 +121,11 @@ int main(int argc, char const* argv[]) { int frames = 0, errors = 0; for (int i = 0; i < 20; i++) { /* read up to 20 telemetry frames */ - size_t n = cel_serial_read(port, buf, sizeof(buf), interval_ms); - if (n == 0) continue; + size_t n = cel_serial_read(port, buf, sizeof(buf)); + if (n == 0) { + sleep_ms(interval_ms); + continue; + } cel_crsf_frame frame; if (cel_crsf_frame_parse(&frame, buf, n) != 0) {