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