Skip to content

Commit

Permalink
✨ (BLE): Robot can disconnect itself
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Jul 19, 2024
1 parent d80edbc commit b38441a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions libs/BLEKit/include/BLEKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class BLEKit
void onDisconnectionCallback(const std::function<void()> &callback);
[[nodiscard]] auto isConnected() const -> bool;

void disconnect();

void onMTUNegotiated(const std::function<void(uint16_t)> &callback);

private:
Expand Down
2 changes: 2 additions & 0 deletions libs/BLEKit/include/CoreGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class CoreGap
void onDisconnectionCallback(const std::function<void()> &callback);
[[nodiscard]] auto isConnected() const -> bool;

void disconnect();

private:
ble::advertising_handle_t _advertising_handle {ble::LEGACY_ADVERTISING_HANDLE};
ble::AdvertisingParameters _advertising_params {};
Expand Down
4 changes: 4 additions & 0 deletions libs/BLEKit/include/CoreGapEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ class CoreGapEventHandler : public ble::Gap::EventHandler
void onDisconnectionCallback(const std::function<void()> &callback);
[[nodiscard]] auto isConnected() const -> bool;

[[nodiscard]] auto getConnectionHandle() const -> ble::connection_handle_t;

private:
bool is_connected = false;

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

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

ble::connection_handle_t _handle;
};

} // namespace leka
4 changes: 4 additions & 0 deletions libs/BLEKit/source/BLEKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ auto BLEKit::isConnected() const -> bool
return _core_gap.isConnected();
}

void BLEKit::disconnect() {
_core_gap.disconnect();
}

void BLEKit::onMTUNegotiated(const std::function<void(uint16_t)> &callback)
{
_core_gatt_server.onMTUNegotiated(callback);
Expand Down
5 changes: 5 additions & 0 deletions libs/BLEKit/source/CoreGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ auto CoreGap::isConnected() const -> bool
{
return _gap_event_handler.isConnected();
}

void CoreGap::disconnect()
{
_gap.disconnect(_gap_event_handler.getConnectionHandle(), ble::local_disconnection_reason_t::USER_TERMINATION);
}
9 changes: 7 additions & 2 deletions libs/BLEKit/source/CoreGapEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ void CoreGapEventHandler::onConnectionComplete(ConnectionCompleteEvent const &ev
}

if (_on_connection_callback != nullptr) {
auto handle = event.getConnectionHandle();
_on_connection_callback(handle);
_handle = event.getConnectionHandle();
_on_connection_callback(_handle);
}
is_connected = true;
}
Expand Down Expand Up @@ -63,3 +63,8 @@ auto CoreGapEventHandler::isConnected() const -> bool
{
return is_connected;
}

auto CoreGapEventHandler::getConnectionHandle() const -> connection_handle_t
{
return _handle;
}

0 comments on commit b38441a

Please sign in to comment.