Skip to content

Windows: harden BLE connection and GATT lifetimes - #284

Merged
fotiDim merged 3 commits into
Navideck:mainfrom
usmanmehmood55:windows-hardening
Aug 24, 2026
Merged

Windows: harden BLE connection and GATT lifetimes#284
fotiDim merged 3 commits into
Navideck:mainfrom
usmanmehmood55:windows-hardening

Conversation

@usmanmehmood55

@usmanmehmood55 usmanmehmood55 commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR hardens the Windows BLE implementation around asynchronous connection, GATT-operation, subscription, and shutdown lifecycles.

Changes

  • Retain GATT objects while reads, writes, descriptor operations, and discovery are in flight.
  • Track connection attempts and queued connection events by generation so stale callbacks, failures, and disconnect events cannot affect newer connections.
  • Validate existing native connections atomically before reusing or replacing them.
  • Collapse duplicate in-progress connection attempts.
  • Handle GattServicesChanged by refreshing the GATT map safely and restoring notification handlers for existing subscriptions.
  • Register notification handlers before enabling the CCC descriptor, preventing an immediately emitted first notification from being lost.
  • Make notification subscription changes transactional, including rollback after a failed CCC write and protection against overlapping operations.
  • Improve descriptor read/write error handling during disconnects.
  • Prevent new native callbacks from entering during plugin shutdown and drain callbacks already in progress before releasing plugin state.
  • Dispatch COM apartment continuations and Windows messages while draining callbacks to avoid blocking UI-thread continuations during shutdown.
  • Coalesce queued UI-thread wake-ups and discard queued work during shutdown.
  • Make the Windows logger’s active level atomic.
  • Normalize characteristic byte buffers at the Dart boundary so consumers do not receive unrelated platform-message bytes through value.buffer.
  • Return descriptor values from BleCharacteristic.readDescriptor().

Validation

All current HIL tests were run:

  • Baseline suite: 25 passed
  • Fault-injection suite: 39 passed

Also manually verified:

  • scanning
  • connection/disconnection
  • services/characteristics discovery
  • data send/receive
  • cleanup

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Hardens Windows BLE asynchronous lifecycles and improves Dart-side value handling.

Changes:

  • Adds connection generations, operation leases, shutdown tracking, and GATT refresh handling.
  • Strengthens notification subscription and descriptor-operation behavior.
  • Normalizes characteristic buffers and returns descriptor values from convenience APIs.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
windows/test/CMakeLists.txt Configures the tracker test.
windows/test/async_operation_tracker_test.cpp Tests lease and shutdown behavior.
windows/src/universal_ble_plugin.h Adds lifecycle and GATT state management.
windows/src/universal_ble_plugin.cpp Implements Windows BLE hardening.
windows/src/ui_thread_handler.hpp Coalesces and shuts down UI work.
windows/src/helper/universal_ble_logger.h Makes logger state atomic.
windows/src/helper/universal_ble_logger.cpp Implements atomic level access.
windows/src/async_operation_tracker.h Introduces asynchronous-operation leases.
windows/CMakeLists.txt Includes the tracker header.
test/device_id_case_insensitivity_test.dart Tests buffer normalization.
test/ble_characteristic_test.dart Tests descriptor reads.
lib/src/interfaces/universal_ble_platform_interface.dart Normalizes received byte buffers.
lib/src/extensions/ble_characteristic_extension.dart Returns descriptor read values.
CHANGELOG.md Documents Windows hardening.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread windows/src/universal_ble_plugin.cpp Outdated
Comment thread windows/test/async_operation_tracker_test.cpp Outdated
Comment thread windows/src/universal_ble_plugin.cpp Outdated
@fotiDim

fotiDim commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@usmanmehmood55 please check the CoPilot review comments when you get the chance. We plan to make a release within the week.

@usmanmehmood55

Copy link
Copy Markdown
Contributor Author

@fotiDim I've applied the changes pointed out by Copilot, waiting to access a Windows machine to run the HIL tests again. I'll push the changes in 2 hours or so.

@usmanmehmood55

Copy link
Copy Markdown
Contributor Author

PR comments addressed, with all hardware in loop and fault injection tests passing for Windows.

@fotiDim
fotiDim merged commit 05e7e73 into Navideck:main Aug 24, 2026
2 checks passed
@usmanmehmood55
usmanmehmood55 deleted the windows-hardening branch August 25, 2026 07:02
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.

3 participants