From b73c5557f3ec008f38fbea5ccd641d95b4ee06fe Mon Sep 17 00:00:00 2001 From: BenjaminZhang000 Date: Tue, 5 Aug 2025 16:02:44 +0800 Subject: [PATCH] feat: Modify ulsr wakeup logic, add ulsr hook Signed-off-by: BenjaminZhang000 --- bundle.json | 1 + .../native/include/power_state_machine_info.h | 2 ++ powermgr.gni | 1 + services/BUILD.gn | 4 +++ .../native/profile/power_wakeup_action.json | 5 ++++ services/native/src/power_hdi_callback.cpp | 8 +++++ services/native/src/power_mgr_service.cpp | 8 ++++- services/native/src/power_state_machine.cpp | 3 ++ .../native/src/suspend/suspend_controller.cpp | 22 +++++++++----- .../native/src/suspend/suspend_controller.h | 6 ++-- .../native/src/wakeup/wakeup_controller.cpp | 17 ++++++----- .../native/src/wakeup/wakeup_controller.h | 4 +-- .../wakeup_action_controller.cpp | 2 +- .../wakeup_action/wakeup_action_controller.h | 2 +- .../wakeup_action_source_parser.cpp | 10 ++++--- .../interface_test/power_mgr_service_test.cpp | 2 ++ .../power_suspend_controller_test.cpp | 30 +++++++++++++++++++ utils/native/src/power_utils.cpp | 2 ++ 18 files changed, 101 insertions(+), 28 deletions(-) diff --git a/bundle.json b/bundle.json index 643681cb..41c9ffe9 100644 --- a/bundle.json +++ b/bundle.json @@ -32,6 +32,7 @@ "power_manager_feature_power_dialog", "power_manager_feature_enable_s4", "power_manager_feature_enable_suspend_with_tag", + "power_manager_feature_enable_ulsr_plugin", "power_manager_feature_audio_lock_unproxy", "power_manager_feature_disable_auto_displayoff", "power_manager_feature_doubleclick", diff --git a/interfaces/inner_api/native/include/power_state_machine_info.h b/interfaces/inner_api/native/include/power_state_machine_info.h index 5873e5ed..106a387a 100644 --- a/interfaces/inner_api/native/include/power_state_machine_info.h +++ b/interfaces/inner_api/native/include/power_state_machine_info.h @@ -169,6 +169,7 @@ enum class WakeupDeviceType : uint32_t { WAKEUP_DEVICE_TENT_MODE_CHANGE = 32, WAKEUP_DEVICE_END_DREAM = 33, WAKEUP_DEVICE_BLUETOOTH_INCOMING_CALL = 34, + WAKEUP_DEVICE_FROM_ULSR = 35, WAKEUP_DEVICE_MAX }; @@ -249,6 +250,7 @@ enum class StateChangeReason : uint32_t { STATE_CHANGE_REASON_START_DREAM = 50, STATE_CHANGE_REASON_END_DREAM = 51, STATE_CHANGE_REASON_BLUETOOTH_INCOMING_CALL = 52, + STATE_CHANGE_REASON_WAKEUP_FROM_ULSR = 53, STATE_CHANGE_REASON_REMOTE = 100, STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK = 101, STATE_CHANGE_REASON_EXIT_SYSTEM_STR = 102, diff --git a/powermgr.gni b/powermgr.gni index fb7d30b6..08238f37 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -26,6 +26,7 @@ declare_args() { power_manager_feature_power_dialog = true power_manager_feature_enable_s4 = false power_manager_feature_enable_suspend_with_tag = false + power_manager_feature_enable_ulsr_plugin = false power_manager_feature_audio_lock_unproxy = false power_manager_feature_screen_on_timeout_check = false power_manager_feature_disable_auto_displayoff = false diff --git a/services/BUILD.gn b/services/BUILD.gn index f65d7af9..5c84fb38 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -219,6 +219,10 @@ ohos_shared_library("powermgrservice") { ] } + if (power_manager_feature_enable_ulsr_plugin) { + defines += [ "POWER_MANAGER_ENABLE_ULSR_PLUGIN" ] + } + if (power_manager_feature_force_sleep_broadcast) { defines += [ "POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST" ] } diff --git a/services/native/profile/power_wakeup_action.json b/services/native/profile/power_wakeup_action.json index 5778295c..fe8a4ce1 100644 --- a/services/native/profile/power_wakeup_action.json +++ b/services/native/profile/power_wakeup_action.json @@ -18,5 +18,10 @@ "scene": "UlsrLowRsoc", "action": 1, "description": "(such as)56 is a wakeup reason by reading node through HDI interface(GetWakeupReason)" + }, + "74": { + "scene": "UlsrWakeupNearlinkRing", + "action": 0, + "description": "(such as)74 is a wakeup reason by reading node through HDI interface(GetWakeupReason)" } } \ No newline at end of file diff --git a/services/native/src/power_hdi_callback.cpp b/services/native/src/power_hdi_callback.cpp index 65556b1c..0ccb6038 100644 --- a/services/native/src/power_hdi_callback.cpp +++ b/services/native/src/power_hdi_callback.cpp @@ -43,6 +43,14 @@ int32_t PowerHdiCallbackExt::OnSuspendWithTag(const std::string& tag) int32_t PowerHdiCallbackExt::OnWakeupWithTag(const std::string& tag) { POWER_HILOGD(FEATURE_WAKEUP, "OnWakeupWithTag, tag:%{public}s", tag.c_str()); +#ifdef POWER_MANAGER_ENABLE_ULSR_PLUGIN + if (tag == "ulsr") { + int ret = HookMgrExecute(GetPowerHookMgr(), static_cast(PowerHookStage::POWER_HDI_CALLBACK_WAKEUP), + nullptr, nullptr); + POWER_HILOGI(FEATURE_WAKEUP, "OnWakeupWithTag, HookMgrExecute, stage: %{public}d, ret: %{public}d", + PowerHookStage::POWER_HDI_CALLBACK_WAKEUP, ret); + } +#endif return HDF_SUCCESS; } } // OHOS diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 549cbbe1..acddf834 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -918,7 +918,9 @@ static const char* UNREGISTER_EXTERNAL_CONFIG = "UnregisterExternalCallback"; using WakeupFunc = std::function; using SuspendFunc = std::function; using PowerConfigFunc = std::function; -typedef void (*FuncRegisterExternalCallback)(WakeupFunc, SuspendFunc, PowerConfigFunc, PowerConfigFunc); +using TriggerSyncSleepCallbackFunc = std::function; +typedef void (*FuncRegisterExternalCallback)(WakeupFunc, SuspendFunc, PowerConfigFunc, PowerConfigFunc, + TriggerSyncSleepCallbackFunc); typedef void (*FuncUnregisterExternalCallback)(); void PowerMgrService::RegisterExternalCallback() @@ -952,6 +954,10 @@ void PowerMgrService::RegisterExternalCallback() }, [](std::string& sceneName, std::string value) { return SystemSuspendController::GetInstance().SetPowerConfig(sceneName, value); + }, + [this](bool isWakeup) { + POWER_HILOGI(COMP_SVC, "[UL_POWER] sync sleep callback triggered, isWakeup: %{public}d", isWakeup); + suspendController_->TriggerSyncSleepCallback(isWakeup); }); POWER_HILOGI(COMP_SVC, "RegisterExternalCallback Success"); dlclose(subscriberHandler); diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index 30395108..134957a0 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -2239,6 +2239,9 @@ StateChangeReason PowerStateMachine::GetReasonByWakeType(WakeupDeviceType type) case WakeupDeviceType::WAKEUP_DEVICE_BLUETOOTH_INCOMING_CALL: ret = StateChangeReason::STATE_CHANGE_REASON_BLUETOOTH_INCOMING_CALL; break; + case WakeupDeviceType::WAKEUP_DEVICE_FROM_ULSR: + ret = StateChangeReason::STATE_CHANGE_REASON_WAKEUP_FROM_ULSR; + break; case WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN: // fall through default: break; diff --git a/services/native/src/suspend/suspend_controller.cpp b/services/native/src/suspend/suspend_controller.cpp index 04028365..6a276b5d 100644 --- a/services/native/src/suspend/suspend_controller.cpp +++ b/services/native/src/suspend/suspend_controller.cpp @@ -784,14 +784,14 @@ void SuspendController::Reset() } #ifdef POWER_MANAGER_WAKEUP_ACTION -bool SuspendController::GetLowCapacityPowerKeyFlag() +bool SuspendController::GetWakeupReasonConfigMatchedFlag() { - return isLowCapacityPowerKey_; + return isWakeupReasonConfigMatched_; } -void SuspendController::SetLowCapacityPowerKeyFlag(bool flag) +void SuspendController::SetWakeupReasonConfigMatchedFlag(bool flag) { - isLowCapacityPowerKey_ = flag; + isWakeupReasonConfigMatched_ = flag; } #endif @@ -875,12 +875,18 @@ void PowerKeySuspendMonitor::ReceivePowerkeyCallback(std::shared_ptr wakeupController = pms->GetWakeupController(); + if (wakeupController == nullptr) { + POWER_HILOGE(FEATURE_WAKEUP, "[UL_POWER] wakeupController is nullptr"); + return; + } #if POWER_MANAGER_WAKEUP_ACTION - bool isLowCapacityPowerKey = suspendController->GetLowCapacityPowerKeyFlag(); - if (isLowCapacityPowerKey) { - POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] skip low capacity powerkey up"); - suspendController->SetLowCapacityPowerKeyFlag(false); + bool isWakeupReasonConfigMatched = suspendController->GetWakeupReasonConfigMatchedFlag(); + if (isWakeupReasonConfigMatched + || wakeupController->IsWakeupReasonConfigMatched(WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON)) { + POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] wakeup reason matcged config, skip powerkey up"); + suspendController->SetWakeupReasonConfigMatchedFlag(false); return; } #endif diff --git a/services/native/src/suspend/suspend_controller.h b/services/native/src/suspend/suspend_controller.h index 54f78cd3..78dd50ba 100644 --- a/services/native/src/suspend/suspend_controller.h +++ b/services/native/src/suspend/suspend_controller.h @@ -100,8 +100,8 @@ public: } #endif #ifdef POWER_MANAGER_WAKEUP_ACTION - bool GetLowCapacityPowerKeyFlag(); - void SetLowCapacityPowerKeyFlag(bool flag); + bool GetWakeupReasonConfigMatchedFlag(); + void SetWakeupReasonConfigMatchedFlag(bool flag); #endif private: @@ -145,7 +145,7 @@ private: #endif sptr suspendPowerStateCallback_ {nullptr}; #ifdef POWER_MANAGER_WAKEUP_ACTION - std::atomic isLowCapacityPowerKey_ {false}; + std::atomic isWakeupReasonConfigMatched_ {false}; #endif }; diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index a6dc33a7..f04b9c65 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -507,7 +507,7 @@ WakeupController::SleepGuard::~SleepGuard() } #ifdef POWER_MANAGER_WAKEUP_ACTION -bool WakeupController::IsLowCapacityWakeup(WakeupDeviceType reason) +bool WakeupController::IsWakeupReasonConfigMatched(WakeupDeviceType reason) { auto pms = DelayedSpSingleton::GetInstance(); if (pms == nullptr) { @@ -520,12 +520,12 @@ bool WakeupController::IsLowCapacityWakeup(WakeupDeviceType reason) return false; } return (reason == WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON) && - (wakeupActionController->IsLowCapacityWakeup()); + (wakeupActionController->IsWakeupReasonConfigMatched()); } -void WakeupController::ProcessLowCapacityWakeup() +void WakeupController::ProcessWakeupReason() { - POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] processing low capacity wake up begins."); + POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] processing wake up reason begins."); if (stateMachine_->GetState() != PowerState::SLEEP) { POWER_HILOGE(FEATURE_WAKEUP, "[UL_POWER] the current power state is not sleep."); return; @@ -1005,12 +1005,13 @@ void PowerkeyWakeupMonitor::ReceivePowerkeyCallback(std::shared_ptrIsLowCapacityWakeup(reason)) { - wakeupController->ProcessLowCapacityWakeup(); - suspendController->SetLowCapacityPowerKeyFlag(true); + // When received powerkey down event, must read wakeup reason! + if (wakeupController->IsWakeupReasonConfigMatched(WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON)) { + wakeupController->ProcessWakeupReason(); + suspendController->SetWakeupReasonConfigMatchedFlag(true); return; } - suspendController->SetLowCapacityPowerKeyFlag(false); + suspendController->SetWakeupReasonConfigMatchedFlag(false); #endif bool poweroffInterrupted = false; diff --git a/services/native/src/wakeup/wakeup_controller.h b/services/native/src/wakeup/wakeup_controller.h index f5c9d988..863fef83 100644 --- a/services/native/src/wakeup/wakeup_controller.h +++ b/services/native/src/wakeup/wakeup_controller.h @@ -52,8 +52,8 @@ public: void NotifyDisplayActionDone(uint32_t event); void SetOriginSettingValue(WakeupSource& source); #ifdef POWER_MANAGER_WAKEUP_ACTION - bool IsLowCapacityWakeup(WakeupDeviceType reason); - void ProcessLowCapacityWakeup(); + bool IsWakeupReasonConfigMatched(WakeupDeviceType reason); + void ProcessWakeupReason(); #endif static int32_t SetWakeupDoubleClickSensor(bool enable); static void ChangeWakeupSourceConfig(bool updateEnable); diff --git a/services/native/src/wakeup_action/wakeup_action_controller.cpp b/services/native/src/wakeup_action/wakeup_action_controller.cpp index dcc42367..2aeffba9 100644 --- a/services/native/src/wakeup_action/wakeup_action_controller.cpp +++ b/services/native/src/wakeup_action/wakeup_action_controller.cpp @@ -45,7 +45,7 @@ void WakeupActionController::Init() } } -bool WakeupActionController::IsLowCapacityWakeup() +bool WakeupActionController::IsWakeupReasonConfigMatched() { std::string reason; SystemSuspendController::GetInstance().GetWakeupReason(reason); diff --git a/services/native/src/wakeup_action/wakeup_action_controller.h b/services/native/src/wakeup_action/wakeup_action_controller.h index 89adc334..df38fc2b 100644 --- a/services/native/src/wakeup_action/wakeup_action_controller.h +++ b/services/native/src/wakeup_action/wakeup_action_controller.h @@ -36,7 +36,7 @@ public: ~WakeupActionController(); void Init(); bool ExecuteByGetReason(); - bool IsLowCapacityWakeup(); + bool IsWakeupReasonConfigMatched(); private: void HandleAction(const std::string& reason); diff --git a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp index 0e7e232c..c946dde8 100644 --- a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp +++ b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp @@ -115,7 +115,7 @@ bool WakeupActionSourceParser::ParseSourcesProc( std::shared_ptr& parseSources, cJSON* valueObj, std::string& key) { std::string scene{""}; - uint32_t action = 0; + uint32_t action = ILLEGAL_ACTION; if (PowerMgrJsonUtils::IsValidJsonObject(valueObj)) { cJSON* sceneValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::SCENE_KEY); if (PowerMgrJsonUtils::IsValidJsonString(sceneValue)) { @@ -125,14 +125,16 @@ bool WakeupActionSourceParser::ParseSourcesProc( cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::ACTION_KEY); if (PowerMgrJsonUtils::IsValidJsonNumber(actionValue)) { action = static_cast(actionValue->valueint); - POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action); if (action >= ILLEGAL_ACTION) { - action = 0; + action = ILLEGAL_ACTION; + POWER_HILOGW(FEATURE_WAKEUP_ACTION, "action=%{public}u, ILLEGAL!", action); + } else { + POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action); } } } - if (action != 0) { + if (action != ILLEGAL_ACTION) { std::shared_ptr wakeupActionSource = std::make_shared(scene, action); parseSources->PutSource(key, wakeupActionSource); } diff --git a/test/unittest/src/interface_test/power_mgr_service_test.cpp b/test/unittest/src/interface_test/power_mgr_service_test.cpp index e29243e2..ae11550f 100644 --- a/test/unittest/src/interface_test/power_mgr_service_test.cpp +++ b/test/unittest/src/interface_test/power_mgr_service_test.cpp @@ -638,6 +638,8 @@ HWTEST_F(PowerMgrServiceTest, PowerMgrService025, TestSize.Level2) StateChangeReason::STATE_CHANGE_REASON_BLUETOOTH_INCOMING_CALL); EXPECT_EQ(stateMaschine_->GetReasonByWakeType(WakeupDeviceType::WAKEUP_DEVICE_PICKUP), StateChangeReason::STATE_CHANGE_REASON_PICKUP); + EXPECT_EQ(stateMaschine_->GetReasonByWakeType(WakeupDeviceType::WAKEUP_DEVICE_FROM_ULSR), + StateChangeReason::STATE_CHANGE_REASON_WAKEUP_FROM_ULSR); pmsTest_->OnStop(); POWER_HILOGI(LABEL_TEST, "PowerMgrServiceTest::PowerMgrService025 function end!"); } diff --git a/test/unittest/src/interface_test/power_suspend_controller_test.cpp b/test/unittest/src/interface_test/power_suspend_controller_test.cpp index c10e8370..ae192325 100644 --- a/test/unittest/src/interface_test/power_suspend_controller_test.cpp +++ b/test/unittest/src/interface_test/power_suspend_controller_test.cpp @@ -565,4 +565,34 @@ HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest022, TestSize.Lev POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest022 function end!"); } #endif + +/** + * @tc.name: PowerSuspendControllerTest023 + * @tc.desc: test simulate powerkey event + * @tc.type: FUNC + */ +HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest023, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest023 function start!"); + GTEST_LOG_(INFO) << "PowerSuspendControllerTest023: start"; + + g_service->SuspendControllerInit(); + g_service->wakeupController_ = nullptr; + auto monitor = g_service->suspendController_->monitorMap_[SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY]; + ASSERT_TRUE(monitor != nullptr); + std::shared_ptr powerkeySuspendMonitor = + std::static_pointer_cast(monitor); + + powerkeySuspendMonitor->ReceivePowerkeyCallback(nullptr); + EXPECT_TRUE(g_service->suspendController_ != nullptr); + EXPECT_TRUE(g_service->wakeupController_ == nullptr); + + g_service->WakeupControllerInit(); + powerkeySuspendMonitor->ReceivePowerkeyCallback(nullptr); + EXPECT_TRUE(g_service->suspendController_ != nullptr); + EXPECT_TRUE(g_service->wakeupController_ != nullptr); + + GTEST_LOG_(INFO) << "PowerSuspendControllerTest023: end"; + POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest023 function end!"); +} } // namespace \ No newline at end of file diff --git a/utils/native/src/power_utils.cpp b/utils/native/src/power_utils.cpp index e49231f9..24322ef9 100644 --- a/utils/native/src/power_utils.cpp +++ b/utils/native/src/power_utils.cpp @@ -115,6 +115,8 @@ const std::string PowerUtils::GetReasonTypeString(StateChangeReason type) return std::string("UNKNOWN"); case StateChangeReason::STATE_CHANGE_REASON_BLUETOOTH_INCOMING_CALL: return std::string("BLUETOOTH_INCOMING_CALL"); + case StateChangeReason::STATE_CHANGE_REASON_WAKEUP_FROM_ULSR: + return std::string("WAKEUP_ULSR"); default: break; } -- Gitee