• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_save_mode.h"
19 #include "singleton.h"
20 #include "hilog_wrapper.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(MODULE_SERVICE, "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(MODULE_SERVICE, "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(MODULE_SERVICE,
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,std::function<void ()> action)110 void PowerModePolicy::AddAction(uint32_t type, std::function<void()> action)
111 {
112     POWER_HILOGW(MODULE_SERVICE, "AddAction: type=(%{public}d)", type);
113     actionMap.emplace(type, action);
114 }
115 
TriggerAction(uint32_t type)116 void PowerModePolicy::TriggerAction(uint32_t type)
117 {
118     auto iterator = actionMap.find(type);
119     if (iterator == actionMap.end()) {
120         POWER_HILOGW(MODULE_SERVICE, "TriggerAction: no such type=(%{public}d)", type);
121         return;
122     }
123     POWER_HILOGW(MODULE_SERVICE, "TriggerAction: type=(%{public}d)", type);
124     iterator->second();
125 }
126 
TriggerAllActions()127 void PowerModePolicy::TriggerAllActions()
128 {
129     POWER_HILOGW(MODULE_SERVICE, "TriggerAllActions start");
130     for (auto iterator = actionMap.begin(); iterator != actionMap.end(); iterator++) {
131         POWER_HILOGW(MODULE_SERVICE, "TriggerAllActions: type=(%{public}d)", iterator->first);
132         iterator->second();
133     }
134 }
135 
IsValidType(uint32_t type)136 bool PowerModePolicy::IsValidType(uint32_t type)
137 {
138     auto iterator = actionMap.find(type);
139     if (iterator == actionMap.end()) {
140         POWER_HILOGW(MODULE_SERVICE, "IsValidType: false (%{public}d)", type);
141         return false;
142     }
143 
144     POWER_HILOGW(MODULE_SERVICE, "IsValidType: true (%{public}d)", type);
145     return true;
146 }
147 } // namespace PowerMgr
148 } // namespace OHOS
149