diff --git a/internal/platform/ble_v2.cc b/internal/platform/ble_v2.cc index 72ce304c..28a1bfc6 100644 --- a/internal/platform/ble_v2.cc +++ b/internal/platform/ble_v2.cc @@ -293,7 +293,7 @@ BleL2capSocket BleV2Medium::ConnectOverL2cap( socket = BleL2capSocket( peripheral, impl_->ConnectOverL2cap(peripheral.GetPsm(), service_id, tx_power_level, - *device, cancellation_flag)); + device->GetUniqueId(), cancellation_flag)); }; return socket; } diff --git a/internal/platform/ble_v2.h b/internal/platform/ble_v2.h index c906eb48..77a246e9 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble_v2.h @@ -423,12 +423,8 @@ class BleL2capServerSocket final { LOG(INFO) << "BleL2capServerSocket Accept() failed on server socket: " << this; } else { - api::ble_v2::BlePeripheral* platform_peripheral = - socket->GetRemotePeripheral(); - if (platform_peripheral != nullptr) { - peripheral = - BleV2Peripheral(*medium_, platform_peripheral->GetUniqueId()); - } + peripheral = + BleV2Peripheral(*medium_, socket->GetRemotePeripheralId()); } return BleL2capSocket(peripheral, std::move(socket)); } diff --git a/internal/platform/implementation/apple/ble_l2cap_socket.h b/internal/platform/implementation/apple/ble_l2cap_socket.h index 2f31821a..af0ed876 100644 --- a/internal/platform/implementation/apple/ble_l2cap_socket.h +++ b/internal/platform/implementation/apple/ble_l2cap_socket.h @@ -84,7 +84,8 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket { // The peripheral used to create the socket must outlive the socket or undefined behavior will // occur. - BleL2capSocket(GNCBLEL2CAPConnection* connection, api::ble_v2::BlePeripheral *peripheral); + BleL2capSocket(GNCBLEL2CAPConnection *connection, + api::ble_v2::BlePeripheral::UniqueId peripheral_id); ~BleL2capSocket() override; // Returns the InputStream of the BleL2capSocket. @@ -108,7 +109,7 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket { // Returns valid BlePeripheral pointer if there is a connection, and // nullptr otherwise. - api::ble_v2::BlePeripheral *GetRemotePeripheral() override { return peripheral_; } + api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; } bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_); @@ -119,7 +120,7 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket { bool closed_ ABSL_GUARDED_BY(mutex_) = false; std::unique_ptr input_stream_; std::unique_ptr output_stream_; - api::ble_v2::BlePeripheral *peripheral_; + api::ble_v2::BlePeripheral::UniqueId peripheral_id_; }; } // namespace apple diff --git a/internal/platform/implementation/apple/ble_l2cap_socket.mm b/internal/platform/implementation/apple/ble_l2cap_socket.mm index a5c45b9a..14062a4a 100644 --- a/internal/platform/implementation/apple/ble_l2cap_socket.mm +++ b/internal/platform/implementation/apple/ble_l2cap_socket.mm @@ -168,13 +168,13 @@ Exception BleL2capOutputStream::Close() { #pragma mark - BleL2capSocket BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection) - : BleL2capSocket(connection, &BlePeripheral::DefaultBlePeripheral()) {} + : BleL2capSocket(connection, BlePeripheral::DefaultBlePeripheral().GetUniqueId()) {} BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection, - api::ble_v2::BlePeripheral *peripheral) + api::ble_v2::BlePeripheral::UniqueId peripheral_id) : input_stream_(std::make_unique(connection)), output_stream_(std::make_unique(connection)), - peripheral_(peripheral) {} + peripheral_id_(peripheral_id) {} BleL2capSocket::~BleL2capSocket() { absl::MutexLock lock(&mutex_); diff --git a/internal/platform/implementation/apple/ble_medium.h b/internal/platform/implementation/apple/ble_medium.h index 5955aaa9..5ec08a08 100644 --- a/internal/platform/implementation/apple/ble_medium.h +++ b/internal/platform/implementation/apple/ble_medium.h @@ -157,7 +157,8 @@ class BleMedium : public api::ble_v2::BleMedium { // On success, returns a new BleL2capSocket. On error, returns nullptr. std::unique_ptr ConnectOverL2cap( int psm, const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral &peripheral, CancellationFlag *cancellation_flag) override; + api::ble_v2::BlePeripheral::UniqueId peripheral_id, + CancellationFlag *cancellation_flag) override; // Returns whether the hardware supports BOTH advertising extensions and extended scans. // diff --git a/internal/platform/implementation/apple/ble_medium.mm b/internal/platform/implementation/apple/ble_medium.mm index e3d8a07d..d14381f1 100644 --- a/internal/platform/implementation/apple/ble_medium.mm +++ b/internal/platform/implementation/apple/ble_medium.mm @@ -483,22 +483,24 @@ std::unique_ptr BleMedium::Connect(const std::string &se std::unique_ptr BleMedium::ConnectOverL2cap( int psm, const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral &peripheral, CancellationFlag *cancellation_flag) { - // Check that the @c api::ble_v2::BlePeripheral is a @c nearby::apple::BlePeripheral and not a - // @c nearby::apple::EmptyBlePeripheral instance, so we can retreive the CBPeripheral object. - BlePeripheral *non_empty_peripheral = dynamic_cast(&peripheral); - if (non_empty_peripheral == nullptr) { - GTMLoggerError(@"[NEARBY] Failed to connect over L2CAP: peripheral is empty."); - return nullptr; + api::ble_v2::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) { + BlePeripheral *peripheral = nullptr; + { + absl::MutexLock lock(&peripherals_mutex_); + const auto& it = peripherals_.find(peripheral_id); + if (it == peripherals_.end()) { + GTMLoggerError(@"[NEARBY] Failed to connect over L2CAP: peripheral is not found."); + return nullptr; + } + peripheral = it->second.get(); } - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, kRequestConnectionTimeoutInSeconds * NSEC_PER_SEC); __block std::unique_ptr socket; const std::string &service_id_str = service_id; [medium_ openL2CAPChannelWithPSM:psm - peripheral:non_empty_peripheral->GetPeripheral() + peripheral:peripheral->GetPeripheral() completionHandler:^(GNCBLEL2CAPStream *stream, NSError *error) { if (error) { dispatch_semaphore_signal(semaphore); @@ -514,7 +516,7 @@ std::unique_ptr BleMedium::ConnectOverL2cap( // Connections layer. [connection requestDataConnectionWithCompletion:^(BOOL result) { if (result) { - socket = std::make_unique(connection, non_empty_peripheral); + socket = std::make_unique(connection, peripheral_id); } GTMLoggerInfo(result ? @"[NEARBY] Request data connection is ok" : @"[NEARBY] Request data connection is not ok"); diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble_v2.h index 51bba096..3e29a69f 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble_v2.h @@ -424,7 +424,7 @@ class BleL2capSocket { // Returns valid BlePeripheral pointer if there is a connection, and // nullptr otherwise. - virtual BlePeripheral* GetRemotePeripheral() = 0; + virtual BlePeripheral::UniqueId GetRemotePeripheralId() = 0; }; // A BLE L2CAP server socket for listening incoming L2CAP socket. @@ -616,7 +616,8 @@ class BleMedium { // Platform implementation should override this method if it supports L2CAP. virtual std::unique_ptr ConnectOverL2cap( int psm, const std::string& service_id, TxPowerLevel tx_power_level, - BlePeripheral& peripheral, CancellationFlag* cancellation_flag) { + BlePeripheral::UniqueId peripheral_id, + CancellationFlag* cancellation_flag) { return nullptr; }