From 8266c540da5c983d54dd9577d10c6a76e96e38c3 Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Fri, 5 Jan 2024 17:03:55 +0100 Subject: [PATCH] :zap: (BLE): Update connection parameters --- libs/BLEKit/include/CoreGap.h | 4 ++++ libs/BLEKit/include/CoreGapEventHandler.h | 4 ++-- libs/BLEKit/source/CoreGap.cpp | 20 +++++++++++++++++- libs/BLEKit/source/CoreGapEventHandler.cpp | 5 +++-- .../BLEKit/tests/CoreGapEventHandler_test.cpp | 2 +- libs/BLEKit/tests/CoreGap_test.cpp | 21 +++++++++++++++++++ 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/libs/BLEKit/include/CoreGap.h b/libs/BLEKit/include/CoreGap.h index 5b0f0896e1..c6be542c90 100644 --- a/libs/BLEKit/include/CoreGap.h +++ b/libs/BLEKit/include/CoreGap.h @@ -28,6 +28,8 @@ class CoreGap void startAdvertising(); void setAdvertising(AdvertisingData advertising_data); + void updateConnectionParameters(ble::connection_handle_t handle); + void onConnectionCallback(const std::function &callback); void onDisconnectionCallback(const std::function &callback); [[nodiscard]] auto isConnected() const -> bool; @@ -40,6 +42,8 @@ class CoreGap CoreGapEventHandler _gap_event_handler; ble::Gap &_gap; + + std::function _on_connection_callback {}; }; } // namespace leka diff --git a/libs/BLEKit/include/CoreGapEventHandler.h b/libs/BLEKit/include/CoreGapEventHandler.h index a4700dc46b..c34941d405 100644 --- a/libs/BLEKit/include/CoreGapEventHandler.h +++ b/libs/BLEKit/include/CoreGapEventHandler.h @@ -24,7 +24,7 @@ class CoreGapEventHandler : public ble::Gap::EventHandler void onDisconnectionComplete(ble::DisconnectionCompleteEvent const &event) override; void onAdvertisingEnd(ble::AdvertisingEndEvent const &event) override; - void onConnectionCallback(const std::function &callback); + void onConnectionCallback(const std::function &callback); void onDisconnectionCallback(const std::function &callback); [[nodiscard]] auto isConnected() const -> bool; @@ -33,7 +33,7 @@ class CoreGapEventHandler : public ble::Gap::EventHandler std::function _start_advertising {}; - std::function _on_connection_callback {}; + std::function _on_connection_callback {}; std::function _on_disconnection_callback {}; }; diff --git a/libs/BLEKit/source/CoreGap.cpp b/libs/BLEKit/source/CoreGap.cpp index 14680fd219..34800b4256 100644 --- a/libs/BLEKit/source/CoreGap.cpp +++ b/libs/BLEKit/source/CoreGap.cpp @@ -58,9 +58,27 @@ void CoreGap::setAdvertising(AdvertisingData advertising_data) _gap.setAdvertisingPayload(_advertising_handle, _advertising_data_builder.getAdvertisingData()); } +void CoreGap::updateConnectionParameters(ble::connection_handle_t handle) +{ + // ? : See mbed-os/connectivity/FEATURE_BLE/include/ble/Gap.h for definitions + // ? : Apple guidelines https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf#page=221 + + auto min_connection_interval = conn_interval_t {12}; // Min: 15ms = 12*1,25 + auto max_connection_interval = min_connection_interval; + auto slave_latency = slave_latency_t {0}; + auto supervision_timeout = supervision_timeout_t {500}; + + _gap.updateConnectionParameters(handle, min_connection_interval, max_connection_interval, slave_latency, + supervision_timeout); +} + void CoreGap::onConnectionCallback(const std::function &callback) { - _gap_event_handler.onConnectionCallback(callback); + _on_connection_callback = [&, callback](connection_handle_t handle) { + updateConnectionParameters(handle); + callback(); + }; + _gap_event_handler.onConnectionCallback(_on_connection_callback); } void CoreGap::onDisconnectionCallback(const std::function &callback) diff --git a/libs/BLEKit/source/CoreGapEventHandler.cpp b/libs/BLEKit/source/CoreGapEventHandler.cpp index 5b172a1155..193cb827b7 100644 --- a/libs/BLEKit/source/CoreGapEventHandler.cpp +++ b/libs/BLEKit/source/CoreGapEventHandler.cpp @@ -28,7 +28,8 @@ void CoreGapEventHandler::onConnectionComplete(ConnectionCompleteEvent const &ev } if (_on_connection_callback != nullptr) { - _on_connection_callback(); + auto handle = event.getConnectionHandle(); + _on_connection_callback(handle); } is_connected = true; } @@ -48,7 +49,7 @@ void CoreGapEventHandler::onAdvertisingEnd(AdvertisingEndEvent const &event) _start_advertising(); } -void CoreGapEventHandler::onConnectionCallback(const std::function &callback) +void CoreGapEventHandler::onConnectionCallback(const std::function &callback) { _on_connection_callback = callback; } diff --git a/libs/BLEKit/tests/CoreGapEventHandler_test.cpp b/libs/BLEKit/tests/CoreGapEventHandler_test.cpp index 627a6a356b..71e79d1b79 100644 --- a/libs/BLEKit/tests/CoreGapEventHandler_test.cpp +++ b/libs/BLEKit/tests/CoreGapEventHandler_test.cpp @@ -97,7 +97,7 @@ TEST_F(CoreGapEventHandlerTest, onAdvertisingEnd) TEST_F(CoreGapEventHandlerTest, onConnectionCallback) { - MockFunction mock_on_connection_callback; + MockFunction mock_on_connection_callback; core_gap_event_handler.onConnectionCallback(mock_on_connection_callback.AsStdFunction()); diff --git a/libs/BLEKit/tests/CoreGap_test.cpp b/libs/BLEKit/tests/CoreGap_test.cpp index c0744a1653..bcd36ab0f2 100644 --- a/libs/BLEKit/tests/CoreGap_test.cpp +++ b/libs/BLEKit/tests/CoreGap_test.cpp @@ -14,6 +14,7 @@ using namespace leka; using namespace ble; +using ::testing::_; using ::testing::Return; using ::testing::Sequence; @@ -64,6 +65,11 @@ MATCHER_P(compareAdvertisingPayload, expected_data_builder, "") return expected_data_builder.getAdvertisingData() == arg; } +MATCHER_P(compareSlaveLatency, expected_slave_latency, "") +{ + return expected_slave_latency.value() == arg.value(); +} + TEST_F(CoreGapTest, initialization) { EXPECT_NE(&coregap, nullptr); @@ -136,6 +142,21 @@ TEST_F(CoreGapTest, startAdvertisingAdvertisingWasActive) coregap.startAdvertising(); } +TEST_F(CoreGapTest, updateConnectionParameters) +{ + auto handle = connection_handle_t {}; + auto min_connection_interval = conn_interval_t {12}; // Min: 15ms = 12*1,25 + auto max_connection_interval = min_connection_interval; + auto slave_latency = slave_latency_t {0}; + auto supervision_timeout = supervision_timeout_t {500}; + + EXPECT_CALL(mbed_mock_gap, + updateConnectionParameters(handle, min_connection_interval, max_connection_interval, + compareSlaveLatency(slave_latency), supervision_timeout, _, _)); + + coregap.updateConnectionParameters(handle); +} + TEST_F(CoreGapTest, onInitializationComplete) { BLE::InitializationCompleteCallbackContext context = {ble, BLE_ERROR_NONE};