Isolate Ble accept handler logic

PiperOrigin-RevId: 540401997
This commit is contained in:
Anay Wadhera
2023-06-14 15:47:43 -07:00
committed by Copybara-Service
parent 169087dc96
commit cf9170ba16
2 changed files with 35 additions and 29 deletions
@@ -18,6 +18,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/functional/bind_front.h"
#include "absl/strings/escaping.h"
@@ -1301,6 +1302,32 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl(
};
}
void P2pClusterPcpHandler::BleConnectionAcceptedHandler(
ClientProxy* client, absl::string_view local_endpoint_info,
BleSocket socket, const std::string& service_id) {
if (!socket.IsValid()) {
NEARBY_LOGS(WARNING) << "Invalid socket in accept callback("
<< absl::BytesToHexString(local_endpoint_info)
<< "), client=" << client->GetClientId();
return;
}
RunOnPcpHandlerThread(
"p2p-ble-on-incoming-connection",
[this, client, service_id,
socket = std::move(socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable {
std::string remote_peripheral_name =
socket.GetRemotePeripheral().GetName();
auto channel = std::make_unique<BleEndpointChannel>(
service_id,
/*channel_name=*/remote_peripheral_name, socket);
ByteArray remote_peripheral_info =
socket.GetRemotePeripheral().GetAdvertisementBytes(service_id);
OnIncomingConnection(client, remote_peripheral_info, std::move(channel),
location::nearby::proto::connections::Medium::BLE);
});
}
location::nearby::proto::connections::Medium
P2pClusterPcpHandler::StartBleAdvertising(
ClientProxy* client, const std::string& service_id,
@@ -1321,35 +1348,10 @@ P2pClusterPcpHandler::StartBleAdvertising(
if (!ble_medium_.IsAcceptingConnections(service_id)) {
if (!bluetooth_radio_.Enable() ||
!ble_medium_.StartAcceptingConnections(
service_id, {.accepted_cb = [this, client, local_endpoint_info](
BleSocket socket,
const std::string& service_id) {
if (!socket.IsValid()) {
NEARBY_LOGS(WARNING)
<< "Invalid socket in accept callback("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId();
return;
}
RunOnPcpHandlerThread(
"p2p-ble-on-incoming-connection",
[this, client, local_endpoint_info, service_id,
socket = std::move(socket)]()
RUN_ON_PCP_HANDLER_THREAD() mutable {
std::string remote_peripheral_name =
socket.GetRemotePeripheral().GetName();
auto channel = std::make_unique<BleEndpointChannel>(
service_id,
/*channel_name=*/remote_peripheral_name, socket);
ByteArray remote_peripheral_info =
socket.GetRemotePeripheral().GetAdvertisementBytes(
service_id);
OnIncomingConnection(
client, remote_peripheral_info, std::move(channel),
location::nearby::proto::connections::Medium::BLE);
});
}})) {
service_id,
{.accepted_cb = absl::bind_front(
&P2pClusterPcpHandler::BleConnectionAcceptedHandler, this,
client, local_endpoint_info.AsStringView())})) {
NEARBY_LOGS(WARNING)
<< "In StartBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -167,6 +167,10 @@ class P2pClusterPcpHandler : public BasePcpHandler {
bool fast_advertisement);
void BlePeripheralLostHandler(ClientProxy* client, BlePeripheral& peripheral,
const std::string& service_id);
void BleConnectionAcceptedHandler(ClientProxy* client,
absl::string_view local_endpoint_info,
BleSocket socket,
const std::string& service_id);
location::nearby::proto::connections::Medium StartBleAdvertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id,