Skip to content

Enhance BLE testing with fakes, stubs, and platform support#947

Draft
Mearman wants to merge 21 commits intocoredevices:mainfrom
Mearman:fix/test-ble-subscriptions
Draft

Enhance BLE testing with fakes, stubs, and platform support#947
Mearman wants to merge 21 commits intocoredevices:mainfrom
Mearman:fix/test-ble-subscriptions

Conversation

@Mearman
Copy link
Contributor

@Mearman Mearman commented Mar 9, 2026

This pull request introduces significant improvements to the test fakes for Bluetooth stack APIs, making them more robust and portable across environments where the real Bluetopia headers are not available (such as Linux or Docker). The changes add conditional compilation and dummy implementations for GAPAPI, GATTAPI, HCIAPI, and SS1BTPS, ensuring tests can run regardless of the presence of these headers. Additionally, error handling and subscription cleanup logic are improved in the BLE client code.

Test Fakes Portability and Robustness

  • Added conditional compilation to fake_GAPAPI.c, fake_GAPAPI.h, fake_GATTAPI.c, fake_GATTAPI.h, fake_HCIAPI.c, fake_gap_le_connect_params.c, and stubs_HCIAPI.h to detect header availability and provide dummy type definitions and stub implementations when the real APIs are missing. This ensures test code builds and runs in all environments. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
  • Implemented dummy GAPAPI and GATTAPI function bodies and type definitions in the test fakes, allowing tests to link and execute even when the real Bluetooth stack is unavailable. [1] [2] [3] [4] [5] [6] [7]

BLE Client Error Handling

  • Improved error handling in prv_handle_meta_read to distinguish between retriable and permanent GATT errors, ensuring clients are deleted immediately on permanent errors and retried only for recoverable ones. [1] [2]

Subscription Management

  • Enhanced subscription cleanup logic in the fake GATT client subscriptions to properly free memory when cleaning up by client or connection, preventing leaks and improving test reliability.

Minor Fixes

  • Corrected the type of the status parameter in fake_gatt_put_discovery_complete_event from uint8_t to uint16_t for accuracy.
  • Fixed list head initialization in BLE GATT client subscription code to handle cases where the subscription list is empty.

@Mearman Mearman force-pushed the fix/test-ble-subscriptions branch 4 times, most recently from a3969ab to 76cd0fa Compare March 9, 2026 20:13
Mearman added 4 commits March 9, 2026 21:53
Require commit messages to use path-based areas (e.g., fw/drivers/hrm)
or known short areas (e.g., ci, docs, treewide) rather than conventional
commit types like feat:, fix:, chore:.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Append -darwin suffix to fixture filenames on macOS to handle rendering
differences in font libraries. Linux (CI) uses standard ~platform naming
to match existing fixtures.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Use memcpy for BD_ADDR_t address fields instead of direct assignment,
which was causing incorrect address comparisons in whitelist operations.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Add run-tests-docker.sh to run tests in Docker matching CI environment,
and generate-linux-fixtures.sh to generate Linux-specific test fixtures.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
@Mearman Mearman force-pushed the fix/test-ble-subscriptions branch 2 times, most recently from 5c331bb to be55eb1 Compare March 9, 2026 22:45
Document the cross-platform fixture naming scheme, Docker testing
workflow, and troubleshooting for CI vs local test discrepancies.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
@Mearman Mearman force-pushed the fix/test-ble-subscriptions branch from be55eb1 to 951d0aa Compare March 9, 2026 23:01
Mearman added 5 commits March 9, 2026 23:08
Restore platform suffix on Linux (e.g. ~spalding) while keeping the
additional -darwin suffix for macOS local development. This matches
the naming convention of the PNG fixture files in the repository.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
- gatt_client_subscriptions: check if gatt_subscriptions is NULL before
  dereferencing when prepending to the list
- ppogatt: distinguish between retriable and permanent GATT errors
  during meta characteristic read; only retry on timeout/resource
  errors, immediately fail on permission/handle errors

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
- Add conditional compilation for Bluetopia headers (SS1BTPS, L2CAPAPI)
- Move bt_driver_gatt and bt_driver_gatt_client_discovery implementations
  to .c files to break circular header dependencies
- Add stub implementations for bt_driver CCCD handling
- Update gap_le_advert stubs with advertising data functions
- Add DiscoveryJobQueue cleanup stub implementation

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
When Bluetopia headers (GAPAPI, GATTAPI) are unavailable (non-BTSTACK
builds), provide dummy implementations so tests can compile. This
enables running BLE-related tests without the full Bluetooth stack
dependencies.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
- Enable previously broken BLE tests (gatt_client_subscriptions,
  gatt_client_discovery, gap_le_advert, ppogatt, kernel_le_client)
- Add compiler normalisation flags for consistent test results
- Fix include path (../include → ../src/include)
- Disable DUMA on macOS ARM
- Create platform-specific failure directories to prevent contamination
- Skip Xbit processing when platform-specific PNG files exist

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
@Mearman Mearman force-pushed the fix/test-ble-subscriptions branch from 530a835 to 8208065 Compare March 10, 2026 05:38
Mearman added 11 commits March 10, 2026 06:19
Rename BTDeviceAddressToBDADDR that takes BTDeviceAddress to
prv_get_addr_octets to avoid conflict with the stub version that
takes const uint8_t* when HCIAPI is not available.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Update test files to pass the new watchdog_timer parameter.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Link bt_driver_gatt and gap_le_advert stubs to resolve
undefined reference errors in BLE tests.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
The sh library has a bug on macOS with Python 3.11 that causes
an OverflowError in os.closerange when calling pip.freeze.
Using subprocess.check_output avoids this issue.

Signed-off-by: Joseph Mearman <[email protected]>
- Update gap_le_connection_add() calls with 4th argument
- Fix include path for stubs_bluetopia_interface.h
- Add static keyword to prevent multiple definition errors
- Remove reference to non-existent stubs_gap_le_advert.c

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
- Add static inline to bt_driver_advert_* functions in stubs_gap_le_advert.h
  to prevent multiple definition linker errors
- Fix gatt_client_discovery_cleanup_by_connection signature in
  fake_gatt_client_discovery.c to match the real function signature

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
This fixes undefined reference errors for bt_driver_advert_* functions.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Remove 'static inline' from stub functions in stubs_gap_le_advert.h and
stubs_gatt_client_discovery.h so they generate linkable symbols.

When real source files like gap_le_advert.c are compiled, they call these
driver functions. With static inline, the compiler can't inline across
translation units, so the linker can't find the symbols.

By removing static inline, these become regular functions that can be
linked against from compiled source files.

Fixes test failures in:
- test_gap_le_advert
- test_gatt_client_discovery
- test_gatt_client_accessors

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
The stub was calling undefined GAP API functions (GAP_LE_Set_Advertising_Data
and GAP_LE_Set_Scan_Response_Data) which caused linker errors.

Simplify the stub to be a no-op, which is sufficient for test purposes.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
Add stub implementations for bt_driver_gatt_* functions that only had
declarations. This fixes linker errors when real source files call these
driver functions.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
The test file declares this function as extern but it was not defined.
Add a stub implementation that checks NumberOfCharacteristics > 0.

Co-authored-by: Claude <[email protected]>

Signed-off-by: Joseph Mearman <[email protected]>
@Mearman Mearman marked this pull request as draft March 10, 2026 16:01
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