From 4ecbd3bca1288908b3b1a89d959ecaf368c90e81 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Thu, 6 Jul 2023 16:38:34 -0700 Subject: [PATCH] Filter devices based on type when listening for incoming connections. PiperOrigin-RevId: 546124493 --- connections/core.cc | 2 + connections/core.h | 2 + .../implementation/base_pcp_handler.cc | 20 +- connections/implementation/base_pcp_handler.h | 4 +- .../implementation/base_pcp_handler_test.cc | 326 +++++++++++++++++- .../implementation/p2p_cluster_pcp_handler.cc | 98 +++--- .../implementation/p2p_cluster_pcp_handler.h | 4 + 7 files changed, 387 insertions(+), 69 deletions(-) diff --git a/connections/core.cc b/connections/core.cc index fdbbc580..27729a85 100644 --- a/connections/core.cc +++ b/connections/core.cc @@ -342,6 +342,8 @@ void Core::StopDiscoveryV3(ResultCallback result_cb) { void Core::StartListeningForIncomingConnectionsV3( const v3::ConnectionListeningOptions& options, absl::string_view service_id, v3::ConnectionListener listener_cb, v3::ListeningResultListener result_cb) { + CHECK(options.listening_endpoint_type != NearbyDevice::Type::kUnknownDevice); + router_->StartListeningForIncomingConnectionsV3( &client_, service_id, std::move(listener_cb), options, std::move(result_cb)); diff --git a/connections/core.h b/connections/core.h index 017970be..f58c3962 100644 --- a/connections/core.h +++ b/connections/core.h @@ -337,6 +337,8 @@ class Core { // Starts listening for incoming connections. // // options - The options for listening for a connection. + // - options.listening_device_type will be checked to make sure it is one of + // kConnectionsDevice or kPresenceDevice before starting the operation. // service_id - The service ID to listen for. // listener_cb - The connection listener to broadcast any updates. // result_cb - to access the status of the operation when available. diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index 6aa13dc6..f26542a0 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -38,6 +38,7 @@ #include "connections/v3/connections_device.h" #include "connections/v3/listeners.h" #include "internal/flags/nearby_flags.h" +#include "internal/interop/device.h" #include "internal/platform/base64_utils.h" #include "internal/platform/bluetooth_connection_info.h" #include "internal/platform/bluetooth_utils.h" @@ -1379,7 +1380,8 @@ bool BasePcpHandler::IsPreferred( Exception BasePcpHandler::OnIncomingConnection( ClientProxy* client, const ByteArray& remote_endpoint_info, std::unique_ptr channel, - location::nearby::proto::connections::Medium medium) { + location::nearby::proto::connections::Medium medium, + NearbyDevice::Type listening_device_type) { absl::Time start_time = SystemClock::ElapsedRealtime(); // Fixes an NPE in ClientProxy.OnConnectionAccepted. The crash happened when @@ -1448,6 +1450,22 @@ Exception BasePcpHandler::OnIncomingConnection( return {Exception::kIo}; } + // Make sure we only accept connections from the device type we're explicitly + // listening to. + NearbyDevice::Type incoming_type = + connection_request.has_connections_device() + ? NearbyDevice::Type::kConnectionsDevice + : connection_request.has_presence_device() + ? NearbyDevice::Type::kPresenceDevice + // Legacy clients will be treated as Connections devices. + : NearbyDevice::Type::kConnectionsDevice; + if (listening_device_type != incoming_type) { + NEARBY_LOGS(WARNING) << "Device requesting a connection is the wrong type." + << "Expected type: " << listening_device_type + << ", got type: " << incoming_type; + return {Exception::kIo}; + } + // The ConnectionRequest frame has two fields that both contain the // EndpointInfo. The legacy field stores it as a string while the newer field // stores it as a byte array. We'll attempt to grab from the newer field, but diff --git a/connections/implementation/base_pcp_handler.h b/connections/implementation/base_pcp_handler.h index ada2c1c8..774e63fb 100644 --- a/connections/implementation/base_pcp_handler.h +++ b/connections/implementation/base_pcp_handler.h @@ -268,8 +268,8 @@ class BasePcpHandler : public PcpHandler, Exception OnIncomingConnection( ClientProxy* client, const ByteArray& remote_endpoint_info, std::unique_ptr endpoint_channel, - location::nearby::proto::connections::Medium - medium); // throws Exception::IO + location::nearby::proto::connections::Medium medium, + NearbyDevice::Type listening_device_type); // throws Exception::IO virtual bool HasOutgoingConnections(ClientProxy* client) const; virtual bool HasIncomingConnections(ClientProxy* client) const; diff --git a/connections/implementation/base_pcp_handler_test.cc b/connections/implementation/base_pcp_handler_test.cc index 38d365e0..304f9986 100644 --- a/connections/implementation/base_pcp_handler_test.cc +++ b/connections/implementation/base_pcp_handler_test.cc @@ -248,6 +248,16 @@ class MockPcpHandler : public BasePcpHandler { return BasePcpHandler::GetConnectionInfoFromResult(service_id, result); } + Exception OnIncomingConnection( + ClientProxy* client, const ByteArray& remote_endpoint_info, + std::unique_ptr endpoint_channel, + location::nearby::proto::connections::Medium medium, + NearbyDevice::Type listening_device_type) { + return BasePcpHandler::OnIncomingConnection(client, remote_endpoint_info, + std::move(endpoint_channel), + medium, listening_device_type); + } + bool NeedsToTurnOffAdvertisingMedium( location::nearby::proto::connections::Medium medium, const AdvertisingOptions& old_options, @@ -1327,7 +1337,6 @@ TEST_F(BasePcpHandlerTest, TestEndpointFoundStopsAlarm) { TEST_P(BasePcpHandlerTest, TestGetConnectionInfosFromMediums) { env_.Start(); - std::string service_id{"service"}; Mediums mediums; EndpointChannelManager endpoint_channel_manager; EndpointManager endpoint_manager(&endpoint_channel_manager); @@ -1340,7 +1349,7 @@ TEST_P(BasePcpHandlerTest, TestGetConnectionInfosFromMediums) { selector.web_rtc = true; std::vector infos = pcp_handler.GetConnectionInfoFromResult( - service_id, {.mediums = selector.GetMediums(true)}); + "service", {.mediums = selector.GetMediums(true)}); // Make sure we don't count webrtc. EXPECT_EQ(infos.size(), selector.Count(true) - 1); env_.Stop(); @@ -1364,8 +1373,8 @@ TEST_F(BasePcpHandlerTest, TestCanStartListeningForIncomingConnections) { .enable_ble_listening = true, .enable_bluetooth_listening = true, .enable_wlan_listening = true}; - pcp_handler.StartListeningForIncomingConnections(&client, "service_id", - options, {}); + pcp_handler.StartListeningForIncomingConnections(&client, "service", options, + {}); EXPECT_TRUE(client.IsListeningForIncomingConnections()); } @@ -1385,15 +1394,13 @@ TEST_F(BasePcpHandlerTest, TestStartListeningForIncomingConnectionsBadStatus) { .enable_ble_listening = true, .enable_bluetooth_listening = true, .enable_wlan_listening = true}; - pcp_handler.StartListeningForIncomingConnections(&client, "service_id", - options, {}); + pcp_handler.StartListeningForIncomingConnections(&client, "service", options, + {}); EXPECT_FALSE(client.IsListeningForIncomingConnections()); } TEST_F(BasePcpHandlerTest, TestCanStopListeningForIncomingConnections) { env_.Start(); - std::string service_id{"service"}; - std::string endpoint_id{"ABCD"}; ClientProxy client; Mediums m; EndpointChannelManager ecm; @@ -1409,7 +1416,7 @@ TEST_F(BasePcpHandlerTest, TestCanStopListeningForIncomingConnections) { .enable_ble_listening = true, .enable_bluetooth_listening = true, .enable_wlan_listening = true}; - pcp_handler.StartListeningForIncomingConnections(&client, service_id, options, + pcp_handler.StartListeningForIncomingConnections(&client, "service", options, {}); pcp_handler.StopListeningForIncomingConnections(&client); EXPECT_FALSE(client.IsListeningForIncomingConnections()); @@ -1418,8 +1425,6 @@ TEST_F(BasePcpHandlerTest, TestCanStopListeningForIncomingConnections) { TEST_F(BasePcpHandlerTest, TestWifiLanStopListeningForIncomingConnectionsSuccessWhenStopped) { env_.Start(); - std::string service_id{"service"}; - std::string endpoint_id{"ABCD"}; ClientProxy client; Mediums m; EndpointChannelManager ecm; @@ -1435,13 +1440,308 @@ TEST_F(BasePcpHandlerTest, .enable_ble_listening = true, .enable_bluetooth_listening = true, .enable_wlan_listening = true}; - pcp_handler.StartListeningForIncomingConnections(&client, service_id, options, + pcp_handler.StartListeningForIncomingConnections(&client, "service", options, {}); - m.GetWifiLan().StopAcceptingConnections(service_id); + m.GetWifiLan().StopAcceptingConnections("service"); pcp_handler.StopListeningForIncomingConnections(&client); EXPECT_FALSE(client.IsListeningForIncomingConnections()); } +TEST_F(BasePcpHandlerTest, TestDeviceFilterForConnectionsWithUnknown) { + env_.Start(); + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + v3::ConnectionListeningOptions options = { + .strategy = Strategy::kP2pCluster, + .enable_ble_listening = true, + .enable_bluetooth_listening = true, + .enable_wlan_listening = true, + .listening_endpoint_type = NearbyDevice::Type::kConnectionsDevice}; + EXPECT_CALL(pcp_handler, StartListeningForIncomingConnectionsImpl) + .WillOnce(Return( + MockPcpHandler::StartOperationResult{.status = {Status::kSuccess}})); + EXPECT_CALL(pcp_handler, CanReceiveIncomingConnection) + .WillRepeatedly(Return(true)); + EXPECT_TRUE( + pcp_handler + .StartListeningForIncomingConnections(&client, "service", options, {}) + .first.Ok()); + ASSERT_TRUE(client.IsListeningForIncomingConnections()); + ASSERT_TRUE(pcp_handler.CanReceiveIncomingConnection(&client)); + auto channel_pair = SetupConnection(pipe_a_, pipe_b_, Medium::BLUETOOTH); + ByteArray serialized_frame = parser::ForConnectionRequest({ + .local_endpoint_id = "ABCD", + .local_endpoint_info = ByteArray("local endpoint"), + }); + location::nearby::connections::OfflineFrame frame; + frame.ParseFromString(serialized_frame.AsStringView()); + frame.mutable_v1()->mutable_connection_request()->clear_connections_device(); + frame.mutable_v1()->mutable_connection_request()->clear_presence_device(); + ASSERT_FALSE(frame.v1().connection_request().has_connections_device()); + ASSERT_FALSE(frame.v1().connection_request().has_presence_device()); + // do a dummy write to get to the actual write. + channel_pair.first->Write(ByteArray()); + channel_pair.first->Write(ByteArray(frame.SerializeAsString())); + EXPECT_TRUE(pcp_handler + .OnIncomingConnection(&client, ByteArray("remote endpoint"), + std::move(channel_pair.second), + Medium::BLUETOOTH, + NearbyDevice::Type::kConnectionsDevice) + .Ok()); + env_.Stop(); +} + +TEST_F(BasePcpHandlerTest, TestDeviceFilterForPresenceWithUnknown) { + env_.Start(); + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + v3::ConnectionListeningOptions options = { + .strategy = Strategy::kP2pCluster, + .enable_ble_listening = true, + .enable_bluetooth_listening = true, + .enable_wlan_listening = true, + .listening_endpoint_type = NearbyDevice::Type::kPresenceDevice}; + EXPECT_CALL(pcp_handler, StartListeningForIncomingConnectionsImpl) + .WillOnce(Return( + MockPcpHandler::StartOperationResult{.status = {Status::kSuccess}})); + EXPECT_CALL(pcp_handler, CanReceiveIncomingConnection) + .WillRepeatedly(Return(true)); + EXPECT_TRUE( + pcp_handler + .StartListeningForIncomingConnections(&client, "service", options, {}) + .first.Ok()); + ASSERT_TRUE(client.IsListeningForIncomingConnections()); + ASSERT_TRUE(pcp_handler.CanReceiveIncomingConnection(&client)); + auto channel_pair = SetupConnection(pipe_a_, pipe_b_, Medium::BLUETOOTH); + ByteArray serialized_frame = parser::ForConnectionRequest({ + .local_endpoint_id = "ABCD", + .local_endpoint_info = ByteArray("local endpoint"), + }); + location::nearby::connections::OfflineFrame frame; + frame.ParseFromString(serialized_frame.AsStringView()); + frame.mutable_v1()->mutable_connection_request()->clear_connections_device(); + frame.mutable_v1()->mutable_connection_request()->clear_presence_device(); + ASSERT_FALSE(frame.v1().connection_request().has_connections_device()); + ASSERT_FALSE(frame.v1().connection_request().has_presence_device()); + // do a dummy write to get to the actual write. + channel_pair.first->Write(ByteArray()); + channel_pair.first->Write(ByteArray(frame.SerializeAsString())); + EXPECT_EQ(pcp_handler + .OnIncomingConnection(&client, ByteArray("remote endpoint"), + std::move(channel_pair.second), + Medium::BLUETOOTH, + NearbyDevice::Type::kPresenceDevice) + .value, + Exception::Value::kIo); + env_.Stop(); +} + +TEST_F(BasePcpHandlerTest, TestDeviceFilterForPresenceWithConnections) { + env_.Start(); + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + v3::ConnectionListeningOptions options = { + .strategy = Strategy::kP2pCluster, + .enable_ble_listening = true, + .enable_bluetooth_listening = true, + .enable_wlan_listening = true, + .listening_endpoint_type = NearbyDevice::Type::kPresenceDevice}; + EXPECT_CALL(pcp_handler, StartListeningForIncomingConnectionsImpl) + .WillOnce(Return( + MockPcpHandler::StartOperationResult{.status = {Status::kSuccess}})); + EXPECT_CALL(pcp_handler, CanReceiveIncomingConnection) + .WillRepeatedly(Return(true)); + EXPECT_TRUE( + pcp_handler + .StartListeningForIncomingConnections(&client, "service", options, {}) + .first.Ok()); + ASSERT_TRUE(client.IsListeningForIncomingConnections()); + ASSERT_TRUE(pcp_handler.CanReceiveIncomingConnection(&client)); + auto channel_pair = SetupConnection(pipe_a_, pipe_b_, Medium::BLUETOOTH); + ByteArray serialized_frame = parser::ForConnectionRequest({ + .local_endpoint_id = "ABCD", + .local_endpoint_info = ByteArray("local endpoint"), + }); + location::nearby::connections::OfflineFrame frame; + frame.ParseFromString(serialized_frame.AsStringView()); + frame.mutable_v1() + ->mutable_connection_request() + ->mutable_connections_device() + ->set_endpoint_id("ABCD"); + ASSERT_TRUE(frame.v1().connection_request().has_connections_device()); + // do a dummy write to get to the actual write. + channel_pair.first->Write(ByteArray()); + channel_pair.first->Write(ByteArray(frame.SerializeAsString())); + EXPECT_EQ(pcp_handler + .OnIncomingConnection(&client, ByteArray("remote endpoint"), + std::move(channel_pair.second), + Medium::BLUETOOTH, + NearbyDevice::Type::kPresenceDevice) + .value, + Exception::Value::kIo); + env_.Stop(); +} + +TEST_F(BasePcpHandlerTest, TestDeviceFilterForPresenceWithPresence) { + env_.Start(); + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + v3::ConnectionListeningOptions options = { + .strategy = Strategy::kP2pCluster, + .enable_ble_listening = true, + .enable_bluetooth_listening = true, + .enable_wlan_listening = true, + .listening_endpoint_type = NearbyDevice::Type::kPresenceDevice}; + EXPECT_CALL(pcp_handler, StartListeningForIncomingConnectionsImpl) + .WillOnce(Return( + MockPcpHandler::StartOperationResult{.status = {Status::kSuccess}})); + EXPECT_CALL(pcp_handler, CanReceiveIncomingConnection) + .WillRepeatedly(Return(true)); + EXPECT_TRUE( + pcp_handler + .StartListeningForIncomingConnections(&client, "service", options, {}) + .first.Ok()); + ASSERT_TRUE(client.IsListeningForIncomingConnections()); + ASSERT_TRUE(pcp_handler.CanReceiveIncomingConnection(&client)); + auto channel_pair = SetupConnection(pipe_a_, pipe_b_, Medium::BLUETOOTH); + ByteArray serialized_frame = parser::ForConnectionRequest({ + .local_endpoint_id = "ABCD", + .local_endpoint_info = ByteArray("local endpoint"), + }); + location::nearby::connections::OfflineFrame frame; + frame.ParseFromString(serialized_frame.AsStringView()); + frame.mutable_v1() + ->mutable_connection_request() + ->mutable_presence_device() + ->set_endpoint_id("ABCD"); + ASSERT_TRUE(frame.v1().connection_request().has_presence_device()); + // do a dummy write to get to the actual write. + channel_pair.first->Write(ByteArray()); + channel_pair.first->Write(ByteArray(frame.SerializeAsString())); + EXPECT_TRUE(pcp_handler + .OnIncomingConnection(&client, ByteArray("remote endpoint"), + std::move(channel_pair.second), + Medium::BLUETOOTH, + NearbyDevice::Type::kPresenceDevice) + .Ok()); + env_.Stop(); +} + +TEST_F(BasePcpHandlerTest, TestDeviceFilterForConnectionsWithConnections) { + env_.Start(); + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + v3::ConnectionListeningOptions options = { + .strategy = Strategy::kP2pCluster, + .enable_ble_listening = true, + .enable_bluetooth_listening = true, + .enable_wlan_listening = true, + .listening_endpoint_type = NearbyDevice::Type::kConnectionsDevice}; + EXPECT_CALL(pcp_handler, StartListeningForIncomingConnectionsImpl) + .WillOnce(Return( + MockPcpHandler::StartOperationResult{.status = {Status::kSuccess}})); + EXPECT_CALL(pcp_handler, CanReceiveIncomingConnection) + .WillRepeatedly(Return(true)); + EXPECT_TRUE( + pcp_handler + .StartListeningForIncomingConnections(&client, "service", options, {}) + .first.Ok()); + ASSERT_TRUE(client.IsListeningForIncomingConnections()); + ASSERT_TRUE(pcp_handler.CanReceiveIncomingConnection(&client)); + auto channel_pair = SetupConnection(pipe_a_, pipe_b_, Medium::BLUETOOTH); + ByteArray serialized_frame = parser::ForConnectionRequest({ + .local_endpoint_id = "ABCD", + .local_endpoint_info = ByteArray("local endpoint"), + }); + location::nearby::connections::OfflineFrame frame; + frame.ParseFromString(serialized_frame.AsStringView()); + frame.mutable_v1() + ->mutable_connection_request() + ->mutable_connections_device() + ->set_endpoint_id("ABCD"); + ASSERT_TRUE(frame.v1().connection_request().has_connections_device()); + // do a dummy write to get to the actual write. + channel_pair.first->Write(ByteArray()); + channel_pair.first->Write(ByteArray(frame.SerializeAsString())); + EXPECT_TRUE(pcp_handler + .OnIncomingConnection(&client, ByteArray("remote endpoint"), + std::move(channel_pair.second), + Medium::BLUETOOTH, + NearbyDevice::Type::kConnectionsDevice) + .Ok()); + env_.Stop(); +} + +TEST_F(BasePcpHandlerTest, TestDeviceFilterForConnectionsWithPresence) { + env_.Start(); + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + v3::ConnectionListeningOptions options = { + .strategy = Strategy::kP2pCluster, + .enable_ble_listening = true, + .enable_bluetooth_listening = true, + .enable_wlan_listening = true, + .listening_endpoint_type = NearbyDevice::Type::kConnectionsDevice}; + EXPECT_CALL(pcp_handler, StartListeningForIncomingConnectionsImpl) + .WillOnce(Return( + MockPcpHandler::StartOperationResult{.status = {Status::kSuccess}})); + EXPECT_CALL(pcp_handler, CanReceiveIncomingConnection) + .WillRepeatedly(Return(true)); + EXPECT_TRUE( + pcp_handler + .StartListeningForIncomingConnections(&client, "service", options, {}) + .first.Ok()); + ASSERT_TRUE(client.IsListeningForIncomingConnections()); + ASSERT_TRUE(pcp_handler.CanReceiveIncomingConnection(&client)); + auto channel_pair = SetupConnection(pipe_a_, pipe_b_, Medium::BLUETOOTH); + ByteArray serialized_frame = parser::ForConnectionRequest({ + .local_endpoint_id = "ABCD", + .local_endpoint_info = ByteArray("local endpoint"), + }); + location::nearby::connections::OfflineFrame frame; + frame.ParseFromString(serialized_frame.AsStringView()); + frame.mutable_v1() + ->mutable_connection_request() + ->mutable_presence_device() + ->set_endpoint_id("ABCD"); + ASSERT_TRUE(frame.v1().connection_request().has_presence_device()); + // do a dummy write to get to the actual write. + channel_pair.first->Write(ByteArray()); + channel_pair.first->Write(ByteArray(frame.SerializeAsString())); + EXPECT_EQ(pcp_handler + .OnIncomingConnection(&client, ByteArray("remote endpoint"), + std::move(channel_pair.second), + Medium::BLUETOOTH, + NearbyDevice::Type::kConnectionsDevice) + .value, + Exception::Value::kIo); + env_.Stop(); +} + TEST_F(BasePcpHandlerTest, TestNeedsToTurnOffAdvertisingMedium) { Mediums m; EndpointChannelManager ecm; diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index f68e99a6..e9779d0b 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -35,6 +35,7 @@ #include "connections/power_level.h" #include "connections/status.h" #include "internal/flags/nearby_flags.h" +#include "internal/interop/device.h" #include "internal/platform/logging.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/types.h" @@ -1154,7 +1155,8 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( std::string(service_id), {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client_proxy, local_endpoint_id)})) { + this, client_proxy, local_endpoint_id, + options.listening_endpoint_type)})) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on Bluetooth"; } else { @@ -1172,7 +1174,8 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( std::string(service_id), {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, - client_proxy, local_endpoint_id)})) { + client_proxy, local_endpoint_id, + options.listening_endpoint_type)})) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on ble_v2"; } else { @@ -1187,7 +1190,8 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( std::string(service_id), {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, - client_proxy, local_endpoint_id)})) { + client_proxy, local_endpoint_id, + options.listening_endpoint_type)})) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on ble"; } else { @@ -1201,7 +1205,8 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( std::string(service_id), {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler, this, - client_proxy, local_endpoint_id, "")})) { + client_proxy, local_endpoint_id, "", + options.listening_endpoint_type)})) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on wifi_lan"; } else { @@ -1382,7 +1387,8 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( void P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler( ClientProxy* client, absl::string_view local_endpoint_info, - const std::string& service_id, BluetoothSocket socket) { + NearbyDevice::Type device_type, const std::string& service_id, + BluetoothSocket socket) { if (!socket.IsValid()) { NEARBY_LOGS(WARNING) << "Invalid socket in accept callback(" << absl::BytesToHexString(local_endpoint_info) @@ -1391,7 +1397,7 @@ void P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler( } RunOnPcpHandlerThread( "p2p-bt-on-incoming-connection", - [this, client, service_id, socket = std::move(socket)]() + [this, client, service_id, socket = std::move(socket), device_type]() RUN_ON_PCP_HANDLER_THREAD() mutable { std::string remote_device_name = socket.GetRemoteDevice().GetName(); auto channel = std::make_unique( @@ -1401,7 +1407,8 @@ void P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler( OnIncomingConnection( client, remote_device_info, std::move(channel), - location::nearby::proto::connections::Medium::BLUETOOTH); + location::nearby::proto::connections::Medium::BLUETOOTH, + device_type); }); } @@ -1422,7 +1429,8 @@ P2pClusterPcpHandler::StartBluetoothAdvertising( service_id, {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client, local_endpoint_info.AsStringView())})) { + this, client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice)})) { NEARBY_LOGS(WARNING) << "In StartBluetoothAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1544,7 +1552,8 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl( void P2pClusterPcpHandler::BleConnectionAcceptedHandler( ClientProxy* client, absl::string_view local_endpoint_info, - BleSocket socket, const std::string& service_id) { + NearbyDevice::Type device_type, BleSocket socket, + const std::string& service_id) { if (!socket.IsValid()) { NEARBY_LOGS(WARNING) << "Invalid socket in accept callback(" << absl::BytesToHexString(local_endpoint_info) @@ -1553,8 +1562,8 @@ void P2pClusterPcpHandler::BleConnectionAcceptedHandler( } RunOnPcpHandlerThread( "p2p-ble-on-incoming-connection", - [this, client, service_id, - socket = std::move(socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable { + [this, client, service_id, socket = std::move(socket), + device_type]() RUN_ON_PCP_HANDLER_THREAD() mutable { std::string remote_peripheral_name = socket.GetRemotePeripheral().GetName(); auto channel = std::make_unique( @@ -1564,7 +1573,8 @@ void P2pClusterPcpHandler::BleConnectionAcceptedHandler( socket.GetRemotePeripheral().GetAdvertisementBytes(service_id); OnIncomingConnection(client, remote_peripheral_info, std::move(channel), - location::nearby::proto::connections::Medium::BLE); + location::nearby::proto::connections::Medium::BLE, + device_type); }); } @@ -1591,7 +1601,8 @@ P2pClusterPcpHandler::StartBleAdvertising( service_id, {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, - client, local_endpoint_info.AsStringView())})) { + client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice)})) { NEARBY_LOGS(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1618,7 +1629,8 @@ P2pClusterPcpHandler::StartBleAdvertising( service_id, {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client, local_endpoint_info.AsStringView())})) { + this, client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice)})) { NEARBY_LOGS(WARNING) << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1755,7 +1767,8 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleConnectImpl( void P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler( ClientProxy* client, absl::string_view local_endpoint_info, - BleV2Socket socket, const std::string& service_id) { + NearbyDevice::Type device_type, BleV2Socket socket, + const std::string& service_id) { if (!socket.IsValid()) { NEARBY_LOGS(WARNING) << "Invalid socket in accept callback(" << absl::BytesToHexString(local_endpoint_info) @@ -1764,14 +1777,15 @@ void P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler( } RunOnPcpHandlerThread( "p2p-ble-on-incoming-connection", - [this, client, service_id, + [this, client, service_id, device_type, socket = std::move(socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable { ByteArray remote_peripheral_info = socket.GetRemotePeripheral().GetId(); auto channel = std::make_unique( service_id, std::string(remote_peripheral_info), socket); OnIncomingConnection(client, remote_peripheral_info, std::move(channel), - location::nearby::proto::connections::Medium::BLE); + location::nearby::proto::connections::Medium::BLE, + device_type); }); } @@ -1792,7 +1806,8 @@ P2pClusterPcpHandler::StartBleV2Advertising( service_id, {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, - client, local_endpoint_info.AsStringView())})) { + client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice)})) { NEARBY_LOGS(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1819,35 +1834,11 @@ P2pClusterPcpHandler::StartBleV2Advertising( !bluetooth_medium_.IsAcceptingConnections(service_id)) { if (!bluetooth_radio_.Enable() || !bluetooth_medium_.StartAcceptingConnections( - service_id, {.accepted_cb = [this, client, local_endpoint_info]( - const std::string& service_id, - BluetoothSocket socket) { - if (!socket.IsValid()) { - NEARBY_LOGS(WARNING) - << "In BT StartAcceptingConnections.accepted_cb(" - << absl::BytesToHexString(local_endpoint_info.data()) - << "), client=" << client->GetClientId() - << ": Invalid socket in accept callback."; - return; - } - RunOnPcpHandlerThread( - "p2p-bt-on-incoming-connection", - [this, client, local_endpoint_info, service_id, - socket = std::move(socket)]() - RUN_ON_PCP_HANDLER_THREAD() mutable { - std::string remote_device_name = - socket.GetRemoteDevice().GetName(); - auto channel = - std::make_unique( - service_id, remote_device_name, socket); - ByteArray remote_device_info{remote_device_name}; - - OnIncomingConnection( - client, remote_device_info, std::move(channel), - location::nearby::proto::connections::Medium:: - BLUETOOTH); - }); - }})) { + service_id, + {.accepted_cb = absl::bind_front( + &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, + this, client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice)})) { NEARBY_LOGS(WARNING) << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1985,8 +1976,8 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleV2ConnectImpl( void P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler( ClientProxy* client, absl::string_view local_endpoint_id, - absl::string_view local_endpoint_info, const std::string& service_id, - WifiLanSocket socket) { + absl::string_view local_endpoint_info, NearbyDevice::Type device_type, + const std::string& service_id, WifiLanSocket socket) { if (!socket.IsValid()) { NEARBY_LOGS(WARNING) << "Invalid socket in accept callback(" << absl::BytesToHexString(local_endpoint_info) @@ -1995,7 +1986,7 @@ void P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler( } RunOnPcpHandlerThread( "p2p-wifi-on-incoming-connection", - [this, client, local_endpoint_id, service_id, + [this, client, local_endpoint_id, service_id, device_type, socket = std::move(socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable { std::string remote_service_name = std::string(local_endpoint_id); auto channel = std::make_unique( @@ -2004,7 +1995,8 @@ void P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler( OnIncomingConnection( client, remote_service_name_byte, std::move(channel), - location::nearby::proto::connections::Medium::WIFI_LAN); + location::nearby::proto::connections::Medium::WIFI_LAN, + device_type); }); } @@ -2022,8 +2014,8 @@ P2pClusterPcpHandler::StartWifiLanAdvertising( service_id, {.accepted_cb = absl::bind_front( &P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler, this, - client, local_endpoint_id, - local_endpoint_info.AsStringView())})) { + client, local_endpoint_id, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice)})) { NEARBY_LOGS(WARNING) << "In StartWifiLanAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) diff --git a/connections/implementation/p2p_cluster_pcp_handler.h b/connections/implementation/p2p_cluster_pcp_handler.h index ac44ebf6..93ae269b 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.h +++ b/connections/implementation/p2p_cluster_pcp_handler.h @@ -173,6 +173,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { BluetoothDevice& device); void BluetoothConnectionAcceptedHandler(ClientProxy* client, absl::string_view local_endpoint_info, + NearbyDevice::Type device_type, const std::string& service_id, BluetoothSocket socket); location::nearby::proto::connections::Medium StartBluetoothAdvertising( @@ -197,6 +198,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { const std::string& service_id); void BleConnectionAcceptedHandler(ClientProxy* client, absl::string_view local_endpoint_info, + NearbyDevice::Type device_type, BleSocket socket, const std::string& service_id); location::nearby::proto::connections::Medium StartBleAdvertising( @@ -226,6 +228,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { bool fast_advertisement); void BleV2ConnectionAcceptedHandler(ClientProxy* client, absl::string_view local_endpoint_info, + NearbyDevice::Type device_type, BleV2Socket socket, const std::string& service_id); location::nearby::proto::connections::Medium StartBleV2Advertising( @@ -252,6 +255,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { void WifiLanConnectionAcceptedHandler(ClientProxy* client, absl::string_view local_endpoint_id, absl::string_view local_endpoint_info, + NearbyDevice::Type device_type, const std::string& service_id, WifiLanSocket socket); location::nearby::proto::connections::Medium StartWifiLanAdvertising(