Skip to content

Commit

Permalink
new solution
Browse files Browse the repository at this point in the history
  • Loading branch information
cassio-lazaro committed Jan 16, 2025
1 parent 56834c3 commit f696da4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
14 changes: 5 additions & 9 deletions services/ble/Gap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ namespace services
return GapBondingObserver::Subject().GetNumberOfBonds();
}

bool GapBondingDecorator::IsDeviceBonded(hal::MacAddress identityAddress) const
bool GapBondingDecorator::IsDeviceBonded(hal::MacAddress identityAddress, GapDeviceAddressType addressType) const
{
return GapBondingObserver::Subject().IsDeviceBonded(identityAddress);
return GapBondingObserver::Subject().IsDeviceBonded(identityAddress, addressType);
}

void GapPeripheralDecorator::StateChanged(GapState state)
Expand Down Expand Up @@ -252,16 +252,12 @@ namespace infra
return stream;
}

TextOutputStream& operator<<(TextOutputStream& stream, const services::GapAdvertisingEventAddressType& addressType)
TextOutputStream& operator<<(TextOutputStream& stream, const services::GapDeviceAddressType& addressType)
{
if (addressType == services::GapAdvertisingEventAddressType::publicDeviceAddress)
if (addressType == services::GapDeviceAddressType::publicAddress)
stream << "Public Device Address";
else if (addressType == services::GapAdvertisingEventAddressType::randomDeviceAddress)
stream << "Random Device Address";
else if (addressType == services::GapAdvertisingEventAddressType::publicIdentityAddress)
stream << "Public Identity Address";
else
stream << "Random Identity Address";
stream << "Random Device Address";

return stream;
}
Expand Down
16 changes: 4 additions & 12 deletions services/ble/Gap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ namespace services
scanResponse,
};

enum class GapAdvertisingEventAddressType : uint8_t
{
publicDeviceAddress,
randomDeviceAddress,
publicIdentityAddress,
randomIdentityAddress
};

enum class GapAdvertisementDataType : uint8_t
{
unknownType = 0x00u,
Expand All @@ -72,7 +64,7 @@ namespace services
struct GapAdvertisingReport
{
GapAdvertisingEventType eventType;
GapAdvertisingEventAddressType addressType;
GapDeviceAddressType addressType;
hal::MacAddress address;
infra::ConstByteRange data;
int8_t rssi;
Expand Down Expand Up @@ -208,7 +200,7 @@ namespace services

virtual std::size_t GetMaxNumberOfBonds() const = 0;
virtual std::size_t GetNumberOfBonds() const = 0;
virtual bool IsDeviceBonded(hal::MacAddress identityAddress) const = 0;
virtual bool IsDeviceBonded(hal::MacAddress identityAddress, GapDeviceAddressType addressType) const = 0;
};

class GapBondingDecorator
Expand All @@ -226,7 +218,7 @@ namespace services
void RemoveOldestBond() override;
std::size_t GetMaxNumberOfBonds() const override;
std::size_t GetNumberOfBonds() const override;
bool IsDeviceBonded(hal::MacAddress identityAddress) const override;
bool IsDeviceBonded(hal::MacAddress identityAddress, GapDeviceAddressType addressType) const override;
};

class GapPeripheral;
Expand Down Expand Up @@ -348,7 +340,7 @@ namespace services
namespace infra
{
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapAdvertisingEventType& eventType);
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapAdvertisingEventAddressType& addressType);
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapDeviceAddressType& addressType);
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapState& state);
}

Expand Down
13 changes: 9 additions & 4 deletions services/ble/Gap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ message AdvertisementData
bytes data = 1 [(bytes_size) = 31];
}

message ConnectionParameters
message PeerNodeParameters
{
Address address = 1;
AddressType addressType = 2;
uint32 initiatingTimeoutInMs = 3;
}

message ConnectionParameters
{
PeerNodeParameters nodeParameters = 1;
uint32 initiatingTimeoutInMs = 2;
}

message Passkey
Expand Down Expand Up @@ -195,7 +200,7 @@ service GapCentral
rpc RemoveAllBonds(Nothing) returns (Nothing) { option (method_id) = 11; }
rpc SetDeviceDiscoveryFilter(DeviceDiscoveryFilter) returns (Nothing) { option (method_id) = 12; }
rpc ResolvePrivateAddress(Address) returns (Nothing) { option (method_id) = 13; }
rpc IsDeviceBonded(Address) returns (Nothing) { option (method_id) = 14; }
rpc IsDeviceBonded(PeerNodeParameters) returns (Nothing) { option (method_id) = 14; }
}

