From 94ddb09a7f3673705ef0b2cfa591bf24c753da05 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 7 May 2025 13:54:15 -0700 Subject: [PATCH] internal fix PiperOrigin-RevId: 755993569 --- .../implementation/apple/ble_gatt_server.h | 1 - .../implementation/apple/ble_gatt_server.mm | 2 +- .../implementation/apple/ble_l2cap_socket.mm | 2 +- .../implementation/apple/ble_medium.h | 5 ++-- .../implementation/apple/ble_medium.mm | 4 +-- .../implementation/apple/ble_peripheral.h | 26 +++---------------- .../implementation/apple/ble_peripheral.mm | 14 +++++----- .../implementation/apple/ble_socket.mm | 2 +- 8 files changed, 16 insertions(+), 40 deletions(-) diff --git a/internal/platform/implementation/apple/ble_gatt_server.h b/internal/platform/implementation/apple/ble_gatt_server.h index abf5e8ea..94aa8d2e 100644 --- a/internal/platform/implementation/apple/ble_gatt_server.h +++ b/internal/platform/implementation/apple/ble_gatt_server.h @@ -71,7 +71,6 @@ class GattServer : public api::ble_v2::GattServer { private: GNCBLEGATTServer *gatt_server_; - EmptyBlePeripheral peripheral_; }; } // namespace apple diff --git a/internal/platform/implementation/apple/ble_gatt_server.mm b/internal/platform/implementation/apple/ble_gatt_server.mm index 57682c73..f28a2d72 100644 --- a/internal/platform/implementation/apple/ble_gatt_server.mm +++ b/internal/platform/implementation/apple/ble_gatt_server.mm @@ -97,7 +97,7 @@ void GattServer::Stop() { // TODO(b/290385712): Implement. api::ble_v2::BlePeripheral &GattServer::GetBlePeripheral() { - return peripheral_; + return BlePeripheral::DefaultBlePeripheral(); } } // namespace apple diff --git a/internal/platform/implementation/apple/ble_l2cap_socket.mm b/internal/platform/implementation/apple/ble_l2cap_socket.mm index 2590a6d8..a5c45b9a 100644 --- a/internal/platform/implementation/apple/ble_l2cap_socket.mm +++ b/internal/platform/implementation/apple/ble_l2cap_socket.mm @@ -168,7 +168,7 @@ Exception BleL2capOutputStream::Close() { #pragma mark - BleL2capSocket BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection) - : BleL2capSocket(connection, new EmptyBlePeripheral()) {} + : BleL2capSocket(connection, &BlePeripheral::DefaultBlePeripheral()) {} BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection, api::ble_v2::BlePeripheral *peripheral) diff --git a/internal/platform/implementation/apple/ble_medium.h b/internal/platform/implementation/apple/ble_medium.h index 86354c93..fb1f381a 100644 --- a/internal/platform/implementation/apple/ble_medium.h +++ b/internal/platform/implementation/apple/ble_medium.h @@ -183,11 +183,10 @@ class BleMedium : public api::ble_v2::BleMedium { GNCBLEMedium *medium_; absl::Mutex peripherals_mutex_; - absl::flat_hash_map> + absl::flat_hash_map> peripherals_ ABSL_GUARDED_BY(peripherals_mutex_); - std::unique_ptr local_peripheral_; - GNSPeripheralServiceManager *socketPeripheralServiceManager_; GNSPeripheralManager *socketPeripheralManager_; GNSCentralManager *socketCentralManager_; diff --git a/internal/platform/implementation/apple/ble_medium.mm b/internal/platform/implementation/apple/ble_medium.mm index 16a1e597..4a9fecd1 100644 --- a/internal/platform/implementation/apple/ble_medium.mm +++ b/internal/platform/implementation/apple/ble_medium.mm @@ -547,8 +547,8 @@ bool BleMedium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId unique_ api::ble_v2::BleMedium::GetRemotePeripheralCallback callback) { // If the unique_id is 0, that means it's the local/empty peripheral. We must return "true" // otherwise the connection will be considered invalid and the application will crash. - if (unique_id == 0) { - callback(*local_peripheral_); + if (unique_id == BlePeripheral::DefaultBlePeripheral().GetUniqueId()) { + callback(BlePeripheral::DefaultBlePeripheral()); return true; } diff --git a/internal/platform/implementation/apple/ble_peripheral.h b/internal/platform/implementation/apple/ble_peripheral.h index a1d23680..81fc9d76 100644 --- a/internal/platform/implementation/apple/ble_peripheral.h +++ b/internal/platform/implementation/apple/ble_peripheral.h @@ -31,29 +31,6 @@ namespace nearby { namespace apple { -// An empty peripheral. -// -// Apple APIs do not expose a peripheral's MAC address and does not provide a -// way to directly connect to a given MAC address. Instead a connection can only -// be made using a CoreBluetooth peripheral object. Many times a CoreBluetooth -// peripheral is not available, namely, when the remote device is a central. For -// these cases, an EmptyBlePeripheral should be used. -class EmptyBlePeripheral : public api::ble_v2::BlePeripheral { - public: - EmptyBlePeripheral(); - ~EmptyBlePeripheral() override = default; - - // Returns an empty string. - std::string GetAddress() const override; - - // Returns an immutable unique identifier. The identifier does not change when - // the peripheral's address is rotated. - api::ble_v2::BlePeripheral::UniqueId GetUniqueId() const override; - - private: - api::ble_v2::BlePeripheral::UniqueId unique_id_; -}; - // A wrapper of a CoreBluetooth peripheral object. This can be used to uniquely // identify a peripheral and connect to its GATT server. // @@ -62,6 +39,9 @@ class EmptyBlePeripheral : public api::ble_v2::BlePeripheral { // used instead. class BlePeripheral : public api::ble_v2::BlePeripheral { public: + // Returns a reference to a default BlePeripheral. + static api::ble_v2::BlePeripheral& DefaultBlePeripheral(); + explicit BlePeripheral(id peripheral); ~BlePeripheral() override = default; diff --git a/internal/platform/implementation/apple/ble_peripheral.mm b/internal/platform/implementation/apple/ble_peripheral.mm index feac69f2..3eefcfff 100644 --- a/internal/platform/implementation/apple/ble_peripheral.mm +++ b/internal/platform/implementation/apple/ble_peripheral.mm @@ -26,19 +26,17 @@ namespace nearby { namespace apple { -#pragma mark - EmptyBlePeripheral - -EmptyBlePeripheral::EmptyBlePeripheral() : unique_id_(0) {} - -std::string EmptyBlePeripheral::GetAddress() const { return ""; } - -api::ble_v2::BlePeripheral::UniqueId EmptyBlePeripheral::GetUniqueId() const { return unique_id_; } - #pragma mark - BlePeripheral BlePeripheral::BlePeripheral(id peripheral) : peripheral_(peripheral), unique_id_(peripheral.identifier.hash) {} +api::ble_v2::BlePeripheral& BlePeripheral::DefaultBlePeripheral() { + static api::ble_v2::BlePeripheral* default_peripheral = + new api::ble_v2::BlePeripheral(0xffffffffffffffff); + return *default_peripheral; +} + std::string BlePeripheral::GetAddress() const { return ""; } api::ble_v2::BlePeripheral::UniqueId BlePeripheral::GetUniqueId() const { return unique_id_; } diff --git a/internal/platform/implementation/apple/ble_socket.mm b/internal/platform/implementation/apple/ble_socket.mm index b150e0a9..7864cbee 100644 --- a/internal/platform/implementation/apple/ble_socket.mm +++ b/internal/platform/implementation/apple/ble_socket.mm @@ -180,7 +180,7 @@ Exception BleOutputStream::Close() { BleSocket::BleSocket(id connection) : input_stream_(new BleInputStream()), output_stream_(new BleOutputStream(connection)), - peripheral_(new EmptyBlePeripheral()) {} + peripheral_(&(BlePeripheral::DefaultBlePeripheral())) {} BleSocket::BleSocket(id connection, api::ble_v2::BlePeripheral *peripheral) : input_stream_(new BleInputStream()),