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_ACTION = "action";
34 const std::string KEY_DELAY_TIME = "delayMs";
35 const std::string KEY_TIME_OUT = "timeout";
36
PowerPolicyPlugin()37 PowerPolicyPlugin::PowerPolicyPlugin()
38 {
39 policyCode_ = EdmInterfaceCode::POWER_POLICY;
40 policyName_ = "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 Json::Reader reader;
122 Json::Value root;
123 if (!reader.parse(powerSuspend.data(), powerSuspend.data() + powerSuspend.size(), root)) {
124 EDMLOGE("PowerPolicyPlugin:DealPowerSuspendPolicy parse error");
125 return false;
126 }
127 if (!root.isMember(policyKey)) {
128 EDMLOGE("PowerPolicyPlugin:DealPowerSuspendPolicy %{public}s is not root member", policyKey.c_str());
129 return false;
130 }
131 Json::Value::Members members = root.getMemberNames();
132 for (auto iter = members.begin(); iter != members.end(); iter++) {
133 std::string key = *iter;
134 if (key != policyKey) {
135 continue;
136 }
137 if (isSetPolicy) {
138 return UpdateSuspendSettingsData(root, key, powerPolicy);
139 }
140 return SetPowerPolicyObject(root, key, powerPolicy);
141 }
142 return false;
143 }
144
UpdateSuspendSettingsData(Json::Value & root,const std::string & key,const PowerPolicy & powerPolicy)145 bool PowerPolicyPlugin::UpdateSuspendSettingsData(Json::Value &root, const std::string &key,
146 const PowerPolicy &powerPolicy)
147 {
148 Json::Value valueObj = root[key];
149 if (!valueObj.isObject()) {
150 return false;
151 }
152 valueObj[KEY_ACTION] = static_cast<uint32_t>(powerPolicy.GetPowerPolicyAction());
153 valueObj[KEY_DELAY_TIME] = powerPolicy.GetDelayTime();
154 root[key] = valueObj;
155 std::string jsonStr = root.toStyledString();
156 EDMLOGD("PowerPolicyPlugin:OnSetPolicy jsonStr = %{public}s", jsonStr.c_str());
157
158 #ifdef FEATURE_CHARGING_TYPE_SETTING
159 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(
160 BatteryUtils::GetSubUserTableUri(), KEY_POWER_AC_SUSPEND, jsonStr))) {
161 return false;
162 }
163 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(
164 BatteryUtils::GetSubUserTableUri(), KEY_POWER_DC_SUSPEND, jsonStr))) {
165 return false;
166 }
167 #else
168 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(KEY_POWER_SUSPEND, jsonStr))) {
169 return false;
170 }
171 #endif
172 return true;
173 }
174
SetPowerPolicyObject(Json::Value & root,std::string & key,PowerPolicy & powerPolicy)175 bool PowerPolicyPlugin::SetPowerPolicyObject(Json::Value &root, std::string &key, PowerPolicy &powerPolicy)
176 {
177 Json::Value valueObj = root[key];
178 if (!valueObj.isObject()) {
179 return false;
180 }
181 Json::Value actionValue = valueObj[KEY_ACTION];
182 Json::Value delayValue = valueObj[KEY_DELAY_TIME];
183 if (actionValue.isUInt() && delayValue.isUInt()) {
184 if (!powerPolicy.SetPowerPolicyAction(actionValue.asUInt())) {
185 return false;
186 }
187 powerPolicy.SetDelayTime(delayValue.asUInt());
188 return true;
189 }
190 return false;
191 }
192
GetPowerSceneKey(const uint32_t & powerScene,std::string & sceneKey)193 bool PowerPolicyPlugin::GetPowerSceneKey(const uint32_t &powerScene, std::string &sceneKey)
194 {
195 if (powerScene == static_cast<uint32_t>(PowerScene::TIME_OUT)) {
196 sceneKey = KEY_TIME_OUT;
197 return true;
198 }
199 return false;
200 }
201 } // namespace EDM
202 } // namespace OHOS
203