1 /*
2 * Copyright (c) 2023-2024 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_policy_plugin.h"
17
18 #include "battery_utils.h"
19 #include "edm_constants.h"
20 #include "edm_data_ability_utils.h"
21 #include "edm_ipc_interface_code.h"
22 #include "edm_log.h"
23 #include "func_code_utils.h"
24 #include "iplugin_manager.h"
25
26 namespace OHOS {
27 namespace EDM {
28 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(std::make_shared<PowerPolicyPlugin>());
29
30 const std::string KEY_POWER_SUSPEND = "settings.power.suspend_sources";
31 const std::string KEY_POWER_AC_SUSPEND = "settings.power.ac.suspend_sources";
32 const std::string KEY_POWER_DC_SUSPEND = "settings.power.dc.suspend_sources";
33 const std::string KEY_TIME_OUT = "timeout";
34 const char* const KEY_ACTION = "action";
35 const char* const KEY_DELAY_TIME = "delayMs";
36
PowerPolicyPlugin()37 PowerPolicyPlugin::PowerPolicyPlugin()
38 {
39 policyCode_ = EdmInterfaceCode::POWER_POLICY;
40 policyName_ = PolicyName::POLICY_POWER_POLICY;
41 permissionConfig_.typePermissions.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
42 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_SETTINGS);
43 permissionConfig_.apiType = IPlugin::ApiType::PUBLIC;
44 needSave_ = false;
45 }
46
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)47 ErrCode PowerPolicyPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
48 HandlePolicyData &policyData, int32_t userId)
49 {
50 uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
51 FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
52 if (type == FuncOperateType::SET) {
53 return SetPowerPolicy(data);
54 }
55 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
56 }
57
SetPowerPolicy(MessageParcel & data)58 ErrCode PowerPolicyPlugin::SetPowerPolicy(MessageParcel &data)
59 {
60 uint32_t powerSence = data.ReadUint32();
61 PowerPolicy powerPolicy;
62 if (!PowerPolicy::Unmarshalling(data, powerPolicy)) {
63 EDMLOGE("PowerPolicyPlugin:Unmarshalling parse error");
64 return EdmReturnErrCode::PARAM_ERROR;
65 }
66 std::string policyKey;
67 if (!GetPowerSceneKey(powerSence, policyKey)) {
68 EDMLOGE("PowerPolicyPlugin:GetPowerSceneKey error");
69 return EdmReturnErrCode::PARAM_ERROR;
70 }
71
72 if (DealPowerSuspendPolicy(policyKey, powerPolicy, true)) {
73 return ERR_OK;
74 }
75 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
76 }
77
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)78 ErrCode PowerPolicyPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
79 int32_t userId)
80 {
81 uint32_t powerSence = data.ReadUint32();
82 std::string policyKey;
83 if (!GetPowerSceneKey(powerSence, policyKey)) {
84 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
85 return EdmReturnErrCode::PARAM_ERROR;
86 }
87 PowerPolicy powerPolicy;
88 if (!DealPowerSuspendPolicy(policyKey, powerPolicy, false)) {
89 EDMLOGE("PowerPolicyPlugin:PowerPolicy parse error");
90 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
91 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
92 }
93 reply.WriteInt32(ERR_OK);
94 if (!powerPolicy.Marshalling(reply)) {
95 EDMLOGE("PowerPolicyPlugin:OnGetPolicy Marshalling error");
96 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
97 }
98 return ERR_OK;
99 }
100
DealPowerSuspendPolicy(const std::string & policyKey,PowerPolicy & powerPolicy,bool isSetPolicy)101 bool PowerPolicyPlugin::DealPowerSuspendPolicy(const std::string &policyKey, PowerPolicy &powerPolicy, bool isSetPolicy)
102 {
103 std::string powerSuspend;
104 #ifdef FEATURE_CHARGING_TYPE_SETTING
105 std::string newKey;
106 if (BatteryUtils::GetBatteryPluggedType() == BatteryUtils::PLUGGED_TYPE_AC) {
107 newKey = KEY_POWER_AC_SUSPEND;
108 } else {
109 newKey = KEY_POWER_DC_SUSPEND;
110 }
111 if (FAILED(EdmDataAbilityUtils::GetStringFromSettingsDataShare(
112 BatteryUtils::GetSubUserTableUri(), newKey, powerSuspend))) {
113 return false;
114 }
115 #else
116 if (FAILED(EdmDataAbilityUtils::GetStringFromSettingsDataShare(KEY_POWER_SUSPEND, powerSuspend))) {
117 return false;
118 }
119 #endif
120
121 cJSON *root = cJSON_Parse(powerSuspend.c_str());
122 if (root == nullptr) {
123 EDMLOGE("PowerPolicyPlugin:DealPowerSuspendPolicy parse error");
124 return false;
125 }
126
127 cJSON *policyItem = cJSON_GetObjectItem(root, policyKey.c_str());
128 if (policyItem == nullptr) {
129 EDMLOGE("PowerPolicyPlugin:DealPowerSuspendPolicy %{public}s is not root member", policyKey.c_str());
130 cJSON_Delete(root);
131 return false;
132 }
133
134 bool result = false;
135 if (isSetPolicy) {
136 result = UpdateSuspendSettingsData(root, policyKey, powerPolicy);
137 } else {
138 result = SetPowerPolicyObject(root, policyKey, powerPolicy);
139 }
140
141 cJSON_Delete(root);
142 return result;
143 }
144
UpdateSuspendSettingsData(cJSON * root,const std::string & key,const PowerPolicy & powerPolicy)145 bool PowerPolicyPlugin::UpdateSuspendSettingsData(cJSON* root, const std::string& key,
146 const PowerPolicy& powerPolicy)
147 {
148 cJSON* valueObj = cJSON_GetObjectItem(root, key.c_str());
149 if (valueObj == nullptr || !cJSON_IsObject(valueObj)) {
150 return false;
151 }
152
153 cJSON* actionItem = cJSON_GetObjectItem(valueObj, KEY_ACTION);
154 if (actionItem != nullptr) {
155 cJSON_SetNumberValue(actionItem, static_cast<uint32_t>(powerPolicy.GetPowerPolicyAction()));
156 } else {
157 cJSON_AddNumberToObject(valueObj, KEY_ACTION, static_cast<uint32_t>(powerPolicy.GetPowerPolicyAction()));
158 }
159
160 cJSON* delayItem = cJSON_GetObjectItem(valueObj, KEY_DELAY_TIME);
161 if (delayItem != nullptr) {
162 cJSON_SetNumberValue(delayItem, powerPolicy.GetDelayTime());
163 } else {
164 cJSON_AddNumberToObject(valueObj, KEY_DELAY_TIME, powerPolicy.GetDelayTime());
165 }
166
167 char* jsonStr = cJSON_PrintUnformatted(root);
168 if (jsonStr == nullptr) {
169 return false;
170 }
171 EDMLOGD("PowerPolicyPlugin:OnSetPolicy jsonStr = %{public}s", jsonStr);
172
173 #ifdef FEATURE_CHARGING_TYPE_SETTING
174 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(
175 BatteryUtils::GetSubUserTableUri(), KEY_POWER_AC_SUSPEND, jsonStr))) {
176 cJSON_free(jsonStr);
177 return false;
178 }
179 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(
180 BatteryUtils::GetSubUserTableUri(), KEY_POWER_DC_SUSPEND, jsonStr))) {
181 cJSON_free(jsonStr);
182 return false;
183 }
184 #else
185 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(KEY_POWER_SUSPEND, jsonStr))) {
186 cJSON_free(jsonStr);
187 return false;
188 }
189 #endif
190
191 cJSON_free(jsonStr);
192 return true;
193 }
194
SetPowerPolicyObject(cJSON * root,const std::string & key,PowerPolicy & powerPolicy)195 bool PowerPolicyPlugin::SetPowerPolicyObject(cJSON* root, const std::string& key, PowerPolicy& powerPolicy)
196 {
197 cJSON* valueObj = cJSON_GetObjectItem(root, key.c_str());
198 if (valueObj == nullptr || !cJSON_IsObject(valueObj)) {
199 return false;
200 }
201
202 cJSON* actionValue = cJSON_GetObjectItem(valueObj, KEY_ACTION);
203 cJSON* delayValue = cJSON_GetObjectItem(valueObj, KEY_DELAY_TIME);
204
205 if (actionValue && cJSON_IsNumber(actionValue) && delayValue && cJSON_IsNumber(delayValue)) {
206 if (!powerPolicy.SetPowerPolicyAction(actionValue->valueint)) {
207 return false;
208 }
209 powerPolicy.SetDelayTime(delayValue->valueint);
210 return true;
211 }
212 return false;
213 }
214
GetPowerSceneKey(const uint32_t & powerScene,std::string & sceneKey)215 bool PowerPolicyPlugin::GetPowerSceneKey(const uint32_t &powerScene, std::string &sceneKey)
216 {
217 if (powerScene == static_cast<uint32_t>(PowerScene::TIME_OUT)) {
218 sceneKey = KEY_TIME_OUT;
219 return true;
220 }
221 return false;
222 }
223 } // namespace EDM
224 } // namespace OHOS
225