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_client.h"
17 
18 #include <cinttypes>
19 #include <mutex>
20 
21 #include "iremote_object.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "time_common.h"
25 #include "time_service_proxy.h"
26 #include "timer_call_back.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30 namespace {
31 static const int MILLI_TO_SEC = 1000LL;
32 static const int NANO_TO_SEC = 1000000000LL;
33 constexpr int32_t NANO_TO_MILLI = NANO_TO_SEC / MILLI_TO_SEC;
34 }
35 
36 std::mutex TimeServiceClient::instanceLock_;
37 sptr<TimeServiceClient> TimeServiceClient::instance_;
38 
TimeServiceListener()39 TimeServiceClient::TimeServiceListener::TimeServiceListener ()
40 {
41 }
42 
OnAddSystemAbility( int32_t saId, const std::string &deviceId)43 void TimeServiceClient::TimeServiceListener::OnAddSystemAbility(
44     int32_t saId, const std::string &deviceId)
45 {
46     if (saId == TIME_SERVICE_ID) {
47         auto proxy = TimeServiceClient::GetInstance()->GetProxy();
48         if (proxy == nullptr) {
49             return;
50         }
51         auto timerCallbackInfoObject = TimerCallback::GetInstance()->AsObject();
52         if (!timerCallbackInfoObject) {
53             TIME_HILOGE(TIME_MODULE_CLIENT, "New TimerCallback failed");
54             return;
55         }
56         std::lock_guard<std::mutex> lock(TimeServiceClient::GetInstance()->recoverTimerInfoLock_);
57         auto iter = TimeServiceClient::GetInstance()->recoverTimerInfoMap_.begin();
58         for (; iter != TimeServiceClient::GetInstance()->recoverTimerInfoMap_.end(); iter++) {
59             auto timerId = iter->first;
60             proxy->CreateTimer(iter->second->timerInfo, timerCallbackInfoObject, timerId);
61             if (iter->second->state == 1) {
62                 proxy->StartTimer(timerId, iter->second->triggerTime);
63             }
64         }
65         return;
66     } else {
67         TIME_HILOGE(TIME_MODULE_SERVICE, "Id is not TIME_SERVICE_ID");
68         return;
69     }
70 }
71 
OnRemoveSystemAbility( int32_t saId, const std::string &deviceId)72 void TimeServiceClient::TimeServiceListener::OnRemoveSystemAbility(
73     int32_t saId, const std::string &deviceId)
74 {
75 }
76 
77 TimeServiceClient::TimeServiceClient() = default;
78 
~TimeServiceClient()79 TimeServiceClient::~TimeServiceClient()
80 {
81     auto proxy = GetProxy();
82     if (proxy != nullptr) {
83         auto remoteObject = proxy->AsObject();
84         if (remoteObject != nullptr) {
85             remoteObject->RemoveDeathRecipient(deathRecipient_);
86         }
87     }
88 }
89 
GetInstance()90 sptr<TimeServiceClient> TimeServiceClient::GetInstance()
91 {
92     if (instance_ == nullptr) {
93         std::lock_guard<std::mutex> autoLock(instanceLock_);
94         if (instance_ == nullptr) {
95             instance_ = new TimeServiceClient;
96         }
97     }
98     return instance_;
99 }
100 
SubscribeSA(sptr<ISystemAbilityManager> systemAbilityManager)101 bool TimeServiceClient::SubscribeSA(sptr<ISystemAbilityManager> systemAbilityManager)
102 {
103     auto timeServiceListener = new (std::nothrow) TimeServiceListener();
104     if (timeServiceListener == nullptr) {
105         TIME_HILOGE(TIME_MODULE_CLIENT, "Get timeServiceListener failed.");
106         return false;
107     }
108     auto ret = systemAbilityManager->SubscribeSystemAbility(TIME_SERVICE_ID, timeServiceListener);
109     if (ret != 0) {
110         TIME_HILOGE(TIME_MODULE_CLIENT, "SubscribeSystemAbility failed: %{public}d", ret);
111         return false;
112     }
113     return true;
114 }
115 
ConnectService()116 bool TimeServiceClient::ConnectService()
117 {
118     if (GetProxy() != nullptr) {
119         return true;
120     }
121 
122     sptr<ISystemAbilityManager> systemAbilityManager =
123         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
124     if (systemAbilityManager == nullptr) {
125         TIME_HILOGE(TIME_MODULE_CLIENT, "Getting SystemAbilityManager failed.");
126         return false;
127     }
128 
129     auto systemAbility = systemAbilityManager->GetSystemAbility(TIME_SERVICE_ID);
130     if (systemAbility == nullptr) {
131         TIME_HILOGE(TIME_MODULE_CLIENT, "Get SystemAbility failed.");
132         return false;
133     }
134 
135     std::lock_guard<std::mutex> autoLock(deathLock_);
136     if (deathRecipient_ == nullptr) {
137         deathRecipient_ = new TimeSaDeathRecipient();
138     }
139 
140     systemAbility->AddDeathRecipient(deathRecipient_);
141     sptr<ITimeService> proxy = iface_cast<ITimeService>(systemAbility);
142     if (proxy == nullptr) {
143         TIME_HILOGE(TIME_MODULE_CLIENT, "Get TimeServiceProxy from SA failed.");
144         return false;
145     }
146     SetProxy(proxy);
147 
148     if (!SubscribeSA(systemAbilityManager)) {
149         return false;
150     }
151     return true;
152 }
153 
GetTimeByClockId(clockid_t clockId, struct timespec &tv)154 bool TimeServiceClient::GetTimeByClockId(clockid_t clockId, struct timespec &tv)
155 {
156     if (clock_gettime(clockId, &tv) < 0) {
157         TIME_HILOGE(TIME_MODULE_CLIENT, "Failed clock_gettime, errno: %{public}s.", strerror(errno));
158         return false;
159     }
160     return true;
161 }
162 
SetTime(int64_t time)163 bool TimeServiceClient::SetTime(int64_t time)
164 {
165     if (!ConnectService()) {
166         return false;
167     }
168     auto proxy = GetProxy();
169     if (proxy == nullptr) {
170         return false;
171     }
172     return proxy->SetTime(time) == ERR_OK;
173 }
174 
SetTime(int64_t milliseconds, int32_t &code)175 bool TimeServiceClient::SetTime(int64_t milliseconds, int32_t &code)
176 {
177     if (!ConnectService()) {
178         code = E_TIME_SA_DIED;
179         return false;
180     }
181     auto proxy = GetProxy();
182     if (proxy == nullptr) {
183         code = E_TIME_NULLPTR;
184         return false;
185     }
186     code = proxy->SetTime(milliseconds);
187     return code == ERR_OK;
188 }
189 
SetTimeV9(int64_t time)190 int32_t TimeServiceClient::SetTimeV9(int64_t time)
191 {
192     if (!ConnectService()) {
193         return E_TIME_SA_DIED;
194     }
195     auto proxy = GetProxy();
196     if (proxy == nullptr) {
197         return E_TIME_NULLPTR;
198     }
199     return proxy->SetTime(time, ITimeService::API_VERSION_9);
200 }
201 
SetTimeZone(const std::string &timezoneId)202 bool TimeServiceClient::SetTimeZone(const std::string &timezoneId)
203 {
204     if (!ConnectService()) {
205         return false;
206     }
207     auto proxy = GetProxy();
208     if (proxy == nullptr) {
209         return false;
210     }
211     return proxy->SetTimeZone(timezoneId) == ERR_OK;
212 }
213 
SetTimeZone(const std::string &timezoneId, int32_t &code)214 bool TimeServiceClient::SetTimeZone(const std::string &timezoneId, int32_t &code)
215 {
216     if (!ConnectService()) {
217         code = E_TIME_SA_DIED;
218         return false;
219     }
220     auto proxy = GetProxy();
221     if (proxy == nullptr) {
222         code =  E_TIME_NULLPTR;
223         return false;
224     }
225     code = proxy->SetTimeZone(timezoneId);
226     TIME_HILOGD(TIME_MODULE_CLIENT, "settimezone end");
227     return code == ERR_OK;
228 }
229 
SetTimeZoneV9(const std::string &timezoneId)230 int32_t TimeServiceClient::SetTimeZoneV9(const std::string &timezoneId)
231 {
232     if (!ConnectService()) {
233         return E_TIME_SA_DIED;
234     }
235     auto proxy = GetProxy();
236     if (proxy == nullptr) {
237         return E_TIME_NULLPTR;
238     }
239     return proxy->SetTimeZone(timezoneId, ITimeService::API_VERSION_9);
240 }
241 
CreateTimer(std::shared_ptr<ITimerInfo> timerOptions)242 uint64_t TimeServiceClient::CreateTimer(std::shared_ptr<ITimerInfo> timerOptions)
243 {
244     uint64_t timerId = 0;
245     auto errCode = CreateTimerV9(timerOptions, timerId);
246     TIME_HILOGD(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId);
247     if (errCode != E_TIME_OK) {
248         return 0;
249     }
250     if (timerId == 0) {
251         TIME_HILOGE(TIME_MODULE_CLIENT, "Create timer failed");
252         return 0;
253     }
254     return timerId;
255 }
256 
CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId)257 int32_t TimeServiceClient::CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId)
258 {
259     if (timerOptions == nullptr) {
260         TIME_HILOGE(TIME_MODULE_CLIENT, "Input nullptr");
261         return E_TIME_NULLPTR;
262     }
263     if (!ConnectService()) {
264         return E_TIME_NULLPTR;
265     }
266     auto timerCallbackInfoObject = TimerCallback::GetInstance()->AsObject();
267     if (!timerCallbackInfoObject) {
268         TIME_HILOGE(TIME_MODULE_CLIENT, "New TimerCallback failed");
269         return E_TIME_NULLPTR;
270     }
271     auto proxy = GetProxy();
272     if (proxy == nullptr) {
273         return E_TIME_NULLPTR;
274     }
275     auto errCode = proxy->CreateTimer(timerOptions, timerCallbackInfoObject, timerId);
276     if (errCode != E_TIME_OK) {
277         TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed, errCode=%{public}d", errCode);
278         return errCode;
279     }
280 
281     if (timerOptions->wantAgent == nullptr) {
282         std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
283         auto info = recoverTimerInfoMap_.find(timerId);
284         if (info != recoverTimerInfoMap_.end()) {
285             TIME_HILOGE(TIME_MODULE_CLIENT, "recover timer info already insert.");
286             return E_TIME_DEAL_FAILED;
287         } else {
288             auto recoverTimerInfo = std::make_shared<RecoverTimerInfo>();
289             recoverTimerInfo->timerInfo = timerOptions;
290             recoverTimerInfo->state = 0;
291             recoverTimerInfo->triggerTime = 0;
292             recoverTimerInfoMap_[timerId] = recoverTimerInfo;
293         }
294     }
295 
296     TIME_HILOGD(TIME_MODULE_CLIENT, "CreateTimer id: %{public}" PRId64 "", timerId);
297     auto ret = TimerCallback::GetInstance()->InsertTimerCallbackInfo(timerId, timerOptions);
298     if (!ret) {
299         return E_TIME_DEAL_FAILED;
300     }
301     return errCode;
302 }
303 
StartTimer(uint64_t timerId, uint64_t triggerTime)304 bool TimeServiceClient::StartTimer(uint64_t timerId, uint64_t triggerTime)
305 {
306     int32_t errCode = StartTimerV9(timerId, triggerTime);
307     if (errCode != E_TIME_OK) {
308         return false;
309     }
310     return true;
311 }
312 
StartTimerV9(uint64_t timerId, uint64_t triggerTime)313 int32_t TimeServiceClient::StartTimerV9(uint64_t timerId, uint64_t triggerTime)
314 {
315     if (!ConnectService()) {
316         return E_TIME_SA_DIED;
317     }
318     auto proxy = GetProxy();
319     if (proxy == nullptr) {
320         return E_TIME_NULLPTR;
321     }
322     auto startRet = proxy->StartTimer(timerId, triggerTime);
323     if (startRet != 0) {
324         TIME_HILOGE(TIME_MODULE_CLIENT, "start timer failed: %{public}d", startRet);
325         return startRet;
326     }
327     std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
328     auto info = recoverTimerInfoMap_.find(timerId);
329     if (info != recoverTimerInfoMap_.end()) {
330         info->second->state = 1;
331         info->second->triggerTime = triggerTime;
332     }
333     return startRet;
334 }
335 
StopTimer(uint64_t timerId)336 bool TimeServiceClient::StopTimer(uint64_t timerId)
337 {
338     int32_t errCode = StopTimerV9(timerId);
339     if (errCode != E_TIME_OK) {
340         return false;
341     }
342     return true;
343 }
344 
StopTimerV9(uint64_t timerId)345 int32_t TimeServiceClient::StopTimerV9(uint64_t timerId)
346 {
347     if (!ConnectService()) {
348         return E_TIME_SA_DIED;
349     }
350     auto proxy = GetProxy();
351     if (proxy == nullptr) {
352         return E_TIME_NULLPTR;
353     }
354     auto stopRet = proxy->StopTimer(timerId);
355     if (stopRet != 0) {
356         TIME_HILOGE(TIME_MODULE_CLIENT, "stop timer failed: %{public}d", stopRet);
357         return stopRet;
358     }
359     std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
360     auto info = recoverTimerInfoMap_.find(timerId);
361     if (info != recoverTimerInfoMap_.end()) {
362         info->second->state = 0;
363     }
364     return stopRet;
365 }
366 
DestroyTimer(uint64_t timerId)367 bool TimeServiceClient::DestroyTimer(uint64_t timerId)
368 {
369     int32_t errCode = DestroyTimerV9(timerId);
370     if (errCode != E_TIME_OK) {
371         return false;
372     }
373     return true;
374 }
375 
DestroyTimerV9(uint64_t timerId)376 int32_t TimeServiceClient::DestroyTimerV9(uint64_t timerId)
377 {
378     if (!ConnectService()) {
379         return E_TIME_SA_DIED;
380     }
381     auto proxy = GetProxy();
382     if (proxy == nullptr) {
383         return E_TIME_NULLPTR;
384     }
385     auto errCode = proxy->DestroyTimer(timerId, false);
386     if (errCode != 0) {
387         TIME_HILOGE(TIME_MODULE_CLIENT, "destroy timer failed: %{public}d", errCode);
388         return errCode;
389     }
390     TimerCallback::GetInstance()->RemoveTimerCallbackInfo(timerId);
391     std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
392     auto info = recoverTimerInfoMap_.find(timerId);
393     if (info != recoverTimerInfoMap_.end()) {
394         recoverTimerInfoMap_.erase(timerId);
395     }
396     return errCode;
397 }
398 
DestroyTimerAsync(uint64_t timerId)399 bool TimeServiceClient::DestroyTimerAsync(uint64_t timerId)
400 {
401     int32_t errCode = DestroyTimerAsyncV9(timerId);
402     if (errCode != E_TIME_OK) {
403         return false;
404     }
405     return true;
406 }
407 
DestroyTimerAsyncV9(uint64_t timerId)408 int32_t TimeServiceClient::DestroyTimerAsyncV9(uint64_t timerId)
409 {
410     if (!ConnectService()) {
411         return E_TIME_SA_DIED;
412     }
413     auto proxy = GetProxy();
414     if (proxy == nullptr) {
415         return E_TIME_NULLPTR;
416     }
417 
418     auto errCode = proxy->DestroyTimer(timerId, true);
419     if (errCode != 0) {
420         TIME_HILOGE(TIME_MODULE_CLIENT, "destroy timer failed: %{public}d", errCode);
421         return errCode;
422     }
423     TimerCallback::GetInstance()->RemoveTimerCallbackInfo(timerId);
424     std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
425     auto info = recoverTimerInfoMap_.find(timerId);
426     if (info != recoverTimerInfoMap_.end()) {
427         recoverTimerInfoMap_.erase(timerId);
428     }
429     return errCode;
430 }
431 
GetTimeZone()432 std::string TimeServiceClient::GetTimeZone()
433 {
434     std::string timeZoneId;
435     if (!ConnectService()) {
436         return std::string("");
437     }
438     auto proxy = GetProxy();
439     if (proxy == nullptr) {
440         return std::string("");
441     }
442     if (proxy->GetTimeZone(timeZoneId) != ERR_OK) {
443         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
444         return std::string("");
445     }
446     return timeZoneId;
447 }
448 
GetTimeZone(std::string &timezoneId)449 int32_t TimeServiceClient::GetTimeZone(std::string &timezoneId)
450 {
451     if (!ConnectService()) {
452         return E_TIME_SA_DIED;
453     }
454     auto proxy = GetProxy();
455     if (proxy == nullptr) {
456         return E_TIME_NULLPTR;
457     }
458     if (proxy->GetTimeZone(timezoneId) != ERR_OK) {
459         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
460         return E_TIME_SA_DIED;
461     }
462     return E_TIME_OK;
463 }
464 
GetWallTimeMs()465 int64_t TimeServiceClient::GetWallTimeMs()
466 {
467     int64_t time;
468     struct timespec tv {};
469     if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
470         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
471         return -1;
472     }
473     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
474     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
475     return time;
476 }
477 
GetWallTimeMs(int64_t &time)478 int32_t TimeServiceClient::GetWallTimeMs(int64_t &time)
479 {
480     struct timespec tv {};
481     if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
482         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
483         return E_TIME_SA_DIED;
484     }
485     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
486     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
487     return E_TIME_OK;
488 }
489 
GetWallTimeNs()490 int64_t TimeServiceClient::GetWallTimeNs()
491 {
492     int64_t time;
493     struct timespec tv {};
494     if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
495         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
496         return -1;
497     }
498     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
499     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
500     return time;
501 }
502 
GetWallTimeNs(int64_t &time)503 int32_t TimeServiceClient::GetWallTimeNs(int64_t &time)
504 {
505     struct timespec tv {};
506     if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
507         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
508         return E_TIME_SA_DIED;
509     }
510     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
511     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
512     return E_TIME_OK;
513 }
514 
GetBootTimeMs()515 int64_t TimeServiceClient::GetBootTimeMs()
516 {
517     int64_t time;
518     struct timespec tv {};
519     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
520         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
521         return -1;
522     }
523     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
524     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
525     return time;
526 }
527 
GetBootTimeMs(int64_t &time)528 int32_t TimeServiceClient::GetBootTimeMs(int64_t &time)
529 {
530     struct timespec tv {};
531     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
532         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
533         return E_TIME_SA_DIED;
534     }
535     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
536     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
537     return E_TIME_OK;
538 }
539 
GetBootTimeNs()540 int64_t TimeServiceClient::GetBootTimeNs()
541 {
542     int64_t time;
543     struct timespec tv {};
544     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
545         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
546         return -1;
547     }
548     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
549     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
550     return time;
551 }
552 
GetBootTimeNs(int64_t &time)553 int32_t TimeServiceClient::GetBootTimeNs(int64_t &time)
554 {
555     struct timespec tv {};
556     if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
557         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
558         return E_TIME_SA_DIED;
559     }
560     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
561     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
562     return E_TIME_OK;
563 }
564 
GetMonotonicTimeMs()565 int64_t TimeServiceClient::GetMonotonicTimeMs()
566 {
567     int64_t time;
568     struct timespec tv {};
569     if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
570         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
571         return -1;
572     }
573     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
574     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
575     return time;
576 }
577 
GetMonotonicTimeMs(int64_t &time)578 int32_t TimeServiceClient::GetMonotonicTimeMs(int64_t &time)
579 {
580     struct timespec tv {};
581     if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
582         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
583         return E_TIME_SA_DIED;
584     }
585     time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
586     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
587     return E_TIME_OK;
588 }
589 
GetMonotonicTimeNs()590 int64_t TimeServiceClient::GetMonotonicTimeNs()
591 {
592     int64_t time;
593     struct timespec tv {};
594     if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
595         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
596         return -1;
597     }
598     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
599     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
600     return time;
601 }
602 
GetMonotonicTimeNs(int64_t &time)603 int32_t TimeServiceClient::GetMonotonicTimeNs(int64_t &time)
604 {
605     struct timespec tv {};
606     if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
607         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
608         return E_TIME_SA_DIED;
609     }
610     time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
611     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
612     return E_TIME_OK;
613 }
614 
GetThreadTimeMs()615 int64_t TimeServiceClient::GetThreadTimeMs()
616 {
617     int64_t time;
618     if (!ConnectService()) {
619         return -1;
620     }
621     auto proxy = GetProxy();
622     if (proxy == nullptr) {
623         return E_TIME_NULLPTR;
624     }
625     if (proxy->GetThreadTimeMs(time) != ERR_OK) {
626         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
627         return -1;
628     }
629     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
630     return time;
631 }
632 
GetThreadTimeMs(int64_t &time)633 int32_t TimeServiceClient::GetThreadTimeMs(int64_t &time)
634 {
635     if (!ConnectService()) {
636         return E_TIME_SA_DIED;
637     }
638     auto proxy = GetProxy();
639     if (proxy == nullptr) {
640         return E_TIME_NULLPTR;
641     }
642     if (proxy->GetThreadTimeMs(time) != ERR_OK) {
643         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
644         return E_TIME_SA_DIED;
645     }
646     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
647     return E_TIME_OK;
648 }
649 
GetThreadTimeNs()650 int64_t TimeServiceClient::GetThreadTimeNs()
651 {
652     int64_t time;
653     if (!ConnectService()) {
654         return -1;
655     }
656     auto proxy = GetProxy();
657     if (proxy == nullptr) {
658         return E_TIME_NULLPTR;
659     }
660     if (proxy->GetThreadTimeNs(time) != ERR_OK) {
661         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
662         return -1;
663     }
664     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
665     return time;
666 }
667 
GetThreadTimeNs(int64_t &time)668 int32_t TimeServiceClient::GetThreadTimeNs(int64_t &time)
669 {
670     if (!ConnectService()) {
671         return E_TIME_SA_DIED;
672     }
673     auto proxy = GetProxy();
674     if (proxy == nullptr) {
675         return E_TIME_NULLPTR;
676     }
677     if (proxy->GetThreadTimeNs(time) != ERR_OK) {
678         TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
679         return E_TIME_SA_DIED;
680     }
681     TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
682     return E_TIME_OK;
683 }
684 
ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger)685 bool TimeServiceClient::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger)
686 {
687     TIME_HILOGD(TIME_MODULE_CLIENT, "ProxyTimer start uid: %{public}d, isProxy: %{public}d", uid, isProxy);
688     if (!ConnectService()) {
689         return false;
690     }
691     auto proxy = GetProxy();
692     if (proxy == nullptr) {
693         return false;
694     }
695     return proxy->ProxyTimer(uid, isProxy, needRetrigger);
696 }
697 
ProxyTimer(std::set<int> pidList, bool isProxy, bool needRetrigger)698 bool TimeServiceClient::ProxyTimer(std::set<int> pidList, bool isProxy, bool needRetrigger)
699 {
700     if (!ConnectService()) {
701         return false;
702     }
703     auto proxy = GetProxy();
704     if (proxy == nullptr) {
705         return false;
706     }
707     return proxy->ProxyTimer(pidList, isProxy, needRetrigger);
708 }
709 
AdjustTimer(bool isAdjust, uint32_t interval)710 int32_t TimeServiceClient::AdjustTimer(bool isAdjust, uint32_t interval)
711 {
712     TIME_HILOGD(TIME_MODULE_CLIENT, "Adjust Timer isAdjust: %{public}d", isAdjust);
713     if (!ConnectService()) {
714         return E_TIME_SA_DIED;
715     }
716     auto proxy = GetProxy();
717     if (proxy == nullptr) {
718         return E_TIME_NULLPTR;
719     }
720     return proxy->AdjustTimer(isAdjust, interval);
721 }
722 
SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption)723 int32_t TimeServiceClient::SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption)
724 {
725     TIME_HILOGD(TIME_MODULE_CLIENT, "set time exemption size: %{public}zu", nameArr.size());
726     if (!ConnectService()) {
727         return E_TIME_SA_DIED;
728     }
729     auto proxy = GetProxy();
730     if (proxy == nullptr) {
731         return E_TIME_NULLPTR;
732     }
733     return proxy->SetTimerExemption(nameArr, isExemption);
734 }
735 
ResetAllProxy()736 bool TimeServiceClient::ResetAllProxy()
737 {
738     TIME_HILOGD(TIME_MODULE_CLIENT, "ResetAllProxy");
739     if (!ConnectService()) {
740         TIME_HILOGE(TIME_MODULE_CLIENT, "ResetAllProxy ConnectService failed.");
741         return false;
742     }
743     auto proxy = GetProxy();
744     if (proxy == nullptr) {
745         return false;
746     }
747     return proxy->ResetAllProxy();
748 }
749 
GetNtpTimeMs(int64_t &time)750 int32_t TimeServiceClient::GetNtpTimeMs(int64_t &time)
751 {
752     if (!ConnectService()) {
753         return E_TIME_SA_DIED;
754     }
755     auto proxy = GetProxy();
756     if (proxy == nullptr) {
757         return E_TIME_NULLPTR;
758     }
759     return proxy->GetNtpTimeMs(time);
760 }
761 
GetRealTimeMs(int64_t &time)762 int32_t TimeServiceClient::GetRealTimeMs(int64_t &time)
763 {
764     if (!ConnectService()) {
765         return E_TIME_SA_DIED;
766     }
767     auto proxy = GetProxy();
768     if (proxy == nullptr) {
769         return E_TIME_NULLPTR;
770     }
771     return proxy->GetRealTimeMs(time);
772 }
773 
GetProxy()774 sptr<ITimeService> TimeServiceClient::GetProxy()
775 {
776     std::lock_guard<std::mutex> autoLock(proxyLock_);
777     return timeServiceProxy_;
778 }
779 
SetProxy(sptr<ITimeService> proxy)780 void TimeServiceClient::SetProxy(sptr<ITimeService> proxy)
781 {
782     std::lock_guard<std::mutex> autoLock(proxyLock_);
783     timeServiceProxy_ = proxy;
784 }
785 
ClearProxy()786 void TimeServiceClient::ClearProxy()
787 {
788     std::lock_guard<std::mutex> autoLock(proxyLock_);
789     timeServiceProxy_ = nullptr;
790 }
791 } // namespace MiscServices
792 } // namespace OHOS