Skip to content

Protocol constant cleanup + NimBLE callback race fix (stacked on #107)#108

Merged
jonasniesner merged 2 commits into
OpenDisplay:mainfrom
davelee98:debug/v2.2-audit
Jul 19, 2026
Merged

Protocol constant cleanup + NimBLE callback race fix (stacked on #107)#108
jonasniesner merged 2 commits into
OpenDisplay:mainfrom
davelee98:debug/v2.2-audit

Conversation

@davelee98

Copy link
Copy Markdown
Contributor

Draft — stacked on #107. This branch builds directly on update/vendor (PR #107, "Vendor canonical protocol header"). Until #107 merges, the diff here also shows its vendoring commit; that overlap disappears automatically once #107 lands, leaving only the two commits below. Please review/merge #107 first.

What this adds on top of #107

1. fix(ble): defer NimBLE onConnect/onDisconnect work to loop() (0e7bf79)

Moves connect/disconnect handling out of the NimBLE callback context into loop() to resolve races surfaced in the NimBLE migration audit (#1–3, 6).

2. Replace protocol magic numbers with canonical named constants (7c6df0b)

Follow-up to the header vendoring: sweeps the remaining hand-coded protocol literals across the BLE command handlers and uses the constants from the vendored opendisplay_protocol.h. Value-for-value — no wire-behavior change.

  • encryption.cpp — auth-status frames → RESP_ACK/RESP_AUTHENTICATE + AUTH_STATUS_*; CCM envelope sizes → BLE_CMD_HEADER_SIZE/ENCRYPTION_NONCE_SIZE/ENCRYPTION_TAG_SIZE (handshake nonces left as-is).
  • communication.cpp — handler response codes → RESP_* (incl. RESP_AUTH_REQUIRED); config limits → CONFIG_CHUNK_SIZE* / MAX_CONFIG_CHUNKS / MAX_RESPONSE_DATA_SIZE; envelope sizes.
  • device_control.cpp / buzzer_control.cpp — LED, deep-sleep, buzzer ack/nack frames → RESP_*.
  • display_service.cpp — direct-write/refresh frames → RESP_DIRECT_WRITE_*; pipe-start errors → OD_ERR_PIPE_START_*; deleted the local ERR_* shadow copies of OD_ERR_PARTIAL_* in favor of the canonical names.

Documented header gaps (TODO(protocol))

Left raw with in-code TODO(protocol) markers because the canonical header has no constant for them yet: pipe/partial opcode-echo bytes (0x76/0x80/0x81/0x82), the pipe data-phase error codes, and the START response-flags bit. Each TODO names the constant to add upstream in opendisplay-protocol.

Verification

  • All three CI envs build clean: nrf52840custom, esp32-s3-N16R8, esp32-c3-N16.
  • Vendored header still byte-in-sync with canonical (sync_protocol_header.py --check).

🤖 Generated with Claude Code

davelee98 and others added 2 commits July 18, 2026 09:44
…dit #1-3,6)

The Bluedroid->NimBLE migration left heavyweight, state-mutating work running
inline inside the ESP32 server callbacks on the NimBLE host task, racing the
Arduino loop() task. Make the callbacks flag-only and service the flags from
loop(), mirroring the existing bleRestartAdvertisingPending pattern.

- onDisconnect: EPD session teardown (bbepSleep/delay/SPI.end + rail cut),
  partial + pipe cleanup, and resetPipeWriteState() ran on the host task while
  loop() streamed SPI via bbepWriteData and processed pipe frames. The only
  guard (epdRefreshInProgress) covered the refresh phase, not streaming.
  Now sets bleDisconnectCleanupPending; serviced by serviceBleDisconnectCleanup()
  in loop() (preserving the epdRefreshInProgress deferral), run before the
  advertising restart. (#1 critical, #2 high, OpenDisplay#6 medium)
- onConnect: updatemsdata() polled I2C and mutated the shared advertisement
  vector that loop() also drives on its 60s cadence -> heap corruption. Now
  sets msdUpdatePending; loop() calls updatemsdata(). (#3 high)
- updatemsdata(): drop the connected-branch that rebuilt *advertisementData
  but never pushed it via setAdvertisementData() -- dead work, no behavior
  change.

All ESP32-guarded; nRF/Bluefruit path untouched. Builds clean on
esp32-s3-N16R8, esp32-c3-N16, nrf52840custom.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Follow-up to the header vendoring (3f09f6d): sweep the remaining
hand-coded protocol literals across the BLE command handlers and use
the constants from the vendored opendisplay_protocol.h. Value-for-value,
no wire-behavior change; all CI envs build clean.

- encryption.cpp: auth-status frames -> RESP_ACK/RESP_AUTHENTICATE +
  AUTH_STATUS_*; CCM envelope sizes -> BLE_CMD_HEADER_SIZE/
  ENCRYPTION_NONCE_SIZE/ENCRYPTION_TAG_SIZE (handshake nonces left as-is).
- communication.cpp: handler response codes -> RESP_* (incl.
  RESP_AUTH_REQUIRED); config limits -> CONFIG_CHUNK_SIZE*/
  MAX_CONFIG_CHUNKS/MAX_RESPONSE_DATA_SIZE; envelope sizes.
- device_control.cpp / buzzer_control.cpp: LED, deep-sleep, and buzzer
  ack/nack frames -> RESP_*.
- display_service.cpp: direct-write/refresh frames -> RESP_DIRECT_WRITE_*;
  pipe-start errors -> OD_ERR_PIPE_START_*; deleted the local ERR_*
  shadow copies of OD_ERR_PARTIAL_* in favor of the canonical names.

Pipe/partial-write opcode-echo bytes (0x76/0x80/0x81/0x82), the pipe
data-phase error codes, and the START response-flags bit stay raw with
TODO(protocol) markers: the canonical header defines no RESP_* mirror or
data-phase error namespace for them yet.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@davelee98
davelee98 marked this pull request as ready for review July 18, 2026 13:46
@davelee98
davelee98 requested a review from jonasniesner as a code owner July 18, 2026 13:46
@jonasniesner
jonasniesner merged commit e1eda28 into OpenDisplay:main Jul 19, 2026
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.

2 participants