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 "timer_call_back.h"
26
27 namespace OHOS {
28 namespace MiscServices {
29 std::mutex TimeServiceClient::instanceLock_;
30 sptr<TimeServiceClient> TimeServiceClient::instance_;
31 TimeServiceClient::TimeServiceClient() = default;
32
~TimeServiceClient()33 TimeServiceClient::~TimeServiceClient()
34 {
35 auto proxy = GetProxy();
36 if (proxy != nullptr) {
37 auto remoteObject = proxy->AsObject();
38 if (remoteObject != nullptr) {
39 remoteObject->RemoveDeathRecipient(deathRecipient_);
40 }
41 }
42 }
43
GetInstance()44 sptr<TimeServiceClient> TimeServiceClient::GetInstance()
45 {
46 if (instance_ == nullptr) {
47 std::lock_guard<std::mutex> autoLock(instanceLock_);
48 if (instance_ == nullptr) {
49 instance_ = new TimeServiceClient;
50 }
51 }
52 return instance_;
53 }
54
ConnectService()55 bool TimeServiceClient::ConnectService()
56 {
57 if (GetProxy() != nullptr) {
58 return true;
59 }
60
61 sptr<ISystemAbilityManager> systemAbilityManager =
62 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
63 if (systemAbilityManager == nullptr) {
64 TIME_HILOGE(TIME_MODULE_CLIENT, "Getting SystemAbilityManager failed.");
65 return false;
66 }
67
68 auto systemAbility = systemAbilityManager->GetSystemAbility(TIME_SERVICE_ID);
69 if (systemAbility == nullptr) {
70 TIME_HILOGE(TIME_MODULE_CLIENT, "Get SystemAbility failed.");
71 return false;
72 }
73
74 std::lock_guard<std::mutex> autoLock(deathLock_);
75 if (deathRecipient_ == nullptr) {
76 deathRecipient_ = new TimeSaDeathRecipient();
77 }
78
79 systemAbility->AddDeathRecipient(deathRecipient_);
80 sptr<ITimeService> proxy = iface_cast<ITimeService>(systemAbility);
81 if (proxy == nullptr) {
82 TIME_HILOGE(TIME_MODULE_CLIENT, "Get TimeServiceProxy from SA failed.");
83 return false;
84 }
85 SetProxy(proxy);
86 return true;
87 }
88
SetTime(int64_t time)89 bool TimeServiceClient::SetTime(int64_t time)
90 {
91 if (!ConnectService()) {
92 return false;
93 }
94 auto proxy = GetProxy();
95 if (proxy == nullptr) {
96 return false;
97 }
98 return proxy->SetTime(time) == ERR_OK;
99 }
100
SetTime(int64_t milliseconds,int32_t & code)101 bool TimeServiceClient::SetTime(int64_t milliseconds, int32_t &code)
102 {
103 if (!ConnectService()) {
104 code = E_TIME_SA_DIED;
105 return false;
106 }
107 auto proxy = GetProxy();
108 if (proxy == nullptr) {
109 code = E_TIME_NULLPTR;
110 return false;
111 }
112 code = proxy->SetTime(milliseconds);
113 return code == ERR_OK;
114 }
115
SetTimeV9(int64_t time)116 int32_t TimeServiceClient::SetTimeV9(int64_t time)
117 {
118 if (!ConnectService()) {
119 return E_TIME_SA_DIED;
120 }
121 auto proxy = GetProxy();
122 if (proxy == nullptr) {
123 return E_TIME_NULLPTR;
124 }
125 return proxy->SetTime(time, ITimeService::API_VERSION_9);
126 }
127
SetTimeZone(const std::string & timezoneId)128 bool TimeServiceClient::SetTimeZone(const std::string &timezoneId)
129 {
130 if (!ConnectService()) {
131 return false;
132 }
133 auto proxy = GetProxy();
134 if (proxy == nullptr) {
135 return false;
136 }
137 return proxy->SetTimeZone(timezoneId) == ERR_OK;
138 }
139
SetTimeZone(const std::string & timezoneId,int32_t & code)140 bool TimeServiceClient::SetTimeZone(const std::string &timezoneId, int32_t &code)
141 {
142 if (!ConnectService()) {
143 code = E_TIME_SA_DIED;
144 return false;
145 }
146 auto proxy = GetProxy();
147 if (proxy == nullptr) {
148 code = E_TIME_NULLPTR;
149 return false;
150 }
151 code = proxy->SetTimeZone(timezoneId);
152 TIME_HILOGD(TIME_MODULE_CLIENT, "settimezone end");
153 return code == ERR_OK;
154 }
155
SetTimeZoneV9(const std::string & timezoneId)156 int32_t TimeServiceClient::SetTimeZoneV9(const std::string &timezoneId)
157 {
158 if (!ConnectService()) {
159 return E_TIME_SA_DIED;
160 }
161 auto proxy = GetProxy();
162 if (proxy == nullptr) {
163 return E_TIME_NULLPTR;
164 }
165 return proxy->SetTimeZone(timezoneId, ITimeService::API_VERSION_9);
166 }
167
CreateTimer(std::shared_ptr<ITimerInfo> timerOptions)168 uint64_t TimeServiceClient::CreateTimer(std::shared_ptr<ITimerInfo> timerOptions)
169 {
170 uint64_t timerId = 0;
171 auto errCode = CreateTimerV9(timerOptions, timerId);
172 TIME_HILOGD(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId);
173 if (errCode != E_TIME_OK) {
174 TIME_HILOGE(TIME_MODULE_CLIENT, "Non-system applications, create timer failed");
175 return 0;
176 }
177 if (timerId == 0) {
178 TIME_HILOGE(TIME_MODULE_CLIENT, "Create timer failed");
179 return 0;
180 }
181 return timerId;
182 }
183
CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions,uint64_t & timerId)184 int32_t TimeServiceClient::CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId)
185 {
186 if (timerOptions == nullptr) {
187 TIME_HILOGE(TIME_MODULE_CLIENT, "Input nullptr");
188 return E_TIME_NULLPTR;
189 }
190 if (!ConnectService()) {
191 return E_TIME_NULLPTR;
192 }
193 auto timerCallbackInfoObject = TimerCallback::GetInstance()->AsObject();
194 if (!timerCallbackInfoObject) {
195 TIME_HILOGE(TIME_MODULE_CLIENT, "New TimerCallback failed");
196 return E_TIME_NULLPTR;
197 }
198 auto proxy = GetProxy();
199 if (proxy == nullptr) {
200 return E_TIME_NULLPTR;
201 }
202 auto errCode = proxy->CreateTimer(timerOptions, timerCallbackInfoObject, timerId);
203 if (errCode != E_TIME_OK) {
204 TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed");
205 return errCode;
206 }
207 TIME_HILOGD(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId);
208 auto ret = TimerCallback::GetInstance()->InsertTimerCallbackInfo(timerId, timerOptions);
209 if (!ret) {
210 return E_TIME_DEAL_FAILED;
211 }
212 return errCode;
213 }
214
StartTimer(uint64_t timerId,uint64_t triggerTime)215 bool TimeServiceClient::StartTimer(uint64_t timerId, uint64_t triggerTime)
216 {
217 int32_t errCode = StartTimerV9(timerId, triggerTime);
218 if (errCode != E_TIME_OK) {
219 return false;
220 }
221 return true;
222 }
223
StartTimerV9(uint64_t timerId,uint64_t triggerTime)224 int32_t TimeServiceClient::StartTimerV9(uint64_t timerId, uint64_t triggerTime)
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->StartTimer(timerId, triggerTime);
234 }
235
StopTimer(uint64_t timerId)236 bool TimeServiceClient::StopTimer(uint64_t timerId)
237 {
238 int32_t errCode = StopTimerV9(timerId);
239 if (errCode != E_TIME_OK) {
240 return false;
241 }
242 return true;
243 }
244
StopTimerV9(uint64_t timerId)245 int32_t TimeServiceClient::StopTimerV9(uint64_t timerId)
246 {
247 if (!ConnectService()) {
248 return E_TIME_SA_DIED;
249 }
250 auto proxy = GetProxy();
251 if (proxy == nullptr) {
252 return E_TIME_NULLPTR;
253 }
254 return proxy->StopTimer(timerId);
255 }
256
DestroyTimer(uint64_t timerId)257 bool TimeServiceClient::DestroyTimer(uint64_t timerId)
258 {
259 int32_t errCode = DestroyTimerV9(timerId);
260 if (errCode != E_TIME_OK) {
261 return false;
262 }
263 return true;
264 }
265
DestroyTimerV9(uint64_t timerId)266 int32_t TimeServiceClient::DestroyTimerV9(uint64_t timerId)
267 {
268 if (!ConnectService()) {
269 return E_TIME_SA_DIED;
270 }
271 auto proxy = GetProxy();
272 if (proxy == nullptr) {
273 return E_TIME_NULLPTR;
274 }
275 auto errCode = proxy->DestroyTimer(timerId);
276 if (errCode == E_TIME_OK) {
277 TimerCallback::GetInstance()->RemoveTimerCallbackInfo(timerId);
278 return E_TIME_OK;
279 }
280 return errCode;
281 }
282
GetTimeZone()283 std::string TimeServiceClient::GetTimeZone()
284 {
285 std::string timeZoneId;
286 if (!ConnectService()) {
287 return std::string("");
288 }
289 auto proxy = GetProxy();
290 if (proxy == nullptr) {
291 return std::string("");
292 }
293 if (proxy->GetTimeZone(timeZoneId) != ERR_OK) {
294 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
295 return std::string("");
296 }
297 return timeZoneId;
298 }
299
GetTimeZone(std::string & timezoneId)300 int32_t TimeServiceClient::GetTimeZone(std::string &timezoneId)
301 {
302 if (!ConnectService()) {
303 return E_TIME_SA_DIED;
304 }
305 auto proxy = GetProxy();
306 if (proxy == nullptr) {
307 return E_TIME_NULLPTR;
308 }
309 if (proxy->GetTimeZone(timezoneId) != ERR_OK) {
310 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
311 return E_TIME_SA_DIED;
312 }
313 return E_TIME_OK;
314 }
315
GetWallTimeMs()316 int64_t TimeServiceClient::GetWallTimeMs()
317 {
318 int64_t time;
319 if (!ConnectService()) {
320 return -1;
321 }
322 auto proxy = GetProxy();
323 if (proxy == nullptr) {
324 return E_TIME_NULLPTR;
325 }
326 if (proxy->GetWallTimeMs(time) != ERR_OK) {
327 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
328 return -1;
329 }
330 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
331 return time;
332 }
333
GetWallTimeMs(int64_t & time)334 int32_t TimeServiceClient::GetWallTimeMs(int64_t &time)
335 {
336 if (!ConnectService()) {
337 return E_TIME_SA_DIED;
338 }
339 auto proxy = GetProxy();
340 if (proxy == nullptr) {
341 return E_TIME_NULLPTR;
342 }
343 if (proxy->GetWallTimeMs(time) != ERR_OK) {
344 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
345 return E_TIME_SA_DIED;
346 }
347 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
348 return E_TIME_OK;
349 }
350
GetWallTimeNs()351 int64_t TimeServiceClient::GetWallTimeNs()
352 {
353 int64_t time;
354 if (!ConnectService()) {
355 return -1;
356 }
357 auto proxy = GetProxy();
358 if (proxy == nullptr) {
359 return E_TIME_NULLPTR;
360 }
361 if (proxy->GetWallTimeNs(time) != ERR_OK) {
362 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
363 return -1;
364 }
365 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
366 return time;
367 }
368
GetWallTimeNs(int64_t & time)369 int32_t TimeServiceClient::GetWallTimeNs(int64_t &time)
370 {
371 if (!ConnectService()) {
372 TIME_HILOGE(TIME_MODULE_CLIENT, "connect service failed.");
373 return E_TIME_SA_DIED;
374 }
375 auto proxy = GetProxy();
376 if (proxy == nullptr) {
377 TIME_HILOGE(TIME_MODULE_CLIENT, "get proxy failed.");
378 return E_TIME_NULLPTR;
379 }
380 if (proxy->GetWallTimeNs(time) != ERR_OK) {
381 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
382 return E_TIME_SA_DIED;
383 }
384 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
385 return E_TIME_OK;
386 }
387
GetBootTimeMs()388 int64_t TimeServiceClient::GetBootTimeMs()
389 {
390 int64_t time;
391 if (!ConnectService()) {
392 return -1;
393 }
394 auto proxy = GetProxy();
395 if (proxy == nullptr) {
396 return E_TIME_NULLPTR;
397 }
398 if (proxy->GetBootTimeMs(time) != ERR_OK) {
399 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
400 return -1;
401 }
402 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
403 return time;
404 }
405
GetBootTimeMs(int64_t & time)406 int32_t TimeServiceClient::GetBootTimeMs(int64_t &time)
407 {
408 if (!ConnectService()) {
409 return E_TIME_SA_DIED;
410 }
411 auto proxy = GetProxy();
412 if (proxy == nullptr) {
413 return E_TIME_NULLPTR;
414 }
415 if (proxy->GetBootTimeMs(time) != ERR_OK) {
416 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
417 return E_TIME_SA_DIED;
418 }
419 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
420 return E_TIME_OK;
421 }
422
GetBootTimeNs()423 int64_t TimeServiceClient::GetBootTimeNs()
424 {
425 int64_t time;
426 if (!ConnectService()) {
427 return -1;
428 }
429 auto proxy = GetProxy();
430 if (proxy == nullptr) {
431 return E_TIME_NULLPTR;
432 }
433 if (proxy->GetBootTimeNs(time) != ERR_OK) {
434 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
435 return -1;
436 }
437 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
438 return time;
439 }
440
GetBootTimeNs(int64_t & time)441 int32_t TimeServiceClient::GetBootTimeNs(int64_t &time)
442 {
443 if (!ConnectService()) {
444 return E_TIME_SA_DIED;
445 }
446 auto proxy = GetProxy();
447 if (proxy == nullptr) {
448 return E_TIME_NULLPTR;
449 }
450 if (proxy->GetBootTimeNs(time) != ERR_OK) {
451 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
452 return E_TIME_SA_DIED;
453 }
454 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
455 return E_TIME_OK;
456 }
457
GetMonotonicTimeMs()458 int64_t TimeServiceClient::GetMonotonicTimeMs()
459 {
460 int64_t time;
461 if (!ConnectService()) {
462 return -1;
463 }
464 auto proxy = GetProxy();
465 if (proxy == nullptr) {
466 return E_TIME_NULLPTR;
467 }
468 if (proxy->GetMonotonicTimeMs(time) != ERR_OK) {
469 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
470 return -1;
471 }
472 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
473 return time;
474 }
475
GetMonotonicTimeMs(int64_t & time)476 int32_t TimeServiceClient::GetMonotonicTimeMs(int64_t &time)
477 {
478 if (!ConnectService()) {
479 return E_TIME_SA_DIED;
480 }
481 auto proxy = GetProxy();
482 if (proxy == nullptr) {
483 return E_TIME_NULLPTR;
484 }
485 if (proxy->GetMonotonicTimeMs(time) != ERR_OK) {
486 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
487 return E_TIME_SA_DIED;
488 }
489 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
490 return E_TIME_OK;
491 }
492
GetMonotonicTimeNs()493 int64_t TimeServiceClient::GetMonotonicTimeNs()
494 {
495 int64_t time;
496 if (!ConnectService()) {
497 return -1;
498 }
499 auto proxy = GetProxy();
500 if (proxy == nullptr) {
501 return E_TIME_NULLPTR;
502 }
503 if (proxy->GetMonotonicTimeNs(time) != ERR_OK) {
504 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
505 return -1;
506 }
507 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
508 return time;
509 }
510
GetMonotonicTimeNs(int64_t & time)511 int32_t TimeServiceClient::GetMonotonicTimeNs(int64_t &time)
512 {
513 if (!ConnectService()) {
514 return E_TIME_SA_DIED;
515 }
516 auto proxy = GetProxy();
517 if (proxy == nullptr) {
518 return E_TIME_NULLPTR;
519 }
520 if (proxy->GetMonotonicTimeNs(time) != ERR_OK) {
521 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
522 return E_TIME_SA_DIED;
523 }
524 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
525 return E_TIME_OK;
526 }
527
GetThreadTimeMs()528 int64_t TimeServiceClient::GetThreadTimeMs()
529 {
530 int64_t time;
531 if (!ConnectService()) {
532 return -1;
533 }
534 auto proxy = GetProxy();
535 if (proxy == nullptr) {
536 return E_TIME_NULLPTR;
537 }
538 if (proxy->GetThreadTimeMs(time) != ERR_OK) {
539 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
540 return -1;
541 }
542 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
543 return time;
544 }
545
GetThreadTimeMs(int64_t & time)546 int32_t TimeServiceClient::GetThreadTimeMs(int64_t &time)
547 {
548 if (!ConnectService()) {
549 return E_TIME_SA_DIED;
550 }
551 auto proxy = GetProxy();
552 if (proxy == nullptr) {
553 return E_TIME_NULLPTR;
554 }
555 if (proxy->GetThreadTimeMs(time) != ERR_OK) {
556 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
557 return E_TIME_SA_DIED;
558 }
559 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
560 return E_TIME_OK;
561 }
562
GetThreadTimeNs()563 int64_t TimeServiceClient::GetThreadTimeNs()
564 {
565 int64_t time;
566 if (!ConnectService()) {
567 return -1;
568 }
569 auto proxy = GetProxy();
570 if (proxy == nullptr) {
571 return E_TIME_NULLPTR;
572 }
573 if (proxy->GetThreadTimeNs(time) != ERR_OK) {
574 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
575 return -1;
576 }
577 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
578 return time;
579 }
580
GetThreadTimeNs(int64_t & time)581 int32_t TimeServiceClient::GetThreadTimeNs(int64_t &time)
582 {
583 if (!ConnectService()) {
584 return E_TIME_SA_DIED;
585 }
586 auto proxy = GetProxy();
587 if (proxy == nullptr) {
588 return E_TIME_NULLPTR;
589 }
590 if (proxy->GetThreadTimeNs(time) != ERR_OK) {
591 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
592 return E_TIME_SA_DIED;
593 }
594 TIME_HILOGD(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
595 return E_TIME_OK;
596 }
597
ProxyTimer(int32_t uid,bool isProxy,bool needRetrigger)598 bool TimeServiceClient::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger)
599 {
600 TIME_HILOGD(TIME_MODULE_CLIENT, "ProxyTimer start uid: %{public}d, isProxy: %{public}d", uid, isProxy);
601 if (!ConnectService()) {
602 return false;
603 }
604 auto proxy = GetProxy();
605 if (proxy == nullptr) {
606 return false;
607 }
608 return proxy->ProxyTimer(uid, isProxy, needRetrigger);
609 }
610
ResetAllProxy()611 bool TimeServiceClient::ResetAllProxy()
612 {
613 TIME_HILOGD(TIME_MODULE_CLIENT, "ResetAllProxy");
614 if (timeServiceProxy_ == nullptr) {
615 TIME_HILOGW(TIME_MODULE_CLIENT, "ResetAllProxy ConnectService");
616 ConnectService();
617 }
618 if (timeServiceProxy_ == nullptr) {
619 TIME_HILOGE(TIME_MODULE_CLIENT, "ResetAllProxy ConnectService failed.");
620 return false;
621 }
622 auto proxy = GetProxy();
623 if (proxy == nullptr) {
624 return false;
625 }
626 return proxy->ResetAllProxy();
627 }
628
GetProxy()629 sptr<ITimeService> TimeServiceClient::GetProxy()
630 {
631 std::lock_guard<std::mutex> autoLock(proxyLock_);
632 return timeServiceProxy_;
633 }
634
SetProxy(sptr<ITimeService> proxy)635 void TimeServiceClient::SetProxy(sptr<ITimeService> proxy)
636 {
637 std::lock_guard<std::mutex> autoLock(proxyLock_);
638 timeServiceProxy_ = proxy;
639 }
640
ClearProxy()641 void TimeServiceClient::ClearProxy()
642 {
643 std::lock_guard<std::mutex> autoLock(proxyLock_);
644 timeServiceProxy_ = nullptr;
645 }
646 } // namespace MiscServices
647 } // namespace OHOS