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 POWERMGR_SUSPEND_SOURCES_H 17 #define POWERMGR_SUSPEND_SOURCES_H 18 19 #include <cstdint> 20 #include <vector> 21 #include <mutex> 22 23 #include "power_state_machine_info.h" 24 25 namespace OHOS { 26 namespace PowerMgr { 27 enum class SuspendAction : uint32_t { 28 ACTION_NONE = 0, // no action 29 ACTION_AUTO_SUSPEND, // automatically enter sleep 30 ACTION_FORCE_SUSPEND, // force enter sleep 31 ACTION_HIBERNATE, // entry hibernate 32 ACTION_SHUTDOWN, // shutdown 33 ACTION_INVALID, 34 ACTION_MAC = ACTION_INVALID 35 }; 36 37 class SuspendSource { 38 public: 39 static const constexpr char* ACTION_KEY = "action"; 40 static const constexpr char* DELAY_KEY = "delayMs"; 41 SuspendSource(SuspendDeviceType reason,uint32_t action,uint32_t delay)42 SuspendSource(SuspendDeviceType reason, uint32_t action, uint32_t delay) 43 { 44 reason_ = reason; 45 action_ = action; 46 delayMs_ = delay; 47 } 48 ~SuspendSource() = default; GetReason()49 SuspendDeviceType GetReason() const 50 { 51 return reason_; 52 } GetAction()53 uint32_t GetAction() const 54 { 55 return action_; 56 } GetDelay()57 uint32_t GetDelay() const 58 { 59 return delayMs_; 60 } 61 62 private: 63 SuspendDeviceType reason_; 64 uint32_t action_; 65 uint32_t delayMs_; 66 }; 67 68 class SuspendSources { 69 public: 70 SuspendSources() = default; 71 ~SuspendSources() = default; 72 73 static const constexpr char* POWERKEY_KEY = "powerkey"; 74 static const constexpr char* TIMEOUT_KEY = "timeout"; 75 static const constexpr char* LID_KEY = "lid"; 76 static const constexpr char* SWITCH_KEY = "switch"; 77 static const constexpr char* TP_COVER_KEY = "tp_cover"; 78 79 static SuspendDeviceType mapSuspendDeviceType(const std::string& key); 80 static std::vector<std::string> getSourceKeys(); 81 void PutSource(SuspendSource& source); 82 std::vector<SuspendSource> GetSourceList(); GetParseErrorFlag()83 bool GetParseErrorFlag() const 84 { 85 return parseErrorFlag_; 86 } 87 SetParseErrorFlag(bool flag)88 void SetParseErrorFlag(bool flag) 89 { 90 parseErrorFlag_ = flag; 91 } 92 93 private: 94 static std::mutex sourceKeysMutex_; 95 std::vector<SuspendSource> sourceList_; 96 std::mutex sourceListMutex_; 97 bool parseErrorFlag_ {false}; 98 }; 99 100 } // namespace PowerMgr 101 } // namespace OHOS 102 103 #endif // POWERMGR_SUSPEND_SOURCES_H