• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "time_tick_notify.h"
16 
17 #include <chrono>
18 #include <cinttypes>
19 #include <ctime>
20 #include <thread>
21 
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "common_timer_errors.h"
25 #include "matching_skills.h"
26 #include "power_subscriber.h"
27 #include "time_common.h"
28 #include "time_service_notify.h"
29 #include "time_system_ability.h"
30 #include "timer_manager_interface.h"
31 
32 using namespace std::chrono;
33 
34 namespace OHOS {
35 namespace MiscServices {
36 using namespace OHOS::EventFwk;
37 namespace {
38 constexpr uint64_t MINUTE_TO_MILLISECOND = 60000;
39 constexpr uint64_t MICRO_TO_MILESECOND = 1000;
40 }
41 
TimeTickNotify()42 TimeTickNotify::TimeTickNotify() : timer_("TickTimer") {};
~TimeTickNotify()43 TimeTickNotify::~TimeTickNotify() {};
44 
Init()45 void TimeTickNotify::Init()
46 {
47     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify start.");
48     MatchingSkills matchingSkills;
49     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
50     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
51     std::shared_ptr<PowerSubscriber> subscriberPtr = std::make_shared<PowerSubscriber>(subscriberInfo);
52     bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
53     if (!subscribeResult) {
54         TIME_HILOGE(TIME_MODULE_SERVICE, "SubscribeCommonEvent failed");
55     }
56     uint32_t ret = timer_.Setup();
57     if (ret != Utils::TIMER_ERR_OK) {
58         TIME_HILOGE(TIME_MODULE_SERVICE, "Timer Setup failed: %{public}d", ret);
59         return;
60     }
61     auto callback = [this]() {
62         this->Callback();
63     };
64     RefreshNextTriggerTime();
65     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime_);
66     timerId_ = timer_.Register(callback, nextTriggerTime_);
67     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick timer ID: %{public}d", timerId_);
68 }
69 
Callback()70 void TimeTickNotify::Callback()
71 {
72     auto currentTime = steady_clock::now().time_since_epoch().count();
73     DelayedSingleton<TimeServiceNotify>::GetInstance()->PublishTimeTickEvents(currentTime);
74     timer_.Unregister(timerId_);
75     RefreshNextTriggerTime();
76     auto callback = [this]() { this->Callback(); };
77     timerId_ = timer_.Register(callback, nextTriggerTime_);
78     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime_);
79     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick timer ID: %{public}d", timerId_);
80 }
81 
PowerCallback()82 void TimeTickNotify::PowerCallback()
83 {
84     timer_.Unregister(timerId_);
85     RefreshNextTriggerTime();
86     auto callback = [this]() { this->Callback(); };
87     timerId_ = timer_.Register(callback, nextTriggerTime_);
88     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime_);
89     TIME_HILOGD(TIME_MODULE_SERVICE, "Tick timer ID: %{public}d", timerId_);
90 }
91 
RefreshNextTriggerTime()92 void TimeTickNotify::RefreshNextTriggerTime()
93 {
94     time_t t = time(nullptr);
95     struct tm *tblock = localtime(&t);
96     TIME_HILOGI(TIME_MODULE_SERVICE, "Time now: %{public}s", asctime(tblock));
97     auto UTCTimeMicro = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
98     TIME_HILOGI(TIME_MODULE_SERVICE, "Time Now Mirc");
99     auto timeMilliseconds = GetMillisecondsFromUTC(UTCTimeMicro);
100     nextTriggerTime_ = MINUTE_TO_MILLISECOND - timeMilliseconds;
101 }
102 
Stop()103 void TimeTickNotify::Stop()
104 {
105     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
106     timer_.Shutdown();
107     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
108 }
109 
GetMillisecondsFromUTC(uint64_t UTCtimeMicro)110 uint64_t TimeTickNotify::GetMillisecondsFromUTC(uint64_t UTCtimeMicro)
111 {
112     TIME_HILOGD(TIME_MODULE_SERVICE, "Time micro: %{public}" PRId64 "", UTCtimeMicro);
113     auto miliseconds = (UTCtimeMicro / MICRO_TO_MILESECOND) % MINUTE_TO_MILLISECOND;
114     TIME_HILOGD(TIME_MODULE_SERVICE, "Time mili: %{public}" PRId64 "", miliseconds);
115     return miliseconds;
116 }
117 } // MiscServices
118 } // OHOS