Skip to content

Add async firmware cache clearing for multi-bond support#7

Merged
RobertDaleSmith merged 7 commits into
mainfrom
rds/multi-bond-support
Nov 28, 2025
Merged

Add async firmware cache clearing for multi-bond support#7
RobertDaleSmith merged 7 commits into
mainfrom
rds/multi-bond-support

Conversation

@RobertDaleSmith
Copy link
Copy Markdown
Owner

Summary

  • Adds ClearFirmwareCacheWrite protobuf command to clear cached firmware versions on the dongle
  • Clears both in-memory cache immediately and persisted flash storage asynchronously via work queue
  • Uses mutex to protect DIS cache from race conditions during BLE discovery
  • Loads all DIS caches into memory at boot for multi-bond support

Changes

  • ble_dis.c: Added mutex protection, async work queue for flash writes, ble_dis_clear_all_cached_firmware() function
  • main.c: Added protobuf handler for ClearFirmwareCacheWrite command
  • Updated nanopb proto bindings

Test plan

  • Connect MouthPad via dongle
  • Send clear firmware cache command from app
  • Verify dongle responds with success
  • Verify firmware version is re-read on next connection

Implements functions to clear only the firmware version field from
cached DIS info, forcing it to be re-read on next connection.

This solves the problem where:
1. Dongle caches MouthPad firmware version for fast reconnection
2. User updates MouthPad firmware directly via system BLE
3. Dongle incorrectly reports old cached version after reconnection

New functions:
- ble_dis_clear_cached_firmware_for_addr() - Clear for specific device
- ble_dis_clear_all_cached_firmware() - Clear for all bonded devices

New shell command:
- clearfwcache - Clears cached firmware for all bonds

This provides the foundation for a future protobuf message that
the app can send when initiating a firmware update flow.
Implements handler for ClearFirmwareCacheWrite message that:
- Clears cached firmware versions for all bonded devices
- Sends ClearFirmwareCacheResponse with success status
- Logs the operation for debugging

This allows the app to trigger firmware cache clearing when
initiating a MouthPad firmware update, preventing false
'update needed' warnings after successful updates.

The handler is ready but requires proto definitions to be added
to the mouthpad-proto submodule. See PROTO_CHANGES_NEEDED.md
for the required proto additions.

Handler location: main.c:673-687
Message tags: clear_firmware_cache_write_tag / clear_firmware_cache_response_tag
Regenerated C files from updated proto definitions that include:
- ClearFirmwareCacheWrite message
- ClearFirmwareCacheResponse message

This completes the proto integration for the firmware cache clearing feature.
Created in-memory cache array (dis_cache[4]) that holds DIS info for
all bonded devices. This cache is:
- Loaded from flash at boot via settings callback
- Updated when DIS discovery completes (flash + memory)
- Updated when clearfwcache is called (flash + memory)
- Queried on device connection to populate device_info

Flow:
1. Boot: Load all cached DIS from flash into dis_cache array
2. Device connects: Copy that device's cache entry into device_info
3. Check device_info for firmware (fast path vs slow path decision)
4. DIS discovery completes: Update both flash and dis_cache
5. clearfwcache: Clear firmware field in both flash and dis_cache

This ensures:
- No flash reads on every connection (only at boot)
- clearfwcache takes effect immediately on next reconnection
- Multi-bond support: each device has its own cached DIS
- Fast reconnection when firmware is cached
- Add mutex protection for dis_cache to prevent race conditions
- Implement deferred flash writes using work queue to avoid blocking
- Clear in-memory cache immediately, flash writes happen asynchronously
- Restore response sending for ClearFirmwareCacheWrite protobuf command
@RobertDaleSmith RobertDaleSmith merged commit cb745e4 into main Nov 28, 2025
13 checks passed
@RobertDaleSmith RobertDaleSmith deleted the rds/multi-bond-support branch November 28, 2025 06:39
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