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 Jan 10, 2024
1 parent 778bf10 commit 35f2040
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions libs/BLEKit/include/CoreGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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
14 changes: 13 additions & 1 deletion libs/BLEKit/source/CoreGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ void CoreGap::setAdvertising(AdvertisingData advertising_data)

void CoreGap::onConnectionCallback(const std::function<void()> &callback)
{
_gap_event_handler.onConnectionCallback(callback);
_on_connection_callback = [&, callback](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::min();
auto supervision_timeout = supervision_timeout_t {500};
_gap.updateConnectionParameters(handle, min_connection_interval, max_connection_interval, slave_latency,
supervision_timeout);

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(); // CAUTION: check still exist
_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

0 comments on commit 35f2040

Please sign in to comment.