Skip to content

Commit

Permalink
🔀️ Merge branch 'yann/bug-fix/rc/disconnect-robot-when-entering-in-DS…
Browse files Browse the repository at this point in the history
…' into develop
  • Loading branch information
ladislas committed Jul 19, 2024
2 parents d80edbc + 50d5150 commit b7867ed
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 14 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
5 changes: 5 additions & 0 deletions libs/BLEKit/source/BLEKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ 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;
}
7 changes: 7 additions & 0 deletions libs/BLEKit/tests/BLEKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ TEST_F(BLEKitTest, isConnectedDefault)

EXPECT_FALSE(is_connected);
}

TEST_F(BLEKitTest, disconnect)
{
EXPECT_CALL(mbed_mock_gap, disconnect);

ble.disconnect();
}
7 changes: 7 additions & 0 deletions libs/BLEKit/tests/CoreGapEventHandler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,10 @@ TEST_F(CoreGapEventHandlerTest, isConnected)
is_connected = core_gap_event_handler.isConnected();
EXPECT_FALSE(is_connected);
}

TEST_F(CoreGapEventHandlerTest, getConnectionHandle)
{
auto handle = core_gap_event_handler.getConnectionHandle();

// nothing expected
}
7 changes: 7 additions & 0 deletions libs/BLEKit/tests/CoreGap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,10 @@ TEST_F(CoreGapTest, isConnectedAfterConnection)

// nothing expected
}

TEST_F(CoreGapTest, disconnect)
{
EXPECT_CALL(mbed_mock_gap, disconnect);

coregap.disconnect();
}
1 change: 1 addition & 0 deletions libs/RobotKit/include/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class RobotController : public interface::RobotController
for (auto &component: _deep_sleep_enabled_components) {
component->enableDeepSleep();
}
_ble.disconnect();
}

void wakeUp() final { system_reset(); }
Expand Down
1 change: 0 additions & 1 deletion libs/RobotKit/include/StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ struct StateMachine {
, sm::state::deep_sleeping + event<sm::event::charge_did_stop> = X

, sm::state::deep_sleeping + event<sm::event::ble_connection> = X
, sm::state::deep_sleeping + event<sm::event::ble_disconnection> = X

, sm::state::charging + boost::sml::on_entry<_> / (sm::action::start_deep_sleep_timeout {}, sm::action::start_charging_behavior {} )
, sm::state::charging + boost::sml::on_exit<_> / (sm::action::stop_deep_sleep_timeout {}, sm::action::stop_charging_behavior {} )
Expand Down
11 changes: 0 additions & 11 deletions libs/RobotKit/tests/StateMachine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,6 @@ TEST_F(StateMachineTest, stateDeepSleepingEventBleConnection)
EXPECT_TRUE(sm.is(X));
}

TEST_F(StateMachineTest, stateDeepSleepingEventBleDisconnection)
{
sm.set_current_states(lksm::state::deep_sleeping);

EXPECT_CALL(mock_rc, wakeUp);

sm.process_event(lksm::event::ble_disconnection {});

EXPECT_TRUE(sm.is(X));
}

TEST_F(StateMachineTest, stateIdleEventChargeDidStart)
{
sm.set_current_states(lksm::state::idle);
Expand Down

0 comments on commit b7867ed

Please sign in to comment.