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 "edm_data_ability_utils.h"
19 #include "edm_ipc_interface_code.h"
20 #include "edm_log.h"
21 #include "func_code_utils.h"
22 #include "plugin_manager.h"
23
24 namespace OHOS {
25 namespace EDM {
26 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(std::make_shared<PowerPolicyPlugin>());
27
28 const std::string KEY_POWER_SUSPEND = "settings.power.suspend_sources";
29 const std::string KEY_ACTION = "action";
30 const std::string KEY_DELAY_TIME = "delayMs";
31 const std::string KEY_TIME_OUT = "timeout";
32
PowerPolicyPlugin()33 PowerPolicyPlugin::PowerPolicyPlugin()
34 {
35 policyCode_ = EdmInterfaceCode::POWER_POLICY;
36 policyName_ = "power_policy";
37 permissionConfig_.permission = "ohos.permission.ENTERPRISE_MANAGE_SETTINGS";
38 permissionConfig_.permissionType = IPlugin::PermissionType::SUPER_DEVICE_ADMIN;
39 permissionConfig_.apiType = IPlugin::ApiType::PUBLIC;
40 needSave_ = false;
41 }
42
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)43 ErrCode PowerPolicyPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
44 HandlePolicyData &policyData, int32_t userId)
45 {
46 uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
47 FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
48 if (type == FuncOperateType::SET) {
49 return SetPowerPolicy(data);
50 }
51 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
52 }
53
SetPowerPolicy(MessageParcel & data)54 ErrCode PowerPolicyPlugin::SetPowerPolicy(MessageParcel &data)
55 {
56 uint32_t powerSence = data.ReadUint32();
57 PowerPolicy powerPolicy;
58 if (!PowerPolicy::Unmarshalling(data, powerPolicy)) {
59 EDMLOGE("PowerPolicyPlugin:Unmarshalling parse error");
60 return EdmReturnErrCode::PARAM_ERROR;
61 }
62 std::string policyKey;
63 if (!GetPowerSceneKey(powerSence, policyKey)) {
64 EDMLOGE("PowerPolicyPlugin:GetPowerSceneKey error");
65 return EdmReturnErrCode::PARAM_ERROR;
66 }
67
68 if (DealPowerSuspendPolicy(policyKey, powerPolicy, true)) {
69 return ERR_OK;
70 }
71 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
72 }
73
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)74 ErrCode PowerPolicyPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
75 int32_t userId)
76 {
77 uint32_t powerSence = data.ReadUint32();
78 std::string policyKey;
79 if (!GetPowerSceneKey(powerSence, policyKey)) {
80 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
81 return EdmReturnErrCode::PARAM_ERROR;
82 }
83 PowerPolicy powerPolicy;
84 if (!DealPowerSuspendPolicy(policyKey, powerPolicy, false)) {
85 EDMLOGE("PowerPolicyPlugin:PowerPolicy parse error");
86 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
87 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
88 }
89 reply.WriteInt32(ERR_OK);
90 if (!powerPolicy.Marshalling(reply)) {
91 EDMLOGE("PowerPolicyPlugin:OnGetPolicy Marshalling error");
92 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
93 }
94 return ERR_OK;
95 }
96
DealPowerSuspendPolicy(const std::string & policyKey,PowerPolicy & powerPolicy,bool isSetPolicy)97 bool PowerPolicyPlugin::DealPowerSuspendPolicy(const std::string &policyKey, PowerPolicy &powerPolicy, bool isSetPolicy)
98 {
99 std::string powerSuspend;
100 if (FAILED(EdmDataAbilityUtils::GetStringFromSettingsDataShare(KEY_POWER_SUSPEND, powerSuspend))) {
101 return false;
102 }
103 Json::Reader reader;
104 Json::Value root;
105 if (!reader.parse(powerSuspend.data(), powerSuspend.data() + powerSuspend.size(), root)) {
106 EDMLOGE("PowerPolicyPlugin:DealPowerSuspendPolicy parse error");
107 return false;
108 }
109 if (!root.isMember(policyKey)) {
110 EDMLOGE("PowerPolicyPlugin:DealPowerSuspendPolicy %{pubilc}s is not root member", policyKey.c_str());
111 return false;
112 }
113 Json::Value::Members members = root.getMemberNames();
114 for (auto iter = members.begin(); iter != members.end(); iter++) {
115 std::string key = *iter;
116 if (key != policyKey) {
117 continue;
118 }
119 if (isSetPolicy) {
120 return UpdateSuspendSettingsData(root, key, powerPolicy);
121 }
122 return SetPowerPolicyObject(root, key, powerPolicy);
123 }
124 return false;
125 }
126
UpdateSuspendSettingsData(Json::Value & root,const std::string & key,const PowerPolicy & powerPolicy)127 bool PowerPolicyPlugin::UpdateSuspendSettingsData(Json::Value &root, const std::string &key,
128 const PowerPolicy &powerPolicy)
129 {
130 Json::Value valueObj = root[key];
131 if (!valueObj.isObject()) {
132 return false;
133 }
134 valueObj[KEY_ACTION] = static_cast<uint32_t>(powerPolicy.GetPowerPolicyAction());
135 valueObj[KEY_DELAY_TIME] = powerPolicy.GetDealyTime();
136 root[key] = valueObj;
137 std::string jsonStr = root.toStyledString();
138 EDMLOGD("PowerPolicyPlugin:OnSetPolicy jsonStr = %{public}s", jsonStr.c_str());
139 if (FAILED(EdmDataAbilityUtils::UpdateSettingsData(KEY_POWER_SUSPEND, jsonStr))) {
140 return false;
141 }
142 return true;
143 }
144
SetPowerPolicyObject(Json::Value & root,std::string & key,PowerPolicy & powerPolicy)145 bool PowerPolicyPlugin::SetPowerPolicyObject(Json::Value &root, std::string &key, PowerPolicy &powerPolicy)
146 {
147 Json::Value valueObj = root[key];
148 if (!valueObj.isObject()) {
149 return false;
150 }
151 Json::Value actionValue = valueObj[KEY_ACTION];
152 Json::Value delayValue = valueObj[KEY_DELAY_TIME];
153 if (actionValue.isUInt() && delayValue.isUInt()) {
154 if (!powerPolicy.SetPowerPolicyAction(actionValue.asUInt())) {
155 return false;
156 }
157 powerPolicy.SetDelayTime(delayValue.asUInt());
158 return true;
159 }
160 return false;
161 }
162
GetPowerSceneKey(const uint32_t & powerScene,std::string & sceneKey)163 bool PowerPolicyPlugin::GetPowerSceneKey(const uint32_t &powerScene, std::string &sceneKey)
164 {
165 if (powerScene == static_cast<uint32_t>(PowerScene::TIME_OUT)) {
166 sceneKey = KEY_TIME_OUT;
167 return true;
168 }
169 return false;
170 }
171 } // namespace EDM
172 } // namespace OHOS
173