Skip to content

Commit 3b8d1a4

Browse files
feat: add support to resolve MAC address (#792)
* Add support to resolve MAC address Return the resolved MAC address instead of the random public one * Changes after sync up with Oguzcan * fix code review * new solution * fix code review * Add support to return resolve MAC address Return the resolved MAC address instead of the random one
1 parent c4c26e0 commit 3b8d1a4

File tree

7 files changed

+57
-29
lines changed

7 files changed

+57
-29
lines changed

services/ble/Gap.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ namespace services
8484
return GapBondingObserver::Subject().GetNumberOfBonds();
8585
}
8686

87+
bool GapBondingDecorator::IsDeviceBonded(hal::MacAddress address, GapDeviceAddressType addressType) const
88+
{
89+
return GapBondingObserver::Subject().IsDeviceBonded(address, addressType);
90+
}
91+
8792
void GapPeripheralDecorator::StateChanged(GapState state)
8893
{
8994
GapPeripheral::NotifyObservers([&state](auto& obs)
@@ -178,6 +183,11 @@ namespace services
178183
GapCentralObserver::Subject().StopDeviceDiscovery();
179184
}
180185

186+
infra::Optional<hal::MacAddress> GapCentralDecorator::ResolvePrivateAddress(hal::MacAddress address) const
187+
{
188+
return GapCentralObserver::Subject().ResolvePrivateAddress(address);
189+
}
190+
181191
GapAdvertisingDataParser::GapAdvertisingDataParser(infra::ConstByteRange data)
182192
: data(data)
183193
{}
@@ -242,16 +252,12 @@ namespace infra
242252
return stream;
243253
}
244254

245-
TextOutputStream& operator<<(TextOutputStream& stream, const services::GapAdvertisingEventAddressType& addressType)
255+
TextOutputStream& operator<<(TextOutputStream& stream, const services::GapDeviceAddressType& addressType)
246256
{
247-
if (addressType == services::GapAdvertisingEventAddressType::publicDeviceAddress)
257+
if (addressType == services::GapDeviceAddressType::publicAddress)
248258
stream << "Public Device Address";
249-
else if (addressType == services::GapAdvertisingEventAddressType::randomDeviceAddress)
250-
stream << "Random Device Address";
251-
else if (addressType == services::GapAdvertisingEventAddressType::publicIdentityAddress)
252-
stream << "Public Identity Address";
253259
else
254-
stream << "Random Identity Address";
260+
stream << "Random Device Address";
255261

256262
return stream;
257263
}

services/ble/Gap.hpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "infra/util/ByteRange.hpp"
88
#include "infra/util/EnumCast.hpp"
99
#include "infra/util/Observer.hpp"
10-
#include <array>
10+
#include "infra/util/Optional.hpp"
1111

1212
namespace services
1313
{
@@ -41,14 +41,6 @@ namespace services
4141
scanResponse,
4242
};
4343

44-
enum class GapAdvertisingEventAddressType : uint8_t
45-
{
46-
publicDeviceAddress,
47-
randomDeviceAddress,
48-
publicIdentityAddress,
49-
randomIdentityAddress
50-
};
51-
5244
enum class GapAdvertisementDataType : uint8_t
5345
{
5446
unknownType = 0x00u,
@@ -201,6 +193,7 @@ namespace services
201193

202194
virtual std::size_t GetMaxNumberOfBonds() const = 0;
203195
virtual std::size_t GetNumberOfBonds() const = 0;
196+
virtual bool IsDeviceBonded(hal::MacAddress address, GapDeviceAddressType addressType) const = 0;
204197
};
205198

206199
class GapBondingDecorator
@@ -218,6 +211,7 @@ namespace services
218211
void RemoveOldestBond() override;
219212
std::size_t GetMaxNumberOfBonds() const override;
220213
std::size_t GetNumberOfBonds() const override;
214+
bool IsDeviceBonded(hal::MacAddress address, GapDeviceAddressType addressType) const override;
221215
};
222216

223217
class GapPeripheral;
@@ -292,7 +286,7 @@ namespace services
292286
struct GapAdvertisingReport
293287
{
294288
GapAdvertisingEventType eventType;
295-
GapAdvertisingEventAddressType addressType;
289+
GapDeviceAddressType addressType;
296290
hal::MacAddress address;
297291
infra::BoundedVector<uint8_t>::WithMaxSize<GapPeripheral::maxAdvertisementDataSize> data;
298292
int32_t rssi;
@@ -320,6 +314,7 @@ namespace services
320314
virtual void SetAddress(hal::MacAddress macAddress, GapDeviceAddressType addressType) = 0;
321315
virtual void StartDeviceDiscovery() = 0;
322316
virtual void StopDeviceDiscovery() = 0;
317+
virtual infra::Optional<hal::MacAddress> ResolvePrivateAddress(hal::MacAddress address) const = 0;
323318
};
324319

