From 59e527dbe3326f52bce1bda3dc30e997a68f2443 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Wed, 7 May 2025 16:34:55 -0700 Subject: [PATCH] Remove unused methods. PiperOrigin-RevId: 756053350 --- connections/implementation/mediums/ble_v2.cc | 2 +- .../implementation/mediums/ble_v2/BUILD | 1 + .../ble_v2/discovered_peripheral_tracker.cc | 3 +- .../ble_v2/discovered_peripheral_tracker.h | 1 - .../discovered_peripheral_tracker_test.cc | 13 +- .../implementation/mediums/ble_v2_test.cc | 10 +- internal/platform/BUILD | 3 +- internal/platform/ble_v2.cc | 25 +-- internal/platform/ble_v2.h | 22 +-- internal/platform/ble_v2_test.cc | 160 +++--------------- .../implementation/apple/ble_medium.h | 6 - .../implementation/apple/ble_medium.mm | 7 - internal/platform/implementation/ble_v2.h | 10 +- internal/platform/implementation/g3/ble_v2.cc | 30 ---- internal/platform/implementation/g3/ble_v2.h | 3 - .../platform/implementation/windows/ble_v2.cc | 20 --- .../platform/implementation/windows/ble_v2.h | 4 - 17 files changed, 57 insertions(+), 263 deletions(-) diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble_v2.cc index 29334cde..b7bf828d 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble_v2.cc @@ -1382,7 +1382,7 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id, [this](api::ble_v2::BlePeripheral& peripheral, BleAdvertisementData advertisement_data) { AssumeHeld(mutex_); - BleV2Peripheral proxy(medium_, peripheral); + BleV2Peripheral proxy(medium_, peripheral.GetUniqueId()); RunOnBleThread([this, proxy = std::move(proxy), advertisement_data]() { MutexLock lock(&mutex_); diff --git a/connections/implementation/mediums/ble_v2/BUILD b/connections/implementation/mediums/ble_v2/BUILD index 77b1d020..3eab5958 100644 --- a/connections/implementation/mediums/ble_v2/BUILD +++ b/connections/implementation/mediums/ble_v2/BUILD @@ -132,6 +132,7 @@ cc_test( "//internal/flags:nearby_flags", "//internal/platform:base", "//internal/platform:comm", + "//internal/platform:mac_address", "//internal/platform:test_util", "//internal/platform:types", "//internal/platform:uuid", diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc b/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc index e94e0453..9ed97989 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc +++ b/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc @@ -143,7 +143,7 @@ void DiscoveredPeripheralTracker::ProcessFoundBleAdvertisement( return; } - if (HandleOnLostAdvertisementLocked(peripheral, advertisement_data)) { + if (HandleOnLostAdvertisementLocked(advertisement_data)) { return; } @@ -181,7 +181,6 @@ void DiscoveredPeripheralTracker::ProcessFoundBleAdvertisement( } bool DiscoveredPeripheralTracker::HandleOnLostAdvertisementLocked( - BleV2Peripheral peripheral, const BleAdvertisementData& advertisement_data) { auto service_data = advertisement_data.service_data.find(bleutils::kCopresenceServiceUuid); diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h b/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h index d634bd08..3792382e 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h +++ b/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h @@ -268,7 +268,6 @@ class DiscoveredPeripheralTracker { // 2. Matches a peripheral's advertisement hash that has previously been // discovered. bool HandleOnLostAdvertisementLocked( - BleV2Peripheral peripheral, const ::nearby::api::ble_v2::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc b/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc index 4053607e..2340e37a 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc +++ b/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc @@ -15,6 +15,7 @@ #include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" #include +#include #include #include #include @@ -44,6 +45,7 @@ #include "internal/platform/count_down_latch.h" #include "internal/platform/feature_flags.h" #include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" #include "internal/platform/mutex.h" #include "internal/platform/mutex_lock.h" @@ -186,8 +188,15 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam { } BleV2Peripheral CreateBlePeripheral() { - return ble_central_->GetRemotePeripheral( - adapter_peripheral_->GetMacAddress()); + MacAddress mac_address; + MacAddress::FromString(adapter_peripheral_->GetMacAddress(), mac_address); + api::ble_v2::BlePeripheral api_peripheral; + ble_central_->GetImpl()->GetRemotePeripheral( + mac_address.address(), + [&api_peripheral](api::ble_v2::BlePeripheral& peripheral) { + api_peripheral = peripheral; + }); + return BleV2Peripheral(*ble_central_, api_peripheral.GetUniqueId()); } // Simulates to see a fast advertisement. diff --git a/connections/implementation/mediums/ble_v2_test.cc b/connections/implementation/mediums/ble_v2_test.cc index 7b81f268..5256b3cb 100644 --- a/connections/implementation/mediums/ble_v2_test.cc +++ b/connections/implementation/mediums/ble_v2_test.cc @@ -113,9 +113,8 @@ TEST_P(BleV2Test, CanConnect) { bool fast_advertisement) { discovered_peripheral = peripheral; NEARBY_LOGS(INFO) - << "Discovered peripheral=" - << peripheral.GetAddress().value_or("") - << ", fast advertisement=" << fast_advertisement; + << "Discovered peripheral, fast advertisement=" + << fast_advertisement; discovered_latch.CountDown(); }, }); @@ -174,9 +173,8 @@ TEST_P(BleV2Test, CanCancelConnect) { bool fast_advertisement) { discovered_peripheral = peripheral; NEARBY_LOGS(INFO) - << "Discovered peripheral=" - << peripheral.GetAddress().value_or("") - << ", fast advertisement=" << fast_advertisement; + << "Discovered peripheral, fast advertisement=" + << fast_advertisement; discovered_latch.CountDown(); }, }); diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 0e136113..f03a249d 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -494,11 +494,12 @@ cc_test( ":cancellation_flag", ":comm", ":connection_info", + ":mac_address", ":test_util", ":types", + ":uuid", "//connections/implementation/flags:connections_flags", "//internal/flags:nearby_flags", - "//internal/platform:uuid", "//internal/platform/implementation:comm", "//internal/platform/implementation/g3", # build_cleaner: keep "//proto:connections_enums_cc_proto", diff --git a/internal/platform/ble_v2.cc b/internal/platform/ble_v2.cc index c10debe2..72ce304c 100644 --- a/internal/platform/ble_v2.cc +++ b/internal/platform/ble_v2.cc @@ -21,7 +21,7 @@ #include "absl/functional/any_invocable.h" #include "absl/status/status.h" -#include "absl/types/optional.h" +#include "absl/strings/string_view.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/implementation/ble_v2.h" #include "internal/platform/logging.h" @@ -78,7 +78,7 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, peripherals_.insert(&peripheral); } - BleV2Peripheral proxy(*this, peripheral); + BleV2Peripheral proxy(*this, peripheral.GetUniqueId()); if (!scanning_enabled_) return; scan_callback_.advertisement_found_cb(std::move(proxy), advertisement_data); @@ -119,7 +119,7 @@ bool BleV2Medium::StartMultipleServicesScanning( peripherals_.insert(&peripheral); } - BleV2Peripheral proxy(*this, peripheral); + BleV2Peripheral proxy(*this, peripheral.GetUniqueId()); if (!scanning_enabled_) return; scan_callback_.advertisement_found_cb(std::move(proxy), advertisement_data); @@ -302,25 +302,6 @@ bool BleV2Medium::IsExtendedAdvertisementsAvailable() { return IsValid() && impl_->IsExtendedAdvertisementsAvailable(); } -BleV2Peripheral BleV2Medium::GetRemotePeripheral( - const std::string& mac_address) { - BleV2Peripheral peripheral; - impl_->GetRemotePeripheral(mac_address, - [&](api::ble_v2::BlePeripheral& device) { - peripheral = BleV2Peripheral(*this, device); - }); - return peripheral; -} - -absl::optional BleV2Peripheral::GetAddress() const { - absl::optional address; - api::ble_v2::BlePeripheral* device = GetImpl(); - if (device != nullptr) { - address = device->GetAddress(); - }; - return address; -} - bool BleV2Peripheral::IsValid() const { return GetImpl() != nullptr; } diff --git a/internal/platform/ble_v2.h b/internal/platform/ble_v2.h index 7503b74a..c906eb48 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble_v2.h @@ -52,16 +52,15 @@ class BleV2Medium; class BleV2Peripheral final { public: BleV2Peripheral() = default; - BleV2Peripheral(BleV2Medium& medium, api::ble_v2::BlePeripheral& impl) - : medium_(&medium), unique_id_(impl.GetUniqueId()) {} + BleV2Peripheral(BleV2Medium& medium, + api::ble_v2::BlePeripheral::UniqueId unique_id) + : medium_(&medium), unique_id_(unique_id) {} BleV2Peripheral(const BleV2Peripheral&) = default; BleV2Peripheral& operator=(const BleV2Peripheral&) = default; BleV2Peripheral(BleV2Peripheral&& other) = default; BleV2Peripheral& operator=(BleV2Peripheral&& other) = default; - // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional GetAddress() const; ByteArray GetId() const { return id_; } void SetId(const ByteArray& id) { id_ = id; } @@ -192,9 +191,11 @@ class BleV2ServerSocket final { if (socket == nullptr) { LOG(INFO) << "BleServerSocket Accept() failed on server socket: " << this; } else { - auto* platform_peripheral = socket->GetRemotePeripheral(); + api::ble_v2::BlePeripheral* platform_peripheral = + socket->GetRemotePeripheral(); if (platform_peripheral != nullptr) { - peripheral = BleV2Peripheral(*medium_, *platform_peripheral); + peripheral = + BleV2Peripheral(*medium_, platform_peripheral->GetUniqueId()); } } return BleV2Socket(peripheral, std::move(socket)); @@ -219,7 +220,6 @@ class BleV2ServerSocket final { // // Note that some of the methods return absl::optional instead // of std::optional, because iOS platform is still in C++14. -// LINT.IfChange class GattServer final { public: GattServer(BleV2Medium& medium, @@ -266,7 +266,6 @@ class GattServer final { BleV2Medium& medium_; std::unique_ptr impl_; }; -// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/implementation/ble_v2.h) // Opaque wrapper for a GattClient. // @@ -427,7 +426,8 @@ class BleL2capServerSocket final { api::ble_v2::BlePeripheral* platform_peripheral = socket->GetRemotePeripheral(); if (platform_peripheral != nullptr) { - peripheral = BleV2Peripheral(*medium_, *platform_peripheral); + peripheral = + BleV2Peripheral(*medium_, platform_peripheral->GetUniqueId()); } } return BleL2capSocket(peripheral, std::move(socket)); @@ -582,10 +582,6 @@ class BleV2Medium final { bool IsValid() const { return impl_ != nullptr; } - // Returns a `BleV2Peripheral` with given mac address. `mac_address` is in - // canonical format. - BleV2Peripheral GetRemotePeripheral(const std::string& mac_address); - api::ble_v2::BleMedium* GetImpl() const { return impl_.get(); } BluetoothAdapter& GetAdapter() { return adapter_; } void AddAlternateUuidForService(uint16_t uuid, diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_v2_test.cc index 0d257fd6..a8d8d305 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_v2_test.cc @@ -32,6 +32,7 @@ #include "internal/platform/count_down_latch.h" #include "internal/platform/feature_flags.h" #include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" #include "internal/platform/single_thread_executor.h" #include "internal/platform/uuid.h" @@ -625,11 +626,11 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { gatt_server->UpdateCharacteristic(*server_characteristic, server_value)); // Start GattClient - BleV2Peripheral ble_peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - std::unique_ptr gatt_client = - ble_b.ConnectToGattServer(BleV2Peripheral(ble_peripheral), kTxPowerLevel, - /*ClientGattConnectionCallback=*/{}); + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromString(adapter_a.GetMacAddress(), mac_address)); + std::unique_ptr gatt_client = ble_b.ConnectToGattServer( + BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); @@ -660,13 +661,13 @@ TEST_F(BleV2MediumTest, GattClientConnectToStoppedGattServerFails) { std::unique_ptr gatt_server = ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{}); ASSERT_NE(gatt_server, nullptr); - BleV2Peripheral ble_peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromString(adapter_a.GetMacAddress(), mac_address)); gatt_server->Stop(); - std::unique_ptr gatt_client = - ble_b.ConnectToGattServer(BleV2Peripheral(ble_peripheral), kTxPowerLevel, - /*ClientGattConnectionCallback=*/{}); + std::unique_ptr gatt_client = ble_b.ConnectToGattServer( + BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); EXPECT_FALSE(gatt_client->IsValid()); @@ -684,10 +685,10 @@ TEST_F(BleV2MediumTest, GattClientNotifiedWhenServerDisconnects) { ASSERT_NE(gatt_server, nullptr); CountDownLatch disconnected_latch(1); // Start GattClient - BleV2Peripheral ble_peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromString(adapter_a.GetMacAddress(), mac_address)); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( - BleV2Peripheral(ble_peripheral), kTxPowerLevel, + BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{.disconnected_cb = [&]() { disconnected_latch.CountDown(); }}); @@ -723,12 +724,11 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { ASSERT_NE(gatt_server, nullptr); // Start GattClient. - BleV2Peripheral ble_peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - ASSERT_TRUE(ble_peripheral.IsValid()); - std::unique_ptr gatt_client = - ble_b.ConnectToGattServer(BleV2Peripheral(ble_peripheral), kTxPowerLevel, - /*ClientGattConnectionCallback=*/{}); + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromString(adapter_a.GetMacAddress(), mac_address)); + std::unique_ptr gatt_client = ble_b.ConnectToGattServer( + BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); // Can't not discover service and characteristic. @@ -806,12 +806,11 @@ TEST_F(BleV2MediumTest, GattClientSubscribeNotificationGattServerCanNotify) { ByteArray("any"))); // Start GattClient - BleV2Peripheral ble_peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - ASSERT_TRUE(ble_peripheral.IsValid()); - std::unique_ptr gatt_client = - ble_b.ConnectToGattServer(BleV2Peripheral(ble_peripheral), kTxPowerLevel, - /*ClientGattConnectionCallback=*/{}); + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromString(adapter_a.GetMacAddress(), mac_address)); + std::unique_ptr gatt_client = ble_b.ConnectToGattServer( + BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); EXPECT_TRUE(gatt_client->DiscoverServiceAndCharacteristics( @@ -858,116 +857,5 @@ TEST_F(BleV2MediumTest, GattClientSubscribeNotificationGattServerCanNotify) { env_.Stop(); } -TEST(BleV2PeripheralTest, ConstructionWorks) { - MediumEnvironment::Instance().Start(); - BluetoothAdapter adapter_a; - BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); - - BleV2Peripheral peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - - ASSERT_TRUE(peripheral.IsValid()); - EXPECT_EQ(peripheral.GetAddress(), adapter_a.GetMacAddress()); - MediumEnvironment::Instance().Stop(); -} - -TEST(BleV2PeripheralTest, SetIdAndPsmWorks) { - MediumEnvironment::Instance().Start(); - BluetoothAdapter adapter_a; - BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); - ByteArray id((std::string(kId))); - int psm = 2; - - BleV2Peripheral peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - peripheral.SetId(id); - peripheral.SetPsm(psm); - - ASSERT_TRUE(peripheral.IsValid()); - EXPECT_EQ(peripheral.GetId(), id); - EXPECT_EQ(peripheral.GetPsm(), 2); - MediumEnvironment::Instance().Stop(); -} - -TEST(BleV2PeripheralTest, CopyConstructorAndAssignmentSuccess) { - MediumEnvironment::Instance().Start(); - BluetoothAdapter adapter_a; - BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); - ByteArray id((std::string(kId))); - int psm = 2; - - BleV2Peripheral peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - peripheral.SetId(id); - peripheral.SetPsm(psm); - - BleV2Peripheral copy_peripheral_1(peripheral); - - ASSERT_TRUE(copy_peripheral_1.IsValid()); - EXPECT_EQ(copy_peripheral_1.GetAddress(), adapter_a.GetMacAddress()); - EXPECT_EQ(copy_peripheral_1.GetId(), id); - EXPECT_EQ(copy_peripheral_1.GetPsm(), 2); - - BleV2Peripheral copy_periphera1_2 = peripheral; - - ASSERT_TRUE(copy_periphera1_2.IsValid()); - EXPECT_EQ(copy_periphera1_2.GetAddress(), adapter_a.GetMacAddress()); - EXPECT_EQ(copy_periphera1_2.GetId(), id); - EXPECT_EQ(copy_periphera1_2.GetPsm(), 2); - MediumEnvironment::Instance().Stop(); -} - -TEST(BleV2PeripheralTest, MoveConstructorSuccess) { - MediumEnvironment::Instance().Start(); - BluetoothAdapter adapter_a; - BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); - ByteArray id((std::string(kId))); - int psm = 2; - - BleV2Peripheral peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - peripheral.SetId(id); - peripheral.SetPsm(psm); - - BleV2Peripheral move_peripheral(std::move(peripheral)); - - ASSERT_TRUE(move_peripheral.IsValid()); - EXPECT_EQ(move_peripheral.GetAddress(), adapter_a.GetMacAddress()); - EXPECT_EQ(move_peripheral.GetId(), id); - EXPECT_EQ(move_peripheral.GetPsm(), 2); - MediumEnvironment::Instance().Stop(); -} - -TEST(BleV2PeripheralTest, MoveAssignmentSuccess) { - MediumEnvironment::Instance().Start(); - BluetoothAdapter adapter_a; - BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); - ByteArray id((std::string(kId))); - int psm = 2; - - BleV2Peripheral peripheral = - ble_b.GetRemotePeripheral(adapter_a.GetMacAddress()); - peripheral.SetId(id); - peripheral.SetPsm(psm); - - BleV2Peripheral move_peripheral = std::move(peripheral); - - ASSERT_TRUE(move_peripheral.IsValid()); - EXPECT_EQ(move_peripheral.GetAddress(), adapter_a.GetMacAddress()); - EXPECT_EQ(move_peripheral.GetId(), id); - EXPECT_EQ(move_peripheral.GetPsm(), 2); - MediumEnvironment::Instance().Stop(); -} - } // namespace } // namespace nearby diff --git a/internal/platform/implementation/apple/ble_medium.h b/internal/platform/implementation/apple/ble_medium.h index fb1f381a..5955aaa9 100644 --- a/internal/platform/implementation/apple/ble_medium.h +++ b/internal/platform/implementation/apple/ble_medium.h @@ -164,12 +164,6 @@ class BleMedium : public api::ble_v2::BleMedium { // This is currently always false for all Apple hardware. bool IsExtendedAdvertisementsAvailable() override; - // A peripheral cannot be retreived via MAC address on Apple platforms. - // - // This always returns false and does not call the callback. - bool GetRemotePeripheral(const std::string &mac_address, - api::ble_v2::BleMedium::GetRemotePeripheralCallback callback) override; - // Returns true if `id` refers to a known BLE peripheral and calls `callback` with a reference to // said peripheral that is only guaranteed to be available for the duration of the callback. // Otherwise, does not call the callback and returns false. diff --git a/internal/platform/implementation/apple/ble_medium.mm b/internal/platform/implementation/apple/ble_medium.mm index 4a9fecd1..e3d8a07d 100644 --- a/internal/platform/implementation/apple/ble_medium.mm +++ b/internal/platform/implementation/apple/ble_medium.mm @@ -536,13 +536,6 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() { return [medium_ supportsExtendedAdvertisements]; } -bool BleMedium::GetRemotePeripheral(const std::string &mac_address, - api::ble_v2::BleMedium::GetRemotePeripheralCallback callback) { - // Apple does not expose MAC address information, so we cannot retreive a peripheral via MAC - // address. - return false; -} - bool BleMedium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId unique_id, api::ble_v2::BleMedium::GetRemotePeripheralCallback callback) { // If the unique_id is 0, that means it's the local/empty peripheral. We must return "true" diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble_v2.h index ab10ee87..51bba096 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble_v2.h @@ -117,7 +117,7 @@ class BlePeripheral { void* GetPlatformData() const { return platform_data_; } - bool IsSet() const { return unique_id_ != 0 || address_.IsSet(); } + bool IsSet() const { return GetUniqueId() != 0 || !GetAddress().empty(); } private: UniqueId unique_id_ = 0; @@ -623,14 +623,6 @@ class BleMedium { // Requests if support extended advertisement. virtual bool IsExtendedAdvertisementsAvailable() = 0; - // Calls `callback` and returns true if `mac_address` is a valid BLE address. - // Otherwise, does not call the callback and returns false. - // - // This method is not available on Apple platforms and will always return - // false, ignoring the callback. - virtual bool GetRemotePeripheral(const std::string& mac_address, - GetRemotePeripheralCallback callback) = 0; - // Calls `callback` and returns true if `id` refers to a known BLE peripheral. // Otherwise, does not call the callback and returns false. virtual bool GetRemotePeripheral(BlePeripheral::UniqueId id, diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index e3b51bc9..c81b14a6 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -349,36 +349,6 @@ bool BleV2Medium::IsExtendedAdvertisementsAvailable() { return is_extended_advertisements_available_; } -bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address, - GetRemotePeripheralCallback callback) { - NEARBY_LOGS(INFO) << "GetRemotePeripheral, address= " << mac_address; - absl::MutexLock lock(&mutex_); - for (auto& item : remote_peripherals_) { - auto* peripheral = item.second.get(); - if (peripheral->GetAddress() == mac_address) { - callback(*peripheral); - return true; - } - } - BleV2Medium* remote_medium = dynamic_cast( - MediumEnvironment::Instance().FindBleV2Medium(mac_address)); - if (remote_medium == nullptr) { - return false; - } - api::ble_v2::BlePeripheral::UniqueId id = - remote_medium->GetPeripheral().GetUniqueId(); - BluetoothAdapter& adapter = remote_medium->GetAdapter(); - MacAddress address; - if (!MacAddress::FromString(adapter.GetMacAddress(), address)) { - return false; - } - remote_peripherals_[id] = - std::make_unique(id, address); - remote_peripherals_[id]->SetPlatformData(&adapter); - callback(*remote_peripherals_[id]); - return true; -} - bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) { absl::MutexLock lock(&mutex_); diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index 11e6d452..5e9fa1cd 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -194,9 +194,6 @@ class BleV2Medium : public api::ble_v2::BleMedium { api::ble_v2::BlePeripheral& GetPeripheral() { return peripheral_; } - bool GetRemotePeripheral(const std::string& mac_address, - GetRemotePeripheralCallback callback) override; - bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) override; diff --git a/internal/platform/implementation/windows/ble_v2.cc b/internal/platform/implementation/windows/ble_v2.cc index 8fa69ba2..cddd7def 100644 --- a/internal/platform/implementation/windows/ble_v2.cc +++ b/internal/platform/implementation/windows/ble_v2.cc @@ -1299,26 +1299,6 @@ void BleV2Medium::AdvertisementFoundHandler( } } -bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address, - GetRemotePeripheralCallback callback) { - MacAddress bluetooth_address; - if (!MacAddress::FromString(mac_address, bluetooth_address)) { - LOG(WARNING) << __func__ << ": Invalid MAC address: " << mac_address; - return false; - } - api::ble_v2::BlePeripheral* peripheral = nullptr; - { - absl::MutexLock lock(&mutex_); - peripheral = GetOrCreatePeripheral(bluetooth_address); - } - - if (peripheral != nullptr && peripheral->IsSet()) { - callback(*peripheral); - return true; - } - return false; -} - bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) { MacAddress bluetooth_address; diff --git a/internal/platform/implementation/windows/ble_v2.h b/internal/platform/implementation/windows/ble_v2.h index 7dcc3d26..25a2d854 100644 --- a/internal/platform/implementation/windows/ble_v2.h +++ b/internal/platform/implementation/windows/ble_v2.h @@ -80,10 +80,6 @@ class BleV2Medium : public api::ble_v2::BleMedium { CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_); bool IsExtendedAdvertisementsAvailable() override; - bool GetRemotePeripheral(const std::string& mac_address, - GetRemotePeripheralCallback callback) override - ABSL_LOCKS_EXCLUDED(mutex_); - bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_);