Switch Connect() to use BlePeripheral::UniqueId.

PiperOrigin-RevId: 756821680
This commit is contained in:
Francis Tsui
2025-05-09 10:36:24 -07:00
committed by Copybara-Service
parent 5869bbbf6e
commit 16839afa18
10 changed files with 44 additions and 56 deletions
+8 -8
View File
@@ -276,14 +276,14 @@ BleV2Socket BleV2Medium::Connect(const std::string& service_id,
TxPowerLevel tx_power_level,
const BleV2Peripheral& peripheral,
CancellationFlag* cancellation_flag) {
BleV2Socket socket;
api::ble_v2::BlePeripheral* device = peripheral.GetImpl();
if (device != nullptr) {
socket = BleV2Socket(
peripheral,
impl_->Connect(service_id, tx_power_level, *device, cancellation_flag));
};
return socket;
std::optional<api::ble_v2::BlePeripheral::UniqueId> id =
peripheral.GetUniqueId();
if (!id.has_value()) {
LOG(ERROR) << "Failed to connect, invalid peripheral";
return {};
}
return BleV2Socket(peripheral, impl_->Connect(service_id, tx_power_level, *id,
cancellation_flag));
}
BleL2capSocket BleV2Medium::ConnectOverL2cap(
@@ -142,10 +142,10 @@ class BleMedium : public api::ble_v2::BleMedium {
// should not be modified by this method.
//
// On success, returns a new BleSocket. On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleSocket> Connect(const std::string &service_id,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral &peripheral,
CancellationFlag *cancellation_flag) override;
std::unique_ptr<api::ble_v2::BleSocket> Connect(
const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
CancellationFlag *cancellation_flag) override;
// TODO(b/290385712): cancellation_flag support is not yet implemented.
//
@@ -431,19 +431,22 @@ std::unique_ptr<api::ble_v2::BleL2capServerSocket> BleMedium::OpenL2capServerSoc
}
// TODO(b/290385712): Add support for @c cancellation_flag.
// TODO(b/293336684): Old Weave code that need to be deleted once shared Weave is complete.
std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(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<BlePeripheral *>(&peripheral);
if (non_empty_peripheral == nullptr) {
return nullptr;
std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(
const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
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 to Gatt server: peripheral is not found.");
return nullptr;
}
peripheral = it->second.get();
}
GNSCentralPeerManager *updatedCentralPeerManager = [socketCentralManager_
retrieveCentralPeerWithIdentifier:non_empty_peripheral->GetPeripheral().identifier];
retrieveCentralPeerWithIdentifier:peripheral->GetPeripheral().identifier];
if (!updatedCentralPeerManager) {
return nullptr;
}
@@ -469,7 +472,7 @@ std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(const std::string &se
expectedIntroPacket:NO
callbackQueue:dispatch_get_main_queue()];
socket =
std::make_unique<BleSocket>(connection, non_empty_peripheral);
std::make_unique<BleSocket>(connection, peripheral);
connection.connectionHandlers =
socket->GetInputStream().GetConnectionHandlers();
dispatch_semaphore_signal(semaphore);
+2 -1
View File
@@ -607,7 +607,8 @@ class BleMedium {
// On error, returns nullptr.
virtual std::unique_ptr<BleSocket> Connect(
const std::string& service_id, TxPowerLevel tx_power_level,
BlePeripheral& peripheral, CancellationFlag* cancellation_flag) = 0;
BlePeripheral::UniqueId peripheral_id,
CancellationFlag* cancellation_flag) = 0;
// Connects to a BLE peripheral over L2CAP.
//
@@ -760,25 +760,23 @@ BleV2Medium::OpenL2capServerSocket(const std::string& service_id) {
std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
const std::string& service_id, TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral& remote_peripheral,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) {
LOG(INFO) << "G3 Ble Connect [self]: medium=" << this
<< ", adapter=" << &GetAdapter()
<< ", peripheral=" << &GetPeripheral()
<< ", service_id=" << service_id;
// First, find an instance of remote medium, that exposed this peripheral.
BluetoothAdapter* remote_adapter =
static_cast<BluetoothAdapter*>(remote_peripheral.GetPlatformData());
auto* remote_medium =
dynamic_cast<BleV2Medium*>(remote_adapter->GetBleV2Medium());
if (!remote_medium) {
BleV2Medium* remote_medium = dynamic_cast<BleV2Medium*>(
MediumEnvironment::Instance().FindBleV2Medium(remote_peripheral_id));
if (remote_medium == nullptr) {
LOG(INFO) << "Peripheral not found, id= " << remote_peripheral_id;
return nullptr;
}
BleV2ServerSocket* remote_server_socket = nullptr;
LOG(INFO) << "G3 Ble Connect [peer]: medium=" << remote_medium
<< ", adapter=" << &remote_adapter
<< ", peripheral=" << &remote_peripheral
<< ", peripheral=" << &remote_peripheral_id
<< ", service_id=" << service_id;
// Then, find our server socket context in this medium.
{
+1 -1
View File
@@ -185,7 +185,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
// On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleSocket> Connect(
const std::string& service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral& remote_peripheral,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
bool IsExtendedAdvertisementsAvailable() override;
@@ -660,7 +660,7 @@ std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
const std::string& service_id, TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral& remote_peripheral,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) {
LOG(INFO) << __func__ << ": Connect to service_id=" << service_id;
@@ -680,7 +680,7 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
nearby::CancellationFlagListener cancellation_flag_listener(
cancellation_flag, [socket = ble_socket.get()]() { socket->Close(); });
if (!ble_socket->Connect(&remote_peripheral)) {
if (!ble_socket->Connect()) {
LOG(INFO) << __func__
<< ": BLE socket connection failed. service_id=" << service_id;
return nullptr;
@@ -76,7 +76,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::BleSocket> Connect(
const std::string& service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral& remote_peripheral,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
bool IsExtendedAdvertisementsAvailable() override;
@@ -14,17 +14,10 @@
#include "internal/platform/implementation/windows/ble_v2_socket.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/logging.h"
#include "internal/platform/output_stream.h"
@@ -38,14 +31,9 @@ OutputStream& BleV2Socket::GetOutputStream() { return output_stream_; }
Exception BleV2Socket::Close() { return {Exception::kSuccess}; }
api::ble_v2::BlePeripheral* BleV2Socket::GetRemotePeripheral() {
return ble_peripheral_;
}
bool BleV2Socket::Connect(api::ble_v2::BlePeripheral* ble_peripheral) {
bool BleV2Socket::Connect() {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__
<< ": Connect to BLE peripheral=" << ble_peripheral->GetAddress();
VLOG(1) << __func__ << ": Connect to BLE peripheral";
return false;
}
@@ -15,10 +15,7 @@
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_
#include <cstddef>
#include <cstdint>
#include <memory>
#include <utility>
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
@@ -40,9 +37,11 @@ class BleV2Socket : public api::ble_v2::BleSocket {
Exception Close() override;
api::ble_v2::BlePeripheral* GetRemotePeripheral() override;
api::ble_v2::BlePeripheral* GetRemotePeripheral() override {
return nullptr;
};
bool Connect(api::ble_v2::BlePeripheral* ble_peripheral);
bool Connect();
private:
class BleInputStream : public InputStream {
@@ -63,7 +62,6 @@ class BleV2Socket : public api::ble_v2::BleSocket {
BleInputStream input_stream_{};
BleOutputStream output_stream_{};
api::ble_v2::BlePeripheral* ble_peripheral_ = nullptr;
};
} // namespace windows