mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Switch ConnectOverL2cap methods to use BlePeripheral::UniqueId instead of BlePeripheral object.
PiperOrigin-RevId: 756491579
This commit is contained in:
committed by
Copybara-Service
parent
09ac8d8c58
commit
e1c072e797
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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<BleL2capInputStream> input_stream_;
|
||||
std::unique_ptr<BleL2capOutputStream> output_stream_;
|
||||
api::ble_v2::BlePeripheral *peripheral_;
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id_;
|
||||
};
|
||||
|
||||
} // namespace apple
|
||||
|
||||
@@ -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<BleL2capInputStream>(connection)),
|
||||
output_stream_(std::make_unique<BleL2capOutputStream>(connection)),
|
||||
peripheral_(peripheral) {}
|
||||
peripheral_id_(peripheral_id) {}
|
||||
|
||||
BleL2capSocket::~BleL2capSocket() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
@@ -157,7 +157,8 @@ class BleMedium : public api::ble_v2::BleMedium {
|
||||
// On success, returns a new BleL2capSocket. On error, returns nullptr.
|
||||
std::unique_ptr<api::ble_v2::BleL2capSocket> 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.
|
||||
//
|
||||
|
||||
@@ -483,22 +483,24 @@ std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(const std::string &se
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleL2capSocket> 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<BlePeripheral *>(&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<BleL2capSocket> 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<api::ble_v2::BleL2capSocket> BleMedium::ConnectOverL2cap(
|
||||
// Connections layer.
|
||||
[connection requestDataConnectionWithCompletion:^(BOOL result) {
|
||||
if (result) {
|
||||
socket = std::make_unique<BleL2capSocket>(connection, non_empty_peripheral);
|
||||
socket = std::make_unique<BleL2capSocket>(connection, peripheral_id);
|
||||
}
|
||||
GTMLoggerInfo(result ? @"[NEARBY] Request data connection is ok"
|
||||
: @"[NEARBY] Request data connection is not ok");
|
||||
|
||||
@@ -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<BleL2capSocket> 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user