Reserve USB KISS framing before normal parsers consume it

This commit is contained in:
mikecarper committed 2026-10-26 17:49:20 -07:00
1 parent ff51126508
commit 1aef6de288
20 files changed
+624 -10

No files matched your search

+4
View File
@@ -307,6 +307,10 @@ size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {
// host must not keep the radio/watchdog loop trapped in this parser.
int pending = _serial->available();
while (pending-- > 0) {
// Input can arrive after the role's initial protocol-owner poll. Leave a
// reserved first byte for that owner without consuming its framing.
if (_state == RECV_STATE_IDLE && _idle_receive_check != nullptr
&& !_idle_receive_check(_serial->peek())) return 0;
int c = _serial->read();
if (c < 0) break;
_last_rx_byte_ms = millis();
+7 -1
View File
@@ -10,6 +10,9 @@ public:
// Optional protocol-owner gate, checked before a completed frame is exposed
// or recorded as client activity. Other transports retain their old behavior.
typedef bool (*ReceiveFrameCheck)(uint32_t completed_at);
// Before reading an idle byte, another protocol may reserve it in-place.
// Length-prefixed frame bodies bypass this gate, including opaque 0xC0.
typedef bool (*IdleReceiveCheck)(int next_byte);
private:
static constexpr uint8_t TX_QUEUE_SIZE = 4;
@@ -46,6 +49,7 @@ private:
const char* _secondaryControlSequence;
ConnectedCheck _conn_check;
ReceiveFrameCheck _receive_frame_check;
IdleReceiveCheck _idle_receive_check;
uint8_t rx_buf[MAX_FRAME_SIZE];
TxFrame _tx_queue[TX_QUEUE_SIZE];
@@ -71,7 +75,7 @@ public:
_has_received_frame(false),
_serial(nullptr), _controlSequence(nullptr),
_secondaryControlSequence(nullptr), _conn_check(nullptr),
_receive_frame_check(nullptr) {}
_receive_frame_check(nullptr), _idle_receive_check(nullptr) {}
void begin(Stream& serial, const char* controlSequence = nullptr,
const char* secondaryControlSequence = nullptr) {
@@ -105,6 +109,8 @@ public:
// Without it isConnected() assumes true, as a plain UART has no way of knowing.
void setConnectedCheck(ConnectedCheck fn) { _conn_check = fn; }
void setReceiveFrameCheck(ReceiveFrameCheck fn) { _receive_frame_check = fn; }
void setIdleReceiveCheck(IdleReceiveCheck fn) { _idle_receive_check = fn; }
bool isReceivingFrame() const { return _state != 0; }
// Clear all protocol state owned by one host session while retaining the
// monotonic completed-frame counter used by startup handoff arbitration.
+4
View File
@@ -522,6 +522,10 @@ bool serviceUsb() {
}
bool usbActive() { return usb_session; }
bool usbInputReserved(int next_byte) {
return usb_session || (settings.selection().enabled
&& settings.selection().transport == Transport::Usb && next_byte == Codec::Fend);
}
bool radioActive() { return current(); }
void resetUsbSession() { if (usb_session) closeSession(); }
+4
View File
@@ -16,6 +16,9 @@ bool handleCommand(const char* command, char* reply, size_t capacity, bool local
void loop();
bool serviceUsb();
bool usbActive();
// Normal USB consumers must leave this byte for serviceUsb(), even when it
// arrives after their loop-top owner poll. Never call inside another frame.
bool usbInputReserved(int next_byte);
bool radioActive();
void resetUsbSession();
void receive(const uint8_t* bytes, size_t length, uint8_t profile,
@@ -29,6 +32,7 @@ inline bool handleCommand(const char*, char*, size_t, bool) { return false; }
inline void loop() {}
inline bool serviceUsb() { return false; }
inline bool usbActive() { return false; }
inline bool usbInputReserved(int) { return false; }
inline bool radioActive() { return false; }
inline void resetUsbSession() {}
inline void receive(const uint8_t*, size_t, uint8_t, uint32_t, float, float) {}