Skip to content

rds/multi-bond-support#6

Merged
RobertDaleSmith merged 21 commits into
mainfrom
rds/multi-bond-support
Nov 24, 2025
Merged

rds/multi-bond-support#6
RobertDaleSmith merged 21 commits into
mainfrom
rds/multi-bond-support

Conversation

@RobertDaleSmith

@RobertDaleSmith RobertDaleSmith commented Nov 22, 2025

Copy link
Copy Markdown
Owner

Adds multiple bond support.

Previously only one MP^ could be bonded at a time. And then it would only re-connect to that one bonded device.

Now the dongle will attempt to re-connect to previous bonds and if not found then auto-bond to any un-bonded advertising MP.


Note

Enable bonding with up to 4 devices, prefer reconnecting to known devices, add a timed additional-scan for new devices, persist per-device DIS info, and expose a shell command to list bonds.

  • BLE Central (src/ble_central.*):
    • Multi-bond management: Track up to MAX_BONDED_DEVICES=4 with struct bonded_device, mutex protection, and APIs: ble_central_is_device_bonded, add/remove/get_bonded_devices, clear_bonded_device, get_bonded_device_addr.
    • Persistence: Save/load bond addr/name under settings (ble_central/bond_X/*); load settings before bt_foreach_bond; display restored bonds at boot.
    • Scanning logic: New modes SCAN_MODE_NORMAL (prefer bonded) and SCAN_MODE_ADDITIONAL (only new devices) with 10s timeout; indicator reflects mode; always restart scan on connection failure.
    • Pairing/startup: On pairing, add to bonded list and exit additional mode; new state queries (is_scanning/is_connecting/is_connected) and mark_services_ready.
  • Device Information Service (src/ble_dis.*):
    • Per-device storage: Save/load DIS info per address (ble_dis/<addr>/info); new APIs ble_dis_load_info_for_addr, ble_dis_clear_saved_for_addr; ble_dis_clear_saved now only clears cache; save DIS on discovery/name read.
  • Main (src/main.c):
    • Add bonds shell command to list bonded devices; delay 500ms before clearing bonds; DeviceInfo response now loads DIS info for the connected or first bonded device.
  • Config (prj.conf):
    • Increase bonds: CONFIG_BT_MAX_PAIRED=4 (was 1).

Written by Cursor Bugbot for commit 9fbf109. This will update automatically on new commits. Configure here.

Add ability to bond with up to 4 MouthPad devices simultaneously.
Users can quickly switch between bonded devices without re-pairing.

Changes:
- Replace single bond tracking with multi-bond array (MAX_BONDED_DEVICES=4)
- Add bonded_device struct with address, name, last_seen timestamp
- Implement bond management functions:
  * ble_central_is_device_bonded() - check if device is bonded
  * ble_central_add_bonded_device() - add new bond to list
  * ble_central_remove_bonded_device() - remove specific bond
  * ble_central_get_bonded_devices() - get all bonded devices
- Update clear_bonded_device() to clear ALL bonds
- Update bt_foreach_bond() callback to populate multi-bond array
- Add mutex protection for thread-safe bond list access
- Add scanning mode enum (NORMAL vs ADDITIONAL) for future use

Note: Settings persistence and additional scan mode to be implemented next
Update settings handlers to save/load multiple bonded device names:
- Parse bond_X/name and bond_X/addr keys from settings
- Save device names when bonds are added or updated
- Load device names on startup for all bonded devices
- Clear all bond settings when bonds are cleared

Settings are stored as:
  ble_central/bond_0/name
  ble_central/bond_1/name
  ...

Device addresses are populated by bt_foreach_bond() and don't
need to be saved to settings separately.
Add ability to scan for additional MouthPad devices via button press.
Users can now bond multiple devices and quickly switch between them.

Button behavior:
- Single press: Start ADDITIONAL scan (10 second timeout)
  * Disconnects current device (keeps bond)
  * Scans for NEW devices only (ignores already-bonded)
  * Auto-returns to NORMAL mode after timeout or new bond
- Hold (2s): Clear ALL bonds (unchanged)

Scan modes:
- NORMAL: Connect to any bonded device (first seen)
- ADDITIONAL: Only connect to NEW unbonded devices

Changes:
- Add ble_central_start_additional_scan() function
- Implement 10-second timeout with countdown display
- Update scan_filter_match() to check bond status based on mode
- Add check_additional_scan_timeout() for automatic mode switching
- Wire button CLICK event to start additional scan
- Update all old single-bond references to use new multi-bond API
- Fix pairing_complete to use ble_central_add_bonded_device()
- Auto-switch to NORMAL mode after successful new bond

Phase 1 complete - ready for testing!
Add comprehensive bonded device display functionality:
- display_bonded_devices() function in ble_central.c that logs all bonded devices with names and addresses during initialization
- Shell command 'bonds' to query bonded devices on demand via serial console
- Called automatically after settings load to verify multi-bond state

This helps verify multi-bond functionality during testing and provides visibility into which devices are currently bonded.
- Remove duplicate struct bonded_device definition (use header definition)
- Add forward declaration for check_additional_scan_timeout()
- Remove unused variables (next, filter_mode)
- Remove unused try_add_address_filter() function
- Initialize bonded_devices array to zero at startup to prevent corrupted entries
The ble_central_get_bonded_devices() function was copying valid entries
sequentially but leaving unused array slots untouched. When called from
the shell command with an uninitialized stack array, this caused
garbage data to appear as phantom bonded devices.

Now properly zeros the entire output array before copying valid entries.
Move settings_load() before bt_foreach_bond() to match the working
single-bond implementation. The BT stack needs settings loaded before
it can enumerate existing bonds.

Fixes bond restoration after reboot for multi-bond support.
- Always connect to bonded devices when available
- Immediately accept unbonded devices if no bonded ones advertising
- Remove timeout-based delay, use dynamic availability check
- Track bonded device advertising per scan session
- Remove button click trigger for additional scan mode
- Auto-fallback to unbonded devices when no bonds available
- Reset scan mode to NORMAL when clearing bonds
- Add delay after disconnect before clearing bonds
- Add yields between flash operations to prevent blocking
Always restart scanning on connection failure, not just when
default_conn matches. Fixes issue where connection timeouts would
leave the device stuck without scanning.
- Save both addresses and names to persistent settings when bonding
- Load addresses from settings before loading names (fixes name loss)
- Track bond count when loading from settings
- Ensures bond order consistency across reboots
Remove is_valid check when loading names - addresses and names
can load in any order from settings subsystem
- Store DIS info with device address in key
- Add functions to load/clear DIS per address
- Load device names from DIS info when enumerating bonds
- Save DIS with current connection address
- Still need to remove old name/address storage code
Settings keys can't have parentheses. Changed from
'F0_1A_5F_52_2A_3E_(random)' to clean hex 'F01A5F522A3E_1'
- Load DIS info for specific device (connected or first bonded)
- Use settings_load_one() instead of settings_runtime_get()
- Prefer connected device, fallback to first bonded device

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

Comment thread ncs/app/src/ble_central.c
When adding a 5th bonded device:
- Find the device with the oldest last_seen timestamp
- Clear its DIS info from storage
- Unpair it from the BT stack
- Clear its settings entries
- Replace it with the new device

This ensures we always maintain the 4 most recently used bonds.
ble_central_remove_bonded_device now properly cleans up:
- DIS info from persistent storage
- Settings entries (bond_N/name and bond_N/addr)
- In-memory bonded_devices array

This prevents removed devices from reappearing after reboot.
Increase key buffer from 32 to 64 bytes to prevent truncation
warnings when formatting settings keys like 'ble_central/bond_X/name'.
@RobertDaleSmith RobertDaleSmith merged commit 493fc90 into main Nov 24, 2025
14 checks passed
@RobertDaleSmith RobertDaleSmith deleted the rds/multi-bond-support branch November 24, 2025 07:14
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