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
@@ -77,6 +77,7 @@ cc_test(
":ble_v2",
"//internal/platform:base",
"//internal/platform:comm",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # buildcleaner: keep
"//proto/mediums:ble_frames_cc_proto",
@@ -18,8 +18,7 @@
#include <functional>
#include <string>
#include "connections/listeners.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/byte_array.h"
namespace nearby {
@@ -26,7 +26,7 @@
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/mutex_lock.h"
namespace nearby {
@@ -339,7 +339,6 @@ BleAdvertisementHeader DiscoveredPeripheralTracker::HandleRawGattAdvertisements(
GattAdvertisementInfo gatt_advertisement_info = {
.service_id = service_id,
.advertisement_header = new_advertisement_header,
.mac_address = peripheral.GetAddress(),
.peripheral = peripheral};
gatt_advertisement_infos_.insert_or_assign(
gatt_advertisement, std::move(gatt_advertisement_info));
@@ -127,11 +127,6 @@ class DiscoveredPeripheralTracker {
// gatt_advertisements_.
BleAdvertisementHeader advertisement_header;
// Used when we need to make a socket connection based off of the GATT
// advertisement alone. Entries are modified every time a GATT
// advertisement's advertisement header is seen.
std::string mac_address;
// A proxy BlePeripheral for found/lost disovery callback.
BleV2Peripheral peripheral;
};
@@ -20,8 +20,9 @@
#include "gtest/gtest.h"
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/mutex.h"
#include "internal/platform/mutex_lock.h"
@@ -35,7 +36,6 @@ constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
constexpr absl::string_view kFastAdvertisementServiceUuid = "FE2C";
constexpr absl::string_view kServiceIdA = "A";
constexpr absl::string_view kServiceIdB = "B";
constexpr absl::string_view kMacAddress1 = "4C:8B:1D:CE:BA:D1";
constexpr absl::string_view kData = "\x04\x02\x00";
constexpr absl::string_view kData2 = "\x07\x00\x07";
constexpr absl::string_view kDeviceToken = "\x04\x20";
@@ -101,26 +101,21 @@ ByteArray GenerateRandomAdvertisementHash() {
return random_advertisement_hash;
}
// A stub BlePeripheral implementation.
class BlePeripheralStub : public api::ble_v2::BlePeripheral {
public:
explicit BlePeripheralStub(absl::string_view mac_address) {
mac_address_ = std::string(mac_address);
}
std::string GetAddress() const override { return mac_address_; }
private:
std::string mac_address_;
};
class DiscoveredPeripheralTrackerTest : public testing::Test {
public:
void SetUp() override {}
void SetUp() override {
MediumEnvironment::Instance().Start();
adapter_peripheral_ = std::make_unique<BluetoothAdapter>();
adapter_central_ = std::make_unique<BluetoothAdapter>();
ble_peripheral_ = std::make_unique<BleV2Medium>(*adapter_peripheral_);
ble_central_ = std::make_unique<BleV2Medium>(*adapter_central_);
}
BleV2Peripheral CreateBlePeripheral(absl::string_view mac_address) {
ble_peripheral_ = std::make_unique<BlePeripheralStub>(mac_address);
return BleV2Peripheral(ble_peripheral_.get());
void TearDown() override { MediumEnvironment::Instance().Stop(); }
BleV2Peripheral CreateBlePeripheral() {
return ble_central_->GetRemotePeripheral(
adapter_peripheral_->GetMacAddress());
}
// Simulates to see a fast advertisement.
@@ -128,7 +123,7 @@ class DiscoveredPeripheralTrackerTest : public testing::Test {
const api::ble_v2::BleAdvertisementData& advertisement_data,
const std::vector<ByteArray>& advertisement_bytes_list,
CountDownLatch& fetch_latch) {
BleV2Peripheral peripheral = CreateBlePeripheral(kMacAddress1);
BleV2Peripheral peripheral = CreateBlePeripheral();
discovered_peripheral_tracker_.ProcessFoundBleAdvertisement(
peripheral, advertisement_data,
@@ -140,7 +135,7 @@ class DiscoveredPeripheralTrackerTest : public testing::Test {
const api::ble_v2::BleAdvertisementData& advertisement_data,
const std::vector<ByteArray>& advertisement_bytes_list,
CountDownLatch& fetch_latch) {
BleV2Peripheral peripheral = CreateBlePeripheral(kMacAddress1);
BleV2Peripheral peripheral = CreateBlePeripheral();
discovered_peripheral_tracker_.ProcessFoundBleAdvertisement(
peripheral, advertisement_data,
@@ -177,9 +172,12 @@ class DiscoveredPeripheralTrackerTest : public testing::Test {
};
}
std::unique_ptr<BluetoothAdapter> adapter_peripheral_;
std::unique_ptr<BluetoothAdapter> adapter_central_;
std::unique_ptr<BleV2Medium> ble_peripheral_;
std::unique_ptr<BleV2Medium> ble_central_;
mutable Mutex mutex_;
int fetch_count_ ABSL_GUARDED_BY(mutex_) = 0;
std::unique_ptr<BlePeripheralStub> ble_peripheral_;
DiscoveredPeripheralTracker discovered_peripheral_tracker_;
};
@@ -100,10 +100,9 @@ TEST_P(BleV2Test, CanConnect) {
const ByteArray& advertisement_bytes,
bool fast_advertisement) {
discovered_peripheral = peripheral;
NEARBY_LOG(
INFO,
"Discovered peripheral=%p [impl=%p], fast advertisement=%d",
&peripheral, &peripheral.GetImpl(), fast_advertisement);
NEARBY_LOG(INFO,
"Discovered peripheral=%p, fast advertisement=%d",
&peripheral, fast_advertisement);
discovered_latch.CountDown();
},
});
@@ -163,10 +162,9 @@ TEST_P(BleV2Test, CanCancelConnect) {
const ByteArray& advertisement_bytes,
bool fast_advertisement) {
discovered_peripheral = peripheral;
NEARBY_LOG(
INFO,
"Discovered peripheral=%p [impl=%p], fast advertisement=%d",
&peripheral, &peripheral.GetImpl(), fast_advertisement);
NEARBY_LOG(INFO,
"Discovered peripheral=%p, fast advertisement=%d",
&peripheral, fast_advertisement);
discovered_latch.CountDown();
},
});