199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci
1699552fe9Sopenharmony_ci#ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_EXT_INCLUDE_BASE_STATE_H
1799552fe9Sopenharmony_ci#define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_EXT_INCLUDE_BASE_STATE_H
1899552fe9Sopenharmony_ci
1999552fe9Sopenharmony_ci#include <memory>
2099552fe9Sopenharmony_ci#include <stdint.h>
2199552fe9Sopenharmony_ci#include <unordered_map>
2299552fe9Sopenharmony_ci
2399552fe9Sopenharmony_ci#include "event_handler.h"
2499552fe9Sopenharmony_ci#ifdef STANDBY_POWER_MANAGER_ENABLE
2599552fe9Sopenharmony_ci#include "power_mgr_client.h"
2699552fe9Sopenharmony_ci#endif
2799552fe9Sopenharmony_ci#include "common_constant.h"
2899552fe9Sopenharmony_ci#include "standby_service_errors.h"
2999552fe9Sopenharmony_ci#include "standby_messsage.h"
3099552fe9Sopenharmony_ci#include "istrategy_manager_adapter.h"
3199552fe9Sopenharmony_ci#include "standby_state.h"
3299552fe9Sopenharmony_ci#include "timed_task.h"
3399552fe9Sopenharmony_ci
3499552fe9Sopenharmony_cinamespace OHOS {
3599552fe9Sopenharmony_cinamespace DevStandbyMgr {
3699552fe9Sopenharmony_cinamespace {
3799552fe9Sopenharmony_ci    const uint32_t THREE_BYTES_LEN = 24;
3899552fe9Sopenharmony_ci    const uint32_t TWO_BYTES_LEN = 16;
3999552fe9Sopenharmony_ci    const uint32_t ONE_BYTE_LEN = 8;
4099552fe9Sopenharmony_ci}
4199552fe9Sopenharmony_ci
4299552fe9Sopenharmony_cistruct ConstraintEvalParam {
4399552fe9Sopenharmony_ci    uint32_t curState_ {0};
4499552fe9Sopenharmony_ci    uint32_t curPhase_ {0};
4599552fe9Sopenharmony_ci    uint32_t nextState_ {0};
4699552fe9Sopenharmony_ci    uint32_t nextPhase_ {0};
4799552fe9Sopenharmony_ci    bool isRepeatedDetection_ {false};
4899552fe9Sopenharmony_ci    ConstraintEvalParam() = default;
4999552fe9Sopenharmony_ci    ConstraintEvalParam(uint32_t curState, uint32_t curPhase, uint32_t nextState, uint32_t nextPhase)
5099552fe9Sopenharmony_ci        : curState_(curState), curPhase_(curPhase), nextState_(nextState), nextPhase_(nextPhase) {}
5199552fe9Sopenharmony_ci    inline uint32_t GetHashValue() const
5299552fe9Sopenharmony_ci    {
5399552fe9Sopenharmony_ci        uint32_t res = (curState_ << THREE_BYTES_LEN) + (curPhase_ << TWO_BYTES_LEN) +
5499552fe9Sopenharmony_ci            (nextState_ << ONE_BYTE_LEN) + (nextPhase_ << 1);
5599552fe9Sopenharmony_ci        if (isRepeatedDetection_) {
5699552fe9Sopenharmony_ci            return res + 1;
5799552fe9Sopenharmony_ci        }
5899552fe9Sopenharmony_ci        return res;
5999552fe9Sopenharmony_ci    }
6099552fe9Sopenharmony_ci};
6199552fe9Sopenharmony_ci
6299552fe9Sopenharmony_ciclass IStateManagerAdapter;
6399552fe9Sopenharmony_cistruct StandbyMessage;
6499552fe9Sopenharmony_ciclass BaseState {
6599552fe9Sopenharmony_cipublic:
6699552fe9Sopenharmony_ci    BaseState(uint32_t curState, uint32_t curPhase,  const std::shared_ptr<IStateManagerAdapter>&
6799552fe9Sopenharmony_ci        stateManager, std::shared_ptr<AppExecFwk::EventHandler>& handler): curState_(curState),
6899552fe9Sopenharmony_ci        curPhase_(curPhase), stateManager_(stateManager), handler_(handler) {}
6999552fe9Sopenharmony_ci    virtual ~BaseState() = default;
7099552fe9Sopenharmony_ci    virtual ErrCode Init(const std::shared_ptr<BaseState>& statePtr);
7199552fe9Sopenharmony_ci    virtual ErrCode UnInit();
7299552fe9Sopenharmony_ci    virtual uint32_t GetCurState();
7399552fe9Sopenharmony_ci    virtual uint32_t GetCurInnerPhase();
7499552fe9Sopenharmony_ci    virtual ErrCode BeginState() = 0;
7599552fe9Sopenharmony_ci    virtual ErrCode EndState() = 0;
7699552fe9Sopenharmony_ci
7799552fe9Sopenharmony_ci    virtual bool CheckTransitionValid(uint32_t nextState) = 0;
7899552fe9Sopenharmony_ci    virtual void EndEvalCurrentState(bool evalResult) = 0;
7999552fe9Sopenharmony_ci    virtual void StartTransitNextState(const std::shared_ptr<BaseState>& statePtr);
8099552fe9Sopenharmony_ci    virtual void TransitToPhase(uint32_t curPhase, uint32_t nextPhase);
8199552fe9Sopenharmony_ci    virtual void TransitToPhaseInner(uint32_t prePhase, uint32_t curPhase);
8299552fe9Sopenharmony_ci    virtual bool IsInFinalPhase();
8399552fe9Sopenharmony_ci    virtual void OnStateBlocked();
8499552fe9Sopenharmony_ci    virtual void ShellDump(const std::vector<std::string>& argsInStr, std::string& result);
8599552fe9Sopenharmony_ci
8699552fe9Sopenharmony_ci    virtual void SetTimedTask(const std::string& timedTaskName, uint64_t timedTaskId);
8799552fe9Sopenharmony_ci    virtual ErrCode StartStateTransitionTimer(int64_t triggerTime);
8899552fe9Sopenharmony_ci    virtual ErrCode StopTimedTask(const std::string& timedTaskName);
8999552fe9Sopenharmony_ci    virtual void DestroyAllTimedTask();
9099552fe9Sopenharmony_ci
9199552fe9Sopenharmony_ci    static void InitRunningLock();
9299552fe9Sopenharmony_ci    static void AcquireStandbyRunningLock();
9399552fe9Sopenharmony_ci    static void ReleaseStandbyRunningLock();
9499552fe9Sopenharmony_ciprotected:
9599552fe9Sopenharmony_ci    uint32_t curState_ {0};
9699552fe9Sopenharmony_ci    uint32_t curPhase_ {0};
9799552fe9Sopenharmony_ci    uint32_t nextState_ {0};
9899552fe9Sopenharmony_ci    std::weak_ptr<IStateManagerAdapter> stateManager_ {};
9999552fe9Sopenharmony_ci    std::shared_ptr<AppExecFwk::EventHandler> &handler_;
10099552fe9Sopenharmony_ci    uint64_t enterStandbyTimerId_ {};
10199552fe9Sopenharmony_ci    std::unordered_map<std::string, uint64_t> timedTaskMap_ {};
10299552fe9Sopenharmony_ci    static bool runningLockStatus_;
10399552fe9Sopenharmony_ci    #ifdef STANDBY_POWER_MANAGER_ENABLE
10499552fe9Sopenharmony_ci    static std::shared_ptr<PowerMgr::RunningLock> standbyRunningLock_;
10599552fe9Sopenharmony_ci    #endif
10699552fe9Sopenharmony_ci};
10799552fe9Sopenharmony_ci
10899552fe9Sopenharmony_ciclass StateWithMaint {
10999552fe9Sopenharmony_ciprotected:
11099552fe9Sopenharmony_ci    virtual int64_t CalculateMaintTimeOut(const std::shared_ptr<IStateManagerAdapter>&
11199552fe9Sopenharmony_ci        stateManagerPtr, bool isFirstInterval);
11299552fe9Sopenharmony_ciprotected:
11399552fe9Sopenharmony_ci    int32_t maintIntervalIndex_ {0};
11499552fe9Sopenharmony_ci    std::vector<int32_t> maintInterval_ {};
11599552fe9Sopenharmony_ci};
11699552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
11799552fe9Sopenharmony_ci}  // namespace OHOS
11899552fe9Sopenharmony_ci#endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_EXT_INCLUDE_BASE_STATE_H