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 <string_ex.h>
20
21 #include "work_sched_common.h"
22
23 namespace OHOS {
24 namespace WorkScheduler {
StartWork(WorkInfo & workInfo)25 bool 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 false;
38 }
39
40 WRITE_PARCEL_WITH_RET(data, Parcelable, &workInfo, false);
41
42 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::START_WORK), data, reply, option);
43 if (ret != ERR_OK) {
44 WS_HILOGE("WorkSchedServiceProxy::%{public}s SendRequest is failed, error code: %{public}d",
45 __func__, ret);
46 return false;
47 }
48 bool result = false;
49 READ_PARCEL_WITH_RET(reply, Bool, result, false);
50 WS_HILOGD("after result : %{public}s ", std::to_string(result).c_str());
51 return result;
52 }
53
StopWork(WorkInfo & workInfo)54 bool WorkSchedServiceProxy::StopWork(WorkInfo& workInfo)
55 {
56 sptr<IRemoteObject> remote = Remote();
57 RETURN_IF_WITH_RET(remote == nullptr, false);
58
59 MessageParcel data;
60 MessageParcel reply;
61 MessageOption option;
62
63 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
64 WS_HILOGE("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
65 return false;
66 }
67
68 WRITE_PARCEL_WITH_RET(data, Parcelable, &workInfo, false);
69
70 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::STOP_WORK), data, reply, option);
71 if (ret != ERR_OK) {
72 WS_HILOGE("WorkSchedServiceProxy::%{public}s SendRequest is failed, err code: %{public}d", __func__, ret);
73 return false;
74 }
75 bool result = false;
76 READ_PARCEL_WITH_RET(reply, Bool, result, false);
77 return result;
78 }
79
StopAndCancelWork(WorkInfo & workInfo)80 bool WorkSchedServiceProxy::StopAndCancelWork(WorkInfo& workInfo)
81 {
82 sptr<IRemoteObject> remote = Remote();
83 RETURN_IF_WITH_RET(remote == nullptr, false);
84
85 MessageParcel data;
86 MessageParcel reply;
87 MessageOption option;
88
89 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
90 WS_HILOGE("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
91 return false;
92 }
93
94 WRITE_PARCEL_WITH_RET(data, Parcelable, &workInfo, false);
95 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::STOP_AND_CANCEL_WORK), data, reply, option);
96 if (ret != ERR_OK) {
97 WS_HILOGE("WorkSchedServiceProxy::%{public}s SendRequest is failed, err code: %{public}d", __func__, ret);
98 return false;
99 }
100 bool result = false;
101 READ_PARCEL_WITH_RET(reply, Bool, result, false);
102 return result;
103 }
104
StopAndClearWorks()105 bool 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("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
115 return false;
116 }
117
118 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::STOP_AND_CLEAR_WORKS), data, reply, option);
119 if (ret != ERR_OK) {
120 WS_HILOGE("WorkSchedServiceProxy::%{public}s SendRequest is failed, err code: %{public}d", __func__, ret);
121 return false;
122 }
123 bool result = false;
124 READ_PARCEL_WITH_RET(reply, Bool, result, false);
125 return result;
126 }
127
IsLastWorkTimeout(int32_t workId)128 bool WorkSchedServiceProxy::IsLastWorkTimeout(int32_t workId)
129 {
130 sptr<IRemoteObject> remote = Remote();
131 RETURN_IF_WITH_RET(remote == nullptr, false);
132
133 MessageParcel data;
134 MessageParcel reply;
135 MessageOption option;
136 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
137 WS_HILOGE("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
138 return false;
139 }
140
141 WRITE_PARCEL_WITHOUT_RET(data, Int32, workId);
142 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::IS_LAST_WORK_TIMEOUT), data, reply, option);
143 if (ret != ERR_OK) {
144 WS_HILOGE("WorkSchedServiceProxy::%{public}s SendRequest is failed, err code: %{public}d", __func__, ret);
145 return false;
146 }
147 bool result = false;
148 READ_PARCEL_WITH_RET(reply, Bool, result, false);
149 return result;
150 }
151
ObtainAllWorks(int32_t & uid,int32_t & pid)152 std::list<std::shared_ptr<WorkInfo>> WorkSchedServiceProxy::ObtainAllWorks(int32_t &uid, int32_t &pid)
153 {
154 WS_HILOGD("BUGOAWF function %{public}s invoked.", __func__);
155 WS_HILOGD("%{public}s uid: %{public}d, pid: %{public}d", __func__, uid, pid);
156 std::list<std::shared_ptr<WorkInfo>> workInfos;
157 sptr<IRemoteObject> remote = Remote();
158 RETURN_IF_WITH_RET(remote == nullptr, workInfos);
159
160 MessageParcel data;
161 MessageParcel reply;
162 MessageOption option;
163
164 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
165 WS_HILOGE("WorkSchedServiceProxy::%{public}s write descriptor failed!", __func__);
166 return workInfos;
167 }
168
169 WRITE_PARCEL_WITHOUT_RET(data, Int32, uid);
170 WRITE_PARCEL_WITHOUT_RET(data, Int32, pid);
171
172 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::OBTAIN_ALL_WORKS), data, reply, option);
173 if (ret != ERR_OK) {
174 WS_HILOGE("WorkSchedServiceProxy::%{public}s SendRequest is failed, err code: %{public}d",
175 __func__, ret);
176 return workInfos;
177 }
178
179 int32_t worksize = 0;
180 READ_PARCEL_WITHOUT_RET(reply, Int32, worksize);
181 WS_HILOGD("BUGOAWF WSSP ObtainAllWorks worksize from read parcel is: %{public}d", worksize);
182 for (int32_t i = 0; i < worksize; i++) {
183 sptr<WorkInfo> workInfo = reply.ReadStrongParcelable<WorkInfo>();
184 if (workInfo == nullptr) {
185 continue;
186 }
187 WS_HILOGD("WP read from parcel, workInfo ID: %{public}d", workInfo->GetWorkId());
188 workInfos.emplace_back(std::make_shared<WorkInfo>(*workInfo));
189 }
190 WS_HILOGD("WorkSchedServiceProxy::%{public}s return list size: %{public}d", __func__, workInfos.size());
191 return workInfos;
192 }
193
GetWorkStatus(int32_t & uid,int32_t & workId)194 std::shared_ptr<WorkInfo> WorkSchedServiceProxy::GetWorkStatus(int32_t &uid, int32_t &workId)
195 {
196 WS_HILOGD("%{public}s enter, workId: %{public}d", __func__, workId);
197 sptr<IRemoteObject> remote = Remote();
198 RETURN_IF_WITH_RET(remote == nullptr, nullptr);
199 MessageParcel data;
200 MessageParcel reply;
201 MessageOption option;
202 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
203 WS_HILOGE("%{public}s write descriptor failed!", __func__);
204 return nullptr;
205 }
206 WRITE_PARCEL_WITHOUT_RET(data, Int32, uid);
207 WRITE_PARCEL_WITHOUT_RET(data, Int32, workId);
208 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::GET_WORK_STATUS), data, reply, option);
209 if (ret != ERR_OK) {
210 WS_HILOGE("%{pulbic}s SendRequest is failed, err code: %{public}d", __func__, ret);
211 return nullptr;
212 }
213 sptr<WorkInfo> workInfo = reply.ReadStrongParcelable<WorkInfo>();
214 if (workInfo == nullptr) {
215 return nullptr;
216 }
217 return std::make_shared<WorkInfo>(*workInfo);
218 }
219
ShellDump(const std::vector<std::string> & dumpOption,std::vector<std::string> & dumpInfo)220 bool WorkSchedServiceProxy::ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo)
221 {
222 sptr<IRemoteObject> remote = Remote();
223 RETURN_IF_WITH_RET(remote == nullptr, false);
224 MessageParcel data;
225 MessageParcel reply;
226 MessageOption option;
227 if (!data.WriteInterfaceToken(WorkSchedServiceProxy::GetDescriptor())) {
228 WS_HILOGE("%{public}s write descriptor failed!", __func__);
229 return false;
230 }
231 if (!data.WriteStringVector(dumpOption)) {
232 WS_HILOGE("[ShellDump] fail: write option failed.");
233 return false;
234 }
235 int ret = remote->SendRequest(static_cast<int>(IWorkSchedService::DUMP_INFO), data, reply, option);
236 if (ret != ERR_OK) {
237 WS_HILOGE("%{public}s SendRequest is failed, err code: %{public}d", __func__, ret);
238 return false;
239 }
240 if (!reply.ReadBool()) {
241 WS_HILOGE("[ShellDump] fail: read result failed.");
242 return false;
243 }
244 if (!reply.ReadStringVector(&dumpInfo)) {
245 WS_HILOGE("[ShellDump] fail: read dumpInfo failed.");
246 return false;
247 }
248 return true;
249 }
250 } // namespace WorkScheduler
251 } // namespace OHOS