From c0b99146855bd3db0970089ffd2dfd1d5f9c4eb1 Mon Sep 17 00:00:00 2001 From: Bruin06 <419738327@qq.com> Date: Mon, 28 Jul 2025 14:19:36 +0800 Subject: [PATCH] P2p Remote Die Signed-off-by: Bruin06 <419738327@qq.com> --- .../wifi_manage/libwifi_manager.map | 1 + .../wifi_internal_event_dispatcher.cpp | 13 +++++++++++++ .../wifi_internal_event_dispatcher.h | 1 + .../hid2d/wifi_hid2d_service_utils.cpp | 18 ++++++++++++++++++ .../wifi_p2p/hid2d/wifi_hid2d_service_utils.h | 1 + .../wifi_manage/wifi_p2p/ip2p_service.h | 8 ++++++++ .../wifi_manage/wifi_p2p/p2p_default_state.cpp | 10 ++++++++++ .../wifi_manage/wifi_p2p/p2p_define.h | 1 + .../wifi_manage/wifi_p2p/p2p_interface.cpp | 5 +++++ .../wifi_manage/wifi_p2p/p2p_interface.h | 8 ++++++++ .../wifi_manage/wifi_p2p/p2p_state_machine.cpp | 6 ++++++ .../wifi_manage/wifi_p2p/p2p_state_machine.h | 2 ++ .../wifi_manage/wifi_p2p/wifi_p2p_service.cpp | 7 +++++++ .../wifi_manage/wifi_p2p/wifi_p2p_service.h | 9 +++++++++ .../wifi_p2p_sa/wifi_p2p_death_recipient.cpp | 6 ++++++ .../wifi_sub_manage/wifi_p2p_manager.h | 1 + .../unittest/wifi_mock_p2p_service.cpp | 5 +++++ .../unittest/wifi_mock_p2p_service.h | 1 + .../wifi_p2p/test/Mock/mock_p2p_interface.cpp | 5 +++++ .../wifi_p2p/test/Mock/mock_p2p_interface.h | 1 + .../wifi_p2p/test/p2p_state_machine_test.cpp | 9 +++++++++ 21 files changed, 118 insertions(+) diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/libwifi_manager.map b/wifi/services/wifi_standard/wifi_framework/wifi_manage/libwifi_manager.map index fa422029c..9cda632a1 100755 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/libwifi_manager.map +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/libwifi_manager.map @@ -378,6 +378,7 @@ "OHOS::Wifi::WifiInternalEventDispatcher::SetSingleP2pCallback(OHOS::sptr const&)"; "OHOS::Wifi::WifiInternalEventDispatcher::RemoveP2pCallback(OHOS::sptr const&)"; "OHOS::Wifi::WifiInternalEventDispatcher::ResetAllFrozenApp()"; + "OHOS::Wifi::WifiInternalEventDispatcher::GetRemoteUid(OHOS::sptr const&)"; "OHOS::Wifi::WifiInternalEventDispatcher::IsAppFrozen(int)"; "OHOS::Wifi::WifiInternalEventDispatcher::SetAppFrozen(std::__h::set, std::__h::allocator>, bool)"; "OHOS::Wifi::WifiInternalEventDispatcher::AddBroadCastMsg(OHOS::Wifi::WifiEventCallbackMsg const&)"; diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.cpp index a7c70f2da..f2be5f339 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.cpp @@ -1100,5 +1100,18 @@ bool WifiInternalEventDispatcher::IsAppFrozen(int pid) } return false; } + +int WifiInternalEventDispatcher::GetRemoteUid(const sptr &remote) +{ + int uid = -1; + if (remote != nullptr) { + std::unique_lock lock(mP2pCallbackMutex); + auto cbInfo = mP2pCallbackInfo.find(remote); + if (cbInfo != mP2pCallbackInfo.end()) { + uid = cbInfo->second.callingUid; + } + } + return uid; +} } // namespace Wifi } // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.h index 79c782229..7985d58ae 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_common/wifi_internal_event_dispatcher.h @@ -122,6 +122,7 @@ public: void SetAppFrozen(std::set pidList, bool isFrozen); void ResetAllFrozenApp(); bool IsAppFrozen(int pid); + int GetRemoteUid(const sptr &remote); private: static void DealStaCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg); static void DealScanCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg); diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.cpp index 633df6eed..150394c1d 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.cpp @@ -209,5 +209,23 @@ void SharedLinkManager::PrintMapInfo(void) } WIFI_LOGI("sharelink map is %{public}s", mapInfo.c_str()); } + +bool SharedLinkManager::CheckNeedRemoveGroup(int uid) +{ + std::unique_lock lock(g_sharedLinkMutex); + if (sharedLinkCountMap.find(uid) != sharedLinkCountMap.end()) { + sharedLinkCountMap[uid] = 0; + } + PrintMapInfo(); + int sharedLinkCount = 0; + for (auto iter : sharedLinkCountMap) { + sharedLinkCount += iter.second; + } + if (sharedLinkCount == 0) { + WIFI_LOGI("all create group server has died, need remove group"); + return true; + } + return false; +} } // namespace Wifi } // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.h index 7197e7838..5d709acea 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/hid2d/wifi_hid2d_service_utils.h @@ -49,6 +49,7 @@ public: static void ClearSharedLinkCount(); static int GetSharedLinkCount(); static void ClearUidCount(int uid); + static bool CheckNeedRemoveGroup(int uid); private: static void PrintMapInfo(void); diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/ip2p_service.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/ip2p_service.h index 7b2484fd5..fc6cdb290 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/ip2p_service.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/ip2p_service.h @@ -365,6 +365,14 @@ public: * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED */ virtual ErrCode SetMiracastSinkConfig(const std::string& config) = 0; + + /** + * @Description notify remote die for remove p2p group + * + * @param uid - remote's uid + * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED + */ + virtual ErrCode NotifyRemoteDie(int uid) = 0; }; } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_default_state.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_default_state.cpp index 7945e82dc..c51b2f9a1 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_default_state.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_default_state.cpp @@ -69,6 +69,16 @@ bool P2pDefaultState::ExecuteStateMsg(InternalMessagePtr msg) p2pStateMachine.ClearAllP2pServiceCallbacks(); break; } + case P2P_STATE_MACHINE_CMD::CMD_NOTIFY_REMOTE_DIE: { + if (SharedLinkManager::GetSharedLinkCount() == 0 || !p2pStateMachine.HasP2pConnected()) { + return EXECUTED; + } + int uid = msg->GetParam1(); + if (SharedLinkManager::CheckNeedRemoveGroup(uid)) { + p2pStateMachine.SendMessage(static_cast(P2P_STATE_MACHINE_CMD::CMD_REMOVE_GROUP)); + } + break; + } default: WIFI_LOGE("Failed:The P2P state machine does not process messages: [%{public}d]", msgName); return NOT_EXECUTED; diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_define.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_define.h index 0620c6f44..c1de8bea0 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_define.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_define.h @@ -119,6 +119,7 @@ enum class P2P_STATE_MACHINE_CMD { CMD_UNREGISTER_SERVICE_CB, CMD_CLEAR_SERVICE_CB, CMD_SET_MIRACAST_SINK_CONFIG, + CMD_NOTIFY_REMOTE_DIE, /* monitor to state machine */ WPA_CONNECTED_EVENT = 100, // result of connect diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.cpp index e8845c6c8..945028133 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.cpp @@ -292,5 +292,10 @@ ErrCode P2pInterface::SetMiracastSinkConfig(const std::string& config) { return p2pService.SetMiracastSinkConfig(config); } + +ErrCode P2pInterface::NotifyRemoteDie(int uid) +{ + return p2pService.NotifyRemoteDie(uid); +} } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.h index 7f932e09b..1e12efea4 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_interface.h @@ -377,6 +377,14 @@ public: * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED */ virtual ErrCode SetMiracastSinkConfig(const std::string& config) override; + + /** + * @Description notify remote die for remove p2p group + * + * @param uid - remote's uid + * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED + */ + virtual ErrCode NotifyRemoteDie(int uid) override; private: WifiP2pGroupManager groupManager; /* group manager */ WifiP2pDeviceManager deviceMgr; /* device manager */ diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.cpp index bceed38b6..de357b0ac 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.cpp @@ -1437,5 +1437,11 @@ void P2pStateMachine::WakeUpScreenSaver() WIFI_LOGI("Wake up screen saver success"); } #endif + +bool P2pStateMachine::HasP2pConnected(void) +{ + WifiP2pLinkedInfo info = groupManager.GetP2pInfo(); + return info.GetConnectState() == P2pConnectedState::P2P_CONNECTED; +} } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.h index 860ee538d..db6e9a3c6 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_state_machine.h @@ -315,6 +315,8 @@ private: bool P2pReject(const std::string mac) const; bool CreateTempGroupWithConfig(const WifiP2pConfigInternal &config, int freq) const; + + bool HasP2pConnected(void); private: /** * @Description - Broadcast state change event. diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.cpp index 82a1dacca..41ceb3b94 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.cpp @@ -530,5 +530,12 @@ ErrCode WifiP2pService::SetMiracastSinkConfig(const std::string& config) p2pStateMachine.SendMessage(static_cast(P2P_STATE_MACHINE_CMD::CMD_SET_MIRACAST_SINK_CONFIG), info); return WIFI_OPT_SUCCESS; } + +ErrCode WifiP2pService::NotifyRemoteDie(int uid) +{ + WIFI_LOGI("NotifyRemoteDie"); + p2pStateMachine.SendMessage(static_cast(P2P_STATE_MACHINE_CMD::CMD_NOTIFY_REMOTE_DIE), uid); + return WIFI_OPT_SUCCESS; +} } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.h index 62a5fe078..2ab69d71c 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/wifi_p2p_service.h @@ -347,6 +347,15 @@ public: * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED */ virtual ErrCode SetMiracastSinkConfig(const std::string& config) override; + + /** + * @Description notify remote die for remove p2p group + * + * @param uid - remote's uid + * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED + */ + virtual ErrCode NotifyRemoteDie(int uid) override; + private: /** * @Description - P2P state machine deregistration event callback. diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp index a25e40ba9..30ccab233 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_sa/wifi_p2p_death_recipient.cpp @@ -15,6 +15,7 @@ #include "wifi_p2p_death_recipient.h" #include "wifi_logger.h" #include "wifi_internal_event_dispatcher.h" +#include "wifi_service_manager.h" DEFINE_WIFILOG_P2P_LABEL("WifiP2pDeathRecipient"); namespace OHOS { @@ -22,7 +23,12 @@ namespace Wifi { void WifiP2pDeathRecipient::OnRemoteDied(const wptr &remoteObject) { WIFI_LOGD("WifiP2pDeathRecipient::OnRemoteDied!"); + int uid = WifiInternalEventDispatcher::GetInstance().GetRemoteUid(remoteObject.promote()); WifiInternalEventDispatcher::GetInstance().RemoveP2pCallback(remoteObject.promote()); + IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst(); + if (uid != -1 && pService != nullptr) { + pService->NotifyRemoteDie(uid); + } } } // namespace Wifi } // namespace OHOS \ No newline at end of file diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h index 9995af8a8..531c52d38 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sub_manage/wifi_p2p_manager.h @@ -22,6 +22,7 @@ #include "wifi_errcode.h" #include "wifi_internal_msg.h" #include "ip2p_service_callbacks.h" +#include "wifi_internal_event_dispatcher.h" namespace OHOS { namespace Wifi { diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.cpp b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.cpp index c9d71250b..780f57e74 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.cpp +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.cpp @@ -441,6 +441,11 @@ ErrCode WifiMockP2pService::SetMiracastSinkConfig(const std::string& config) return WIFI_OPT_SUCCESS; } +ErrCode WifiMockP2pService::NotifyRemoteDie(int uid) +{ + return WIFI_OPT_SUCCESS; +} + extern "C" IP2pService *Create(void) { return new (std::nothrow) WifiMockP2pService(); diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.h b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.h index f03716b89..b2db5a2c7 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.h +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/unittest/wifi_mock_p2p_service.h @@ -55,6 +55,7 @@ public: ErrCode GetRptStationsList(std::vector &result); void NotifyWscDialogConfirmResult(bool isAccept); ErrCode SetMiracastSinkConfig(const std::string& config); + ErrCode NotifyRemoteDie(int uid); public: IP2pServiceCallbacks mCallback; diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.cpp b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.cpp index 4d35247e7..cc0151724 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.cpp +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.cpp @@ -242,5 +242,10 @@ ErrCode P2pInterface::SetMiracastSinkConfig(const std::string& config) { return WIFI_OPT_SUCCESS; } + +ErrCode P2pInterface::NotifyRemoteDie(int uid) +{ + return WIFI_OPT_SUCCESS; +} } // namespace Wifi } // namespace OHOS diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.h b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.h index ec19c6b1a..09f92c974 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.h +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/Mock/mock_p2p_interface.h @@ -69,6 +69,7 @@ public: virtual ErrCode GetRptStationsList(std::vector &result); virtual void NotifyWscDialogConfirmResult(bool isAccept); virtual ErrCode SetMiracastSinkConfig(const std::string& config); + virtual ErrCode NotifyRemoteDie(int uid); }; } // namespace Wifi } // namespace OHOS diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/p2p_state_machine_test.cpp b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/p2p_state_machine_test.cpp index be991a962..61b8c194d 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/p2p_state_machine_test.cpp +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/test/p2p_state_machine_test.cpp @@ -431,6 +431,10 @@ public: std::vector groups = pP2pStateMachine->groupManager.GetGroups(); return groups.size(); } + bool WarpHasP2pConnected() + { + return pP2pStateMachine->HasP2pConnected(); + } }; void ButtonTest(AlertDialog &dialog, std::any ctx) @@ -988,5 +992,10 @@ HWTEST_F(P2pStateMachineTest, WakeUpScreenSaverTest, TestSize.Level1) WarpWakeUpScreenSaver(); EXPECT_FALSE(g_errLog.find("P2pStateMachine") != std::string::npos); } + +HWTEST_F(P2pStateMachineTest, HasP2pConnectedTest, TestSize.Level1) +{ + EXPECT_FALSE(WarpHasP2pConnected()); +} } // namespace Wifi } // namespace OHOS \ No newline at end of file -- Gitee