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