diff --git a/services/native/src/actions/default/system_suspend_controller.cpp b/services/native/src/actions/default/system_suspend_controller.cpp index 6e65f20fa683a984a306fb90107bd3ac3e0969f0..164c4a2edc49a85e82294b4ba3beaf5ebaf80093 100644 --- a/services/native/src/actions/default/system_suspend_controller.cpp +++ b/services/native/src/actions/default/system_suspend_controller.cpp @@ -55,8 +55,11 @@ void SystemSuspendController::RegisterHdiStatusListener() }; FFRTUtils::SubmitTask(task); POWER_HILOGI(COMP_SVC, "power interface service start"); - } else if (status.status == SERVIE_STATUS_STOP && powerInterface_) { - powerInterface_ = nullptr; + } else if (status.status == SERVIE_STATUS_STOP) { + std::lock_guard lock(interfaceMutex_); + if (powerInterface_) { + powerInterface_ = nullptr; + } POWER_HILOGW(COMP_SVC, "power interface service stop, unregister interface"); } })); @@ -71,50 +74,61 @@ void SystemSuspendController::RegisterHdiStatusListener() } } -void SystemSuspendController::RegisterPowerHdiCallback() +sptr SystemSuspendController::GetPowerInterface() { - POWER_HILOGD(COMP_SVC, "register power hdi callback"); + std::lock_guard lock(interfaceMutex_); if (powerInterface_ == nullptr) { powerInterface_ = IPowerInterface::Get(); - RETURN_IF_WITH_LOG(powerInterface_ == nullptr, "failed to get power hdi interface"); + RETURN_IF_WITH_RET(powerInterface_ == nullptr, nullptr); + } + return powerInterface_; +} + +void SystemSuspendController::RegisterPowerHdiCallback() +{ + POWER_HILOGD(COMP_SVC, "register power hdi callback"); + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { + POWER_HILOGE(COMP_SVC, "The hdf interface is null"); + return; } sptr callback = new PowerHdiCallback(); - powerInterface_->RegisterCallback(callback); + powerInterface->RegisterCallback(callback); POWER_HILOGD(COMP_SVC, "register power hdi callback end"); } void SystemSuspendController::UnRegisterPowerHdiCallback() { POWER_HILOGD(COMP_SVC, "unregister power hdi callback"); - if (powerInterface_ == nullptr) { - powerInterface_ = IPowerInterface::Get(); - RETURN_IF_WITH_LOG(powerInterface_ == nullptr, "failed to get power hdi interface"); + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { + POWER_HILOGE(COMP_SVC, "The hdf interface is null"); + return; } sptr callback = nullptr; - powerInterface_->RegisterCallback(callback); + powerInterface->RegisterCallback(callback); POWER_HILOGD(COMP_SVC, "unregister power hdi callback end"); } void SystemSuspendController::SetSuspendTag(const std::string& tag) { - if (powerInterface_ == nullptr) { + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return; } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "SET_SUSPEND_TAG", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "TAG", tag); - powerInterface_->SetSuspendTag(tag); + powerInterface->SetSuspendTag(tag); } void SystemSuspendController::AllowAutoSleep() { - std::lock_guard lock(mutex_); allowSleepTask_ = true; } void SystemSuspendController::DisallowAutoSleep() { - std::lock_guard lock(mutex_); allowSleepTask_ = false; } @@ -123,39 +137,42 @@ void SystemSuspendController::Suspend( { std::lock_guard lock(mutex_); POWER_HILOGI(COMP_SVC, "The hdf interface, force=%{public}u", static_cast(force)); - if (powerInterface_ == nullptr) { + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return; } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "DO_SUSPEND", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "TYPE", static_cast(1)); if (force) { - powerInterface_->ForceSuspend(); + powerInterface->ForceSuspend(); } else if (allowSleepTask_.load()) { - powerInterface_->StartSuspend(); + powerInterface->StartSuspend(); } } void SystemSuspendController::Wakeup() { std::lock_guard lock(mutex_); - if (powerInterface_ == nullptr) { + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return; } HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "DO_SUSPEND", HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "TYPE", static_cast(0)); - powerInterface_->StopSuspend(); + powerInterface->StopSuspend(); } bool SystemSuspendController::Hibernate() { POWER_HILOGI(COMP_SVC, "SystemSuspendController hibernate begin."); - if (powerInterface_ == nullptr) { + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return false; } - int32_t ret = powerInterface_->Hibernate(); + int32_t ret = powerInterface->Hibernate(); if (ret != HDF_SUCCESS) { POWER_HILOGE(COMP_SVC, "SystemSuspendController hibernate failed."); return false; @@ -177,46 +194,50 @@ OHOS::HDI::Power::V1_2::RunningLockInfo SystemSuspendController::FillRunningLock int32_t SystemSuspendController::AcquireRunningLock(const RunningLockParam& param) { + sptr powerInterface = GetPowerInterface(); int32_t status = RUNNINGLOCK_FAILURE; - if (powerInterface_ == nullptr) { + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return status; } OHOS::HDI::Power::V1_2::RunningLockInfo filledInfo = FillRunningLockInfo(param); - status = powerInterface_->HoldRunningLockExt(filledInfo, + status = powerInterface->HoldRunningLockExt(filledInfo, param.lockid, param.bundleName); return status; } int32_t SystemSuspendController::ReleaseRunningLock(const RunningLockParam& param) { + sptr powerInterface = GetPowerInterface(); int32_t status = RUNNINGLOCK_FAILURE; - if (powerInterface_ == nullptr) { + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return status; } OHOS::HDI::Power::V1_2::RunningLockInfo filledInfo = FillRunningLockInfo(param); - status = powerInterface_->UnholdRunningLockExt(filledInfo, + status = powerInterface->UnholdRunningLockExt(filledInfo, param.lockid, param.bundleName); return status; } void SystemSuspendController::Dump(std::string& info) { - if (powerInterface_ == nullptr) { + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return; } - powerInterface_->PowerDump(info); + powerInterface->PowerDump(info); } void SystemSuspendController::GetWakeupReason(std::string& reason) { - if (powerInterface_ == nullptr) { + sptr powerInterface = GetPowerInterface(); + if (powerInterface == nullptr) { POWER_HILOGE(COMP_SVC, "The hdf interface is null"); return; } - powerInterface_->GetWakeupReason(reason); + powerInterface->GetWakeupReason(reason); } int32_t SystemSuspendController::PowerHdfCallback::OnSuspend() diff --git a/services/native/src/actions/default/system_suspend_controller.h b/services/native/src/actions/default/system_suspend_controller.h index 5ee5abf541675665b949c702d6c90de08401fd46..0184c4df60ec5602d2c2e264bfeeaf0b49285ede 100644 --- a/services/native/src/actions/default/system_suspend_controller.h +++ b/services/native/src/actions/default/system_suspend_controller.h @@ -63,9 +63,12 @@ private: std::function onWakeup_; }; OHOS::HDI::Power::V1_2::RunningLockInfo FillRunningLockInfo(const RunningLockParam& param); + using IPowerInterface = OHOS::HDI::Power::V1_2::IPowerInterface; + sptr GetPowerInterface(); std::mutex mutex_; + std::mutex interfaceMutex_; std::shared_ptr sc_; - sptr powerInterface_ { nullptr }; + sptr powerInterface_ { nullptr }; sptr hdiServiceMgr_ { nullptr }; sptr hdiServStatListener_ { nullptr }; std::atomic allowSleepTask_ {false};