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 support to resolve MAC address #792

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion protobuf/echo/ProtoMessageReceiver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace services
Message message;

private:
infra::BoundedVector<std::pair<uint32_t, infra::Function<void(const infra::DataInputStream& stream)>>>::WithMaxSize<MessageDepth<services::ProtoMessage<Message>>::value + 1> stack{ { std::pair<uint32_t, infra::Function<void(const infra::DataInputStream& stream)>>{ std::numeric_limits<uint32_t>::max(), [this](const infra::DataInputStream& stream)
infra::BoundedVector<std::pair<uint32_t, infra::Function<void(const infra::DataInputStream& stream)>>>::WithMaxSize<MessageDepth<services::ProtoMessage<Message>>::value + 2> stack{ { std::pair<uint32_t, infra::Function<void(const infra::DataInputStream& stream)>>{ std::numeric_limits<uint32_t>::max(), [this](const infra::DataInputStream& stream)
{
FeedForMessage(stream, message);
} } } };
Expand Down
2 changes: 1 addition & 1 deletion protobuf/echo/ProtoMessageSender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace services

private:
const Message& message;
infra::BoundedVector<std::pair<uint32_t, infra::Function<bool(infra::DataOutputStream& stream, uint32_t& index, bool& retry, const infra::StreamWriter& finalWriter), 3 * sizeof(uint8_t*)>>>::WithMaxSize<MessageDepth<services::ProtoMessage<Message>>::value + 1> stack{ { std::pair<uint32_t, infra::Function<bool(infra::DataOutputStream& stream, uint32_t& index, bool& retry, const infra::StreamWriter& finalWriter), 3 * sizeof(uint8_t*)>>{ 0, [this](infra::DataOutputStream& stream, uint32_t& index, bool& retry, const infra::StreamWriter& finalWriter)
infra::BoundedVector<std::pair<uint32_t, infra::Function<bool(infra::DataOutputStream& stream, uint32_t& index, bool& retry, const infra::StreamWriter& finalWriter), 3 * sizeof(uint8_t*)>>>::WithMaxSize<MessageDepth<services::ProtoMessage<Message>>::value + 2> stack{ { std::pair<uint32_t, infra::Function<bool(infra::DataOutputStream& stream, uint32_t& index, bool& retry, const infra::StreamWriter& finalWriter), 3 * sizeof(uint8_t*)>>{ 0, [this](infra::DataOutputStream& stream, uint32_t& index, bool& retry, const infra::StreamWriter& finalWriter)
{
return FillForMessage(stream, message, index, retry, finalWriter);
} } } };
Expand Down
9 changes: 9 additions & 0 deletions protobuf/echo/TracingEcho.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ namespace services
tracer.Continue() << "]";
}

template<class T>
void PrintField(const infra::Optional<T>& value, services::Tracer& tracer)
{
if (value)
PrintField(*value, tracer);
else
tracer.Continue() << "null";
}

namespace detail
{
class TracingEchoOnStreamsDescendantHelper
Expand Down
20 changes: 13 additions & 7 deletions services/ble/Gap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ namespace services
return GapBondingObserver::Subject().GetNumberOfBonds();
}

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

void GapPeripheralDecorator::StateChanged(GapState state)
{
GapPeripheral::NotifyObservers([&state](auto& obs)
Expand Down Expand Up @@ -178,6 +183,11 @@ namespace services
GapCentralObserver::Subject().StopDeviceDiscovery();
}

infra::Optional<hal::MacAddress> GapCentralDecorator::ResolvePrivateAddress(hal::MacAddress address) const
{
return GapCentralObserver::Subject().ResolvePrivateAddress(address);
}

GapAdvertisingDataParser::GapAdvertisingDataParser(infra::ConstByteRange data)
: data(data)
{}
Expand Down Expand Up @@ -242,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
17 changes: 7 additions & 10 deletions services/ble/Gap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "infra/timer/Timer.hpp"
#include "infra/util/EnumCast.hpp"
#include "infra/util/Observer.hpp"
#include "infra/util/Optional.hpp"

namespace services
{
Expand Down Expand Up @@ -38,14 +39,6 @@ namespace services
scanResponse,
};

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

enum class GapAdvertisementDataType : uint8_t
{
unknownType = 0x00u,
Expand All @@ -71,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 @@ -207,6 +200,7 @@ namespace services

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

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

class GapPeripheral;
Expand Down Expand Up @@ -317,6 +312,7 @@ namespace services
virtual void SetAddress(hal::MacAddress macAddress, GapDeviceAddressType addressType) = 0;
virtual void StartDeviceDiscovery() = 0;
virtual void StopDeviceDiscovery() = 0;
virtual infra::Optional<hal::MacAddress> ResolvePrivateAddress(hal::MacAddress address) const = 0;
};

class GapCentralDecorator
Expand All @@ -337,13 +333,14 @@ namespace services
void SetAddress(hal::MacAddress macAddress, GapDeviceAddressType addressType) override;
void StartDeviceDiscovery() override;
void StopDeviceDiscovery() override;
infra::Optional<hal::MacAddress> ResolvePrivateAddress(hal::MacAddress address) const override;
};
}

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
18 changes: 16 additions & 2 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 peerParameters = 1;
uint32 initiatingTimeoutInMs = 2;
}

message Passkey
Expand Down Expand Up @@ -163,6 +168,11 @@ message UInt32Value
uint32 value = 1;
}

message OptionalAddress
{
optional Address address = 1;
}

service GapPeripheral
{
option (service_id) = 32;
Expand Down Expand Up @@ -194,6 +204,8 @@ service GapCentral
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; }
rpc ResolvePrivateAddress(Address) returns (Nothing) { option (method_id) = 13; }
rpc IsDeviceBonded(PeerNodeParameters) returns (Nothing) { option (method_id) = 14; }
}

service GapPeripheralResponse
Expand All @@ -218,4 +230,6 @@ service GapCentralResponse
rpc PairingFailed(PairingStatus) returns (Nothing) { option (method_id) = 5; }
rpc NumberOfBondsChanged(UInt32Value) returns (Nothing) { option (method_id) = 6; }
rpc DeviceStarted(Nothing) returns (Nothing) { option (method_id) = 7; }
rpc ResolvedPrivateAddress(OptionalAddress) returns (Nothing) { option (method_id) = 8; }
rpc DeviceBonded(BoolValue) returns (Nothing) { option (method_id) = 9; }
}
9 changes: 9 additions & 0 deletions services/ble/test/TestGapBonding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,14 @@ namespace services

