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 "work_sched_errors.h"
20
21 #include "work_sched_common.h"
22
23 namespace OHOS {
24 namespace WorkScheduler {
StartWork(WorkInfo & workInfo)25 int32_t WorkSchedServiceProxy::StartWork(WorkInfo& workInfo)
26 {
27 WS_HILOGI("proxy Call StartWork come in");
28 sptr<IRemoteObject> remote = Remote();
29 RETURN_IF_WITH_RET(remote == nullptr, false);
30
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option;
34
35 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
36 WS_HILOGE("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
37 return E_PARCEL_OPERATION_FAILED;
38 }
39
40 WRITE_PARCEL_WITH_RET(data, Parcelable, &workInfo, false);
41
42 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::START_WORK), data, reply, option);
43 if (ret != ERR_OK) {
44 WS_HILOGE("SendRequest is failed, error code: %{public}d", ret);
45 return E_PARCEL_OPERATION_FAILED;
46 }
47 int32_t result = E_PARCEL_OPERATION_FAILED;
48 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
49 WS_HILOGD("after result : %{public}s ", std::to_string(result).c_str());
50 return result;
51 }
52
StopWork(WorkInfo & workInfo)53 int32_t WorkSchedServiceProxy::StopWork(WorkInfo& workInfo)
54 {
55 sptr<IRemoteObject> remote = Remote();
56 RETURN_IF_WITH_RET(remote == nullptr, false);
57
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option;
61
62 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
63 WS_HILOGE(" write descriptor failed!");
64 return E_PARCEL_OPERATION_FAILED;
65 }
66
67 WRITE_PARCEL_WITH_RET(data, Parcelable, &workInfo, false);
68
69 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::STOP_WORK), data, reply, option);
70 if (ret != ERR_OK) {
71 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
72 return E_PARCEL_OPERATION_FAILED;
73 }
74 int32_t result = E_PARCEL_OPERATION_FAILED;
75 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
76 return result;
77 }
78
StopAndCancelWork(WorkInfo & workInfo)79 int32_t WorkSchedServiceProxy::StopAndCancelWork(WorkInfo& workInfo)
80 {
81 sptr<IRemoteObject> remote = Remote();
82 RETURN_IF_WITH_RET(remote == nullptr, false);
83
84 MessageParcel data;
85 MessageParcel reply;
86 MessageOption option;
87
88 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
89 WS_HILOGE("write descriptor failed!");
90 return false;
91 }
92
93 WRITE_PARCEL_WITH_RET(data, Parcelable, &workInfo, false);
94 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::STOP_AND_CANCEL_WORK),
95 data, reply, option);
96 if (ret != ERR_OK) {
97 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
98 return E_PARCEL_OPERATION_FAILED;
99 }
100 int32_t result = E_PARCEL_OPERATION_FAILED;
101 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
102 return result;
103 }
104
StopAndClearWorks()105 int32_t WorkSchedServiceProxy::StopAndClearWorks()
106 {
107 sptr<IRemoteObject> remote = Remote();
108 RETURN_IF_WITH_RET(remote == nullptr, false);
109
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option;
113 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
114 WS_HILOGE("write descriptor failed!");
115 return E_PARCEL_OPERATION_FAILED;
116 }
117
118 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::STOP_AND_CLEAR_WORKS),
119 data, reply, option);
120 if (ret != ERR_OK) {
121 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
122 return E_PARCEL_OPERATION_FAILED;
123 }
124 int32_t result = E_PARCEL_OPERATION_FAILED;
125 READ_PARCEL_WITHOUT_RET(reply, Int32, result);
126 return result;
127 }
128
IsLastWorkTimeout(int32_t workId,bool & result)129 int32_t WorkSchedServiceProxy::IsLastWorkTimeout(int32_t workId, bool &result)
130 {
131 sptr<IRemoteObject> remote = Remote();
132 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
133
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option;
137 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
138 WS_HILOGE("write descriptor failed!");
139 return E_PARCEL_OPERATION_FAILED;
140 }
141
142 WRITE_PARCEL_WITHOUT_RET(data, Int32, workId);
143 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::IS_LAST_WORK_TIMEOUT),
144 data, reply, option);
145 if (ret != ERR_OK) {
146 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
147 return E_PARCEL_OPERATION_FAILED;
148 }
149 ret = E_PARCEL_OPERATION_FAILED;
150 READ_PARCEL_WITHOUT_RET(reply, Int32, ret);
151 READ_PARCEL_WITHOUT_RET(reply, Bool, result);
152 return ret;
153 }
154
ObtainAllWorks(int32_t & uid,int32_t & pid,std::list<std::shared_ptr<WorkInfo>> & workInfos)155 int32_t WorkSchedServiceProxy::ObtainAllWorks(int32_t &uid, int32_t &pid,
156 std::list<std::shared_ptr<WorkInfo>>& workInfos)
157 {
158 WS_HILOGD("uid: %{public}d, pid: %{public}d", uid, pid);
159 sptr<IRemoteObject> remote = Remote();
160 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
161
162 MessageParcel data;
163 MessageParcel reply;
164 MessageOption option;
165
166 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
167 WS_HILOGE("write descriptor failed!");
168 return E_PARCEL_OPERATION_FAILED;
169 }
170
171 WRITE_PARCEL_WITHOUT_RET(data, Int32, uid);
172 WRITE_PARCEL_WITHOUT_RET(data, Int32, pid);
173
174 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::OBTAIN_ALL_WORKS), data, reply, option);
175 if (ret != ERR_OK) {
176 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
177 return E_PARCEL_OPERATION_FAILED;
178 }
179
180 ErrCode errCode;
181 READ_PARCEL_WITHOUT_RET(reply, Int32, errCode);
182 int32_t worksize = 0;
183 READ_PARCEL_WITHOUT_RET(reply, Int32, worksize);
184 WS_HILOGD("ObtainAllWorks worksize from read parcel is: %{public}d", worksize);
185 for (int32_t i = 0; i < worksize; i++) {
186 sptr<WorkInfo> workInfo = reply.ReadStrongParcelable<WorkInfo>();
187 if (workInfo == nullptr) {
188 continue;
189 }
190 WS_HILOGD("WP read from parcel, workInfo ID: %{public}d", workInfo->GetWorkId());
191 workInfos.emplace_back(std::make_shared<WorkInfo>(*workInfo));
192 }
193 WS_HILOGD("return list size: %{public}zu", workInfos.size());
194 return errCode;
195 }
196
GetWorkStatus(int32_t & uid,int32_t & workId,std::shared_ptr<WorkInfo> & workInfo)197 int32_t WorkSchedServiceProxy::GetWorkStatus(int32_t &uid, int32_t &workId, std::shared_ptr<WorkInfo>& workInfo)
198 {
199 WS_HILOGD("enter, workId: %{public}d", workId);
200 sptr<IRemoteObject> remote = Remote();
201 RETURN_IF_WITH_RET(remote == nullptr, E_MEMORY_OPERATION_FAILED);
202 MessageParcel data;
203 MessageParcel reply;
204 MessageOption option;
205 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
206 WS_HILOGE("write descriptor failed!");
207 return E_PARCEL_OPERATION_FAILED;
208 }
209 WRITE_PARCEL_WITHOUT_RET(data, Int32, uid);
210 WRITE_PARCEL_WITHOUT_RET(data, Int32, workId);
211 int32_t ret = remote->SendRequest(static_cast<int32_t>(IWorkSchedService::GET_WORK_STATUS), data, reply, option);
212 if (ret != ERR_OK) {
213 WS_HILOGE("SendRequest is failed, err code: %{public}d", ret);
214 return E_PARCEL_OPERATION_FAILED;
215 }
216 ErrCode errCode;
217 READ_PARCEL_WITHOUT_RET(reply, Int32, errCode);
218 sptr<WorkInfo> workInfoSptr = reply.ReadStrongParcelable<WorkInfo>();
219 if (workInfoSptr == nullptr) {
220 return E_WORK_NOT_EXIST_FAILED;
221 }
222 workInfo = std::make_shared<WorkInfo>(*workInfoSptr);
223 return errCode;
224 }
225 } // namespace WorkScheduler
226 } // namespace OHOS
227