1 /*
2 * Copyright (c) 2022 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 #include "work_sched_service_proxy.h"
16
17 #include <ipc_types.h>
18 #include <message_parcel.h>
19 #include "iwork_sched_service_ipc_interface_code.h"
20 #include "work_sched_errors.h"
21
22 #include "work_sched_common.h"
23
24 namespace OHOS {
25 namespace WorkScheduler {
StartWork(WorkInfo & workInfo)26 int32_t WorkSchedServiceProxy::StartWork(WorkInfo& workInfo)
27 {
28 WS_HILOGI("proxy Call StartWork come in");
29 sptr<IRemoteObject> remote = Remote();
30 RETURN_IF_WITH_RET(remote == nullptr, false);
31
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35
36 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
37 WS_HILOGE("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
38 return E_PARCEL_OPERATION_FAILED;
39 }
40
41 if (!data.WriteParcelable(&workInfo)) {
42 WS_HILOGE("StartWork failed, write workinfo failed!");
43 return E_PARCEL_OPERATION_FAILED;
44 }
45
46 int32_t ret = remote->SendRequest(
47 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::START_WORK), data, reply, option);
48 if (ret != ERR_OK) {
49 WS_HILOGE("SendRequest is failed, error code: %{public}d", ret);
50 return E_PARCEL_OPERATION_FAILED;
51 }
52 int32_t result = E_PARCEL_OPERATION_FAILED;
53 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
54 WS_HILOGD("after result : %{public}s ", std::to_string(result).c_str());
55 return result;
56 }
57
StopWork(WorkInfo & workInfo)58 int32_t WorkSchedServiceProxy::StopWork(WorkInfo& workInfo)
59 {
60 sptr<IRemoteObject> remote = Remote();
61 RETURN_IF_WITH_RET(remote == nullptr, false);
62
63 MessageParcel data;
64 MessageParcel reply;
65 MessageOption option;
66
67 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
68 WS_HILOGE(" write descriptor failed!");
69 return E_PARCEL_OPERATION_FAILED;
70 }
71
72 if (!data.WriteParcelable(&workInfo)) {
73 WS_HILOGE("StopWork failed, write workinfo failed!");
74 return E_PARCEL_OPERATION_FAILED;
75 }
76
77 int32_t ret = remote->SendRequest(
78 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_WORK), data, reply, option);
79 if (ret != ERR_OK) {
80 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
81 return E_PARCEL_OPERATION_FAILED;
82 }
83 int32_t result = E_PARCEL_OPERATION_FAILED;
84 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
85 return result;
86 }
87
StopAndCancelWork(WorkInfo & workInfo)88 int32_t WorkSchedServiceProxy::StopAndCancelWork(WorkInfo& workInfo)
89 {
90 sptr<IRemoteObject> remote = Remote();
91 RETURN_IF_WITH_RET(remote == nullptr, false);
92
93 MessageParcel data;
94 MessageParcel reply;
95 MessageOption option;
96
97 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
98 WS_HILOGE("write descriptor failed!");
99 return false;
100 }
101
102 if (!data.WriteParcelable(&workInfo)) {
103 WS_HILOGE("StopAndCancelWork failed, write workinfo failed!");
104 return E_PARCEL_OPERATION_FAILED;
105 }
106
107 int32_t ret = remote->SendRequest(
108 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_AND_CANCEL_WORK),
109 data, reply, option);
110 if (ret != ERR_OK) {
111 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
112 return E_PARCEL_OPERATION_FAILED;
113 }
114 int32_t result = E_PARCEL_OPERATION_FAILED;
115 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
116 return result;
117 }
118
StopAndClearWorks()119 int32_t WorkSchedServiceProxy::StopAndClearWorks()
120 {
121 sptr<IRemoteObject> remote = Remote();
122 RETURN_IF_WITH_RET(remote == nullptr, false);
123
124 MessageParcel data;
125 MessageParcel reply;
126 MessageOption option;
127 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
128 WS_HILOGE("write descriptor failed!");
129 return E_PARCEL_OPERATION_FAILED;
130 }
131
132 int32_t ret = remote->SendRequest(
133 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_AND_CLEAR_WORKS),
134 data, reply, option);
135 if (ret != ERR_OK) {
136 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
137 return E_PARCEL_OPERATION_FAILED;
138 }
139 int32_t result = E_PARCEL_OPERATION_FAILED;
140 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
141 return result;
142 }
143
IsLastWorkTimeout(int32_t workId,bool & result)144 int32_t WorkSchedServiceProxy::IsLastWorkTimeout(int32_t workId, bool &result)
145 {
146 sptr<IRemoteObject> remote = Remote();
147 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
148
149 MessageParcel data;
150 MessageParcel reply;
151 MessageOption option;
152 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
153 WS_HILOGE("write descriptor failed!");
154 return E_PARCEL_OPERATION_FAILED;
155 }
156
157 WRITE_PARCEL_WITHOUT_RET(data, Int32, workId);
158 int32_t ret = remote->SendRequest(
159 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::IS_LAST_WORK_TIMEOUT),
160 data, reply, option);
161 if (ret != ERR_OK) {
162 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
163 return E_PARCEL_OPERATION_FAILED;
164 }
165 ret = E_PARCEL_OPERATION_FAILED;
166 READ_PARCEL_WITHOUT_RET(reply, Int32, ret);
167 READ_PARCEL_WITHOUT_RET(reply, Bool, result);
168 return ret;
169 }
170
ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)171 int32_t WorkSchedServiceProxy::ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos)
172 {
173 sptr<IRemoteObject> remote = Remote();
174 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
175
176 MessageParcel data;
177 MessageParcel reply;
178 MessageOption option;
179
180 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
181 WS_HILOGE("write descriptor failed!");
182 return E_PARCEL_OPERATION_FAILED;
183 }
184
185 int32_t ret = remote->SendRequest(
186 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::OBTAIN_ALL_WORKS), data, reply, option);
187 if (ret != ERR_OK) {
188 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
189 return E_PARCEL_OPERATION_FAILED;
190 }
191
192 ErrCode errCode;
193 READ_PARCEL_WITHOUT_RET(reply, Int32, errCode);
194 int32_t worksize = 0;
195 READ_PARCEL_WITHOUT_RET(reply, Int32, worksize);
196 WS_HILOGD("ObtainAllWorks worksize from read parcel is: %{public}d", worksize);
197 for (int32_t i = 0; i < worksize; i++) {
198 sptr<WorkInfo> workInfo = reply.ReadStrongParcelable<WorkInfo>();
199 if (workInfo == nullptr) {
200 continue;
201 }
202 WS_HILOGD("WP read from parcel, workInfo ID: %{public}d", workInfo->GetWorkId());
203 workInfos.emplace_back(std::make_shared<WorkInfo>(*workInfo));
204 }
205 WS_HILOGD("return list size: %{public}zu", workInfos.size());
206 return errCode;
207 }
208
GetWorkStatus(int32_t & workId,std::shared_ptr<WorkInfo> & workInfo)209 int32_t WorkSchedServiceProxy::GetWorkStatus(int32_t &workId, std::shared_ptr<WorkInfo>& workInfo)
210 {
211 WS_HILOGD("enter, workId: %{public}d", workId);
212 sptr<IRemoteObject> remote = Remote();
213 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
214 MessageParcel data;
215 MessageParcel reply;
216 MessageOption option;
217 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
218 WS_HILOGE("write descriptor failed!");
219 return E_PARCEL_OPERATION_FAILED;
220 }
221 WRITE_PARCEL_WITHOUT_RET(data, Int32, workId);
222 int32_t ret = remote->SendRequest(
223 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::GET_WORK_STATUS), data, reply, option);
224 if (ret != ERR_OK) {
225 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
226 return E_PARCEL_OPERATION_FAILED;
227 }
228 ErrCode errCode;
229 READ_PARCEL_WITHOUT_RET(reply, Int32, errCode);
230 sptr<WorkInfo> workInfoSptr = reply.ReadStrongParcelable<WorkInfo>();
231 if (workInfoSptr == nullptr) {
232 return E_WORK_NOT_EXIST_FAILED;
233 }
234 workInfo = std::make_shared<WorkInfo>(*workInfoSptr);
235 return errCode;
236 }
237
GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)238 int32_t WorkSchedServiceProxy::GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos)
239 {
240 WS_HILOGD("get all running work sheduler work");
241 sptr<IRemoteObject> remote = Remote();
242 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
243
244 MessageParcel data;
245 MessageParcel reply;
246 MessageOption option;
247
248 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
249 WS_HILOGE("write descriptor failed!");
250 return E_PARCEL_OPERATION_FAILED;
251 }
252
253 int32_t ret = remote->SendRequest(
254 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::GET_ALL_RUNNING_WORKS), data, reply, option);
255 if (ret != ERR_OK) {
256 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
257 return E_PARCEL_OPERATION_FAILED;
258 }
259
260 ErrCode errCode;
261 READ_PARCEL_WITHOUT_RET(reply, Int32, errCode);
262 int32_t worksize = 0;
263 READ_PARCEL_WITHOUT_RET(reply, Int32, worksize);
264 WS_HILOGD("GetAllRunningWorks worksize from read parcel is: %{public}d", worksize);
265 for (int32_t i = 0; i < worksize; i++) {
266 sptr<WorkInfo> workInfo = reply.ReadStrongParcelable<WorkInfo>();
267 if (workInfo == nullptr) {
268 continue;
269 }
270 WS_HILOGD("WP read from parcel, workInfo ID: %{public}d", workInfo->GetWorkId());
271 workInfos.emplace_back(std::make_shared<WorkInfo>(*workInfo));
272 }
273 WS_HILOGD("return list size: %{public}zu", workInfos.size());
274 return errCode;
275 }
276
PauseRunningWorks(int32_t uid)277 int32_t WorkSchedServiceProxy::PauseRunningWorks(int32_t uid)
278 {
279 WS_HILOGD("Pause Running Work Scheduler Work, uid:%{public}d", uid);
280 sptr<IRemoteObject> remote = Remote();
281 RETURN_IF_WITH_RET(remote == nullptr, E_CLIENT_CONNECT_SERVICE_FAILED);
282
283 MessageParcel data;
284 MessageParcel reply;
285 MessageOption option;
286 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
287 WS_HILOGE("write descriptor failed!");
288 return E_PARCEL_OPERATION_FAILED;
289 }
290
291 if (!data.WriteInt32(uid)) {
292 WS_HILOGE("PauseRunningWorks failed, write uid failed!");
293 return E_PARCEL_OPERATION_FAILED;
294 }
295 int32_t ret = remote->SendRequest(
296 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::PAUSE_RUNNING_WORKS), data, reply, option);
297 if (ret != ERR_OK) {
298 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
299 return E_PARCEL_OPERATION_FAILED;
300 }
301
302 if (!reply.ReadInt32(ret)) {
303 WS_HILOGE("PauseRunningWorks failed, read errCode error");
304 return E_PARCEL_OPERATION_FAILED;
305 }
306 return ret;
307 }
308
ResumePausedWorks(int32_t uid)309 int32_t WorkSchedServiceProxy::ResumePausedWorks(int32_t uid)
310 {
311 WS_HILOGD("Resume Paused Work Scheduler Work, uid:%{public}d", uid);
312 sptr<IRemoteObject> remote = Remote();
313 RETURN_IF_WITH_RET(remote == nullptr, E_CLIENT_CONNECT_SERVICE_FAILED);
314
315 MessageParcel data;
316 MessageParcel reply;
317 MessageOption option;
318 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
319 WS_HILOGE("ResumePausedWorks failed, write descriptor failed!");
320 return E_PARCEL_OPERATION_FAILED;
321 }
322
323 if (!data.WriteInt32(uid)) {
324 WS_HILOGE("ResumePausedWorks failed, write uid failed!");
325 return E_PARCEL_OPERATION_FAILED;
326 }
327 int32_t ret = remote->SendRequest(
328 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::RESUME_PAUSED_WORKS), data, reply, option);
329 if (ret != ERR_OK) {
330 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
331 return E_PARCEL_OPERATION_FAILED;
332 }
333
334 if (!reply.ReadInt32(ret)) {
335 WS_HILOGE("ResumePausedWorks failed, read errCode error");
336 return E_PARCEL_OPERATION_FAILED;
337 }
338 return ret;
339 }
340
SetWorkSchedulerConfig(const std::string & configData,int32_t sourceType)341 int32_t WorkSchedServiceProxy::SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType)
342 {
343 WS_HILOGD("Set work scheduler config");
344 sptr<IRemoteObject> remote = Remote();
345 RETURN_IF_WITH_RET(remote == nullptr, E_CLIENT_CONNECT_SERVICE_FAILED);
346
347 MessageParcel data;
348 MessageParcel reply;
349 MessageOption option;
350 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
351 WS_HILOGE("SetWorkSchedulerConfig failed, write descriptor failed!");
352 return E_PARCEL_OPERATION_FAILED;
353 }
354 if (!data.WriteString(configData)) {
355 WS_HILOGE("SetWorkSchedulerConfig failed, write configData failed!");
356 return E_PARCEL_OPERATION_FAILED;
357 }
358 if (!data.WriteInt32(sourceType)) {
359 WS_HILOGE("SetWorkSchedulerConfig failed, write sourceType failed!");
360 return E_PARCEL_OPERATION_FAILED;
361 }
362 int32_t ret = remote->SendRequest(
363 static_cast<int32_t>(IWorkSchedServiceInterfaceCode::SET_WORK_SCHEDULER_CONFIG), data, reply, option);
364 if (ret != ERR_OK) {
365 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
366 return E_PARCEL_OPERATION_FAILED;
367 }
368
369 if (!reply.ReadInt32(ret)) {
370 WS_HILOGE("SetWorkSchedulerConfig failed, read errCode error");
371 return E_PARCEL_OPERATION_FAILED;
372 }
373 return ret;
374 }
375 } // namespace WorkScheduler
376 } // namespace OHOS
377