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 valueiter = valueModePolicy.find(type);
40 if (valueiter != valueModePolicy.end()) {
41 ret = valueiter->second;
42 return ret;
43 } else {
44 return ret;
45 }
46 }
47
GetRecoverPolicyFromMap(uint32_t type)48 int32_t PowerModePolicy::GetRecoverPolicyFromMap(uint32_t type)
49 {
50 int32_t ret = INIT_VALUE_FALSE;
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 recoverModePolicy.clear();
94 valueModePolicy.clear();
95
96 for (auto openlit = openPolicy.begin(); openlit != openPolicy.end(); openlit++) {
97 valueModePolicy[(*openlit).id] = (*openlit).value;
98 }
99
100 for (recoverlit = recoverPolicy.begin(); recoverlit != recoverPolicy.end(); recoverlit++) {
101 recoverModePolicy[(*recoverlit).id] = (*recoverlit).value;
102 POWER_HILOGD(FEATURE_POWER_MODE,
103 "(*recoverlit).id=%{public}d, (*recoverlit).value=%{public}d", (*recoverlit).id, (*recoverlit).value);
104 }
105
106 openPolicy.clear();
107 recoverPolicy.clear();
108 }
109
AddAction(uint32_t type,ModeAction & action)110 void PowerModePolicy::AddAction(uint32_t type, ModeAction& action)
111 {
112 POWER_HILOGD(FEATURE_POWER_MODE, "type=%{public}d", type);
113 actionMap.emplace(type, action);
114 }
115
TriggerAllActions(bool isBoot)116 void PowerModePolicy::TriggerAllActions(bool isBoot)
117 {
118 for (auto iterator = actionMap.begin(); iterator != actionMap.end(); iterator++) {
119 POWER_HILOGD(FEATURE_POWER_MODE, "type=%{public}d", iterator->first);
120 iterator->second(isBoot);
121 }
122 }
123
IsValidType(uint32_t type)124 bool PowerModePolicy::IsValidType(uint32_t type)
125 {
126 auto iterator = actionMap.find(type);
127 if (iterator == actionMap.end()) {
128 POWER_HILOGW(FEATURE_POWER_MODE, "Invalid type: %{public}d", type);
129 return false;
130 }
131 return true;
132 }
133 } // namespace PowerMgr
134 } // namespace OHOS
135