325320
class GapCentralDecorator
@@ -340,13 +335,14 @@ namespace services
340335
void SetAddress(hal::MacAddress macAddress, GapDeviceAddressType addressType) override;
341336
void StartDeviceDiscovery() override;
342337
void StopDeviceDiscovery() override;
338+
infra::Optional<hal::MacAddress> ResolvePrivateAddress(hal::MacAddress address) const override;
343339
};
344340
}
345341

346342
namespace infra
347343
{
348344
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapAdvertisingEventType& eventType);
349-
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapAdvertisingEventAddressType& addressType);
345+
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapDeviceAddressType& addressType);
350346
infra::TextOutputStream& operator<<(infra::TextOutputStream& stream, const services::GapState& state);
351347
}
352348

services/ble/Gap.proto

+14-2
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,16 @@ message AdvertisementData
112112
bytes data = 1 [(bytes_size) = 31];
113113
}
114114

115-
message ConnectionParameters
115+
message PeerNodeParameters
116116
{
117117
Address address = 1;
118118
AddressType addressType = 2;
119-
uint32 initiatingTimeoutInMs = 3;
119+
}
120+
121+
message ConnectionParameters
122+
{
123+
PeerNodeParameters peerParameters = 1;
124+
uint32 initiatingTimeoutInMs = 2;
120125
}
121126

122127
message Passkey
@@ -193,6 +198,7 @@ service GapPeripheral
193198

194199
// Allowed states: advertising
195200
rpc GetResolvableAddress(Nothing) returns (Nothing) { option (method_id) = 9; }
201+
rpc GetIdentityAddress(Nothing) returns (Nothing) { option (method_id) = 10; }
196202
}
197203

198204
service GapCentral
@@ -225,6 +231,8 @@ service GapCentral
225231
rpc NumericComparisonConfirm(BoolValue) returns (Nothing) { option (method_id) = 10; }
226232
rpc RemoveAllBonds(Nothing) returns (Nothing) { option (method_id) = 11; }
227233
rpc SetDeviceDiscoveryFilter(DeviceDiscoveryFilter) returns (Nothing) { option (method_id) = 12; }
234+
rpc ResolvePrivateAddress(Address) returns (Nothing) { option (method_id) = 13; }
235+
rpc IsDeviceBonded(PeerNodeParameters) returns (Nothing) { option (method_id) = 14; }
228236
}
229237

230238
service GapPeripheralResponse
@@ -236,6 +244,7 @@ service GapPeripheralResponse
236244
rpc StateChanged(State) returns (Nothing) { option (method_id) = 3; }
237245
rpc ResolvableAddress(Address) returns (Nothing) { option (method_id) = 4; }
238246
rpc DisplayPasskey(Passkey) returns (Nothing) { option (method_id) = 5; }
247+
rpc IdentityAddress(Address) returns (Nothing) { option (method_id) = 6; }
239248
}
240249

241250
service GapCentralResponse
@@ -249,4 +258,7 @@ service GapCentralResponse
249258
rpc PairingFailed(PairingStatus) returns (Nothing) { option (method_id) = 5; }
250259
rpc NumberOfBondsChanged(UInt32Value) returns (Nothing) { option (method_id) = 6; }
251260
rpc DeviceStarted(Nothing) returns (Nothing) { option (method_id) = 7; }
261+
rpc ResolvedPrivateAddress(Address) returns (Nothing) { option (method_id) = 8; }
262+
rpc ResolvePrivateAddressFailed(Nothing) returns (Nothing) { option (method_id) = 9; }
263+
rpc DeviceBonded(BoolValue) returns (Nothing) { option (method_id) = 10; }
252264
}

services/ble/test/TestGapBonding.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,14 @@ namespace services
4141

4242
EXPECT_CALL(gapBonding, GetNumberOfBonds()).WillOnce(testing::Return(5));
4343
EXPECT_EQ(decorator.GetNumberOfBonds(), 5);
44+
45+
hal::MacAddress mac = { 0x00, 0x1A, 0x7D, 0xDA, 0x71, 0x13 };
46+
services::GapDeviceAddressType addressType = services::GapDeviceAddressType::randomAddress;
47+
48+
EXPECT_CALL(gapBonding, IsDeviceBonded(mac, addressType)).WillOnce(testing::Return(true));
49+
EXPECT_THAT(decorator.IsDeviceBonded(mac, addressType), testing::IsTrue());
50+
51+
EXPECT_CALL(gapBonding, IsDeviceBonded(mac, addressType)).WillOnce(testing::Return(false));
52+
EXPECT_THAT(decorator.IsDeviceBonded(mac, addressType), testing::IsFalse());
4453
}
4554
}

