From ca23cdc81d8427ba4ff20a8f3bfc4d8f42408aa5 Mon Sep 17 00:00:00 2001 From: volcano_wang <351030513@qq.com> Date: Thu, 17 Apr 2025 20:31:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E7=94=B5=E6=BA=90=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=A2=9E=E5=BC=BA=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/native/include/power_mgr_service.h | 3 +- services/native/src/power_mgr_service.cpp | 30 ++++++++++++++- services/native/src/power_state_machine.cpp | 2 +- utils/param/BUILD.gn | 3 ++ utils/param/include/sysparam.h | 2 + utils/param/src/sysparam.cpp | 42 ++++++++++++++++++++- 6 files changed, 78 insertions(+), 4 deletions(-) diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index 5cbdf15e..404d0797 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -290,7 +290,8 @@ private: static constexpr uint32_t HALL_SAMPLING_RATE = 100000000; static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"}; static std::atomic_bool isBootCompleted_; - + static std::atomic_bool isNeedReInit_ ; + static std::atomic_bool displayManagerServiceCrash_; static void RegisterBootCompletedCallback(); static void PowerExternalAbilityInit(); static bool IsDeveloperMode(); diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 8572c596..a55623cb 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -77,6 +77,7 @@ constexpr uint64_t VIRTUAL_SCREEN_START_ID = 1000; auto pms = DelayedSpSingleton::GetInstance(); const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr()); SysParam::BootCompletedCallback g_bootCompletedCallback; +static std::mutex powerInitMutex_; #ifdef POWER_PICKUP_ENABLE bool g_isPickUpOpen = false; #endif @@ -84,6 +85,8 @@ constexpr int32_t API18 = 18; } // namespace std::atomic_bool PowerMgrService::isBootCompleted_ = false; +std::atomic_bool PowerMgrService::isNeedReInit_ = false; +std::atomic_bool PowerMgrService::displayManagerServiceCrash_ = false; #ifdef HAS_SENSORS_SENSOR_PART bool PowerMgrService::isInLidMode_ = false; #endif @@ -156,8 +159,15 @@ bool PowerMgrService::Init() void PowerMgrService::RegisterBootCompletedCallback() { + POWER_HILOGI(COMP_SVC, "plan to RegisterBootCompletedCallback."); g_bootCompletedCallback = []() { + std::lock_guard lock(powerInitMutex_); + if (!isNeedReInit_ ) { + POWER_HILOGW(COMP_SVC, "Power initialization is not required."); + return; + } POWER_HILOGI(COMP_SVC, "BootCompletedCallback triggered"); + isNeedReInit_ = false; auto power = DelayedSpSingleton::GetInstance(); if (power == nullptr) { POWER_HILOGI(COMP_SVC, "get PowerMgrService fail"); @@ -192,7 +202,7 @@ void PowerMgrService::RegisterBootCompletedCallback() power->KeepScreenOnInit(); isBootCompleted_ = true; }; - SysParam::RegisterBootCompletedCallback(g_bootCompletedCallback); + SysParam::RegisterBootCompletedCallbackForPowerSa(g_bootCompletedCallback); } void PowerMgrService::PowerExternalAbilityInit() @@ -699,6 +709,11 @@ void PowerMgrService::OnRemoveSystemAbility(int32_t systemAbilityId, const std:: power->ResetMovementState(); } #endif + if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) { + std::lock_guard lock(powerInitMutex_); + POWER_HILOGI(COMP_SVC, "get DISPLAY_MANAGER_SERVICE_SA_ID crash in PowerService."); + displayManagerServiceCrash_ = true; + } } void PowerMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) @@ -711,8 +726,21 @@ void PowerMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::str } } if (systemAbilityId == DISPLAY_MANAGER_SERVICE_ID) { + std::lock_guard lock(powerInitMutex_); + POWER_HILOGI(COMP_SVC, "get DISPLAY_MANAGER_SERVICE_ID in PowerService"); + isNeedReInit_ = true; RegisterBootCompletedCallback(); } + + if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) { + std::lock_guard lock(powerInitMutex_); + POWER_HILOGI(COMP_SVC, "get DISPLAY_MANAGER_SERVICE_SA_ID in PowerService"); + if (displayManagerServiceCrash_) { + isNeedReInit_ = true; + RegisterBootCompletedCallback(); + displayManagerServiceCrash_ = false; + } + } #ifdef MSDP_MOVEMENT_ENABLE if (systemAbilityId == MSDP_MOVEMENT_SERVICE_ID) { auto power = DelayedSpSingleton::GetInstance(); diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index 43542725..4bbc9bef 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -266,7 +266,7 @@ void PowerStateMachine::StartSleepTimer(PowerState from) void PowerStateMachine::InitState() { - POWER_HILOGD(FEATURE_POWER_STATE, "Init power state"); + POWER_HILOGI(FEATURE_POWER_STATE, "Init power state"); if (IsScreenOn()) { #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "SCREEN_STATE", diff --git a/utils/param/BUILD.gn b/utils/param/BUILD.gn index 458eff38..7dc9a5a8 100644 --- a/utils/param/BUILD.gn +++ b/utils/param/BUILD.gn @@ -34,8 +34,11 @@ ohos_shared_library("power_sysparam") { public_configs = [ ":public_config" ] + deps = [ "../ffrt:power_ffrt" ] + external_deps = [ "c_utils:utils", + "ffrt:libffrt", "hilog:libhilog", "init:libbeget_proxy", "init:libbegetutil", diff --git a/utils/param/include/sysparam.h b/utils/param/include/sysparam.h index dec02cc6..c3f8ea48 100644 --- a/utils/param/include/sysparam.h +++ b/utils/param/include/sysparam.h @@ -27,11 +27,13 @@ class SysParam { public: typedef void (* BootCompletedCallback)(); static void RegisterBootCompletedCallback(BootCompletedCallback&); + static void RegisterBootCompletedCallbackForPowerSa(BootCompletedCallback&); static int32_t GetIntValue(const std::string& key, int32_t def); private: static constexpr const char* KEY_BOOT_COMPLETED {"bootevent.boot.completed"}; static constexpr int32_t VALUE_MAX_LEN = 32; + static void LoopReadBootCompletedParameter(BootCompletedCallback& callback); }; } // namespace PowerMgr } // namespace OHOS diff --git a/utils/param/src/sysparam.cpp b/utils/param/src/sysparam.cpp index bc601206..7cb8976b 100644 --- a/utils/param/src/sysparam.cpp +++ b/utils/param/src/sysparam.cpp @@ -15,14 +15,37 @@ #include "sysparam.h" +#include "ffrt_utils.h" #include "power_log.h" #include "string_ex.h" #include "syspara/parameter.h" +#include "syspara/parameters.h" namespace OHOS { namespace PowerMgr { + +void SysParam::LoopReadBootCompletedParameter(BootCompletedCallback& callback) +{ + ffrt::submit([callback](void) -> void { + constexpr int32_t delayTimeMs = 50; + constexpr int32_t logInterval = 100; + int32_t count = 0; + while (!system::GetBoolParameter(KEY_BOOT_COMPLETED, false)) { + count++; + if (count >= logInterval) { + POWER_HILOGW(COMP_UTILS, "bootevent not fired!"); + count = 0; + } + ffrt::this_task::sleep_for(std::chrono::milliseconds(delayTimeMs)); + } + POWER_HILOGI(COMP_UTILS, "Get booteventCompleted true success!"); + callback(); + }); +} + void SysParam::RegisterBootCompletedCallback(BootCompletedCallback& callback) { + POWER_HILOGI(COMP_UTILS, "start to RegisterBootCompletedCallback"); int32_t ret = WatchParameter( KEY_BOOT_COMPLETED, [](const char* key, const char* value, void* context) { @@ -31,11 +54,28 @@ void SysParam::RegisterBootCompletedCallback(BootCompletedCallback& callback) } }, reinterpret_cast(callback)); - if (ret < 0) { + if (ret != 0) { POWER_HILOGW(COMP_UTILS, "RegisterBootCompletedCallback failed, ret=%{public}d", ret); } } +void SysParam::RegisterBootCompletedCallbackForPowerSa(BootCompletedCallback& callback) +{ + POWER_HILOGI(COMP_UTILS, "start to RegisterBootCompletedCallback for power SA"); + int32_t ret = WatchParameter( + KEY_BOOT_COMPLETED, + [](const char* key, const char* value, void* context) { + if (strcmp(value, "true") == 0) { + ((BootCompletedCallback)context)(); + } + }, + reinterpret_cast(callback)); + if (ret != 0) { + POWER_HILOGW(COMP_UTILS, "RegisterBootCompletedCallback for power SA failed, ret=%{public}d", ret); + } + LoopReadBootCompletedParameter(callback); +} + int32_t SysParam::GetIntValue(const std::string& key, int32_t def) { char value[VALUE_MAX_LEN] = {0}; -- Gitee From 694c74ec95f2ac557c8316664085355bc782dbb9 Mon Sep 17 00:00:00 2001 From: volcano6322 Date: Mon, 19 May 2025 12:23:13 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E7=94=B5=E6=BA=90=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=A2=9E=E5=BC=BA=E7=BC=A9=E5=87=8F?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: volcano6322 --- services/native/src/power_mgr_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index a55623cb..f1811ba5 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -162,7 +162,7 @@ void PowerMgrService::RegisterBootCompletedCallback() POWER_HILOGI(COMP_SVC, "plan to RegisterBootCompletedCallback."); g_bootCompletedCallback = []() { std::lock_guard lock(powerInitMutex_); - if (!isNeedReInit_ ) { + if (!isNeedReInit_) { POWER_HILOGW(COMP_SVC, "Power initialization is not required."); return; } -- Gitee