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