Skip to content

Commit

Permalink
⚡ (BLE): Update connection parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Feb 5, 2024
1 parent 53d1a4f commit 9f77d69
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
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
20 changes: 19 additions & 1 deletion libs/BLEKit/source/CoreGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> &callback)
{
_gap_event_handler.onConnectionCallback(callback);
_on_connection_callback = [this, &callback](connection_handle_t handle) {
updateConnectionParameters(handle);
callback();

Check warning on line 79 in libs/BLEKit/source/CoreGap.cpp

View check run for this annotation

Codecov / codecov/patch

libs/BLEKit/source/CoreGap.cpp#L78-L79

Added lines #L78 - L79 were not covered by tests
};
_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

0 comments on commit 9f77d69

Please sign in to comment.