Repository navigation
feat(switch2_pro): pair a new console without forgetting the current one - #915
Merged
Merged
Conversation
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
|
✅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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 priorstop_advertising()suppressed; does not drop a live link (usedisconnect()for that), and discovery starts once nothing is connected.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.is_pairing()reports an open window.clear_bond()also forgets a kept bond (and its key record).start_pairing()andclear_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. Sincestart_pairing()never touches NVS, a reboot during or after the window also comes back to the previous console.Test
switch2_proexample on esp32s3 (IDF 6.1).start_pairing()/cancel_pairing()for its pairing window. Not yet tested against a console.🤖 Generated with Claude Code
https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU