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#ifndef POWERMGR_POWER_STATE_MACHINE_H
175ccb8f90Sopenharmony_ci#define POWERMGR_POWER_STATE_MACHINE_H
185ccb8f90Sopenharmony_ci
195ccb8f90Sopenharmony_ci#include <set>
205ccb8f90Sopenharmony_ci#include <map>
215ccb8f90Sopenharmony_ci#include <singleton.h>
225ccb8f90Sopenharmony_ci
235ccb8f90Sopenharmony_ci#include "actions/idevice_state_action.h"
245ccb8f90Sopenharmony_ci#include "ffrt_utils.h"
255ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4
265ccb8f90Sopenharmony_ci#include "hibernate_controller.h"
275ccb8f90Sopenharmony_ci#endif
285ccb8f90Sopenharmony_ci#include "ipower_state_callback.h"
295ccb8f90Sopenharmony_ci#include "power_common.h"
305ccb8f90Sopenharmony_ci#include "power_state_machine_info.h"
315ccb8f90Sopenharmony_ci#include "running_lock_info.h"
325ccb8f90Sopenharmony_ci
335ccb8f90Sopenharmony_cinamespace OHOS {
345ccb8f90Sopenharmony_cinamespace PowerMgr {
355ccb8f90Sopenharmony_ciclass RunningLockMgr;
365ccb8f90Sopenharmony_ciclass PowerMgrService;
375ccb8f90Sopenharmony_ci
385ccb8f90Sopenharmony_cistruct ScreenState {
395ccb8f90Sopenharmony_ci    DisplayState state;
405ccb8f90Sopenharmony_ci    int64_t lastOnTime;
415ccb8f90Sopenharmony_ci    int64_t lastOffTime;
425ccb8f90Sopenharmony_ci};
435ccb8f90Sopenharmony_ci
445ccb8f90Sopenharmony_cistruct DevicePowerState {
455ccb8f90Sopenharmony_ci    ScreenState screenState;
465ccb8f90Sopenharmony_ci    // record the last time when get wakeup event from A side
475ccb8f90Sopenharmony_ci    int64_t lastWakeupEventTime;
485ccb8f90Sopenharmony_ci    // record the last time when calling func RefreshActivityInner
495ccb8f90Sopenharmony_ci    int64_t lastRefreshActivityTime;
505ccb8f90Sopenharmony_ci    // record the last time when calling func WakeupDeviceInner
515ccb8f90Sopenharmony_ci    int64_t lastWakeupDeviceTime;
525ccb8f90Sopenharmony_ci    // record the last time when calling func SuspendDeviceInner
535ccb8f90Sopenharmony_ci    int64_t lastSuspendDeviceTime;
545ccb8f90Sopenharmony_ci};
555ccb8f90Sopenharmony_ci
565ccb8f90Sopenharmony_cienum class TransitResult {
575ccb8f90Sopenharmony_ci    SUCCESS = 0,
585ccb8f90Sopenharmony_ci    ALREADY_IN_STATE = 1,
595ccb8f90Sopenharmony_ci    LOCKING = 2,
605ccb8f90Sopenharmony_ci    HDI_ERR = 3,
615ccb8f90Sopenharmony_ci    DISPLAY_ON_ERR = 4,
625ccb8f90Sopenharmony_ci    DISPLAY_OFF_ERR = 5,
635ccb8f90Sopenharmony_ci    FORBID_TRANSIT = 6,
645ccb8f90Sopenharmony_ci    OTHER_ERR = 99
655ccb8f90Sopenharmony_ci};
665ccb8f90Sopenharmony_ci
675ccb8f90Sopenharmony_ciclass PowerStateMachine : public std::enable_shared_from_this<PowerStateMachine> {
685ccb8f90Sopenharmony_cipublic:
695ccb8f90Sopenharmony_ci    PowerStateMachine(const wptr<PowerMgrService>& pms, const std::shared_ptr<FFRTTimer>& ffrtTimer = nullptr);
705ccb8f90Sopenharmony_ci    ~PowerStateMachine();
715ccb8f90Sopenharmony_ci
725ccb8f90Sopenharmony_ci    enum {
735ccb8f90Sopenharmony_ci        CHECK_USER_ACTIVITY_TIMEOUT_MSG = 0,
745ccb8f90Sopenharmony_ci        CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG,
755ccb8f90Sopenharmony_ci        CHECK_PRE_BRIGHT_AUTH_TIMEOUT_MSG,
765ccb8f90Sopenharmony_ci        CHECK_PROXIMITY_SCREEN_OFF_MSG,
775ccb8f90Sopenharmony_ci    };
785ccb8f90Sopenharmony_ci
795ccb8f90Sopenharmony_ci    class PowerStateCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
805ccb8f90Sopenharmony_ci    public:
815ccb8f90Sopenharmony_ci        PowerStateCallbackDeathRecipient() = default;
825ccb8f90Sopenharmony_ci        virtual void OnRemoteDied(const wptr<IRemoteObject>& remote);
835ccb8f90Sopenharmony_ci        virtual ~PowerStateCallbackDeathRecipient() = default;
845ccb8f90Sopenharmony_ci    };
855ccb8f90Sopenharmony_ci
865ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
875ccb8f90Sopenharmony_ci    static constexpr int32_t DEFAULT_AC_DISPLAY_OFF_TIME_MS = 600000;
885ccb8f90Sopenharmony_ci    static constexpr int32_t DEFAULT_DC_DISPLAY_OFF_TIME_MS = 300000
895ccb8f90Sopenharmony_ci    static constexpr int32_t DEFAULT_DISPLAY_OFF_TIME_MS = DEFAULT_DC_DISPLAY_OFF_TIME_MS;
905ccb8f90Sopenharmony_ci#else
915ccb8f90Sopenharmony_ci    static constexpr int32_t DEFAULT_DISPLAY_OFF_TIME_MS = 30000;
925ccb8f90Sopenharmony_ci#endif
935ccb8f90Sopenharmony_ci    static constexpr int32_t DEFAULT_SLEEP_TIME_MS = 5000;
945ccb8f90Sopenharmony_ci    static constexpr int64_t OFF_TIMEOUT_FACTOR = 5;
955ccb8f90Sopenharmony_ci    static constexpr int64_t MAX_DIM_TIME_MS = 7500;
965ccb8f90Sopenharmony_ci    static constexpr int64_t COORDINATED_STATE_SCREEN_OFF_TIME_MS = 10000;
975ccb8f90Sopenharmony_ci    static constexpr uint32_t SCREEN_CHANGE_TIMEOUT_MS = 10000;
985ccb8f90Sopenharmony_ci    static constexpr uint32_t SCREEN_CHANGE_REPORT_INTERVAL_MS = 600000;
995ccb8f90Sopenharmony_ci
1005ccb8f90Sopenharmony_ci    static void onSuspend();
1015ccb8f90Sopenharmony_ci    static void onWakeup();
1025ccb8f90Sopenharmony_ci    static void DisplayOffTimeUpdateFunc();
1035ccb8f90Sopenharmony_ci    static void RegisterDisplayOffTimeObserver();
1045ccb8f90Sopenharmony_ci    static void UnregisterDisplayOffTimeObserver();
1055ccb8f90Sopenharmony_ci
1065ccb8f90Sopenharmony_ci    bool Init();
1075ccb8f90Sopenharmony_ci    void InitState();
1085ccb8f90Sopenharmony_ci    void SuspendDeviceInner(
1095ccb8f90Sopenharmony_ci        pid_t pid, int64_t callTimeMs, SuspendDeviceType type, bool suspendImmed, bool ignoreScreenState = false);
1105ccb8f90Sopenharmony_ci    void WakeupDeviceInner(
1115ccb8f90Sopenharmony_ci        pid_t pid, int64_t callTimeMs, WakeupDeviceType type, const std::string& details, const std::string& pkgName);
1125ccb8f90Sopenharmony_ci    void HandlePreBrightWakeUp(int64_t callTimeMs, WakeupDeviceType type, const std::string& details,
1135ccb8f90Sopenharmony_ci        const std::string& pkgName, bool timeoutTriggered = false);
1145ccb8f90Sopenharmony_ci    void RefreshActivityInner(pid_t pid, int64_t callTimeMs, UserActivityType type, bool needChangeBacklight);
1155ccb8f90Sopenharmony_ci    bool CheckRefreshTime();
1165ccb8f90Sopenharmony_ci    bool OverrideScreenOffTimeInner(int64_t timeout);
1175ccb8f90Sopenharmony_ci    bool RestoreScreenOffTimeInner();
1185ccb8f90Sopenharmony_ci    void ReceiveScreenEvent(bool isScreenOn);
1195ccb8f90Sopenharmony_ci    bool IsScreenOn(bool needPrintLog = true);
1205ccb8f90Sopenharmony_ci    bool IsFoldScreenOn();
1215ccb8f90Sopenharmony_ci    bool IsCollaborationScreenOn();
1225ccb8f90Sopenharmony_ci    bool CheckFFRTTaskAvailability(PowerState state, StateChangeReason reason) const;
1235ccb8f90Sopenharmony_ci    bool IsTimeoutReason(StateChangeReason reason) const;
1245ccb8f90Sopenharmony_ci    void Reset();
1255ccb8f90Sopenharmony_ci    int64_t GetSleepTime();
1265ccb8f90Sopenharmony_ci#ifdef MSDP_MOVEMENT_ENABLE
1275ccb8f90Sopenharmony_ci    bool IsMovementStateOn();
1285ccb8f90Sopenharmony_ci#endif
1295ccb8f90Sopenharmony_ci
1305ccb8f90Sopenharmony_ci    PowerState GetState()
1315ccb8f90Sopenharmony_ci    {
1325ccb8f90Sopenharmony_ci        return currentState_;
1335ccb8f90Sopenharmony_ci    };
1345ccb8f90Sopenharmony_ci    const std::shared_ptr<IDeviceStateAction>& GetStateAction()
1355ccb8f90Sopenharmony_ci    {
1365ccb8f90Sopenharmony_ci        return stateAction_;
1375ccb8f90Sopenharmony_ci    };
1385ccb8f90Sopenharmony_ci    bool ForceSuspendDeviceInner(pid_t pid, int64_t callTimeMs);
1395ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4
1405ccb8f90Sopenharmony_ci    bool HibernateInner(bool clearMemory);
1415ccb8f90Sopenharmony_ci#endif
1425ccb8f90Sopenharmony_ci    void RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true);
1435ccb8f90Sopenharmony_ci    void UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
1445ccb8f90Sopenharmony_ci    void SetDelayTimer(int64_t delayTime, int32_t event);
1455ccb8f90Sopenharmony_ci    void CancelDelayTimer(int32_t event);
1465ccb8f90Sopenharmony_ci    void ResetInactiveTimer(bool needPrintLog = true);
1475ccb8f90Sopenharmony_ci    void ResetSleepTimer();
1485ccb8f90Sopenharmony_ci    void SetAutoSuspend(SuspendDeviceType type, uint32_t delay);
1495ccb8f90Sopenharmony_ci    bool SetState(PowerState state, StateChangeReason reason, bool force = false);
1505ccb8f90Sopenharmony_ci    bool TryToCancelScreenOff();
1515ccb8f90Sopenharmony_ci    void BeginPowerkeyScreenOff();
1525ccb8f90Sopenharmony_ci    void EndPowerkeyScreenOff();
1535ccb8f90Sopenharmony_ci    void SetDisplaySuspend(bool enable);
1545ccb8f90Sopenharmony_ci    StateChangeReason GetReasonByUserActivity(UserActivityType type);
1555ccb8f90Sopenharmony_ci    StateChangeReason GetReasonByWakeType(WakeupDeviceType type);
1565ccb8f90Sopenharmony_ci    StateChangeReason GetReasonBySuspendType(SuspendDeviceType type);
1575ccb8f90Sopenharmony_ci    WakeupDeviceType ParseWakeupDeviceType(const std::string& details);
1585ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
1595ccb8f90Sopenharmony_ci    int32_t GetExternalScreenNumber() const;
1605ccb8f90Sopenharmony_ci    void IncreaseExternalScreenNumber();
1615ccb8f90Sopenharmony_ci    void DecreaseExternalScreenNumber();
1625ccb8f90Sopenharmony_ci#endif
1635ccb8f90Sopenharmony_ci
1645ccb8f90Sopenharmony_ci    // only use for test
1655ccb8f90Sopenharmony_ci    int64_t GetLastSuspendDeviceTime() const
1665ccb8f90Sopenharmony_ci    {
1675ccb8f90Sopenharmony_ci        return mDeviceState_.lastSuspendDeviceTime;
1685ccb8f90Sopenharmony_ci    }
1695ccb8f90Sopenharmony_ci    int64_t GetLastWakeupDeviceTime() const
1705ccb8f90Sopenharmony_ci    {
1715ccb8f90Sopenharmony_ci        return mDeviceState_.lastWakeupDeviceTime;
1725ccb8f90Sopenharmony_ci    }
1735ccb8f90Sopenharmony_ci    int64_t GetLastRefreshActivityTime() const
1745ccb8f90Sopenharmony_ci    {
1755ccb8f90Sopenharmony_ci        return mDeviceState_.lastRefreshActivityTime;
1765ccb8f90Sopenharmony_ci    }
1775ccb8f90Sopenharmony_ci    int64_t GetLastWakeupEventTime() const
1785ccb8f90Sopenharmony_ci    {
1795ccb8f90Sopenharmony_ci        return mDeviceState_.lastWakeupEventTime;
1805ccb8f90Sopenharmony_ci    }
1815ccb8f90Sopenharmony_ci    void SetSwitchState(bool switchOpen)
1825ccb8f90Sopenharmony_ci    {
1835ccb8f90Sopenharmony_ci        switchOpen_ = switchOpen;
1845ccb8f90Sopenharmony_ci    }
1855ccb8f90Sopenharmony_ci    bool IsSwitchOpen() const
1865ccb8f90Sopenharmony_ci    {
1875ccb8f90Sopenharmony_ci        return switchOpen_;
1885ccb8f90Sopenharmony_ci    }
1895ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4
1905ccb8f90Sopenharmony_ci    bool IsHibernating() const
1915ccb8f90Sopenharmony_ci    {
1925ccb8f90Sopenharmony_ci        return hibernating_;
1935ccb8f90Sopenharmony_ci    }
1945ccb8f90Sopenharmony_ci#endif
1955ccb8f90Sopenharmony_ci    int64_t GetLastOnTime() const
1965ccb8f90Sopenharmony_ci    {
1975ccb8f90Sopenharmony_ci        return mDeviceState_.screenState.lastOnTime;
1985ccb8f90Sopenharmony_ci    }
1995ccb8f90Sopenharmony_ci
2005ccb8f90Sopenharmony_ci    void DumpInfo(std::string& result);
2015ccb8f90Sopenharmony_ci    void EnableMock(IDeviceStateAction* mockAction);
2025ccb8f90Sopenharmony_ci    int64_t GetDisplayOffTime();
2035ccb8f90Sopenharmony_ci    int64_t GetDimTime(int64_t displayOffTime);
2045ccb8f90Sopenharmony_ci    void SetDisplayOffTime(int64_t time, bool needUpdateSetting = true);
2055ccb8f90Sopenharmony_ci    void SetSleepTime(int64_t time);
2065ccb8f90Sopenharmony_ci    bool IsRunningLockEnabled(RunningLockType type);
2075ccb8f90Sopenharmony_ci    void SetForceTimingOut(bool enabled);
2085ccb8f90Sopenharmony_ci    void LockScreenAfterTimingOut(bool enabled, bool checkScreenOnLock, bool sendScreenOffEvent);
2095ccb8f90Sopenharmony_ci    bool IsSettingState(PowerState state);
2105ccb8f90Sopenharmony_ci
2115ccb8f90Sopenharmony_ciprivate:
2125ccb8f90Sopenharmony_ci    enum PreBrightState : uint32_t {
2135ccb8f90Sopenharmony_ci        PRE_BRIGHT_UNSTART = 0,
2145ccb8f90Sopenharmony_ci        PRE_BRIGHT_STARTED,
2155ccb8f90Sopenharmony_ci        PRE_BRIGHT_FINISHED,
2165ccb8f90Sopenharmony_ci    };
2175ccb8f90Sopenharmony_ci
2185ccb8f90Sopenharmony_ci    class SettingStateFlag {
2195ccb8f90Sopenharmony_ci    public:
2205ccb8f90Sopenharmony_ci        SettingStateFlag(PowerState state, std::shared_ptr<PowerStateMachine> owner, StateChangeReason reason)
2215ccb8f90Sopenharmony_ci            : owner_(owner)
2225ccb8f90Sopenharmony_ci        {
2235ccb8f90Sopenharmony_ci            std::shared_ptr<PowerStateMachine> stateMachine = owner_.lock();
2245ccb8f90Sopenharmony_ci            int64_t flag;
2255ccb8f90Sopenharmony_ci            if (state == PowerState::DIM && reason == StateChangeReason::STATE_CHANGE_REASON_COORDINATION) {
2265ccb8f90Sopenharmony_ci                flag = static_cast<int64_t>(StateFlag::FORCE_SETTING_DIM);
2275ccb8f90Sopenharmony_ci            } else {
2285ccb8f90Sopenharmony_ci                flag = static_cast<int64_t>(state);
2295ccb8f90Sopenharmony_ci            }
2305ccb8f90Sopenharmony_ci            stateMachine->settingStateFlag_ = flag;
2315ccb8f90Sopenharmony_ci        }
2325ccb8f90Sopenharmony_ci        SettingStateFlag(const SettingStateFlag&) = delete;
2335ccb8f90Sopenharmony_ci        SettingStateFlag& operator=(const SettingStateFlag&) = delete;
2345ccb8f90Sopenharmony_ci        SettingStateFlag(SettingStateFlag&&) = delete;
2355ccb8f90Sopenharmony_ci        SettingStateFlag& operator=(SettingStateFlag&&) = delete;
2365ccb8f90Sopenharmony_ci        ~SettingStateFlag()
2375ccb8f90Sopenharmony_ci        {
2385ccb8f90Sopenharmony_ci            std::shared_ptr<PowerStateMachine> stateMachine = owner_.lock();
2395ccb8f90Sopenharmony_ci            stateMachine->settingStateFlag_ = static_cast<int64_t>(StateFlag::NONE);
2405ccb8f90Sopenharmony_ci        }
2415ccb8f90Sopenharmony_ci        enum class StateFlag : int64_t {
2425ccb8f90Sopenharmony_ci            FORCE_SETTING_DIM = -3,
2435ccb8f90Sopenharmony_ci            SETTING_DIM_INTERRUPTED = -2,
2445ccb8f90Sopenharmony_ci            NONE = -1
2455ccb8f90Sopenharmony_ci        };
2465ccb8f90Sopenharmony_ci    protected:
2475ccb8f90Sopenharmony_ci        std::weak_ptr<PowerStateMachine> owner_;
2485ccb8f90Sopenharmony_ci    };
2495ccb8f90Sopenharmony_ci    class StateController {
2505ccb8f90Sopenharmony_ci    public:
2515ccb8f90Sopenharmony_ci        StateController(PowerState state, std::shared_ptr<PowerStateMachine> owner,
2525ccb8f90Sopenharmony_ci            std::function<TransitResult(StateChangeReason)> action)
2535ccb8f90Sopenharmony_ci            : state_(state),
2545ccb8f90Sopenharmony_ci            owner_(owner), action_(action)
2555ccb8f90Sopenharmony_ci        {
2565ccb8f90Sopenharmony_ci        }
2575ccb8f90Sopenharmony_ci        ~StateController() = default;
2585ccb8f90Sopenharmony_ci        PowerState GetState()
2595ccb8f90Sopenharmony_ci        {
2605ccb8f90Sopenharmony_ci            return state_;
2615ccb8f90Sopenharmony_ci        }
2625ccb8f90Sopenharmony_ci        TransitResult TransitTo(StateChangeReason reason, bool ignoreLock = false);
2635ccb8f90Sopenharmony_ci        void RecordFailure(PowerState from, StateChangeReason trigger, TransitResult failReason);
2645ccb8f90Sopenharmony_ci        static bool IsReallyFailed(StateChangeReason reason);
2655ccb8f90Sopenharmony_ci        StateChangeReason lastReason_;
2665ccb8f90Sopenharmony_ci        int64_t lastTime_ {0};
2675ccb8f90Sopenharmony_ci        PowerState failFrom_;
2685ccb8f90Sopenharmony_ci        StateChangeReason failTrigger_;
2695ccb8f90Sopenharmony_ci        std::string failReason_;
2705ccb8f90Sopenharmony_ci        int64_t failTime_ {0};
2715ccb8f90Sopenharmony_ci
2725ccb8f90Sopenharmony_ci    protected:
2735ccb8f90Sopenharmony_ci        bool CheckState();
2745ccb8f90Sopenharmony_ci        bool NeedNotify(PowerState currentState);
2755ccb8f90Sopenharmony_ci        void MatchState(PowerState& currentState, DisplayState state);
2765ccb8f90Sopenharmony_ci        void CorrectState(PowerState& currentState, PowerState correctState, DisplayState state);
2775ccb8f90Sopenharmony_ci        PowerState state_;
2785ccb8f90Sopenharmony_ci        std::weak_ptr<PowerStateMachine> owner_;
2795ccb8f90Sopenharmony_ci        std::function<TransitResult(StateChangeReason)> action_;
2805ccb8f90Sopenharmony_ci    };
2815ccb8f90Sopenharmony_ci
2825ccb8f90Sopenharmony_ci    struct classcomp {
2835ccb8f90Sopenharmony_ci        bool operator()(const sptr<IPowerStateCallback>& l, const sptr<IPowerStateCallback>& r) const
2845ccb8f90Sopenharmony_ci        {
2855ccb8f90Sopenharmony_ci            return l->AsObject() < r->AsObject();
2865ccb8f90Sopenharmony_ci        }
2875ccb8f90Sopenharmony_ci    };
2885ccb8f90Sopenharmony_ci
2895ccb8f90Sopenharmony_ci    class ScreenChangeCheck {
2905ccb8f90Sopenharmony_ci    public:
2915ccb8f90Sopenharmony_ci        ScreenChangeCheck(std::shared_ptr<FFRTTimer> ffrtTimer, PowerState state, StateChangeReason reason);
2925ccb8f90Sopenharmony_ci        void Finish(TransitResult result);
2935ccb8f90Sopenharmony_ci    private:
2945ccb8f90Sopenharmony_ci        void Report(const std::string &msg);
2955ccb8f90Sopenharmony_ci        std::shared_ptr<FFRTTimer> timer_ {nullptr};
2965ccb8f90Sopenharmony_ci        pid_t pid_ {-1};
2975ccb8f90Sopenharmony_ci        pid_t uid_ {-1};
2985ccb8f90Sopenharmony_ci        PowerState state_;
2995ccb8f90Sopenharmony_ci        StateChangeReason reason_;
3005ccb8f90Sopenharmony_ci    };
3015ccb8f90Sopenharmony_ci
3025ccb8f90Sopenharmony_ci    static std::string GetTransitResultString(TransitResult result);
3035ccb8f90Sopenharmony_ci
3045ccb8f90Sopenharmony_ci    void UpdateSettingStateFlag(PowerState state, StateChangeReason reason);
3055ccb8f90Sopenharmony_ci    void RestoreSettingStateFlag();
3065ccb8f90Sopenharmony_ci    void InitStateMap();
3075ccb8f90Sopenharmony_ci    void EmplaceAwake();
3085ccb8f90Sopenharmony_ci    void EmplaceFreeze();
3095ccb8f90Sopenharmony_ci    void EmplaceInactive();
3105ccb8f90Sopenharmony_ci    void EmplaceStandBy();
3115ccb8f90Sopenharmony_ci    void EmplaceDoze();
3125ccb8f90Sopenharmony_ci    void EmplaceSleep();
3135ccb8f90Sopenharmony_ci    void EmplaceHibernate();
3145ccb8f90Sopenharmony_ci    void EmplaceShutdown();
3155ccb8f90Sopenharmony_ci    void EmplaceDim();
3165ccb8f90Sopenharmony_ci    void InitTransitMap();
3175ccb8f90Sopenharmony_ci    bool CanTransitTo(PowerState to, StateChangeReason reason);
3185ccb8f90Sopenharmony_ci    void NotifyPowerStateChanged(PowerState state,
3195ccb8f90Sopenharmony_ci        StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_APPLICATION);
3205ccb8f90Sopenharmony_ci    void SendEventToPowerMgrNotify(PowerState state, int64_t callTime);
3215ccb8f90Sopenharmony_ci    bool CheckRunningLock(PowerState state);
3225ccb8f90Sopenharmony_ci    void HandleActivityTimeout();
3235ccb8f90Sopenharmony_ci    void HandleActivitySleepTimeout();
3245ccb8f90Sopenharmony_ci    void HandleSystemWakeup();
3255ccb8f90Sopenharmony_ci    void AppendDumpInfo(std::string& result, std::string& reason, std::string& time);
3265ccb8f90Sopenharmony_ci    std::shared_ptr<StateController> GetStateController(PowerState state);
3275ccb8f90Sopenharmony_ci    void ResetScreenOffPreTimeForSwing(int64_t displayOffTime);
3285ccb8f90Sopenharmony_ci    void ShowCurrentScreenLocks();
3295ccb8f90Sopenharmony_ci    void HandleProximityScreenOffTimer(PowerState state, StateChangeReason reason);
3305ccb8f90Sopenharmony_ci    bool HandlePreBrightState(StateChangeReason reason);
3315ccb8f90Sopenharmony_ci    bool IsPreBrightAuthReason(StateChangeReason reason);
3325ccb8f90Sopenharmony_ci    bool IsPreBrightWakeUp(WakeupDeviceType type);
3335ccb8f90Sopenharmony_ci    bool NeedShowScreenLocks(PowerState state);
3345ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4
3355ccb8f90Sopenharmony_ci    bool PrepareHibernate(bool clearMemory);
3365ccb8f90Sopenharmony_ci    uint32_t GetPreHibernateDelay();
3375ccb8f90Sopenharmony_ci#endif
3385ccb8f90Sopenharmony_ci#ifdef HAS_SENSORS_SENSOR_PART
3395ccb8f90Sopenharmony_ci    bool IsProximityClose();
3405ccb8f90Sopenharmony_ci#endif
3415ccb8f90Sopenharmony_ci
3425ccb8f90Sopenharmony_ci    const wptr<PowerMgrService> pms_;
3435ccb8f90Sopenharmony_ci    std::shared_ptr<FFRTTimer> ffrtTimer_ {nullptr};
3445ccb8f90Sopenharmony_ci    PowerState currentState_;
3455ccb8f90Sopenharmony_ci    std::map<PowerState, std::shared_ptr<std::vector<RunningLockType>>> lockMap_;
3465ccb8f90Sopenharmony_ci    std::map<PowerState, std::shared_ptr<StateController>> controllerMap_;
3475ccb8f90Sopenharmony_ci    std::mutex mutex_;
3485ccb8f90Sopenharmony_ci    // all change to currentState_ should be inside stateMutex_
3495ccb8f90Sopenharmony_ci    std::mutex stateMutex_;
3505ccb8f90Sopenharmony_ci    DevicePowerState mDeviceState_;
3515ccb8f90Sopenharmony_ci    sptr<IRemoteObject::DeathRecipient> powerStateCBDeathRecipient_;
3525ccb8f90Sopenharmony_ci    std::set<const sptr<IPowerStateCallback>, classcomp> syncPowerStateListeners_;
3535ccb8f90Sopenharmony_ci    std::set<const sptr<IPowerStateCallback>, classcomp> asyncPowerStateListeners_;
3545ccb8f90Sopenharmony_ci    std::shared_ptr<IDeviceStateAction> stateAction_;
3555ccb8f90Sopenharmony_ci
3565ccb8f90Sopenharmony_ci    std::atomic<int64_t> displayOffTime_ {DEFAULT_DISPLAY_OFF_TIME_MS};
3575ccb8f90Sopenharmony_ci    int64_t sleepTime_ {DEFAULT_SLEEP_TIME_MS};
3585ccb8f90Sopenharmony_ci    bool enableDisplaySuspend_ {false};
3595ccb8f90Sopenharmony_ci    bool isScreenOffTimeOverride_ {false};
3605ccb8f90Sopenharmony_ci    std::unordered_map<PowerState, std::set<PowerState>> forbidMap_;
3615ccb8f90Sopenharmony_ci    std::atomic<bool> switchOpen_ {true};
3625ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_POWER_ENABLE_S4
3635ccb8f90Sopenharmony_ci    std::atomic<bool> hibernating_ {false};
3645ccb8f90Sopenharmony_ci#endif
3655ccb8f90Sopenharmony_ci    std::unordered_map<StateChangeReason, std::unordered_map<PowerState, std::set<PowerState>>> allowMapByReason_;
3665ccb8f90Sopenharmony_ci    std::atomic<bool> forceTimingOut_ {false};
3675ccb8f90Sopenharmony_ci    std::atomic<bool> enabledTimingOutLockScreen_ {true};
3685ccb8f90Sopenharmony_ci    std::atomic<bool> enabledTimingOutLockScreenCheckLock_ {false};
3695ccb8f90Sopenharmony_ci    std::atomic<bool> enabledScreenOffEvent_{true};
3705ccb8f90Sopenharmony_ci    std::atomic<int64_t> settingStateFlag_ {-1};
3715ccb8f90Sopenharmony_ci    std::atomic<bool> settingOnStateFlag_ {false};
3725ccb8f90Sopenharmony_ci    std::atomic<bool> settingOffStateFlag_ {false};
3735ccb8f90Sopenharmony_ci    std::atomic<bool> isAwakeNotified_ {false};
3745ccb8f90Sopenharmony_ci    std::atomic<PreBrightState> preBrightState_ {PRE_BRIGHT_UNSTART};
3755ccb8f90Sopenharmony_ci    std::atomic<bool> proximityScreenOffTimerStarted_ {false};
3765ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
3775ccb8f90Sopenharmony_ci    std::atomic<int32_t> externalScreenNumber_ {0};
3785ccb8f90Sopenharmony_ci#endif
3795ccb8f90Sopenharmony_ci};
3805ccb8f90Sopenharmony_ci} // namespace PowerMgr
3815ccb8f90Sopenharmony_ci} // namespace OHOS
3825ccb8f90Sopenharmony_ci#endif // POWERMGR_POWER_STATE_MACHINE_H
383