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(const int64_t time,APIVersion apiVersion)30 int32_t TimeServiceProxy::SetTime(const 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 auto ret =
91 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::CREATE_TIMER), data, reply, option);
92 if (ret == E_TIME_OK) {
93 timerId = reply.ReadUint64();
94 return E_TIME_OK;
95 }
96 return ret;
97 }
98
StartTimer(uint64_t timerId,uint64_t triggerTime)99 int32_t TimeServiceProxy::StartTimer(uint64_t timerId, uint64_t triggerTime)
100 {
101 MessageParcel data, reply;
102 MessageOption option;
103 if (!data.WriteInterfaceToken(GetDescriptor())) {
104 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
105 return E_TIME_WRITE_PARCEL_ERROR;
106 }
107 if (!data.WriteUint64(timerId)) {
108 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
109 return E_TIME_WRITE_PARCEL_ERROR;
110 }
111 if (!data.WriteUint64(triggerTime)) {
112 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write triggerTime");
113 return E_TIME_WRITE_PARCEL_ERROR;
114 }
115 return Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::START_TIMER), data, reply, option);
116 }
117
StopTimer(uint64_t timerId)118 int32_t TimeServiceProxy::StopTimer(uint64_t timerId)
119 {
120 MessageParcel data, reply;
121 MessageOption option;
122 if (!data.WriteInterfaceToken(GetDescriptor())) {
123 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
124 return E_TIME_WRITE_PARCEL_ERROR;
125 }
126 if (!data.WriteUint64(timerId)) {
127 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
128 return E_TIME_WRITE_PARCEL_ERROR;
129 }
130 return Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::STOP_TIMER), data, reply, option);
131 }
132
DestroyTimer(uint64_t timerId)133 int32_t TimeServiceProxy::DestroyTimer(uint64_t timerId)
134 {
135 MessageParcel data, reply;
136 MessageOption option;
137 if (!data.WriteInterfaceToken(GetDescriptor())) {
138 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
139 return E_TIME_WRITE_PARCEL_ERROR;
140 }
141 if (!data.WriteUint64(timerId)) {
142 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timerId");
143 return E_TIME_WRITE_PARCEL_ERROR;
144 }
145 return Remote()->SendRequest(
146 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::DESTROY_TIMER), data, reply, option);
147 }
148
SetTimeZone(const std::string & timeZoneId,APIVersion apiVersion)149 int32_t TimeServiceProxy::SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion)
150 {
151 MessageParcel data, reply;
152 MessageOption option;
153 if (!data.WriteInterfaceToken(GetDescriptor())) {
154 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
155 return E_TIME_WRITE_PARCEL_ERROR;
156 }
157 if (!data.WriteString(timeZoneId)) {
158 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write timeZoneId");
159 return E_TIME_WRITE_PARCEL_ERROR;
160 }
161 if (!data.WriteInt8(apiVersion)) {
162 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write apiVersion");
163 return E_TIME_WRITE_PARCEL_ERROR;
164 }
165 int32_t result =
166 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME_ZONE), data, reply, option);
167 if (result != ERR_NONE) {
168 TIME_HILOGE(TIME_MODULE_CLIENT, "SetTimeZone failed, error code is: %{public}d", result);
169 return result;
170 }
171 return result;
172 }
173
GetTimeZone(std::string & timeZoneId)174 int32_t TimeServiceProxy::GetTimeZone(std::string &timeZoneId)
175 {
176 MessageParcel data, reply;
177 MessageOption option;
178 if (!data.WriteInterfaceToken(GetDescriptor())) {
179 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
180 return E_TIME_WRITE_PARCEL_ERROR;
181 }
182 int32_t result =
183 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_TIME_ZONE), data, reply, option);
184 if (result != ERR_NONE) {
185 TIME_HILOGE(TIME_MODULE_CLIENT, "GetTimeZone failed, error code is: %{public}d", result);
186 return result;
187 }
188 timeZoneId = reply.ReadString();
189 return result;
190 }
191
GetWallTimeMs(int64_t & times)192 int32_t TimeServiceProxy::GetWallTimeMs(int64_t ×)
193 {
194 MessageParcel data, reply;
195 MessageOption option;
196 if (!data.WriteInterfaceToken(GetDescriptor())) {
197 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
198 return E_TIME_WRITE_PARCEL_ERROR;
199 }
200 int32_t result = Remote()->SendRequest(
201 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_WALL_TIME_MILLI), data, reply, option);
202 if (result != ERR_NONE) {
203 TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeMs failed, error code is: %{public}d", result);
204 return result;
205 }
206 times = reply.ReadInt64();
207 return result;
208 }
209
GetWallTimeNs(int64_t & times)210 int32_t TimeServiceProxy::GetWallTimeNs(int64_t ×)
211 {
212 MessageParcel data, reply;
213 MessageOption option;
214 if (!data.WriteInterfaceToken(GetDescriptor())) {
215 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
216 return E_TIME_WRITE_PARCEL_ERROR;
217 }
218 int32_t result = Remote()->SendRequest(
219 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_WALL_TIME_NANO), data, reply, option);
220 if (result != ERR_NONE) {
221 TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeNs failed, error code is: %{public}d", result);
222 return result;
223 }
224 times = reply.ReadInt64();
225 return result;
226 }
227
GetBootTimeMs(int64_t & times)228 int32_t TimeServiceProxy::GetBootTimeMs(int64_t ×)
229 {
230 MessageParcel data, reply;
231 MessageOption option;
232 if (!data.WriteInterfaceToken(GetDescriptor())) {
233 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
234 return E_TIME_WRITE_PARCEL_ERROR;
235 }
236 int32_t result = Remote()->SendRequest(
237 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_BOOT_TIME_MILLI), data, reply, option);
238 if (result != ERR_NONE) {
239 TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeMs failed, error code is: %{public}d", result);
240 return result;
241 }
242 times = reply.ReadInt64();
243 return result;
244 }
245
GetBootTimeNs(int64_t & times)246 int32_t TimeServiceProxy::GetBootTimeNs(int64_t ×)
247 {
248 MessageParcel data, reply;
249 MessageOption option;
250 if (!data.WriteInterfaceToken(GetDescriptor())) {
251 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable");
252 return E_TIME_WRITE_PARCEL_ERROR;
253 }
254 int32_t result = Remote()->SendRequest(
255 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_BOOT_TIME_NANO), data, reply, option);
256 if (result != ERR_NONE) {
257 TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeNs failed, error code is: %{public}d", result);
258 return result;
259 }
260 times = reply.ReadInt64();
261 return result;
262 }
263
GetMonotonicTimeMs(int64_t & times)264 int32_t TimeServiceProxy::GetMonotonicTimeMs(int64_t ×)
265 {
266 MessageParcel data, reply;
267 MessageOption option;
268 if (!data.WriteInterfaceToken(GetDescriptor())) {
269 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
270 return E_TIME_WRITE_PARCEL_ERROR;
271 }
272 int32_t result = Remote()->SendRequest(
273 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_MONO_TIME_MILLI), data, reply, option);
274 if (result != ERR_NONE) {
275 TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeMs failed, error code is: %{public}d", result);
276 return result;
277 }
278 times = reply.ReadInt64();
279 return result;
280 }
281
GetMonotonicTimeNs(int64_t & times)282 int32_t TimeServiceProxy::GetMonotonicTimeNs(int64_t ×)
283 {
284 MessageParcel data, reply;
285 MessageOption option;
286 if (!data.WriteInterfaceToken(GetDescriptor())) {
287 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
288 return E_TIME_WRITE_PARCEL_ERROR;
289 }
290 int32_t result = Remote()->SendRequest(
291 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_MONO_TIME_NANO), data, reply, option);
292 if (result != ERR_NONE) {
293 TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeNs failed, error code is: %{public}d", result);
294 return result;
295 }
296 times = reply.ReadInt64();
297 return result;
298 }
299
GetThreadTimeMs(int64_t & times)300 int32_t TimeServiceProxy::GetThreadTimeMs(int64_t ×)
301 {
302 MessageParcel data, reply;
303 MessageOption option;
304 if (!data.WriteInterfaceToken(GetDescriptor())) {
305 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
306 return E_TIME_WRITE_PARCEL_ERROR;
307 }
308 int32_t result = Remote()->SendRequest(
309 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI), data, reply, option);
310 if (result != ERR_NONE) {
311 TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeMs failed, error code is: %{public}d", result);
312 return result;
313 }
314 times = reply.ReadInt64();
315 return result;
316 }
317
GetThreadTimeNs(int64_t & times)318 int32_t TimeServiceProxy::GetThreadTimeNs(int64_t ×)
319 {
320 MessageParcel data, reply;
321 MessageOption option;
322 if (!data.WriteInterfaceToken(GetDescriptor())) {
323 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
324 return E_TIME_WRITE_PARCEL_ERROR;
325 }
326 int32_t result = Remote()->SendRequest(
327 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO), data, reply, option);
328 if (result != ERR_NONE) {
329 TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeNs failed, error code is: %{public}d", result);
330 return result;
331 }
332 times = reply.ReadInt64();
333 return result;
334 }
335
ProxyTimer(int32_t uid,bool isProxy,bool needRetrigger)336 bool TimeServiceProxy::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger)
337 {
338 MessageParcel data, reply;
339 MessageOption option;
340 if (!data.WriteInterfaceToken(GetDescriptor())) {
341 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
342 return false;
343 }
344 if (!data.WriteInt32(uid)) {
345 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write uid");
346 return false;
347 }
348 if (!data.WriteBool(isProxy)) {
349 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write isProxy");
350 return false;
351 }
352 if (!data.WriteBool(needRetrigger)) {
353 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write needRetrigger");
354 return false;
355 }
356
357 int32_t result =
358 Remote()->SendRequest(static_cast<uint32_t>(TimeServiceIpcInterfaceCode::PROXY_TIMER), data, reply, option);
359 if (result != ERR_NONE) {
360 TIME_HILOGE(TIME_MODULE_CLIENT, "ProxyTimer failed, error code is: %{public}d", result);
361 return false;
362 }
363 return true;
364 }
365
ResetAllProxy()366 bool TimeServiceProxy::ResetAllProxy()
367 {
368 MessageParcel data, reply;
369 MessageOption option;
370 if (!data.WriteInterfaceToken(GetDescriptor())) {
371 TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor");
372 return false;
373 }
374 int32_t result = Remote()->SendRequest(
375 static_cast<uint32_t>(TimeServiceIpcInterfaceCode::RESET_ALL_PROXY), data, reply, option);
376 if (result != ERR_NONE) {
377 TIME_HILOGE(TIME_MODULE_CLIENT, "ProxyTimer failed, error code is: %{public}d", result);
378 return false;
379 }
380 return true;
381 }
382 } // namespace MiscServices
383 } // namespace OHOS