From 493826bef5bfca71114c79081c5cc005f30e92f8 Mon Sep 17 00:00:00 2001 From: Scott Penrose Date: Wed, 10 Jun 2026 11:26:13 +1000 Subject: [PATCH] fully drain sync messages --- src/MeshCoreCompanion.cpp | 14 ++++++++++++-- src/MeshCoreCompanion.h | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/MeshCoreCompanion.cpp b/src/MeshCoreCompanion.cpp index cf97faa..f4d6aa5 100644 --- a/src/MeshCoreCompanion.cpp +++ b/src/MeshCoreCompanion.cpp @@ -32,6 +32,7 @@ void MeshCoreCompanion::loop() { while (mc_rx_poll(&_rx, _scratch, sizeof(_scratch), &olen)) { mc_event_t ev; if (mc_parse(_scratch, olen, &ev)) dispatch(ev); + else if (_onUnparsed) _onUnparsed(_scratch, olen); /* frame we couldn't decode */ } } @@ -121,7 +122,11 @@ void MeshCoreCompanion::syncNextMessage() { } void MeshCoreCompanion::drainMessages() { - if (!_draining) { _draining = true; syncNextMessage(); } + // Always (re)issue a sync — recovers a wedged drain (a lost reply leaving + // _draining stuck true), not only when idle. Safe to call periodically: + // the radio answers each sync with the next queued message or NoMoreMessages. + _draining = true; + syncNextMessage(); } void MeshCoreCompanion::getStats(uint8_t statsType) { @@ -217,7 +222,12 @@ void MeshCoreCompanion::dispatch(const mc_event_t &ev) { if (_onBinaryResp) _onBinaryResp(ev.u.binary_resp); break; case MC_PUSH_MSG_WAITING: - if (_autoSync && !_draining) { _draining = true; syncNextMessage(); } + // Always (re)start the drain on a MsgWaiting push. If a prior sync + // reply was lost (e.g. the host stalled and its UART RX overflowed), + // _draining can be left stuck true; gating on !_draining would then + // ignore every later push and silently stop pulling messages. + // Re-kicking unconditionally self-heals that. + if (_autoSync) { _draining = true; syncNextMessage(); } break; case MC_RESP_CHANNEL_MSG_RECV: case MC_RESP_CHANNEL_MSG_RECV_V3: diff --git a/src/MeshCoreCompanion.h b/src/MeshCoreCompanion.h index 73b3b36..c4ee87f 100644 --- a/src/MeshCoreCompanion.h +++ b/src/MeshCoreCompanion.h @@ -105,6 +105,10 @@ public: void onContactsDone(ContactsDoneCb cb){ _onContactsDone = cb; } void onBinaryResponse(BinaryRespCb cb){ _onBinaryResp = cb; } void onEvent(EventCb cb) { _onEvent = cb; } /* every parsed frame */ + /* Raw frames the parser did NOT recognise (payload incl. code byte). For + * diagnosing firmware/protocol mismatches — normally unused. */ + using RawCb = std::function; + void onUnparsedFrame(RawCb cb) { _onUnparsed = cb; } private: void sendPayload(const uint8_t *payload, size_t len); @@ -134,6 +138,7 @@ private: ContactsDoneCb _onContactsDone; BinaryRespCb _onBinaryResp; EventCb _onEvent; + RawCb _onUnparsed; }; #endif /* MESHCORE_COMPANION_HPP */