Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ (BLE): Update connection parameters #1353

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libs/BLEKit/include/CoreGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> &callback);
void onDisconnectionCallback(const std::function<void()> &callback);
[[nodiscard]] auto isConnected() const -> bool;
Expand All @@ -40,6 +42,8 @@ class CoreGap

CoreGapEventHandler _gap_event_handler;
ble::Gap &_gap;

std::function<void(ble::connection_handle_t handle)> _on_connection_callback {};
};

} // namespace leka
4 changes: 2 additions & 2 deletions libs/BLEKit/include/CoreGapEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> &callback);
void onConnectionCallback(const std::function<void(ble::connection_handle_t handle)> &callback);
void onDisconnectionCallback(const std::function<void()> &callback);
[[nodiscard]] auto isConnected() const -> bool;

Expand All @@ -33,7 +33,7 @@ class CoreGapEventHandler : public ble::Gap::EventHandler

std::function<void()> _start_advertising {};

std::function<void()> _on_connection_callback {};
std::function<void(ble::connection_handle_t handle)> _on_connection_callback {};
std::function<void()> _on_disconnection_callback {};
};

Expand Down
22 changes: 21 additions & 1 deletion libs/BLEKit/source/CoreGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,29 @@ void CoreGap::setAdvertising(AdvertisingData advertising_data)
_gap.setAdvertisingPayload(_advertising_handle, _advertising_data_builder.getAdvertisingData());
}

void CoreGap::updateConnectionParameters(ble::connection_handle_t handle)
{
// ? For mbed definitions, see:
// ? mbed-os/connectivity/FEATURE_BLE/include/ble/Gap.h
// ? For Apple guidelines, see:
// ? 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<void()> &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<void()> &callback)
Expand Down
5 changes: 3 additions & 2 deletions libs/BLEKit/source/CoreGapEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -48,7 +49,7 @@ void CoreGapEventHandler::onAdvertisingEnd(AdvertisingEndEvent const &event)
_start_advertising();
}

void CoreGapEventHandler::onConnectionCallback(const std::function<void()> &callback)
void CoreGapEventHandler::onConnectionCallback(const std::function<void(connection_handle_t)> &callback)
{
_on_connection_callback = callback;
}
Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/tests/CoreGapEventHandler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ TEST_F(CoreGapEventHandlerTest, onAdvertisingEnd)

TEST_F(CoreGapEventHandlerTest, onConnectionCallback)
{
MockFunction<void()> mock_on_connection_callback;
MockFunction<void(ble::connection_handle_t handle)> mock_on_connection_callback;

core_gap_event_handler.onConnectionCallback(mock_on_connection_callback.AsStdFunction());

Expand Down
21 changes: 21 additions & 0 deletions libs/BLEKit/tests/CoreGap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using namespace leka;
using namespace ble;

using ::testing::_;
using ::testing::Return;
using ::testing::Sequence;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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};
Expand Down
Loading