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