service GapPeripheralResponse
Expand All @@ -221,5 +226,5 @@ service GapCentralResponse
rpc NumberOfBondsChanged(UInt32Value) returns (Nothing) { option (method_id) = 6; }
rpc DeviceStarted(Nothing) returns (Nothing) { option (method_id) = 7; }
rpc ResolvePrivateAddress(Address) returns (Nothing) { option (method_id) = 8; }
rpc IsDeviceBonded(Address) returns (Nothing) { option (method_id) = 9; }
rpc IsDeviceBonded(PeerNodeParameters) returns (Nothing) { option (method_id) = 9; }
}
10 changes: 6 additions & 4 deletions services/ble/test/TestGapBonding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ namespace services
EXPECT_EQ(decorator.GetNumberOfBonds(), 5);

hal::MacAddress mac = { 0x00, 0x1A, 0x7D, 0xDA, 0x71, 0x13 };
EXPECT_CALL(gapBonding, IsDeviceBonded(mac)).WillOnce(testing::Return(true));
EXPECT_THAT(decorator.IsDeviceBonded(mac), testing::IsTrue());
services::GapDeviceAddressType addressType = services::GapDeviceAddressType::randomAddress;

EXPECT_CALL(gapBonding, IsDeviceBonded(mac)).WillOnce(testing::Return(false));
EXPECT_THAT(decorator.IsDeviceBonded(mac), testing::IsFalse());
EXPECT_CALL(gapBonding, IsDeviceBonded(mac, addressType)).WillOnce(testing::Return(true));
EXPECT_THAT(decorator.IsDeviceBonded(mac, addressType), testing::IsTrue());

EXPECT_CALL(gapBonding, IsDeviceBonded(mac, addressType)).WillOnce(testing::Return(false));
EXPECT_THAT(decorator.IsDeviceBonded(mac, addressType), testing::IsFalse());
}
}
13 changes: 5 additions & 8 deletions services/ble/test/TestGapCentral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace services

TEST_F(GapCentralDecoratorTest, forward_device_discovered_event_to_observers)
{
GapAdvertisingReport deviceDiscovered{ GapAdvertisingEventType::advInd, GapAdvertisingEventAddressType::publicDeviceAddress, hal::MacAddress{ 0, 1, 2, 3, 4, 5 }, infra::ConstByteRange(), -75 };
GapAdvertisingReport deviceDiscovered{ GapAdvertisingEventType::advInd, GapDeviceAddressType::publicAddress, hal::MacAddress{ 0, 1, 2, 3, 4, 5 }, infra::ConstByteRange(), -75 };

EXPECT_CALL(gapObserver, DeviceDiscovered(ObjectContentsEqual(deviceDiscovered)));

Expand Down Expand Up @@ -173,14 +173,11 @@ namespace services
{
infra::StringOutputStream::WithStorage<128> stream;

services::GapAdvertisingEventAddressType eventAddressTypePublicDevice = services::GapAdvertisingEventAddressType::publicDeviceAddress;
services::GapAdvertisingEventAddressType eventAddressTypeRandomDevice = services::GapAdvertisingEventAddressType::randomDeviceAddress;
services::GapAdvertisingEventAddressType eventAddressTypePublicIdentity = services::GapAdvertisingEventAddressType::publicIdentityAddress;
services::GapAdvertisingEventAddressType eventAddressTypeRandomIdentity = services::GapAdvertisingEventAddressType::randomIdentityAddress;
services::GapDeviceAddressType eventAddressTypePublicDevice = services::GapDeviceAddressType::publicAddress;
services::GapDeviceAddressType eventAddressTypeRandomDevice = services::GapDeviceAddressType::randomAddress;
stream << eventAddressTypePublicDevice << " " << eventAddressTypeRandomDevice;

stream << eventAddressTypePublicDevice << " " << eventAddressTypeRandomDevice << " " << eventAddressTypePublicIdentity << " " << eventAddressTypeRandomIdentity;

EXPECT_EQ("Public Device Address Random Device Address Public Identity Address Random Identity Address", stream.Storage());
EXPECT_EQ("Public Device Address Random Device Address", stream.Storage());
}

TEST(GapInsertionOperatorStateTest, state_overload_operator)
Expand Down
2 changes: 1 addition & 1 deletion services/ble/test_doubles/GapBondingMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace services
MOCK_METHOD(void, RemoveOldestBond, ());
MOCK_METHOD(std::size_t, GetMaxNumberOfBonds, (), (const));
MOCK_METHOD(std::size_t, GetNumberOfBonds, (), (const));
MOCK_METHOD(bool, IsDeviceBonded, (hal::MacAddress identityAddress), (const));
MOCK_METHOD(bool, IsDeviceBonded, (hal::MacAddress deviceAddress, GapDeviceAddressType addressType), (const));
};
}

Expand Down

0 comments on commit f696da4

Please sign in to comment.