1 /*
2 * Copyright (c) 2021-2022 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 #include "power_mode_policy.h"
17
18 #include "power_log.h"
19 #include "power_save_mode.h"
20 #include "singleton.h"
21
22 using namespace std;
23
24 namespace OHOS {
25 namespace PowerMgr {
GetPowerModeValuePolicy(uint32_t type)26 int32_t PowerModePolicy::GetPowerModeValuePolicy(uint32_t type)
27 {
28 int32_t ret = INIT_VALUE_FALSE;
29 if (IsValidType(type)) {
30 ret = GetPolicyFromMap(type);
31 }
32
33 return ret;
34 }
35
GetPolicyFromMap(uint32_t type)36 int32_t PowerModePolicy::GetPolicyFromMap(uint32_t type)
37 {
38 int32_t ret = INIT_VALUE_FALSE;
39 std::lock_guard<std::mutex> lock(policyMutex_);
40 valueiter = valueModePolicy.find(type);
41 if (valueiter != valueModePolicy.end()) {
42 ret = valueiter->second;
43 }
44 return ret;
45 }
46
GetRecoverPolicyFromMap(uint32_t type)47 int32_t PowerModePolicy::GetRecoverPolicyFromMap(uint32_t type)
48 {
49 int32_t ret = INIT_VALUE_FALSE;
50 std::lock_guard<std::mutex> lock(policyMutex_);
51 recoveriter = recoverModePolicy.find(type);
52 if (recoveriter != recoverModePolicy.end()) {
53 ret = recoveriter->second;
54 POWER_HILOGD(FEATURE_POWER_MODE, "Recover value: %{public}d", ret);
55 }
56 return ret;
57 }
58
GetPowerModeRecoverPolicy(uint32_t type)59 int32_t PowerModePolicy::GetPowerModeRecoverPolicy(uint32_t type)
60 {
61 int32_t ret = INIT_VALUE_FALSE;
62 if (IsValidType(type)) {
63 ret = GetRecoverPolicyFromMap(type);
64 }
65
66 return ret;
67 }
68
SetPowerModePolicy(uint32_t mode,uint32_t lastMode)69 void PowerModePolicy::SetPowerModePolicy(uint32_t mode, uint32_t lastMode)
70 {
71 POWER_HILOGD(FEATURE_POWER_MODE, "mode=%{public}d, lastMode=%{public}d", mode, lastMode);
72 if (lastMode != LAST_MODE_FLAG) {
73 ReadRecoverPolicy(lastMode);
74 }
75
76 ReadOpenPolicy(mode);
77
78 CompareModeItem(mode, lastMode);
79 }
80
ReadOpenPolicy(uint32_t mode)81 void PowerModePolicy::ReadOpenPolicy(uint32_t mode)
82 {
83 DelayedSpSingleton<PowerSaveMode>::GetInstance()->GetValuePolicy(openPolicy, mode);
84 }
85
ReadRecoverPolicy(uint32_t mode)86 void PowerModePolicy::ReadRecoverPolicy(uint32_t mode)
87 {
88 DelayedSpSingleton<PowerSaveMode>::GetInstance()->GetRecoverPolicy(recoverPolicy, mode);
89 }
90
CompareModeItem(uint32_t mode,uint32_t lastMode)91 void PowerModePolicy::CompareModeItem(uint32_t mode, uint32_t lastMode)
92 {
93 std::lock_guard<std::mutex> lock(policyMutex_);
94 recoverModePolicy.clear();
95 valueModePolicy.clear();
96
97 for (auto openlit = openPolicy.begin(); openlit != openPolicy.end(); openlit++) {
98 valueModePolicy[(*openlit).id] = (*openlit).value;
99 }
100
101 for (recoverlit = recoverPolicy.begin(); recoverlit != recoverPolicy.end(); recoverlit++) {
102 recoverModePolicy[(*recoverlit).id] = (*recoverlit).value;
103 POWER_HILOGD(FEATURE_POWER_MODE,
104 "(*recoverlit).id=%{public}d, (*recoverlit).value=%{public}d", (*recoverlit).id, (*recoverlit).value);
105 }
106
107 openPolicy.clear();
108 recoverPolicy.clear();
109 }
110
AddAction(uint32_t type,ModeAction & action)111 void PowerModePolicy::AddAction(uint32_t type, ModeAction& action)
112 {
113 POWER_HILOGD(FEATURE_POWER_MODE, "type=%{public}d", type);
114 std::lock_guard<std::mutex> lock(actionMapMutex_);
115 actionMap.emplace(type, action);
116 }
117
TriggerAllActions(bool isBoot)118 void PowerModePolicy::TriggerAllActions(bool isBoot)
119 {
120 std::vector<ModeAction> allActions;
121 {
122 std::lock_guard<std::mutex> lock(actionMapMutex_);
123 for (auto iterator = actionMap.begin(); iterator != actionMap.end(); iterator++) {
124 POWER_HILOGD(FEATURE_POWER_MODE, "type=%{public}d", iterator->first);
125 allActions.emplace_back(iterator->second);
126 }
127 }
128 for (const auto &actions : allActions) {
129 actions(isBoot);
130 }
131 }
132
IsValidType(uint32_t type)133 bool PowerModePolicy::IsValidType(uint32_t type)
134 {
135 std::lock_guard<std::mutex> lock(actionMapMutex_);
136 auto iterator = actionMap.find(type);
137 if (iterator == actionMap.end()) {
138 POWER_HILOGW(FEATURE_POWER_MODE, "Invalid type: %{public}d", type);
139 return false;
140 }
141 return true;
142 }
143 } // namespace PowerMgr
144 } // namespace OHOS
145