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