diff --git a/cpp/core/internal/mediums/webrtc.cc b/cpp/core/internal/mediums/webrtc.cc index 96cdec3d..34c8f63e 100644 --- a/cpp/core/internal/mediums/webrtc.cc +++ b/cpp/core/internal/mediums/webrtc.cc @@ -54,8 +54,8 @@ bool WebRtc::IsAcceptingConnections(const std::string& service_id) { return role_ == Role::kOfferer; } -bool WebRtc::StartAcceptingConnections(const PeerId& self_id, - const std::string& service_id, +bool WebRtc::StartAcceptingConnections(const std::string& service_id, + const PeerId& self_id, const LocationHint& location_hint, AcceptedConnectionCallback callback) { if (!IsAvailable()) { diff --git a/cpp/core/internal/mediums/webrtc.h b/cpp/core/internal/mediums/webrtc.h index 33f39008..f09a3063 100644 --- a/cpp/core/internal/mediums/webrtc.h +++ b/cpp/core/internal/mediums/webrtc.h @@ -61,8 +61,8 @@ class WebRtc { // Prepares the device to accept incoming WebRtc connections. Returns a // boolean value indicating if the device has started accepting connections. // Runs on @MainThread. - bool StartAcceptingConnections(const PeerId& self_id, - const std::string& service_id, + bool StartAcceptingConnections(const std::string& service_id, + const PeerId& self_id, const LocationHint& location_hint, AcceptedConnectionCallback callback) ABSL_LOCKS_EXCLUDED(mutex_); diff --git a/cpp/core/internal/mediums/webrtc_test.cc b/cpp/core/internal/mediums/webrtc_test.cc index ae5c599e..68d05686 100644 --- a/cpp/core/internal/mediums/webrtc_test.cc +++ b/cpp/core/internal/mediums/webrtc_test.cc @@ -43,10 +43,10 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {mock_accepted_callback_.AsStdFunction()})); EXPECT_FALSE(webrtc.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {mock_accepted_callback_.AsStdFunction()})); EXPECT_TRUE(webrtc.IsAcceptingConnections(std::string{})); } @@ -64,7 +64,7 @@ TEST_F(WebRtcTest, Connect_DataChannelTimeOut) { EXPECT_FALSE(wrapper_1.IsValid()); EXPECT_TRUE(webrtc.StartAcceptingConnections( - peer_id, service_id, location_hint, AcceptedConnectionCallback())); + service_id, peer_id, location_hint, AcceptedConnectionCallback())); } // Tests the flow when the device calls Connect() after calling @@ -81,14 +81,14 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {mock_accepted_callback_.AsStdFunction()})); WebRtcSocketWrapper wrapper = webrtc.Connect(PeerId("random_peer_id"), location_hint); EXPECT_TRUE(webrtc.IsAcceptingConnections(std::string{})); EXPECT_FALSE(wrapper.IsValid()); EXPECT_FALSE(webrtc.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {mock_accepted_callback_.AsStdFunction()})); } @@ -106,7 +106,7 @@ TEST_F(WebRtcTest, StartAndStopAcceptingConnections) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {mock_accepted_callback_.AsStdFunction()})); webrtc.StopAcceptingConnections(service_id); EXPECT_FALSE(webrtc.IsAcceptingConnections(std::string{})); @@ -124,7 +124,7 @@ TEST_F(WebRtcTest, ConnectTwice) { ByteArray message("message xyz"); receiver.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {[&receiver_socket, connected](WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); @@ -133,7 +133,7 @@ TEST_F(WebRtcTest, ConnectTwice) { using MockAcceptedCallback = testing::MockFunction; testing::StrictMock mock_accepted_callback_; - device_c.StartAcceptingConnections(other_id, service_id, location_hint, + device_c.StartAcceptingConnections(service_id, other_id, location_hint, {mock_accepted_callback_.AsStdFunction()}); sender_socket = sender.Connect(self_id, location_hint); @@ -170,7 +170,7 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) { ByteArray message("message xyz"); receiver.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {[&receiver_socket, connected](WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); @@ -198,7 +198,7 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) { ByteArray message("message"); receiver.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {[&receiver_socket, connected](WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); @@ -232,7 +232,7 @@ TEST_F(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) { ByteArray message("message xyz"); receiver.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {[&receiver_socket, connected](WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); @@ -270,7 +270,7 @@ TEST_F(WebRtcTest, StartAcceptingConnections_NullPeerConnection) { ASSERT_TRUE(webrtc.IsAvailable()); EXPECT_FALSE(webrtc.StartAcceptingConnections( - self_id, service_id, location_hint, + service_id, self_id, location_hint, {mock_accepted_callback_.AsStdFunction()})); } diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.cc b/cpp/core/internal/p2p_cluster_pcp_handler.cc index 964fb569..d0108e66 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler.cc @@ -37,8 +37,7 @@ bool P2pClusterPcpHandler::ShouldAcceptBluetoothConnections( P2pClusterPcpHandler::P2pClusterPcpHandler( Mediums* mediums, EndpointManager* endpoint_manager, EndpointChannelManager* endpoint_channel_manager, BwuManager* bwu_manager, - InjectedBluetoothDeviceStore& injected_bluetooth_device_store, - Pcp pcp) + InjectedBluetoothDeviceStore& injected_bluetooth_device_store, Pcp pcp) : BasePcpHandler(mediums, endpoint_manager, endpoint_channel_manager, bwu_manager, pcp), bluetooth_radio_(mediums->GetBluetoothRadio()), @@ -103,6 +102,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) { NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: BT added"); mediums_started_successfully.push_back(bluetooth_medium); + bluetooth_classic_advertiser_client_id_ = client->GetClientId(); } } @@ -134,7 +134,14 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( } Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { - bluetooth_medium_.TurnOffDiscoverability(); + if (client->GetClientId() == bluetooth_classic_advertiser_client_id_) { + bluetooth_medium_.TurnOffDiscoverability(); + } else { + NEARBY_LOG(INFO, + "Skipped BT TurnOffDiscoverability for client %d, client that " + "turned on discoverability is %d", + client->GetClientId(), bluetooth_classic_advertiser_client_id_); + } bluetooth_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); ble_medium_.StopAdvertising(client->GetAdvertisingServiceId()); @@ -581,6 +588,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) { NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartDiscoveryImpl: BT added"); mediums_started_successfully.push_back(bluetooth_medium); + bluetooth_classic_discoverer_client_id_ = client->GetClientId(); } } @@ -615,7 +623,14 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( Status P2pClusterPcpHandler::StopDiscoveryImpl(ClientProxy* client) { wifi_lan_medium_.StopDiscovery(client->GetDiscoveryServiceId()); - bluetooth_medium_.StopDiscovery(); + if (client->GetClientId() == bluetooth_classic_discoverer_client_id_) { + bluetooth_medium_.StopDiscovery(); + } else { + NEARBY_LOG(INFO, + "Skipped BT stopDiscovery for client %d, client that started " + "discovery is %d", + client->GetClientId(), bluetooth_classic_discoverer_client_id_); + } ble_medium_.StopScanning(client->GetDiscoveryServiceId()); return {Status::kSuccess}; } @@ -632,8 +647,7 @@ Status P2pClusterPcpHandler::InjectEndpointImpl( BluetoothDevice remote_bluetooth_device = injected_bluetooth_device_store_.CreateInjectedBluetoothDevice( - metadata.remote_bluetooth_mac_address, - metadata.endpoint_id, + metadata.remote_bluetooth_mac_address, metadata.endpoint_id, metadata.endpoint_info, GenerateHash(service_id, BluetoothDeviceName::kServiceIdHashLength), GetPcp()); @@ -1143,7 +1157,7 @@ P2pClusterPcpHandler::StartListeningForWebRtcConnections( service_id, local_endpoint_id, local_endpoint_info); std::string empty_country_code; if (!webrtc_medium_.StartAcceptingConnections( - self_id, service_id, Utils::BuildLocationHint(empty_country_code), + service_id, self_id, Utils::BuildLocationHint(empty_country_code), {[this, client, local_endpoint_info](mediums::WebRtcSocketWrapper socket) { if (!socket.IsValid()) { diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.h b/cpp/core/internal/p2p_cluster_pcp_handler.h index 97fb698a..731fc9bb 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.h +++ b/cpp/core/internal/p2p_cluster_pcp_handler.h @@ -207,6 +207,8 @@ class P2pClusterPcpHandler : public BasePcpHandler { WifiLan& wifi_lan_medium_; mediums::WebRtc& webrtc_medium_; InjectedBluetoothDeviceStore& injected_bluetooth_device_store_; + std::int64_t bluetooth_classic_discoverer_client_id_; + std::int64_t bluetooth_classic_advertiser_client_id_; }; } // namespace connections diff --git a/cpp/core/internal/webrtc_bwu_handler.cc b/cpp/core/internal/webrtc_bwu_handler.cc index 4bd3e269..0a2be7d9 100644 --- a/cpp/core/internal/webrtc_bwu_handler.cc +++ b/cpp/core/internal/webrtc_bwu_handler.cc @@ -63,7 +63,7 @@ ByteArray WebrtcBwuHandler::InitializeUpgradedMediumForEndpoint( mediums::PeerId self_id{mediums::PeerId::FromRandom()}; if (!webrtc_.IsAcceptingConnections(service_id)) { if (!webrtc_.StartAcceptingConnections( - self_id, upgrade_service_id, location_hint, + upgrade_service_id, self_id, location_hint, { .accepted_cb = absl::bind_front( &WebrtcBwuHandler::OnIncomingWebrtcConnection, this, client, diff --git a/cpp/platform/base/byte_array.h b/cpp/platform/base/byte_array.h index 788aff18..544588e0 100644 --- a/cpp/platform/base/byte_array.h +++ b/cpp/platform/base/byte_array.h @@ -1,6 +1,7 @@ #ifndef PLATFORM_BASE_BYTE_ARRAY_H_ #define PLATFORM_BASE_BYTE_ARRAY_H_ +#include #include #include #include diff --git a/proto/error_code_enums.proto b/proto/error_code_enums.proto index 290517b0..302c30d1 100644 --- a/proto/error_code_enums.proto +++ b/proto/error_code_enums.proto @@ -272,6 +272,14 @@ enum ConnectError { // Next ID :44 } +// The error for event DISCONNECT. The range between 31 and 99. +enum DisconnectError { + // System error, failed to disconnect the network. + DISCONNECT_NETWORK_FAILED = 31; + + // Next ID :32 +} + enum Description { reserved 28, 29;