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
26 namespace OHOS {
27 namespace MiscServices {
28 std::mutex TimeServiceClient::instanceLock_;
29 sptr<TimeServiceClient> TimeServiceClient::instance_;
TimeServiceClient()30 TimeServiceClient::TimeServiceClient()
31 {
32 }
33
~TimeServiceClient()34 TimeServiceClient::~TimeServiceClient()
35 {
36 auto proxy = GetProxy();
37 if (proxy != nullptr) {
38 auto remoteObject = proxy->AsObject();
39 if (remoteObject != nullptr) {
40 remoteObject->RemoveDeathRecipient(deathRecipient_);
41 }
42 }
43 }
44
GetInstance()45 sptr<TimeServiceClient> TimeServiceClient::GetInstance()
46 {
47 if (instance_ == nullptr) {
48 std::lock_guard<std::mutex> autoLock(instanceLock_);
49 if (instance_ == nullptr) {
50 instance_ = new TimeServiceClient;
51 }
52 }
53 return instance_;
54 }
55
ConnectService()56 bool TimeServiceClient::ConnectService()
57 {
58 if (GetProxy() != nullptr) {
59 return true;
60 }
61
62 sptr<ISystemAbilityManager> systemAbilityManager =
63 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (systemAbilityManager == nullptr) {
65 TIME_HILOGE(TIME_MODULE_CLIENT, "Getting SystemAbilityManager failed.");
66 return false;
67 }
68
69 auto systemAbility = systemAbilityManager->GetSystemAbility(TIME_SERVICE_ID);
70 if (systemAbility == nullptr) {
71 TIME_HILOGE(TIME_MODULE_CLIENT, "Get SystemAbility failed.");
72 return false;
73 }
74
75 std::lock_guard<std::mutex> autoLock(deathLock_);
76 if (deathRecipient_ == nullptr) {
77 deathRecipient_ = new TimeSaDeathRecipient();
78 }
79
80 systemAbility->AddDeathRecipient(deathRecipient_);
81 sptr<ITimeService> proxy = iface_cast<ITimeService>(systemAbility);
82 if (proxy == nullptr) {
83 TIME_HILOGE(TIME_MODULE_CLIENT, "Get TimeServiceProxy from SA failed.");
84 return false;
85 }
86 SetProxy(proxy);
87 return true;
88 }
89
SetTime(const int64_t time)90 bool TimeServiceClient::SetTime(const int64_t time)
91 {
92 if (!ConnectService()) {
93 return false;
94 }
95 auto proxy = GetProxy();
96 if (proxy == nullptr) {
97 return false;
98 }
99 return proxy->SetTime(time) == ERR_OK;
100 }
101
SetTime(const int64_t milliseconds,int32_t & code)102 bool TimeServiceClient::SetTime(const int64_t milliseconds, int32_t &code)
103 {
104 if (!ConnectService()) {
105 code = E_TIME_SA_DIED;
106 return false;
107 }
108 auto proxy = GetProxy();
109 if (proxy == nullptr) {
110 code = E_TIME_NULLPTR;
111 return false;
112 }
113 code = proxy->SetTime(milliseconds);
114 return code == ERR_OK;
115 }
116
SetTimeV9(const int64_t & time)117 int32_t TimeServiceClient::SetTimeV9(const int64_t &time)
118 {
119 if (!ConnectService()) {
120 return E_TIME_SA_DIED;
121 }
122 auto proxy = GetProxy();
123 if (proxy == nullptr) {
124 return E_TIME_NULLPTR;
125 }
126 return proxy->SetTime(time, ITimeService::API_VERSION_9);
127 }
128
SetTimeZone(const std::string timezoneId)129 bool TimeServiceClient::SetTimeZone(const std::string timezoneId)
130 {
131 if (!ConnectService()) {
132 return false;
133 }
134 auto proxy = GetProxy();
135 if (proxy == nullptr) {
136 return false;
137 }
138 return proxy->SetTimeZone(timezoneId) == ERR_OK;
139 }
140
SetTimeZone(const std::string timezoneId,int32_t & code)141 bool TimeServiceClient::SetTimeZone(const std::string timezoneId, int32_t &code)
142 {
143 if (!ConnectService()) {
144 code = E_TIME_SA_DIED;
145 return false;
146 }
147 auto proxy = GetProxy();
148 if (proxy == nullptr) {
149 code = E_TIME_NULLPTR;
150 return false;
151 }
152 code = proxy->SetTimeZone(timezoneId);
153 TIME_HILOGI(TIME_MODULE_CLIENT, "settimezone end");
154 return code == ERR_OK;
155 }
156
SetTimeZoneV9(const std::string timezoneId)157 int32_t TimeServiceClient::SetTimeZoneV9(const std::string timezoneId)
158 {
159 if (!ConnectService()) {
160 return E_TIME_SA_DIED;
161 }
162 auto proxy = GetProxy();
163 if (proxy == nullptr) {
164 return E_TIME_NULLPTR;
165 }
166 return proxy->SetTimeZone(timezoneId, ITimeService::API_VERSION_9);
167 }
168
CreateTimer(std::shared_ptr<ITimerInfo> TimerOptions)169 uint64_t TimeServiceClient::CreateTimer(std::shared_ptr<ITimerInfo> TimerOptions)
170 {
171 uint64_t timerId = 0;
172 auto errCode = CreateTimerV9(TimerOptions, timerId);
173 TIME_HILOGI(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId);
174 if (errCode != E_TIME_OK) {
175 TIME_HILOGE(TIME_MODULE_CLIENT, "Non-system applications, create timer failed");
176 return 0;
177 }
178 if (timerId == 0) {
179 TIME_HILOGE(TIME_MODULE_CLIENT, "Create timer failed");
180 return 0;
181 }
182 TIME_HILOGI(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId);
183 return timerId;
184 }
185
CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions,uint64_t & timerId)186 int32_t TimeServiceClient::CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId)
187 {
188 if (timerOptions == nullptr) {
189 TIME_HILOGW(TIME_MODULE_CLIENT, "Input nullptr");
190 return E_TIME_NULLPTR;
191 }
192 if (!ConnectService()) {
193 return E_TIME_NULLPTR;
194 }
195 auto timerCallbackInfoObject = TimerCallback::GetInstance()->AsObject();
196 if (!timerCallbackInfoObject) {
197 TIME_HILOGE(TIME_MODULE_CLIENT, "New TimerCallback failed");
198 return E_TIME_NULLPTR;
199 }
200 auto proxy = GetProxy();
201 if (proxy == nullptr) {
202 return E_TIME_NULLPTR;
203 }
204 auto errCode = proxy->CreateTimer(timerOptions, timerCallbackInfoObject, timerId);
205 if (errCode != E_TIME_OK) {
206 TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed");
207 return errCode;
208 }
209 TIME_HILOGI(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId);
210 auto ret = TimerCallback::GetInstance()->InsertTimerCallbackInfo(timerId, timerOptions);
211 if (!ret) {
212 return E_TIME_DEAL_FAILED;
213 }
214 return errCode;
215 }
216
StartTimer(uint64_t timerId,uint64_t triggerTime)217 bool TimeServiceClient::StartTimer(uint64_t timerId, uint64_t triggerTime)
218 {
219 int32_t errCode = StartTimerV9(timerId, triggerTime);
220 if (errCode != E_TIME_OK) {
221 return false;
222 }
223 return true;
224 }
225
StartTimerV9(uint64_t timerId,uint64_t triggerTime)226 int32_t TimeServiceClient::StartTimerV9(uint64_t timerId, uint64_t triggerTime)
227 {
228 if (!ConnectService()) {
229 return E_TIME_SA_DIED;
230 }
231 auto proxy = GetProxy();
232 if (proxy == nullptr) {
233 return E_TIME_NULLPTR;
234 }
235 return proxy->StartTimer(timerId, triggerTime);
236 }
237
StopTimer(uint64_t timerId)238 bool TimeServiceClient::StopTimer(uint64_t timerId)
239 {
240 int32_t errCode = StopTimerV9(timerId);
241 if (errCode != E_TIME_OK) {
242 return false;
243 }
244 return true;
245 }
246
StopTimerV9(uint64_t timerId)247 int32_t TimeServiceClient::StopTimerV9(uint64_t timerId)
248 {
249 if (!ConnectService()) {
250 return E_TIME_SA_DIED;
251 }
252 auto proxy = GetProxy();
253 if (proxy == nullptr) {
254 return E_TIME_NULLPTR;
255 }
256 return proxy->StopTimer(timerId);
257 }
258
DestroyTimer(uint64_t timerId)259 bool TimeServiceClient::DestroyTimer(uint64_t timerId)
260 {
261 int32_t errCode = DestroyTimerV9(timerId);
262 if (errCode != E_TIME_OK) {
263 return false;
264 }
265 return true;
266 }
267
DestroyTimerV9(uint64_t timerId)268 int32_t TimeServiceClient::DestroyTimerV9(uint64_t timerId)
269 {
270 if (!ConnectService()) {
271 return E_TIME_SA_DIED;
272 }
273 auto proxy = GetProxy();
274 if (proxy == nullptr) {
275 return E_TIME_NULLPTR;
276 }
277 auto errCode = proxy->DestroyTimer(timerId);
278 if (errCode == E_TIME_OK) {
279 TimerCallback::GetInstance()->RemoveTimerCallbackInfo(timerId);
280 return E_TIME_OK;
281 }
282 return errCode;
283 }
284
GetTimeZone()285 std::string TimeServiceClient::GetTimeZone()
286 {
287 std::string timeZoneId;
288 if (!ConnectService()) {
289 return std::string("");
290 }
291 auto proxy = GetProxy();
292 if (proxy == nullptr) {
293 return std::string("");
294 }
295 if (proxy->GetTimeZone(timeZoneId) != ERR_OK) {
296 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
297 return std::string("");
298 }
299 return timeZoneId;
300 }
301
GetTimeZone(std::string & timezoneId)302 int32_t TimeServiceClient::GetTimeZone(std::string &timezoneId)
303 {
304 if (!ConnectService()) {
305 return E_TIME_SA_DIED;
306 }
307 auto proxy = GetProxy();
308 if (proxy == nullptr) {
309 return E_TIME_NULLPTR;
310 }
311 if (proxy->GetTimeZone(timezoneId) != ERR_OK) {
312 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
313 return E_TIME_SA_DIED;
314 }
315 return E_TIME_OK;
316 }
317
GetWallTimeMs()318 int64_t TimeServiceClient::GetWallTimeMs()
319 {
320 int64_t time;
321 if (!ConnectService()) {
322 return -1;
323 }
324 auto proxy = GetProxy();
325 if (proxy == nullptr) {
326 return E_TIME_NULLPTR;
327 }
328 if (proxy->GetWallTimeMs(time) != ERR_OK) {
329 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
330 return -1;
331 }
332 TIME_HILOGI(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
333 return time;
334 }
335
GetWallTimeMs(int64_t & time)336 int32_t TimeServiceClient::GetWallTimeMs(int64_t &time)
337 {
338 if (!ConnectService()) {
339 return E_TIME_SA_DIED;
340 }
341 auto proxy = GetProxy();
342 if (proxy == nullptr) {
343 return E_TIME_NULLPTR;
344 }
345 if (proxy->GetWallTimeMs(time) != ERR_OK) {
346 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
347 return E_TIME_SA_DIED;
348 }
349 TIME_HILOGI(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
350 return E_TIME_OK;
351 }
352
GetWallTimeNs()353 int64_t TimeServiceClient::GetWallTimeNs()
354 {
355 int64_t time;
356 if (!ConnectService()) {
357 return -1;
358 }
359 auto proxy = GetProxy();
360 if (proxy == nullptr) {
361 return E_TIME_NULLPTR;
362 }
363 if (proxy->GetWallTimeNs(time) != ERR_OK) {
364 TIME_HILOGE(TIME_MODULE_CLIENT, "get failed.");
365 return -1;
366 }
367 TIME_HILOGI(TIME_MODULE_SERVICE, "Result: %{public}" PRId64 "", time);
368 return time;
369 }
370
GetWallTimeNs(int64_t & time)371 int32_t TimeServiceClient::GetWallTimeNs(int64_t &time)
372 {
373 if (!ConnectService()) {
374 return E_TIME_SA_DIED;
375 }
376 auto proxy = GetProxy();
377 if (proxy == nullptr) {
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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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