Skip to content

Synchronize ESPNOWRadio RX frame handoff#2061

Open
robekl wants to merge 1 commit intomeshcore-dev:devfrom
robekl:fix-espnow-radio-rx-handoff
Open

Synchronize ESPNOWRadio RX frame handoff#2061
robekl wants to merge 1 commit intomeshcore-dev:devfrom
robekl:fix-espnow-radio-rx-handoff

Conversation

@robekl
Copy link
Contributor

@robekl robekl commented Mar 17, 2026

Summary

This fixes the ESP-NOW radio receive handoff so callback context no longer races the main loop over a single shared RX buffer.

Problem

ESPNOWRadio currently receives frames like this:

  • OnDataRecv() copies bytes into one global rx_buf
  • it then publishes last_rx_len
  • recvRaw() later reads last_rx_len, copies from that same buffer, and clears the length

There is no atomic handoff, no critical section, and no second buffer. If a callback lands while recvRaw() is reading, the consumer can observe torn data, lose a packet, or read a length that no longer matches the payload currently in the buffer.

Fix

This PR replaces the single shared RX slot with a small fixed-size mailbox queue:

  • the ESP-NOW callback now enqueues complete received frames into a bounded ring buffer
  • queue publication/consumption is protected by a very short critical section
  • recvRaw() pops one complete frame atomically and only then copies it into the caller buffer
  • explicit overflow logging/drop counting is added instead of silent overwrite

I also added the small no-op compatibility helpers currently expected by the ESP-NOW companion target (getPacketsRecvErrors(), setRxBoostedGainMode(), powerOff()) so the intended build target stays green.

Why this fixes it

The producer and consumer no longer share one mutable rx_buf/last_rx_len pair. A received frame is either fully published in a queue slot or not visible yet, and recvRaw() only consumes stable slots after claiming them under lock.

That removes the callback/main-loop race that previously allowed partial or corrupted reads under load.

Validation

Built successfully:

  • SenseCapIndicator-ESPNow_comp_radio_usb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant