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