mirror of
https://gitee.com/openharmony/communication_wifi.git
synced 2025-02-22 10:04:19 +00:00
Timer
Signed-off-by: gaosong <gaosong29@huawei.com>
This commit is contained in:
parent
d0ef9d9be0
commit
609fef1d52
@ -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<WifiP2pServiceImpl> 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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<Utils::Timer>("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<Utils::Timer> 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<int>(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<std::mutex> 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<std::mutex> lock(unloadStaSaTimerMutex);
|
||||
if (lpUnloadStaSaTimer == nullptr) {
|
||||
lpUnloadStaSaTimer = std::make_unique<Utils::Timer>("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<Utils::Timer> 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<std::mutex> 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<std::mutex> lock(unloadHotspotSaTimerMutex);
|
||||
if (lpUnloadHotspotSaTimer == nullptr) {
|
||||
lpUnloadHotspotSaTimer = std::make_unique<Utils::Timer>("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<Utils::Timer> 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<std::mutex> 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<std::mutex> lock(unloadP2PSaTimerMutex);
|
||||
if (lpUnloadP2PSaTimer == nullptr) {
|
||||
lpUnloadP2PSaTimer = std::make_unique<Utils::Timer>("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<Utils::Timer>("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
|
||||
|
@ -73,6 +73,22 @@ public:
|
||||
virtual ~ScreenEventSubscriber() {};
|
||||
void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override;
|
||||
};
|
||||
|
||||
class WifiTimer {
|
||||
public:
|
||||
using TimerCallback = std::function<void()>;
|
||||
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<Utils::Timer> 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<Utils::Timer> lpUnloadStaSaTimer;
|
||||
static uint32_t unloadStaSaTimerId;
|
||||
static std::mutex unloadStaSaTimerMutex;
|
||||
#endif
|
||||
#ifdef FEATURE_AP_SUPPORT
|
||||
IApServiceCallbacks mApCallback;
|
||||
#ifndef OHOS_ARCH_LITE
|
||||
static std::unique_ptr<Utils::Timer> 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<Utils::Timer> lpUnloadP2PSaTimer;
|
||||
static uint32_t unloadP2PSaTimerId;
|
||||
static std::mutex unloadP2PSaTimerMutex;
|
||||
#endif
|
||||
#endif
|
||||
@ -249,7 +265,7 @@ private:
|
||||
void RegisterScreenEvent();
|
||||
void UnRegisterScreenEvent();
|
||||
std::shared_ptr<ScreenEventSubscriber> screenEventSubscriber_ = nullptr;
|
||||
std::unique_ptr<Utils::Timer> lpScreenTimer_ = nullptr;
|
||||
uint32_t screenTimerId{0};
|
||||
#endif
|
||||
InitStatus mInitStatus;
|
||||
long mSupportedFeatures;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user