diff --git a/connections/implementation/base_bwu_handler.cc b/connections/implementation/base_bwu_handler.cc index 1f69d2c8..f1162fa5 100644 --- a/connections/implementation/base_bwu_handler.cc +++ b/connections/implementation/base_bwu_handler.cc @@ -75,6 +75,10 @@ void BaseBwuHandler::RevertInitiatorState(const std::string& upgrade_service_id, } } +void BaseBwuHandler::RevertResponderState(const std::string& service_id) { + HandleRevertInitiatorStateForService(service_id); +} + } // namespace connections } // namespace nearby } // namespace location diff --git a/connections/implementation/base_bwu_handler.h b/connections/implementation/base_bwu_handler.h index 78065eed..39c71072 100644 --- a/connections/implementation/base_bwu_handler.h +++ b/connections/implementation/base_bwu_handler.h @@ -40,6 +40,10 @@ class BaseBwuHandler : public BwuHandler { void RevertInitiatorState() final; void RevertInitiatorState(const std::string& upgrade_service_id, const std::string& endpoint_id) final; + // If BWU Medium is Hotspot. The client needs to disconnect from Hotspot, then + // it can restore the previous AP connection right away. The following method + // is only for Hotspot Client + void RevertResponderState(const std::string& service_id) final; protected: // Invoked by InitializeUpgradedMediumForEndpoint and RevertInitiatorState, diff --git a/connections/implementation/bwu_handler.h b/connections/implementation/bwu_handler.h index 0521d2a8..201e5d81 100644 --- a/connections/implementation/bwu_handler.h +++ b/connections/implementation/bwu_handler.h @@ -77,6 +77,8 @@ class BwuHandler { virtual void RevertInitiatorState(const std::string& service_id, const std::string& endpoint_id) = 0; + virtual void RevertResponderState(const std::string& service_id) = 0; + // Called by the Responder to set up the upgraded medium for this endpoint (if // that hasn't already been done) using the UpgradePathInfo sent by the // Initiator, and returns a new EndpointChannel for the upgraded medium. diff --git a/connections/implementation/bwu_manager.cc b/connections/implementation/bwu_manager.cc index 45ee3726..083014f1 100644 --- a/connections/implementation/bwu_manager.cc +++ b/connections/implementation/bwu_manager.cc @@ -374,12 +374,23 @@ void BwuManager::RevertBwuMediumForEndpoint(const std::string& service_id, << endpoint_id; endpoint_id_to_bwu_medium_.erase(endpoint_id); - // If |service_id| isn't of the INITIATOR-upgrade format--for example, if this - // is called by the RESPONDER--there is no need to call RevertInitiatorState. - if (!IsInitiatorUpgradeServiceId(service_id)) return; - BwuHandler* handler = GetHandlerForMedium(medium); - if (!handler) return; + if (!handler) { + NEARBY_LOGS(INFO) << "No BWU handler can be found for " + << proto::connections::Medium_Name(medium); + return; + } + // If |service_id| isn't of the INITIATOR-upgrade format--for example, if this + // is called by the RESPONDER--there is no need to call RevertInitiatorState + // unless the BWU Medium is Hotspot. The client needs to disconnect from + // Hotspot, then it can restore the previous AP connection right away. + if (!IsInitiatorUpgradeServiceId(service_id)) { + if (medium == Medium::WIFI_HOTSPOT) { + handler->RevertResponderState(service_id); + } + + return; + } handler->RevertInitiatorState(service_id, endpoint_id); } diff --git a/connections/implementation/bwu_manager_test.cc b/connections/implementation/bwu_manager_test.cc index 779b42ae..229866aa 100644 --- a/connections/implementation/bwu_manager_test.cc +++ b/connections/implementation/bwu_manager_test.cc @@ -669,6 +669,59 @@ TEST_F(BwuManagerTest, InitiateBwu_Revert_OnUpgradeFailure_FlagDisabled) { EXPECT_TRUE(fake_web_rtc_bwu_handler_->handle_revert_calls().empty()); } +TEST_F(BwuManagerTest, InitiateBwu_Revert_OnDisconnect_Hotspot) { + FeatureFlags::GetMutableFlagsForTesting().support_multiple_bwu_mediums = true; + + CreateInitialEndpoint(kServiceIdA, kEndpointId1, Medium::BLUETOOTH); + + ExceptionOr hotspot_path_available_frame = + parser::FromBytes(parser::ForBwuWifiHotspotPathAvailable( + /*ssid=*/"Direct-357a2d8c", /*password=*/"b592f7d3", + /*port=*/1234, /*gateway=*/"123.234.23.1", false)); + OfflineFrame frame = hotspot_path_available_frame.result(); + frame.set_version(OfflineFrame::V1); + auto* v1_frame = frame.mutable_v1(); + auto* sub_frame = v1_frame->mutable_bandwidth_upgrade_negotiation(); + sub_frame->set_event_type( + BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE); + auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info(); + upgrade_path_info->set_supports_client_introduction_ack(false); + + bwu_manager_->OnIncomingFrame(frame, std::string(kEndpointId1), &client_, + Medium::BLUETOOTH); + CountDownLatch latch(1); + bwu_manager_->OnEndpointDisconnect(&client_, (std::string)kServiceIdA, + std::string(kEndpointId1), latch); + + ASSERT_EQ(fake_wifi_hotspot_bwu_handler_->handle_revert_calls().size(), 1u); +} + +TEST_F(BwuManagerTest, InitiateBwu_Revert_OnDisconnect_Wlan) { + FeatureFlags::GetMutableFlagsForTesting().support_multiple_bwu_mediums = true; + + CreateInitialEndpoint(kServiceIdA, kEndpointId1, Medium::BLUETOOTH); + + ExceptionOr wlan_path_available_frame = parser::FromBytes( + parser::ForBwuWifiLanPathAvailable(/*ip_address=*/"ABCD", + /*port=*/1234)); + OfflineFrame frame = wlan_path_available_frame.result(); + frame.set_version(OfflineFrame::V1); + auto* v1_frame = frame.mutable_v1(); + auto* sub_frame = v1_frame->mutable_bandwidth_upgrade_negotiation(); + sub_frame->set_event_type( + BandwidthUpgradeNegotiationFrame::UPGRADE_PATH_AVAILABLE); + auto* upgrade_path_info = sub_frame->mutable_upgrade_path_info(); + upgrade_path_info->set_supports_client_introduction_ack(false); + + bwu_manager_->OnIncomingFrame(frame, std::string(kEndpointId1), &client_, + Medium::BLUETOOTH); + CountDownLatch latch(1); + bwu_manager_->OnEndpointDisconnect(&client_, (std::string)kServiceIdA, + std::string(kEndpointId1), latch); + + ASSERT_EQ(fake_wifi_lan_bwu_handler_->handle_revert_calls().size(), 0u); +} + TEST_F(BwuManagerTest, OnReceiveBwuEvent) { // TODO(b/235109434): Add more unit tests coverage for BWU module }