fix: alarm

Signed-off-by: Hellen_666 <yanxuejun@h-partners.com>
This commit is contained in:
Hellen_666
2026-02-24 18:51:39 +08:00
parent eff6898a88
commit c88d58f04f
19 changed files with 114 additions and 80 deletions
+5
View File
@@ -23,6 +23,11 @@ declare_args() {
}
defines = []
if (battery_manager_feature_set_low_capacity_threshold) {
defines += [ "BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD" ]
}
if (!defined(global_parts_info) ||
defined(global_parts_info.hiviewdfx_hisysevent)) {
has_hiviewdfx_hisysevent_part = true
+10 -6
View File
@@ -120,9 +120,11 @@ public:
void Reset();
void VibratorInit();
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
void ClearLowCapacityShutdownTask();
void SubscribeHibernateCommonEvent();
void UnsubscribeHibernateCommonEvent();
void SubscribeCommonEvent();
void UnSubscribeCommonEvent();
void CreateShutdownGuard();
void LockShutdownGuard();
void UnlockShutdownGuard();
#endif
private:
bool Init();
@@ -138,6 +140,7 @@ private:
void HandleCapacity(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent);
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
void HandleCapacityExt(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent);
bool IsDelayHibernateTimerValid();
bool CheckIfCreateHibernateTask(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent);
bool CheckIfClearHibernateTask(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent);
#endif
@@ -148,9 +151,6 @@ private:
void WakeupDevice(BatteryPluggedType pluggedType);
bool IsCharging(BatteryChargeState chargeState);
bool IsInExtremePowerSaveMode();
void CreateShutdownGuard();
void LockShutdownGuard();
void UnlockShutdownGuard();
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
void SetLowCapacityThreshold();
@@ -195,6 +195,10 @@ private:
};
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
enum BatteryTimerId {
TIMER_ID_DELAY_HIBERNATE,
};
class BatteryCommonEventSubscriber : public EventFwk::CommonEventSubscriber {
public:
explicit BatteryCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
@@ -78,7 +78,7 @@ bool NotificationLocale::SaveJsonToMap(const std::string& fileStr, const std::st
cJSON_Delete(root);
return false;
}
cJSON* stringConf = cJSON_GetObjectItemCaseSensitive(root, "String");
cJSON* stringConf = cJSON_GetObjectItemCaseSensitive(root, "string");
if (!BatteryMgrJsonUtils::IsValidJsonArray(stringConf)) {
BATTERY_HILOGE(COMP_SVC, "%{public}s stringConf invalid", targetPath.c_str());
cJSON_Delete(root);
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
+1 -1
View File
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"zh-Hans-CN",
"value":"zh_CN"
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
@@ -1,5 +1,5 @@
{
"String":[
"string":[
{
"name":"",
"value":""
+1 -2
View File
@@ -71,7 +71,7 @@ const std::string REBOOT = "reboot";
const std::string SEND_COMMONEVENT = "sendcommonevent";
const std::string SEND_CUSTOMEVENT = "sendcustomevent";
const std::string SEND_POPUP = "sendpopup";
const std::string BATTERY_CUSTOM_EVENT_PREFIX = "usual.event.";
const std::string BATTERY_CUSTOM_EVENT_PREFIX = "usual.event.battery";
sptr<BatteryService> g_service = DelayedSpSingleton<BatteryService>::GetInstance();
BatteryNotify::BatteryNotify()
@@ -423,7 +423,6 @@ void StartChargingSoundFunc()
g_released.store(true);
});
}
}
#endif
+60 -41
View File
@@ -66,12 +66,12 @@ const std::string COMMON_EVENT_BATTERY_CHANGED = "usual.event.BATTERY_CHANGED";
sptr<BatteryService> g_service = DelayedSpSingleton<BatteryService>::GetInstance();
FFRTQueue g_queue("battery_service");
FFRTHandle g_lowCapacityShutdownHandle = nullptr;
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
std::atomic_bool g_isLowCapacityShutdownHandleValid = false;
#endif
BatteryPluggedType g_lastPluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE;
SysParam::BootCompletedCallback g_bootCompletedCallback;
std::shared_ptr<RunningLock> g_shutdownGuard = nullptr;
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
std::shared_ptr<FFRTTimer> g_ffrtTimer = nullptr;
#endif
}
std::atomic_bool BatteryService::isBootCompleted_ = false;
@@ -122,6 +122,11 @@ bool BatteryService::Init()
if (!batteryNotify_) {
batteryNotify_ = std::make_unique<BatteryNotify>();
}
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
if (!g_ffrtTimer) {
g_ffrtTimer = std::make_shared<FFRTTimer>("battery_manager_ffrt_queue");
}
#endif
VibratorInit();
RegisterBootCompletedCallback();
return true;
@@ -143,6 +148,9 @@ void BatteryService::OnAddSystemAbility(int32_t systemAbilityId, const std::stri
}
if (systemAbilityId == COMMON_EVENT_SERVICE_ID && !isCommonEventReady_.load()) {
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
SubscribeCommonEvent();
#endif
if (!isBatteryHdiReady_.load()) {
BATTERY_HILOGE(COMP_SVC, "battery hdi interface is not ready, return");
return;
@@ -412,6 +420,9 @@ void BatteryService::OnStop()
ModuleMgrDestroy(g_moduleMgr);
g_moduleMgr = nullptr;
}
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
UnSubscribeCommonEvent();
#endif
}
bool BatteryService::IsLastPlugged()
@@ -513,20 +524,18 @@ void BatteryService::HandleCapacityExt(int32_t capacity, BatteryChargeState char
capacity,
static_cast<uint32_t>(chargeState),
isBatteryPresent);
g_islowCapacityShutdownHandleValid = true;
g_service->SubscribeHibernateCommonEvent();
CreateShutdownGuard();
LockShutdownGuard();
FFRTTask task = [&] {
g_islowCapacityShutdownHandleValid == false;
g_service->UnsubscribeHibernateCommonEvent();
if (!IsInExtremePowerSaveMode()) {
BATTERY_HILOGI(COMP_SVC, "HandleCapacityExt begin to hibernate");
PowerMgrClient::GetInstance().Hibernate(false, "LowCapacity");
}
UnlockShutdownGuard();
};
g_lowCapacityShutdownHandle = FFRTUtils::SubmitDelayTask(task, SHUTDOWN_DELAY_TIME_MS, g_queue);
if (g_ffrtTimer) {
g_ffrtTimer->SetTimer(TIMER_ID_DELAY_HIBERNATE, task, SHUTDOWN_DELAY_TIME_MS);
}
}
if (CheckIfClearHibernateTask(capacity, chargeState, isBatteryPresent)) {
@@ -537,45 +546,48 @@ void BatteryService::HandleCapacityExt(int32_t capacity, BatteryChargeState char
static_cast<uint32_t>(chargeState),
lastBatteryInfo_.GetCapacity(),
isBatteryPresent);
UnsubscribeHibernateCommonEvent();
ClearLowCapacityShutdownTask();
if (g_ffrtTimer) {
g_ffrtTimer->CancelTimer(TIMER_ID_DELAY_HIBERNATE);
}
UnlockShutdownGuard();
}
}
bool BatteryService::IsDelayHibernateTimerValid()
{
if (g_ffrtTimer == nullptr) {
BATTERY_HILOGE(COMP_SVC, "g_ffrtTimer null error");
return false;
}
if (g_ffrtTimer->GetTaskHandlePtr(TIMER_ID_DELAY_HIBERNATE) == nullptr) {
return false;
}
return true;
}
bool BatteryService::CheckIfCreateHibernateTask(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent)
{
// Check if the battery capacity is below the shutdown threshold, no shutdown task is currently active,
// Check if the battery capacity is below the shutdown threshold, no shutdown timer is currently valid,
// and the capacity has decreased or the battery is present but not charging.
if ((capacity <= shutdownCapacityThreshold_) && (_islowCapacityShutdownHandleValid == false) &&
(capacity < lastBatteryInfo_.GetCapacity()) || (isBatteryPresent && !IsCharging(chargeState))) {
return true;
}
if ((capacity <= shutdownCapacityThreshold_) && !IsDelayHibernateTimerValid() &&
(capacity < lastBatteryInfo_.GetCapacity() || (isBatteryPresent && !IsCharging(chargeState)))) {
return true;
}
return false;
}
bool BatteryService::CheckIfClearHibernateTask(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent)
{
// Check if a Shutdown task is already actived and if the battery capacity has increased or the battery is present
// Check if the Shutdown task is valid and if the battery capacity has increased or the battery is present
// but charging.
if (g_islowCapacityShutdownHandleValid == true &&
if (IsDelayHibernateTimerValid() &&
(capacity > lastBatteryInfo_.GetCapacity() || (isBatteryPresent && IsCharging(chargeState)))) {
return true;
}
return false;
}
void BatteryService::ClearLowCapacityShutdownTask()
{
if (g_islowCapacityShutdownHandleValid == true) {
BATTERY_HILOG(COMP_SVC, "clearLowCapacityShutdownTask");
g_islowCapacityShutdownHandleValid = false;
FFRTUtils::CancelTask(g_lowCapacityShutdownHandle, g_queue);
g_lowCapacityShutdownHandle = nullptr;
UnlockShutdownGuard();
}
}
void BatteryService::SubscribeHibernateCommonEvent()
void BatteryService::SubscribeCommonEvent()
{
using namespace OHOS::EventFwk;
MatchingSkills matchingSkills;
@@ -584,7 +596,7 @@ void BatteryService::SubscribeHibernateCommonEvent()
CommonEventSubscribeInfo subscribeInfo(matchingSkills);
subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::COMMON);
if (!subscriberPtr_) {
subscriberPtr_ = std::make_shared<PowerCommonEventSubscriber>(subscribeInfo);
subscriberPtr_ = std::make_shared<BatteryCommonEventSubscriber>(subscribeInfo);
}
bool result = CommonEventManager::SubscribeCommonEvent(subscriberPtr_);
if (!result) {
@@ -592,13 +604,16 @@ void BatteryService::SubscribeHibernateCommonEvent()
}
}
void BatteryService::UnsubscribeHibernateCommonEvent()
void BatteryService::UnSubscribeCommonEvent()
{
if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr_)) {
BATTERY_HILOGE(COMP_SVC, "unsubscribe to commonevent manager failed!");
}
if (!subscriberPtr_) {
return;
}
if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr_)) {
BATTERY_HILOGE(COMP_SVC, "unsubscribe to commonevent manager failed!");
}
subscriberPtr_ = nullptr;
}
#endif
void BatteryService::CreateShutdownGuard()
{
@@ -630,6 +645,7 @@ void BatteryService::UnlockShutdownGuard()
}
g_shutdownGuard->UnLock();
}
#endif
int32_t BatteryService::GetCapacityInner()
{
@@ -991,7 +1007,6 @@ int32_t BatteryService::Dump(int32_t fd, const std::vector<std::u16string> &args
if (!Permission::IsSystem()) {
return ERR_PERMISSION_DENIED;
}
BatteryDump& batteryDump = BatteryDump::GetInstance();
if ((args.empty()) || (args[0].compare(u"-h") == 0)) {
batteryDump.DumpBatteryHelp(fd);
@@ -1084,6 +1099,9 @@ void BatteryService::Reset()
iBatteryInterface_->GetBatteryInfo(event);
ConvertingEvent(event);
HandleBatteryInfo();
#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
g_ffrtTimer.reset(); // all strong references gone, ffrtTimer will be destructed.
#endif
}
void BatteryService::VibratorInit()
@@ -1099,11 +1117,12 @@ void BatteryCommonEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEv
std::string action = data.GetWant().GetAction();
if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_ENTER_HIBERNATE ||
action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_EXIT_HIBERNATE) {
FFRTTask unSubFunc = [this] {
g_service->UnsubscribeHibernateCommonEvent();
};
FFRTUtils::SubmitTask(unSubFunc);
g_service->ClearLowCapacityShutdownTask();
if (g_ffrtTimer) {
g_ffrtTimer->CancelTimer(TIMER_ID_DELAY_HIBERNATE);
}
if (g_service) {
g_service->UnlockShutdownGuard();
}
}
}
#endif
+1 -1
View File
@@ -26,10 +26,10 @@ group("battery_fuzzertest") {
group("battery_unittest") {
testonly = true
deps = [
"unittest:test_battery_charger",
"unittest:battery_common_event_part2_test",
"unittest:battery_common_event_test",
"unittest:battery_hookmgr_test",
"unittest:test_battery_charger",
"unittest:test_battery_callback",
"unittest:test_battery_config",
"unittest:test_battery_dump",
+13 -9
View File
@@ -388,14 +388,14 @@ ohos_unittest("test_battery_stub") {
# test exceptions when Remote() return nullptr
ohos_unittest("test_battery_proxy_mock_iremoteobject") {
module_out_path = "${module_output_path}"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
module_out_path = "${module_output_path}"
sources = [
"mock/source/mock_peer_holder.cpp",
"src/battery_srv_proxy_mock_test.cpp",
@@ -431,14 +431,14 @@ ohos_unittest("test_battery_proxy_mock_iremoteobject") {
# test exceptions when MessageParcel::WriteInterfaceToken() return false
ohos_unittest("test_battery_proxy_mock_messageparcel_write_error") {
module_out_path = "${module_output_path}"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
module_out_path = "${module_output_path}"
sources = [
"mock/source/mock_message_parcel.cpp",
"src/battery_srv_proxy_mock_test.cpp",
@@ -474,15 +474,15 @@ ohos_unittest("test_battery_proxy_mock_messageparcel_write_error") {
# test exceptions when SendRequest() return value not equal ERR_OK
ohos_unittest("test_battery_proxy_mock_sendrequest_error") {
module_out_path = "${module_output_path}"
defines += [ "ENABLE_REMOTE_INTERFACE" ]
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
module_out_path = "${module_output_path}"
defines += [ "ENABLE_REMOTE_INTERFACE" ]
sources = [
"mock/source/mock_remote_object.cpp",
"src/battery_srv_proxy_mock_test.cpp",
@@ -518,14 +518,14 @@ ohos_unittest("test_battery_proxy_mock_sendrequest_error") {
# test exceptions when read reply MessageParcel error
ohos_unittest("test_battery_proxy_mock_parcel_read_error") {
module_out_path = "${module_output_path}"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
module_out_path = "${module_output_path}"
sources = [
"mock/source/mock_parcel.cpp",
"src/battery_srv_proxy_mock_test.cpp",
@@ -758,6 +758,10 @@ ohos_unittest("battery_common_event_part2_test") {
if (battery_manager_feature_support_notification) {
defines += [ "BATTERY_SUPPORT_NOTIFICATION" ]
}
if (battery_manager_feature_enable_charging_sound) {
defines += [ "BATTERY_MANAGER_ENABLE_CHARGING_SOUND" ]
}
}
ohos_unittest("battery_hookmgr_test") {
@@ -64,6 +64,14 @@ bool BatteryNotify::IsCommonEventServiceAbilityExist() const
return true;
}
#ifdef BATTERY_MANAGER_ENABLE_CHARGING_SOUND
void BatteryNotify::TriggerChargingSound(bool isStart) const
{
BATTERY_HILOGI(LABEL_TEST, "TriggerChargingSound %{public}s", isStart ? "start" : "stop");
}
#endif
class BatteryEventTestPart2 : public testing::Test {
public:
static void SetUpTestCase();
@@ -544,7 +544,7 @@ HWTEST_F(BatteryConfigTest, BatteryConfig0021, TestSize.Level1)
BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0021 function start!");
std::string jsonStr = R"({"notification": null})";
ASSERT_TRUE(ParseJsonStr(jsonStr, true));
g_configTest.popupConfig_.clear();
g_configTest.notificationConfMap_.clear();
g_configTest.ParseNotificationConf();
EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
DestroyJsonValue(g_configTest.config_);
@@ -582,7 +582,7 @@ HWTEST_F(BatteryConfigTest, BatteryConfig0022, TestSize.Level1)
BATTERY_HILOGI(LABEL_TEST, "BatteryConfig0022 function start!");
std::string jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": 4}]})";
ASSERT_TRUE(ParseJsonStr(jsonStr, true));
g_configTest.popupConfig_.clear();
g_configTest.notificationConfMap_.clear();
g_configTest.ParseNotificationConf();
EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
DestroyJsonValue(g_configTest.config_);
@@ -632,7 +632,7 @@ HWTEST_F(BatteryConfigTest, BatteryConfig0023, TestSize.Level1)
std::string jsonStr = R"({"notification": [{"name": "1", "icon": "2", "title": "3", "text": "4",
"button": [{"name": "1", "action": 1}, {}]}]})";
ASSERT_TRUE(ParseJsonStr(jsonStr, true));
g_configTest.popupConfig_.clear();
g_configTest.notificationConfMap_.clear();
g_configTest.ParseNotificationConf();
EXPECT_TRUE(g_configTest.notificationConfMap_.size() == 0);
DestroyJsonValue(g_configTest.config_);
@@ -62,8 +62,8 @@ void BatteryNotificationTest::TearDown()
HWTEST_F(BatteryNotificationTest, BatteryNotification001, TestSize.Level1)
{
BATTERY_HILOGI(LABEL_TEST, "BatteryNotification001 function start!");
g_batteryInfo->SetUevent("battery_notification_test001");
EXPECT_EQ(g_batteryInfo->GetUevent(), "battery_notification_test001");
g_batteryServiceInfo->SetUevent("battery_notification_test001");
EXPECT_EQ(g_batteryServiceInfo->GetUevent(), "battery_notification_test001");
std::string popupName = "BatteryNotification001_popup";
bool ret = g_batteryServiceNotify->HandleNotification(popupName);
BATTERY_HILOGI(LABEL_TEST, "HandleNotification ret[%d]", static_cast<int32_t>(ret));
+2 -7
View File
@@ -19,16 +19,11 @@
namespace OHOS {
namespace PowerMgr {
static const char* BATTERY_HOOKMGR_NAME = "battery_hookmgr";
static HOOK_MGR* g_batteryHookMgr = nullptr;
HOOK_MGR* GetBatteryHookMgr()
{
if (g_batteryHookMgr != nullptr) {
return g_batteryHookMgr;
}
g_batteryHookMgr = HookMgrCreate(BATTERY_HOOKMGR_NAME);
return g_batteryHookMgr;
static HOOK_MGR* batteryHookMgr = HookMgrCreate(BATTERY_HOOKMGR_NAME);
return batteryHookMgr;
}
} // namespace PowerMgr
} // namespace OHOS