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