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
266 // needs to acquire the lock `recoverTimerInfoLock_` before calling this method
CheckNameLocked(std::string name)267 void TimeServiceClient::CheckNameLocked(std::string name)
268 {
269 auto it = std::find(timerNameList_.begin(), timerNameList_.end(), name);
270 if (it == timerNameList_.end()) {
271 timerNameList_.push_back(name);
272 return;
273 }
274 auto recoverIter = std::find_if(recoverTimerInfoMap_.begin(), recoverTimerInfoMap_.end(),
275 [name](const auto& pair) {
276 return pair.second->timerInfo->name == name;
277 });
278 if (recoverIter != recoverTimerInfoMap_.end()) {
279 recoverIter = recoverTimerInfoMap_.erase(recoverIter);
280 }
281 }
282
CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions,uint64_t & timerId)283 int32_t TimeServiceClient::CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId)
284 {
285 if (timerOptions == nullptr) {
286 TIME_HILOGE(TIME_MODULE_CLIENT, "Input nullptr");
287 return E_TIME_NULLPTR;
288 }
289 if (!ConnectService()) {
290 return E_TIME_NULLPTR;
291 }
292 auto timerCallbackInfoObject = TimerCallback::GetInstance()->AsObject();
293 if (!timerCallbackInfoObject) {
294 TIME_HILOGE(TIME_MODULE_CLIENT, "New TimerCallback failed");
295 return E_TIME_NULLPTR;
296 }
297 auto proxy = GetProxy();
298 if (proxy == nullptr) {
299 return E_TIME_NULLPTR;
300 }
301 auto errCode = proxy->CreateTimer(timerOptions, timerCallbackInfoObject, timerId);
302 if (errCode != E_TIME_OK) {
303 TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed, errCode=%{public}d", errCode);
304 return errCode;
305 }
306
307 if (timerOptions->wantAgent == nullptr) {
308 std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
309 if (timerOptions->name != "") {
310 CheckNameLocked(timerOptions->name);
311 }
312 auto info = recoverTimerInfoMap_.find(timerId);
313 if (info != recoverTimerInfoMap_.end()) {
314 TIME_HILOGE(TIME_MODULE_CLIENT, "recover timer info already insert.");
315 return E_TIME_DEAL_FAILED;
316 } else {
317 auto recoverTimerInfo = std::make_shared<RecoverTimerInfo>();
318 recoverTimerInfo->timerInfo = timerOptions;
319 recoverTimerInfo->state = 0;
320 recoverTimerInfo->triggerTime = 0;
321 recoverTimerInfoMap_[timerId] = recoverTimerInfo;
322 }
323 }
324
325 TIME_HILOGD(TIME_MODULE_CLIENT, "CreateTimer id: %{public}" PRId64 "", timerId);
326 auto ret = TimerCallback::GetInstance()->InsertTimerCallbackInfo(timerId, timerOptions);
327 if (!ret) {
328 return E_TIME_DEAL_FAILED;
329 }
330 return errCode;
331 }
332
StartTimer(uint64_t timerId,uint64_t triggerTime)333 bool TimeServiceClient::StartTimer(uint64_t timerId, uint64_t triggerTime)
334 {
335 int32_t errCode = StartTimerV9(timerId, triggerTime);
336 if (errCode != E_TIME_OK) {
337 return false;
338 }
339 return true;
340 }
341
StartTimerV9(uint64_t timerId,uint64_t triggerTime)342 int32_t TimeServiceClient::StartTimerV9(uint64_t timerId, uint64_t triggerTime)
343 {
344 if (!ConnectService()) {
345 return E_TIME_SA_DIED;
346 }
347 auto proxy = GetProxy();
348 if (proxy == nullptr) {
349 return E_TIME_NULLPTR;
350 }
351 auto startRet = proxy->StartTimer(timerId, triggerTime);
352 if (startRet != 0) {
353 TIME_HILOGE(TIME_MODULE_CLIENT, "start timer failed: %{public}d", startRet);
354 return startRet;
355 }
356 std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
357 auto info = recoverTimerInfoMap_.find(timerId);
358 if (info != recoverTimerInfoMap_.end()) {
359 info->second->state = 1;
360 info->second->triggerTime = triggerTime;
361 }
362 return startRet;
363 }
364
StopTimer(uint64_t timerId)365 bool TimeServiceClient::StopTimer(uint64_t timerId)
366 {
367 int32_t errCode = StopTimerV9(timerId);
368 if (errCode != E_TIME_OK) {
369 return false;
370 }
371 return true;
372 }
373
StopTimerV9(uint64_t timerId)374 int32_t TimeServiceClient::StopTimerV9(uint64_t timerId)
375 {
376 if (!ConnectService()) {
377 return E_TIME_SA_DIED;
378 }
379 auto proxy = GetProxy();
380 if (proxy == nullptr) {
381 return E_TIME_NULLPTR;
382 }
383 auto stopRet = proxy->StopTimer(timerId);
384 if (stopRet != 0) {
385 TIME_HILOGE(TIME_MODULE_CLIENT, "stop timer failed: %{public}d", stopRet);
386 return stopRet;
387 }
388 std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
389 auto info = recoverTimerInfoMap_.find(timerId);
390 if (info != recoverTimerInfoMap_.end()) {
391 info->second->state = 0;
392 }
393 return stopRet;
394 }
395
DestroyTimer(uint64_t timerId)396 bool TimeServiceClient::DestroyTimer(uint64_t timerId)
397 {
398 int32_t errCode = DestroyTimerV9(timerId);
399 if (errCode != E_TIME_OK) {
400 return false;
401 }
402 return true;
403 }
404
DestroyTimerV9(uint64_t timerId)405 int32_t TimeServiceClient::DestroyTimerV9(uint64_t timerId)
406 {
407 if (!ConnectService()) {
408 return E_TIME_SA_DIED;
409 }
410 auto proxy = GetProxy();
411 if (proxy == nullptr) {
412 return E_TIME_NULLPTR;
413 }
414 auto errCode = proxy->DestroyTimer(timerId, false);
415 if (errCode != 0) {
416 TIME_HILOGE(TIME_MODULE_CLIENT, "destroy timer failed: %{public}d", errCode);
417 return errCode;
418 }
419 TimerCallback::GetInstance()->RemoveTimerCallbackInfo(timerId);
420 std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
421 auto info = recoverTimerInfoMap_.find(timerId);
422 if (info != recoverTimerInfoMap_.end()) {
423 if (info->second->timerInfo->name != "") {
424 auto it = std::find(timerNameList_.begin(), timerNameList_.end(), info->second->timerInfo->name);
425 if (it != timerNameList_.end()) {
426 timerNameList_.erase(it);
427 }
428 }
429 recoverTimerInfoMap_.erase(timerId);
430 }
431 return errCode;
432 }
433
DestroyTimerAsync(uint64_t timerId)434 bool TimeServiceClient::DestroyTimerAsync(uint64_t timerId)
435 {
436 int32_t errCode = DestroyTimerAsyncV9(timerId);
437 if (errCode != E_TIME_OK) {
438 return false;
439 }
440 return true;
441 }
442
DestroyTimerAsyncV9(uint64_t timerId)443 int32_t TimeServiceClient::DestroyTimerAsyncV9(uint64_t timerId)
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
453 auto errCode = proxy->DestroyTimer(timerId, true);
454 if (errCode != 0) {
455 TIME_HILOGE(TIME_MODULE_CLIENT, "destroy timer failed: %{public}d", errCode);
456 return errCode;
457 }
458 TimerCallback::GetInstance()->RemoveTimerCallbackInfo(timerId);
459 std::lock_guard<std::mutex> lock(recoverTimerInfoLock_);
460 auto info = recoverTimerInfoMap_.find(timerId);
461 if (info != recoverTimerInfoMap_.end()) {
462 recoverTimerInfoMap_.erase(timerId);
463 }
464 return errCode;
465 }
466
GetTimeZone()467 std::string TimeServiceClient::GetTimeZone()
468 {
469 std::string timeZoneId;
470 if (!ConnectService()) {
471 return std::string("");
472 }
473 auto proxy = GetProxy();
474 if (proxy == nullptr) {
475 return std::string("");
476 }
477 if (proxy->GetTimeZone(timeZoneId) != ERR_OK) {
478 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
479 return std::string("");
480 }
481 return timeZoneId;
482 }
483
GetTimeZone(std::string & timezoneId)484 int32_t TimeServiceClient::GetTimeZone(std::string &timezoneId)
485 {
486 if (!ConnectService()) {
487 return E_TIME_SA_DIED;
488 }
489 auto proxy = GetProxy();
490 if (proxy == nullptr) {
491 return E_TIME_NULLPTR;
492 }
493 if (proxy->GetTimeZone(timezoneId) != ERR_OK) {
494 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
495 return E_TIME_SA_DIED;
496 }
497 return E_TIME_OK;
498 }
499
GetWallTimeMs()500 int64_t TimeServiceClient::GetWallTimeMs()
501 {
502 int64_t time;
503 struct timespec tv {};
504 if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
505 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
506 return -1;
507 }
508 time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
509 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
510 return time;
511 }
512
GetWallTimeMs(int64_t & time)513 int32_t TimeServiceClient::GetWallTimeMs(int64_t &time)
514 {
515 struct timespec tv {};
516 if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
517 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
518 return E_TIME_SA_DIED;
519 }
520 time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
521 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
522 return E_TIME_OK;
523 }
524
GetWallTimeNs()525 int64_t TimeServiceClient::GetWallTimeNs()
526 {
527 int64_t time;
528 struct timespec tv {};
529 if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
530 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
531 return -1;
532 }
533 time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
534 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
535 return time;
536 }
537
GetWallTimeNs(int64_t & time)538 int32_t TimeServiceClient::GetWallTimeNs(int64_t &time)
539 {
540 struct timespec tv {};
541 if (!GetTimeByClockId(CLOCK_REALTIME, tv)) {
542 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
543 return E_TIME_SA_DIED;
544 }
545 time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
546 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
547 return E_TIME_OK;
548 }
549
GetBootTimeMs()550 int64_t TimeServiceClient::GetBootTimeMs()
551 {
552 int64_t time;
553 struct timespec tv {};
554 if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
555 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
556 return -1;
557 }
558 time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
559 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
560 return time;
561 }
562
GetBootTimeMs(int64_t & time)563 int32_t TimeServiceClient::GetBootTimeMs(int64_t &time)
564 {
565 struct timespec tv {};
566 if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
567 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
568 return E_TIME_SA_DIED;
569 }
570 time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
571 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
572 return E_TIME_OK;
573 }
574
GetBootTimeNs()575 int64_t TimeServiceClient::GetBootTimeNs()
576 {
577 int64_t time;
578 struct timespec tv {};
579 if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
580 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
581 return -1;
582 }
583 time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
584 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
585 return time;
586 }
587
GetBootTimeNs(int64_t & time)588 int32_t TimeServiceClient::GetBootTimeNs(int64_t &time)
589 {
590 struct timespec tv {};
591 if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) {
592 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
593 return E_TIME_SA_DIED;
594 }
595 time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
596 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
597 return E_TIME_OK;
598 }
599
GetMonotonicTimeMs()600 int64_t TimeServiceClient::GetMonotonicTimeMs()
601 {
602 int64_t time;
603 struct timespec tv {};
604 if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
605 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
606 return -1;
607 }
608 time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
609 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
610 return time;
611 }
612
GetMonotonicTimeMs(int64_t & time)613 int32_t TimeServiceClient::GetMonotonicTimeMs(int64_t &time)
614 {
615 struct timespec tv {};
616 if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
617 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
618 return E_TIME_SA_DIED;
619 }
620 time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI;
621 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
622 return E_TIME_OK;
623 }
624
GetMonotonicTimeNs()625 int64_t TimeServiceClient::GetMonotonicTimeNs()
626 {
627 int64_t time;
628 struct timespec tv {};
629 if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
630 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
631 return -1;
632 }
633 time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
634 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
635 return time;
636 }
637
GetMonotonicTimeNs(int64_t & time)638 int32_t TimeServiceClient::GetMonotonicTimeNs(int64_t &time)
639 {
640 struct timespec tv {};
641 if (!GetTimeByClockId(CLOCK_MONOTONIC, tv)) {
642 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
643 return E_TIME_SA_DIED;
644 }
645 time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec;
646 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
647 return E_TIME_OK;
648 }
649
GetThreadTimeMs()650 int64_t TimeServiceClient::GetThreadTimeMs()
651 {
652 int64_t time;
653 if (!ConnectService()) {
654 return -1;
655 }
656 auto proxy = GetProxy();
657 if (proxy == nullptr) {
658 return E_TIME_NULLPTR;
659 }
660 if (proxy->GetThreadTimeMs(time) != ERR_OK) {
661 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
662 return -1;
663 }
664 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
665 return time;
666 }
667
GetThreadTimeMs(int64_t & time)668 int32_t TimeServiceClient::GetThreadTimeMs(int64_t &time)
669 {
670 if (!ConnectService()) {
671 return E_TIME_SA_DIED;
672 }
673 auto proxy = GetProxy();
674 if (proxy == nullptr) {
675 return E_TIME_NULLPTR;
676 }
677 if (proxy->GetThreadTimeMs(time) != ERR_OK) {
678 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
679 return E_TIME_SA_DIED;
680 }
681 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
682 return E_TIME_OK;
683 }
684
GetThreadTimeNs()685 int64_t TimeServiceClient::GetThreadTimeNs()
686 {
687 int64_t time;
688 if (!ConnectService()) {
689 return -1;
690 }
691 auto proxy = GetProxy();
692 if (proxy == nullptr) {
693 return E_TIME_NULLPTR;
694 }
695 if (proxy->GetThreadTimeNs(time) != ERR_OK) {
696 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
697 return -1;
698 }
699 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
700 return time;
701 }
702
GetThreadTimeNs(int64_t & time)703 int32_t TimeServiceClient::GetThreadTimeNs(int64_t &time)
704 {
705 if (!ConnectService()) {
706 return E_TIME_SA_DIED;
707 }
708 auto proxy = GetProxy();
709 if (proxy == nullptr) {
710 return E_TIME_NULLPTR;
711 }
712 if (proxy->GetThreadTimeNs(time) != ERR_OK) {
713 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
714 return E_TIME_SA_DIED;
715 }
716 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
717 return E_TIME_OK;
718 }
719
ProxyTimer(int32_t uid,bool isProxy,bool needRetrigger)720 bool TimeServiceClient::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger)
721 {
722 TIME_HILOGD(TIME_MODULE_CLIENT, "ProxyTimer start uid: %{public}d, isProxy: %{public}d", uid, isProxy);
723 if (!ConnectService()) {
724 return false;
725 }
726 auto proxy = GetProxy();
727 if (proxy == nullptr) {
728 return false;
729 }
730 return proxy->ProxyTimer(uid, isProxy, needRetrigger);
731 }
732
ProxyTimer(int32_t uid,std::set<int> pidList,bool isProxy,bool needRetrigger)733 bool TimeServiceClient::ProxyTimer(int32_t uid, std::set<int> pidList, bool isProxy, bool needRetrigger)
734 {
735 if (!ConnectService()) {
736 return false;
737 }
738 auto proxy = GetProxy();
739 if (proxy == nullptr) {
740 return false;
741 }
742 return proxy->ProxyTimer(uid, pidList, isProxy, needRetrigger);
743 }
744
AdjustTimer(bool isAdjust,uint32_t interval)745 int32_t TimeServiceClient::AdjustTimer(bool isAdjust, uint32_t interval)
746 {
747 TIME_HILOGD(TIME_MODULE_CLIENT, "Adjust Timer isAdjust: %{public}d", isAdjust);
748 if (!ConnectService()) {
749 return E_TIME_SA_DIED;
750 }
751 auto proxy = GetProxy();
752 if (proxy == nullptr) {
753 return E_TIME_NULLPTR;
754 }
755 return proxy->AdjustTimer(isAdjust, interval);
756 }
757
SetTimerExemption(const std::unordered_set<std::string> & nameArr,bool isExemption)758 int32_t TimeServiceClient::SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption)
759 {
760 TIME_HILOGD(TIME_MODULE_CLIENT, "set time exemption size: %{public}zu", nameArr.size());
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->SetTimerExemption(nameArr, isExemption);
769 }
770
ResetAllProxy()771 bool TimeServiceClient::ResetAllProxy()
772 {
773 TIME_HILOGD(TIME_MODULE_CLIENT, "ResetAllProxy");
774 if (!ConnectService()) {
775 TIME_HILOGE(TIME_MODULE_CLIENT, "ResetAllProxy ConnectService failed.");
776 return false;
777 }
778 auto proxy = GetProxy();
779 if (proxy == nullptr) {
780 return false;
781 }
782 return proxy->ResetAllProxy();
783 }
784
GetNtpTimeMs(int64_t & time)785 int32_t TimeServiceClient::GetNtpTimeMs(int64_t &time)
786 {
787 if (!ConnectService()) {
788 return E_TIME_SA_DIED;
789 }
790 auto proxy = GetProxy();
791 if (proxy == nullptr) {
792 return E_TIME_NULLPTR;
793 }
794 return proxy->GetNtpTimeMs(time);
795 }
796
GetRealTimeMs(int64_t & time)797 int32_t TimeServiceClient::GetRealTimeMs(int64_t &time)
798 {
799 if (!ConnectService()) {
800 return E_TIME_SA_DIED;
801 }
802 auto proxy = GetProxy();
803 if (proxy == nullptr) {
804 return E_TIME_NULLPTR;
805 }
806 return proxy->GetRealTimeMs(time);
807 }
808
GetProxy()809 sptr<ITimeService> TimeServiceClient::GetProxy()
810 {
811 std::lock_guard<std::mutex> autoLock(proxyLock_);
812 return timeServiceProxy_;
813 }
814
SetProxy(sptr<ITimeService> proxy)815 void TimeServiceClient::SetProxy(sptr<ITimeService> proxy)
816 {
817 std::lock_guard<std::mutex> autoLock(proxyLock_);
818 timeServiceProxy_ = proxy;
819 }
820
ClearProxy()821 void TimeServiceClient::ClearProxy()
822 {
823 std::lock_guard<std::mutex> autoLock(proxyLock_);
824 timeServiceProxy_ = nullptr;
825 }
826 } // namespace MiscServices
827 } // namespace OHOS