• 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         default:
70             return IRemoteStub<IStandbyService>::OnRemoteRequest(code, data, reply, option);
71     }
72     STANDBYSERVICE_LOGW("StandbyServiceStub: Failed to call interface %{public}u,", code);
73     return ERR_OK;
74 }
75 
HandleSubscribeStandbyCallback(MessageParcel & data,MessageParcel & reply)76 ErrCode StandbyServiceStub::HandleSubscribeStandbyCallback(MessageParcel& data, MessageParcel& reply)
77 {
78     auto subscriber = iface_cast<IStandbyServiceSubscriber>(data.ReadRemoteObject());
79     if (!subscriber) {
80         STANDBYSERVICE_LOGW("HandleSubscribeStandbyCallback Read callback fail.");
81         return ERR_STANDBY_PARCELABLE_FAILED;
82     }
83     std::string strategyName = data.ReadString();
84     STANDBYSERVICE_LOGD("HandleSubscribeStandbyCallback callback name is %{public}s 11111", strategyName.c_str());
85     subscriber->SetSubscriberName(strategyName);
86 
87     ErrCode result = SubscribeStandbyCallback(subscriber);
88     if (!reply.WriteInt32(result)) {
89         STANDBYSERVICE_LOGW("HandleSubscribeStandbyCallback Write result failed, ErrCode=%{public}d", result);
90         return ERR_STANDBY_PARCELABLE_FAILED;
91     }
92     return ERR_OK;
93 }
94 
HandleReportWorkSchedulerStatus(MessageParcel & data,MessageParcel & reply)95 ErrCode StandbyServiceStub::HandleReportWorkSchedulerStatus(MessageParcel& data, MessageParcel& reply)
96 {
97     bool started {false};
98     int32_t uid {0};
99     std::string bundleName {""};
100     if (!data.ReadBool(started) || !data.ReadInt32(uid) || !data.ReadString(bundleName)) {
101         STANDBYSERVICE_LOGW("HandleReportWorkSchedulerStatus ReadParcelable failed");
102         return ERR_STANDBY_PARCELABLE_FAILED;
103     }
104     ErrCode result = ReportWorkSchedulerStatus(started, uid, bundleName);
105     if (!reply.WriteInt32(result)) {
106         STANDBYSERVICE_LOGW("HandleReportWorkSchedulerStatus Write result failed, ErrCode=%{public}d", result);
107         return ERR_STANDBY_PARCELABLE_FAILED;
108     }
109     return ERR_OK;
110 }
111 
HandleGetRestrictList(MessageParcel & data,MessageParcel & reply)112 ErrCode StandbyServiceStub::HandleGetRestrictList(MessageParcel& data, MessageParcel& reply)
113 {
114     uint32_t restrictType {0};
115     uint32_t reasonCode {0};
116     if (!data.ReadUint32(restrictType) || !data.ReadUint32(reasonCode)) {
117         STANDBYSERVICE_LOGW("HandleGetRestrictList ReadParcelable failed");
118         return ERR_STANDBY_PARCELABLE_FAILED;
119     }
120     std::vector<AllowInfo> restrictInfoList {};
121     ErrCode result = GetRestrictList(restrictType, restrictInfoList, reasonCode);
122     if (!reply.WriteInt32(result)) {
123         STANDBYSERVICE_LOGW("HandleGetRestrictList Write result failed, ErrCode=%{public}d", result);
124         return ERR_STANDBY_PARCELABLE_FAILED;
125     }
126     if (!reply.WriteUint32(restrictInfoList.size())) {
127         STANDBYSERVICE_LOGW("HandleGetRestrictList Write result size failed");
128         return ERR_STANDBY_PARCELABLE_FAILED;
129     }
130     for (auto& info : restrictInfoList) {
131         if (!info.Marshalling(reply)) {
132             return ERR_STANDBY_PARCELABLE_FAILED;
133         }
134     }
135     return ERR_OK;
136 }
137 
HandleIsStrategyEnabled(MessageParcel & data,MessageParcel & reply)138 ErrCode StandbyServiceStub::HandleIsStrategyEnabled(MessageParcel& data, MessageParcel& reply)
139 {
140     bool enabled {false};
141     std::string strategyName {""};
142     if (!data.ReadString(strategyName)) {
143         STANDBYSERVICE_LOGW("HandleIsStrategyEnabled ReadParcelable failed");
144         return ERR_STANDBY_PARCELABLE_FAILED;
145     }
146     ErrCode result = IsDeviceInStandby(enabled);
147     if (!reply.WriteInt32(result)) {
148         STANDBYSERVICE_LOGW("HandleIsStrategyEnabled Write result failed, ErrCode=%{public}d", result);
149         return ERR_STANDBY_PARCELABLE_FAILED;
150     }
151     if (!reply.WriteBool(enabled)) {
152         STANDBYSERVICE_LOGW("HandleIsStrategyEnabled Write enabled failed");
153         return ERR_STANDBY_PARCELABLE_FAILED;
154     }
155     return ERR_OK;
156 }
157 
HandleUnsubscribeStandbyCallback(MessageParcel & data,MessageParcel & reply)158 ErrCode StandbyServiceStub::HandleUnsubscribeStandbyCallback(MessageParcel& data, MessageParcel& reply)
159 {
160     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
161     if (subscriber == nullptr) {
162         STANDBYSERVICE_LOGW("HandleUnsubscribeStandbyCallback Read callback fail.");
163         return ERR_STANDBY_PARCELABLE_FAILED;
164     }
165 
166     ErrCode result = UnsubscribeStandbyCallback(iface_cast<IStandbyServiceSubscriber>(subscriber));
167     if (!reply.WriteInt32(result)) {
168         STANDBYSERVICE_LOGW("HandleUnsubscribeStandbyCallback Write result failed, ErrCode=%{public}d", result);
169         return ERR_STANDBY_PARCELABLE_FAILED;
170     }
171     return ERR_OK;
172 }
173 
HandleApplyAllowResource(MessageParcel & data,MessageParcel & reply)174 ErrCode StandbyServiceStub::HandleApplyAllowResource(MessageParcel& data, MessageParcel& reply)
175 {
176     auto resourceRequest = ResourceRequest::Unmarshalling(data);
177     if (resourceRequest == nullptr) {
178         STANDBYSERVICE_LOGW("HandleApplyAllowResource ReadParcelable failed");
179         return ERR_STANDBY_PARCELABLE_FAILED;
180     }
181     ErrCode result = ApplyAllowResource(resourceRequest);
182     if (!reply.WriteInt32(result)) {
183         STANDBYSERVICE_LOGW("HandleApplyAllowResource Write result failed, ErrCode=%{public}d", result);
184         return ERR_STANDBY_PARCELABLE_FAILED;
185     }
186     return ERR_OK;
187 }
188 
HandleUnapplyAllowResource(MessageParcel & data,MessageParcel & reply)189 ErrCode StandbyServiceStub::HandleUnapplyAllowResource(MessageParcel& data, MessageParcel& reply)
190 {
191     auto resourceRequest = ResourceRequest::Unmarshalling(data);
192     if (resourceRequest == nullptr) {
193         STANDBYSERVICE_LOGW("HandleUnapplyAllowResource ReadParcelable failed");
194         return ERR_STANDBY_PARCELABLE_FAILED;
195     }
196     ErrCode result = UnapplyAllowResource(resourceRequest);
197     if (!reply.WriteInt32(result)) {
198         STANDBYSERVICE_LOGW("HandleUnapplyAllowResource Write result failed, ErrCode=%{public}d", result);
199         return ERR_STANDBY_PARCELABLE_FAILED;
200     }
201     return ERR_OK;
202 }
203 
HandleGetAllowList(MessageParcel & data,MessageParcel & reply)204 ErrCode StandbyServiceStub::HandleGetAllowList(MessageParcel& data, MessageParcel& reply)
205 {
206     uint32_t allowType {0};
207     uint32_t reasonCode {0};
208     if (!data.ReadUint32(allowType) || !data.ReadUint32(reasonCode)) {
209         STANDBYSERVICE_LOGW("HandleGetAllowList ReadParcelable failed");
210         return ERR_STANDBY_PARCELABLE_FAILED;
211     }
212     std::vector<AllowInfo> allowInfoList {};
213     ErrCode result = GetAllowList(allowType, allowInfoList, reasonCode);
214     if (!reply.WriteInt32(result)) {
215         STANDBYSERVICE_LOGW("HandleGetAllowList Write result failed, ErrCode=%{public}d", result);
216         return ERR_STANDBY_PARCELABLE_FAILED;
217     }
218     if (!reply.WriteUint32(allowInfoList.size())) {
219         STANDBYSERVICE_LOGW("HandleGetAllowList Write result size failed");
220         return ERR_STANDBY_PARCELABLE_FAILED;
221     }
222     for (auto& info : allowInfoList) {
223         if (!info.Marshalling(reply)) {
224             return ERR_STANDBY_PARCELABLE_FAILED;
225         }
226     }
227     return ERR_OK;
228 }
229 
HandleIsDeviceInStandby(MessageParcel & data,MessageParcel & reply)230 ErrCode StandbyServiceStub::HandleIsDeviceInStandby(MessageParcel& data, MessageParcel& reply)
231 {
232     bool isStandby {false};
233     ErrCode result = IsDeviceInStandby(isStandby);
234     if (!reply.WriteInt32(result)) {
235         STANDBYSERVICE_LOGW("HandleIsDeviceInStandby Write result failed, ErrCode=%{public}d", result);
236         return ERR_STANDBY_PARCELABLE_FAILED;
237     }
238     if (!reply.WriteBool(isStandby)) {
239         STANDBYSERVICE_LOGW("HandleIsDeviceInStandby Write isStandby failed");
240         return ERR_STANDBY_PARCELABLE_FAILED;
241     }
242     return ERR_OK;
243 }
244 
HandleReportDeviceStateChanged(MessageParcel & data,MessageParcel & reply)245 ErrCode StandbyServiceStub::HandleReportDeviceStateChanged(MessageParcel& data, MessageParcel& reply)
246 {
247     int32_t type {0};
248     bool enable {false};
249     if (!data.ReadInt32(type) || !data.ReadBool(enable)) {
250         STANDBYSERVICE_LOGW("HandleReportDeviceStateChanged ReadParcelable failed");
251         return ERR_STANDBY_PARCELABLE_FAILED;
252     }
253     ErrCode result = ReportDeviceStateChanged(static_cast<DeviceStateType>(type), enable);
254     if (!reply.WriteInt32(result)) {
255         STANDBYSERVICE_LOGW("HandleReportDeviceStateChanged Write result failed, ErrCode=%{public}d", result);
256         return ERR_STANDBY_PARCELABLE_FAILED;
257     }
258     return ERR_OK;
259 }
260 }  // namespace DevStandbyMgr
261 }  // namespace OHOS