1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_EXT_INCLUDE_BASE_STATE_H 17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_EXT_INCLUDE_BASE_STATE_H 18 19 #include <memory> 20 #include <stdint.h> 21 #include <unordered_map> 22 23 #include "event_handler.h" 24 #ifdef STANDBY_POWER_MANAGER_ENABLE 25 #include "power_mgr_client.h" 26 #endif 27 #include "common_constant.h" 28 #include "standby_service_errors.h" 29 #include "standby_messsage.h" 30 #include "istrategy_manager_adapter.h" 31 #include "standby_state.h" 32 #include "timed_task.h" 33 34 namespace OHOS { 35 namespace DevStandbyMgr { 36 namespace { 37 const uint32_t THREE_BYTES_LEN = 24; 38 const uint32_t TWO_BYTES_LEN = 16; 39 const uint32_t ONE_BYTE_LEN = 8; 40 } 41 42 struct ConstraintEvalParam { 43 uint32_t curState_ {0}; 44 uint32_t curPhase_ {0}; 45 uint32_t nextState_ {0}; 46 uint32_t nextPhase_ {0}; 47 bool isRepeatedDetection_ {false}; 48 ConstraintEvalParam() = default; ConstraintEvalParamConstraintEvalParam49 ConstraintEvalParam(uint32_t curState, uint32_t curPhase, uint32_t nextState, uint32_t nextPhase) 50 : curState_(curState), curPhase_(curPhase), nextState_(nextState), nextPhase_(nextPhase) {} GetHashValueConstraintEvalParam51 inline uint32_t GetHashValue() const 52 { 53 uint32_t res = (curState_ << THREE_BYTES_LEN) + (curPhase_ << TWO_BYTES_LEN) + 54 (nextState_ << ONE_BYTE_LEN) + (nextPhase_ << 1); 55 if (isRepeatedDetection_) { 56 return res + 1; 57 } 58 return res; 59 } 60 }; 61 62 class IStateManagerAdapter; 63 struct StandbyMessage; 64 class BaseState { 65 public: BaseState(uint32_t curState,uint32_t curPhase,const std::shared_ptr<IStateManagerAdapter> & stateManager,std::shared_ptr<AppExecFwk::EventHandler> & handler)66 BaseState(uint32_t curState, uint32_t curPhase, const std::shared_ptr<IStateManagerAdapter>& 67 stateManager, std::shared_ptr<AppExecFwk::EventHandler>& handler): curState_(curState), 68 curPhase_(curPhase), stateManager_(stateManager), handler_(handler) {} 69 virtual ~BaseState() = default; 70 virtual ErrCode Init(const std::shared_ptr<BaseState>& statePtr); 71 virtual ErrCode UnInit(); 72 virtual uint32_t GetCurState(); 73 virtual uint32_t GetCurInnerPhase(); 74 virtual ErrCode BeginState() = 0; 75 virtual ErrCode EndState() = 0; 76 77 virtual bool CheckTransitionValid(uint32_t nextState) = 0; 78 virtual void EndEvalCurrentState(bool evalResult) = 0; 79 virtual void StartTransitNextState(const std::shared_ptr<BaseState>& statePtr); 80 virtual void TransitToPhase(uint32_t curPhase, uint32_t nextPhase); 81 virtual void TransitToPhaseInner(uint32_t prePhase, uint32_t curPhase); 82 virtual bool IsInFinalPhase(); 83 virtual void OnStateBlocked(); 84 virtual void ShellDump(const std::vector<std::string>& argsInStr, std::string& result); 85 86 virtual void SetTimedTask(const std::string& timedTaskName, uint64_t timedTaskId); 87 virtual ErrCode StartStateTransitionTimer(int64_t triggerTime); 88 virtual ErrCode StopTimedTask(const std::string& timedTaskName); 89 virtual void DestroyAllTimedTask(); 90 91 static void InitRunningLock(); 92 static void AcquireStandbyRunningLock(); 93 static void ReleaseStandbyRunningLock(); 94 protected: 95 uint32_t curState_ {0}; 96 uint32_t curPhase_ {0}; 97 uint32_t nextState_ {0}; 98 std::weak_ptr<IStateManagerAdapter> stateManager_ {}; 99 std::shared_ptr<AppExecFwk::EventHandler> &handler_; 100 uint64_t enterStandbyTimerId_ {}; 101 std::unordered_map<std::string, uint64_t> timedTaskMap_ {}; 102 static bool runningLockStatus_; 103 #ifdef STANDBY_POWER_MANAGER_ENABLE 104 static std::shared_ptr<PowerMgr::RunningLock> standbyRunningLock_; 105 #endif 106 }; 107 108 class StateWithMaint { 109 protected: 110 virtual int64_t CalculateMaintTimeOut(const std::shared_ptr<IStateManagerAdapter>& 111 stateManagerPtr, bool isFirstInterval); 112 protected: 113 int32_t maintIntervalIndex_ {0}; 114 std::vector<int32_t> maintInterval_ {}; 115 }; 116 } // namespace DevStandbyMgr 117 } // namespace OHOS 118 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_PLUGINS_EXT_INCLUDE_BASE_STATE_H