Skip to content

feat(switch2_pro): pair a new console without forgetting the current one - #915

Merged
finger563 merged 2 commits into
mainfrom
feat/switch2-pro-keep-bond
Nov 20, 2026
Merged

finger563 merged 2 commits into
mainfrom
feat/switch2-pro-keep-bond

Conversation

@finger563

Copy link
Copy Markdown
Contributor

What

Adds a non-destructive pairing mode to Switch2Pro, matching how a real controller's sync button behaves:

  • start_pairing(): advertise for discovery (fresh pairing) but keep the current bond aside, both in NVS and in NimBLE's security store. Re-enables the automatic re-advertise a prior stop_advertising() suppressed; does not drop a live link (use disconnect() for that), and discovery starts once nothing is connected.
  • A console that completes the 0x15 handshake replaces the bond (FINALISE persists it as before). The previous console's NimBLE key record is dropped, unless it was the same console pairing again.
  • cancel_pairing(): end a window no console completed. It restores the kept bond (reconnect mode, LTK re-injected) and advertises for that console again, unless advertising is suppressed or a console is connected. A half-done handshake on a live link is abandoned, so its FINALISE is rejected.
  • If the previous console reconnects with its stored key during the window (the link encrypts without running the handshake), the window closes on its bond.
  • is_pairing() reports an open window. clear_bond() also forgets a kept bond (and its key record).
  • README: a short "Pairing with another console" section contrasting start_pairing() and clear_bond().

Why

clear_bond() was the only way into pairing mode, so entering it erased the stored console before any new console had paired. A pairing window that timed out without a new console left the controller bonded to nothing, and the user had to re-pair the console they already had. Since start_pairing() never touches NVS, a reboot during or after the window also comes back to the previous console.

Test

  • Builds the switch2_pro example on esp32s3 (IDF 6.1).
  • Builds in an application that uses start_pairing() / cancel_pairing() for its pairing window. Not yet tested against a console.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

clear_bond() was the only way into pairing mode, so entering it erased the
stored console (NVS + NimBLE key record) before any new console had paired:
a pairing window that ended without one left the controller bonded to nothing.

start_pairing() behaves like a real controller's sync button: it advertises
for discovery but keeps the current bond aside, in NVS and in NimBLE's
security store. A console that completes the 0x15 handshake replaces it (and
the previous console's key record is dropped unless it is the same console
pairing again); cancel_pairing() restores it (reconnect mode, LTK re-injected,
reconnect advertising unless stop_advertising() suppressed it), and so does a
reboot, since NVS is never touched. If the previous console reconnects with
its stored key during the window, the window closes on its bond. is_pairing()
reports an open window; clear_bond() also forgets a kept bond.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@github-actions

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

- The 0x15 handshake now fills its own scratch (console identity + LTK);
  only an accepted FINALISE commits it into the bond. Each step checks and
  advances pairing_stage_ under bond_mutex_ in one hold, and FINALISE
  checks and commits (bond, paired, reconnect mode, window close, kept
  bond) in one hold. A repeat start_pairing() no longer zeroes a live
  handshake's LTK, and cancel_pairing() can no longer be overwritten by
  an in-flight step or mix two consoles' bonds in NVS.
- cancel_pairing() rejects every further pairing step on the current
  link (until the console reconnects), so a still-connected console
  cannot restart the handshake over the restored bond.
- clear_bond() / FINALISE stop advertising before deleting a NimBLE key
  record: ble_gap_unpair() returns BLE_HS_EBUSY while advertising, so
  the record was silently kept.
- adv_mutex_ serialises choosing + starting an advertisement against the
  bond transitions, so a wake-timer tick holding the old bond cannot put
  its advertisement back on the air after start_pairing().
- start_pairing() rolls a window it opened back if advertising fails.
- FINALISE drops the previous console's key record only once NVS holds
  the new bond (save_bond() now reports it); otherwise a reboot returns
  to the previous console, which must still be able to encrypt.
- The "previous console reconnected" check matches the kept console's
  address and only the link's first encryption.
- Bond struct + std::optional kept bond, reset_bond_locked() and
  unpair_peer() helpers replace the copied fields and reset blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563
finger563 merged commit 9814377 into main Nov 20, 2026
185 of 186 checks passed
@finger563
finger563 deleted the feat/switch2-pro-keep-bond branch November 20, 2026 18:51
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