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 #include "iremote_broker.h"
18 #include "time_common.h"
19 #include "time_service_interface.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 using namespace OHOS::HiviewDFX;
24
TimeServiceProxy(const sptr<IRemoteObject> & object)25 TimeServiceProxy::TimeServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<ITimeService>(object)
26 {
27 }
28
SetTime(const int64_t time)29 int32_t TimeServiceProxy::SetTime(const int64_t time)
30 {
31 MessageParcel data, reply;
32 MessageOption option;
33
34 if (!data.WriteInterfaceToken(GetDescriptor())) {
35 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
36 return E_TIME_WRITE_PARCEL_ERROR;
37 }
38
39 if (!data.WriteInt64(time)) {
40 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
41 return E_TIME_WRITE_PARCEL_ERROR;
42 }
43 int32_t result = Remote()->SendRequest(SET_TIME, data, reply, option);
44 if (result != ERR_NONE) {
45 TIME_HILOGE(TIME_MODULE_CLIENT, "SetTime failed, error code is: %{public}d", result);
46 return result;
47 }
48 return result;
49 }
50
CreateTimer(int32_t type,bool repeat,uint64_t interval,sptr<IRemoteObject> & timerCallback)51 uint64_t TimeServiceProxy::CreateTimer(int32_t type, bool repeat, uint64_t interval, sptr<IRemoteObject> &timerCallback)
52 {
53 MessageParcel data, reply;
54 MessageOption option;
55
56 if (!data.WriteInterfaceToken(GetDescriptor())) {
57 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
58 return 0;
59 }
60
61 if (!data.WriteInt32(type)) {
62 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
63 return 0;
64 }
65
66 if (!data.WriteBool(repeat)) {
67 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
68 return 0;
69 }
70
71 if (!data.WriteUint64(interval)) {
72 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
73 return 0;
74 }
75
76 if (!data.WriteRemoteObject(timerCallback)) {
77 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
78 return 0;
79 }
80 int32_t result = Remote()->SendRequest(CREATE_TIMER, data, reply, option);
81 if (result != ERR_NONE) {
82 TIME_HILOGE(TIME_MODULE_CLIENT, "CreateTimer failed, error code is: %{public}d", result);
83 return 0;
84 }
85 auto TimerId = reply.ReadUint64();
86
87 return TimerId;
88 }
89
StartTimer(uint64_t timerId,uint64_t triggerTimes)90 bool TimeServiceProxy::StartTimer(uint64_t timerId, uint64_t triggerTimes)
91 {
92 MessageParcel data, reply;
93 MessageOption option;
94
95 if (!data.WriteInterfaceToken(GetDescriptor())) {
96 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
97 return false;
98 }
99
100 if (!data.WriteUint64(timerId)) {
101 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
102 return false;
103 }
104
105 if (!data.WriteUint64(triggerTimes)) {
106 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
107 return false;
108 }
109 int32_t result = Remote()->SendRequest(START_TIMER, data, reply, option);
110 if (result != ERR_NONE) {
111 TIME_HILOGE(TIME_MODULE_CLIENT, "Start failed, error code is: %{public}d", result);
112 return false;
113 }
114 return true;
115 }
116
StopTimer(uint64_t timerId)117 bool TimeServiceProxy::StopTimer(uint64_t timerId)
118 {
119 MessageParcel data, reply;
120 MessageOption option;
121
122 if (!data.WriteInterfaceToken(GetDescriptor())) {
123 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
124 return false;
125 }
126
127 if (!data.WriteUint64(timerId)) {
128 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
129 return false;
130 }
131 int32_t result = Remote()->SendRequest(STOP_TIMER, data, reply, option);
132 if (result != ERR_NONE) {
133 TIME_HILOGE(TIME_MODULE_CLIENT, "Stop failed, error code is: %{public}d", result);
134 return false;
135 }
136
137 return true;
138 }
DestroyTimer(uint64_t timerId)139 bool TimeServiceProxy::DestroyTimer(uint64_t timerId)
140 {
141 MessageParcel data, reply;
142 MessageOption option;
143
144 if (!data.WriteInterfaceToken(GetDescriptor())) {
145 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
146 return false;
147 }
148
149 if (!data.WriteUint64(timerId)) {
150 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
151 return false;
152 }
153 int32_t result = Remote()->SendRequest(DESTORY_TIMER, data, reply, option);
154 if (result != ERR_NONE) {
155 TIME_HILOGE(TIME_MODULE_CLIENT, "failed, error code is: %{public}d", result);
156 return false;
157 }
158
159 return true;
160 }
161
SetTimeZone(const std::string timezoneId)162 int32_t TimeServiceProxy::SetTimeZone(const std::string timezoneId)
163 {
164 MessageParcel data, reply;
165 MessageOption option;
166
167 if (!data.WriteInterfaceToken(GetDescriptor())) {
168 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
169 return E_TIME_WRITE_PARCEL_ERROR;
170 }
171
172 if (!data.WriteString(timezoneId)) {
173 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
174 return E_TIME_WRITE_PARCEL_ERROR;
175 }
176
177 int32_t result = Remote()->SendRequest(SET_TIME_ZONE, data, reply, option);
178 if (result != ERR_NONE) {
179 TIME_HILOGE(TIME_MODULE_CLIENT, "SetTimeZone failed, error code is: %{public}d", result);
180 return result;
181 }
182 return result;
183 }
184
GetTimeZone(std::string & timezoneId)185 int32_t TimeServiceProxy::GetTimeZone(std::string &timezoneId)
186 {
187 MessageParcel data, reply;
188 MessageOption option;
189
190 if (!data.WriteInterfaceToken(GetDescriptor())) {
191 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
192 return E_TIME_WRITE_PARCEL_ERROR;
193 }
194
195 int32_t result = Remote()->SendRequest(GET_TIME_ZONE, data, reply, option);
196 if (result != ERR_NONE) {
197 TIME_HILOGE(TIME_MODULE_CLIENT, "GetTimeZone failed, error code is: %{public}d", result);
198 return result;
199 }
200 timezoneId = reply.ReadString();
201 return result;
202 }
203
GetWallTimeMs(int64_t & times)204 int32_t TimeServiceProxy::GetWallTimeMs(int64_t ×)
205 {
206 MessageParcel data, reply;
207 MessageOption option;
208
209 if (!data.WriteInterfaceToken(GetDescriptor())) {
210 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
211 return E_TIME_WRITE_PARCEL_ERROR;
212 }
213
214 int32_t result = Remote()->SendRequest(GET_WALL_TIME_MILLI, data, reply, option);
215 if (result != ERR_NONE) {
216 TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeMs failed, error code is: %{public}d", result);
217 return result;
218 }
219 times = reply.ReadInt64();
220 return result;
221 }
222
GetWallTimeNs(int64_t & times)223 int32_t TimeServiceProxy::GetWallTimeNs(int64_t ×)
224 {
225 MessageParcel data, reply;
226 MessageOption option;
227
228 if (!data.WriteInterfaceToken(GetDescriptor())) {
229 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
230 return E_TIME_WRITE_PARCEL_ERROR;
231 }
232
233 int32_t result = Remote()->SendRequest(GET_WALL_TIME_NANO, data, reply, option);
234 if (result != ERR_NONE) {
235 TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeNs failed, error code is: %{public}d", result);
236 return result;
237 }
238 times = reply.ReadInt64();
239 return result;
240 }
241
GetBootTimeMs(int64_t & times)242 int32_t TimeServiceProxy::GetBootTimeMs(int64_t ×)
243 {
244 MessageParcel data, reply;
245 MessageOption option;
246
247 if (!data.WriteInterfaceToken(GetDescriptor())) {
248 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
249 return E_TIME_WRITE_PARCEL_ERROR;
250 }
251
252 int32_t result = Remote()->SendRequest(GET_BOOT_TIME_MILLI, data, reply, option);
253 if (result != ERR_NONE) {
254 TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeMs failed, error code is: %{public}d", result);
255 return result;
256 }
257 times = reply.ReadInt64();
258 return result;
259 }
260
GetBootTimeNs(int64_t & times)261 int32_t TimeServiceProxy::GetBootTimeNs(int64_t ×)
262 {
263 MessageParcel data, reply;
264 MessageOption option;
265
266 if (!data.WriteInterfaceToken(GetDescriptor())) {
267 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
268 return E_TIME_WRITE_PARCEL_ERROR;
269 }
270
271 int32_t result = Remote()->SendRequest(GET_BOOT_TIME_NANO, data, reply, option);
272 if (result != ERR_NONE) {
273 TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeNs failed, error code is: %{public}d", result);
274 return result;
275 }
276 times = reply.ReadInt64();
277 return result;
278 }
279
GetMonotonicTimeMs(int64_t & times)280 int32_t TimeServiceProxy::GetMonotonicTimeMs(int64_t ×)
281 {
282 MessageParcel data, reply;
283 MessageOption option;
284
285 if (!data.WriteInterfaceToken(GetDescriptor())) {
286 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
287 return E_TIME_WRITE_PARCEL_ERROR;
288 }
289
290 int32_t result = Remote()->SendRequest(GET_MONO_TIME_MILLI, data, reply, option);
291 if (result != ERR_NONE) {
292 TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeMs failed, error code is: %{public}d", result);
293 return result;
294 }
295 times = reply.ReadInt64();
296 return result;
297 }
298
GetMonotonicTimeNs(int64_t & times)299 int32_t TimeServiceProxy::GetMonotonicTimeNs(int64_t ×)
300 {
301 MessageParcel data, reply;
302 MessageOption option;
303
304 if (!data.WriteInterfaceToken(GetDescriptor())) {
305 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
306 return E_TIME_WRITE_PARCEL_ERROR;
307 }
308
309 int32_t result = Remote()->SendRequest(GET_MONO_TIME_NANO, data, reply, option);
310 if (result != ERR_NONE) {
311 TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeNs failed, error code is: %{public}d", result);
312 return result;
313 }
314 times = reply.ReadInt64();
315 return result;
316 }
317
GetThreadTimeMs(int64_t & times)318 int32_t TimeServiceProxy::GetThreadTimeMs(int64_t ×)
319 {
320 MessageParcel data, reply;
321 MessageOption option;
322
323 if (!data.WriteInterfaceToken(GetDescriptor())) {
324 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
325 return E_TIME_WRITE_PARCEL_ERROR;
326 }
327
328 int32_t result = Remote()->SendRequest(GET_THREAD_TIME_MILLI, data, reply, option);
329 if (result != ERR_NONE) {
330 TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeMs failed, error code is: %{public}d", result);
331 return result;
332 }
333 times = reply.ReadInt64();
334 return result;
335 }
336
GetThreadTimeNs(int64_t & times)337 int32_t TimeServiceProxy::GetThreadTimeNs(int64_t ×)
338 {
339 MessageParcel data, reply;
340 MessageOption option;
341
342 if (!data.WriteInterfaceToken(GetDescriptor())) {
343 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
344 return E_TIME_WRITE_PARCEL_ERROR;
345 }
346
347 int32_t result = Remote()->SendRequest(GET_THREAD_TIME_NANO, data, reply, option);
348 if (result != ERR_NONE) {
349 TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeNs failed, error code is: %{public}d", result);
350 return result;
351 }
352 times = reply.ReadInt64();
353 return result;
354 }
355
NetworkTimeStatusOn()356 void TimeServiceProxy::NetworkTimeStatusOn()
357 {
358 MessageParcel data, reply;
359 MessageOption option;
360
361 if (!data.WriteInterfaceToken(GetDescriptor())) {
362 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
363 return;
364 }
365
366 int32_t result = Remote()->SendRequest(NETWORK_TIME_ON, data, reply, option);
367 if (result != ERR_NONE) {
368 TIME_HILOGE(TIME_MODULE_CLIENT, "NetworkTimeStatusOn failed, error code is: %{public}d", result);
369 return;
370 }
371 return;
372 }
373
NetworkTimeStatusOff()374 void TimeServiceProxy::NetworkTimeStatusOff()
375 {
376 MessageParcel data, reply;
377 MessageOption option;
378
379 if (!data.WriteInterfaceToken(GetDescriptor())) {
380 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
381 return;
382 }
383
384 int32_t result = Remote()->SendRequest(NETWORK_TIME_OFF, data, reply, option);
385 if (result != ERR_NONE) {
386 TIME_HILOGE(TIME_MODULE_CLIENT, "NetworkTimeStatusOff failed, error code is: %{public}d", result);
387 return;
388 }
389
390 return;
391 }
392 } // namespace MiscServices
393 } // namespace OHOS