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 "standby_service_stub.h"
17 
18 #include <ipc_skeleton.h>
19 #include <string_ex.h>
20 
21 #include "istandby_ipc_inteface_code.h"
22 #include "standby_service_subscriber_proxy.h"
23 #include "standby_service_errors.h"
24 #include "standby_service_log.h"
25 
26 namespace OHOS {
27 namespace DevStandbyMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 ErrCode StandbyServiceStub::OnRemoteRequest(uint32_t code,
29     MessageParcel& data, MessageParcel& reply, MessageOption& option)
30 {
31     if (StandbyServiceStub::GetDescriptor() != data.ReadInterfaceToken()) {
32         STANDBYSERVICE_LOGE("StandbyServiceStub: Local descriptor not match remote.");
33         return ERR_TRANSACTION_FAILED;
34     }
35 
36     switch (code) {
37         case static_cast<uint32_t>(IStandbyInterfaceCode::SUBSCRIBE_STANDBY_CALLBACK):
38             HandleSubscribeStandbyCallback(data, reply);
39             break;
40         case static_cast<uint32_t>(IStandbyInterfaceCode::UNSUBSCRIBE_STANDBY_CALLBACK):
41             HandleUnsubscribeStandbyCallback(data, reply);
42             break;
43         case static_cast<uint32_t>(IStandbyInterfaceCode::APPLY_ALLOW_RESOURCE):
44             HandleApplyAllowResource(data, reply);
45             break;
46         case static_cast<uint32_t>(IStandbyInterfaceCode::UNAPPLY_ALLOW_RESOURCE):
47             HandleUnapplyAllowResource(data, reply);
48             break;
49         case static_cast<uint32_t>(IStandbyInterfaceCode::GET_ALLOW_LIST):
50             HandleGetAllowList(data, reply);
51             break;
52         case static_cast<uint32_t>(IStandbyInterfaceCode::IS_DEVICE_IN_STANDBY):
53             HandleIsDeviceInStandby(data, reply);
54             break;
55         case static_cast<uint32_t>(IStandbyInterfaceCode::REPORT_WORK_SCHEDULER_STATUS):
56             HandleReportWorkSchedulerStatus(data, reply);
57             break;
58         case static_cast<uint32_t>(IStandbyInterfaceCode::GET_RESTRICT_LIST):
59             HandleGetRestrictList(data, reply);
60             break;
61         case static_cast<uint32_t>(IStandbyInterfaceCode::IS_STRATEGY_ENABLED):
62             HandleIsStrategyEnabled(data, reply);
63             break;
64         case static_cast<uint32_t>(IStandbyInterfaceCode::REPORT_DEVICE_STATE_CHANGED):
65             HandleReportDeviceStateChanged(data, reply);
66             break;
67         case static_cast<uint32_t>(IStandbyInterfaceCode::HANDLE_EVENT):
68             HandleCommonEvent(data, reply);
69             break;
70         case static_cast<uint32_t>(IStandbyInterfaceCode::SET_NAT_INTERVAL):
71             HandleSetNatInterval(data, reply);
72             break;
73         case static_cast<uint32_t>(IStandbyInterfaceCode::POWER_OVERUSED):
74             HandleReportPowerOverused(data, reply);
75             break;
76         default:
77             return IRemoteStub<IStandbyService>::OnRemoteRequest(code, data, reply, option);
78     }
79     return ERR_OK;
80 }
81 
HandleSubscribeStandbyCallback(MessageParcel & data,MessageParcel & reply)82 ErrCode StandbyServiceStub::HandleSubscribeStandbyCallback(MessageParcel& data, MessageParcel& reply)
83 {
84     auto subscriber = iface_cast<IStandbyServiceSubscriber>(data.ReadRemoteObject());
85     if (!subscriber) {
86         STANDBYSERVICE_LOGW("HandleSubscribeStandbyCallback Read callback fail.");
87         return ERR_STANDBY_PARCELABLE_FAILED;
88     }
89     std::string strategyName = data.ReadString();
90     std::string moduleName = data.ReadString();
91 
92     STANDBYSERVICE_LOGD("HandleSubscribeStandbyCallback strategyName is %{public}s, moduleName is %{public}s.",
93         strategyName.c_str(), moduleName.c_str());
94     subscriber->SetSubscriberName(strategyName);
95     subscriber->SetModuleName(moduleName);
96 
97     ErrCode result = SubscribeStandbyCallback(subscriber);
98     if (!reply.WriteInt32(result)) {
99         STANDBYSERVICE_LOGW("HandleSubscribeStandbyCallback Write result failed, ErrCode=%{public}d", result);
100         return ERR_STANDBY_PARCELABLE_FAILED;
101     }
102     return ERR_OK;
103 }
104 
HandleReportWorkSchedulerStatus(MessageParcel & data,MessageParcel & reply)105 ErrCode StandbyServiceStub::HandleReportWorkSchedulerStatus(MessageParcel& data, MessageParcel& reply)
106 {
107     bool started {false};
108     int32_t uid {0};
109     std::string bundleName {""};
110     if (!data.ReadBool(started) || !data.ReadInt32(uid) || !data.ReadString(bundleName)) {
111         STANDBYSERVICE_LOGW("HandleReportWorkSchedulerStatus ReadParcelable failed");
112         return ERR_STANDBY_PARCELABLE_FAILED;
113     }
114     ErrCode result = ReportWorkSchedulerStatus(started, uid, bundleName);
115     if (!reply.WriteInt32(result)) {
116         STANDBYSERVICE_LOGW("HandleReportWorkSchedulerStatus Write result failed, ErrCode=%{public}d", result);
117         return ERR_STANDBY_PARCELABLE_FAILED;
118     }
119     return ERR_OK;
120 }
121 
HandleGetRestrictList(MessageParcel & data,MessageParcel & reply)122 ErrCode StandbyServiceStub::HandleGetRestrictList(MessageParcel& data, MessageParcel& reply)
123 {
124     uint32_t restrictType {0};
125     uint32_t reasonCode {0};
126     if (!data.ReadUint32(restrictType) || !data.ReadUint32(reasonCode)) {
127         STANDBYSERVICE_LOGW("HandleGetRestrictList ReadParcelable failed");
128         return ERR_STANDBY_PARCELABLE_FAILED;
129     }
130     std::vector<AllowInfo> restrictInfoList {};
131     ErrCode result = GetRestrictList(restrictType, restrictInfoList, reasonCode);
132     if (!reply.WriteInt32(result)) {
133         STANDBYSERVICE_LOGW("HandleGetRestrictList Write result failed, ErrCode=%{public}d", result);
134         return ERR_STANDBY_PARCELABLE_FAILED;
135     }
136     if (!reply.WriteUint32(restrictInfoList.size())) {
137         STANDBYSERVICE_LOGW("HandleGetRestrictList Write result size failed");
138         return ERR_STANDBY_PARCELABLE_FAILED;
139     }
140     for (auto& info : restrictInfoList) {
141         if (!info.Marshalling(reply)) {
142             return ERR_STANDBY_PARCELABLE_FAILED;
143         }
144     }
145     return ERR_OK;
146 }
147 
HandleIsStrategyEnabled(MessageParcel & data,MessageParcel & reply)148 ErrCode StandbyServiceStub::HandleIsStrategyEnabled(MessageParcel& data, MessageParcel& reply)
149 {
150     bool enabled {false};
151     std::string strategyName {""};
152     if (!data.ReadString(strategyName)) {
153         STANDBYSERVICE_LOGW("HandleIsStrategyEnabled ReadParcelable failed");
154         return ERR_STANDBY_PARCELABLE_FAILED;
155     }
156     ErrCode result = IsDeviceInStandby(enabled);
157     if (!reply.WriteInt32(result)) {
158         STANDBYSERVICE_LOGW("HandleIsStrategyEnabled Write result failed, ErrCode=%{public}d", result);
159         return ERR_STANDBY_PARCELABLE_FAILED;
160     }
161     if (!reply.WriteBool(enabled)) {
162         STANDBYSERVICE_LOGW("HandleIsStrategyEnabled Write enabled failed");
163         return ERR_STANDBY_PARCELABLE_FAILED;
164     }
165     return ERR_OK;
166 }
167 
HandleUnsubscribeStandbyCallback(MessageParcel & data,MessageParcel & reply)168 ErrCode StandbyServiceStub::HandleUnsubscribeStandbyCallback(MessageParcel& data, MessageParcel& reply)
169 {
170     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
171     if (subscriber == nullptr) {
172         STANDBYSERVICE_LOGW("HandleUnsubscribeStandbyCallback Read callback fail.");
173         return ERR_STANDBY_PARCELABLE_FAILED;
174     }
175 
176     ErrCode result = UnsubscribeStandbyCallback(iface_cast<IStandbyServiceSubscriber>(subscriber));
177     if (!reply.WriteInt32(result)) {
178         STANDBYSERVICE_LOGW("HandleUnsubscribeStandbyCallback Write result failed, ErrCode=%{public}d", result);
179         return ERR_STANDBY_PARCELABLE_FAILED;
180     }
181     return ERR_OK;
182 }
183 
HandleApplyAllowResource(MessageParcel & data,MessageParcel & reply)184 ErrCode StandbyServiceStub::HandleApplyAllowResource(MessageParcel& data, MessageParcel& reply)
185 {
186     auto resourceRequest = ResourceRequest::Unmarshalling(data);
187     if (resourceRequest == nullptr) {
188         STANDBYSERVICE_LOGW("HandleApplyAllowResource ReadParcelable failed");
189         return ERR_STANDBY_PARCELABLE_FAILED;
190     }
191     ErrCode result = ApplyAllowResource(resourceRequest);
192     if (!reply.WriteInt32(result)) {
193         STANDBYSERVICE_LOGW("HandleApplyAllowResource Write result failed, ErrCode=%{public}d", result);
194         return ERR_STANDBY_PARCELABLE_FAILED;
195     }
196     return ERR_OK;
197 }
198 
HandleUnapplyAllowResource(MessageParcel & data,MessageParcel & reply)199 ErrCode StandbyServiceStub::HandleUnapplyAllowResource(MessageParcel& data, MessageParcel& reply)
200 {
201     auto resourceRequest = ResourceRequest::Unmarshalling(data);
202     if (resourceRequest == nullptr) {
203         STANDBYSERVICE_LOGW("HandleUnapplyAllowResource ReadParcelable failed");
204         return ERR_STANDBY_PARCELABLE_FAILED;
205     }
206     ErrCode result = UnapplyAllowResource(resourceRequest);
207     if (!reply.WriteInt32(result)) {
208         STANDBYSERVICE_LOGW("HandleUnapplyAllowResource Write result failed, ErrCode=%{public}d", result);
209         return ERR_STANDBY_PARCELABLE_FAILED;
210     }
211     return ERR_OK;
212 }
213 
HandleGetAllowList(MessageParcel & data,MessageParcel & reply)214 ErrCode StandbyServiceStub::HandleGetAllowList(MessageParcel& data, MessageParcel& reply)
215 {
216     uint32_t allowType {0};
217     uint32_t reasonCode {0};
218     if (!data.ReadUint32(allowType) || !data.ReadUint32(reasonCode)) {
219         STANDBYSERVICE_LOGW("HandleGetAllowList ReadParcelable failed");
220         return ERR_STANDBY_PARCELABLE_FAILED;
221     }
222     std::vector<AllowInfo> allowInfoList {};
223     ErrCode result = GetAllowList(allowType, allowInfoList, reasonCode);
224     if (!reply.WriteInt32(result)) {
225         STANDBYSERVICE_LOGW("HandleGetAllowList Write result failed, ErrCode=%{public}d", result);
226         return ERR_STANDBY_PARCELABLE_FAILED;
227     }
228     if (!reply.WriteUint32(allowInfoList.size())) {
229         STANDBYSERVICE_LOGW("HandleGetAllowList Write result size failed");
230         return ERR_STANDBY_PARCELABLE_FAILED;
231     }
232     for (auto& info : allowInfoList) {
233         if (!info.Marshalling(reply)) {
234             return ERR_STANDBY_PARCELABLE_FAILED;
235         }
236     }
237     return ERR_OK;
238 }
239 
HandleIsDeviceInStandby(MessageParcel & data,MessageParcel & reply)240 ErrCode StandbyServiceStub::HandleIsDeviceInStandby(MessageParcel& data, MessageParcel& reply)
241 {
242     bool isStandby {false};
243     ErrCode result = IsDeviceInStandby(isStandby);
244     if (!reply.WriteInt32(result)) {
245         STANDBYSERVICE_LOGW("HandleIsDeviceInStandby Write result failed, ErrCode=%{public}d", result);
246         return ERR_STANDBY_PARCELABLE_FAILED;
247     }
248     if (!reply.WriteBool(isStandby)) {
249         STANDBYSERVICE_LOGW("HandleIsDeviceInStandby Write isStandby failed");
250         return ERR_STANDBY_PARCELABLE_FAILED;
251     }
252     return ERR_OK;
253 }
254 
HandleSetNatInterval(MessageParcel & data,MessageParcel & reply)255 ErrCode StandbyServiceStub::HandleSetNatInterval(MessageParcel& data, MessageParcel& reply)
256 {
257     uint32_t type {0};
258     bool enable {false};
259     uint32_t interval {0};
260     if (!data.ReadUint32(type) || !data.ReadBool(enable) || !data.ReadUint32(interval)) {
261         STANDBYSERVICE_LOGW("HandleSetNatInterval ReadParcelable failed");
262         return ERR_STANDBY_PARCELABLE_FAILED;
263     }
264     ErrCode result = SetNatInterval(type, enable, interval);
265     if (!reply.WriteInt32(result)) {
266         STANDBYSERVICE_LOGW("HandleSetNatInterval Write result failed, ErrCode=%{public}d", result);
267         return ERR_STANDBY_PARCELABLE_FAILED;
268     }
269     return ERR_OK;
270 }
271 
HandleReportPowerOverused(MessageParcel & data,MessageParcel & reply)272 ErrCode StandbyServiceStub::HandleReportPowerOverused(MessageParcel& data, MessageParcel& reply)
273 {
274     std::string module{""};
275     uint32_t level{0};
276 
277     if (!data.ReadString(module) || !data.ReadUint32(level)) {
278         STANDBYSERVICE_LOGW("HandleReportPowerOverused ReadParcelable failed");
279         return ERR_STANDBY_PARCELABLE_FAILED;
280     }
281     ErrCode result = ReportPowerOverused(module, level);
282     if (!reply.WriteInt32(result)) {
283         STANDBYSERVICE_LOGW("HandleReportPowerOverused Write result failed, ErrCode=%{public}d", result);
284         return ERR_STANDBY_PARCELABLE_FAILED;
285     }
286 
287     return ERR_OK;
288 }
289 
HandleReportDeviceStateChanged(MessageParcel & data,MessageParcel & reply)290 ErrCode StandbyServiceStub::HandleReportDeviceStateChanged(MessageParcel& data, MessageParcel& reply)
291 {
292     int32_t type {0};
293     bool enable {false};
294     if (!data.ReadInt32(type) || !data.ReadBool(enable)) {
295         STANDBYSERVICE_LOGW("HandleReportDeviceStateChanged ReadParcelable failed");
296         return ERR_STANDBY_PARCELABLE_FAILED;
297     }
298     ErrCode result = ReportDeviceStateChanged(static_cast<DeviceStateType>(type), enable);
299     if (!reply.WriteInt32(result)) {
300         STANDBYSERVICE_LOGW("HandleReportDeviceStateChanged Write result failed, ErrCode=%{public}d", result);
301         return ERR_STANDBY_PARCELABLE_FAILED;
302     }
303     return ERR_OK;
304 }
305 
HandleCommonEvent(MessageParcel & data,MessageParcel & reply)306 ErrCode StandbyServiceStub::HandleCommonEvent(MessageParcel& data, MessageParcel& reply)
307 {
308     uint32_t resType = 0;
309     if (!data.ReadUint32(resType)) {
310         STANDBYSERVICE_LOGW("Failed to read resType");
311         return ERR_STANDBY_PARCELABLE_FAILED;
312     }
313     int64_t value = 0;
314     if (!data.ReadInt64(value)) {
315         STANDBYSERVICE_LOGW("Failed to read value");
316         return ERR_STANDBY_PARCELABLE_FAILED;
317     }
318     std::string sceneInfo = "";
319     if (!data.ReadString(sceneInfo)) {
320         STANDBYSERVICE_LOGW("Failed to read sceneInfo");
321         return ERR_STANDBY_PARCELABLE_FAILED;
322     }
323     return HandleEvent(resType, value, sceneInfo);
324 }
325 }  // namespace DevStandbyMgr
326 }  // namespace OHOS