diff --git a/cpp/core/internal/mediums/webrtc.cc b/cpp/core/internal/mediums/webrtc.cc index d20fb9b7..ead57cfc 100644 --- a/cpp/core/internal/mediums/webrtc.cc +++ b/cpp/core/internal/mediums/webrtc.cc @@ -68,8 +68,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 e288f257..1cc1867e 100644 --- a/cpp/core/internal/mediums/webrtc.h +++ b/cpp/core/internal/mediums/webrtc.h @@ -75,8 +75,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 45c9c3fc..96f40868 100644 --- a/cpp/core/internal/mediums/webrtc_test.cc +++ b/cpp/core/internal/mediums/webrtc_test.cc @@ -57,10 +57,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{})); } @@ -78,7 +78,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 @@ -95,14 +95,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()})); } @@ -120,7 +120,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{})); @@ -138,7 +138,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()); @@ -147,7 +147,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); @@ -184,7 +184,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()); @@ -212,7 +212,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()); @@ -246,7 +246,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()); @@ -284,7 +284,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 5e1ef4ca..9c1e3d93 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler.cc @@ -51,8 +51,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()), @@ -117,6 +116,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(); } } @@ -148,7 +148,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()); @@ -595,6 +602,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(); } } @@ -629,7 +637,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}; } @@ -646,8 +661,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()); @@ -1157,7 +1171,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 e60c527d..c9c3060b 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.h +++ b/cpp/core/internal/p2p_cluster_pcp_handler.h @@ -221,6 +221,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 0f08abab..31de6499 100644 --- a/cpp/core/internal/webrtc_bwu_handler.cc +++ b/cpp/core/internal/webrtc_bwu_handler.cc @@ -77,7 +77,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 953aa21e..3e12abb6 100644 --- a/cpp/platform/base/byte_array.h +++ b/cpp/platform/base/byte_array.h @@ -15,6 +15,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 29470214..56104839 100644 --- a/proto/error_code_enums.proto +++ b/proto/error_code_enums.proto @@ -286,6 +286,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;