From 03266e709e8effe2250adda39824a506cd501a9b Mon Sep 17 00:00:00 2001 From: Strycher Date: Fri, 25 Sep 2026 00:22:12 -0400 Subject: [PATCH] fix(#710): flserial never overwrites incoming serial bytes flserial's reader thread copied every byte into an 8 KB ring that overwrote the oldest bytes when full, and told Dart about data only once the port went idle. The RK4 sends its 350-contact list (about 52 KB) in about 0.17 s, so most of it was overwritten before the app read it: the app saw 106-154 contacts and #660's contacts were lost. The thread now reads only as much as the ring has room for. When the ring is full it leaves the bytes in the OS driver, and USB flow control holds the radio, so nothing is dropped. The ring is 256 KB (the size the header already declared as MAX_PORT_FIFO_LEN, and a power of two as the ring's mask needs); the per-read chunk stays 8 KB on the thread stack. Dart is called back during a burst (4 KB waiting or 10 ms) and whenever the ring fills, not only when the port goes idle. Same change in the macOS copy of the source (macos/Classes), which was identical. (std::min) is parenthesised because the Windows headers define a min macro. Verified on the owner's RK4 (read-only harness driving the DLL): new DLL 350/350 in every mode, including a reader 2 s late, zero bytes skipped; the old DLL, as the control, got 345 app-like and 54 (8191 bytes, the old ring's capacity) when 300 ms or 2 s late. Epic #702. Co-Authored-By: Claude Opus 5.5 --- packages/flserial/macos/Classes/flserial.cpp | 61 ++++++++++++++++---- packages/flserial/src/flserial.cpp | 61 ++++++++++++++++---- 2 files changed, 100 insertions(+), 22 deletions(-) diff --git a/packages/flserial/macos/Classes/flserial.cpp b/packages/flserial/macos/Classes/flserial.cpp index ec30afc7..7b3f53f4 100644 --- a/packages/flserial/macos/Classes/flserial.cpp +++ b/packages/flserial/macos/Classes/flserial.cpp @@ -3,8 +3,19 @@ #include "native_libs/include/tinycthread.h" #include "native_libs/include/ringbuffer.h" #include - -#define FL_SERIAL_BUFF_LEN (8 * 1024) +#include +#include +#include + +// Offband (#702/#710): the 8 KB ring overwrote the oldest bytes whenever the +// radio sent faster than Dart drained it, so a 52 KB contact sync arrived +// mostly lost. The ring is now 256 KB (a power of two, as the ring's mask +// needs), the thread never reads more than the ring has room for, and Dart is +// told about data during a burst rather than only when the port goes idle. +#define FL_SERIAL_BUFF_LEN (256 * 1024) +#define FL_SERIAL_READ_CHUNK (8 * 1024) +#define FL_NOTIFY_BYTES (4 * 1024) +#define FL_NOTIFY_INTERVAL std::chrono::milliseconds(10) typedef struct _flserial_ { @@ -28,28 +39,56 @@ int SerialThread(void *aArg) { FlSerial *serial = (FlSerial *)aArg; ring_buffer_size_t res = 0; - uint8_t buff[FL_SERIAL_BUFF_LEN]; + uint8_t buff[FL_SERIAL_READ_CHUNK]; ring_buffer_size_t len = sizeof(buff); ring_buffer_size_t total = 0; - int isEmpty = 0; + auto lastNotify = std::chrono::steady_clock::now(); while (!serial->breakThread) { try { - res = (int)serial->serialport->read((uint8_t *)buff, (size_t)len); + // Read only what the ring can hold. When it is full, leave the + // bytes in the OS driver: USB flow control holds the sender, and + // nothing is overwritten. + mtx_lock(&serial->inFifo_mutex); + ring_buffer_size_t room = RING_BUFFER_MASK((&serial->cinfifo)) - + ring_buffer_num_items(&serial->cinfifo); + mtx_unlock(&serial->inFifo_mutex); + + res = 0; + if (room > 0) + { + res = (int)serial->serialport->read( + (uint8_t *)buff, (size_t)(std::min)(len, room)); + } if (res > 0) { mtx_lock(&serial->inFifo_mutex); ring_buffer_queue_arr(&serial->cinfifo, (const char *)buff, res); mtx_unlock(&serial->inFifo_mutex); total += res; - if(!serial->serialport->available()){ - serial->callback(0, total); - total = 0; - } else { - continue; - } + } + + bool idle = !serial->serialport->available(); + auto now = std::chrono::steady_clock::now(); + if (total > 0 && + (idle || room == 0 || total >= FL_NOTIFY_BYTES || + now - lastNotify >= FL_NOTIFY_INTERVAL)) + { + serial->callback(0, (unsigned int)total); + total = 0; + lastNotify = now; + } + + if (room == 0) + { + // Give Dart time to drain before trying again. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + else if (res > 0 && !idle) + { + continue; } mtx_lock(&serial->outFifo_mutex); diff --git a/packages/flserial/src/flserial.cpp b/packages/flserial/src/flserial.cpp index ec30afc7..7b3f53f4 100644 --- a/packages/flserial/src/flserial.cpp +++ b/packages/flserial/src/flserial.cpp @@ -3,8 +3,19 @@ #include "native_libs/include/tinycthread.h" #include "native_libs/include/ringbuffer.h" #include - -#define FL_SERIAL_BUFF_LEN (8 * 1024) +#include +#include +#include + +// Offband (#702/#710): the 8 KB ring overwrote the oldest bytes whenever the +// radio sent faster than Dart drained it, so a 52 KB contact sync arrived +// mostly lost. The ring is now 256 KB (a power of two, as the ring's mask +// needs), the thread never reads more than the ring has room for, and Dart is +// told about data during a burst rather than only when the port goes idle. +#define FL_SERIAL_BUFF_LEN (256 * 1024) +#define FL_SERIAL_READ_CHUNK (8 * 1024) +#define FL_NOTIFY_BYTES (4 * 1024) +#define FL_NOTIFY_INTERVAL std::chrono::milliseconds(10) typedef struct _flserial_ { @@ -28,28 +39,56 @@ int SerialThread(void *aArg) { FlSerial *serial = (FlSerial *)aArg; ring_buffer_size_t res = 0; - uint8_t buff[FL_SERIAL_BUFF_LEN]; + uint8_t buff[FL_SERIAL_READ_CHUNK]; ring_buffer_size_t len = sizeof(buff); ring_buffer_size_t total = 0; - int isEmpty = 0; + auto lastNotify = std::chrono::steady_clock::now(); while (!serial->breakThread) { try { - res = (int)serial->serialport->read((uint8_t *)buff, (size_t)len); + // Read only what the ring can hold. When it is full, leave the + // bytes in the OS driver: USB flow control holds the sender, and + // nothing is overwritten. + mtx_lock(&serial->inFifo_mutex); + ring_buffer_size_t room = RING_BUFFER_MASK((&serial->cinfifo)) - + ring_buffer_num_items(&serial->cinfifo); + mtx_unlock(&serial->inFifo_mutex); + + res = 0; + if (room > 0) + { + res = (int)serial->serialport->read( + (uint8_t *)buff, (size_t)(std::min)(len, room)); + } if (res > 0) { mtx_lock(&serial->inFifo_mutex); ring_buffer_queue_arr(&serial->cinfifo, (const char *)buff, res); mtx_unlock(&serial->inFifo_mutex); total += res; - if(!serial->serialport->available()){ - serial->callback(0, total); - total = 0; - } else { - continue; - } + } + + bool idle = !serial->serialport->available(); + auto now = std::chrono::steady_clock::now(); + if (total > 0 && + (idle || room == 0 || total >= FL_NOTIFY_BYTES || + now - lastNotify >= FL_NOTIFY_INTERVAL)) + { + serial->callback(0, (unsigned int)total); + total = 0; + lastNotify = now; + } + + if (room == 0) + { + // Give Dart time to drain before trying again. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + else if (res > 0 && !idle) + { + continue; } mtx_lock(&serial->outFifo_mutex);