services/ble/test/TestGapCentral.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "services/ble/test_doubles/GapCentralMock.hpp"
66
#include "services/ble/test_doubles/GapCentralObserverMock.hpp"
77
#include "gmock/gmock.h"
8-
#include <chrono>
98

109
namespace services
1110
{
@@ -49,7 +48,7 @@ namespace services
4948

5049
TEST_F(GapCentralDecoratorTest, forward_device_discovered_event_to_observers)
5150
{
52-
GapAdvertisingReport deviceDiscovered{ GapAdvertisingEventType::advInd, GapAdvertisingEventAddressType::publicDeviceAddress, hal::MacAddress{ 0, 1, 2, 3, 4, 5 }, infra::BoundedVector<uint8_t>::WithMaxSize<GapPeripheral::maxAdvertisementDataSize>{}, -75 };
51+
GapAdvertisingReport deviceDiscovered{ GapAdvertisingEventType::advInd, GapDeviceAddressType::publicAddress, hal::MacAddress{ 0, 1, 2, 3, 4, 5 }, infra::BoundedVector<uint8_t>::WithMaxSize<GapPeripheral::maxAdvertisementDataSize>{}, -75 };
5352

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

@@ -80,6 +79,13 @@ namespace services
8079

8180
EXPECT_CALL(gap, StopDeviceDiscovery());
8281
decorator.StopDeviceDiscovery();
82+
83+
hal::MacAddress mac = { 0x00, 0x1A, 0x7D, 0xDA, 0x71, 0x13 };
84+
EXPECT_CALL(gap, ResolvePrivateAddress(mac)).WillOnce(testing::Return(infra::none));
85+
EXPECT_EQ(decorator.ResolvePrivateAddress(mac), infra::none);
86+
87+
EXPECT_CALL(gap, ResolvePrivateAddress(mac)).WillOnce(testing::Return(infra::MakeOptional(mac)));
88+
EXPECT_EQ(decorator.ResolvePrivateAddress(mac), mac);
8389
}
8490

8591
TEST(GapAdvertisingDataParserTest, payload_too_small)
@@ -167,14 +173,11 @@ namespace services
167173
{
168174
infra::StringOutputStream::WithStorage<128> stream;
169175

170-
services::GapAdvertisingEventAddressType eventAddressTypePublicDevice = services::GapAdvertisingEventAddressType::publicDeviceAddress;
171-
services::GapAdvertisingEventAddressType eventAddressTypeRandomDevice = services::GapAdvertisingEventAddressType::randomDeviceAddress;
172-
services::GapAdvertisingEventAddressType eventAddressTypePublicIdentity = services::GapAdvertisingEventAddressType::publicIdentityAddress;
173-
services::GapAdvertisingEventAddressType eventAddressTypeRandomIdentity = services::GapAdvertisingEventAddressType::randomIdentityAddress;
174-
175-
stream << eventAddressTypePublicDevice << " " << eventAddressTypeRandomDevice << " " << eventAddressTypePublicIdentity << " " << eventAddressTypeRandomIdentity;
176+
services::GapDeviceAddressType eventAddressTypePublicDevice = services::GapDeviceAddressType::publicAddress;
177+
services::GapDeviceAddressType eventAddressTypeRandomDevice = services::GapDeviceAddressType::randomAddress;
178+
stream << eventAddressTypePublicDevice << " " << eventAddressTypeRandomDevice;
176179

177-
EXPECT_EQ("Public Device Address Random Device Address Public Identity Address Random Identity Address", stream.Storage());
180+
EXPECT_EQ("Public Device Address Random Device Address", stream.Storage());
178181
}
179182

180183
TEST(GapInsertionOperatorStateTest, state_overload_operator)

services/ble/test_doubles/GapBondingMock.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace services
1414
MOCK_METHOD(void, RemoveOldestBond, ());
1515
MOCK_METHOD(std::size_t, GetMaxNumberOfBonds, (), (const));
1616
MOCK_METHOD(std::size_t, GetNumberOfBonds, (), (const));
17+
MOCK_METHOD(bool, IsDeviceBonded, (hal::MacAddress deviceAddress, GapDeviceAddressType addressType), (const));
1718
};
1819
}
1920

services/ble/test_doubles/GapCentralMock.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace services
1616
MOCK_METHOD(void, SetAddress, (hal::MacAddress macAddress, GapDeviceAddressType addressType));
1717
MOCK_METHOD(void, StartDeviceDiscovery, ());
1818
MOCK_METHOD(void, StopDeviceDiscovery, ());
19+
MOCK_METHOD(infra::Optional<hal::MacAddress>, ResolvePrivateAddress, (hal::MacAddress address), (const));
1920
};
2021
}
2122

0 commit comments

Comments
 (0)