1 /*
2 * Copyright (C) 2021 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 "time_service_proxy.h"
17
18 #include "iremote_broker.h"
19 #include "time_common.h"
20 #include "time_service_ipc_interface_code.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace OHOS::HiviewDFX;
25
TimeServiceProxy(const sptr<IRemoteObject> & object)26 TimeServiceProxy::TimeServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<ITimeService>(object)
27 {
28 }
29
SetTime(int64_t time,APIVersion apiVersion)30 int32_t TimeServiceProxy::SetTime(int64_t time, APIVersion apiVersion)
31 {
32 MessageParcel data, reply;
33 MessageOption option;
34 if (!data.WriteInterfaceToken(GetDescriptor())) {
35 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
36 return E_TIME_WRITE_PARCEL_ERROR;
37 }
38 if (!data.WriteInt64(time)) {
39 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write time");
40 return E_TIME_WRITE_PARCEL_ERROR;
41 }
42 if (!data.WriteInt8(apiVersion)) {
43 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write apiVersion");
44 return E_TIME_WRITE_PARCEL_ERROR;
45 }
46 int32_t result =
47 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME), data, reply, option);
48 if (result != ERR_NONE) {
49 TIME_HILOGE(TIME_MODULE_CLIENT, "SetTime failed, error code is: %{public}d", result);
50 return result;
51 }
52 return result;
53 }
54
CreateTimer(const std::shared_ptr<ITimerInfo> & timerOptions,sptr<IRemoteObject> & timerCallback,uint64_t & timerId)55 int32_t TimeServiceProxy::CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions,
56 sptr<IRemoteObject> &timerCallback, uint64_t &timerId)
57 {
58 MessageParcel data, reply;
59 MessageOption option;
60 if (!data.WriteInterfaceToken(GetDescriptor())) {
61 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
62 return E_TIME_WRITE_PARCEL_ERROR;
63 }
64 if (!data.WriteInt32(timerOptions->type)) {
65 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write type");
66 return E_TIME_WRITE_PARCEL_ERROR;
67 }
68 if (!data.WriteBool(timerOptions->repeat)) {
69 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write repeat");
70 return E_TIME_WRITE_PARCEL_ERROR;
71 }
72 if (!data.WriteUint64(timerOptions->interval)) {
73 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write interval");
74 return E_TIME_WRITE_PARCEL_ERROR;
75 }
76 if (!data.WriteBool(timerOptions->wantAgent != nullptr)) {
77 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write wantAgent status");
78 return E_TIME_WRITE_PARCEL_ERROR;
79 }
80 if (timerOptions->wantAgent != nullptr) {
81 if (!data.WriteParcelable(&(*timerOptions->wantAgent))) {
82 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write wantAgent");
83 return E_TIME_WRITE_PARCEL_ERROR;
84 }
85 }
86 if (!data.WriteRemoteObject(timerCallback)) {
87 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerCallback");
88 return E_TIME_WRITE_PARCEL_ERROR;
89 }
90 if (!data.WriteUint64(timerId)) {
91 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
92 return E_TIME_WRITE_PARCEL_ERROR;
93 }
94 auto ret =
95 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::CREATE_TIMER), data, reply, option);
96 if (ret == E_TIME_OK) {
97 timerId = reply.ReadUint64();
98 return E_TIME_OK;
99 }
100 return ret;
101 }
102
StartTimer(uint64_t timerId,uint64_t triggerTime)103 int32_t TimeServiceProxy::StartTimer(uint64_t timerId, uint64_t triggerTime)
104 {
105 MessageParcel data, reply;
106 MessageOption option;
107 if (!data.WriteInterfaceToken(GetDescriptor())) {
108 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
109 return E_TIME_WRITE_PARCEL_ERROR;
110 }
111 if (!data.WriteUint64(timerId)) {
112 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
113 return E_TIME_WRITE_PARCEL_ERROR;
114 }
115 if (!data.WriteUint64(triggerTime)) {
116 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write triggerTime");
117 return E_TIME_WRITE_PARCEL_ERROR;
118 }
119 return Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::START_TIMER), data, reply, option);
120 }
121
StopTimer(uint64_t timerId)122 int32_t TimeServiceProxy::StopTimer(uint64_t timerId)
123 {
124 MessageParcel data, reply;
125 MessageOption option;
126 if (!data.WriteInterfaceToken(GetDescriptor())) {
127 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
128 return E_TIME_WRITE_PARCEL_ERROR;
129 }
130 if (!data.WriteUint64(timerId)) {
131 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
132 return E_TIME_WRITE_PARCEL_ERROR;
133 }
134 return Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::STOP_TIMER), data, reply, option);
135 }
136
DestroyTimer(uint64_t timerId)137 int32_t TimeServiceProxy::DestroyTimer(uint64_t timerId)
138 {
139 MessageParcel data, reply;
140 MessageOption option;
141 if (!data.WriteInterfaceToken(GetDescriptor())) {
142 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
143 return E_TIME_WRITE_PARCEL_ERROR;
144 }
145 if (!data.WriteUint64(timerId)) {
146 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
147 return E_TIME_WRITE_PARCEL_ERROR;
148 }
149 return Remote()->SendRequest(
150 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::DESTROY_TIMER), data, reply, option);
151 }
152
SetTimeZone(const std::string & timeZoneId,APIVersion apiVersion)153 int32_t TimeServiceProxy::SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion)
154 {
155 MessageParcel data, reply;
156 MessageOption option;
157 if (!data.WriteInterfaceToken(GetDescriptor())) {
158 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
159 return E_TIME_WRITE_PARCEL_ERROR;
160 }
161 if (!data.WriteString(timeZoneId)) {
162 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timeZoneId");
163 return E_TIME_WRITE_PARCEL_ERROR;
164 }
165 if (!data.WriteInt8(apiVersion)) {
166 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write apiVersion");
167 return E_TIME_WRITE_PARCEL_ERROR;
168 }
169 int32_t result =
170 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME_ZONE), data, reply, option);
171 if (result != ERR_NONE) {
172 TIME_HILOGE(TIME_MODULE_CLIENT, "SetTimeZone failed, error code is: %{public}d", result);
173 return result;
174 }
175 return result;
176 }
177
GetTimeZone(std::string & timeZoneId)178 int32_t TimeServiceProxy::GetTimeZone(std::string &timeZoneId)
179 {
180 MessageParcel data, reply;
181 MessageOption option;
182 if (!data.WriteInterfaceToken(GetDescriptor())) {
183 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
184 return E_TIME_WRITE_PARCEL_ERROR;
185 }
186 int32_t result =
187 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_TIME_ZONE), data, reply, option);
188 if (result != ERR_NONE) {
189 TIME_HILOGE(TIME_MODULE_CLIENT, "GetTimeZone failed, error code is: %{public}d", result);
190 return result;
191 }
192 timeZoneId = reply.ReadString();
193 return result;
194 }
195
GetWallTimeMs(int64_t & times)196 int32_t TimeServiceProxy::GetWallTimeMs(int64_t ×)
197 {
198 MessageParcel data, reply;
199 MessageOption option;
200 if (!data.WriteInterfaceToken(GetDescriptor())) {
201 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
202 return E_TIME_WRITE_PARCEL_ERROR;
203 }
204 int32_t result = Remote()->SendRequest(
205 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_WALL_TIME_MILLI), data, reply, option);
206 if (result != ERR_NONE) {
207 TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeMs failed, error code is: %{public}d", result);
208 return result;
209 }
210 times = reply.ReadInt64();
211 return result;
212 }
213
GetWallTimeNs(int64_t & times)214 int32_t TimeServiceProxy::GetWallTimeNs(int64_t ×)
215 {
216 MessageParcel data, reply;
217 MessageOption option;
218 if (!data.WriteInterfaceToken(GetDescriptor())) {
219 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
220 return E_TIME_WRITE_PARCEL_ERROR;
221 }
222 int32_t result = Remote()->SendRequest(
223 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_WALL_TIME_NANO), data, reply, option);
224 if (result != ERR_NONE) {
225 TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeNs failed, error code is: %{public}d", result);
226 return result;
227 }
228 times = reply.ReadInt64();
229 return result;
230 }
231
GetBootTimeMs(int64_t & times)232 int32_t TimeServiceProxy::GetBootTimeMs(int64_t ×)
233 {
234 MessageParcel data, reply;
235 MessageOption option;
236 if (!data.WriteInterfaceToken(GetDescriptor())) {
237 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
238 return E_TIME_WRITE_PARCEL_ERROR;
239 }
240 int32_t result = Remote()->SendRequest(
241 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_BOOT_TIME_MILLI), data, reply, option);
242 if (result != ERR_NONE) {
243 TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeMs failed, error code is: %{public}d", result);
244 return result;
245 }
246 times = reply.ReadInt64();
247 return result;
248 }
249
GetBootTimeNs(int64_t & times)250 int32_t TimeServiceProxy::GetBootTimeNs(int64_t ×)
251 {
252 MessageParcel data, reply;
253 MessageOption option;
254 if (!data.WriteInterfaceToken(GetDescriptor())) {
255 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
256 return E_TIME_WRITE_PARCEL_ERROR;
257 }
258 int32_t result = Remote()->SendRequest(
259 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_BOOT_TIME_NANO), data, reply, option);
260 if (result != ERR_NONE) {
261 TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeNs failed, error code is: %{public}d", result);
262 return result;
263 }
264 times = reply.ReadInt64();
265 return result;
266 }
267
GetMonotonicTimeMs(int64_t & times)268 int32_t TimeServiceProxy::GetMonotonicTimeMs(int64_t ×)
269 {
270 MessageParcel data, reply;
271 MessageOption option;
272 if (!data.WriteInterfaceToken(GetDescriptor())) {
273 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
274 return E_TIME_WRITE_PARCEL_ERROR;
275 }
276 int32_t result = Remote()->SendRequest(
277 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_MONO_TIME_MILLI), data, reply, option);
278 if (result != ERR_NONE) {
279 TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeMs failed, error code is: %{public}d", result);
280 return result;
281 }
282 times = reply.ReadInt64();
283 return result;
284 }
285
GetMonotonicTimeNs(int64_t & times)286 int32_t TimeServiceProxy::GetMonotonicTimeNs(int64_t ×)
287 {
288 MessageParcel data, reply;
289 MessageOption option;
290 if (!data.WriteInterfaceToken(GetDescriptor())) {
291 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
292 return E_TIME_WRITE_PARCEL_ERROR;
293 }
294 int32_t result = Remote()->SendRequest(
295 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_MONO_TIME_NANO), data, reply, option);
296 if (result != ERR_NONE) {
297 TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeNs failed, error code is: %{public}d", result);
298 return result;
299 }
300 times = reply.ReadInt64();
301 return result;
302 }
303
GetThreadTimeMs(int64_t & times)304 int32_t TimeServiceProxy::GetThreadTimeMs(int64_t ×)
305 {
306 MessageParcel data, reply;
307 MessageOption option;
308 if (!data.WriteInterfaceToken(GetDescriptor())) {
309 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
310 return E_TIME_WRITE_PARCEL_ERROR;
311 }
312 int32_t result = Remote()->SendRequest(
313 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI), data, reply, option);
314 if (result != ERR_NONE) {
315 TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeMs failed, error code is: %{public}d", result);
316 return result;
317 }
318 times = reply.ReadInt64();
319 return result;
320 }
321
GetThreadTimeNs(int64_t & times)322 int32_t TimeServiceProxy::GetThreadTimeNs(int64_t ×)
323 {
324 MessageParcel data, reply;
325 MessageOption option;
326 if (!data.WriteInterfaceToken(GetDescriptor())) {
327 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
328 return E_TIME_WRITE_PARCEL_ERROR;
329 }
330 int32_t result = Remote()->SendRequest(
331 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO), data, reply, option);
332 if (result != ERR_NONE) {
333 TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeNs failed, error code is: %{public}d", result);
334 return result;
335 }
336 times = reply.ReadInt64();
337 return result;
338 }
339
ProxyTimer(int32_t uid,bool isProxy,bool needRetrigger)340 bool TimeServiceProxy::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger)
341 {
342 MessageParcel data, reply;
343 MessageOption option;
344 if (!data.WriteInterfaceToken(GetDescriptor())) {
345 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
346 return false;
347 }
348 if (!data.WriteInt32(uid)) {
349 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write uid");
350 return false;
351 }
352 if (!data.WriteBool(isProxy)) {
353 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write isProxy");
354 return false;
355 }
356 if (!data.WriteBool(needRetrigger)) {
357 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write needRetrigger");
358 return false;
359 }
360
361 int32_t result =
362 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::PROXY_TIMER), data, reply, option);
363 if (result != ERR_NONE) {
364 TIME_HILOGE(TIME_MODULE_CLIENT, "ProxyTimer failed, error code is: %{public}d", result);
365 return false;
366 }
367 return true;
368 }
369
ProxyTimer(std::set<int> pidList,bool isProxy,bool needRetrigger)370 bool TimeServiceProxy::ProxyTimer(std::set<int> pidList, bool isProxy, bool needRetrigger)
371 {
372 MessageParcel data, reply;
373 MessageOption option;
374 if (!data.WriteInterfaceToken(GetDescriptor())) {
375 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
376 return false;
377 }
378 if (!data.WriteInt32(pidList.size())) {
379 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write pid size");
380 return false;
381 }
382 for (std::set<int>::iterator pid = pidList.begin(); pid != pidList.end(); ++pid) {
383 if (!data.WriteInt32(*pid)) {
384 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write pid");
385 return false;
386 }
387 }
388 if (!data.WriteBool(isProxy)) {
389 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write isProxy");
390 return false;
391 }
392 if (!data.WriteBool(needRetrigger)) {
393 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write needRetrigger");
394 return false;
395 }
396
397 int32_t result =
398 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::PID_PROXY_TIMER), data, reply, option);
399 if (result != ERR_NONE) {
400 TIME_HILOGE(TIME_MODULE_CLIENT, "ProxyTimer failed, error code is: %{public}d", result);
401 return false;
402 }
403 return true;
404 }
405
AdjustTimer(bool isAdjust,uint32_t interval)406 int32_t TimeServiceProxy::AdjustTimer(bool isAdjust, uint32_t interval)
407 {
408 MessageParcel data;
409 MessageParcel reply;
410 MessageOption option;
411 if (!data.WriteInterfaceToken(GetDescriptor())) {
412 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
413 return E_TIME_WRITE_PARCEL_ERROR;
414 }
415 if (!data.WriteBool(isAdjust)) {
416 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write adjust state");
417 return E_TIME_WRITE_PARCEL_ERROR;
418 }
419 if (!data.WriteUint32(interval)) {
420 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write interval");
421 return E_TIME_WRITE_PARCEL_ERROR;
422 }
423 int32_t result =
424 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::ADJUST_TIMER), data, reply, option);
425 if (result != ERR_NONE) {
426 TIME_HILOGE(TIME_MODULE_CLIENT, "Adjust Timer failed, error code is: %{public}d", result);
427 return result;
428 }
429 return result;
430 }
431
SetTimerExemption(const std::unordered_set<std::string> & nameArr,bool isExemption)432 int32_t TimeServiceProxy::SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption)
433 {
434 MessageParcel data;
435 MessageParcel reply;
436 MessageOption option;
437 if (!data.WriteInterfaceToken(GetDescriptor())) {
438 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
439 return E_TIME_WRITE_PARCEL_ERROR;
440 }
441
442 if (nameArr.empty()) {
443 TIME_HILOGE(TIME_MODULE_CLIENT, "Nothing need cache");
444 return E_TIME_NOT_FOUND;
445 }
446
447 uint32_t size = static_cast<uint32_t>(nameArr.size());
448 if (!data.WriteUint32(size)) {
449 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write size");
450 return E_TIME_WRITE_PARCEL_ERROR;
451 }
452
453 for (auto name : nameArr) {
454 if (!data.WriteString(name)) {
455 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write name");
456 return E_TIME_WRITE_PARCEL_ERROR;
457 }
458 }
459
460 if (!data.WriteBool(isExemption)) {
461 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write is exemption");
462 return E_TIME_WRITE_PARCEL_ERROR;
463 }
464
465 int32_t result =
466 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIMER_EXEMPTION),
467 data, reply, option);
468 if (result != ERR_NONE) {
469 TIME_HILOGE(TIME_MODULE_CLIENT, "Set Timer Exemption failed, error code is: %{public}d", result);
470 return result;
471 }
472 return result;
473 }
474
ResetAllProxy()475 bool TimeServiceProxy::ResetAllProxy()
476 {
477 MessageParcel data, reply;
478 MessageOption option;
479 if (!data.WriteInterfaceToken(GetDescriptor())) {
480 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
481 return false;
482 }
483 int32_t result = Remote()->SendRequest(
484 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::RESET_ALL_PROXY), data, reply, option);
485 if (result != ERR_NONE) {
486 TIME_HILOGE(TIME_MODULE_CLIENT, "ProxyTimer failed, error code is: %{public}d", result);
487 return false;
488 }
489 return true;
490 }
491 } // namespace MiscServices
492 } // namespace OHOS