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