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

feat!: add ble state 'initiating' #723

Merged
merged 13 commits into from
Oct 24, 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
11 changes: 9 additions & 2 deletions services/ble/Gap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,14 @@ namespace services
});
}

void GapCentralDecorator::Connect(hal::MacAddress macAddress, GapDeviceAddressType addressType)
void GapCentralDecorator::Connect(hal::MacAddress macAddress, GapDeviceAddressType addressType, infra::Duration initiatingTimeout)
{
GapCentralObserver::Subject().Connect(macAddress, addressType);
GapCentralObserver::Subject().Connect(macAddress, addressType, initiatingTimeout);
}

void GapCentralDecorator::CancelConnect()
{
GapCentralObserver::Subject().CancelConnect();
}

void GapCentralDecorator::Disconnect()
Expand Down Expand Up @@ -259,6 +264,8 @@ namespace infra
stream << "Scanning";
else if (state == services::GapState::advertising)
stream << "Advertising";
else if (state == services::GapState::initiating)
stream << "Initiating";
else
stream << "Connected";

Expand Down
10 changes: 7 additions & 3 deletions services/ble/Gap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SERVICES_GAP_HPP

#include "hal/interfaces/MacAddress.hpp"
#include "infra/timer/Timer.hpp"
#include "infra/util/EnumCast.hpp"
#include "infra/util/Observer.hpp"

Expand All @@ -24,7 +25,8 @@ namespace services
standby,
scanning,
advertising,
connected
connected,
initiating
};

enum class GapAdvertisingEventType : uint8_t
Expand Down Expand Up @@ -309,7 +311,8 @@ namespace services
: public infra::Subject<GapCentralObserver>
{
public:
virtual void Connect(hal::MacAddress macAddress, GapDeviceAddressType addressType) = 0;
virtual void Connect(hal::MacAddress macAddress, GapDeviceAddressType addressType, infra::Duration initiatingTimeout) = 0;
virtual void CancelConnect() = 0;
virtual void Disconnect() = 0;
virtual void SetAddress(hal::MacAddress macAddress, GapDeviceAddressType addressType) = 0;
virtual void StartDeviceDiscovery() = 0;
Expand All @@ -328,7 +331,8 @@ namespace services
void StateChanged(GapState state) override;

// Implementation of GapCentral
void Connect(hal::MacAddress macAddress, GapDeviceAddressType addressType) override;
void Connect(hal::MacAddress macAddress, GapDeviceAddressType addressType, infra::Duration initiatingTimeout) override;
gabrielsantosphilips marked this conversation as resolved.
Show resolved Hide resolved
void CancelConnect() override;
void Disconnect() override;
void SetAddress(hal::MacAddress macAddress, GapDeviceAddressType addressType) override;
void StartDeviceDiscovery() override;
Expand Down
19 changes: 11 additions & 8 deletions services/ble/Gap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ message State
scanning = 1;
connected = 2;
advertising = 3;
initiating = 4;
}
Event value = 1;
}
Expand Down Expand Up @@ -107,6 +108,7 @@ message ConnectionParameters
{
Address address = 1;
AddressType addressType = 2;
uint32 initiatingTimeoutInMs = 3;
}

message Passkey
Expand Down Expand Up @@ -183,14 +185,15 @@ service GapCentral
rpc StartDeviceDiscovery(Nothing) returns (Nothing) { option (method_id) = 1; }
rpc StopDeviceDiscovery(Nothing) returns (Nothing) { option (method_id) = 2; }
rpc Connect(ConnectionParameters) returns (Nothing) { option (method_id) = 3; }
rpc Disconnect(Nothing) returns (Nothing) { option (method_id) = 4; }
rpc Pair(Nothing) returns (Nothing) { option (method_id) = 5; }
rpc SetSecurityMode(SecurityModeAndLevel) returns (Nothing) { option (method_id) = 6; }
rpc SetIoCapabilities(IoCapabilities) returns (Nothing) { option (method_id) = 7; }
rpc AuthenticateWithPasskey(UInt32Value) returns (Nothing) { option (method_id) = 8; }
rpc NumericComparisonConfirm(BoolValue) returns (Nothing) { option (method_id) = 9; }
rpc RemoveAllBonds(Nothing) returns (Nothing) { option (method_id) = 10; }
rpc SetDeviceDiscoveryFilter(DeviceDiscoveryFilter) returns (Nothing) { option (method_id) = 11; }
rpc CancelConnect(Nothing) returns (Nothing) { option (method_id) = 4; }
rpc Disconnect(Nothing) returns (Nothing) { option (method_id) = 5; }
rpc Pair(Nothing) returns (Nothing) { option (method_id) = 6; }
rpc SetSecurityMode(SecurityModeAndLevel) returns (Nothing) { option (method_id) = 7; }
rpc SetIoCapabilities(IoCapabilities) returns (Nothing) { option (method_id) = 8; }
rpc AuthenticateWithPasskey(UInt32Value) returns (Nothing) { option (method_id) = 9; }
rpc NumericComparisonConfirm(BoolValue) returns (Nothing) { option (method_id) = 10; }
rpc RemoveAllBonds(Nothing) returns (Nothing) { option (method_id) = 11; }
rpc SetDeviceDiscoveryFilter(DeviceDiscoveryFilter) returns (Nothing) { option (method_id) = 12; }
}