EXPECT_CALL(gapBonding, GetNumberOfBonds()).WillOnce(testing::Return(5));
EXPECT_EQ(decorator.GetNumberOfBonds(), 5);

hal::MacAddress mac = { 0x00, 0x1A, 0x7D, 0xDA, 0x71, 0x13 };
services::GapDeviceAddressType addressType = services::GapDeviceAddressType::randomAddress;

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());
}
}
20 changes: 12 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 @@ -79,6 +79,13 @@ namespace services

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

hal::MacAddress mac = { 0x00, 0x1A, 0x7D, 0xDA, 0x71, 0x13 };
EXPECT_CALL(gap, ResolvePrivateAddress(mac)).WillOnce(testing::Return(infra::none));
EXPECT_EQ(decorator.ResolvePrivateAddress(mac), infra::none);

EXPECT_CALL(gap, ResolvePrivateAddress(mac)).WillOnce(testing::Return(infra::MakeOptional(mac)));
EXPECT_EQ(decorator.ResolvePrivateAddress(mac), mac);
}

TEST(GapAdvertisingDataParserTest, payload_too_small)
Expand Down Expand Up @@ -166,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;

stream << eventAddressTypePublicDevice << " " << eventAddressTypeRandomDevice << " " << eventAddressTypePublicIdentity << " " << eventAddressTypeRandomIdentity;
services::GapDeviceAddressType eventAddressTypePublicDevice = services::GapDeviceAddressType::publicAddress;
services::GapDeviceAddressType eventAddressTypeRandomDevice = services::GapDeviceAddressType::randomAddress;
stream << eventAddressTypePublicDevice << " " << eventAddressTypeRandomDevice;

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
1 change: 1 addition & 0 deletions services/ble/test_doubles/GapBondingMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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 deviceAddress, GapDeviceAddressType addressType), (const));
};
}

Expand Down
1 change: 1 addition & 0 deletions services/ble/test_doubles/GapCentralMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace services
MOCK_METHOD(void, SetAddress, (hal::MacAddress macAddress, GapDeviceAddressType addressType));
MOCK_METHOD(void, StartDeviceDiscovery, ());
MOCK_METHOD(void, StopDeviceDiscovery, ());
MOCK_METHOD(infra::Optional<hal::MacAddress>, ResolvePrivateAddress, (hal::MacAddress address), (const));
};
}

Expand Down
Loading