From 609fef1d52bc02e079a274916208acea73a5b72e Mon Sep 17 00:00:00 2001 From: gaosong Date: Thu, 1 Jun 2023 21:28:24 +0800 Subject: [PATCH] Timer Signed-off-by: gaosong --- .../wifi_manage/wifi_device_service_impl.cpp | 4 +- .../wifi_manage/wifi_hotspot_service_impl.cpp | 2 +- .../wifi_manage/wifi_manager.cpp | 136 +++++++++++------- .../wifi_framework/wifi_manage/wifi_manager.h | 30 +++- .../wifi_manage/wifi_p2p_service_impl.cpp | 2 +- 5 files changed, 110 insertions(+), 64 deletions(-) diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp index fba3dee44..afa48831a 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp @@ -276,7 +276,7 @@ ErrCode WifiDeviceServiceImpl::EnableWifi() #ifdef FEATURE_P2P_SUPPORT #ifndef OHOS_ARCH_LITE WifiSaLoadManager::GetInstance().LoadWifiSa(WIFI_P2P_ABILITY_ID); - WifiManager::GetInstance().ShutdownUnloadP2PSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadP2PSaTimer(); #endif sptr p2pService = WifiP2pServiceImpl::GetInstance(); if (p2pService != nullptr && p2pService->EnableP2p() != WIFI_OPT_SUCCESS) { @@ -286,7 +286,7 @@ ErrCode WifiDeviceServiceImpl::EnableWifi() #endif WifiSettings::GetInstance().SyncWifiConfig(); - WifiManager::GetInstance().ShutdownUnloadStaSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadStaSaTimer(); return WIFI_OPT_SUCCESS; } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp index 4df975435..cf36d41c5 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_hotspot_service_impl.cpp @@ -380,7 +380,7 @@ ErrCode WifiHotspotServiceImpl::EnableHotspot(const ServiceType type) WifiConfigCenter::GetInstance().SetApMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED, m_id); WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_AP, m_id); } else { - WifiManager::GetInstance().ShutdownUnloadApSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadApSaTimer(); } return errCode; } diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp index 4aacc12f5..cc518611a 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp @@ -28,6 +28,7 @@ #include "common_event_manager.h" #include "common_event_support.h" #include "wifi_sa_manager.h" +#include "common_timer_errors.h" #endif #include "wifi_sta_hal_interface.h" #include "wifi_service_manager.h" @@ -255,14 +256,9 @@ int WifiManager::Init() #ifndef OHOS_ARCH_LITE if (screenEventSubscriber_ == nullptr) { - lpScreenTimer_ = std::make_unique("WifiManager"); TimeOutCallback timeoutCallback = std::bind(&WifiManager::RegisterScreenEvent, this); - if (lpScreenTimer_ != nullptr) { - lpScreenTimer_->Setup(); - lpScreenTimer_->Register(timeoutCallback, TIMEOUT_SCREEN_EVENT, true); - } else { - WIFI_LOGE("lpScreenTimer_ is nullptr"); - } + WifiTimer::GetInstance()->Register(timeoutCallback, screenTimerId, TIMEOUT_SCREEN_EVENT); + WIFI_LOGI("RegisterScreenEvent success! screenTimerId:%{public}u", screenTimerId); } #endif mInitStatus = INIT_OK; @@ -311,10 +307,8 @@ void WifiManager::Exit() if (screenEventSubscriber_ != nullptr) { UnRegisterScreenEvent(); } - if (lpScreenTimer_ != nullptr) { - lpScreenTimer_->Shutdown(true); - lpScreenTimer_ = nullptr; - } + WIFI_LOGI("UnRegisterScreenEvent, screenTimerId:%{public}u", screenTimerId); + WifiTimer::GetInstance()->UnRegister(screenTimerId); #endif return; } @@ -354,7 +348,7 @@ InitStatus WifiManager::GetInitStatus() } #ifndef OHOS_ARCH_LITE -std::unique_ptr WifiManager::lpUnloadStaSaTimer = nullptr; +uint32_t WifiManager::unloadStaSaTimerId{0}; std::mutex WifiManager::unloadStaSaTimerMutex{}; void WifiManager::UnloadStaSaTimerCallback() { @@ -364,17 +358,15 @@ void WifiManager::UnloadStaSaTimerCallback() if (static_cast(ApState::AP_STATE_CLOSED) == WifiConfigCenter::GetInstance().GetHotspotState(0)) { WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_HOTSPOT_ABILITY_ID); } - WifiManager::GetInstance().ShutdownUnloadStaSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadStaSaTimer(); } -void WifiManager::ShutdownUnloadStaSaTimer(void) +void WifiManager::UnRegisterUnloadStaSaTimer(void) { + WIFI_LOGI("UnRegisterUnloadStaSaTimer! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); std::unique_lock lock(unloadStaSaTimerMutex); - if (lpUnloadStaSaTimer != nullptr) { - lpUnloadStaSaTimer->Shutdown(true); - lpUnloadStaSaTimer = nullptr; - WIFI_LOGI("ShutdownUnloadStaSaTimer success!"); - } + WifiTimer::GetInstance()->UnRegister(unloadStaSaTimerId); + unloadStaSaTimerId = 0; return; } #endif @@ -391,13 +383,10 @@ void WifiManager::CloseStaService(void) WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); #ifndef OHOS_ARCH_LITE std::unique_lock lock(unloadStaSaTimerMutex); - if (lpUnloadStaSaTimer == nullptr) { - lpUnloadStaSaTimer = std::make_unique("WifiManager"); + if (unloadStaSaTimerId == 0) { TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadStaSaTimerCallback); - if (lpUnloadStaSaTimer != nullptr) { - lpUnloadStaSaTimer->Setup(); - lpUnloadStaSaTimer->Register(timeoutCallback, TIMEOUT_UNLOAD_WIFI_SA, true); - } + WifiTimer::GetInstance()->Register(timeoutCallback, unloadStaSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("RegisterUnloadStaSaTimer success! unloadStaSaTimerId:%{public}u", unloadStaSaTimerId); } #endif return; @@ -406,22 +395,20 @@ void WifiManager::CloseStaService(void) #ifdef FEATURE_AP_SUPPORT #ifndef OHOS_ARCH_LITE -std::unique_ptr WifiManager::lpUnloadHotspotSaTimer = nullptr; +uint32_t WifiManager::unloadHotspotSaTimerId{0}; std::mutex WifiManager::unloadHotspotSaTimerMutex{}; void WifiManager::UnloadHotspotSaTimerCallback() { WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_HOTSPOT_ABILITY_ID); - WifiManager::GetInstance().ShutdownUnloadApSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadApSaTimer(); } -void WifiManager::ShutdownUnloadApSaTimer(void) +void WifiManager::UnRegisterUnloadApSaTimer(void) { + WIFI_LOGI("UnRegisterUnloadApSaTimer! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); std::unique_lock lock(unloadHotspotSaTimerMutex); - if (lpUnloadHotspotSaTimer != nullptr) { - lpUnloadHotspotSaTimer->Shutdown(true); - lpUnloadHotspotSaTimer = nullptr; - WIFI_LOGI("ShutdownUnloadApSaTimer success!"); - } + WifiTimer::GetInstance()->UnRegister(unloadHotspotSaTimerId); + unloadHotspotSaTimerId = 0; return; } #endif @@ -439,13 +426,10 @@ void WifiManager::CloseApService(int id) WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); #ifndef OHOS_ARCH_LITE std::unique_lock lock(unloadHotspotSaTimerMutex); - if (lpUnloadHotspotSaTimer == nullptr) { - lpUnloadHotspotSaTimer = std::make_unique("WifiManager"); + if (unloadHotspotSaTimerId == 0) { TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadHotspotSaTimerCallback); - if (lpUnloadHotspotSaTimer != nullptr) { - lpUnloadHotspotSaTimer->Setup(); - lpUnloadHotspotSaTimer->Register(timeoutCallback, TIMEOUT_UNLOAD_WIFI_SA, true); - } + WifiTimer::GetInstance()->Register(timeoutCallback, unloadHotspotSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("RegisterUnloadHotspotSaTimer success! unloadHotspotSaTimerId:%{public}u", unloadHotspotSaTimerId); } #endif return; @@ -463,22 +447,20 @@ void WifiManager::CloseScanService(void) #ifdef FEATURE_P2P_SUPPORT #ifndef OHOS_ARCH_LITE -std::unique_ptr WifiManager::lpUnloadP2PSaTimer = nullptr; +uint32_t WifiManager::unloadP2PSaTimerId{0}; std::mutex WifiManager::unloadP2PSaTimerMutex{}; void WifiManager::UnloadP2PSaTimerCallback() { WifiSaLoadManager::GetInstance().UnloadWifiSa(WIFI_P2P_ABILITY_ID); - WifiManager::GetInstance().ShutdownUnloadP2PSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadP2PSaTimer(); } -void WifiManager::ShutdownUnloadP2PSaTimer(void) +void WifiManager::UnRegisterUnloadP2PSaTimer(void) { + WIFI_LOGI("UnRegisterUnloadP2PSaTimer! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); std::unique_lock lock(unloadP2PSaTimerMutex); - if (lpUnloadP2PSaTimer != nullptr) { - lpUnloadP2PSaTimer->Shutdown(true); - lpUnloadP2PSaTimer = nullptr; - WIFI_LOGI("ShutdownUnloadP2PSaTimer success!"); - } + WifiTimer::GetInstance()->UnRegister(unloadP2PSaTimerId); + unloadP2PSaTimerId = 0; return; } #endif @@ -495,13 +477,10 @@ void WifiManager::CloseP2pService(void) WifiInternalEventDispatcher::GetInstance().AddBroadCastMsg(cbMsg); #ifndef OHOS_ARCH_LITE std::unique_lock lock(unloadP2PSaTimerMutex); - if (lpUnloadP2PSaTimer == nullptr) { - lpUnloadP2PSaTimer = std::make_unique("WifiManager"); + if (unloadP2PSaTimerId == 0) { TimeOutCallback timeoutCallback = std::bind(WifiManager::UnloadP2PSaTimerCallback); - if (lpUnloadP2PSaTimer != nullptr) { - lpUnloadP2PSaTimer->Setup(); - lpUnloadP2PSaTimer->Register(timeoutCallback, TIMEOUT_UNLOAD_WIFI_SA, true); - } + WifiTimer::GetInstance()->Register(timeoutCallback, unloadP2PSaTimerId, TIMEOUT_UNLOAD_WIFI_SA); + WIFI_LOGI("RegisterUnloadP2PSaTimer success! unloadP2PSaTimerId:%{public}u", unloadP2PSaTimerId); } #endif return; @@ -1137,6 +1116,57 @@ void ScreenEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData } WIFI_LOGW("ScreenEventSubscriber::OnReceiveEvent, screen state: %{public}d.", screenState); } + +WifiTimer *WifiTimer::GetInstance() +{ + static WifiTimer instance; + return &instance; +} + +WifiTimer::WifiTimer() : timer_(std::make_unique("WifiManagerTimer")) +{ + timer_->Setup(); +} + +WifiTimer::~WifiTimer() +{ + if (timer_) { + timer_->Shutdown(true); + } +} + +ErrCode WifiTimer::Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval) +{ + if (timer_ == nullptr) { + WIFI_LOGE("timer_ is nullptr"); + return WIFI_OPT_FAILED; + } + + uint32_t ret = timer_->Register(callback, interval, true); + if (ret == Utils::TIMER_ERR_DEAL_FAILED) { + WIFI_LOGE("Register timer failed"); + return WIFI_OPT_FAILED; + } + + outTimerId = ret; + return WIFI_OPT_SUCCESS; +} + +void WifiTimer::UnRegister(uint32_t timerId) +{ + if (timerId == 0) { + WIFI_LOGE("timerId is 0, no register timer"); + return; + } + + if (timer_ == nullptr) { + WIFI_LOGE("timer_ is nullptr"); + return; + } + + timer_->Unregister(timerId); + return; +} #endif } // namespace Wifi } // namespace OHOS diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h index 8b87bad13..ce36e3b1f 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h @@ -73,6 +73,22 @@ public: virtual ~ScreenEventSubscriber() {}; void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; }; + +class WifiTimer { +public: + using TimerCallback = std::function; + static constexpr uint32_t DEFAULT_TIMEROUT = 10000; + static WifiTimer *GetInstance(void); + + WifiTimer(); + ~WifiTimer(); + + ErrCode Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT); + void UnRegister(uint32_t timerId); + +private: + std::unique_ptr timer_{nullptr}; +}; #endif class WifiManager { @@ -115,7 +131,7 @@ public: */ IScanSerivceCallbacks GetScanCallback(void); - void ShutdownUnloadStaSaTimer(void); + void UnRegisterUnloadStaSaTimer(void); #ifdef FEATURE_AP_SUPPORT /** * @Description Get the ap callback object. @@ -124,7 +140,7 @@ public: */ IApServiceCallbacks GetApCallback(void); - void ShutdownUnloadApSaTimer(void); + void UnRegisterUnloadApSaTimer(void); #endif #ifdef FEATURE_P2P_SUPPORT @@ -135,7 +151,7 @@ public: */ IP2pServiceCallbacks GetP2pCallback(void); - void ShutdownUnloadP2PSaTimer(void); + void UnRegisterUnloadP2PSaTimer(void); #endif /** @@ -227,13 +243,13 @@ private: StaServiceCallback mStaCallback; IScanSerivceCallbacks mScanCallback; #ifndef OHOS_ARCH_LITE - static std::unique_ptr lpUnloadStaSaTimer; + static uint32_t unloadStaSaTimerId; static std::mutex unloadStaSaTimerMutex; #endif #ifdef FEATURE_AP_SUPPORT IApServiceCallbacks mApCallback; #ifndef OHOS_ARCH_LITE - static std::unique_ptr lpUnloadHotspotSaTimer; + static uint32_t unloadHotspotSaTimerId; static std::mutex unloadHotspotSaTimerMutex; #endif #endif @@ -241,7 +257,7 @@ private: IP2pServiceCallbacks mP2pCallback; static WifiCfgMonitorEventCallback cfgMonitorCallback; #ifndef OHOS_ARCH_LITE - static std::unique_ptr lpUnloadP2PSaTimer; + static uint32_t unloadP2PSaTimerId; static std::mutex unloadP2PSaTimerMutex; #endif #endif @@ -249,7 +265,7 @@ private: void RegisterScreenEvent(); void UnRegisterScreenEvent(); std::shared_ptr screenEventSubscriber_ = nullptr; - std::unique_ptr lpScreenTimer_ = nullptr; + uint32_t screenTimerId{0}; #endif InitStatus mInitStatus; long mSupportedFeatures; diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp index 0adbe2df9..91f10250c 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p_service_impl.cpp @@ -158,7 +158,7 @@ ErrCode WifiP2pServiceImpl::EnableP2p(void) WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); } else { - WifiManager::GetInstance().ShutdownUnloadP2PSaTimer(); + WifiManager::GetInstance().UnRegisterUnloadP2PSaTimer(); } return ret; }