service GapPeripheralResponse
Expand Down
21 changes: 10 additions & 11 deletions services/ble/test/TestGapCentral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#include "infra/util/test_helper/MemoryRangeMatcher.hpp"
#include "services/ble/test_doubles/GapCentralMock.hpp"
#include "services/ble/test_doubles/GapCentralObserverMock.hpp"
#include "services/ble/test_doubles/GapPeripheralMock.hpp"
#include "services/ble/test_doubles/GapPeripheralObserverMock.hpp"
#include "gmock/gmock.h"
#include <chrono>

namespace services
{
Expand Down Expand Up @@ -34,12 +33,14 @@ namespace services
TEST_F(GapCentralDecoratorTest, forward_all_state_changed_events_to_observers)
{
EXPECT_CALL(gapObserver, StateChanged(GapState::connected));
EXPECT_CALL(gapObserver, StateChanged(GapState::initiating));
EXPECT_CALL(gapObserver, StateChanged(GapState::scanning));
EXPECT_CALL(gapObserver, StateChanged(GapState::standby));

gap.NotifyObservers([](GapCentralObserver& obs)
{
obs.StateChanged(GapState::connected);
obs.StateChanged(GapState::initiating);
obs.StateChanged(GapState::scanning);
obs.StateChanged(GapState::standby);
});
Expand All @@ -61,8 +62,11 @@ namespace services
{
hal::MacAddress macAddress{ 0, 1, 2, 3, 4, 5 };

EXPECT_CALL(gap, Connect(MacAddressContentsEqual(macAddress), services::GapDeviceAddressType::publicAddress));
decorator.Connect(macAddress, services::GapDeviceAddressType::publicAddress);
EXPECT_CALL(gap, Connect(MacAddressContentsEqual(macAddress), services::GapDeviceAddressType::publicAddress, infra::Duration{ 0 }));
decorator.Connect(macAddress, services::GapDeviceAddressType::publicAddress, std::chrono::seconds(0));

EXPECT_CALL(gap, CancelConnect());
decorator.CancelConnect();

EXPECT_CALL(gap, Disconnect());
decorator.Disconnect();
Expand Down Expand Up @@ -176,13 +180,8 @@ namespace services
{
infra::StringOutputStream::WithStorage<128> stream;

services::GapState stateStandby = services::GapState::standby;
services::GapState stateScanning = services::GapState::scanning;
services::GapState stateAdvertising = services::GapState::advertising;
services::GapState stateConnected = services::GapState::connected;

stream << stateStandby << " " << stateScanning << " " << stateAdvertising << " " << stateConnected;
stream << services::GapState::standby << " " << services::GapState::scanning << " " << services::GapState::advertising << " " << services::GapState::connected << " " << services::GapState::initiating;

EXPECT_EQ("Standby Scanning Advertising Connected", stream.Storage());
EXPECT_EQ("Standby Scanning Advertising Connected Initiating", stream.Storage());
}
}
3 changes: 2 additions & 1 deletion services/ble/test_doubles/GapCentralMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace services
: public GapCentral
{
public:
MOCK_METHOD(void, Connect, (hal::MacAddress macAddress, GapDeviceAddressType addressType));
MOCK_METHOD(void, Connect, (hal::MacAddress macAddress, GapDeviceAddressType addressType, infra::Duration initiatingTimeout));
MOCK_METHOD(void, CancelConnect, ());
MOCK_METHOD(void, Disconnect, ());
MOCK_METHOD(void, SetAddress, (hal::MacAddress macAddress, GapDeviceAddressType addressType));
MOCK_METHOD(void, StartDeviceDiscovery, ());
Expand Down
Loading