• 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 "standby_service_proxy.h"
17 
18 #include <message_parcel.h>
19 
20 #include "standby_service_errors.h"
21 #include "standby_service_log.h"
22 #include "istandby_ipc_inteface_code.h"
23 
24 namespace OHOS {
25 namespace DevStandbyMgr {
StandbyServiceProxy(const sptr<IRemoteObject> & impl)26 StandbyServiceProxy::StandbyServiceProxy(const sptr<IRemoteObject>& impl)
27     :IRemoteProxy<IStandbyService>(impl) {}
~StandbyServiceProxy()28 StandbyServiceProxy::~StandbyServiceProxy() {}
29 
SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)30 ErrCode StandbyServiceProxy::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
31 {
32     if (subscriber == nullptr) {
33         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent subscriber is null");
34         return ERR_STANDBY_PARCELABLE_FAILED;
35     }
36 
37     MessageParcel data;
38     MessageParcel reply;
39     MessageOption option = {MessageOption::TF_SYNC};
40     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
41         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent write descriptor failed");
42         return ERR_STANDBY_PARCELABLE_FAILED;
43     }
44     if (!data.WriteRemoteObject(subscriber->AsObject())) {
45         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent write subscriber failed");
46         return ERR_STANDBY_PARCELABLE_FAILED;
47     }
48     if (!data.WriteString(subscriber->GetSubscriberName())) {
49         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent write SubscriberName failed");
50         return ERR_STANDBY_PARCELABLE_FAILED;
51     }
52 
53     ErrCode result = InnerTransact(
54         static_cast<uint32_t>(IStandbyInterfaceCode::SUBSCRIBE_STANDBY_CALLBACK), option, data, reply);
55     if (result != ERR_OK) {
56         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent fail: transact ErrCode=%{public}d", result);
57         return ERR_STANDBY_TRANSACT_FAILED;
58     }
59     if (!reply.ReadInt32(result)) {
60         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent fail: read result failed");
61         return ERR_STANDBY_PARCELABLE_FAILED;
62     }
63     if (result != ERR_OK) {
64         STANDBYSERVICE_LOGW("SubscribeSleepStateEvent failed");
65     }
66     return result;
67 }
68 
UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber> & subscriber)69 ErrCode StandbyServiceProxy::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
70 {
71     if (subscriber == nullptr) {
72         STANDBYSERVICE_LOGW("UnsubscribeSleepStateEvent subscriber is null");
73         return ERR_STANDBY_PARCELABLE_FAILED;
74     }
75 
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option = {MessageOption::TF_SYNC};
79     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
80         STANDBYSERVICE_LOGW("UnsubscribeSleepStateEvent write descriptor failed");
81         return ERR_STANDBY_PARCELABLE_FAILED;
82     }
83     if (!data.WriteRemoteObject(subscriber->AsObject())) {
84         STANDBYSERVICE_LOGW("UnsubscribeSleepStateEvent write subscriber failed");
85         return ERR_STANDBY_PARCELABLE_FAILED;
86     }
87 
88     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::UNSUBSCRIBE_STANDBY_CALLBACK),
89         option, data, reply);
90     if (result != ERR_OK) {
91         STANDBYSERVICE_LOGW("UnsubscribeSleepStateEvent fail: transact ErrCode=%{public}d", result);
92         return ERR_STANDBY_TRANSACT_FAILED;
93     }
94     if (!reply.ReadInt32(result)) {
95         STANDBYSERVICE_LOGW("UnsubscribeSleepStateEvent fail: read result failed");
96         return ERR_STANDBY_PARCELABLE_FAILED;
97     }
98     if (result != ERR_OK) {
99         STANDBYSERVICE_LOGW("UnsubscribeSleepStateEvent failed");
100         return result;
101     }
102     return result;
103 }
104 
ApplyAllowResource(const sptr<ResourceRequest> & resourceRequest)105 ErrCode StandbyServiceProxy::ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
106 {
107     MessageParcel data;
108     MessageParcel reply;
109     MessageOption option = {MessageOption::TF_SYNC};
110     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
111         STANDBYSERVICE_LOGW("ApplyAllowResource write descriptor failed");
112         return ERR_STANDBY_PARCELABLE_FAILED;
113     }
114     if (!resourceRequest->Marshalling(data)) {
115         STANDBYSERVICE_LOGW("ApplyAllowResource write parameter failed");
116         return ERR_STANDBY_PARCELABLE_FAILED;
117     }
118 
119     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::APPLY_ALLOW_RESOURCE),
120         option, data, reply);
121     if (result != ERR_OK) {
122         STANDBYSERVICE_LOGW("ApplyAllowResource fail: transact ErrCode=%{public}d", result);
123         return ERR_STANDBY_TRANSACT_FAILED;
124     }
125     if (!reply.ReadInt32(result)) {
126         STANDBYSERVICE_LOGW("ApplyAllowResource fail: read result failed");
127         return ERR_STANDBY_PARCELABLE_FAILED;
128     }
129     if (result != ERR_OK) {
130         STANDBYSERVICE_LOGW("ApplyAllowResource failed");
131         return result;
132     }
133     return result;
134 }
135 
UnapplyAllowResource(const sptr<ResourceRequest> & resourceRequest)136 ErrCode StandbyServiceProxy::UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
137 {
138     MessageParcel data;
139     MessageParcel reply;
140     MessageOption option = {MessageOption::TF_SYNC};
141     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
142         STANDBYSERVICE_LOGW("RemoveAllowList write descriptor failed");
143         return ERR_STANDBY_PARCELABLE_FAILED;
144     }
145     if (!resourceRequest->Marshalling(data)) {
146         STANDBYSERVICE_LOGW("RemoveAllowList write parameter failed");
147         return ERR_STANDBY_PARCELABLE_FAILED;
148     }
149 
150     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::UNAPPLY_ALLOW_RESOURCE),
151         option, data, reply);
152     if (result != ERR_OK) {
153         STANDBYSERVICE_LOGW("RemoveAllowList fail: transact ErrCode=%{public}d", result);
154         return ERR_STANDBY_TRANSACT_FAILED;
155     }
156     if (!reply.ReadInt32(result)) {
157         STANDBYSERVICE_LOGW("RemoveAllowList fail: read result failed");
158         return ERR_STANDBY_PARCELABLE_FAILED;
159     }
160     if (result != ERR_OK) {
161         STANDBYSERVICE_LOGW("RemoveAllowList failed");
162         return result;
163     }
164     return result;
165 }
166 
GetAllowList(uint32_t allowType,std::vector<AllowInfo> & allowInfoList,uint32_t reasonCode)167 ErrCode StandbyServiceProxy::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList,
168     uint32_t reasonCode)
169 {
170     MessageParcel data;
171     MessageParcel reply;
172     MessageOption option = {MessageOption::TF_SYNC};
173     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
174         STANDBYSERVICE_LOGW("GetAllowList write descriptor failed");
175         return ERR_STANDBY_PARCELABLE_FAILED;
176     }
177     if (!data.WriteUint32(allowType) || !data.WriteUint32(reasonCode)) {
178         STANDBYSERVICE_LOGW("GetAllowList write parameter failed");
179         return ERR_STANDBY_PARCELABLE_FAILED;
180     }
181 
182     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::GET_ALLOW_LIST),
183         option, data, reply);
184     if (result != ERR_OK) {
185         STANDBYSERVICE_LOGW("GetAllowList fail: transact ErrCode=%{public}d", result);
186         return ERR_STANDBY_TRANSACT_FAILED;
187     }
188     if (!reply.ReadInt32(result)) {
189         STANDBYSERVICE_LOGW("GetAllowList fail: read result failed.");
190         return ERR_STANDBY_PARCELABLE_FAILED;
191     }
192     if (result != ERR_OK) {
193         STANDBYSERVICE_LOGW("GetAllowList failed");
194         return result;
195     }
196     uint32_t infoSize = reply.ReadUint32();
197     for (uint32_t i = 0; i < infoSize; i++) {
198         auto info = AllowInfo::Unmarshalling(reply);
199         if (info == nullptr) {
200             STANDBYSERVICE_LOGW("GetAllowList Read Parcelable infos failed.");
201             return ERR_STANDBY_PARCELABLE_FAILED;
202         }
203         allowInfoList.emplace_back(*info);
204     }
205 
206     return result;
207 }
208 
IsDeviceInStandby(bool & isStandby)209 ErrCode StandbyServiceProxy::IsDeviceInStandby(bool& isStandby)
210 {
211     MessageParcel data;
212     MessageParcel reply;
213     MessageOption option = {MessageOption::TF_SYNC};
214     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
215         STANDBYSERVICE_LOGW("IsDeviceInStandby write descriptor failed");
216         return ERR_STANDBY_PARCELABLE_FAILED;
217     }
218 
219     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::IS_DEVICE_IN_STANDBY),
220         option, data, reply);
221     if (result != ERR_OK) {
222         STANDBYSERVICE_LOGW("IsDeviceInStandby fail: transact ErrCode=%{public}d", result);
223         return ERR_STANDBY_TRANSACT_FAILED;
224     }
225     if (!reply.ReadInt32(result)) {
226         STANDBYSERVICE_LOGW("IsDeviceInStandby fail: read result failed.");
227         return ERR_STANDBY_PARCELABLE_FAILED;
228     }
229     if (result != ERR_OK) {
230         STANDBYSERVICE_LOGW("IsDeviceInStandby failed");
231         return result;
232     }
233     if (!reply.ReadBool(isStandby)) {
234         STANDBYSERVICE_LOGW("IsDeviceInStandby fail: read result failed.");
235         return ERR_STANDBY_PARCELABLE_FAILED;
236     }
237     return result;
238 }
239 
ReportWorkSchedulerStatus(bool started,int32_t uid,const std::string & bundleName)240 ErrCode StandbyServiceProxy::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
241 {
242     MessageParcel data;
243     MessageParcel reply;
244     MessageOption option = {MessageOption::TF_SYNC};
245     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
246         STANDBYSERVICE_LOGW("IsDeviceInStandby write descriptor failed");
247         return ERR_STANDBY_PARCELABLE_FAILED;
248     }
249 
250     if (!data.WriteBool(started) || !data.WriteInt32(uid) || !data.WriteString(bundleName)) {
251         STANDBYSERVICE_LOGW("ReportWorkSchedulerStatus write parameter failed");
252         return ERR_STANDBY_PARCELABLE_FAILED;
253     }
254     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::REPORT_WORK_SCHEDULER_STATUS),
255         option, data, reply);
256     if (result != ERR_OK) {
257         STANDBYSERVICE_LOGW("ReportWorkSchedulerStatus fail: transact ErrCode=%{public}d", result);
258         return ERR_STANDBY_TRANSACT_FAILED;
259     }
260     if (!reply.ReadInt32(result)) {
261         STANDBYSERVICE_LOGW("ReportWorkSchedulerStatus fail: read result failed.");
262         return ERR_STANDBY_PARCELABLE_FAILED;
263     }
264     if (result != ERR_OK) {
265         STANDBYSERVICE_LOGW("ReportWorkSchedulerStatus failed");
266         return result;
267     }
268     return result;
269 }
270 
GetRestrictList(uint32_t restrictType,std::vector<AllowInfo> & restrictInfoList,uint32_t reasonCode)271 ErrCode StandbyServiceProxy::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
272     uint32_t reasonCode)
273 {
274     MessageParcel data;
275     MessageParcel reply;
276     MessageOption option = {MessageOption::TF_SYNC};
277     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
278         STANDBYSERVICE_LOGW("GetRestrictList write descriptor failed");
279         return ERR_STANDBY_PARCELABLE_FAILED;
280     }
281     if (!data.WriteUint32(restrictType) || !data.WriteUint32(reasonCode)) {
282         STANDBYSERVICE_LOGW("GetRestrictList write parameter failed");
283         return ERR_STANDBY_PARCELABLE_FAILED;
284     }
285 
286     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::GET_RESTRICT_LIST),
287         option, data, reply);
288     if (result != ERR_OK) {
289         STANDBYSERVICE_LOGW("GetRestrictList fail: transact ErrCode=%{public}d", result);
290         return ERR_STANDBY_TRANSACT_FAILED;
291     }
292     if (!reply.ReadInt32(result)) {
293         STANDBYSERVICE_LOGW("GetRestrictList fail: read result failed.");
294         return ERR_STANDBY_PARCELABLE_FAILED;
295     }
296     if (result != ERR_OK) {
297         STANDBYSERVICE_LOGW("GetRestrictList failed");
298         return result;
299     }
300     uint32_t infoSize = reply.ReadUint32();
301     for (uint32_t i = 0; i < infoSize; i++) {
302         auto info = AllowInfo::Unmarshalling(reply);
303         if (info == nullptr) {
304             STANDBYSERVICE_LOGW("GetRestrictList Read Parcelable infos failed.");
305             return ERR_STANDBY_PARCELABLE_FAILED;
306         }
307         restrictInfoList.emplace_back(*info);
308     }
309 
310     return result;
311 }
312 
IsStrategyEnabled(const std::string & strategyName,bool & enabled)313 ErrCode StandbyServiceProxy::IsStrategyEnabled(const std::string& strategyName, bool& enabled)
314 {
315     MessageParcel data;
316     MessageParcel reply;
317     MessageOption option = {MessageOption::TF_SYNC};
318     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
319         STANDBYSERVICE_LOGW("IsStrategyEnabled write descriptor failed");
320         return ERR_STANDBY_PARCELABLE_FAILED;
321     }
322     if (!data.WriteString(strategyName)) {
323         STANDBYSERVICE_LOGW("IsStrategyEnabled write parameter failed");
324         return ERR_STANDBY_PARCELABLE_FAILED;
325     }
326 
327     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::IS_STRATEGY_ENABLED),
328         option, data, reply);
329     if (result != ERR_OK) {
330         STANDBYSERVICE_LOGW("IsStrategyEnabled fail: transact ErrCode=%{public}d", result);
331         return ERR_STANDBY_TRANSACT_FAILED;
332     }
333     if (!reply.ReadInt32(result)) {
334         STANDBYSERVICE_LOGW("IsStrategyEnabled fail: read result failed.");
335         return ERR_STANDBY_PARCELABLE_FAILED;
336     }
337     if (result != ERR_OK) {
338         STANDBYSERVICE_LOGW("IsStrategyEnabled failed");
339         return result;
340     }
341     if (!reply.ReadBool(enabled)) {
342         STANDBYSERVICE_LOGW("IsStrategyEnabled fail: read result failed.");
343         return ERR_STANDBY_PARCELABLE_FAILED;
344     }
345     return result;
346 }
347 
ReportDeviceStateChanged(DeviceStateType type,bool enabled)348 ErrCode StandbyServiceProxy::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
349 {
350     MessageParcel data;
351     MessageParcel reply;
352     MessageOption option = {MessageOption::TF_SYNC};
353     if (!data.WriteInterfaceToken(StandbyServiceProxy::GetDescriptor())) {
354         STANDBYSERVICE_LOGW("IsDeviceInStandby write descriptor failed");
355         return ERR_STANDBY_PARCELABLE_FAILED;
356     }
357 
358     if (!data.WriteInt32(static_cast<int32_t>(type)) || !data.WriteBool(enabled)) {
359         STANDBYSERVICE_LOGW("ReportDeviceStateChanged write parameter failed");
360         return ERR_STANDBY_PARCELABLE_FAILED;
361     }
362     ErrCode result = InnerTransact(static_cast<uint32_t>(IStandbyInterfaceCode::REPORT_DEVICE_STATE_CHANGED),
363         option, data, reply);
364     if (result != ERR_OK) {
365         STANDBYSERVICE_LOGW("ReportDeviceStateChanged fail: transact ErrCode=%{public}d", result);
366         return ERR_STANDBY_TRANSACT_FAILED;
367     }
368     if (!reply.ReadInt32(result)) {
369         STANDBYSERVICE_LOGW("ReportDeviceStateChanged fail: read result failed.");
370         return ERR_STANDBY_PARCELABLE_FAILED;
371     }
372     if (result != ERR_OK) {
373         STANDBYSERVICE_LOGW("ReportDeviceStateChanged failed");
374         return result;
375     }
376     return result;
377 }
378 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)379 ErrCode StandbyServiceProxy::InnerTransact(uint32_t code, MessageOption& flags,
380     MessageParcel& data, MessageParcel& reply)
381 {
382     auto remote = Remote();
383     if (remote == nullptr) {
384         STANDBYSERVICE_LOGE("InnerTransact get Remote fail code %{public}d", code);
385         return ERR_DEAD_OBJECT;
386     }
387     int32_t err = remote->SendRequest(code, data, reply, flags);
388     switch (err) {
389         case NO_ERROR: {
390             return ERR_OK;
391         }
392         case DEAD_OBJECT: {
393             STANDBYSERVICE_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}u", err, code);
394             return ERR_DEAD_OBJECT;
395         }
396         default: {
397             STANDBYSERVICE_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}u", err, code);
398             return ERR_STANDBY_TRANSACT_FAILED;
399         }
400     }
401 }
402 }  // namespace DevStandbyMgr
403 }  // namespace OHOS