Skip to content

feat(switch2_pro): application lifecycle callbacks and control surface - #913

Merged
finger563 merged 2 commits into
mainfrom
feat/switch2-pro-app-api
Nov 18, 2026
Merged

finger563 merged 2 commits into
mainfrom
feat/switch2-pro-app-api

Conversation

@finger563

Copy link
Copy Markdown
Contributor

What

Adds the application-integration surface to Switch2Pro, for a host that drives its own connection manager, LED/telemetry, or pairing-record store around the controller. All additions are additive and opt-in; existing users are unaffected.

  • Lifecycle callbacks (Config::callbacks): on_connected, on_disconnected(reason), on_paired, on_input_streaming(bool), mirroring the polling getters so an app can be notified instead of polling from its IO tick. on_input_streaming is edge-triggered off is_input_streaming(). All fire from the NimBLE host-task context (documented).
  • State + control: is_initialized(), is_advertising(), stop_advertising() (also stops the wake re-advertiser), and disconnect() (terminate the link, keep the bond).
  • Bond management without a live session: static erase_stored_bond() erases the persisted bond from NVS with no instance (for a host that forgets a console while the radio is up as something else), and Config::start_fresh makes init() drop any stored bond and advertise for a fresh pairing instead of reconnecting.
  • Identity: bonded_console_address() returns the bonded console's address (or nullopt) so a host can name it in its own records.
  • Battery: Pro2InputReport::set_battery_percent(pct, charging, external) quantises a 0..100 percentage to the report's 0..9 level, so hosts don't each pick their own mapping.

Why

Today the class exposes only polling state and init() brings the radio up and advertises with no public way to stop advertising, forget a bond without an instance, or learn the bonded address. These are the hooks an application needs to integrate the controller into a larger device without reaching into NimBLE.

Test

Builds the switch2_pro example on esp32s3.

Adds the integration surface an application needs to drive its own connection
manager, LEDs/telemetry, and pairing-record store around the emulated
controller. All additions are additive and opt-in; existing users are unaffected.

- Config::callbacks (Callbacks): on_connected, on_disconnected(reason),
  on_paired, on_input_streaming(bool), mirroring the polling getters so an app
  can be notified instead of polling. on_input_streaming is edge-triggered off
  is_input_streaming(); all fire from the NimBLE host-task context (documented).
- is_initialized(), is_advertising(), stop_advertising() (also stops the wake
  re-advertiser), disconnect() (terminate the link, keep the bond).
- static erase_stored_bond() erases the persisted bond from NVS with no live
  instance; Config::start_fresh makes init() drop any stored bond and advertise
  for a fresh pairing instead of reconnecting.
- bonded_console_address() returns the bonded console's address (or nullopt).
- Pro2InputReport::set_battery_percent(pct, charging, external) quantises a
  0..100 percentage to the report's 0..9 level.

Builds the switch2_pro example on esp32s3.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563
finger563 marked this pull request as ready for review November 18, 2026 02:22
@github-actions

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

- is_advertising(): use auto for NimBLEDevice::getAdvertising() so it builds
  under CONFIG_BT_NIMBLE_EXT_ADV (where it returns NimBLEExtAdvertising*), not
  only the legacy-advertising config.
- bonded_console_address(): return the stable identity (host_addr_, the 0x15
  exchange address the component trusts for reconnect) instead of bond_peer_val_,
  which is the NimBLE connection address and may be a rotating private address
  (no SMP resolution here). host_addr_ is set during the handshake before paired_
  flips, so this also removes the narrow FINALISE race the old getter had.
- stop_advertising(): latch readvertise_suppressed_ so the disconnect callback
  does not auto re-advertise afterwards; a "disconnect() then stop_advertising()"
  go-dark sequence is now race-free regardless of when the disconnect callback
  runs. wake_console() clears the latch.
- disconnect(): return false when the link was already gone (BLE_HS_ENOTCONN),
  matching the documented "true only if a connection was live".
- erase_stored_bond(): document the NVS-initialised precondition; start_fresh
  now logs an erase failure instead of ignoring the return value.

Builds the switch2_pro example on esp32s3.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563
finger563 force-pushed the feat/switch2-pro-app-api branch from 219cba4 to 1304f8d Compare November 18, 2026 15:01
@finger563
finger563 merged commit b3895ad into main Nov 18, 2026
183 checks passed
@finger563
finger563 deleted the feat/switch2-pro-app-api branch November 18, 2026 21:09
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