15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include "power_state_machine.h" 175ccb8f90Sopenharmony_ci 185ccb8f90Sopenharmony_ci#include <algorithm> 195ccb8f90Sopenharmony_ci#include <cinttypes> 205ccb8f90Sopenharmony_ci#include <datetime_ex.h> 215ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 225ccb8f90Sopenharmony_ci#include <hisysevent.h> 235ccb8f90Sopenharmony_ci#endif 245ccb8f90Sopenharmony_ci#include <ipc_skeleton.h> 255ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HITRACE_PART 265ccb8f90Sopenharmony_ci#include "power_hitrace.h" 275ccb8f90Sopenharmony_ci#endif 285ccb8f90Sopenharmony_ci#include "power_mode_policy.h" 295ccb8f90Sopenharmony_ci#include "power_mgr_factory.h" 305ccb8f90Sopenharmony_ci#include "power_mgr_service.h" 315ccb8f90Sopenharmony_ci#include "power_utils.h" 325ccb8f90Sopenharmony_ci#include "setting_helper.h" 335ccb8f90Sopenharmony_ci#include "system_suspend_controller.h" 345ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 355ccb8f90Sopenharmony_ci#include "os_account_manager.h" 365ccb8f90Sopenharmony_ci#include "parameters.h" 375ccb8f90Sopenharmony_ci#endif 385ccb8f90Sopenharmony_ci#ifdef MSDP_MOVEMENT_ENABLE 395ccb8f90Sopenharmony_ci#include <dlfcn.h> 405ccb8f90Sopenharmony_ci#endif 415ccb8f90Sopenharmony_ci 425ccb8f90Sopenharmony_cinamespace OHOS { 435ccb8f90Sopenharmony_cinamespace PowerMgr { 445ccb8f90Sopenharmony_cinamespace { 455ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING 465ccb8f90Sopenharmony_cisptr<SettingObserver> g_displayOffTimeAcObserver; 475ccb8f90Sopenharmony_cisptr<SettingObserver> g_displayOffTimeDcObserver; 485ccb8f90Sopenharmony_ci#else 495ccb8f90Sopenharmony_cisptr<SettingObserver> g_displayOffTimeObserver; 505ccb8f90Sopenharmony_ci#endif 515ccb8f90Sopenharmony_cistatic int64_t g_beforeOverrideTime {-1}; 525ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 535ccb8f90Sopenharmony_ciconstexpr int32_t DISPLAY_OFF = 0; 545ccb8f90Sopenharmony_ciconstexpr int32_t DISPLAY_ON = 2; 555ccb8f90Sopenharmony_ci#endif 565ccb8f90Sopenharmony_ciconst std::string POWERMGR_STOPSERVICE = "persist.powermgr.stopservice"; 575ccb8f90Sopenharmony_ciconstexpr uint32_t PRE_BRIGHT_AUTH_TIMER_DELAY_MS = 3000; 585ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 595ccb8f90Sopenharmony_ciconstexpr uint32_t HIBERNATE_DELAY_MS = 3500; 605ccb8f90Sopenharmony_cistatic int64_t g_preHibernateStart = 0; 615ccb8f90Sopenharmony_ci#endif 625ccb8f90Sopenharmony_ci} 635ccb8f90Sopenharmony_ciPowerStateMachine::PowerStateMachine(const wptr<PowerMgrService>& pms, const std::shared_ptr<FFRTTimer>& ffrtTimer) 645ccb8f90Sopenharmony_ci : pms_(pms), ffrtTimer_(ffrtTimer), currentState_(PowerState::UNKNOWN) 655ccb8f90Sopenharmony_ci{ 665ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Instance start"); 675ccb8f90Sopenharmony_ci // NOTICE Need get screen state when device startup, 685ccb8f90Sopenharmony_ci // rightnow we set screen is on as default 695ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOnTime = GetTickCount(); 705ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOffTime = 0; 715ccb8f90Sopenharmony_ci mDeviceState_.lastWakeupEventTime = 0; 725ccb8f90Sopenharmony_ci mDeviceState_.lastRefreshActivityTime = 0; 735ccb8f90Sopenharmony_ci mDeviceState_.lastWakeupDeviceTime = 0; 745ccb8f90Sopenharmony_ci mDeviceState_.lastSuspendDeviceTime = 0; 755ccb8f90Sopenharmony_ci 765ccb8f90Sopenharmony_ci // init lock map which will block state transit 775ccb8f90Sopenharmony_ci std::vector<RunningLockType> awakeBlocker {}; 785ccb8f90Sopenharmony_ci std::vector<RunningLockType> freezeBlocker {}; 795ccb8f90Sopenharmony_ci std::vector<RunningLockType> inactiveBlocker {RunningLockType::RUNNINGLOCK_SCREEN}; 805ccb8f90Sopenharmony_ci std::vector<RunningLockType> standByBlocker {}; 815ccb8f90Sopenharmony_ci std::vector<RunningLockType> dozeBlocker {}; 825ccb8f90Sopenharmony_ci std::vector<RunningLockType> sleepBlocker { 835ccb8f90Sopenharmony_ci RunningLockType::RUNNINGLOCK_COORDINATION 845ccb8f90Sopenharmony_ci }; 855ccb8f90Sopenharmony_ci std::vector<RunningLockType> hibernateBlocker {}; 865ccb8f90Sopenharmony_ci std::vector<RunningLockType> shutdownBlocker {}; 875ccb8f90Sopenharmony_ci 885ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::AWAKE, std::make_shared<std::vector<RunningLockType>>(awakeBlocker)); 895ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::FREEZE, std::make_shared<std::vector<RunningLockType>>(freezeBlocker)); 905ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::INACTIVE, std::make_shared<std::vector<RunningLockType>>(inactiveBlocker)); 915ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::STAND_BY, std::make_shared<std::vector<RunningLockType>>(standByBlocker)); 925ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::DOZE, std::make_shared<std::vector<RunningLockType>>(dozeBlocker)); 935ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::SLEEP, std::make_shared<std::vector<RunningLockType>>(sleepBlocker)); 945ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::HIBERNATE, std::make_shared<std::vector<RunningLockType>>(hibernateBlocker)); 955ccb8f90Sopenharmony_ci lockMap_.emplace(PowerState::SHUTDOWN, std::make_shared<std::vector<RunningLockType>>(shutdownBlocker)); 965ccb8f90Sopenharmony_ci 975ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Instance end"); 985ccb8f90Sopenharmony_ci} 995ccb8f90Sopenharmony_ci 1005ccb8f90Sopenharmony_ciPowerStateMachine::~PowerStateMachine() 1015ccb8f90Sopenharmony_ci{ 1025ccb8f90Sopenharmony_ci ffrtTimer_.reset(); 1035ccb8f90Sopenharmony_ci} 1045ccb8f90Sopenharmony_ci 1055ccb8f90Sopenharmony_cibool PowerStateMachine::Init() 1065ccb8f90Sopenharmony_ci{ 1075ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Start init"); 1085ccb8f90Sopenharmony_ci stateAction_ = PowerMgrFactory::GetDeviceStateAction(); 1095ccb8f90Sopenharmony_ci InitTransitMap(); 1105ccb8f90Sopenharmony_ci InitStateMap(); 1115ccb8f90Sopenharmony_ci 1125ccb8f90Sopenharmony_ci if (powerStateCBDeathRecipient_ == nullptr) { 1135ccb8f90Sopenharmony_ci powerStateCBDeathRecipient_ = new PowerStateCallbackDeathRecipient(); 1145ccb8f90Sopenharmony_ci } 1155ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Init success"); 1165ccb8f90Sopenharmony_ci return true; 1175ccb8f90Sopenharmony_ci} 1185ccb8f90Sopenharmony_ci 1195ccb8f90Sopenharmony_civoid PowerStateMachine::InitTransitMap() 1205ccb8f90Sopenharmony_ci{ 1215ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 1225ccb8f90Sopenharmony_ci std::vector<PowerState> awake { PowerState::SLEEP }; 1235ccb8f90Sopenharmony_ci#else 1245ccb8f90Sopenharmony_ci std::vector<PowerState> awake { PowerState::SLEEP, PowerState::HIBERNATE }; 1255ccb8f90Sopenharmony_ci#endif 1265ccb8f90Sopenharmony_ci std::vector<PowerState> inactive { PowerState::DIM }; 1275ccb8f90Sopenharmony_ci std::vector<PowerState> sleep { PowerState::DIM }; 1285ccb8f90Sopenharmony_ci 1295ccb8f90Sopenharmony_ci forbidMap_.emplace(PowerState::AWAKE, std::set<PowerState>(awake.begin(), awake.end())); 1305ccb8f90Sopenharmony_ci forbidMap_.emplace(PowerState::INACTIVE, std::set<PowerState>(inactive.begin(), inactive.end())); 1315ccb8f90Sopenharmony_ci forbidMap_.emplace(PowerState::SLEEP, std::set<PowerState>(sleep.begin(), sleep.end())); 1325ccb8f90Sopenharmony_ci 1335ccb8f90Sopenharmony_ci allowMapByReason_.insert({ 1345ccb8f90Sopenharmony_ci { 1355ccb8f90Sopenharmony_ci StateChangeReason::STATE_CHANGE_REASON_REFRESH, 1365ccb8f90Sopenharmony_ci { 1375ccb8f90Sopenharmony_ci {PowerState::DIM, {PowerState::AWAKE}}, 1385ccb8f90Sopenharmony_ci {PowerState::AWAKE, {PowerState::AWAKE}} 1395ccb8f90Sopenharmony_ci } 1405ccb8f90Sopenharmony_ci }, 1415ccb8f90Sopenharmony_ci { 1425ccb8f90Sopenharmony_ci StateChangeReason::STATE_CHANGE_REASON_TIMEOUT, 1435ccb8f90Sopenharmony_ci { 1445ccb8f90Sopenharmony_ci // allow AWAKE to INACTIVE without going to DIM for UTs to pass 1455ccb8f90Sopenharmony_ci {PowerState::AWAKE, {PowerState::DIM, PowerState::INACTIVE}}, 1465ccb8f90Sopenharmony_ci {PowerState::DIM, {PowerState::INACTIVE}}, 1475ccb8f90Sopenharmony_ci {PowerState::INACTIVE, {PowerState::SLEEP}} 1485ccb8f90Sopenharmony_ci } 1495ccb8f90Sopenharmony_ci }, 1505ccb8f90Sopenharmony_ci { 1515ccb8f90Sopenharmony_ci StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK, 1525ccb8f90Sopenharmony_ci { 1535ccb8f90Sopenharmony_ci {PowerState::DIM, {PowerState::INACTIVE}}, 1545ccb8f90Sopenharmony_ci // allow AWAKE to INACTIVE without going to DIM for UTs to pass 1555ccb8f90Sopenharmony_ci {PowerState::AWAKE, {PowerState::INACTIVE}} 1565ccb8f90Sopenharmony_ci } 1575ccb8f90Sopenharmony_ci }, 1585ccb8f90Sopenharmony_ci }); 1595ccb8f90Sopenharmony_ci} 1605ccb8f90Sopenharmony_ci 1615ccb8f90Sopenharmony_ci#ifdef MSDP_MOVEMENT_ENABLE 1625ccb8f90Sopenharmony_cistatic const char* MOVEMENT_STATE_CONFIG = "GetMovementState"; 1635ccb8f90Sopenharmony_cistatic const char* POWER_MANAGER_EXT_PATH = "libpower_manager_ext.z.so"; 1645ccb8f90Sopenharmony_citypedef bool(*FuncMovementState)(); 1655ccb8f90Sopenharmony_ci 1665ccb8f90Sopenharmony_cibool PowerStateMachine::IsMovementStateOn() 1675ccb8f90Sopenharmony_ci{ 1685ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Start to GetMovementState"); 1695ccb8f90Sopenharmony_ci void *stateHandler = dlopen(POWER_MANAGER_EXT_PATH, RTLD_LAZY | RTLD_NODELETE); 1705ccb8f90Sopenharmony_ci if (stateHandler == nullptr) { 1715ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Dlopen GetMovementState failed, reason : %{public}s", dlerror()); 1725ccb8f90Sopenharmony_ci return false; 1735ccb8f90Sopenharmony_ci } 1745ccb8f90Sopenharmony_ci 1755ccb8f90Sopenharmony_ci FuncMovementState MovementStateFlag = reinterpret_cast<FuncMovementState>(dlsym(stateHandler, 1765ccb8f90Sopenharmony_ci MOVEMENT_STATE_CONFIG)); 1775ccb8f90Sopenharmony_ci if (MovementStateFlag == nullptr) { 1785ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "GetMovementState is null, reason : %{public}s", dlerror()); 1795ccb8f90Sopenharmony_ci dlclose(stateHandler); 1805ccb8f90Sopenharmony_ci stateHandler = nullptr; 1815ccb8f90Sopenharmony_ci return false; 1825ccb8f90Sopenharmony_ci } 1835ccb8f90Sopenharmony_ci bool ret = MovementStateFlag(); 1845ccb8f90Sopenharmony_ci dlclose(stateHandler); 1855ccb8f90Sopenharmony_ci stateHandler = nullptr; 1865ccb8f90Sopenharmony_ci return ret; 1875ccb8f90Sopenharmony_ci} 1885ccb8f90Sopenharmony_ci#endif 1895ccb8f90Sopenharmony_ci 1905ccb8f90Sopenharmony_cibool PowerStateMachine::CanTransitTo(PowerState to, StateChangeReason reason) 1915ccb8f90Sopenharmony_ci{ 1925ccb8f90Sopenharmony_ci bool isForbidden = forbidMap_.count(currentState_) && forbidMap_[currentState_].count(to); 1935ccb8f90Sopenharmony_ci if (isForbidden) { 1945ccb8f90Sopenharmony_ci return false; 1955ccb8f90Sopenharmony_ci } 1965ccb8f90Sopenharmony_ci // prevent the double click and pickup to light up the screen when calling or sporting 1975ccb8f90Sopenharmony_ci if ((reason == StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK || 1985ccb8f90Sopenharmony_ci reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) && to == PowerState::AWAKE) { 1995ccb8f90Sopenharmony_ci#ifdef HAS_SENSORS_SENSOR_PART 2005ccb8f90Sopenharmony_ci if (IsProximityClose()) { 2015ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, 2025ccb8f90Sopenharmony_ci "Double-click or pickup isn't allowed to wakeup device when proximity is close."); 2035ccb8f90Sopenharmony_ci return false; 2045ccb8f90Sopenharmony_ci } 2055ccb8f90Sopenharmony_ci#endif 2065ccb8f90Sopenharmony_ci#ifdef MSDP_MOVEMENT_ENABLE 2075ccb8f90Sopenharmony_ci if (IsMovementStateOn()) { 2085ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, 2095ccb8f90Sopenharmony_ci "Double-click or pickup isn't allowed to wakeup device when movement state is on."); 2105ccb8f90Sopenharmony_ci return false; 2115ccb8f90Sopenharmony_ci } 2125ccb8f90Sopenharmony_ci#endif 2135ccb8f90Sopenharmony_ci } 2145ccb8f90Sopenharmony_ci bool isAllowed = (!allowMapByReason_.count(reason) || 2155ccb8f90Sopenharmony_ci (allowMapByReason_[reason].count(currentState_) && allowMapByReason_[reason][currentState_].count(to))); 2165ccb8f90Sopenharmony_ci return isAllowed; 2175ccb8f90Sopenharmony_ci} 2185ccb8f90Sopenharmony_ci 2195ccb8f90Sopenharmony_civoid PowerStateMachine::InitState() 2205ccb8f90Sopenharmony_ci{ 2215ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Init power state"); 2225ccb8f90Sopenharmony_ci if (IsScreenOn()) { 2235ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 2245ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "SCREEN_STATE", 2255ccb8f90Sopenharmony_ci HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", DISPLAY_ON); 2265ccb8f90Sopenharmony_ci#endif 2275ccb8f90Sopenharmony_ci SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_INIT, true); 2285ccb8f90Sopenharmony_ci } else { 2295ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 2305ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "SCREEN_STATE", 2315ccb8f90Sopenharmony_ci HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", DISPLAY_OFF); 2325ccb8f90Sopenharmony_ci#endif 2335ccb8f90Sopenharmony_ci SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_INIT, true); 2345ccb8f90Sopenharmony_ci } 2355ccb8f90Sopenharmony_ci} 2365ccb8f90Sopenharmony_ci 2375ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceAwake() 2385ccb8f90Sopenharmony_ci{ 2395ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::AWAKE, 2405ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::AWAKE, shared_from_this(), [this](StateChangeReason reason) { 2415ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "[UL_POWER] StateController_AWAKE lambda start, reason=%{public}s", 2425ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str()); 2435ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 2445ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER_UE, "SCREEN_ON", 2455ccb8f90Sopenharmony_ci HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "PNAMEID", "PowerManager", "PVERSIONID", "1.0", 2465ccb8f90Sopenharmony_ci "REASON", PowerUtils::GetReasonTypeString(reason).c_str()); 2475ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "SCREEN_ON", 2485ccb8f90Sopenharmony_ci HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "REASON", PowerUtils::GetReasonTypeString(reason).c_str()); 2495ccb8f90Sopenharmony_ci#endif 2505ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOnTime = GetTickCount(); 2515ccb8f90Sopenharmony_ci uint32_t ret = this->stateAction_->SetDisplayState(DisplayState::DISPLAY_ON, reason); 2525ccb8f90Sopenharmony_ci if (ret != ActionResult::SUCCESS) { 2535ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Failed to go to AWAKE, display error, ret: %{public}u", ret); 2545ccb8f90Sopenharmony_ci return TransitResult::DISPLAY_ON_ERR; 2555ccb8f90Sopenharmony_ci } 2565ccb8f90Sopenharmony_ci if (reason != StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT) { 2575ccb8f90Sopenharmony_ci ResetInactiveTimer(); 2585ccb8f90Sopenharmony_ci } 2595ccb8f90Sopenharmony_ci SystemSuspendController::GetInstance().DisallowAutoSleep(); 2605ccb8f90Sopenharmony_ci SystemSuspendController::GetInstance().Wakeup(); 2615ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 2625ccb8f90Sopenharmony_ci })); 2635ccb8f90Sopenharmony_ci} 2645ccb8f90Sopenharmony_ci 2655ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceFreeze() 2665ccb8f90Sopenharmony_ci{ 2675ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::FREEZE, 2685ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::FREEZE, shared_from_this(), [this](StateChangeReason reason) { 2695ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "StateController_FREEZE lambda start"); 2705ccb8f90Sopenharmony_ci // Subsequent added functions 2715ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 2725ccb8f90Sopenharmony_ci })); 2735ccb8f90Sopenharmony_ci} 2745ccb8f90Sopenharmony_ci 2755ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceInactive() 2765ccb8f90Sopenharmony_ci{ 2775ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::INACTIVE, 2785ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::INACTIVE, shared_from_this(), [this](StateChangeReason reason) { 2795ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "[UL_POWER] StateController_INACTIVE lambda start"); 2805ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 2815ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER_UE, "SCREEN_OFF", 2825ccb8f90Sopenharmony_ci HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "PNAMEID", "PowerManager", "PVERSIONID", "1.0", 2835ccb8f90Sopenharmony_ci "REASON", PowerUtils::GetReasonTypeString(reason).c_str()); 2845ccb8f90Sopenharmony_ci#endif 2855ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOffTime = GetTickCount(); 2865ccb8f90Sopenharmony_ci DisplayState state = DisplayState::DISPLAY_OFF; 2875ccb8f90Sopenharmony_ci if (enableDisplaySuspend_) { 2885ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Display suspend enabled"); 2895ccb8f90Sopenharmony_ci state = DisplayState::DISPLAY_SUSPEND; 2905ccb8f90Sopenharmony_ci } 2915ccb8f90Sopenharmony_ci uint32_t ret = this->stateAction_->SetDisplayState(state, reason); 2925ccb8f90Sopenharmony_ci if (ret != ActionResult::SUCCESS) { 2935ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Failed to go to INACTIVE, display error, ret: %{public}u", ret); 2945ccb8f90Sopenharmony_ci return TransitResult::DISPLAY_OFF_ERR; 2955ccb8f90Sopenharmony_ci } 2965ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_TIMEOUT_MSG); 2975ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); 2985ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 2995ccb8f90Sopenharmony_ci })); 3005ccb8f90Sopenharmony_ci} 3015ccb8f90Sopenharmony_ci 3025ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceStandBy() 3035ccb8f90Sopenharmony_ci{ 3045ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::STAND_BY, 3055ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::STAND_BY, shared_from_this(), [this](StateChangeReason reason) { 3065ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "StateController_STAND_BY lambda start"); 3075ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOffTime = GetTickCount(); 3085ccb8f90Sopenharmony_ci // Subsequent added functions 3095ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 3105ccb8f90Sopenharmony_ci })); 3115ccb8f90Sopenharmony_ci} 3125ccb8f90Sopenharmony_ci 3135ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceDoze() 3145ccb8f90Sopenharmony_ci{ 3155ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::DOZE, 3165ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::DOZE, shared_from_this(), [this](StateChangeReason reason) { 3175ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "StateController_DOZE lambda start"); 3185ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOffTime = GetTickCount(); 3195ccb8f90Sopenharmony_ci // Subsequent added functions 3205ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 3215ccb8f90Sopenharmony_ci })); 3225ccb8f90Sopenharmony_ci} 3235ccb8f90Sopenharmony_ci 3245ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceSleep() 3255ccb8f90Sopenharmony_ci{ 3265ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::SLEEP, 3275ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::SLEEP, shared_from_this(), [this](StateChangeReason reason) { 3285ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "StateController_SLEEP lambda start"); 3295ccb8f90Sopenharmony_ci SystemSuspendController::GetInstance().AllowAutoSleep(); 3305ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 3315ccb8f90Sopenharmony_ci })); 3325ccb8f90Sopenharmony_ci} 3335ccb8f90Sopenharmony_ci 3345ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceHibernate() 3355ccb8f90Sopenharmony_ci{ 3365ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::HIBERNATE, 3375ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::HIBERNATE, shared_from_this(), [this](StateChangeReason reason) { 3385ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "StateController_HIBERNATE lambda start"); 3395ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 3405ccb8f90Sopenharmony_ci })); 3415ccb8f90Sopenharmony_ci} 3425ccb8f90Sopenharmony_ci 3435ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceShutdown() 3445ccb8f90Sopenharmony_ci{ 3455ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::SHUTDOWN, 3465ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::SHUTDOWN, shared_from_this(), [this](StateChangeReason reason) { 3475ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "StateController_SHUTDOWN lambda start"); 3485ccb8f90Sopenharmony_ci // Subsequent added functions 3495ccb8f90Sopenharmony_ci return TransitResult::SUCCESS; 3505ccb8f90Sopenharmony_ci })); 3515ccb8f90Sopenharmony_ci} 3525ccb8f90Sopenharmony_ci 3535ccb8f90Sopenharmony_civoid PowerStateMachine::EmplaceDim() 3545ccb8f90Sopenharmony_ci{ 3555ccb8f90Sopenharmony_ci controllerMap_.emplace(PowerState::DIM, 3565ccb8f90Sopenharmony_ci std::make_shared<StateController>(PowerState::DIM, shared_from_this(), [this](StateChangeReason reason) { 3575ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "[UL_POWER] StateController_DIM lambda start"); 3585ccb8f90Sopenharmony_ci if (GetDisplayOffTime() < 0) { 3595ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Auto display off is disabled"); 3605ccb8f90Sopenharmony_ci return TransitResult::OTHER_ERR; 3615ccb8f90Sopenharmony_ci } 3625ccb8f90Sopenharmony_ci int64_t dimTime = GetDimTime(GetDisplayOffTime()); 3635ccb8f90Sopenharmony_ci if (reason == StateChangeReason::STATE_CHANGE_REASON_COORDINATION) { 3645ccb8f90Sopenharmony_ci dimTime = COORDINATED_STATE_SCREEN_OFF_TIME_MS; 3655ccb8f90Sopenharmony_ci } 3665ccb8f90Sopenharmony_ci uint32_t ret = stateAction_->SetDisplayState(DisplayState::DISPLAY_DIM, reason); 3675ccb8f90Sopenharmony_ci if (ret != ActionResult::SUCCESS) { 3685ccb8f90Sopenharmony_ci // failed but not return, still need to set screen off 3695ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Failed to go to DIM, display error, ret: %{public}u", ret); 3705ccb8f90Sopenharmony_ci } 3715ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_TIMEOUT_MSG); 3725ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); 3735ccb8f90Sopenharmony_ci // Set a timer without checking runninglock, but the actual timeout event can still be blocked. 3745ccb8f90Sopenharmony_ci // Theoretically, this timer is always cancelable before the current task is finished. 3755ccb8f90Sopenharmony_ci SetDelayTimer(dimTime, PowerStateMachine::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); 3765ccb8f90Sopenharmony_ci // in case a refresh action occurs, change display state back to on 3775ccb8f90Sopenharmony_ci if (settingStateFlag_.load() == 3785ccb8f90Sopenharmony_ci static_cast<int64_t>(SettingStateFlag::StateFlag::SETTING_DIM_INTERRUPTED)) { 3795ccb8f90Sopenharmony_ci stateAction_->SetDisplayState(DisplayState::DISPLAY_ON, StateChangeReason::STATE_CHANGE_REASON_REFRESH); 3805ccb8f90Sopenharmony_ci ResetInactiveTimer(); 3815ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_STATE, "Setting DIM interrupted!"); 3825ccb8f90Sopenharmony_ci return TransitResult::OTHER_ERR; 3835ccb8f90Sopenharmony_ci } 3845ccb8f90Sopenharmony_ci return ret == ActionResult::SUCCESS ? TransitResult::SUCCESS : TransitResult::OTHER_ERR; 3855ccb8f90Sopenharmony_ci })); 3865ccb8f90Sopenharmony_ci} 3875ccb8f90Sopenharmony_ci 3885ccb8f90Sopenharmony_civoid PowerStateMachine::InitStateMap() 3895ccb8f90Sopenharmony_ci{ 3905ccb8f90Sopenharmony_ci EmplaceAwake(); 3915ccb8f90Sopenharmony_ci EmplaceFreeze(); 3925ccb8f90Sopenharmony_ci EmplaceInactive(); 3935ccb8f90Sopenharmony_ci EmplaceStandBy(); 3945ccb8f90Sopenharmony_ci EmplaceDoze(); 3955ccb8f90Sopenharmony_ci EmplaceSleep(); 3965ccb8f90Sopenharmony_ci EmplaceHibernate(); 3975ccb8f90Sopenharmony_ci EmplaceShutdown(); 3985ccb8f90Sopenharmony_ci EmplaceDim(); 3995ccb8f90Sopenharmony_ci} 4005ccb8f90Sopenharmony_ci 4015ccb8f90Sopenharmony_civoid PowerStateMachine::onSuspend() 4025ccb8f90Sopenharmony_ci{ 4035ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_SUSPEND, "System is suspending"); 4045ccb8f90Sopenharmony_ci} 4055ccb8f90Sopenharmony_ci 4065ccb8f90Sopenharmony_civoid PowerStateMachine::onWakeup() 4075ccb8f90Sopenharmony_ci{ 4085ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "System is awaking"); 4095ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 4105ccb8f90Sopenharmony_ci if (pms == nullptr) { 4115ccb8f90Sopenharmony_ci return; 4125ccb8f90Sopenharmony_ci } 4135ccb8f90Sopenharmony_ci FFRTTask task = [&pms] { 4145ccb8f90Sopenharmony_ci pms->GetPowerStateMachine()->HandleSystemWakeup(); 4155ccb8f90Sopenharmony_ci }; 4165ccb8f90Sopenharmony_ci} 4175ccb8f90Sopenharmony_ci 4185ccb8f90Sopenharmony_civoid PowerStateMachine::SuspendDeviceInner( 4195ccb8f90Sopenharmony_ci pid_t pid, int64_t callTimeMs, SuspendDeviceType type, bool suspendImmed, bool ignoreScreenState) 4205ccb8f90Sopenharmony_ci{ 4215ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HITRACE_PART 4225ccb8f90Sopenharmony_ci PowerHitrace powerHitrace("SuspendDevice"); 4235ccb8f90Sopenharmony_ci#endif 4245ccb8f90Sopenharmony_ci if (type > SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX) { 4255ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_SUSPEND, "Invalid type: %{public}d", type); 4265ccb8f90Sopenharmony_ci return; 4275ccb8f90Sopenharmony_ci } 4285ccb8f90Sopenharmony_ci // Check the screen state 4295ccb8f90Sopenharmony_ci if (!ignoreScreenState) { 4305ccb8f90Sopenharmony_ci if (stateAction_ != nullptr) { 4315ccb8f90Sopenharmony_ci stateAction_->Suspend( 4325ccb8f90Sopenharmony_ci callTimeMs, type, suspendImmed ? SUSPEND_DEVICE_IMMEDIATELY : SUSPEND_DEVICE_NEED_DOZE); 4335ccb8f90Sopenharmony_ci } 4345ccb8f90Sopenharmony_ci mDeviceState_.lastSuspendDeviceTime = callTimeMs; 4355ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SUSPEND, "Suspend device success"); 4365ccb8f90Sopenharmony_ci } else { 4375ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SUSPEND, "Do not suspend device, screen state is ignored"); 4385ccb8f90Sopenharmony_ci } 4395ccb8f90Sopenharmony_ci 4405ccb8f90Sopenharmony_ci if (SetState(PowerState::INACTIVE, GetReasonBySuspendType(type), true)) { 4415ccb8f90Sopenharmony_ci uint32_t delay = 0; 4425ccb8f90Sopenharmony_ci SetAutoSuspend(type, delay); 4435ccb8f90Sopenharmony_ci } 4445ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SUSPEND, "Suspend device finish"); 4455ccb8f90Sopenharmony_ci} 4465ccb8f90Sopenharmony_ci 4475ccb8f90Sopenharmony_ciWakeupDeviceType PowerStateMachine::ParseWakeupDeviceType(const std::string& details) 4485ccb8f90Sopenharmony_ci{ 4495ccb8f90Sopenharmony_ci WakeupDeviceType parsedType = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION; 4505ccb8f90Sopenharmony_ci 4515ccb8f90Sopenharmony_ci if (strcmp(details.c_str(), "pre_bright") == 0) { 4525ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT; 4535ccb8f90Sopenharmony_ci } else if (strcmp(details.c_str(), "pre_bright_auth_success") == 0) { 4545ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_SUCCESS; 4555ccb8f90Sopenharmony_ci } else if (strcmp(details.c_str(), "pre_bright_auth_fail_screen_on") == 0) { 4565ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON; 4575ccb8f90Sopenharmony_ci } else if (strcmp(details.c_str(), "pre_bright_auth_fail_screen_off") == 0) { 4585ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF; 4595ccb8f90Sopenharmony_ci } else if (strcmp(details.c_str(), "incoming call") == 0) { 4605ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_INCOMING_CALL; 4615ccb8f90Sopenharmony_ci } else if (strcmp(details.c_str(), "fake_str_check_unlock") == 0) { 4625ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_EXIT_SYSTEM_STR; 4635ccb8f90Sopenharmony_ci } else if (strcmp(details.c_str(), "shell") == 0) { 4645ccb8f90Sopenharmony_ci parsedType = WakeupDeviceType::WAKEUP_DEVICE_SHELL; 4655ccb8f90Sopenharmony_ci } 4665ccb8f90Sopenharmony_ci 4675ccb8f90Sopenharmony_ci if (parsedType != WakeupDeviceType::WAKEUP_DEVICE_APPLICATION) { 4685ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "Parsed wakeup type is %{public}d", static_cast<uint32_t>(parsedType)); 4695ccb8f90Sopenharmony_ci } 4705ccb8f90Sopenharmony_ci return parsedType; 4715ccb8f90Sopenharmony_ci} 4725ccb8f90Sopenharmony_ci 4735ccb8f90Sopenharmony_cibool PowerStateMachine::IsPreBrightAuthReason(StateChangeReason reason) 4745ccb8f90Sopenharmony_ci{ 4755ccb8f90Sopenharmony_ci bool ret = false; 4765ccb8f90Sopenharmony_ci switch (reason) { 4775ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS: 4785ccb8f90Sopenharmony_ci ret = true; 4795ccb8f90Sopenharmony_ci break; 4805ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON: 4815ccb8f90Sopenharmony_ci ret = true; 4825ccb8f90Sopenharmony_ci break; 4835ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF: 4845ccb8f90Sopenharmony_ci ret = true; 4855ccb8f90Sopenharmony_ci break; 4865ccb8f90Sopenharmony_ci default: 4875ccb8f90Sopenharmony_ci break; 4885ccb8f90Sopenharmony_ci } 4895ccb8f90Sopenharmony_ci return ret; 4905ccb8f90Sopenharmony_ci} 4915ccb8f90Sopenharmony_ci 4925ccb8f90Sopenharmony_cibool PowerStateMachine::IsPreBrightWakeUp(WakeupDeviceType type) 4935ccb8f90Sopenharmony_ci{ 4945ccb8f90Sopenharmony_ci bool ret = false; 4955ccb8f90Sopenharmony_ci switch (type) { 4965ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT: 4975ccb8f90Sopenharmony_ci ret = true; 4985ccb8f90Sopenharmony_ci break; 4995ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_SUCCESS: 5005ccb8f90Sopenharmony_ci ret = true; 5015ccb8f90Sopenharmony_ci break; 5025ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON: 5035ccb8f90Sopenharmony_ci ret = true; 5045ccb8f90Sopenharmony_ci break; 5055ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF: 5065ccb8f90Sopenharmony_ci ret = true; 5075ccb8f90Sopenharmony_ci break; 5085ccb8f90Sopenharmony_ci default: 5095ccb8f90Sopenharmony_ci break; 5105ccb8f90Sopenharmony_ci } 5115ccb8f90Sopenharmony_ci return ret; 5125ccb8f90Sopenharmony_ci} 5135ccb8f90Sopenharmony_ci 5145ccb8f90Sopenharmony_civoid PowerStateMachine::HandlePreBrightWakeUp(int64_t callTimeMs, WakeupDeviceType type, const std::string& details, 5155ccb8f90Sopenharmony_ci const std::string& pkgName, bool timeoutTriggered) 5165ccb8f90Sopenharmony_ci{ 5175ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "This wakeup event is trigged by %{public}s.", details.c_str()); 5185ccb8f90Sopenharmony_ci 5195ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 5205ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 5215ccb8f90Sopenharmony_ci if (suspendController != nullptr) { 5225ccb8f90Sopenharmony_ci suspendController->StopSleep(); 5235ccb8f90Sopenharmony_ci } else { 5245ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "suspendController is nullptr, can't stop sleep ffrt task"); 5255ccb8f90Sopenharmony_ci } 5265ccb8f90Sopenharmony_ci if (stateAction_ != nullptr) { 5275ccb8f90Sopenharmony_ci stateAction_->Wakeup(callTimeMs, type, details, pkgName); 5285ccb8f90Sopenharmony_ci } 5295ccb8f90Sopenharmony_ci mDeviceState_.lastWakeupDeviceTime = callTimeMs; 5305ccb8f90Sopenharmony_ci 5315ccb8f90Sopenharmony_ci StateChangeReason reason = GetReasonByWakeType(type); 5325ccb8f90Sopenharmony_ci if (!timeoutTriggered && IsPreBrightAuthReason(reason)) { 5335ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "Cancel pre-bright-auth timer, rason=%{public}s", 5345ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str()); 5355ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG); 5365ccb8f90Sopenharmony_ci } 5375ccb8f90Sopenharmony_ci SetState(PowerState::AWAKE, reason, true); 5385ccb8f90Sopenharmony_ci 5395ccb8f90Sopenharmony_ci switch (type) { 5405ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT: { 5415ccb8f90Sopenharmony_ci break; 5425ccb8f90Sopenharmony_ci } 5435ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_SUCCESS: // fall through 5445ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON: 5455ccb8f90Sopenharmony_ci if (suspendController != nullptr) { 5465ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "HandlePreBrightWakeUp. TriggerSyncSleepCallback start."); 5475ccb8f90Sopenharmony_ci suspendController->TriggerSyncSleepCallback(true); 5485ccb8f90Sopenharmony_ci } else { 5495ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "HandlePreBrightWakeUp. suspendController is nullptr"); 5505ccb8f90Sopenharmony_ci } 5515ccb8f90Sopenharmony_ci break; 5525ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF: 5535ccb8f90Sopenharmony_ci if (suspendController != nullptr) { 5545ccb8f90Sopenharmony_ci suspendController->StartSleepTimer( 5555ccb8f90Sopenharmony_ci SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, 5565ccb8f90Sopenharmony_ci static_cast<uint32_t>(SuspendAction::ACTION_AUTO_SUSPEND), 0); 5575ccb8f90Sopenharmony_ci } else { 5585ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "suspendController is nullptr, can't restore sleep ffrt task"); 5595ccb8f90Sopenharmony_ci } 5605ccb8f90Sopenharmony_ci break; 5615ccb8f90Sopenharmony_ci default: 5625ccb8f90Sopenharmony_ci break; 5635ccb8f90Sopenharmony_ci } 5645ccb8f90Sopenharmony_ci 5655ccb8f90Sopenharmony_ci return; 5665ccb8f90Sopenharmony_ci} 5675ccb8f90Sopenharmony_ci 5685ccb8f90Sopenharmony_civoid PowerStateMachine::WakeupDeviceInner( 5695ccb8f90Sopenharmony_ci pid_t pid, int64_t callTimeMs, WakeupDeviceType type, const std::string& details, const std::string& pkgName) 5705ccb8f90Sopenharmony_ci{ 5715ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HITRACE_PART 5725ccb8f90Sopenharmony_ci PowerHitrace powerHitrace("WakeupDevice"); 5735ccb8f90Sopenharmony_ci#endif 5745ccb8f90Sopenharmony_ci if (type > WakeupDeviceType::WAKEUP_DEVICE_MAX) { 5755ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_WAKEUP, "Invalid type: %{public}d", type); 5765ccb8f90Sopenharmony_ci return; 5775ccb8f90Sopenharmony_ci } 5785ccb8f90Sopenharmony_ci 5795ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 5805ccb8f90Sopenharmony_ci if (!IsSwitchOpen() || IsHibernating()) { 5815ccb8f90Sopenharmony_ci#else 5825ccb8f90Sopenharmony_ci if (!IsSwitchOpen()) { 5835ccb8f90Sopenharmony_ci#endif 5845ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "Switch is closed or hibernating, wakeup device do nothing."); 5855ccb8f90Sopenharmony_ci return; 5865ccb8f90Sopenharmony_ci } 5875ccb8f90Sopenharmony_ci 5885ccb8f90Sopenharmony_ci if (type == WakeupDeviceType::WAKEUP_DEVICE_APPLICATION) { 5895ccb8f90Sopenharmony_ci type = ParseWakeupDeviceType(details); 5905ccb8f90Sopenharmony_ci } 5915ccb8f90Sopenharmony_ci 5925ccb8f90Sopenharmony_ci if (IsPreBrightWakeUp(type)) { 5935ccb8f90Sopenharmony_ci HandlePreBrightWakeUp(callTimeMs, type, details, pkgName); 5945ccb8f90Sopenharmony_ci return; 5955ccb8f90Sopenharmony_ci } 5965ccb8f90Sopenharmony_ci 5975ccb8f90Sopenharmony_ci // Call legacy wakeup, Check the screen state 5985ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 5995ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 6005ccb8f90Sopenharmony_ci if (suspendController != nullptr) { 6015ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "Stop sleep ffrt task"); 6025ccb8f90Sopenharmony_ci suspendController->StopSleep(); 6035ccb8f90Sopenharmony_ci } 6045ccb8f90Sopenharmony_ci 6055ccb8f90Sopenharmony_ci if (stateAction_ != nullptr) { 6065ccb8f90Sopenharmony_ci stateAction_->Wakeup(callTimeMs, type, details, pkgName); 6075ccb8f90Sopenharmony_ci } 6085ccb8f90Sopenharmony_ci mDeviceState_.lastWakeupDeviceTime = callTimeMs; 6095ccb8f90Sopenharmony_ci 6105ccb8f90Sopenharmony_ci SetState(PowerState::AWAKE, GetReasonByWakeType(type), true); 6115ccb8f90Sopenharmony_ci 6125ccb8f90Sopenharmony_ci if (suspendController != nullptr) { 6135ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "WakeupDeviceInner. TriggerSyncSleepCallback start."); 6145ccb8f90Sopenharmony_ci suspendController->TriggerSyncSleepCallback(true); 6155ccb8f90Sopenharmony_ci } else { 6165ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "WakeupDeviceInner. suspendController is nullptr"); 6175ccb8f90Sopenharmony_ci } 6185ccb8f90Sopenharmony_ci 6195ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "Wakeup device finish"); 6205ccb8f90Sopenharmony_ci} 6215ccb8f90Sopenharmony_ci 6225ccb8f90Sopenharmony_civoid PowerStateMachine::RefreshActivityInner( 6235ccb8f90Sopenharmony_ci pid_t pid, int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) 6245ccb8f90Sopenharmony_ci{ 6255ccb8f90Sopenharmony_ci if (type > UserActivityType::USER_ACTIVITY_TYPE_MAX) { 6265ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_ACTIVITY, "Invalid type: %{public}d", type); 6275ccb8f90Sopenharmony_ci return; 6285ccb8f90Sopenharmony_ci } 6295ccb8f90Sopenharmony_ci // Check the screen state 6305ccb8f90Sopenharmony_ci if (IsScreenOn() && !IsSettingState(PowerState::INACTIVE)) { 6315ccb8f90Sopenharmony_ci if (stateAction_ != nullptr) { 6325ccb8f90Sopenharmony_ci stateAction_->RefreshActivity(callTimeMs, type, 6335ccb8f90Sopenharmony_ci needChangeBacklight ? REFRESH_ACTIVITY_NEED_CHANGE_LIGHTS : REFRESH_ACTIVITY_NO_CHANGE_LIGHTS); 6345ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOnTime = GetTickCount(); 6355ccb8f90Sopenharmony_ci } 6365ccb8f90Sopenharmony_ci if (GetState() == PowerState::DIM || IsSettingState(PowerState::DIM)) { 6375ccb8f90Sopenharmony_ci // Inactive to Awake will be blocked for this reason in CanTransitTo() 6385ccb8f90Sopenharmony_ci SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_REFRESH, true); 6395ccb8f90Sopenharmony_ci } else { 6405ccb8f90Sopenharmony_ci // There is a small chance that the last "if" statement occurs before the (already started) ffrt task 6415ccb8f90Sopenharmony_ci // is actually trying to set DIM state. 6425ccb8f90Sopenharmony_ci // In that case we may still (not guaranteed) interrupt it. 6435ccb8f90Sopenharmony_ci ResetInactiveTimer(false); 6445ccb8f90Sopenharmony_ci } 6455ccb8f90Sopenharmony_ci } else { 6465ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Ignore refresh activity, screen is off"); 6475ccb8f90Sopenharmony_ci } 6485ccb8f90Sopenharmony_ci} 6495ccb8f90Sopenharmony_ci 6505ccb8f90Sopenharmony_cibool PowerStateMachine::CheckRefreshTime() 6515ccb8f90Sopenharmony_ci{ 6525ccb8f90Sopenharmony_ci // The minimum refreshactivity interval is 100ms!! 6535ccb8f90Sopenharmony_ci int64_t now = GetTickCount(); 6545ccb8f90Sopenharmony_ci if ((mDeviceState_.lastRefreshActivityTime + MIN_TIME_MS_BETWEEN_USERACTIVITIES) > now) { 6555ccb8f90Sopenharmony_ci return true; 6565ccb8f90Sopenharmony_ci } 6575ccb8f90Sopenharmony_ci mDeviceState_.lastRefreshActivityTime = now; 6585ccb8f90Sopenharmony_ci return false; 6595ccb8f90Sopenharmony_ci} 6605ccb8f90Sopenharmony_ci 6615ccb8f90Sopenharmony_cibool PowerStateMachine::OverrideScreenOffTimeInner(int64_t timeout) 6625ccb8f90Sopenharmony_ci{ 6635ccb8f90Sopenharmony_ci if (!isScreenOffTimeOverride_) { 6645ccb8f90Sopenharmony_ci int64_t beforeOverrideTime = this->GetDisplayOffTime(); 6655ccb8f90Sopenharmony_ci isScreenOffTimeOverride_ = true; 6665ccb8f90Sopenharmony_ci g_beforeOverrideTime = beforeOverrideTime; 6675ccb8f90Sopenharmony_ci } 6685ccb8f90Sopenharmony_ci this->SetDisplayOffTime(timeout, false); 6695ccb8f90Sopenharmony_ci POWER_HILOGD(COMP_SVC, "Override screenOffTime finish"); 6705ccb8f90Sopenharmony_ci return true; 6715ccb8f90Sopenharmony_ci} 6725ccb8f90Sopenharmony_ci 6735ccb8f90Sopenharmony_cibool PowerStateMachine::RestoreScreenOffTimeInner() 6745ccb8f90Sopenharmony_ci{ 6755ccb8f90Sopenharmony_ci if (!isScreenOffTimeOverride_) { 6765ccb8f90Sopenharmony_ci POWER_HILOGD(COMP_SVC, "RestoreScreenOffTime is not override, no need to restore"); 6775ccb8f90Sopenharmony_ci return false; 6785ccb8f90Sopenharmony_ci } 6795ccb8f90Sopenharmony_ci this->SetDisplayOffTime(g_beforeOverrideTime, false); 6805ccb8f90Sopenharmony_ci isScreenOffTimeOverride_ = false; 6815ccb8f90Sopenharmony_ci POWER_HILOGD(COMP_SVC, "Restore screenOffTime finish"); 6825ccb8f90Sopenharmony_ci return true; 6835ccb8f90Sopenharmony_ci} 6845ccb8f90Sopenharmony_ci 6855ccb8f90Sopenharmony_cibool PowerStateMachine::ForceSuspendDeviceInner(pid_t pid, int64_t callTimeMs) 6865ccb8f90Sopenharmony_ci{ 6875ccb8f90Sopenharmony_ci SetState( 6885ccb8f90Sopenharmony_ci PowerState::INACTIVE, GetReasonBySuspendType(SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND), true); 6895ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 6905ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 6915ccb8f90Sopenharmony_ci if (suspendController != nullptr) { 6925ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_SUSPEND, "ForceSuspendDeviceInner StartSleepTimer start."); 6935ccb8f90Sopenharmony_ci suspendController->StartSleepTimer( 6945ccb8f90Sopenharmony_ci SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, 6955ccb8f90Sopenharmony_ci static_cast<uint32_t>(SuspendAction::ACTION_FORCE_SUSPEND), 0); 6965ccb8f90Sopenharmony_ci } 6975ccb8f90Sopenharmony_ci 6985ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_SUSPEND, "Force suspend finish"); 6995ccb8f90Sopenharmony_ci return true; 7005ccb8f90Sopenharmony_ci} 7015ccb8f90Sopenharmony_ci 7025ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 7035ccb8f90Sopenharmony_cibool PowerStateMachine::PrepareHibernate(bool clearMemory) 7045ccb8f90Sopenharmony_ci{ 7055ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 7065ccb8f90Sopenharmony_ci auto hibernateController = pms->GetHibernateController(); 7075ccb8f90Sopenharmony_ci if (hibernateController == nullptr) { 7085ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "hibernateController is nullptr."); 7095ccb8f90Sopenharmony_ci return false; 7105ccb8f90Sopenharmony_ci } 7115ccb8f90Sopenharmony_ci SystemSuspendController::GetInstance().Wakeup(); 7125ccb8f90Sopenharmony_ci bool ret = true; 7135ccb8f90Sopenharmony_ci if (!SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true)) { 7145ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "failed to set state to inactive."); 7155ccb8f90Sopenharmony_ci } 7165ccb8f90Sopenharmony_ci 7175ccb8f90Sopenharmony_ci hibernateController->PreHibernate(); 7185ccb8f90Sopenharmony_ci g_preHibernateStart = GetTickCount(); 7195ccb8f90Sopenharmony_ci if (clearMemory) { 7205ccb8f90Sopenharmony_ci if (AccountSA::OsAccountManager::DeactivateAllOsAccounts() != ERR_OK) { 7215ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "deactivate all os accounts failed."); 7225ccb8f90Sopenharmony_ci return false; 7235ccb8f90Sopenharmony_ci } 7245ccb8f90Sopenharmony_ci int32_t id; 7255ccb8f90Sopenharmony_ci if (AccountSA::OsAccountManager::GetDefaultActivatedOsAccount(id) != ERR_OK) { 7265ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "get default activated os account failed."); 7275ccb8f90Sopenharmony_ci return false; 7285ccb8f90Sopenharmony_ci } 7295ccb8f90Sopenharmony_ci if (AccountSA::OsAccountManager::ActivateOsAccount(id) != ERR_OK) { 7305ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "activate os account failed."); 7315ccb8f90Sopenharmony_ci return false; 7325ccb8f90Sopenharmony_ci } 7335ccb8f90Sopenharmony_ci } 7345ccb8f90Sopenharmony_ci if (clearMemory) { 7355ccb8f90Sopenharmony_ci if (!OHOS::system::SetParameter(POWERMGR_STOPSERVICE.c_str(), "true")) { 7365ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "set parameter POWERMGR_STOPSERVICE true failed."); 7375ccb8f90Sopenharmony_ci return false; 7385ccb8f90Sopenharmony_ci } 7395ccb8f90Sopenharmony_ci } 7405ccb8f90Sopenharmony_ci 7415ccb8f90Sopenharmony_ci if (!SetState(PowerState::HIBERNATE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true)) { 7425ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "failed to set state to hibernate."); 7435ccb8f90Sopenharmony_ci ret = false; 7445ccb8f90Sopenharmony_ci } 7455ccb8f90Sopenharmony_ci return ret; 7465ccb8f90Sopenharmony_ci} 7475ccb8f90Sopenharmony_ci#endif 7485ccb8f90Sopenharmony_ci 7495ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 7505ccb8f90Sopenharmony_ciuint32_t PowerStateMachine::GetPreHibernateDelay() 7515ccb8f90Sopenharmony_ci{ 7525ccb8f90Sopenharmony_ci int64_t preHibernateEnd = GetTickCount(); 7535ccb8f90Sopenharmony_ci uint32_t preHibernateDelay = static_cast<uint32_t>(preHibernateEnd - g_preHibernateStart); 7545ccb8f90Sopenharmony_ci preHibernateDelay = preHibernateDelay > HIBERNATE_DELAY_MS ? 0 : HIBERNATE_DELAY_MS - preHibernateDelay; 7555ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_SUSPEND, "preHibernateDelay = %{public}u", preHibernateDelay); 7565ccb8f90Sopenharmony_ci return preHibernateDelay; 7575ccb8f90Sopenharmony_ci} 7585ccb8f90Sopenharmony_ci#endif 7595ccb8f90Sopenharmony_ci 7605ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4 7615ccb8f90Sopenharmony_cibool PowerStateMachine::HibernateInner(bool clearMemory) 7625ccb8f90Sopenharmony_ci{ 7635ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "HibernateInner begin."); 7645ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 7655ccb8f90Sopenharmony_ci auto hibernateController = pms->GetHibernateController(); 7665ccb8f90Sopenharmony_ci if (hibernateController == nullptr) { 7675ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "hibernateController is nullptr."); 7685ccb8f90Sopenharmony_ci return false; 7695ccb8f90Sopenharmony_ci } 7705ccb8f90Sopenharmony_ci if (hibernating_) { 7715ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "the device is hibernating, please try again later."); 7725ccb8f90Sopenharmony_ci return false; 7735ccb8f90Sopenharmony_ci } 7745ccb8f90Sopenharmony_ci hibernating_ = true; 7755ccb8f90Sopenharmony_ci if (!PrepareHibernate(clearMemory) && clearMemory) { 7765ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "prepare hibernate failed, shutdown begin."); 7775ccb8f90Sopenharmony_ci pms->ShutDownDevice("shutdown_by_user"); 7785ccb8f90Sopenharmony_ci hibernating_ = false; 7795ccb8f90Sopenharmony_ci return true; 7805ccb8f90Sopenharmony_ci } 7815ccb8f90Sopenharmony_ci 7825ccb8f90Sopenharmony_ci FFRTTask task = [hibernateController, this, clearMemory, pms]() { 7835ccb8f90Sopenharmony_ci bool success = hibernateController->Hibernate(clearMemory); 7845ccb8f90Sopenharmony_ci if (!success && clearMemory) { 7855ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "hibernate failed, shutdown begin."); 7865ccb8f90Sopenharmony_ci pms->ShutDownDevice("shutdown_by_user"); 7875ccb8f90Sopenharmony_ci hibernating_ = false; 7885ccb8f90Sopenharmony_ci return; 7895ccb8f90Sopenharmony_ci } 7905ccb8f90Sopenharmony_ci if (success) { 7915ccb8f90Sopenharmony_ci switchOpen_ = true; 7925ccb8f90Sopenharmony_ci } 7935ccb8f90Sopenharmony_ci hibernating_ = false; 7945ccb8f90Sopenharmony_ci if (!SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true)) { 7955ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "failed to set state to awake when hibernate."); 7965ccb8f90Sopenharmony_ci } 7975ccb8f90Sopenharmony_ci if (clearMemory) { 7985ccb8f90Sopenharmony_ci if (!OHOS::system::SetParameter(POWERMGR_STOPSERVICE.c_str(), "false")) { 7995ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "set parameter POWERMGR_STOPSERVICE false failed."); 8005ccb8f90Sopenharmony_ci } 8015ccb8f90Sopenharmony_ci } 8025ccb8f90Sopenharmony_ci hibernateController->PostHibernate(success); 8035ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_SUSPEND, "power mgr machine hibernate end."); 8045ccb8f90Sopenharmony_ci }; 8055ccb8f90Sopenharmony_ci if (ffrtTimer_ == nullptr) { 8065ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "ffrtTimer is null"); 8075ccb8f90Sopenharmony_ci hibernating_ = false; 8085ccb8f90Sopenharmony_ci return false; 8095ccb8f90Sopenharmony_ci } 8105ccb8f90Sopenharmony_ci ffrtTimer_->SetTimer(TIMER_ID_HIBERNATE, task, GetPreHibernateDelay()); 8115ccb8f90Sopenharmony_ci return true; 8125ccb8f90Sopenharmony_ci} 8135ccb8f90Sopenharmony_ci#endif 8145ccb8f90Sopenharmony_ci 8155ccb8f90Sopenharmony_cibool PowerStateMachine::IsScreenOn(bool needPrintLog) 8165ccb8f90Sopenharmony_ci{ 8175ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT 8185ccb8f90Sopenharmony_ci bool isScreenOn {false}; 8195ccb8f90Sopenharmony_ci // When there's external screen, the original way to get screen state is inaccurate, 8205ccb8f90Sopenharmony_ci // so use PowerState instead 8215ccb8f90Sopenharmony_ci if (GetExternalScreenNumber() > 0) { 8225ccb8f90Sopenharmony_ci PowerState powerState = GetState(); 8235ccb8f90Sopenharmony_ci isScreenOn = (powerState == PowerState::AWAKE) || (powerState == PowerState::DIM); 8245ccb8f90Sopenharmony_ci } else { 8255ccb8f90Sopenharmony_ci DisplayState displayState = stateAction_->GetDisplayState(); 8265ccb8f90Sopenharmony_ci isScreenOn = (displayState == DisplayState::DISPLAY_ON) || (displayState == DisplayState::DISPLAY_DIM); 8275ccb8f90Sopenharmony_ci } 8285ccb8f90Sopenharmony_ci#else 8295ccb8f90Sopenharmony_ci DisplayState state = stateAction_->GetDisplayState(); 8305ccb8f90Sopenharmony_ci bool isScreenOn = (state == DisplayState::DISPLAY_ON) || (state == DisplayState::DISPLAY_DIM); 8315ccb8f90Sopenharmony_ci#endif 8325ccb8f90Sopenharmony_ci 8335ccb8f90Sopenharmony_ci if (needPrintLog) { 8345ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Current screen is %{public}s", isScreenOn ? "ON" : "OFF"); 8355ccb8f90Sopenharmony_ci } 8365ccb8f90Sopenharmony_ci return isScreenOn; 8375ccb8f90Sopenharmony_ci} 8385ccb8f90Sopenharmony_ci 8395ccb8f90Sopenharmony_cibool PowerStateMachine::IsFoldScreenOn() 8405ccb8f90Sopenharmony_ci{ 8415ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, 8425ccb8f90Sopenharmony_ci "IsFoldScreenOn settingOnStateFlag_ is %{public}d and settingOffStateFlag_ is %{public}d", 8435ccb8f90Sopenharmony_ci settingOnStateFlag_.load(), settingOffStateFlag_.load()); 8445ccb8f90Sopenharmony_ci 8455ccb8f90Sopenharmony_ci if (settingOnStateFlag_ == true) { 8465ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Current fold screen is going on"); 8475ccb8f90Sopenharmony_ci return true; 8485ccb8f90Sopenharmony_ci } 8495ccb8f90Sopenharmony_ci if (settingOffStateFlag_ == true) { 8505ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Current fold screen is going off"); 8515ccb8f90Sopenharmony_ci return false; 8525ccb8f90Sopenharmony_ci } 8535ccb8f90Sopenharmony_ci DisplayState state = stateAction_->GetDisplayState(); 8545ccb8f90Sopenharmony_ci if (state == DisplayState::DISPLAY_ON || state == DisplayState::DISPLAY_DIM) { 8555ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Current fold screen is on or going on"); 8565ccb8f90Sopenharmony_ci return true; 8575ccb8f90Sopenharmony_ci } 8585ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Current fold screen state is off, state: %{public}u", state); 8595ccb8f90Sopenharmony_ci return false; 8605ccb8f90Sopenharmony_ci} 8615ccb8f90Sopenharmony_ci 8625ccb8f90Sopenharmony_cibool PowerStateMachine::IsCollaborationScreenOn() 8635ccb8f90Sopenharmony_ci{ 8645ccb8f90Sopenharmony_ci return isAwakeNotified_.load(std::memory_order_relaxed); 8655ccb8f90Sopenharmony_ci} 8665ccb8f90Sopenharmony_ci 8675ccb8f90Sopenharmony_civoid PowerStateMachine::ReceiveScreenEvent(bool isScreenOn) 8685ccb8f90Sopenharmony_ci{ 8695ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Enter"); 8705ccb8f90Sopenharmony_ci std::lock_guard lock(mutex_); 8715ccb8f90Sopenharmony_ci auto prestate = mDeviceState_.screenState.state; 8725ccb8f90Sopenharmony_ci if (isScreenOn) { 8735ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOnTime = GetTickCount(); 8745ccb8f90Sopenharmony_ci } else { 8755ccb8f90Sopenharmony_ci mDeviceState_.screenState.lastOffTime = GetTickCount(); 8765ccb8f90Sopenharmony_ci } 8775ccb8f90Sopenharmony_ci if (prestate != mDeviceState_.screenState.state) { 8785ccb8f90Sopenharmony_ci NotifyPowerStateChanged(isScreenOn ? PowerState::AWAKE : PowerState::INACTIVE); 8795ccb8f90Sopenharmony_ci } 8805ccb8f90Sopenharmony_ci} 8815ccb8f90Sopenharmony_ci 8825ccb8f90Sopenharmony_civoid PowerStateMachine::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync) 8835ccb8f90Sopenharmony_ci{ 8845ccb8f90Sopenharmony_ci std::lock_guard lock(mutex_); 8855ccb8f90Sopenharmony_ci RETURN_IF(callback == nullptr); 8865ccb8f90Sopenharmony_ci auto object = callback->AsObject(); 8875ccb8f90Sopenharmony_ci RETURN_IF(object == nullptr); 8885ccb8f90Sopenharmony_ci 8895ccb8f90Sopenharmony_ci bool result = false; 8905ccb8f90Sopenharmony_ci if (isSync) { 8915ccb8f90Sopenharmony_ci auto retIt = syncPowerStateListeners_.insert(callback); 8925ccb8f90Sopenharmony_ci result = retIt.second; 8935ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "sync listeners.size = %{public}d, insertOk = %{public}d", 8945ccb8f90Sopenharmony_ci static_cast<unsigned int>(syncPowerStateListeners_.size()), retIt.second); 8955ccb8f90Sopenharmony_ci } else { 8965ccb8f90Sopenharmony_ci auto retIt = asyncPowerStateListeners_.insert(callback); 8975ccb8f90Sopenharmony_ci result = retIt.second; 8985ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "async listeners.size = %{public}d, insertOk = %{public}d", 8995ccb8f90Sopenharmony_ci static_cast<unsigned int>(asyncPowerStateListeners_.size()), retIt.second); 9005ccb8f90Sopenharmony_ci } 9015ccb8f90Sopenharmony_ci if (result) { 9025ccb8f90Sopenharmony_ci object->AddDeathRecipient(powerStateCBDeathRecipient_); 9035ccb8f90Sopenharmony_ci } 9045ccb8f90Sopenharmony_ci} 9055ccb8f90Sopenharmony_ci 9065ccb8f90Sopenharmony_civoid PowerStateMachine::UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) 9075ccb8f90Sopenharmony_ci{ 9085ccb8f90Sopenharmony_ci std::lock_guard lock(mutex_); 9095ccb8f90Sopenharmony_ci RETURN_IF(callback == nullptr); 9105ccb8f90Sopenharmony_ci auto object = callback->AsObject(); 9115ccb8f90Sopenharmony_ci RETURN_IF(object == nullptr); 9125ccb8f90Sopenharmony_ci size_t eraseNum = 0; 9135ccb8f90Sopenharmony_ci if (syncPowerStateListeners_.find(callback) != syncPowerStateListeners_.end()) { 9145ccb8f90Sopenharmony_ci eraseNum = syncPowerStateListeners_.erase(callback); 9155ccb8f90Sopenharmony_ci if (eraseNum != 0) { 9165ccb8f90Sopenharmony_ci object->RemoveDeathRecipient(powerStateCBDeathRecipient_); 9175ccb8f90Sopenharmony_ci } 9185ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "sync listeners.size = %{public}d, eraseNum = %{public}zu", 9195ccb8f90Sopenharmony_ci static_cast<unsigned int>(syncPowerStateListeners_.size()), eraseNum); 9205ccb8f90Sopenharmony_ci } else { 9215ccb8f90Sopenharmony_ci eraseNum = asyncPowerStateListeners_.erase(callback); 9225ccb8f90Sopenharmony_ci if (eraseNum != 0) { 9235ccb8f90Sopenharmony_ci object->RemoveDeathRecipient(powerStateCBDeathRecipient_); 9245ccb8f90Sopenharmony_ci } 9255ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "async listeners.size = %{public}d, eraseNum = %{public}zu", 9265ccb8f90Sopenharmony_ci static_cast<unsigned int>(asyncPowerStateListeners_.size()), eraseNum); 9275ccb8f90Sopenharmony_ci } 9285ccb8f90Sopenharmony_ci} 9295ccb8f90Sopenharmony_ci 9305ccb8f90Sopenharmony_civoid PowerStateMachine::EnableMock(IDeviceStateAction* mockAction) 9315ccb8f90Sopenharmony_ci{ 9325ccb8f90Sopenharmony_ci std::lock_guard lock(mutex_); 9335ccb8f90Sopenharmony_ci displayOffTime_ = DEFAULT_DISPLAY_OFF_TIME_MS; 9345ccb8f90Sopenharmony_ci sleepTime_ = DEFAULT_SLEEP_TIME_MS; 9355ccb8f90Sopenharmony_ci ResetInactiveTimer(); 9365ccb8f90Sopenharmony_ci 9375ccb8f90Sopenharmony_ci std::unique_ptr<IDeviceStateAction> mock(mockAction); 9385ccb8f90Sopenharmony_ci if (stateAction_ != nullptr) { 9395ccb8f90Sopenharmony_ci stateAction_.reset(); 9405ccb8f90Sopenharmony_ci } 9415ccb8f90Sopenharmony_ci stateAction_ = std::move(mock); 9425ccb8f90Sopenharmony_ci} 9435ccb8f90Sopenharmony_ci 9445ccb8f90Sopenharmony_civoid PowerStateMachine::NotifyPowerStateChanged(PowerState state, StateChangeReason reason) 9455ccb8f90Sopenharmony_ci{ 9465ccb8f90Sopenharmony_ci if (GetState() == PowerState::INACTIVE && 9475ccb8f90Sopenharmony_ci !enabledScreenOffEvent_.load(std::memory_order_relaxed) && 9485ccb8f90Sopenharmony_ci reason == StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK) { 9495ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER] not notify inactive power state"); 9505ccb8f90Sopenharmony_ci return; 9515ccb8f90Sopenharmony_ci } 9525ccb8f90Sopenharmony_ci POWER_HILOGD( 9535ccb8f90Sopenharmony_ci FEATURE_POWER_STATE, "state=%{public}u, listeners.size=%{public}zu", state, syncPowerStateListeners_.size()); 9545ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 9555ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "STATE", HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", 9565ccb8f90Sopenharmony_ci static_cast<uint32_t>(state)); 9575ccb8f90Sopenharmony_ci#endif 9585ccb8f90Sopenharmony_ci std::lock_guard lock(mutex_); 9595ccb8f90Sopenharmony_ci int64_t now = GetTickCount(); 9605ccb8f90Sopenharmony_ci // Send Notification event 9615ccb8f90Sopenharmony_ci SendEventToPowerMgrNotify(state, now); 9625ccb8f90Sopenharmony_ci 9635ccb8f90Sopenharmony_ci // Call back all native function 9645ccb8f90Sopenharmony_ci for (auto& listener : asyncPowerStateListeners_) { 9655ccb8f90Sopenharmony_ci listener->OnAsyncPowerStateChanged(state); 9665ccb8f90Sopenharmony_ci } 9675ccb8f90Sopenharmony_ci for (auto& listener : syncPowerStateListeners_) { 9685ccb8f90Sopenharmony_ci listener->OnPowerStateChanged(state); 9695ccb8f90Sopenharmony_ci } 9705ccb8f90Sopenharmony_ci} 9715ccb8f90Sopenharmony_ci 9725ccb8f90Sopenharmony_civoid PowerStateMachine::SendEventToPowerMgrNotify(PowerState state, int64_t callTime) 9735ccb8f90Sopenharmony_ci{ 9745ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 9755ccb8f90Sopenharmony_ci if (pms == nullptr) { 9765ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Pms is nullptr"); 9775ccb8f90Sopenharmony_ci return; 9785ccb8f90Sopenharmony_ci } 9795ccb8f90Sopenharmony_ci auto notify = pms->GetPowerMgrNotify(); 9805ccb8f90Sopenharmony_ci if (notify == nullptr) { 9815ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Notify is null"); 9825ccb8f90Sopenharmony_ci return; 9835ccb8f90Sopenharmony_ci } 9845ccb8f90Sopenharmony_ci 9855ccb8f90Sopenharmony_ci switch (state) { 9865ccb8f90Sopenharmony_ci case PowerState::AWAKE: { 9875ccb8f90Sopenharmony_ci notify->PublishScreenOnEvents(callTime); 9885ccb8f90Sopenharmony_ci isAwakeNotified_.store(true, std::memory_order_relaxed); 9895ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST 9905ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 9915ccb8f90Sopenharmony_ci if (suspendController != nullptr && suspendController->GetForceSleepingFlag()) { 9925ccb8f90Sopenharmony_ci notify->PublishExitForceSleepEvents(callTime); 9935ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Set flag of force sleeping to false"); 9945ccb8f90Sopenharmony_ci suspendController->SetForceSleepingFlag(false); 9955ccb8f90Sopenharmony_ci } 9965ccb8f90Sopenharmony_ci#endif 9975ccb8f90Sopenharmony_ci break; 9985ccb8f90Sopenharmony_ci } 9995ccb8f90Sopenharmony_ci case PowerState::INACTIVE: { 10005ccb8f90Sopenharmony_ci notify->PublishScreenOffEvents(callTime); 10015ccb8f90Sopenharmony_ci isAwakeNotified_.store(false, std::memory_order_relaxed); 10025ccb8f90Sopenharmony_ci break; 10035ccb8f90Sopenharmony_ci } 10045ccb8f90Sopenharmony_ci case PowerState::SLEEP: { 10055ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST 10065ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 10075ccb8f90Sopenharmony_ci if (suspendController != nullptr && suspendController->GetForceSleepingFlag()) { 10085ccb8f90Sopenharmony_ci notify->PublishEnterForceSleepEvents(callTime); 10095ccb8f90Sopenharmony_ci } 10105ccb8f90Sopenharmony_ci break; 10115ccb8f90Sopenharmony_ci#endif 10125ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "No need to publish EnterForceSleepEvent, state:%{public}u", state); 10135ccb8f90Sopenharmony_ci break; 10145ccb8f90Sopenharmony_ci } 10155ccb8f90Sopenharmony_ci default: 10165ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "No need to publish event, state:%{public}u", state); 10175ccb8f90Sopenharmony_ci } 10185ccb8f90Sopenharmony_ci} 10195ccb8f90Sopenharmony_ci 10205ccb8f90Sopenharmony_civoid PowerStateMachine::PowerStateCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote) 10215ccb8f90Sopenharmony_ci{ 10225ccb8f90Sopenharmony_ci if (remote == nullptr || remote.promote() == nullptr) { 10235ccb8f90Sopenharmony_ci return; 10245ccb8f90Sopenharmony_ci } 10255ccb8f90Sopenharmony_ci sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(remote.promote()); 10265ccb8f90Sopenharmony_ci FFRTTask unRegFunc = [callback] { 10275ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 10285ccb8f90Sopenharmony_ci if (pms == nullptr) { 10295ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Pms is nullptr"); 10305ccb8f90Sopenharmony_ci return; 10315ccb8f90Sopenharmony_ci } 10325ccb8f90Sopenharmony_ci pms->UnRegisterPowerStateCallback(callback); 10335ccb8f90Sopenharmony_ci }; 10345ccb8f90Sopenharmony_ci FFRTUtils::SubmitTask(unRegFunc); 10355ccb8f90Sopenharmony_ci} 10365ccb8f90Sopenharmony_ci 10375ccb8f90Sopenharmony_civoid PowerStateMachine::SetDelayTimer(int64_t delayTime, int32_t event) 10385ccb8f90Sopenharmony_ci{ 10395ccb8f90Sopenharmony_ci if (!ffrtTimer_) { 10405ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_ACTIVITY, "Failed to set delay timer, the timer pointer is null"); 10415ccb8f90Sopenharmony_ci return; 10425ccb8f90Sopenharmony_ci } 10435ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Set delay timer, delayTime=%{public}s, event=%{public}d", 10445ccb8f90Sopenharmony_ci std::to_string(delayTime).c_str(), event); 10455ccb8f90Sopenharmony_ci 10465ccb8f90Sopenharmony_ci switch (event) { 10475ccb8f90Sopenharmony_ci case CHECK_USER_ACTIVITY_TIMEOUT_MSG: { 10485ccb8f90Sopenharmony_ci FFRTTask task = [this] { this->HandleActivityTimeout(); }; 10495ccb8f90Sopenharmony_ci ffrtTimer_->SetTimer(TIMER_ID_USER_ACTIVITY_TIMEOUT, task, delayTime); 10505ccb8f90Sopenharmony_ci break; 10515ccb8f90Sopenharmony_ci } 10525ccb8f90Sopenharmony_ci case CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG: { 10535ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 10545ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 10555ccb8f90Sopenharmony_ci if (suspendController == nullptr) { 10565ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_ACTIVITY, "suspendController is nullptr"); 10575ccb8f90Sopenharmony_ci return; 10585ccb8f90Sopenharmony_ci } 10595ccb8f90Sopenharmony_ci suspendController->HandleEvent(delayTime); 10605ccb8f90Sopenharmony_ci break; 10615ccb8f90Sopenharmony_ci } 10625ccb8f90Sopenharmony_ci case CHECK_PROXIMITY_SCREEN_OFF_MSG: { 10635ccb8f90Sopenharmony_ci FFRTTask delayScreenOffTask = [this] { 10645ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "proximity-screen-off timer task is triggered"); 10655ccb8f90Sopenharmony_ci proximityScreenOffTimerStarted_.store(false, std::memory_order_relaxed); 10665ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 10675ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 10685ccb8f90Sopenharmony_ci if (suspendController == nullptr) { 10695ccb8f90Sopenharmony_ci POWER_HILOGW( 10705ccb8f90Sopenharmony_ci FEATURE_POWER_STATE, "suspendController is nullptr, exit proximity-screen-off timer task"); 10715ccb8f90Sopenharmony_ci return; 10725ccb8f90Sopenharmony_ci } 10735ccb8f90Sopenharmony_ci bool ret = SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_PROXIMITY, true); 10745ccb8f90Sopenharmony_ci if (ret) { 10755ccb8f90Sopenharmony_ci suspendController->StartSleepTimer(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, 10765ccb8f90Sopenharmony_ci static_cast<uint32_t>(SuspendAction::ACTION_AUTO_SUSPEND), 0); 10775ccb8f90Sopenharmony_ci } 10785ccb8f90Sopenharmony_ci }; 10795ccb8f90Sopenharmony_ci ffrtTimer_->SetTimer(TIMER_ID_PROXIMITY_SCREEN_OFF, delayScreenOffTask, delayTime); 10805ccb8f90Sopenharmony_ci proximityScreenOffTimerStarted_.store(true, std::memory_order_relaxed); 10815ccb8f90Sopenharmony_ci break; 10825ccb8f90Sopenharmony_ci } 10835ccb8f90Sopenharmony_ci default: { 10845ccb8f90Sopenharmony_ci break; 10855ccb8f90Sopenharmony_ci } 10865ccb8f90Sopenharmony_ci } 10875ccb8f90Sopenharmony_ci} 10885ccb8f90Sopenharmony_ci 10895ccb8f90Sopenharmony_civoid PowerStateMachine::CancelDelayTimer(int32_t event) 10905ccb8f90Sopenharmony_ci{ 10915ccb8f90Sopenharmony_ci if (!ffrtTimer_) { 10925ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_ACTIVITY, "Failed to cancel delay timer, the timer pointer is null"); 10935ccb8f90Sopenharmony_ci return; 10945ccb8f90Sopenharmony_ci } 10955ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Cancel delay timer, event: %{public}d", event); 10965ccb8f90Sopenharmony_ci 10975ccb8f90Sopenharmony_ci switch (event) { 10985ccb8f90Sopenharmony_ci case CHECK_USER_ACTIVITY_TIMEOUT_MSG: { 10995ccb8f90Sopenharmony_ci ffrtTimer_->CancelTimer(TIMER_ID_USER_ACTIVITY_TIMEOUT); 11005ccb8f90Sopenharmony_ci break; 11015ccb8f90Sopenharmony_ci } 11025ccb8f90Sopenharmony_ci case CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG: { 11035ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 11045ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 11055ccb8f90Sopenharmony_ci if (suspendController == nullptr) { 11065ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_ACTIVITY, "suspendController is nullptr"); 11075ccb8f90Sopenharmony_ci return; 11085ccb8f90Sopenharmony_ci } 11095ccb8f90Sopenharmony_ci suspendController->CancelEvent(); 11105ccb8f90Sopenharmony_ci break; 11115ccb8f90Sopenharmony_ci } 11125ccb8f90Sopenharmony_ci case CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG: { 11135ccb8f90Sopenharmony_ci ffrtTimer_->CancelTimer(TIMER_ID_PRE_BRIGHT_AUTH); 11145ccb8f90Sopenharmony_ci break; 11155ccb8f90Sopenharmony_ci } 11165ccb8f90Sopenharmony_ci case CHECK_PROXIMITY_SCREEN_OFF_MSG: { 11175ccb8f90Sopenharmony_ci ffrtTimer_->CancelTimer(TIMER_ID_PROXIMITY_SCREEN_OFF); 11185ccb8f90Sopenharmony_ci proximityScreenOffTimerStarted_.store(false, std::memory_order_relaxed); 11195ccb8f90Sopenharmony_ci break; 11205ccb8f90Sopenharmony_ci } 11215ccb8f90Sopenharmony_ci default: { 11225ccb8f90Sopenharmony_ci break; 11235ccb8f90Sopenharmony_ci } 11245ccb8f90Sopenharmony_ci } 11255ccb8f90Sopenharmony_ci} 11265ccb8f90Sopenharmony_ci 11275ccb8f90Sopenharmony_civoid PowerStateMachine::ResetInactiveTimer(bool needPrintLog) 11285ccb8f90Sopenharmony_ci{ 11295ccb8f90Sopenharmony_ci // change the flag to notify the thread which is setting DIM 11305ccb8f90Sopenharmony_ci int64_t expectedFlag = static_cast<int64_t>(PowerState::DIM); 11315ccb8f90Sopenharmony_ci settingStateFlag_.compare_exchange_strong( 11325ccb8f90Sopenharmony_ci expectedFlag, static_cast<int64_t>(SettingStateFlag::StateFlag::SETTING_DIM_INTERRUPTED)); 11335ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_TIMEOUT_MSG); 11345ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); 11355ccb8f90Sopenharmony_ci if (this->GetDisplayOffTime() < 0) { 11365ccb8f90Sopenharmony_ci if (needPrintLog) { 11375ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_ACTIVITY, "Auto display off is disabled"); 11385ccb8f90Sopenharmony_ci } 11395ccb8f90Sopenharmony_ci return; 11405ccb8f90Sopenharmony_ci } 11415ccb8f90Sopenharmony_ci 11425ccb8f90Sopenharmony_ci int64_t displayOffTime = this->GetDisplayOffTime(); 11435ccb8f90Sopenharmony_ci ResetScreenOffPreTimeForSwing(displayOffTime); 11445ccb8f90Sopenharmony_ci this->SetDelayTimer( 11455ccb8f90Sopenharmony_ci displayOffTime - this->GetDimTime(displayOffTime), PowerStateMachine::CHECK_USER_ACTIVITY_TIMEOUT_MSG); 11465ccb8f90Sopenharmony_ci if (needPrintLog) { 11475ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_ACTIVITY, "reset inactive timer: %{public}" PRId64, displayOffTime); 11485ccb8f90Sopenharmony_ci } 11495ccb8f90Sopenharmony_ci} 11505ccb8f90Sopenharmony_ci 11515ccb8f90Sopenharmony_civoid PowerStateMachine::ResetScreenOffPreTimeForSwing(int64_t displayOffTime) 11525ccb8f90Sopenharmony_ci{ 11535ccb8f90Sopenharmony_ci int64_t now = GetTickCount(); 11545ccb8f90Sopenharmony_ci int64_t nextTimeOut = now + displayOffTime - this->GetDimTime(displayOffTime); 11555ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SCREEN_OFF_PRE, 11565ccb8f90Sopenharmony_ci "now=%{public}lld,displayOffTime=%{public}lld,nextTimeOut=%{public}lld", 11575ccb8f90Sopenharmony_ci static_cast<long long>(now), static_cast<long long>(displayOffTime), static_cast<long long>(nextTimeOut)); 11585ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 11595ccb8f90Sopenharmony_ci auto screenOffPreController = pms->GetScreenOffPreController(); 11605ccb8f90Sopenharmony_ci if (screenOffPreController != nullptr && screenOffPreController->IsRegistered()) { 11615ccb8f90Sopenharmony_ci screenOffPreController->SchedulEyeDetectTimeout(nextTimeOut, now); 11625ccb8f90Sopenharmony_ci } 11635ccb8f90Sopenharmony_ci} 11645ccb8f90Sopenharmony_ci 11655ccb8f90Sopenharmony_civoid PowerStateMachine::ResetSleepTimer() 11665ccb8f90Sopenharmony_ci{ 11675ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_TIMEOUT_MSG); 11685ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); 11695ccb8f90Sopenharmony_ci if (this->GetSleepTime() < 0) { 11705ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Auto sleep is disabled"); 11715ccb8f90Sopenharmony_ci return; 11725ccb8f90Sopenharmony_ci } 11735ccb8f90Sopenharmony_ci} 11745ccb8f90Sopenharmony_ci 11755ccb8f90Sopenharmony_civoid PowerStateMachine::SetAutoSuspend(SuspendDeviceType type, uint32_t delay) 11765ccb8f90Sopenharmony_ci{ 11775ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 11785ccb8f90Sopenharmony_ci if (pms == nullptr) { 11795ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "Pms is nullptr"); 11805ccb8f90Sopenharmony_ci return; 11815ccb8f90Sopenharmony_ci } 11825ccb8f90Sopenharmony_ci auto suspendController = pms->GetSuspendController(); 11835ccb8f90Sopenharmony_ci if (suspendController == nullptr) { 11845ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_SUSPEND, "Suspend controller is nullptr"); 11855ccb8f90Sopenharmony_ci return; 11865ccb8f90Sopenharmony_ci } 11875ccb8f90Sopenharmony_ci suspendController->StartSleepTimer(type, static_cast<uint32_t>(SuspendAction::ACTION_AUTO_SUSPEND), delay); 11885ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SUSPEND, "Set auto suspend finish"); 11895ccb8f90Sopenharmony_ci} 11905ccb8f90Sopenharmony_ci 11915ccb8f90Sopenharmony_civoid PowerStateMachine::ShowCurrentScreenLocks() 11925ccb8f90Sopenharmony_ci{ 11935ccb8f90Sopenharmony_ci auto pms = pms_.promote(); 11945ccb8f90Sopenharmony_ci if (pms == nullptr) { 11955ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Pms is nullptr"); 11965ccb8f90Sopenharmony_ci return; 11975ccb8f90Sopenharmony_ci } 11985ccb8f90Sopenharmony_ci std::map<std::string, RunningLockInfo> screenOnLockLists; 11995ccb8f90Sopenharmony_ci pms_->QueryRunningLockListsInner(screenOnLockLists); 12005ccb8f90Sopenharmony_ci std::string message; 12015ccb8f90Sopenharmony_ci uint32_t mapSize = screenOnLockLists.size(); 12025ccb8f90Sopenharmony_ci uint32_t counter = 0; 12035ccb8f90Sopenharmony_ci for (auto it : screenOnLockLists) { 12045ccb8f90Sopenharmony_ci counter++; 12055ccb8f90Sopenharmony_ci message.append(std::to_string(counter)).append(". ") 12065ccb8f90Sopenharmony_ci .append("bundleName=").append(it.second.bundleName) 12075ccb8f90Sopenharmony_ci .append(" name=").append(it.second.name) 12085ccb8f90Sopenharmony_ci .append(" pid=").append(std::to_string(it.second.pid)) 12095ccb8f90Sopenharmony_ci .append(". "); 12105ccb8f90Sopenharmony_ci } 12115ccb8f90Sopenharmony_ci if (counter == 0) { 12125ccb8f90Sopenharmony_ci return; 12135ccb8f90Sopenharmony_ci } 12145ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_RUNNING_LOCK, "%{public}d screen on locks as follows: %{public}s", mapSize, message.c_str()); 12155ccb8f90Sopenharmony_ci} 12165ccb8f90Sopenharmony_ci 12175ccb8f90Sopenharmony_ci#ifdef HAS_SENSORS_SENSOR_PART 12185ccb8f90Sopenharmony_cibool PowerStateMachine::IsProximityClose() 12195ccb8f90Sopenharmony_ci{ 12205ccb8f90Sopenharmony_ci auto pms = pms_.promote(); 12215ccb8f90Sopenharmony_ci if (pms == nullptr) { 12225ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Pms is nullptr"); 12235ccb8f90Sopenharmony_ci return false; 12245ccb8f90Sopenharmony_ci } 12255ccb8f90Sopenharmony_ci auto runningLockMgr = pms->GetRunningLockMgr(); 12265ccb8f90Sopenharmony_ci if (runningLockMgr == nullptr) { 12275ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "RunningLockMgr is nullptr"); 12285ccb8f90Sopenharmony_ci return false; 12295ccb8f90Sopenharmony_ci } 12305ccb8f90Sopenharmony_ci return runningLockMgr->IsProximityClose(); 12315ccb8f90Sopenharmony_ci} 12325ccb8f90Sopenharmony_ci#endif 12335ccb8f90Sopenharmony_ci 12345ccb8f90Sopenharmony_civoid PowerStateMachine::HandleActivityTimeout() 12355ccb8f90Sopenharmony_ci{ 12365ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Enter, displayState = %{public}d", stateAction_->GetDisplayState()); 12375ccb8f90Sopenharmony_ci SetState(PowerState::DIM, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT); 12385ccb8f90Sopenharmony_ci} 12395ccb8f90Sopenharmony_ci 12405ccb8f90Sopenharmony_civoid PowerStateMachine::HandleActivitySleepTimeout() 12415ccb8f90Sopenharmony_ci{ 12425ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_ACTIVITY, "Enter, displayState = %{public}d", stateAction_->GetDisplayState()); 12435ccb8f90Sopenharmony_ci if (!this->CheckRunningLock(PowerState::SLEEP)) { 12445ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_STATE, "RunningLock is blocking to transit to SLEEP"); 12455ccb8f90Sopenharmony_ci return; 12465ccb8f90Sopenharmony_ci } 12475ccb8f90Sopenharmony_ci DisplayState dispState = stateAction_->GetDisplayState(); 12485ccb8f90Sopenharmony_ci if (dispState == DisplayState::DISPLAY_OFF) { 12495ccb8f90Sopenharmony_ci SetState(PowerState::SLEEP, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT); 12505ccb8f90Sopenharmony_ci } else { 12515ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_ACTIVITY, "Display is on, ignore activity sleep timeout, state = %{public}d", dispState); 12525ccb8f90Sopenharmony_ci } 12535ccb8f90Sopenharmony_ci} 12545ccb8f90Sopenharmony_ci 12555ccb8f90Sopenharmony_civoid PowerStateMachine::HandleSystemWakeup() 12565ccb8f90Sopenharmony_ci{ 12575ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "Enter, displayState = %{public}d", stateAction_->GetDisplayState()); 12585ccb8f90Sopenharmony_ci if (IsScreenOn()) { 12595ccb8f90Sopenharmony_ci SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true); 12605ccb8f90Sopenharmony_ci } else { 12615ccb8f90Sopenharmony_ci SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true); 12625ccb8f90Sopenharmony_ci } 12635ccb8f90Sopenharmony_ci} 12645ccb8f90Sopenharmony_ci 12655ccb8f90Sopenharmony_civoid PowerStateMachine::SetForceTimingOut(bool enabled) 12665ccb8f90Sopenharmony_ci{ 12675ccb8f90Sopenharmony_ci bool isScreenOnLockActive = IsRunningLockEnabled(RunningLockType::RUNNINGLOCK_SCREEN); 12685ccb8f90Sopenharmony_ci bool currentValue = forceTimingOut_.exchange(enabled); 12695ccb8f90Sopenharmony_ci pid_t pid = IPCSkeleton::GetCallingPid(); 12705ccb8f90Sopenharmony_ci auto uid = IPCSkeleton::GetCallingUid(); 12715ccb8f90Sopenharmony_ci PowerState curState = GetState(); 12725ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_RUNNING_LOCK, 12735ccb8f90Sopenharmony_ci "SetForceTimingOut: %{public}s -> %{public}s, screenOnLockActive=%{public}s, PowerState=%{public}u, " 12745ccb8f90Sopenharmony_ci "PID=%{public}d, UID=%{public}d", 12755ccb8f90Sopenharmony_ci currentValue ? "TRUE" : "FALSE", enabled ? "TRUE" : "FALSE", isScreenOnLockActive ? "TRUE" : "FALSE", curState, 12765ccb8f90Sopenharmony_ci pid, uid); 12775ccb8f90Sopenharmony_ci if (currentValue == enabled || !isScreenOnLockActive || IsSettingState(PowerState::DIM)) { 12785ccb8f90Sopenharmony_ci // no need to interact with screen state or timer 12795ccb8f90Sopenharmony_ci return; 12805ccb8f90Sopenharmony_ci } 12815ccb8f90Sopenharmony_ci if (enabled) { 12825ccb8f90Sopenharmony_ci // In case the PowerState is AWAKE, we need to reset the timer since there is no existing one. 12835ccb8f90Sopenharmony_ci // Only reset the timer if no SetState operation is currently in progress, and if any, 12845ccb8f90Sopenharmony_ci // make sure this ResetInactiveTimer operation does not interfere with it. 12855ccb8f90Sopenharmony_ci // Because the goal here is to ensure that there exist some Timer, regardless who sets the timer. 12865ccb8f90Sopenharmony_ci // I call it "weak" ResetInactiveTimer to distinguish it from the "strong" one invoked by RefreshActivity, 12875ccb8f90Sopenharmony_ci // which (should) invalidates any time-out timer previously set. 12885ccb8f90Sopenharmony_ci if (stateMutex_.try_lock()) { 12895ccb8f90Sopenharmony_ci if (GetState() == PowerState::AWAKE) { 12905ccb8f90Sopenharmony_ci ResetInactiveTimer(); 12915ccb8f90Sopenharmony_ci } 12925ccb8f90Sopenharmony_ci stateMutex_.unlock(); 12935ccb8f90Sopenharmony_ci } 12945ccb8f90Sopenharmony_ci } else { 12955ccb8f90Sopenharmony_ci // SetForceTimingOut from TRUE to FALSE, with screen-on-lock active. 12965ccb8f90Sopenharmony_ci // Need to exit DIM and/or reset(cancel) timer 12975ccb8f90Sopenharmony_ci SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_REFRESH); 12985ccb8f90Sopenharmony_ci } 12995ccb8f90Sopenharmony_ci} 13005ccb8f90Sopenharmony_ci 13015ccb8f90Sopenharmony_civoid PowerStateMachine::LockScreenAfterTimingOut(bool enabled, bool checkScreenOnLock, bool sendScreenOffEvent) 13025ccb8f90Sopenharmony_ci{ 13035ccb8f90Sopenharmony_ci pid_t pid = IPCSkeleton::GetCallingPid(); 13045ccb8f90Sopenharmony_ci auto uid = IPCSkeleton::GetCallingUid(); 13055ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_RUNNING_LOCK, 13065ccb8f90Sopenharmony_ci "LockScreenAfterTimingOut: %{public}u, %{public}u, %{public}u, PID=%{public}d, UID=%{public}d", 13075ccb8f90Sopenharmony_ci static_cast<uint32_t>(enabled), static_cast<uint32_t>(checkScreenOnLock), 13085ccb8f90Sopenharmony_ci static_cast<uint32_t>(sendScreenOffEvent), pid, uid); 13095ccb8f90Sopenharmony_ci enabledTimingOutLockScreen_.store(enabled, std::memory_order_relaxed); 13105ccb8f90Sopenharmony_ci enabledTimingOutLockScreenCheckLock_.store(checkScreenOnLock, std::memory_order_relaxed); 13115ccb8f90Sopenharmony_ci enabledScreenOffEvent_.store(sendScreenOffEvent, std::memory_order_relaxed); 13125ccb8f90Sopenharmony_ci} 13135ccb8f90Sopenharmony_ci 13145ccb8f90Sopenharmony_cibool PowerStateMachine::CheckRunningLock(PowerState state) 13155ccb8f90Sopenharmony_ci{ 13165ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "Enter, state = %{public}u", state); 13175ccb8f90Sopenharmony_ci auto pms = pms_.promote(); 13185ccb8f90Sopenharmony_ci if (pms == nullptr) { 13195ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Pms is nullptr"); 13205ccb8f90Sopenharmony_ci return false; 13215ccb8f90Sopenharmony_ci } 13225ccb8f90Sopenharmony_ci auto runningLockMgr = pms->GetRunningLockMgr(); 13235ccb8f90Sopenharmony_ci if (runningLockMgr == nullptr) { 13245ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "RunningLockMgr is nullptr"); 13255ccb8f90Sopenharmony_ci return false; 13265ccb8f90Sopenharmony_ci } 13275ccb8f90Sopenharmony_ci if (state == PowerState::DIM) { 13285ccb8f90Sopenharmony_ci // screen on lock need to block DIM state as well 13295ccb8f90Sopenharmony_ci state = PowerState::INACTIVE; 13305ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_RUNNING_LOCK, "check Screen on Lock for DIM state"); 13315ccb8f90Sopenharmony_ci } 13325ccb8f90Sopenharmony_ci auto iterator = lockMap_.find(state); 13335ccb8f90Sopenharmony_ci if (iterator == lockMap_.end()) { 13345ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_RUNNING_LOCK, "No specific lock in lockMap_ for state: %{public}u", state); 13355ccb8f90Sopenharmony_ci return true; 13365ccb8f90Sopenharmony_ci } 13375ccb8f90Sopenharmony_ci 13385ccb8f90Sopenharmony_ci std::shared_ptr<std::vector<RunningLockType>> pLock = iterator->second; 13395ccb8f90Sopenharmony_ci for (std::vector<RunningLockType>::const_iterator iter = pLock->begin(); iter != pLock->end(); ++iter) { 13405ccb8f90Sopenharmony_ci uint32_t count = runningLockMgr->GetValidRunningLockNum(*iter); 13415ccb8f90Sopenharmony_ci if (count > 0) { 13425ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, 13435ccb8f90Sopenharmony_ci "RunningLock %{public}s is locking (count=%{public}d), blocking %{public}s", 13445ccb8f90Sopenharmony_ci PowerUtils::GetRunningLockTypeString(*iter).c_str(), count, 13455ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(state).c_str()); 13465ccb8f90Sopenharmony_ci return false; 13475ccb8f90Sopenharmony_ci } 13485ccb8f90Sopenharmony_ci } 13495ccb8f90Sopenharmony_ci 13505ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "No specific lock for state: %{public}u", state); 13515ccb8f90Sopenharmony_ci return true; 13525ccb8f90Sopenharmony_ci} 13535ccb8f90Sopenharmony_ci 13545ccb8f90Sopenharmony_cibool PowerStateMachine::IsRunningLockEnabled(RunningLockType type) 13555ccb8f90Sopenharmony_ci{ 13565ccb8f90Sopenharmony_ci auto pms = pms_.promote(); 13575ccb8f90Sopenharmony_ci if (pms == nullptr) { 13585ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "Pms is nullptr"); 13595ccb8f90Sopenharmony_ci return false; 13605ccb8f90Sopenharmony_ci } 13615ccb8f90Sopenharmony_ci auto runningLockMgr = pms->GetRunningLockMgr(); 13625ccb8f90Sopenharmony_ci if (runningLockMgr == nullptr) { 13635ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "RunningLockMgr is nullptr"); 13645ccb8f90Sopenharmony_ci return false; 13655ccb8f90Sopenharmony_ci } 13665ccb8f90Sopenharmony_ci if (runningLockMgr->GetValidRunningLockNum(type) > 0) { 13675ccb8f90Sopenharmony_ci return true; 13685ccb8f90Sopenharmony_ci } 13695ccb8f90Sopenharmony_ci return false; 13705ccb8f90Sopenharmony_ci} 13715ccb8f90Sopenharmony_ci 13725ccb8f90Sopenharmony_civoid PowerStateMachine::SetDisplayOffTime(int64_t time, bool needUpdateSetting) 13735ccb8f90Sopenharmony_ci{ 13745ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "set display off time %{public}" PRId64 " -> %{public}" PRId64 "", 13755ccb8f90Sopenharmony_ci displayOffTime_.load(), time); 13765ccb8f90Sopenharmony_ci displayOffTime_ = time; 13775ccb8f90Sopenharmony_ci if (currentState_ == PowerState::AWAKE) { 13785ccb8f90Sopenharmony_ci if (isScreenOffTimeOverride_ == true) { 13795ccb8f90Sopenharmony_ci ResetInactiveTimer(false); 13805ccb8f90Sopenharmony_ci } else { 13815ccb8f90Sopenharmony_ci ResetInactiveTimer(); 13825ccb8f90Sopenharmony_ci } 13835ccb8f90Sopenharmony_ci } 13845ccb8f90Sopenharmony_ci if (needUpdateSetting) { 13855ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING 13865ccb8f90Sopenharmony_ci SettingHelper::SetSettingDisplayAcScreenOffTime(displayOffTime_); 13875ccb8f90Sopenharmony_ci SettingHelper::SetSettingDisplayDcScreenOffTime(displayOffTime_); 13885ccb8f90Sopenharmony_ci#else 13895ccb8f90Sopenharmony_ci SettingHelper::SetSettingDisplayOffTime(displayOffTime_); 13905ccb8f90Sopenharmony_ci#endif 13915ccb8f90Sopenharmony_ci } 13925ccb8f90Sopenharmony_ci} 13935ccb8f90Sopenharmony_ci 13945ccb8f90Sopenharmony_civoid PowerStateMachine::DisplayOffTimeUpdateFunc() 13955ccb8f90Sopenharmony_ci{ 13965ccb8f90Sopenharmony_ci auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 13975ccb8f90Sopenharmony_ci if (pms == nullptr) { 13985ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "get PowerMgrService fail"); 13995ccb8f90Sopenharmony_ci return; 14005ccb8f90Sopenharmony_ci } 14015ccb8f90Sopenharmony_ci auto stateMachine = pms->GetPowerStateMachine(); 14025ccb8f90Sopenharmony_ci if (stateMachine == nullptr) { 14035ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "get PowerStateMachine fail"); 14045ccb8f90Sopenharmony_ci return; 14055ccb8f90Sopenharmony_ci } 14065ccb8f90Sopenharmony_ci 14075ccb8f90Sopenharmony_ci int64_t systemTime = stateMachine->GetDisplayOffTime(); 14085ccb8f90Sopenharmony_ci int64_t settingTime = pms->GetSettingDisplayOffTime(systemTime); 14095ccb8f90Sopenharmony_ci if (settingTime == systemTime) { 14105ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "setting display off time %{public}" PRId64 " already worked", settingTime); 14115ccb8f90Sopenharmony_ci return; 14125ccb8f90Sopenharmony_ci } 14135ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "setting update display off time %{public}" PRId64 " -> %{public}" PRId64 "", 14145ccb8f90Sopenharmony_ci systemTime, settingTime); 14155ccb8f90Sopenharmony_ci g_beforeOverrideTime = settingTime; 14165ccb8f90Sopenharmony_ci auto policy = DelayedSingleton<PowerModePolicy>::GetInstance(); 14175ccb8f90Sopenharmony_ci if (policy == nullptr) { 14185ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "get PowerModePolicy fail"); 14195ccb8f90Sopenharmony_ci return; 14205ccb8f90Sopenharmony_ci } 14215ccb8f90Sopenharmony_ci policy->RemoveBackupMapSettingSwitch(PowerModePolicy::ServiceType::DISPLAY_OFFTIME); 14225ccb8f90Sopenharmony_ci stateMachine->SetDisplayOffTime(settingTime, false); 14235ccb8f90Sopenharmony_ci} 14245ccb8f90Sopenharmony_ci 14255ccb8f90Sopenharmony_civoid PowerStateMachine::RegisterDisplayOffTimeObserver() 14265ccb8f90Sopenharmony_ci{ 14275ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING 14285ccb8f90Sopenharmony_ci if (g_displayOffTimeAcObserver && g_displayOffTimeDcObserver) { 14295ccb8f90Sopenharmony_ci#else 14305ccb8f90Sopenharmony_ci if (g_displayOffTimeObserver) { 14315ccb8f90Sopenharmony_ci#endif 14325ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "setting display off time observer is already registered"); 14335ccb8f90Sopenharmony_ci return; 14345ccb8f90Sopenharmony_ci } 14355ccb8f90Sopenharmony_ci DisplayOffTimeUpdateFunc(); 14365ccb8f90Sopenharmony_ci SettingObserver::UpdateFunc updateFunc = [&](const std::string&) { 14375ccb8f90Sopenharmony_ci PowerStateMachine::DisplayOffTimeUpdateFunc(); 14385ccb8f90Sopenharmony_ci }; 14395ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING 14405ccb8f90Sopenharmony_ci if (g_displayOffTimeAcObserver == nullptr) { 14415ccb8f90Sopenharmony_ci g_displayOffTimeAcObserver = SettingHelper::RegisterSettingDisplayAcScreenOffTimeObserver(updateFunc); 14425ccb8f90Sopenharmony_ci } 14435ccb8f90Sopenharmony_ci if (g_displayOffTimeDcObserver == nullptr) { 14445ccb8f90Sopenharmony_ci g_displayOffTimeDcObserver = SettingHelper::RegisterSettingDisplayDcScreenOffTimeObserver(updateFunc); 14455ccb8f90Sopenharmony_ci } 14465ccb8f90Sopenharmony_ci#else 14475ccb8f90Sopenharmony_ci g_displayOffTimeObserver = SettingHelper::RegisterSettingDisplayOffTimeObserver(updateFunc); 14485ccb8f90Sopenharmony_ci#endif 14495ccb8f90Sopenharmony_ci} 14505ccb8f90Sopenharmony_ci 14515ccb8f90Sopenharmony_civoid PowerStateMachine::UnregisterDisplayOffTimeObserver() 14525ccb8f90Sopenharmony_ci{ 14535ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING 14545ccb8f90Sopenharmony_ci if (g_displayOffTimeAcObserver) { 14555ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_displayOffTimeAcObserver); 14565ccb8f90Sopenharmony_ci g_displayOffTimeAcObserver = nullptr; 14575ccb8f90Sopenharmony_ci } 14585ccb8f90Sopenharmony_ci if (g_displayOffTimeDcObserver) { 14595ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_displayOffTimeDcObserver); 14605ccb8f90Sopenharmony_ci g_displayOffTimeDcObserver = nullptr; 14615ccb8f90Sopenharmony_ci } 14625ccb8f90Sopenharmony_ci#else 14635ccb8f90Sopenharmony_ci if (g_displayOffTimeObserver == nullptr) { 14645ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "g_displayOffTimeObserver is nullptr, no need to unregister"); 14655ccb8f90Sopenharmony_ci return; 14665ccb8f90Sopenharmony_ci } 14675ccb8f90Sopenharmony_ci SettingHelper::UnregisterSettingObserver(g_displayOffTimeObserver); 14685ccb8f90Sopenharmony_ci g_displayOffTimeObserver = nullptr; 14695ccb8f90Sopenharmony_ci#endif 14705ccb8f90Sopenharmony_ci} 14715ccb8f90Sopenharmony_ci 14725ccb8f90Sopenharmony_civoid PowerStateMachine::SetSleepTime(int64_t time) 14735ccb8f90Sopenharmony_ci{ 14745ccb8f90Sopenharmony_ci sleepTime_ = time; 14755ccb8f90Sopenharmony_ci} 14765ccb8f90Sopenharmony_ci 14775ccb8f90Sopenharmony_ciint64_t PowerStateMachine::GetDisplayOffTime() 14785ccb8f90Sopenharmony_ci{ 14795ccb8f90Sopenharmony_ci return displayOffTime_; 14805ccb8f90Sopenharmony_ci} 14815ccb8f90Sopenharmony_ci 14825ccb8f90Sopenharmony_ciint64_t PowerStateMachine::GetDimTime(int64_t displayOffTime) 14835ccb8f90Sopenharmony_ci{ 14845ccb8f90Sopenharmony_ci int64_t dimTime = displayOffTime / OFF_TIMEOUT_FACTOR; 14855ccb8f90Sopenharmony_ci return std::clamp(dimTime, static_cast<int64_t>(0), MAX_DIM_TIME_MS); 14865ccb8f90Sopenharmony_ci} 14875ccb8f90Sopenharmony_ci 14885ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT 14895ccb8f90Sopenharmony_ci int32_t PowerStateMachine::GetExternalScreenNumber() const 14905ccb8f90Sopenharmony_ci { 14915ccb8f90Sopenharmony_ci return externalScreenNumber_.load(); 14925ccb8f90Sopenharmony_ci } 14935ccb8f90Sopenharmony_ci 14945ccb8f90Sopenharmony_ci void PowerStateMachine::IncreaseExternalScreenNumber() 14955ccb8f90Sopenharmony_ci { 14965ccb8f90Sopenharmony_ci ++externalScreenNumber_; 14975ccb8f90Sopenharmony_ci } 14985ccb8f90Sopenharmony_ci 14995ccb8f90Sopenharmony_ci void PowerStateMachine::DecreaseExternalScreenNumber() 15005ccb8f90Sopenharmony_ci { 15015ccb8f90Sopenharmony_ci int32_t curNum = externalScreenNumber_.load(); 15025ccb8f90Sopenharmony_ci if (curNum == 0) { 15035ccb8f90Sopenharmony_ci POWER_HILOGW(COMP_SVC, "No external screen already"); 15045ccb8f90Sopenharmony_ci return; 15055ccb8f90Sopenharmony_ci } 15065ccb8f90Sopenharmony_ci --externalScreenNumber_; 15075ccb8f90Sopenharmony_ci } 15085ccb8f90Sopenharmony_ci#endif 15095ccb8f90Sopenharmony_ci 15105ccb8f90Sopenharmony_cibool PowerStateMachine::IsSettingState(PowerState state) 15115ccb8f90Sopenharmony_ci{ 15125ccb8f90Sopenharmony_ci int64_t flag = settingStateFlag_.load(); 15135ccb8f90Sopenharmony_ci bool matched = flag == static_cast<int64_t>(state); 15145ccb8f90Sopenharmony_ci if (matched) { 15155ccb8f90Sopenharmony_ci return true; 15165ccb8f90Sopenharmony_ci } else { 15175ccb8f90Sopenharmony_ci return ( 15185ccb8f90Sopenharmony_ci state == PowerState::DIM && flag == static_cast<int64_t>(SettingStateFlag::StateFlag::FORCE_SETTING_DIM)); 15195ccb8f90Sopenharmony_ci } 15205ccb8f90Sopenharmony_ci} 15215ccb8f90Sopenharmony_ci 15225ccb8f90Sopenharmony_ciint64_t PowerStateMachine::GetSleepTime() 15235ccb8f90Sopenharmony_ci{ 15245ccb8f90Sopenharmony_ci return sleepTime_; 15255ccb8f90Sopenharmony_ci} 15265ccb8f90Sopenharmony_ci 15275ccb8f90Sopenharmony_ciPowerStateMachine::ScreenChangeCheck::ScreenChangeCheck(std::shared_ptr<FFRTTimer> ffrtTimer, 15285ccb8f90Sopenharmony_ci PowerState state, StateChangeReason reason): timer_(ffrtTimer), state_(state), reason_(reason) 15295ccb8f90Sopenharmony_ci{ 15305ccb8f90Sopenharmony_ci // only check for screen on/off event 15315ccb8f90Sopenharmony_ci if (state != PowerState::INACTIVE && state != PowerState::AWAKE) { 15325ccb8f90Sopenharmony_ci return; 15335ccb8f90Sopenharmony_ci } 15345ccb8f90Sopenharmony_ci 15355ccb8f90Sopenharmony_ci if (!timer_) { 15365ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "ScreenChangeCheck failed: invalid timer"); 15375ccb8f90Sopenharmony_ci return; 15385ccb8f90Sopenharmony_ci } 15395ccb8f90Sopenharmony_ci 15405ccb8f90Sopenharmony_ci pid_ = IPCSkeleton::GetCallingPid(); 15415ccb8f90Sopenharmony_ci uid_ = IPCSkeleton::GetCallingUid(); 15425ccb8f90Sopenharmony_ci 15435ccb8f90Sopenharmony_ci FFRTTask task = [this]() { 15445ccb8f90Sopenharmony_ci Report("TIMEOUT"); 15455ccb8f90Sopenharmony_ci }; 15465ccb8f90Sopenharmony_ci 15475ccb8f90Sopenharmony_ci timer_->SetTimer(TIMER_ID_SCREEN_TIMEOUT_CHECK, task, SCREEN_CHANGE_TIMEOUT_MS); 15485ccb8f90Sopenharmony_ci} 15495ccb8f90Sopenharmony_ci 15505ccb8f90Sopenharmony_civoid PowerStateMachine::ScreenChangeCheck::Finish(TransitResult result) 15515ccb8f90Sopenharmony_ci{ 15525ccb8f90Sopenharmony_ci if (!timer_) { 15535ccb8f90Sopenharmony_ci return; 15545ccb8f90Sopenharmony_ci } 15555ccb8f90Sopenharmony_ci timer_->CancelTimer(TIMER_ID_SCREEN_TIMEOUT_CHECK); 15565ccb8f90Sopenharmony_ci 15575ccb8f90Sopenharmony_ci bool transitSuccess = (result == TransitResult::SUCCESS) || (result == TransitResult::ALREADY_IN_STATE); 15585ccb8f90Sopenharmony_ci bool skipReport = (result == TransitResult::LOCKING) || (result == TransitResult::FORBID_TRANSIT) || 15595ccb8f90Sopenharmony_ci !StateController::IsReallyFailed(reason_); 15605ccb8f90Sopenharmony_ci if (transitSuccess || skipReport) { 15615ccb8f90Sopenharmony_ci return; 15625ccb8f90Sopenharmony_ci } 15635ccb8f90Sopenharmony_ci 15645ccb8f90Sopenharmony_ci std::string msg = std::string("Transit failed with: ") + GetTransitResultString(result); 15655ccb8f90Sopenharmony_ci} 15665ccb8f90Sopenharmony_ci 15675ccb8f90Sopenharmony_civoid PowerStateMachine::ScreenChangeCheck::Report(const std::string &msg) 15685ccb8f90Sopenharmony_ci{ 15695ccb8f90Sopenharmony_ci const char* eventName = (state_ == PowerState::INACTIVE) ? "SCREEN_OFF_TIMEOUT" : "SCREEN_ON_TIMEOUT"; 15705ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "event=%{public}s, reason=%{public}s, msg=%{public}s, pid=%{public}d," 15715ccb8f90Sopenharmony_ci " uid=%{public}d", eventName, PowerUtils::GetReasonTypeString(reason_).c_str(), msg.c_str(), pid_, uid_); 15725ccb8f90Sopenharmony_ci 15735ccb8f90Sopenharmony_ci static int64_t lastReportTime = -1; 15745ccb8f90Sopenharmony_ci int64_t now = GetTickCount(); 15755ccb8f90Sopenharmony_ci int64_t nextReportTime = lastReportTime + SCREEN_CHANGE_REPORT_INTERVAL_MS; 15765ccb8f90Sopenharmony_ci if (nextReportTime > SCREEN_CHANGE_REPORT_INTERVAL_MS && now < nextReportTime) { 15775ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Will skip report for another %{public}s ms", 15785ccb8f90Sopenharmony_ci std::to_string(nextReportTime - now).c_str()); 15795ccb8f90Sopenharmony_ci return; 15805ccb8f90Sopenharmony_ci } 15815ccb8f90Sopenharmony_ci lastReportTime = now; 15825ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 15835ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, eventName, HiviewDFX::HiSysEvent::EventType::FAULT, 15845ccb8f90Sopenharmony_ci "PID", pid_, "UID", uid_, "PACKAGE_NAME", "", "PROCESS_NAME", "", "MSG", msg.c_str(), 15855ccb8f90Sopenharmony_ci "REASON", PowerUtils::GetReasonTypeString(reason_).c_str()); 15865ccb8f90Sopenharmony_ci#endif 15875ccb8f90Sopenharmony_ci} 15885ccb8f90Sopenharmony_ci 15895ccb8f90Sopenharmony_cistd::shared_ptr<PowerStateMachine::StateController> PowerStateMachine::GetStateController(PowerState state) 15905ccb8f90Sopenharmony_ci{ 15915ccb8f90Sopenharmony_ci auto iterator = controllerMap_.find(state); 15925ccb8f90Sopenharmony_ci if (iterator == controllerMap_.end()) { 15935ccb8f90Sopenharmony_ci return nullptr; 15945ccb8f90Sopenharmony_ci } 15955ccb8f90Sopenharmony_ci return iterator->second; 15965ccb8f90Sopenharmony_ci} 15975ccb8f90Sopenharmony_ci 15985ccb8f90Sopenharmony_cibool PowerStateMachine::NeedShowScreenLocks(PowerState state) 15995ccb8f90Sopenharmony_ci{ 16005ccb8f90Sopenharmony_ci return state == PowerState::AWAKE || 16015ccb8f90Sopenharmony_ci state == PowerState::INACTIVE || state == PowerState::DIM; 16025ccb8f90Sopenharmony_ci} 16035ccb8f90Sopenharmony_ci 16045ccb8f90Sopenharmony_civoid PowerStateMachine::UpdateSettingStateFlag(PowerState state, StateChangeReason reason) 16055ccb8f90Sopenharmony_ci{ 16065ccb8f90Sopenharmony_ci if (reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT || 16075ccb8f90Sopenharmony_ci reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF) { 16085ccb8f90Sopenharmony_ci settingOnStateFlag_ = false; 16095ccb8f90Sopenharmony_ci settingOffStateFlag_ = true; 16105ccb8f90Sopenharmony_ci return; 16115ccb8f90Sopenharmony_ci } 16125ccb8f90Sopenharmony_ci settingOnStateFlag_ = (state == PowerState::AWAKE); 16135ccb8f90Sopenharmony_ci settingOffStateFlag_ = (state == PowerState::INACTIVE); 16145ccb8f90Sopenharmony_ci} 16155ccb8f90Sopenharmony_ci 16165ccb8f90Sopenharmony_civoid PowerStateMachine::RestoreSettingStateFlag() 16175ccb8f90Sopenharmony_ci{ 16185ccb8f90Sopenharmony_ci settingOnStateFlag_ = false; 16195ccb8f90Sopenharmony_ci settingOffStateFlag_ = false; 16205ccb8f90Sopenharmony_ci} 16215ccb8f90Sopenharmony_ci 16225ccb8f90Sopenharmony_civoid PowerStateMachine::HandleProximityScreenOffTimer(PowerState state, StateChangeReason reason) 16235ccb8f90Sopenharmony_ci{ 16245ccb8f90Sopenharmony_ci if (!proximityScreenOffTimerStarted_.load()) { 16255ccb8f90Sopenharmony_ci return; 16265ccb8f90Sopenharmony_ci } 16275ccb8f90Sopenharmony_ci if ((reason == StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK || 16285ccb8f90Sopenharmony_ci reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) && 16295ccb8f90Sopenharmony_ci IsProximityClose() && state == PowerState::AWAKE) { 16305ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Double-click or pickup is not allowed to cancel proximity-screen-off timer"); 16315ccb8f90Sopenharmony_ci return; 16325ccb8f90Sopenharmony_ci } 16335ccb8f90Sopenharmony_ci if (reason != StateChangeReason::STATE_CHANGE_REASON_PROXIMITY) { 16345ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Cancel proximity-screen-off timer, reason:%{public}s", 16355ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str()); 16365ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG); 16375ccb8f90Sopenharmony_ci return; 16385ccb8f90Sopenharmony_ci } 16395ccb8f90Sopenharmony_ci if (state == PowerState::AWAKE) { 16405ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Cancel proximity-screen-off timer, reason:%{public}s(away)", 16415ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str()); 16425ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_PROXIMITY_SCREEN_OFF_MSG); 16435ccb8f90Sopenharmony_ci } 16445ccb8f90Sopenharmony_ci} 16455ccb8f90Sopenharmony_ci 16465ccb8f90Sopenharmony_cibool PowerStateMachine::HandlePreBrightState(StateChangeReason reason) 16475ccb8f90Sopenharmony_ci{ 16485ccb8f90Sopenharmony_ci bool ret = false; 16495ccb8f90Sopenharmony_ci PowerStateMachine::PreBrightState curState = preBrightState_.load(); 16505ccb8f90Sopenharmony_ci if (reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT) { 16515ccb8f90Sopenharmony_ci if (ffrtTimer_ != nullptr) { 16525ccb8f90Sopenharmony_ci FFRTTask authFailTask = [this] { 16535ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_WAKEUP, "Auth result of PRE_BRIGHT isn't received within %{public}u ms", 16545ccb8f90Sopenharmony_ci PRE_BRIGHT_AUTH_TIMER_DELAY_MS); 16555ccb8f90Sopenharmony_ci const std::string detail = "pre_bright_auth_fail_screen_off"; 16565ccb8f90Sopenharmony_ci const std::string pkgName = "pre_bright_auth_time"; 16575ccb8f90Sopenharmony_ci HandlePreBrightWakeUp(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF, 16585ccb8f90Sopenharmony_ci detail, pkgName, true); 16595ccb8f90Sopenharmony_ci }; 16605ccb8f90Sopenharmony_ci if (curState == PowerStateMachine::PRE_BRIGHT_STARTED) { 16615ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "Cancel pre-bright-auth timer, rason=%{public}s", 16625ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str()); 16635ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG); 16645ccb8f90Sopenharmony_ci } 16655ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Start pre-bright-auth timer"); 16665ccb8f90Sopenharmony_ci ffrtTimer_->SetTimer(TIMER_ID_PRE_BRIGHT_AUTH, authFailTask, PRE_BRIGHT_AUTH_TIMER_DELAY_MS); 16675ccb8f90Sopenharmony_ci preBrightState_.store(PowerStateMachine::PRE_BRIGHT_STARTED, std::memory_order_relaxed); 16685ccb8f90Sopenharmony_ci ret = true; 16695ccb8f90Sopenharmony_ci } 16705ccb8f90Sopenharmony_ci } else if (IsPreBrightAuthReason(reason)) { 16715ccb8f90Sopenharmony_ci if (curState == PowerStateMachine::PRE_BRIGHT_STARTED) { 16725ccb8f90Sopenharmony_ci preBrightState_.store(PowerStateMachine::PRE_BRIGHT_FINISHED, std::memory_order_relaxed); 16735ccb8f90Sopenharmony_ci ret = true; 16745ccb8f90Sopenharmony_ci } 16755ccb8f90Sopenharmony_ci } else { 16765ccb8f90Sopenharmony_ci if (curState == PowerStateMachine::PRE_BRIGHT_STARTED) { 16775ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "Cancel pre-bright-auth timer, rason=%{public}s", 16785ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str()); 16795ccb8f90Sopenharmony_ci CancelDelayTimer(PowerStateMachine::CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG); 16805ccb8f90Sopenharmony_ci } 16815ccb8f90Sopenharmony_ci preBrightState_.store(PowerStateMachine::PRE_BRIGHT_UNSTART, std::memory_order_relaxed); 16825ccb8f90Sopenharmony_ci ret = true; 16835ccb8f90Sopenharmony_ci } 16845ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "Pre bright state: %{public}u", static_cast<uint32_t>(preBrightState_.load())); 16855ccb8f90Sopenharmony_ci return ret; 16865ccb8f90Sopenharmony_ci} 16875ccb8f90Sopenharmony_ci 16885ccb8f90Sopenharmony_cibool PowerStateMachine::CheckFFRTTaskAvailability(PowerState state, StateChangeReason reason) const 16895ccb8f90Sopenharmony_ci{ 16905ccb8f90Sopenharmony_ci if (!IsTimeoutReason(reason)) { 16915ccb8f90Sopenharmony_ci return true; 16925ccb8f90Sopenharmony_ci } 16935ccb8f90Sopenharmony_ci void* curTask = ffrt_get_cur_task(); 16945ccb8f90Sopenharmony_ci if (curTask == nullptr) { 16955ccb8f90Sopenharmony_ci // not actually an ffrt task; 16965ccb8f90Sopenharmony_ci return true; 16975ccb8f90Sopenharmony_ci } 16985ccb8f90Sopenharmony_ci if (!ffrtTimer_) { 16995ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_POWER_STATE, "ffrtTimer_ is nullptr"); 17005ccb8f90Sopenharmony_ci return false; 17015ccb8f90Sopenharmony_ci } 17025ccb8f90Sopenharmony_ci const void* pendingTask = nullptr; 17035ccb8f90Sopenharmony_ci switch (state) { 17045ccb8f90Sopenharmony_ci case PowerState::DIM: 17055ccb8f90Sopenharmony_ci pendingTask = ffrtTimer_->GetTaskHandlePtr(TIMER_ID_USER_ACTIVITY_TIMEOUT); 17065ccb8f90Sopenharmony_ci break; 17075ccb8f90Sopenharmony_ci case PowerState::INACTIVE: 17085ccb8f90Sopenharmony_ci pendingTask = ffrtTimer_->GetTaskHandlePtr(TIMER_ID_USER_ACTIVITY_OFF); 17095ccb8f90Sopenharmony_ci break; 17105ccb8f90Sopenharmony_ci default: 17115ccb8f90Sopenharmony_ci pendingTask = ffrtTimer_->GetTaskHandlePtr(TIMER_ID_SLEEP); 17125ccb8f90Sopenharmony_ci break; 17135ccb8f90Sopenharmony_ci } 17145ccb8f90Sopenharmony_ci return curTask == pendingTask; 17155ccb8f90Sopenharmony_ci} 17165ccb8f90Sopenharmony_ci 17175ccb8f90Sopenharmony_cibool PowerStateMachine::IsTimeoutReason(StateChangeReason reason) const 17185ccb8f90Sopenharmony_ci{ 17195ccb8f90Sopenharmony_ci return reason == StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK || 17205ccb8f90Sopenharmony_ci reason == StateChangeReason::STATE_CHANGE_REASON_TIMEOUT; 17215ccb8f90Sopenharmony_ci} 17225ccb8f90Sopenharmony_ci 17235ccb8f90Sopenharmony_cibool PowerStateMachine::SetState(PowerState state, StateChangeReason reason, bool force) 17245ccb8f90Sopenharmony_ci{ 17255ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "state=%{public}s, reason=%{public}s, force=%{public}d", 17265ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(state).c_str(), PowerUtils::GetReasonTypeString(reason).c_str(), force); 17275ccb8f90Sopenharmony_ci std::lock_guard<std::mutex> lock(stateMutex_); 17285ccb8f90Sopenharmony_ci if (!CheckFFRTTaskAvailability(state, reason)) { 17295ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "this timeout task is invalidated, directly return"); 17305ccb8f90Sopenharmony_ci return false; 17315ccb8f90Sopenharmony_ci } 17325ccb8f90Sopenharmony_ci ScreenChangeCheck timeoutCheck(ffrtTimer_, state, reason); 17335ccb8f90Sopenharmony_ci SettingStateFlag flag(state, shared_from_this(), reason); 17345ccb8f90Sopenharmony_ci 17355ccb8f90Sopenharmony_ci if (NeedShowScreenLocks(state)) { 17365ccb8f90Sopenharmony_ci ShowCurrentScreenLocks(); 17375ccb8f90Sopenharmony_ci } 17385ccb8f90Sopenharmony_ci 17395ccb8f90Sopenharmony_ci HandleProximityScreenOffTimer(state, reason); 17405ccb8f90Sopenharmony_ci if (!HandlePreBrightState(reason)) { 17415ccb8f90Sopenharmony_ci timeoutCheck.Finish(TransitResult::OTHER_ERR); 17425ccb8f90Sopenharmony_ci return false; 17435ccb8f90Sopenharmony_ci } 17445ccb8f90Sopenharmony_ci 17455ccb8f90Sopenharmony_ci std::shared_ptr<StateController> pController = GetStateController(state); 17465ccb8f90Sopenharmony_ci if (pController == nullptr) { 17475ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_STATE, "StateController is not init"); 17485ccb8f90Sopenharmony_ci timeoutCheck.Finish(TransitResult::OTHER_ERR); 17495ccb8f90Sopenharmony_ci return false; 17505ccb8f90Sopenharmony_ci } 17515ccb8f90Sopenharmony_ci if (IsTimeoutReason(reason) && forceTimingOut_.load()) { 17525ccb8f90Sopenharmony_ci force = true; 17535ccb8f90Sopenharmony_ci } 17545ccb8f90Sopenharmony_ci UpdateSettingStateFlag(state, reason); 17555ccb8f90Sopenharmony_ci TransitResult ret = pController->TransitTo(reason, force); 17565ccb8f90Sopenharmony_ci timeoutCheck.Finish(ret); 17575ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER] StateController::TransitTo %{public}s ret: %{public}d", 17585ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(state).c_str(), ret); 17595ccb8f90Sopenharmony_ci RestoreSettingStateFlag(); 17605ccb8f90Sopenharmony_ci return (ret == TransitResult::SUCCESS || ret == TransitResult::ALREADY_IN_STATE); 17615ccb8f90Sopenharmony_ci} 17625ccb8f90Sopenharmony_ci 17635ccb8f90Sopenharmony_civoid PowerStateMachine::SetDisplaySuspend(bool enable) 17645ccb8f90Sopenharmony_ci{ 17655ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "enable: %{public}d", enable); 17665ccb8f90Sopenharmony_ci std::lock_guard<std::mutex> lock(stateMutex_); 17675ccb8f90Sopenharmony_ci enableDisplaySuspend_ = enable; 17685ccb8f90Sopenharmony_ci if (GetState() == PowerState::INACTIVE) { 17695ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "Change display state"); 17705ccb8f90Sopenharmony_ci if (enable) { 17715ccb8f90Sopenharmony_ci stateAction_->SetDisplayState( 17725ccb8f90Sopenharmony_ci DisplayState::DISPLAY_SUSPEND, StateChangeReason::STATE_CHANGE_REASON_APPLICATION); 17735ccb8f90Sopenharmony_ci } else { 17745ccb8f90Sopenharmony_ci stateAction_->SetDisplayState( 17755ccb8f90Sopenharmony_ci DisplayState::DISPLAY_OFF, StateChangeReason::STATE_CHANGE_REASON_APPLICATION); 17765ccb8f90Sopenharmony_ci } 17775ccb8f90Sopenharmony_ci } 17785ccb8f90Sopenharmony_ci} 17795ccb8f90Sopenharmony_ci 17805ccb8f90Sopenharmony_cibool PowerStateMachine::TryToCancelScreenOff() 17815ccb8f90Sopenharmony_ci{ 17825ccb8f90Sopenharmony_ci return stateAction_->TryToCancelScreenOff(); 17835ccb8f90Sopenharmony_ci} 17845ccb8f90Sopenharmony_ci 17855ccb8f90Sopenharmony_civoid PowerStateMachine::BeginPowerkeyScreenOff() 17865ccb8f90Sopenharmony_ci{ 17875ccb8f90Sopenharmony_ci stateAction_->BeginPowerkeyScreenOff(); 17885ccb8f90Sopenharmony_ci} 17895ccb8f90Sopenharmony_ci 17905ccb8f90Sopenharmony_civoid PowerStateMachine::EndPowerkeyScreenOff() 17915ccb8f90Sopenharmony_ci{ 17925ccb8f90Sopenharmony_ci stateAction_->EndPowerkeyScreenOff(); 17935ccb8f90Sopenharmony_ci} 17945ccb8f90Sopenharmony_ci 17955ccb8f90Sopenharmony_ciStateChangeReason PowerStateMachine::GetReasonByUserActivity(UserActivityType type) 17965ccb8f90Sopenharmony_ci{ 17975ccb8f90Sopenharmony_ci StateChangeReason ret = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; 17985ccb8f90Sopenharmony_ci switch (type) { 17995ccb8f90Sopenharmony_ci case UserActivityType::USER_ACTIVITY_TYPE_BUTTON: 18005ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; 18015ccb8f90Sopenharmony_ci break; 18025ccb8f90Sopenharmony_ci case UserActivityType::USER_ACTIVITY_TYPE_TOUCH: 18035ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_TOUCH; 18045ccb8f90Sopenharmony_ci break; 18055ccb8f90Sopenharmony_ci case UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY: 18065ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_ACCESSIBILITY; 18075ccb8f90Sopenharmony_ci break; 18085ccb8f90Sopenharmony_ci case UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE: 18095ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_APPLICATION; 18105ccb8f90Sopenharmony_ci break; 18115ccb8f90Sopenharmony_ci case UserActivityType::USER_ACTIVITY_TYPE_ATTENTION: // fall through 18125ccb8f90Sopenharmony_ci case UserActivityType::USER_ACTIVITY_TYPE_OTHER: // fall through 18135ccb8f90Sopenharmony_ci default: 18145ccb8f90Sopenharmony_ci break; 18155ccb8f90Sopenharmony_ci } 18165ccb8f90Sopenharmony_ci return ret; 18175ccb8f90Sopenharmony_ci} 18185ccb8f90Sopenharmony_ci 18195ccb8f90Sopenharmony_ciStateChangeReason PowerStateMachine::GetReasonByWakeType(WakeupDeviceType type) 18205ccb8f90Sopenharmony_ci{ 18215ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "WakeupDeviceType :%{public}u", type); 18225ccb8f90Sopenharmony_ci StateChangeReason ret = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; 18235ccb8f90Sopenharmony_ci switch (type) { 18245ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON: 18255ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_POWER_KEY; 18265ccb8f90Sopenharmony_ci break; 18275ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY: 18285ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; 18295ccb8f90Sopenharmony_ci break; 18305ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_APPLICATION: 18315ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_APPLICATION; 18325ccb8f90Sopenharmony_ci break; 18335ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN: // fall through 18345ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_HDMI: 18355ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_CABLE; 18365ccb8f90Sopenharmony_ci break; 18375ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_GESTURE: 18385ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_TOUCH; 18395ccb8f90Sopenharmony_ci break; 18405ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH: 18415ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_CAMERA; 18425ccb8f90Sopenharmony_ci break; 18435ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION: 18445ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_SENSOR; 18455ccb8f90Sopenharmony_ci break; 18465ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_LID: 18475ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_LID; 18485ccb8f90Sopenharmony_ci break; 18495ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_DOUBLE_CLICK: 18505ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK; 18515ccb8f90Sopenharmony_ci break; 18525ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PEN: 18535ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_PEN; 18545ccb8f90Sopenharmony_ci break; 18555ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_KEYBOARD: 18565ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_KEYBOARD; 18575ccb8f90Sopenharmony_ci break; 18585ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_MOUSE: 18595ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_MOUSE; 18605ccb8f90Sopenharmony_ci break; 18615ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_SWITCH: 18625ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_SWITCH; 18635ccb8f90Sopenharmony_ci break; 18645ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT: 18655ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT; 18665ccb8f90Sopenharmony_ci break; 18675ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_SUCCESS: 18685ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS; 18695ccb8f90Sopenharmony_ci break; 18705ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON: 18715ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON; 18725ccb8f90Sopenharmony_ci break; 18735ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF: 18745ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF; 18755ccb8f90Sopenharmony_ci break; 18765ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_AOD_SLIDING: 18775ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_AOD_SLIDING; 18785ccb8f90Sopenharmony_ci break; 18795ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_INCOMING_CALL: 18805ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_INCOMING_CALL; 18815ccb8f90Sopenharmony_ci break; 18825ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_SHELL: 18835ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_SHELL; 18845ccb8f90Sopenharmony_ci break; 18855ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_PICKUP: 18865ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_PICKUP; 18875ccb8f90Sopenharmony_ci break; 18885ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_EXIT_SYSTEM_STR: 18895ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_EXIT_SYSTEM_STR; 18905ccb8f90Sopenharmony_ci break; 18915ccb8f90Sopenharmony_ci case WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN: // fall through 18925ccb8f90Sopenharmony_ci default: 18935ccb8f90Sopenharmony_ci break; 18945ccb8f90Sopenharmony_ci } 18955ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_WAKEUP, "StateChangeReason: %{public}u", ret); 18965ccb8f90Sopenharmony_ci return ret; 18975ccb8f90Sopenharmony_ci} 18985ccb8f90Sopenharmony_ci 18995ccb8f90Sopenharmony_ciStateChangeReason PowerStateMachine::GetReasonBySuspendType(SuspendDeviceType type) 19005ccb8f90Sopenharmony_ci{ 19015ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SUSPEND, "SuspendDeviceType: %{public}u", type); 19025ccb8f90Sopenharmony_ci StateChangeReason ret = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; 19035ccb8f90Sopenharmony_ci switch (type) { 19045ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION: 19055ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_APPLICATION; 19065ccb8f90Sopenharmony_ci break; 19075ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN: 19085ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_REMOTE; 19095ccb8f90Sopenharmony_ci break; 19105ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT: 19115ccb8f90Sopenharmony_ci ret = (enabledTimingOutLockScreen_.load() && 19125ccb8f90Sopenharmony_ci (!enabledTimingOutLockScreenCheckLock_.load() || 19135ccb8f90Sopenharmony_ci CheckRunningLock(PowerState::INACTIVE))) ? 19145ccb8f90Sopenharmony_ci StateChangeReason::STATE_CHANGE_REASON_TIMEOUT : 19155ccb8f90Sopenharmony_ci StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK; 19165ccb8f90Sopenharmony_ci break; 19175ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_LID: 19185ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_LID; 19195ccb8f90Sopenharmony_ci break; 19205ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_SWITCH: 19215ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_SWITCH; 19225ccb8f90Sopenharmony_ci break; 19235ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY: // fall through 19245ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_SLEEP_KEY: 19255ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; 19265ccb8f90Sopenharmony_ci break; 19275ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_HDMI: 19285ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_CABLE; 19295ccb8f90Sopenharmony_ci break; 19305ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_ACCESSIBILITY: 19315ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_ACCESSIBILITY; 19325ccb8f90Sopenharmony_ci break; 19335ccb8f90Sopenharmony_ci case SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND: 19345ccb8f90Sopenharmony_ci ret = StateChangeReason::STATE_CHANGE_REASON_SYSTEM; 19355ccb8f90Sopenharmony_ci break; 19365ccb8f90Sopenharmony_ci default: 19375ccb8f90Sopenharmony_ci break; 19385ccb8f90Sopenharmony_ci } 19395ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_SUSPEND, "StateChangeReason: %{public}u", ret); 19405ccb8f90Sopenharmony_ci return ret; 19415ccb8f90Sopenharmony_ci} 19425ccb8f90Sopenharmony_ci 19435ccb8f90Sopenharmony_civoid PowerStateMachine::AppendDumpInfo(std::string& result, std::string& reason, std::string& time) 19445ccb8f90Sopenharmony_ci{ 19455ccb8f90Sopenharmony_ci result.append("POWER STATE DUMP:\n"); 19465ccb8f90Sopenharmony_ci result.append("Current State: ") 19475ccb8f90Sopenharmony_ci .append(PowerUtils::GetPowerStateString(GetState())) 19485ccb8f90Sopenharmony_ci .append(" Reason: ") 19495ccb8f90Sopenharmony_ci .append(reason) 19505ccb8f90Sopenharmony_ci .append(" Time: ") 19515ccb8f90Sopenharmony_ci .append(time) 19525ccb8f90Sopenharmony_ci .append("\n"); 19535ccb8f90Sopenharmony_ci 19545ccb8f90Sopenharmony_ci result.append("ScreenOffTime: Timeout="); 19555ccb8f90Sopenharmony_ci if (isScreenOffTimeOverride_) { 19565ccb8f90Sopenharmony_ci result.append((ToString(g_beforeOverrideTime))) 19575ccb8f90Sopenharmony_ci .append("ms OverrideTimeout=") 19585ccb8f90Sopenharmony_ci .append((ToString(GetDisplayOffTime()))) 19595ccb8f90Sopenharmony_ci .append("ms\n"); 19605ccb8f90Sopenharmony_ci } else { 19615ccb8f90Sopenharmony_ci result.append((ToString(GetDisplayOffTime()))).append("ms\n"); 19625ccb8f90Sopenharmony_ci } 19635ccb8f90Sopenharmony_ci 19645ccb8f90Sopenharmony_ci result.append("DUMP DETAILS:\n"); 19655ccb8f90Sopenharmony_ci result.append("Last Screen On: ").append(ToString(mDeviceState_.screenState.lastOnTime)).append("\n"); 19665ccb8f90Sopenharmony_ci result.append("Last Screen Off: ").append(ToString(mDeviceState_.screenState.lastOffTime)).append("\n"); 19675ccb8f90Sopenharmony_ci result.append("Last SuspendDevice: ").append(ToString(mDeviceState_.lastSuspendDeviceTime)).append("\n"); 19685ccb8f90Sopenharmony_ci result.append("Last WakeupDevice: ").append(ToString(mDeviceState_.lastWakeupDeviceTime)).append("\n"); 19695ccb8f90Sopenharmony_ci result.append("Last Refresh: ").append(ToString(mDeviceState_.lastRefreshActivityTime)).append("\n"); 19705ccb8f90Sopenharmony_ci 19715ccb8f90Sopenharmony_ci result.append("DUMP EACH STATES:\n"); 19725ccb8f90Sopenharmony_ci for (auto it = controllerMap_.begin(); it != controllerMap_.end(); it++) { 19735ccb8f90Sopenharmony_ci result.append("State: ") 19745ccb8f90Sopenharmony_ci .append(PowerUtils::GetPowerStateString(it->second->GetState())) 19755ccb8f90Sopenharmony_ci .append(" Reason: ") 19765ccb8f90Sopenharmony_ci .append(PowerUtils::GetReasonTypeString(it->second->lastReason_).c_str()) 19775ccb8f90Sopenharmony_ci .append(" Time: ") 19785ccb8f90Sopenharmony_ci .append(ToString(it->second->lastTime_)) 19795ccb8f90Sopenharmony_ci .append("\n") 19805ccb8f90Sopenharmony_ci .append(" Failure: ") 19815ccb8f90Sopenharmony_ci .append(PowerUtils::GetReasonTypeString(it->second->failTrigger_).c_str()) 19825ccb8f90Sopenharmony_ci .append(" Reason: ") 19835ccb8f90Sopenharmony_ci .append(it->second->failReason_) 19845ccb8f90Sopenharmony_ci .append(" From: ") 19855ccb8f90Sopenharmony_ci .append(PowerUtils::GetPowerStateString(it->second->failFrom_)) 19865ccb8f90Sopenharmony_ci .append(" Time: ") 19875ccb8f90Sopenharmony_ci .append(ToString(it->second->failTime_)) 19885ccb8f90Sopenharmony_ci .append("\n\n"); 19895ccb8f90Sopenharmony_ci } 19905ccb8f90Sopenharmony_ci} 19915ccb8f90Sopenharmony_ci 19925ccb8f90Sopenharmony_civoid PowerStateMachine::DumpInfo(std::string& result) 19935ccb8f90Sopenharmony_ci{ 19945ccb8f90Sopenharmony_ci std::string reason = "UNKNOWN"; 19955ccb8f90Sopenharmony_ci std::string time = "UNKNOWN"; 19965ccb8f90Sopenharmony_ci auto it = controllerMap_.find(GetState()); 19975ccb8f90Sopenharmony_ci if (it != controllerMap_.end() && it->second != nullptr) { 19985ccb8f90Sopenharmony_ci reason = ToString(static_cast<uint32_t>(it->second->lastReason_)); 19995ccb8f90Sopenharmony_ci time = ToString(it->second->lastTime_); 20005ccb8f90Sopenharmony_ci } 20015ccb8f90Sopenharmony_ci AppendDumpInfo(result, reason, time); 20025ccb8f90Sopenharmony_ci} 20035ccb8f90Sopenharmony_ci 20045ccb8f90Sopenharmony_cibool PowerStateMachine::StateController::NeedNotify(PowerState currentState) 20055ccb8f90Sopenharmony_ci{ 20065ccb8f90Sopenharmony_ci if (currentState == GetState()) { 20075ccb8f90Sopenharmony_ci return false; 20085ccb8f90Sopenharmony_ci } 20095ccb8f90Sopenharmony_ci if (currentState == PowerState::DIM && GetState() == PowerState::AWAKE) { 20105ccb8f90Sopenharmony_ci return false; 20115ccb8f90Sopenharmony_ci } 20125ccb8f90Sopenharmony_ci return true; 20135ccb8f90Sopenharmony_ci} 20145ccb8f90Sopenharmony_ci 20155ccb8f90Sopenharmony_ciTransitResult PowerStateMachine::StateController::TransitTo(StateChangeReason reason, bool ignoreLock) 20165ccb8f90Sopenharmony_ci{ 20175ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Start"); 20185ccb8f90Sopenharmony_ci std::shared_ptr<PowerStateMachine> owner = owner_.lock(); 20195ccb8f90Sopenharmony_ci if (owner == nullptr) { 20205ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_STATE, "owner is nullptr"); 20215ccb8f90Sopenharmony_ci return TransitResult::OTHER_ERR; 20225ccb8f90Sopenharmony_ci } 20235ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, 20245ccb8f90Sopenharmony_ci "[UL_POWER] Transit from %{public}s to %{public}s for %{public}s ignoreLock=%{public}d", 20255ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(owner->currentState_).c_str(), 20265ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(this->state_).c_str(), 20275ccb8f90Sopenharmony_ci PowerUtils::GetReasonTypeString(reason).c_str(), ignoreLock); 20285ccb8f90Sopenharmony_ci MatchState(owner->currentState_, owner->stateAction_->GetDisplayState()); 20295ccb8f90Sopenharmony_ci if (!CheckState()) { 20305ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Already in state: %{public}d", owner->currentState_); 20315ccb8f90Sopenharmony_ci RecordFailure(owner->currentState_, reason, TransitResult::ALREADY_IN_STATE); 20325ccb8f90Sopenharmony_ci return TransitResult::ALREADY_IN_STATE; 20335ccb8f90Sopenharmony_ci } 20345ccb8f90Sopenharmony_ci 20355ccb8f90Sopenharmony_ci if (reason != StateChangeReason::STATE_CHANGE_REASON_INIT && !owner->CanTransitTo(state_, reason)) { 20365ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Block Transit from %{public}s to %{public}s", 20375ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(owner->currentState_).c_str(), 20385ccb8f90Sopenharmony_ci PowerUtils::GetPowerStateString(state_).c_str()); 20395ccb8f90Sopenharmony_ci RecordFailure(owner->currentState_, reason, TransitResult::FORBID_TRANSIT); 20405ccb8f90Sopenharmony_ci return TransitResult::FORBID_TRANSIT; 20415ccb8f90Sopenharmony_ci } 20425ccb8f90Sopenharmony_ci 20435ccb8f90Sopenharmony_ci if (!ignoreLock && !owner->CheckRunningLock(GetState())) { 20445ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Running lock block"); 20455ccb8f90Sopenharmony_ci RecordFailure(owner->currentState_, reason, TransitResult::LOCKING); 20465ccb8f90Sopenharmony_ci return TransitResult::LOCKING; 20475ccb8f90Sopenharmony_ci } 20485ccb8f90Sopenharmony_ci TransitResult ret = action_(reason); 20495ccb8f90Sopenharmony_ci if (ret == TransitResult::SUCCESS) { 20505ccb8f90Sopenharmony_ci bool needNotify = NeedNotify(owner->currentState_); 20515ccb8f90Sopenharmony_ci lastReason_ = reason; 20525ccb8f90Sopenharmony_ci lastTime_ = GetTickCount(); 20535ccb8f90Sopenharmony_ci owner->currentState_ = GetState(); 20545ccb8f90Sopenharmony_ci if (needNotify) { 20555ccb8f90Sopenharmony_ci owner->NotifyPowerStateChanged(owner->currentState_, reason); 20565ccb8f90Sopenharmony_ci } 20575ccb8f90Sopenharmony_ci } else if (IsReallyFailed(reason)) { 20585ccb8f90Sopenharmony_ci RecordFailure(owner->currentState_, reason, ret); 20595ccb8f90Sopenharmony_ci } 20605ccb8f90Sopenharmony_ci 20615ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "Finish, result: %{public}d", ret); 20625ccb8f90Sopenharmony_ci return ret; 20635ccb8f90Sopenharmony_ci} 20645ccb8f90Sopenharmony_ci 20655ccb8f90Sopenharmony_cibool PowerStateMachine::StateController::CheckState() 20665ccb8f90Sopenharmony_ci{ 20675ccb8f90Sopenharmony_ci std::shared_ptr<PowerStateMachine> owner = owner_.lock(); 20685ccb8f90Sopenharmony_ci if (owner == nullptr) { 20695ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_STATE, "Owner is nullptr"); 20705ccb8f90Sopenharmony_ci return false; 20715ccb8f90Sopenharmony_ci } 20725ccb8f90Sopenharmony_ci auto state = GetState(); 20735ccb8f90Sopenharmony_ci if (state == PowerState::DIM || state == PowerState::AWAKE) { 20745ccb8f90Sopenharmony_ci return true; 20755ccb8f90Sopenharmony_ci } 20765ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_POWER_STATE, "state: %{public}u, currentState_: %{public}u", state, owner->currentState_); 20775ccb8f90Sopenharmony_ci return state != owner->currentState_; 20785ccb8f90Sopenharmony_ci} 20795ccb8f90Sopenharmony_ci 20805ccb8f90Sopenharmony_civoid PowerStateMachine::StateController::CorrectState( 20815ccb8f90Sopenharmony_ci PowerState& currentState, PowerState correctState, DisplayState state) 20825ccb8f90Sopenharmony_ci{ 20835ccb8f90Sopenharmony_ci std::string msg = "[UL_POWER] Correct power state errors from "; 20845ccb8f90Sopenharmony_ci msg.append(PowerUtils::GetPowerStateString(currentState)) 20855ccb8f90Sopenharmony_ci .append(" to ") 20865ccb8f90Sopenharmony_ci .append(PowerUtils::GetPowerStateString(correctState)) 20875ccb8f90Sopenharmony_ci .append(" due to current display state is ") 20885ccb8f90Sopenharmony_ci .append(PowerUtils::GetDisplayStateString(state)); 20895ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_POWER_STATE, "%{public}s", msg.c_str()); 20905ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 20915ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "STATE_CORRECTION", HiviewDFX::HiSysEvent::EventType::FAULT, 20925ccb8f90Sopenharmony_ci "ERROR_STATE", static_cast<uint32_t>(currentState), "CORRECTION_STATE", static_cast<uint32_t>(correctState), 20935ccb8f90Sopenharmony_ci "DISPLAY_STATE", static_cast<uint32_t>(state), "MSG", msg); 20945ccb8f90Sopenharmony_ci#endif 20955ccb8f90Sopenharmony_ci currentState = correctState; 20965ccb8f90Sopenharmony_ci} 20975ccb8f90Sopenharmony_ci 20985ccb8f90Sopenharmony_civoid PowerStateMachine::StateController::MatchState(PowerState& currentState, DisplayState state) 20995ccb8f90Sopenharmony_ci{ 21005ccb8f90Sopenharmony_ci if (GetState() == PowerState::SLEEP || currentState == PowerState::SLEEP || GetState() == PowerState::HIBERNATE || 21015ccb8f90Sopenharmony_ci currentState == PowerState::HIBERNATE || GetState() == PowerState::SHUTDOWN || 21025ccb8f90Sopenharmony_ci currentState == PowerState::SHUTDOWN) { 21035ccb8f90Sopenharmony_ci return; 21045ccb8f90Sopenharmony_ci } 21055ccb8f90Sopenharmony_ci 21065ccb8f90Sopenharmony_ci // Keep the state of display consistent with the state of power 21075ccb8f90Sopenharmony_ci switch (state) { 21085ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_OFF: 21095ccb8f90Sopenharmony_ci if (currentState == PowerState::AWAKE || currentState == PowerState::FREEZE) { 21105ccb8f90Sopenharmony_ci CorrectState(currentState, PowerState::INACTIVE, state); 21115ccb8f90Sopenharmony_ci } 21125ccb8f90Sopenharmony_ci break; 21135ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_DIM: 21145ccb8f90Sopenharmony_ci if (currentState == PowerState::INACTIVE || currentState == PowerState::STAND_BY || 21155ccb8f90Sopenharmony_ci currentState == PowerState::DOZE) { 21165ccb8f90Sopenharmony_ci CorrectState(currentState, PowerState::DIM, state); 21175ccb8f90Sopenharmony_ci } 21185ccb8f90Sopenharmony_ci break; 21195ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_ON: 21205ccb8f90Sopenharmony_ci if (currentState == PowerState::INACTIVE || currentState == PowerState::STAND_BY || 21215ccb8f90Sopenharmony_ci currentState == PowerState::DOZE) { 21225ccb8f90Sopenharmony_ci CorrectState(currentState, PowerState::AWAKE, state); 21235ccb8f90Sopenharmony_ci } 21245ccb8f90Sopenharmony_ci break; 21255ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_SUSPEND: 21265ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_UNKNOWN: 21275ccb8f90Sopenharmony_ci default: 21285ccb8f90Sopenharmony_ci break; 21295ccb8f90Sopenharmony_ci } 21305ccb8f90Sopenharmony_ci} 21315ccb8f90Sopenharmony_ci 21325ccb8f90Sopenharmony_civoid PowerStateMachine::Reset() 21335ccb8f90Sopenharmony_ci{ 21345ccb8f90Sopenharmony_ci ffrtTimer_.reset(); 21355ccb8f90Sopenharmony_ci} 21365ccb8f90Sopenharmony_ci 21375ccb8f90Sopenharmony_cistd::string PowerStateMachine::GetTransitResultString(TransitResult result) 21385ccb8f90Sopenharmony_ci{ 21395ccb8f90Sopenharmony_ci switch (result) { 21405ccb8f90Sopenharmony_ci case TransitResult::ALREADY_IN_STATE: 21415ccb8f90Sopenharmony_ci return "Already in the state"; 21425ccb8f90Sopenharmony_ci case TransitResult::LOCKING: 21435ccb8f90Sopenharmony_ci return "Blocked by running lock"; 21445ccb8f90Sopenharmony_ci case TransitResult::HDI_ERR: 21455ccb8f90Sopenharmony_ci return "Power HDI error"; 21465ccb8f90Sopenharmony_ci case TransitResult::DISPLAY_ON_ERR: 21475ccb8f90Sopenharmony_ci return "SetDisplayState(ON) error"; 21485ccb8f90Sopenharmony_ci case TransitResult::DISPLAY_OFF_ERR: 21495ccb8f90Sopenharmony_ci return "SetDisplayState(OFF) error"; 21505ccb8f90Sopenharmony_ci case TransitResult::FORBID_TRANSIT: 21515ccb8f90Sopenharmony_ci return "Forbid transit"; 21525ccb8f90Sopenharmony_ci case TransitResult::OTHER_ERR: 21535ccb8f90Sopenharmony_ci return "Other error"; 21545ccb8f90Sopenharmony_ci default: 21555ccb8f90Sopenharmony_ci break; 21565ccb8f90Sopenharmony_ci } 21575ccb8f90Sopenharmony_ci return "Unknown error"; 21585ccb8f90Sopenharmony_ci} 21595ccb8f90Sopenharmony_ci 21605ccb8f90Sopenharmony_civoid PowerStateMachine::StateController::RecordFailure( 21615ccb8f90Sopenharmony_ci PowerState from, StateChangeReason trigger, TransitResult failReason) 21625ccb8f90Sopenharmony_ci{ 21635ccb8f90Sopenharmony_ci failFrom_ = from; 21645ccb8f90Sopenharmony_ci failTrigger_ = trigger; 21655ccb8f90Sopenharmony_ci failTime_ = GetTickCount(); 21665ccb8f90Sopenharmony_ci failReason_ = GetTransitResultString(failReason); 21675ccb8f90Sopenharmony_ci std::string message = "State Transit Failed from "; 21685ccb8f90Sopenharmony_ci message.append(PowerUtils::GetPowerStateString(failFrom_)) 21695ccb8f90Sopenharmony_ci .append(" to ") 21705ccb8f90Sopenharmony_ci .append(PowerUtils::GetPowerStateString(GetState())) 21715ccb8f90Sopenharmony_ci .append(" by ") 21725ccb8f90Sopenharmony_ci .append(PowerUtils::GetReasonTypeString(failTrigger_).c_str()) 21735ccb8f90Sopenharmony_ci .append(" Reason:") 21745ccb8f90Sopenharmony_ci .append(failReason_) 21755ccb8f90Sopenharmony_ci .append(" Time:") 21765ccb8f90Sopenharmony_ci .append(ToString(failTime_)) 21775ccb8f90Sopenharmony_ci .append("\n"); 21785ccb8f90Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 21795ccb8f90Sopenharmony_ci const int logLevel = 2; 21805ccb8f90Sopenharmony_ci const std::string tag = "TAG_POWER"; 21815ccb8f90Sopenharmony_ci HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "SCREEN", HiviewDFX::HiSysEvent::EventType::FAULT, 21825ccb8f90Sopenharmony_ci "LOG_LEVEL", logLevel, "TAG", tag, "MESSAGE", message); 21835ccb8f90Sopenharmony_ci POWER_HILOGI(FEATURE_POWER_STATE, "RecordFailure: %{public}s", message.c_str()); 21845ccb8f90Sopenharmony_ci#endif 21855ccb8f90Sopenharmony_ci} 21865ccb8f90Sopenharmony_ci 21875ccb8f90Sopenharmony_cibool PowerStateMachine::StateController::IsReallyFailed(StateChangeReason reason) 21885ccb8f90Sopenharmony_ci{ 21895ccb8f90Sopenharmony_ci if (reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT || 21905ccb8f90Sopenharmony_ci reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF) { 21915ccb8f90Sopenharmony_ci return false; 21925ccb8f90Sopenharmony_ci } 21935ccb8f90Sopenharmony_ci return true; 21945ccb8f90Sopenharmony_ci} 21955ccb8f90Sopenharmony_ci} // namespace PowerMgr 21965ccb8f90Sopenharmony_ci} // namespace OHOS 2197