From 5f2d294c09d0fe296cf22f086f3df0bd055c11e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=9C=AA=E6=9D=A5?= Date: Mon, 30 Dec 2024 14:24:22 +0800 Subject: [PATCH] =?UTF-8?q?TimeTick=E5=9C=A8ScreenON=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E8=A1=A5=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 徐未来 --- services/BUILD.gn | 2 - services/time/include/time_tick_notify.h | 9 +-- services/time/src/power_subscriber.cpp | 2 +- services/time/src/time_tick_notify.cpp | 74 +++++++++---------- services/time_system_ability.cpp | 1 - services/timer/include/timer_proxy.h | 2 +- .../service_test/src/time_service_test.cpp | 51 ++++--------- 7 files changed, 53 insertions(+), 88 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index ce76a4be..75fd0f53 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -71,7 +71,6 @@ ohos_shared_library("time_system_ability") { "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "cJSON:cjson", - "c_utils:utils", "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hisysevent:libhisysevent", @@ -144,7 +143,6 @@ ohos_static_library("time_system_ability_static") { "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "cJSON:cjson", - "c_utils:utils", "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hisysevent:libhisysevent", diff --git a/services/time/include/time_tick_notify.h b/services/time/include/time_tick_notify.h index c6547eb3..17b423eb 100644 --- a/services/time/include/time_tick_notify.h +++ b/services/time/include/time_tick_notify.h @@ -18,8 +18,6 @@ #include -#include "timer.h" - namespace OHOS { namespace MiscServices { class TimeTickNotify { @@ -30,13 +28,12 @@ public: void Init(); void Callback(); void Stop(); - void PowerCallback(); private: - uint64_t RefreshNextTriggerTime(); - Utils::Timer timer_; - uint32_t timerId_; + std::pair RefreshNextTriggerTime(); + uint64_t timerId_; std::mutex timeridMutex_; + int64_t lastTriggerTime_ = 0; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/time/src/power_subscriber.cpp b/services/time/src/power_subscriber.cpp index 3ea20ef2..86ec11e1 100644 --- a/services/time/src/power_subscriber.cpp +++ b/services/time/src/power_subscriber.cpp @@ -48,7 +48,7 @@ void PowerSubscriber::OnReceiveEvent(const CommonEventData &data) void PowerSubscriber::PowerBroadcast(const CommonEventData &data) { - TimeTickNotify::GetInstance().PowerCallback(); + TimeTickNotify::GetInstance().Callback(); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/time/src/time_tick_notify.cpp b/services/time/src/time_tick_notify.cpp index b34bd05d..7aabad8d 100644 --- a/services/time/src/time_tick_notify.cpp +++ b/services/time/src/time_tick_notify.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include "common_timer_errors.h" #include "matching_skills.h" @@ -32,8 +33,8 @@ namespace OHOS { namespace MiscServices { namespace { constexpr uint64_t MINUTE_TO_MILLISECOND = 60000; -constexpr uint64_t MICRO_TO_MILLISECOND = 1000; -constexpr uint64_t MILLISECOND_TO_SECOND = 1000; +constexpr uint64_t SECOND_TO_MILLISECOND = 1000; +constexpr int64_t SECOND_TO_NANO = 1000000000; } // namespace TimeTickNotify &TimeTickNotify::GetInstance() @@ -42,65 +43,58 @@ TimeTickNotify &TimeTickNotify::GetInstance() return instance; } -TimeTickNotify::TimeTickNotify() : timer_("TickTimer"){}; +TimeTickNotify::TimeTickNotify() = default; TimeTickNotify::~TimeTickNotify() = default; void TimeTickNotify::Init() { TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify start."); - uint32_t ret = timer_.Setup(); - if (ret != Utils::TIMER_ERR_OK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "Timer Setup failed: %{public}d", ret); - return; - } - auto callback = [this]() { this->Callback(); }; - uint64_t nextTriggerTime = RefreshNextTriggerTime(); - TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime); + TimerPara timerPara{}; + timerPara.timerType = static_cast(ITimerManager::TimerType::RTC); + timerPara.windowLength = 0; + timerPara.interval = 0; + timerPara.flag = 0; + auto callback = [this](uint64_t id) -> int32_t { + this->Callback(); + return E_TIME_OK; + }; std::lock_guard lock(timeridMutex_); - timerId_ = timer_.Register(callback, nextTriggerTime); - TIME_HILOGD(TIME_MODULE_SERVICE, "Tick timer ID: %{public}d", timerId_); + TimeSystemAbility::GetInstance()->CreateTimer(timerPara, callback, timerId_); + auto trigger = RefreshNextTriggerTime(); + TimeSystemAbility::GetInstance()->StartTimer(timerId_, trigger.first); + TIME_HILOGI(TIME_MODULE_SERVICE, "Tick timer timerId: %{public}" PRIu64 "", timerId_); } void TimeTickNotify::Callback() { std::lock_guard lock(timeridMutex_); - timer_.Unregister(timerId_); - TIME_HILOGD(TIME_MODULE_SERVICE, "Unregister id: %{public}d", timerId_); - uint64_t nextTriggerTime = RefreshNextTriggerTime(); - auto callback = [this]() { this->Callback(); }; - timerId_ = timer_.Register(callback, nextTriggerTime, true); - if (nextTriggerTime > (MINUTE_TO_MILLISECOND - MILLISECOND_TO_SECOND)) { + auto trigger = RefreshNextTriggerTime(); + TimeSystemAbility::GetInstance()->StartTimer(timerId_, trigger.first); + if (trigger.second) { auto currentTime = steady_clock::now().time_since_epoch().count(); - TimeServiceNotify::GetInstance().PublishTimeTickEvents(currentTime); + if (std::abs(currentTime - lastTriggerTime_) > SECOND_TO_NANO) { + TimeServiceNotify::GetInstance().PublishTimeTickEvents(currentTime); + lastTriggerTime_ = currentTime; + } } - TIME_HILOGI(TIME_MODULE_SERVICE, "id: %{public}d triggertime: %{public}" PRId64 "", timerId_, nextTriggerTime); -} - -void TimeTickNotify::PowerCallback() -{ - std::lock_guard lock(timeridMutex_); - timer_.Unregister(timerId_); - TIME_HILOGD(TIME_MODULE_SERVICE, "Unregister id: %{public}d", timerId_); - uint64_t nextTriggerTime = RefreshNextTriggerTime(); - auto callback = [this]() { this->Callback(); }; - timerId_ = timer_.Register(callback, nextTriggerTime, true); - TIME_HILOGI(TIME_MODULE_SERVICE, "id: %{public}d triggertime: %{public}" PRId64 "", timerId_, nextTriggerTime); + TIME_HILOGI(TIME_MODULE_SERVICE, "id: %{public}" PRIu64 "", timerId_); } -uint64_t TimeTickNotify::RefreshNextTriggerTime() +std::pair TimeTickNotify::RefreshNextTriggerTime() { - auto UTCTimeMicro = static_cast(duration_cast(system_clock::now() - .time_since_epoch()).count()); - TIME_HILOGD(TIME_MODULE_SERVICE, "Time micro: %{public}" PRIu64 "", UTCTimeMicro); - uint64_t timeMilliseconds = (UTCTimeMicro / MICRO_TO_MILLISECOND) % MINUTE_TO_MILLISECOND; - uint64_t nextTriggerTime = MINUTE_TO_MILLISECOND - timeMilliseconds; - return nextTriggerTime; + int64_t time = 0; + TimeUtils::GetWallTimeMs(time); + uint64_t currTime = static_cast(time); + uint64_t timeMilliseconds = currTime % MINUTE_TO_MILLISECOND; + bool isFirstSecond = timeMilliseconds < SECOND_TO_MILLISECOND; + uint64_t nextTriggerTime = ((currTime / MINUTE_TO_MILLISECOND) + 1) * MINUTE_TO_MILLISECOND; + return std::make_pair(nextTriggerTime, isFirstSecond); } void TimeTickNotify::Stop() { TIME_HILOGD(TIME_MODULE_SERVICE, "start."); - timer_.Shutdown(); + TimeSystemAbility::GetInstance()->DestroyTimer(timerId_, false); TIME_HILOGD(TIME_MODULE_SERVICE, "end."); } } // namespace MiscServices diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index a45dea11..af3cba3c 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -535,7 +535,6 @@ bool TimeSystemAbility::SetRealTime(int64_t time) if (currentTime < (time - ONE_MILLI) || currentTime > (time + ONE_MILLI)) { TimeServiceNotify::GetInstance().PublishTimeChangeEvents(currentTime); } - TimeTickNotify::GetInstance().PowerCallback(); return true; } diff --git a/services/timer/include/timer_proxy.h b/services/timer/include/timer_proxy.h index 18e2c176..39354c52 100644 --- a/services/timer/include/timer_proxy.h +++ b/services/timer/include/timer_proxy.h @@ -113,7 +113,7 @@ private: std::map>> proxyMap_ {}; std::map>> proxyPidMap_ {}; std::mutex adjustMutex_; - std::unordered_set adjustExemptionList_ {}; + std::unordered_set adjustExemptionList_ { "time_service" }; std::vector> adjustTimers_ {}; /* ms for 3 days */ int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000; diff --git a/test/unittest/service_test/src/time_service_test.cpp b/test/unittest/service_test/src/time_service_test.cpp index 729be7d3..1d64da99 100644 --- a/test/unittest/service_test/src/time_service_test.cpp +++ b/test/unittest/service_test/src/time_service_test.cpp @@ -64,6 +64,7 @@ constexpr int ONE_HUNDRED = 100; constexpr int FIVE_HUNDRED = 500; constexpr uint64_t MICRO_TO_MILLISECOND = 1000; constexpr int TIMER_ALARM_COUNT = 50; +constexpr int64_t MINUTE_TO_MILLISECOND = 60000; static HapPolicyParams g_policyA = { .apl = APL_SYSTEM_CORE, @@ -1042,47 +1043,23 @@ HWTEST_F(TimeServiceTest, NtpTrustedTime001, TestSize.Level0) } /** -* @tc.name: PowerSubscriber001 -* @tc.desc: test power subscriber data is invalid. +* @tc.name: TimeTick001 +* @tc.desc: Check RefreshNextTriggerTime(). * @tc.type: FUNC * @tc.require: */ -HWTEST_F(TimeServiceTest, PowerSubscriber001, TestSize.Level0) +HWTEST_F(TimeServiceTest, TimeTick001, TestSize.Level0) { - auto timerId = TimeTickNotify::GetInstance().timerId_; - std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED; - EventFwk::Want want; - want.SetAction(commonEvent); - int32_t code = 100; - std::string data(commonEvent); - EventFwk::CommonEventData eventData(want, code, data); - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED); - auto subscriber = std::make_shared(CommonEventSubscribeInfo(matchingSkills)); - subscriber->OnReceiveEvent(eventData); - EXPECT_EQ(timerId, TimeTickNotify::GetInstance().timerId_); -} - -/** -* @tc.name: PowerSubscriber002 -* @tc.desc: test power subscriber data is valid. -* @tc.type: FUNC -* @tc.require: -*/ -HWTEST_F(TimeServiceTest, PowerSubscriber002, TestSize.Level0) -{ - auto timerId = TimeTickNotify::GetInstance().timerId_; - std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON; - EventFwk::Want want; - want.SetAction(commonEvent); - int32_t code = RESERVED_UID; - std::string data(commonEvent); - EventFwk::CommonEventData eventData(want, code, data); - OHOS::EventFwk::MatchingSkills matchingSkills; - matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON); - auto subscriber = std::make_shared(CommonEventSubscribeInfo(matchingSkills)); - subscriber->OnReceiveEvent(eventData); - EXPECT_NE(timerId, TimeTickNotify::GetInstance().timerId_); + AddPermission(); + int64_t time = 0; + TimeUtils::GetWallTimeMs(time); + time = (time / MINUTE_TO_MILLISECOND) * MINUTE_TO_MILLISECOND; + bool result = TimeServiceClient::GetInstance()->SetTime(time); + EXPECT_TRUE(result); + auto pair = TimeTickNotify::GetInstance().RefreshNextTriggerTime(); + EXPECT_EQ(pair.first, time + MINUTE_TO_MILLISECOND); + EXPECT_TRUE(pair.second); + DeletePermission(); } /** -- Gitee