[BLEREFACTOR]: Refactor BLE/L2CAP endpoint channel

PiperOrigin-RevId: 830711004
This commit is contained in:
Edwin Wu
2025-11-10 20:15:50 -08:00
committed by Copybara-Service
parent fdffc105e0
commit 1e7611f61b
6 changed files with 167 additions and 24 deletions
@@ -44,6 +44,8 @@ namespace connections {
namespace {
using ::location::nearby::analytics::proto::ConnectionsLog;
using ::location::nearby::proto::connections::Medium::BLE;
using ::location::nearby::proto::connections::Medium::BLE_L2CAP;
using DisconnectionReason =
::location::nearby::proto::connections::DisconnectionReason;
@@ -130,7 +132,15 @@ ExceptionOr<ByteArray> BaseEndpointChannel::Read(
MutexLock lock(&reader_mutex_);
packet_meta_data.StartSocketIo();
ExceptionOr<std::int32_t> read_int = ReadInt(reader_);
ExceptionOr<std::int32_t> read_int;
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kRefactorBleL2cap)) {
// TODO(edwinwu): Implement the new read logic.
return ExceptionOr<ByteArray>(Exception::kFailed);
} else {
read_int = ReadInt(reader_);
}
if (!read_int.ok()) {
return ExceptionOr<ByteArray>(read_int.exception());
}
@@ -249,8 +259,16 @@ Exception BaseEndpointChannel::Write(const ByteArray& data,
}
packet_meta_data.StartSocketIo();
Exception write_exception =
WriteInt(writer_, static_cast<std::int32_t>(data_size));
Exception write_exception;
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kRefactorBleL2cap) &&
(GetMedium() == BLE || GetMedium() == BLE_L2CAP)) {
// TODO(edwinwu): Implement the new write logic.
write_exception = {Exception::kFailed};
} else {
write_exception = WriteInt(writer_, static_cast<std::int32_t>(data_size));
}
if (write_exception.Raised()) {
LOG(WARNING) << __func__
<< ": Failed to write header: " << write_exception.value;
@@ -14,10 +14,12 @@
#include "connections/implementation/ble_endpoint_channel.h"
#include <memory>
#include <string>
#include <utility>
#include "connections/implementation/base_endpoint_channel.h"
#include "connections/implementation/mediums/ble/ble_socket.h"
#include "internal/platform/ble.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
@@ -43,6 +45,20 @@ InputStream* GetInputStreamOrNull(BleSocket& socket) {
return nullptr;
}
OutputStream* GetOutputStreamOrNull(mediums::BleSocket* socket) {
if (socket != nullptr && socket->IsValid()) {
return &socket->GetOutputStream();
}
return nullptr;
}
InputStream* GetInputStreamOrNull(mediums::BleSocket* socket) {
if (socket != nullptr && socket->IsValid()) {
return &socket->GetInputStream();
}
return nullptr;
}
} // namespace
BleEndpointChannel::BleEndpointChannel(const std::string& service_id,
@@ -53,6 +69,14 @@ BleEndpointChannel::BleEndpointChannel(const std::string& service_id,
GetOutputStreamOrNull(socket)),
ble_socket_(std::move(socket)) {}
BleEndpointChannel::BleEndpointChannel(
const std::string& service_id, const std::string& channel_name,
std::unique_ptr<mediums::BleSocket> socket)
: BaseEndpointChannel(service_id, channel_name,
GetInputStreamOrNull(socket.get()),
GetOutputStreamOrNull(socket.get())),
ble_socket_2_(std::move(socket)) {}
location::nearby::proto::connections::Medium BleEndpointChannel::GetMedium()
const {
return location::nearby::proto::connections::Medium::BLE;
@@ -63,11 +87,33 @@ int BleEndpointChannel::GetMaxTransmitPacketSize() const {
}
void BleEndpointChannel::CloseImpl() {
Exception status = ble_socket_.Close();
if (!status.Ok()) {
LOG(WARNING) << "Failed to close underlying socket for BleEndpointChannel "
<< GetName() << ": exception=" << status.value;
if (ble_socket_2_ != nullptr) {
if (!ble_socket_2_->IsValid()) {
LOG(WARNING) << "BleEndpointChannel " << GetName()
<< " is already closed.";
return;
}
Exception status = ble_socket_2_->Close();
if (!status.Ok()) {
LOG(WARNING)
<< "Failed to close underlying socket for BleEndpointChannel "
<< GetName() << ": exception=" << status.value;
}
} else {
if (!ble_socket_.IsValid()) {
LOG(WARNING) << "BleEndpointChannel " << GetName()
<< " is already closed.";
return;
}
Exception status = ble_socket_.Close();
if (!status.Ok()) {
LOG(WARNING)
<< "Failed to close underlying socket for BleEndpointChannel "
<< GetName() << ": exception=" << status.value;
}
}
LOG(INFO) << "BleEndpointChannel " << GetName() << " is already closed.";
}
} // namespace connections
@@ -15,9 +15,11 @@
#ifndef CONNECTIONS_IMPLEMENTATION_BLE_ENDPOINT_CHANNEL_H_
#define CONNECTIONS_IMPLEMENTATION_BLE_ENDPOINT_CHANNEL_H_
#include <memory>
#include <string>
#include "connections/implementation/base_endpoint_channel.h"
#include "connections/implementation/mediums/ble/ble_socket.h"
#include "internal/platform/ble.h"
namespace nearby {
@@ -29,6 +31,11 @@ class BleEndpointChannel final : public BaseEndpointChannel {
BleEndpointChannel(const std::string& service_id,
const std::string& channel_name, BleSocket socket);
// A refactor version of the above constructor.
BleEndpointChannel(const std::string& service_id,
const std::string& channel_name,
std::unique_ptr<mediums::BleSocket> socket);
location::nearby::proto::connections::Medium GetMedium() const override;
int GetMaxTransmitPacketSize() const override;
@@ -39,6 +46,7 @@ class BleEndpointChannel final : public BaseEndpointChannel {
void CloseImpl() override;
BleSocket ble_socket_;
std::unique_ptr<mediums::BleSocket> ble_socket_2_ = nullptr;
};
} // namespace connections
@@ -14,10 +14,12 @@
#include "connections/implementation/ble_l2cap_endpoint_channel.h"
#include <memory>
#include <string>
#include <utility>
#include "connections/implementation/base_endpoint_channel.h"
#include "connections/implementation/mediums/ble/ble_socket.h"
#include "internal/platform/ble.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
@@ -45,6 +47,20 @@ InputStream* GetInputStreamOrNull(BleL2capSocket& socket) {
return nullptr;
}
OutputStream* GetOutputStreamOrNull(mediums::BleSocket* socket) {
if (socket != nullptr && socket->IsValid()) {
return &socket->GetOutputStream();
}
return nullptr;
}
InputStream* GetInputStreamOrNull(mediums::BleSocket* socket) {
if (socket != nullptr && socket->IsValid()) {
return &socket->GetInputStream();
}
return nullptr;
}
} // namespace
BleL2capEndpointChannel::BleL2capEndpointChannel(
@@ -55,6 +71,14 @@ BleL2capEndpointChannel::BleL2capEndpointChannel(
GetOutputStreamOrNull(socket)),
ble_l2cap_socket_(std::move(socket)) {}
BleL2capEndpointChannel::BleL2capEndpointChannel(
const std::string& service_id, const std::string& channel_name,
std::unique_ptr<mediums::BleSocket> socket)
: BaseEndpointChannel(service_id, channel_name,
GetInputStreamOrNull(socket.get()),
GetOutputStreamOrNull(socket.get())),
ble_l2cap_socket_2_(std::move(socket)) {}
location::nearby::proto::connections::Medium
BleL2capEndpointChannel::GetMedium() const {
return location::nearby::proto::connections::Medium::BLE_L2CAP;
@@ -65,12 +89,33 @@ int BleL2capEndpointChannel::GetMaxTransmitPacketSize() const {
}
void BleL2capEndpointChannel::CloseImpl() {
Exception status = ble_l2cap_socket_.Close();
if (!status.Ok()) {
LOG(WARNING)
<< "Failed to close underlying socket for BleL2capEndpointChannel "
<< GetName() << ": exception=" << status.value;
if (ble_l2cap_socket_2_ != nullptr) {
if (!ble_l2cap_socket_2_->IsValid()) {
LOG(WARNING) << "BleL2capEndpointChannel " << GetName()
<< " is already closed.";
return;
}
Exception status = ble_l2cap_socket_2_->Close();
if (!status.Ok()) {
LOG(WARNING)
<< "Failed to close underlying socket for BleL2capEndpointChannel "
<< GetName() << ": exception=" << status.value;
}
} else {
if (!ble_l2cap_socket_.IsValid()) {
LOG(WARNING) << "BleL2capEndpointChannel " << GetName()
<< " is already closed.";
return;
}
Exception status = ble_l2cap_socket_.Close();
if (!status.Ok()) {
LOG(WARNING)
<< "Failed to close underlying socket for BleL2capEndpointChannel "
<< GetName() << ": exception=" << status.value;
}
}
LOG(INFO) << "BleL2capEndpointChannel " << GetName() << " is already closed.";
}
} // namespace connections
@@ -15,9 +15,11 @@
#ifndef CORE_INTERNAL_BLE_L2CAP_ENDPOINT_CHANNEL_H_
#define CORE_INTERNAL_BLE_L2CAP_ENDPOINT_CHANNEL_H_
#include <memory>
#include <string>
#include "connections/implementation/base_endpoint_channel.h"
#include "connections/implementation/mediums/ble/ble_socket.h"
#include "internal/platform/ble.h"
namespace nearby {
@@ -30,6 +32,11 @@ class BleL2capEndpointChannel final : public BaseEndpointChannel {
const std::string& channel_name,
BleL2capSocket socket);
// A refactor version of the above constructor.
BleL2capEndpointChannel(const std::string& service_id,
const std::string& channel_name,
std::unique_ptr<mediums::BleSocket> socket);
// Returns the medium of this endpoint channel.
location::nearby::proto::connections::Medium GetMedium() const override;
@@ -40,6 +47,7 @@ class BleL2capEndpointChannel final : public BaseEndpointChannel {
void CloseImpl() override;
BleL2capSocket ble_l2cap_socket_;
std::unique_ptr<mediums::BleSocket> ble_l2cap_socket_2_ = nullptr;
};
} // namespace connections
@@ -2173,7 +2173,26 @@ void P2pClusterPcpHandler::BleConnectionAcceptedHandler2(
<< "), client=" << client->GetClientId();
return;
}
// TODO: edwinwu - Implement Ble endpoint channel for refactored version.
RunOnPcpHandlerThread(
"p2p-ble-on-incoming-connection",
[this, client, service_id, device_type, socket = std::move(socket)]()
RUN_ON_PCP_HANDLER_THREAD() mutable {
ByteArray remote_peripheral_info =
socket->GetRemotePeripheral().GetId();
if (socket->GetMedium() == Medium::BLE) {
auto channel = std::make_unique<BleEndpointChannel>(
service_id, std::string(remote_peripheral_info),
std::move(socket));
OnIncomingConnection(client, remote_peripheral_info,
std::move(channel), BLE, device_type);
} else {
auto channel = std::make_unique<BleL2capEndpointChannel>(
service_id, std::string(remote_peripheral_info),
std::move(socket));
OnIncomingConnection(client, remote_peripheral_info,
std::move(channel), BLE, device_type);
}
});
}
ErrorOr<Medium> P2pClusterPcpHandler::StartBleAdvertising(
@@ -2454,12 +2473,14 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleConnectImpl(
LOG(INFO) << "In BleV2ConnectImpl(), connected to Ble L2CAP device "
<< absl::BytesToHexString(peripheral.GetId().data())
<< " for endpoint(id=" << endpoint->endpoint_id << ").";
// TODO: edwinwu - Change to refactored version of
// BleL2capEndpointChannel
auto channel = std::make_unique<BleL2capEndpointChannel>(
endpoint->service_id, /*channel_name=*/endpoint->endpoint_id,
std::move(ble_l2cap_socket_result.value()));
return BasePcpHandler::ConnectImplResult{
.status = {Status::kBleError},
.operation_result_code =
ble_l2cap_socket_result.error().operation_result_code().value(),
.medium = BLE,
.status = {Status::kSuccess},
.operation_result_code = OperationResultCode::DETAIL_SUCCESS,
.endpoint_channel = std::move(channel),
};
} else {
LOG(WARNING) << "In BleConnectImpl(), failed to connect to Ble L2CAP "
@@ -2510,12 +2531,9 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleConnectImpl(
ble_socket_result.error().operation_result_code().value(),
};
}
// TODO: edwinwu - Change to refactor version of
// BleV2EndpointChannel
return BasePcpHandler::ConnectImplResult{
.status = {Status::kBleError},
.operation_result_code = OperationResultCode::DETAIL_UNKNOWN,
};
channel = std::make_unique<BleEndpointChannel>(
endpoint->service_id, /*channel_name=*/endpoint->endpoint_id,
std::move(ble_socket_result.value()));
} else {
ErrorOr<BleSocket> ble_socket_result =
ble_medium_.Connect(endpoint->service_id, peripheral,