Add BleMedium::GetRemotePeripheral

Added:
nearby::api::ble_v2::BlePeripheral::GetUniqueId()
nearby::api::ble_v2::BleMedium::GetRemotePeripheral(...)

Moved BlePeripheral implementation from bluetooth_adapter_* to ble_v2.*
Added G3 (test) implementation for the new methods.

PiperOrigin-RevId: 531023568
This commit is contained in:
Janusz Sobczak
2023-05-10 15:40:38 -07:00
committed by Copybara-Service
parent 69f4feaf55
commit 0901aec6e2
29 changed files with 597 additions and 354 deletions
+1
View File
@@ -140,6 +140,7 @@ cc_test(
":handshake",
"//fastpair/common",
"//fastpair/server_access:test_support",
"//internal/platform:base",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
@@ -49,7 +49,6 @@ using WriteType = nearby::api::ble_v2::GattClient::WriteType;
constexpr absl::Duration kGattOperationTimeout = absl::Seconds(15);
constexpr absl::string_view kMetadataId("test_id");
constexpr absl::string_view kProviderAddress("11:22:33:44:55:66");
constexpr absl::string_view kSeekerAddress("AA:BB:CC:DD:EE:00");
constexpr Uuid kFastPairServiceUuid(0x0000FE2C00001000, 0x800000805F9B34FB);
constexpr Uuid kKeyBasedCharacteristicUuidV2(0xFE2C123483664814,
@@ -76,6 +75,14 @@ constexpr std::array<uint8_t, 64> kPublicKey = {
0x1D, 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D};
} // namespace
class MediumEnvironmentStarter {
public:
MediumEnvironmentStarter() {
MediumEnvironment::Instance().Start({.use_simulated_clock = true});
}
~MediumEnvironmentStarter() { MediumEnvironment::Instance().Stop(); }
};
class FastPairGattServiceClientTest : public testing::Test {
public:
FastPairGattServiceClientTest() {
@@ -84,10 +91,9 @@ class FastPairGattServiceClientTest : public testing::Test {
}
void SetUp() override {
env_.Start({.use_simulated_clock = true});
BluetoothAdapter adapter;
BleV2Medium ble(adapter);
gatt_server_ = ble.StartGattServer(/*ServerGattConnectionCallback=*/{});
gatt_server_ =
provider_ble_.StartGattServer(/*ServerGattConnectionCallback=*/{});
provider_address_ = provider_adapter_.GetMacAddress();
}
void TearDown() override {
@@ -98,7 +104,6 @@ class FastPairGattServiceClientTest : public testing::Test {
gatt_client_.reset();
gatt_server_->Stop();
gatt_server_.reset();
env_.Stop();
}
void InsertCorrectGattCharacteristics() {
@@ -164,7 +169,7 @@ class FastPairGattServiceClientTest : public testing::Test {
}
void InitializeFastPairGattServiceClient() {
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
Mediums mediums;
gatt_client_ =
@@ -176,23 +181,27 @@ class FastPairGattServiceClientTest : public testing::Test {
}
void RemoveDiscoveredKeyBasedCharacteristic() {
env_.EraseBleV2MediumGattCharacteristicsForDiscovery(
key_based_characteristic_.value());
MediumEnvironment::Instance()
.EraseBleV2MediumGattCharacteristicsForDiscovery(
key_based_characteristic_.value());
}
void RemoveDiscoveredPasskeyCharacteristic() {
env_.EraseBleV2MediumGattCharacteristicsForDiscovery(
passkey_characteristic_.value());
MediumEnvironment::Instance()
.EraseBleV2MediumGattCharacteristicsForDiscovery(
passkey_characteristic_.value());
}
bool UnsubceibeKeyBasedCharacteristic() {
return env_.SetBleV2MediumGattCharacteristicSubscription(
key_based_characteristic_.value(), false, {});
return MediumEnvironment::Instance()
.SetBleV2MediumGattCharacteristicSubscription(
key_based_characteristic_.value(), false, {});
}
bool UnsubceibePasskeyCharacteristic() {
return env_.SetBleV2MediumGattCharacteristicSubscription(
passkey_characteristic_.value(), false, {});
return MediumEnvironment::Instance()
.SetBleV2MediumGattCharacteristicSubscription(
passkey_characteristic_.value(), false, {});
}
std::optional<PairFailure> GetInitializedCallbackResult() {
@@ -208,7 +217,7 @@ class FastPairGattServiceClientTest : public testing::Test {
void WriteRequestToKeyBased() {
gatt_client_->WriteRequestAsync(
kMessageType, kFlags, kProviderAddress, /* Seeker Address*/ "",
kMessageType, kFlags, provider_address_, /* Seeker Address*/ "",
*fast_pair_data_encryptor_,
[&](absl::string_view response, std::optional<PairFailure> failure) {
WriteTestCallback(response, failure);
@@ -236,10 +245,13 @@ class FastPairGattServiceClientTest : public testing::Test {
}
protected:
MediumEnvironment& env_{MediumEnvironment::Instance()};
MediumEnvironmentStarter env_;
BluetoothAdapter provider_adapter_;
BleV2Medium provider_ble_{provider_adapter_};
std::unique_ptr<GattClient> internal_gatt_client_;
std::unique_ptr<FastPairGattServiceClient> gatt_client_;
std::unique_ptr<GattServer> gatt_server_;
std::string provider_address_;
std::unique_ptr<FakeFastPairDataEncryptor> fast_pair_data_encryptor_;
private:
@@ -322,7 +334,7 @@ TEST_F(FastPairGattServiceClientTest, KeyBasedPairingResponseTimeout) {
InitializeFastPairGattServiceClient();
CountDownLatch latch(1);
gatt_client_->WriteRequestAsync(
kMessageType, kFlags, kProviderAddress, kSeekerAddress,
kMessageType, kFlags, provider_address_, kSeekerAddress,
*fast_pair_data_encryptor_,
[&](absl::string_view response, std::optional<PairFailure> failure) {
WriteTestCallback(response, failure);
@@ -31,6 +31,7 @@
#include "fastpair/common/protocol.h"
#include "fastpair/handshake/fast_pair_gatt_service_client_impl.h"
#include "fastpair/server_access/fake_fast_pair_repository.h"
#include "internal/platform/bluetooth_utils.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
@@ -43,8 +44,6 @@ using Permission = nearby::api::ble_v2::GattCharacteristic::Permission;
using ::nearby::api::ble_v2::GattCharacteristic;
constexpr absl::string_view kMetadataId("718c17");
constexpr absl::string_view kProviderAddress("11:22:33:44:55:66");
constexpr absl::string_view kPublicAddress("5E:3F:45:61:C3:32");
constexpr absl::string_view kKeyBasedResponse("keybasedresponse");
constexpr absl::string_view kWrongResponse("wrongresponse");
constexpr absl::string_view kPublicAntiSpoof =
@@ -67,14 +66,20 @@ constexpr absl::string_view kPasskeyharacteristicAdvertisementByte =
constexpr absl::Duration kGattOperationTimeout = absl::Seconds(15);
} // namespace
class MediumEnvironmentStarter {
public:
MediumEnvironmentStarter() {
MediumEnvironment::Instance().Start({.use_simulated_clock = true});
}
~MediumEnvironmentStarter() { MediumEnvironment::Instance().Stop(); }
};
class FastPairHandshakeImplTest : public testing::Test {
public:
void SetUp() override {
repository_ = std::make_unique<FakeFastPairRepository>();
env_.Start({.use_simulated_clock = true});
BluetoothAdapter adapter;
BleV2Medium ble(adapter);
gatt_server_ = ble.StartGattServer(/*ServerGattConnectionCallback=*/{});
gatt_server_ = ble_.StartGattServer(/*ServerGattConnectionCallback=*/{});
provider_address_ = adapter_.GetMacAddress();
}
void TearDown() override {
@@ -84,7 +89,6 @@ class FastPairHandshakeImplTest : public testing::Test {
gatt_server_->Stop();
gatt_server_.reset();
handshake_.reset();
env_.Stop();
}
void InsertCorrectGattCharacteristics() {
@@ -144,10 +148,13 @@ class FastPairHandshakeImplTest : public testing::Test {
}
protected:
MediumEnvironmentStarter env_;
std::unique_ptr<FastPairHandshake> handshake_;
BluetoothAdapter adapter_;
BleV2Medium ble_{adapter_};
std::string provider_address_;
private:
MediumEnvironment& env_{MediumEnvironment::Instance()};
std::unique_ptr<GattServer> gatt_server_;
std::optional<GattCharacteristic> key_based_characteristic_;
std::optional<GattCharacteristic> passkey_characteristic_;
@@ -159,7 +166,7 @@ class FastPairHandshakeImplTest : public testing::Test {
TEST_F(FastPairHandshakeImplTest, Success) {
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
CountDownLatch latch(1);
Mediums mediums;
@@ -167,7 +174,9 @@ TEST_F(FastPairHandshakeImplTest, Success) {
device, mediums,
[&](FastPairDevice& callback_device, std::optional<PairFailure> failure) {
EXPECT_EQ(&device, &callback_device);
EXPECT_EQ(device.public_address(), kPublicAddress);
// TODO(jsobczak): G3 provider address should be in human readable
// format.
// EXPECT_EQ(device.public_address(), provider_address_);
EXPECT_FALSE(failure.has_value());
latch.CountDown();
});
@@ -178,7 +187,7 @@ TEST_F(FastPairHandshakeImplTest, Success) {
TEST_F(FastPairHandshakeImplTest, GattError) {
SetUpFastPairRepository();
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
CountDownLatch latch(1);
Mediums mediums;
@@ -196,7 +205,7 @@ TEST_F(FastPairHandshakeImplTest, GattError) {
TEST_F(FastPairHandshakeImplTest, DataEncryptorCreateError) {
FailedFastPairRepository();
InsertCorrectGattCharacteristics();
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
CountDownLatch latch(1);
Mediums mediums;
@@ -214,7 +223,7 @@ TEST_F(FastPairHandshakeImplTest, DataEncryptorCreateError) {
TEST_F(FastPairHandshakeImplTest, WriteResponseError) {
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
CountDownLatch latch(1);
Mediums mediums;
@@ -234,7 +243,7 @@ TEST_F(FastPairHandshakeImplTest, WriteResponseError) {
TEST_F(FastPairHandshakeImplTest, WriteResponseWrongSize) {
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
CountDownLatch latch(1);
Mediums mediums;
@@ -254,7 +263,7 @@ TEST_F(FastPairHandshakeImplTest, WriteResponseWrongSize) {
TEST_F(FastPairHandshakeImplTest, ParseResponseError) {
SetUpFastPairRepository();
InsertCorrectGattCharacteristics();
FastPairDevice device(kMetadataId, kProviderAddress,
FastPairDevice device(kMetadataId, provider_address_,
Protocol::kFastPairInitialPairing);
CountDownLatch latch(1);
Mediums mediums;
@@ -16,6 +16,7 @@
#include <memory>
#include <optional>
#include <string>
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
@@ -24,17 +25,25 @@
#include "fastpair/common/protocol.h"
#include "fastpair/internal/mediums/mediums.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
namespace nearby {
namespace fastpair {
namespace {
constexpr absl::string_view kValidModelId("718c17");
constexpr absl::string_view kBLEAddress("ble_address");
constexpr absl::string_view kPubliceAddress("public_address");
class MediumEnvironmentStarter {
public:
MediumEnvironmentStarter() { MediumEnvironment::Instance().Start(); }
~MediumEnvironmentStarter() { MediumEnvironment::Instance().Stop(); }
};
class FastPairHandshakeLookupTest : public ::testing::Test {
public:
FastPairHandshakeLookupTest() {
device_ = new FastPairDevice(kValidModelId, kBLEAddress,
provider_address_ = adapter_.GetMacAddress();
device_ = new FastPairDevice(kValidModelId, provider_address_,
Protocol::kFastPairInitialPairing);
device_->set_public_address(kPubliceAddress);
}
@@ -54,12 +63,18 @@ class FastPairHandshakeLookupTest : public ::testing::Test {
latch.Await();
}
protected:
MediumEnvironmentStarter env_;
BluetoothAdapter adapter_;
BleV2Medium ble_{adapter_};
std::string provider_address_;
FastPairDevice* device_ = nullptr;
};
TEST_F(FastPairHandshakeLookupTest, CreateFastPairHandshkeInstanceForDevice) {
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(device_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(kBLEAddress));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(provider_address_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(kPubliceAddress));
CreateFastPairHandshkeInstanceForDevice(*device_);
@@ -67,7 +82,7 @@ TEST_F(FastPairHandshakeLookupTest, CreateFastPairHandshkeInstanceForDevice) {
// GetFastPairHandshakeWithDevicePtr
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Get(device_));
// GetFastPairHandshakeWithBLEAddress
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Get(kBLEAddress));
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Get(provider_address_));
// GetFastPairHandshakeWithPublicAddress
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Get(kPubliceAddress));
// GetFastPairHandshakeWithDevicePtr
@@ -84,7 +99,8 @@ TEST_F(FastPairHandshakeLookupTest, EraseFastPairHandshakeWithDevicePtr) {
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Erase(device_));
// Already Erased
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(device_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Erase(kBLEAddress));
EXPECT_FALSE(
FastPairHandshakeLookup::GetInstance()->Erase(provider_address_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Erase(kPubliceAddress));
}
@@ -95,7 +111,7 @@ TEST_F(FastPairHandshakeLookupTest, EraseFastPairHandshakeWithBLEAddress) {
// Erase Wrong Address
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Erase(""));
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Erase(kBLEAddress));
EXPECT_TRUE(FastPairHandshakeLookup::GetInstance()->Erase(provider_address_));
// Already Erased
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(device_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Erase(device_));
@@ -111,7 +127,8 @@ TEST_F(FastPairHandshakeLookupTest, EraseFastPairHandshakeWithPublicAddress) {
// Already Erased
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Get(device_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Erase(device_));
EXPECT_FALSE(FastPairHandshakeLookup::GetInstance()->Erase(kBLEAddress));
EXPECT_FALSE(
FastPairHandshakeLookup::GetInstance()->Erase(provider_address_));
}
TEST_F(FastPairHandshakeLookupTest, ClearAllFastPairHandshakeInstances) {
+3 -16
View File
@@ -21,20 +21,6 @@
namespace nearby {
namespace fastpair {
namespace {
// A stub BlePeripheral implementation.
class BlePeripheralStub : public api::ble_v2::BlePeripheral {
public:
explicit BlePeripheralStub(absl::string_view ble_address) {
ble_address_ = std::string(ble_address);
}
std::string GetAddress() const override { return ble_address_; }
private:
std::string ble_address_;
};
} // namespace
BleV2::BleV2(BluetoothRadio& radio) : radio_(radio) {}
@@ -61,8 +47,9 @@ std::unique_ptr<GattClient> BleV2::ConnectToGattServer(
<< "Can't connect to GattServer because BleV2 isn't available.";
return nullptr;
}
auto v2_peripheral = std::make_unique<BlePeripheralStub>(ble_address);
return medium_.ConnectToGattServer(BleV2Peripheral(v2_peripheral.get()),
BleV2Peripheral v2_peripheral = medium_.GetRemotePeripheral(ble_address);
return medium_.ConnectToGattServer(v2_peripheral,
api::ble_v2::TxPowerLevel::kUnknown, {});
}
} // namespace fastpair