Skip to content

Windows: skip OS-reserved services during discovery (#400) - #401

Merged
fotiDim merged 1 commit into
mainfrom
fix/400-windows-os-reserved-service
Nov 26, 2026
Merged

fotiDim merged 1 commit into
mainfrom
fix/400-windows-os-reserved-service

Conversation

@fotiDim

@fotiDim fotiDim commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #400.

Problem

Windows reserves a few GATT services for its own stack and answers GetCharacteristicsAsync with AccessDenied for them, whether or not the device is paired. HID over GATT (0x1812) is the common one, and it is on Microsoft's list of services the GATT API prevents apps from accessing.

Since #284 turned the discovery loop's continue into break + a stored error, that answer aborted the whole connection, so a device exposing HID could not connect at all: Failed to get characteristics for service 00001812-0000-1000-8000-00805f9b34fb: AccessDenied. Up to 2.1.0 the service was skipped and the device connected.

Fix

classify_discovery_status() in the new windows/src/gatt_discovery_policy.h decides what discovery does with each GetCharacteristicsAsync status: Success includes the service, AccessDenied skips it, and every other status fails discovery. Both loops use it:

  • ConnectAsync: a denied service is logged at info level and skipped; the rest of the database connects normally. If every service was denied the connection still succeeds, but a warning names the count, because an empty service map is otherwise indistinguishable from a device with no services.
  • RefreshGattServicesAsync: a denied service is skipped, so a Service Changed refresh with a reserved service in the database installs the new map instead of retaining the old one. The exception is a denied service the connection had already reported: dropping it would silently break its live reads and subscriptions, so the previous map is kept and the error is logged.

Unreachable and ProtocolError still fail discovery, so #284's intent — reporting a discovery that a dropped link cut short — is kept. The reserved service stays out of the service map, so discoverServices() does not report it. (Chromium reports a denied service with an empty characteristic list instead; this leaves it out so a service with no reachable characteristics is not returned.)

Tests

  • Windows HIL (hil/integration_test/baseline_hil_test.dart, Windows-only): uses the fixture's existing SET_HID_SERVICE command (0x17, already in universal_ble_hil_firmware, no firmware change needed). It registers the HID service, reconnects so connect-time discovery meets AccessDenied (this is the exact failure from the report), asserts the connection is up, the reserved service is absent and the fixture's own service is still readable, then adds the auxiliary service and requires it to appear — which proves the refresh walked past the denied service instead of keeping the old map. Cleanup removes both dynamic services while connected, because the fixture's reset clears the request flags without unregistering them.
  • Windows unit test (windows/test/gatt_discovery_policy_test.cpp): pins the status classification in pull-request CI, where the HIL suite does not run — including that an unknown status fails discovery instead of being skipped. The loops themselves are covered only by the HIL suite.

hil/README.md and hil/COVERAGE.md counts are updated (86 tests, 26 baseline).

Verification

  • flutter analyze/dart format in hil/: clean.
  • Windows build + ctest and the HIL test have not been run locally (macOS host); CI covers the build, the unit test, and the example app.
  • Manual on Windows: cd hil && flutter test integration_test/baseline_hil_test.dart -d windows --plain-name "OS-reserved". On a build without the native fix it fails at peripheral.reconnect() with the reported AccessDenied error.

Docs

  • CHANGELOG.md: new ## Unreleased entry.
  • README.md DiscoverServices: documents that Windows-denied services are left out of discovery, and that a service already reported is kept if a later refresh is denied access to it.

Windows reserves a few GATT services for its own stack and answers
GetCharacteristicsAsync with AccessDenied for them whether or not the device
is paired. HID over GATT (0x1812) is the common one. Since #284 turned the
discovery loop's `continue` into `break`, that answer aborted the whole
connection attempt, so a device exposing HID could not connect at all.

classify_discovery_status() in windows/src/gatt_discovery_policy.h maps the
status to include, skip or fail, and both discovery loops use it. AccessDenied
is skipped, so the reserved service stays out of the service map while the
rest of the database connects normally. Unreachable and ProtocolError still
fail discovery, which keeps #284's intent of reporting a discovery a dropped
link cut short.

Two boundaries keep the skip from hiding a real problem. AccessDenied is not
exclusive to reserved services, so if every service of a device was denied the
connection still succeeds but a warning names the count. In the Service
Changed refresh, a denied service the map already exposes is not dropped:
reads and subscriptions are live against it, so the previous map is kept and
the error is logged, as before the loop skipped reserved services.

Cover it with the fixture's existing SET_HID_SERVICE command (0x17): the new
Windows HIL test registers the HID service, reconnects so connect-time
discovery meets the denied service, checks the reserved service is absent
while the fixture's own service stays readable, then adds the auxiliary
service and requires it to appear, which proves the refresh walked past the
denied service. A Windows unit test pins the status classification for
pull-request CI, where the HIL suite does not run.
@fotiDim
fotiDim force-pushed the fix/400-windows-os-reserved-service branch from 1763400 to 0402205 Compare November 25, 2026 11:42
@fotiDim
fotiDim merged commit 8979822 into main Nov 26, 2026
4 checks passed
@fotiDim
fotiDim deleted the fix/400-windows-os-reserved-service branch November 26, 2026 20:29
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.

[Bug]: Windows: connect fails for devices with an HID service because 0x1812 returns AccessDenied

1 participant