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