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     static const constexpr char* POWERKEY_KEY = "powerkey";
71     static const constexpr char* TIMEOUT_KEY = "timeout";
72     static const constexpr char* LID_KEY = "lid";
73     static const constexpr char* SWITCH_KEY = "switch";
74     SuspendSources() = default;
75     ~SuspendSources() = default;
76     static SuspendDeviceType mapSuspendDeviceType(const std::string& key);
77     static std::vector<std::string> getSourceKeys();
78     void PutSource(SuspendSource& source);
79     std::vector<SuspendSource> GetSourceList();
80 
81 private:
82     std::vector<SuspendSource> sourceList_;
83     std::mutex sourceListMutex_;
84     static std::mutex sourceKeysMutex_;
85 };
86 
87 } // namespace PowerMgr
88 } // namespace OHOS
89 
90 #endif // POWERMGR_SUSPEND_SOURCES_H