1f16e0440Sopenharmony_ci/* 2f16e0440Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3f16e0440Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f16e0440Sopenharmony_ci * you may not use this file except in compliance with the License. 5f16e0440Sopenharmony_ci * You may obtain a copy of the License at 6f16e0440Sopenharmony_ci * 7f16e0440Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f16e0440Sopenharmony_ci * 9f16e0440Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f16e0440Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f16e0440Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f16e0440Sopenharmony_ci * See the License for the specific language governing permissions and 13f16e0440Sopenharmony_ci * limitations under the License. 14f16e0440Sopenharmony_ci */ 15f16e0440Sopenharmony_ci 16f16e0440Sopenharmony_ci#include "battery_service.h" 17f16e0440Sopenharmony_ci 18f16e0440Sopenharmony_ci#include <cstdio> 19f16e0440Sopenharmony_ci#include <ctime> 20f16e0440Sopenharmony_ci#include <functional> 21f16e0440Sopenharmony_ci#include <new> 22f16e0440Sopenharmony_ci 23f16e0440Sopenharmony_ci#include "ability_manager_client.h" 24f16e0440Sopenharmony_ci#include "errors.h" 25f16e0440Sopenharmony_ci#include "hdf_device_class.h" 26f16e0440Sopenharmony_ci#include "hdf_service_status.h" 27f16e0440Sopenharmony_ci#include "ipc_skeleton.h" 28f16e0440Sopenharmony_ci#include "iremote_object.h" 29f16e0440Sopenharmony_ci#include "permission.h" 30f16e0440Sopenharmony_ci#include "power_common.h" 31f16e0440Sopenharmony_ci#include "power_mgr_client.h" 32f16e0440Sopenharmony_ci#include "ffrt_utils.h" 33f16e0440Sopenharmony_ci#include "sysparam.h" 34f16e0440Sopenharmony_ci#include "system_ability_definition.h" 35f16e0440Sopenharmony_ci#include "xcollie/watchdog.h" 36f16e0440Sopenharmony_ci 37f16e0440Sopenharmony_ci#include "battery_callback.h" 38f16e0440Sopenharmony_ci#include "battery_config.h" 39f16e0440Sopenharmony_ci#include "battery_dump.h" 40f16e0440Sopenharmony_ci#include "battery_log.h" 41f16e0440Sopenharmony_ci#include "power_vibrator.h" 42f16e0440Sopenharmony_ci#include "v2_0/ibattery_callback.h" 43f16e0440Sopenharmony_ci 44f16e0440Sopenharmony_ciusing namespace OHOS::HDI::Battery; 45f16e0440Sopenharmony_ciusing namespace OHOS::AAFwk; 46f16e0440Sopenharmony_ci 47f16e0440Sopenharmony_cinamespace OHOS { 48f16e0440Sopenharmony_cinamespace PowerMgr { 49f16e0440Sopenharmony_cinamespace { 50f16e0440Sopenharmony_ciconstexpr const char* BATTERY_HDI_NAME = "battery_interface_service"; 51f16e0440Sopenharmony_ciconstexpr int32_t BATTERY_FULL_CAPACITY = 100; 52f16e0440Sopenharmony_ciconstexpr uint32_t RETRY_TIME = 1000; 53f16e0440Sopenharmony_ciconstexpr uint32_t SHUTDOWN_DELAY_TIME_MS = 60000; 54f16e0440Sopenharmony_ciconst std::string BATTERY_VIBRATOR_CONFIG_FILE = "etc/battery/battery_vibrator.json"; 55f16e0440Sopenharmony_ciconst std::string VENDOR_BATTERY_VIBRATOR_CONFIG_FILE = "/vendor/etc/battery/battery_vibrator.json"; 56f16e0440Sopenharmony_ciconst std::string SYSTEM_BATTERY_VIBRATOR_CONFIG_FILE = "/system/etc/battery/battery_vibrator.json"; 57f16e0440Sopenharmony_ciconst std::string COMMON_EVENT_BATTERY_CHANGED = "usual.event.BATTERY_CHANGED"; 58f16e0440Sopenharmony_cisptr<BatteryService> g_service = DelayedSpSingleton<BatteryService>::GetInstance(); 59f16e0440Sopenharmony_ciFFRTQueue g_queue("battery_service"); 60f16e0440Sopenharmony_ciFFRTHandle g_lowCapacityShutdownHandle = nullptr; 61f16e0440Sopenharmony_ciBatteryPluggedType g_lastPluggedType = BatteryPluggedType::PLUGGED_TYPE_NONE; 62f16e0440Sopenharmony_ciSysParam::BootCompletedCallback g_bootCompletedCallback; 63f16e0440Sopenharmony_ci} 64f16e0440Sopenharmony_cistd::atomic_bool BatteryService::isBootCompleted_ = false; 65f16e0440Sopenharmony_ci 66f16e0440Sopenharmony_ciconst bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility( 67f16e0440Sopenharmony_ci DelayedSpSingleton<BatteryService>::GetInstance().GetRefPtr()); 68f16e0440Sopenharmony_ci 69f16e0440Sopenharmony_ciBatteryService::BatteryService() 70f16e0440Sopenharmony_ci : SystemAbility(POWER_MANAGER_BATT_SERVICE_ID, true) 71f16e0440Sopenharmony_ci{ 72f16e0440Sopenharmony_ci} 73f16e0440Sopenharmony_ci 74f16e0440Sopenharmony_ciBatteryService::~BatteryService() {} 75f16e0440Sopenharmony_ci 76f16e0440Sopenharmony_cistatic int64_t GetCurrentTime() 77f16e0440Sopenharmony_ci{ 78f16e0440Sopenharmony_ci constexpr int32_t SEC_TO_MSEC = 1000; 79f16e0440Sopenharmony_ci constexpr int32_t NSEC_TO_MSEC = 1000000; 80f16e0440Sopenharmony_ci timespec tm {}; 81f16e0440Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &tm); 82f16e0440Sopenharmony_ci 83f16e0440Sopenharmony_ci return tm.tv_sec * SEC_TO_MSEC + (tm.tv_nsec / NSEC_TO_MSEC); 84f16e0440Sopenharmony_ci} 85f16e0440Sopenharmony_ci 86f16e0440Sopenharmony_civoid BatteryService::OnStart() 87f16e0440Sopenharmony_ci{ 88f16e0440Sopenharmony_ci if (ready_) { 89f16e0440Sopenharmony_ci BATTERY_HILOGD(COMP_SVC, "Service is ready, nothing to do"); 90f16e0440Sopenharmony_ci return; 91f16e0440Sopenharmony_ci } 92f16e0440Sopenharmony_ci if (!(Init())) { 93f16e0440Sopenharmony_ci BATTERY_HILOGE(COMP_SVC, "Call init failed"); 94f16e0440Sopenharmony_ci return; 95f16e0440Sopenharmony_ci } 96f16e0440Sopenharmony_ci RegisterHdiStatusListener(); 97f16e0440Sopenharmony_ci if (!Publish(this)) { 98f16e0440Sopenharmony_ci BATTERY_HILOGE(COMP_SVC, "Register to system ability manager failed"); 99f16e0440Sopenharmony_ci return; 100f16e0440Sopenharmony_ci } 101f16e0440Sopenharmony_ci AddSystemAbilityListener(MISCDEVICE_SERVICE_ABILITY_ID); 102f16e0440Sopenharmony_ci ready_ = true; 103f16e0440Sopenharmony_ci} 104f16e0440Sopenharmony_ci 105f16e0440Sopenharmony_cibool BatteryService::Init() 106f16e0440Sopenharmony_ci{ 107f16e0440Sopenharmony_ci InitConfig(); 108f16e0440Sopenharmony_ci if (!batteryNotify_) { 109f16e0440Sopenharmony_ci batteryNotify_ = std::make_unique<BatteryNotify>(); 110f16e0440Sopenharmony_ci } 111f16e0440Sopenharmony_ci VibratorInit(); 112f16e0440Sopenharmony_ci RegisterBootCompletedCallback(); 113f16e0440Sopenharmony_ci return true; 114f16e0440Sopenharmony_ci} 115f16e0440Sopenharmony_ci 116f16e0440Sopenharmony_civoid BatteryService::RegisterBootCompletedCallback() 117f16e0440Sopenharmony_ci{ 118f16e0440Sopenharmony_ci g_bootCompletedCallback = []() { 119f16e0440Sopenharmony_ci isBootCompleted_ = true; 120f16e0440Sopenharmony_ci }; 121f16e0440Sopenharmony_ci SysParam::RegisterBootCompletedCallback(g_bootCompletedCallback); 122f16e0440Sopenharmony_ci} 123f16e0440Sopenharmony_ci 124f16e0440Sopenharmony_civoid BatteryService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) 125f16e0440Sopenharmony_ci{ 126f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "systemAbilityId=%{public}d, deviceId=%{private}s", systemAbilityId, deviceId.c_str()); 127f16e0440Sopenharmony_ci if (systemAbilityId == MISCDEVICE_SERVICE_ABILITY_ID) { 128f16e0440Sopenharmony_ci batteryLight_.InitLight(); 129f16e0440Sopenharmony_ci } 130f16e0440Sopenharmony_ci} 131f16e0440Sopenharmony_ci 132f16e0440Sopenharmony_cibool BatteryService::RegisterBatteryHdiCallback() 133f16e0440Sopenharmony_ci{ 134f16e0440Sopenharmony_ci std::lock_guard<std::shared_mutex> lock(mutex_); 135f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 136f16e0440Sopenharmony_ci iBatteryInterface_ = V2_0::IBatteryInterface::Get(); 137f16e0440Sopenharmony_ci BATTERY_HILOGE(COMP_SVC, "failed to get battery hdi interface"); 138f16e0440Sopenharmony_ci RETURN_IF_WITH_RET(iBatteryInterface_ == nullptr, false); 139f16e0440Sopenharmony_ci } 140f16e0440Sopenharmony_ci sptr<V2_0::IBatteryCallback> callback = new BatteryCallback(); 141f16e0440Sopenharmony_ci ErrCode ret = iBatteryInterface_->Register(callback); 142f16e0440Sopenharmony_ci if (ret < 0) { 143f16e0440Sopenharmony_ci BATTERY_HILOGE(COMP_SVC, "register callback failed"); 144f16e0440Sopenharmony_ci return false; 145f16e0440Sopenharmony_ci } 146f16e0440Sopenharmony_ci 147f16e0440Sopenharmony_ci BatteryCallback::BatteryEventCallback eventCb = 148f16e0440Sopenharmony_ci [this](const V2_0::BatteryInfo& event) -> int32_t { return this->HandleBatteryCallbackEvent(event); }; 149f16e0440Sopenharmony_ci BatteryCallback::RegisterBatteryEvent(eventCb); 150f16e0440Sopenharmony_ci return true; 151f16e0440Sopenharmony_ci} 152f16e0440Sopenharmony_ci 153f16e0440Sopenharmony_civoid BatteryService::InitConfig() 154f16e0440Sopenharmony_ci{ 155f16e0440Sopenharmony_ci auto& batteryConfig = BatteryConfig::GetInstance(); 156f16e0440Sopenharmony_ci#ifdef HAS_BATTERY_CONFIG_POLICY_PART 157f16e0440Sopenharmony_ci batteryConfig.ParseConfig(); 158f16e0440Sopenharmony_ci#endif 159f16e0440Sopenharmony_ci 160f16e0440Sopenharmony_ci warnCapacity_ = batteryConfig.GetInt("soc.warning", warnCapacity_); 161f16e0440Sopenharmony_ci highTemperature_ = batteryConfig.GetInt("temperature.high", highTemperature_); 162f16e0440Sopenharmony_ci lowTemperature_ = batteryConfig.GetInt("temperature.low", lowTemperature_); 163f16e0440Sopenharmony_ci shutdownCapacityThreshold_ = batteryConfig.GetInt("soc.shutdown", shutdownCapacityThreshold_); 164f16e0440Sopenharmony_ci criticalCapacityThreshold_ = batteryConfig.GetInt("soc.critical", criticalCapacityThreshold_); 165f16e0440Sopenharmony_ci warningCapacityThreshold_ = batteryConfig.GetInt("soc.warning", warningCapacityThreshold_); 166f16e0440Sopenharmony_ci lowCapacityThreshold_ = batteryConfig.GetInt("soc.low", lowCapacityThreshold_); 167f16e0440Sopenharmony_ci normalCapacityThreshold_ = batteryConfig.GetInt("soc.normal", normalCapacityThreshold_); 168f16e0440Sopenharmony_ci highCapacityThreshold_ = batteryConfig.GetInt("soc.high", highCapacityThreshold_); 169f16e0440Sopenharmony_ci fullCapacityThreshold_ = batteryConfig.GetInt("soc.full", fullCapacityThreshold_); 170f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "warnCapacity_=%{public}d, highTemperature_=%{public}d,\ 171f16e0440Sopenharmony_ci lowTemperature_=%{public}d, shutdownCapacityThreshold_=%{public}d,\ 172f16e0440Sopenharmony_ci criticalCapacityThreshold_=%{public}d, warningCapacityThreshold_=%{public}d, lowCapacityThreshold_=%{public}d,\ 173f16e0440Sopenharmony_ci normalCapacityThreshold_=%{public}d, highCapacityThreshold_=%{public}d, fullCapacityThreshold_=%{public}d", 174f16e0440Sopenharmony_ci warnCapacity_, highTemperature_, lowTemperature_, shutdownCapacityThreshold_, criticalCapacityThreshold_, 175f16e0440Sopenharmony_ci warningCapacityThreshold_, lowCapacityThreshold_, normalCapacityThreshold_, highCapacityThreshold_, 176f16e0440Sopenharmony_ci fullCapacityThreshold_); 177f16e0440Sopenharmony_ci} 178f16e0440Sopenharmony_ci 179f16e0440Sopenharmony_ciint32_t BatteryService::HandleBatteryCallbackEvent(const V2_0::BatteryInfo& event) 180f16e0440Sopenharmony_ci{ 181f16e0440Sopenharmony_ci if (isMockUnplugged_ || isMockCapacity_ || isMockUevent_) { 182f16e0440Sopenharmony_ci return ERR_OK; 183f16e0440Sopenharmony_ci } 184f16e0440Sopenharmony_ci 185f16e0440Sopenharmony_ci ConvertingEvent(event); 186f16e0440Sopenharmony_ci RETURN_IF_WITH_RET(lastBatteryInfo_ == batteryInfo_, ERR_OK); 187f16e0440Sopenharmony_ci HandleBatteryInfo(); 188f16e0440Sopenharmony_ci return ERR_OK; 189f16e0440Sopenharmony_ci} 190f16e0440Sopenharmony_ci 191f16e0440Sopenharmony_civoid BatteryService::ConvertingEvent(const V2_0::BatteryInfo& event) 192f16e0440Sopenharmony_ci{ 193f16e0440Sopenharmony_ci if (!isMockCapacity_) { 194f16e0440Sopenharmony_ci batteryInfo_.SetCapacity(event.capacity); 195f16e0440Sopenharmony_ci } 196f16e0440Sopenharmony_ci if (!isMockUnplugged_) { 197f16e0440Sopenharmony_ci batteryInfo_.SetPluggedType(BatteryPluggedType(event.pluggedType)); 198f16e0440Sopenharmony_ci batteryInfo_.SetPluggedMaxCurrent(event.pluggedMaxCurrent); 199f16e0440Sopenharmony_ci batteryInfo_.SetPluggedMaxVoltage(event.pluggedMaxVoltage); 200f16e0440Sopenharmony_ci batteryInfo_.SetChargeState(BatteryChargeState(event.chargeState)); 201f16e0440Sopenharmony_ci } 202f16e0440Sopenharmony_ci batteryInfo_.SetVoltage(event.voltage); 203f16e0440Sopenharmony_ci batteryInfo_.SetTemperature(event.temperature); 204f16e0440Sopenharmony_ci batteryInfo_.SetHealthState(BatteryHealthState(event.healthState)); 205f16e0440Sopenharmony_ci batteryInfo_.SetChargeCounter(event.chargeCounter); 206f16e0440Sopenharmony_ci batteryInfo_.SetTotalEnergy(event.totalEnergy); 207f16e0440Sopenharmony_ci batteryInfo_.SetCurAverage(event.curAverage); 208f16e0440Sopenharmony_ci batteryInfo_.SetRemainEnergy(event.remainEnergy); 209f16e0440Sopenharmony_ci batteryInfo_.SetPresent(event.present); 210f16e0440Sopenharmony_ci batteryInfo_.SetTechnology(event.technology); 211f16e0440Sopenharmony_ci batteryInfo_.SetNowCurrent(event.curNow); 212f16e0440Sopenharmony_ci batteryInfo_.SetChargeType(GetChargeType()); 213f16e0440Sopenharmony_ci if (!isMockUevent_) { 214f16e0440Sopenharmony_ci batteryInfo_.SetUevent(event.uevent); 215f16e0440Sopenharmony_ci } 216f16e0440Sopenharmony_ci} 217f16e0440Sopenharmony_ci 218f16e0440Sopenharmony_civoid BatteryService::InitBatteryInfo() 219f16e0440Sopenharmony_ci{ 220f16e0440Sopenharmony_ci batteryInfo_.SetCapacity(GetCapacity()); 221f16e0440Sopenharmony_ci batteryInfo_.SetPluggedType(GetPluggedType()); 222f16e0440Sopenharmony_ci batteryInfo_.SetChargeState(GetChargingStatus()); 223f16e0440Sopenharmony_ci batteryInfo_.SetVoltage(GetVoltage()); 224f16e0440Sopenharmony_ci batteryInfo_.SetTemperature(GetBatteryTemperature()); 225f16e0440Sopenharmony_ci batteryInfo_.SetHealthState(GetHealthStatus()); 226f16e0440Sopenharmony_ci batteryInfo_.SetTotalEnergy(GetTotalEnergy()); 227f16e0440Sopenharmony_ci batteryInfo_.SetCurAverage(GetCurrentAverage()); 228f16e0440Sopenharmony_ci batteryInfo_.SetRemainEnergy(GetRemainEnergy()); 229f16e0440Sopenharmony_ci batteryInfo_.SetPresent(GetPresent()); 230f16e0440Sopenharmony_ci batteryInfo_.SetTechnology(GetTechnology()); 231f16e0440Sopenharmony_ci batteryInfo_.SetNowCurrent(GetNowCurrent()); 232f16e0440Sopenharmony_ci batteryInfo_.SetChargeType(GetChargeType()); 233f16e0440Sopenharmony_ci AddBootCommonEvents(); 234f16e0440Sopenharmony_ci HandleBatteryInfo(); 235f16e0440Sopenharmony_ci} 236f16e0440Sopenharmony_ci 237f16e0440Sopenharmony_civoid BatteryService::AddBootCommonEvents() 238f16e0440Sopenharmony_ci{ 239f16e0440Sopenharmony_ci std::string ueventName; 240f16e0440Sopenharmony_ci std::string commonEventName = COMMON_EVENT_BATTERY_CHANGED; 241f16e0440Sopenharmony_ci if (FillCommonEvent(ueventName, commonEventName)) { 242f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "need boot broadcast %{public}s", commonEventName.c_str()); 243f16e0440Sopenharmony_ci // Splicing strings for parsing uevent 244f16e0440Sopenharmony_ci batteryInfo_.SetUevent(ueventName + "$sendcommonevent"); 245f16e0440Sopenharmony_ci } 246f16e0440Sopenharmony_ci 247f16e0440Sopenharmony_ci if (commonEventName != COMMON_EVENT_BATTERY_CHANGED) { 248f16e0440Sopenharmony_ci batteryInfo_.SetUevent(ueventName); 249f16e0440Sopenharmony_ci batteryNotify_->PublishCustomEvent(batteryInfo_, commonEventName); 250f16e0440Sopenharmony_ci batteryInfo_.SetUevent(""); 251f16e0440Sopenharmony_ci } 252f16e0440Sopenharmony_ci} 253f16e0440Sopenharmony_ci 254f16e0440Sopenharmony_cibool BatteryService::FillCommonEvent(std::string& ueventName, std::string& commonEventName) 255f16e0440Sopenharmony_ci{ 256f16e0440Sopenharmony_ci const auto& commonEventConf = BatteryConfig::GetInstance().GetCommonEventConf(); 257f16e0440Sopenharmony_ci if (commonEventConf.empty()) { 258f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "don't need send common event, config is empty!"); 259f16e0440Sopenharmony_ci return false; 260f16e0440Sopenharmony_ci } 261f16e0440Sopenharmony_ci for (const auto& iter : commonEventConf) { 262f16e0440Sopenharmony_ci commonEventName = iter.eventName; 263f16e0440Sopenharmony_ci ueventName = iter.uevent; 264f16e0440Sopenharmony_ci std::string result; 265f16e0440Sopenharmony_ci BatteryError error = GetBatteryConfig(iter.sceneConfigName, result); 266f16e0440Sopenharmony_ci if (error != BatteryError::ERR_OK) { 267f16e0440Sopenharmony_ci continue; 268f16e0440Sopenharmony_ci } 269f16e0440Sopenharmony_ci bool isEqual = iter.sceneConfigEqual; 270f16e0440Sopenharmony_ci std::string configValue = iter.sceneConfigValue; 271f16e0440Sopenharmony_ci ueventName += result; 272f16e0440Sopenharmony_ci if ((isEqual && result == configValue) || (!isEqual && result != configValue)) { 273f16e0440Sopenharmony_ci return true; 274f16e0440Sopenharmony_ci } 275f16e0440Sopenharmony_ci } 276f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "don't need send common event"); 277f16e0440Sopenharmony_ci return false; 278f16e0440Sopenharmony_ci} 279f16e0440Sopenharmony_ci 280f16e0440Sopenharmony_civoid BatteryService::HandleBatteryInfo() 281f16e0440Sopenharmony_ci{ 282f16e0440Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_INFO, "capacity=%{public}d, voltage=%{public}d, temperature=%{public}d, " 283f16e0440Sopenharmony_ci "healthState=%{public}d, pluggedType=%{public}d, pluggedMaxCurrent=%{public}d, " 284f16e0440Sopenharmony_ci "pluggedMaxVoltage=%{public}d, chargeState=%{public}d, chargeCounter=%{public}d, present=%{public}d, " 285f16e0440Sopenharmony_ci "technology=%{public}s, currNow=%{public}d, totalEnergy=%{public}d, curAverage=%{public}d, " 286f16e0440Sopenharmony_ci "remainEnergy=%{public}d, chargeType=%{public}d, event=%{public}s", batteryInfo_.GetCapacity(), 287f16e0440Sopenharmony_ci batteryInfo_.GetVoltage(), batteryInfo_.GetTemperature(), batteryInfo_.GetHealthState(), 288f16e0440Sopenharmony_ci batteryInfo_.GetPluggedType(), batteryInfo_.GetPluggedMaxCurrent(), batteryInfo_.GetPluggedMaxVoltage(), 289f16e0440Sopenharmony_ci batteryInfo_.GetChargeState(), batteryInfo_.GetChargeCounter(), batteryInfo_.IsPresent(), 290f16e0440Sopenharmony_ci batteryInfo_.GetTechnology().c_str(), batteryInfo_.GetNowCurrent(), batteryInfo_.GetTotalEnergy(), 291f16e0440Sopenharmony_ci batteryInfo_.GetCurAverage(), batteryInfo_.GetRemainEnergy(), batteryInfo_.GetChargeType(), 292f16e0440Sopenharmony_ci batteryInfo_.GetUevent().c_str()); 293f16e0440Sopenharmony_ci 294f16e0440Sopenharmony_ci batteryLight_.UpdateColor(batteryInfo_.GetChargeState(), batteryInfo_.GetCapacity()); 295f16e0440Sopenharmony_ci WakeupDevice(batteryInfo_.GetPluggedType()); 296f16e0440Sopenharmony_ci CalculateRemainingChargeTime(batteryInfo_.GetCapacity(), batteryInfo_.GetChargeState()); 297f16e0440Sopenharmony_ci 298f16e0440Sopenharmony_ci batteryNotify_->PublishEvents(batteryInfo_); 299f16e0440Sopenharmony_ci HandleTemperature(batteryInfo_.GetTemperature()); 300f16e0440Sopenharmony_ci HandleCapacity(batteryInfo_.GetCapacity(), batteryInfo_.GetChargeState(), batteryInfo_.IsPresent()); 301f16e0440Sopenharmony_ci lastBatteryInfo_ = batteryInfo_; 302f16e0440Sopenharmony_ci} 303f16e0440Sopenharmony_ci 304f16e0440Sopenharmony_cibool BatteryService::RegisterHdiStatusListener() 305f16e0440Sopenharmony_ci{ 306f16e0440Sopenharmony_ci hdiServiceMgr_ = OHOS::HDI::ServiceManager::V1_0::IServiceManager::Get(); 307f16e0440Sopenharmony_ci if (hdiServiceMgr_ == nullptr) { 308f16e0440Sopenharmony_ci BATTERY_HILOGW(COMP_SVC, "hdi service manager is nullptr, Try again after %{public}u second", RETRY_TIME); 309f16e0440Sopenharmony_ci FFRTTask retryTask = [this] { 310f16e0440Sopenharmony_ci return RegisterHdiStatusListener(); 311f16e0440Sopenharmony_ci }; 312f16e0440Sopenharmony_ci FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue); 313f16e0440Sopenharmony_ci return false; 314f16e0440Sopenharmony_ci } 315f16e0440Sopenharmony_ci 316f16e0440Sopenharmony_ci hdiServStatListener_ = new HdiServiceStatusListener(HdiServiceStatusListener::StatusCallback( 317f16e0440Sopenharmony_ci [this](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus &status) { 318f16e0440Sopenharmony_ci RETURN_IF(status.serviceName != BATTERY_HDI_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT); 319f16e0440Sopenharmony_ci 320f16e0440Sopenharmony_ci std::lock_guard<std::shared_mutex> lock(mutex_); 321f16e0440Sopenharmony_ci if (status.status == SERVIE_STATUS_START) { 322f16e0440Sopenharmony_ci FFRTTask task = [this] { 323f16e0440Sopenharmony_ci (void)RegisterBatteryHdiCallback(); 324f16e0440Sopenharmony_ci#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD 325f16e0440Sopenharmony_ci SetLowCapacityThreshold(); 326f16e0440Sopenharmony_ci#endif 327f16e0440Sopenharmony_ci InitBatteryInfo(); 328f16e0440Sopenharmony_ci return; 329f16e0440Sopenharmony_ci }; 330f16e0440Sopenharmony_ci FFRTUtils::SubmitTask(task); 331f16e0440Sopenharmony_ci BATTERY_HILOGD(COMP_SVC, "battery interface service start"); 332f16e0440Sopenharmony_ci } else if (status.status == SERVIE_STATUS_STOP && iBatteryInterface_) { 333f16e0440Sopenharmony_ci iBatteryInterface_->UnRegister(); 334f16e0440Sopenharmony_ci iBatteryInterface_ = nullptr; 335f16e0440Sopenharmony_ci BATTERY_HILOGW(COMP_SVC, "battery interface service stop, unregister interface"); 336f16e0440Sopenharmony_ci } 337f16e0440Sopenharmony_ci } 338f16e0440Sopenharmony_ci )); 339f16e0440Sopenharmony_ci 340f16e0440Sopenharmony_ci int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT); 341f16e0440Sopenharmony_ci if (status != ERR_OK) { 342f16e0440Sopenharmony_ci BATTERY_HILOGW(COMP_SVC, "Register hdi failed, Try again after %{public}u second", RETRY_TIME); 343f16e0440Sopenharmony_ci FFRTTask retryTask = [this] { 344f16e0440Sopenharmony_ci return RegisterHdiStatusListener(); 345f16e0440Sopenharmony_ci }; 346f16e0440Sopenharmony_ci FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue); 347f16e0440Sopenharmony_ci return false; 348f16e0440Sopenharmony_ci } 349f16e0440Sopenharmony_ci return true; 350f16e0440Sopenharmony_ci} 351f16e0440Sopenharmony_ci 352f16e0440Sopenharmony_civoid BatteryService::OnStop() 353f16e0440Sopenharmony_ci{ 354f16e0440Sopenharmony_ci if (!ready_) { 355f16e0440Sopenharmony_ci return; 356f16e0440Sopenharmony_ci } 357f16e0440Sopenharmony_ci ready_ = false; 358f16e0440Sopenharmony_ci isBootCompleted_ = false; 359f16e0440Sopenharmony_ci 360f16e0440Sopenharmony_ci std::lock_guard<std::shared_mutex> lock(mutex_); 361f16e0440Sopenharmony_ci if (iBatteryInterface_ != nullptr) { 362f16e0440Sopenharmony_ci iBatteryInterface_->UnRegister(); 363f16e0440Sopenharmony_ci iBatteryInterface_ = nullptr; 364f16e0440Sopenharmony_ci } 365f16e0440Sopenharmony_ci if (hdiServiceMgr_ != nullptr) { 366f16e0440Sopenharmony_ci hdiServiceMgr_->UnregisterServiceStatusListener(hdiServStatListener_); 367f16e0440Sopenharmony_ci hdiServiceMgr_ = nullptr; 368f16e0440Sopenharmony_ci } 369f16e0440Sopenharmony_ci} 370f16e0440Sopenharmony_ci 371f16e0440Sopenharmony_cibool BatteryService::IsLastPlugged() 372f16e0440Sopenharmony_ci{ 373f16e0440Sopenharmony_ci if (g_lastPluggedType != BatteryPluggedType::PLUGGED_TYPE_NONE && 374f16e0440Sopenharmony_ci g_lastPluggedType != BatteryPluggedType::PLUGGED_TYPE_BUTT) { 375f16e0440Sopenharmony_ci return true; 376f16e0440Sopenharmony_ci } 377f16e0440Sopenharmony_ci return false; 378f16e0440Sopenharmony_ci} 379f16e0440Sopenharmony_ci 380f16e0440Sopenharmony_cibool BatteryService::IsNowPlugged(BatteryPluggedType pluggedType) 381f16e0440Sopenharmony_ci{ 382f16e0440Sopenharmony_ci if (pluggedType != BatteryPluggedType::PLUGGED_TYPE_NONE && 383f16e0440Sopenharmony_ci pluggedType != BatteryPluggedType::PLUGGED_TYPE_BUTT) { 384f16e0440Sopenharmony_ci return true; 385f16e0440Sopenharmony_ci } 386f16e0440Sopenharmony_ci return false; 387f16e0440Sopenharmony_ci} 388f16e0440Sopenharmony_ci 389f16e0440Sopenharmony_cibool BatteryService::IsPlugged(BatteryPluggedType pluggedType) 390f16e0440Sopenharmony_ci{ 391f16e0440Sopenharmony_ci if (!IsLastPlugged() && IsNowPlugged(pluggedType)) { 392f16e0440Sopenharmony_ci return true; 393f16e0440Sopenharmony_ci } 394f16e0440Sopenharmony_ci return false; 395f16e0440Sopenharmony_ci} 396f16e0440Sopenharmony_ci 397f16e0440Sopenharmony_cibool BatteryService::IsUnplugged(BatteryPluggedType pluggedType) 398f16e0440Sopenharmony_ci{ 399f16e0440Sopenharmony_ci if (IsLastPlugged() && !IsNowPlugged(pluggedType)) { 400f16e0440Sopenharmony_ci return true; 401f16e0440Sopenharmony_ci } 402f16e0440Sopenharmony_ci return false; 403f16e0440Sopenharmony_ci} 404f16e0440Sopenharmony_ci 405f16e0440Sopenharmony_cibool BatteryService::IsCharging(BatteryChargeState chargeState) 406f16e0440Sopenharmony_ci{ 407f16e0440Sopenharmony_ci return chargeState == BatteryChargeState::CHARGE_STATE_ENABLE; 408f16e0440Sopenharmony_ci} 409f16e0440Sopenharmony_ci 410f16e0440Sopenharmony_cibool BatteryService::IsInExtremePowerSaveMode() 411f16e0440Sopenharmony_ci{ 412f16e0440Sopenharmony_ci PowerMode mode = PowerMgrClient::GetInstance().GetDeviceMode(); 413f16e0440Sopenharmony_ci return mode == PowerMode::EXTREME_POWER_SAVE_MODE; 414f16e0440Sopenharmony_ci} 415f16e0440Sopenharmony_ci 416f16e0440Sopenharmony_civoid BatteryService::WakeupDevice(BatteryPluggedType pluggedType) 417f16e0440Sopenharmony_ci{ 418f16e0440Sopenharmony_ci if (IsPlugged(pluggedType) || IsUnplugged(pluggedType)) { 419f16e0440Sopenharmony_ci PowerMgrClient::GetInstance().WakeupDevice(); 420f16e0440Sopenharmony_ci } 421f16e0440Sopenharmony_ci g_lastPluggedType = pluggedType; 422f16e0440Sopenharmony_ci} 423f16e0440Sopenharmony_ci 424f16e0440Sopenharmony_civoid BatteryService::HandleTemperature(int32_t temperature) 425f16e0440Sopenharmony_ci{ 426f16e0440Sopenharmony_ci if (((temperature <= lowTemperature_) || (temperature >= highTemperature_)) && 427f16e0440Sopenharmony_ci (highTemperature_ != lowTemperature_)) { 428f16e0440Sopenharmony_ci PowerMgrClient::GetInstance().ShutDownDevice("TemperatureOutOfRange"); 429f16e0440Sopenharmony_ci } 430f16e0440Sopenharmony_ci} 431f16e0440Sopenharmony_ci 432f16e0440Sopenharmony_civoid BatteryService::HandleCapacity(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent) 433f16e0440Sopenharmony_ci{ 434f16e0440Sopenharmony_ci if ((capacity <= shutdownCapacityThreshold_) && 435f16e0440Sopenharmony_ci (g_lowCapacityShutdownHandle == nullptr) && 436f16e0440Sopenharmony_ci#ifndef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD 437f16e0440Sopenharmony_ci isBatteryPresent && 438f16e0440Sopenharmony_ci#endif 439f16e0440Sopenharmony_ci (!IsCharging(chargeState))) { 440f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "HandleCapacity begin to submit task, " 441f16e0440Sopenharmony_ci "capacity=%{public}d, chargeState=%{public}u, isBatteryPresent=%{public}d", 442f16e0440Sopenharmony_ci capacity, static_cast<uint32_t>(chargeState), isBatteryPresent); 443f16e0440Sopenharmony_ci FFRTTask task = [&] { 444f16e0440Sopenharmony_ci if (!IsInExtremePowerSaveMode()) { 445f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "HandleCapacity begin to shutdown"); 446f16e0440Sopenharmony_ci PowerMgrClient::GetInstance().ShutDownDevice("LowCapacity"); 447f16e0440Sopenharmony_ci } 448f16e0440Sopenharmony_ci }; 449f16e0440Sopenharmony_ci g_lowCapacityShutdownHandle = FFRTUtils::SubmitDelayTask(task, SHUTDOWN_DELAY_TIME_MS, g_queue); 450f16e0440Sopenharmony_ci } 451f16e0440Sopenharmony_ci 452f16e0440Sopenharmony_ci if (g_lowCapacityShutdownHandle != nullptr && IsCharging(chargeState)) { 453f16e0440Sopenharmony_ci BATTERY_HILOGI(COMP_SVC, "HandleCapacity cancel shutdown task, " 454f16e0440Sopenharmony_ci "capacity=%{public}d, chargeState=%{public}u, isBatteryPresent=%{public}d", 455f16e0440Sopenharmony_ci capacity, static_cast<uint32_t>(chargeState), isBatteryPresent); 456f16e0440Sopenharmony_ci FFRTUtils::CancelTask(g_lowCapacityShutdownHandle, g_queue); 457f16e0440Sopenharmony_ci g_lowCapacityShutdownHandle = nullptr; 458f16e0440Sopenharmony_ci } 459f16e0440Sopenharmony_ci} 460f16e0440Sopenharmony_ci 461f16e0440Sopenharmony_ciint32_t BatteryService::GetCapacity() 462f16e0440Sopenharmony_ci{ 463f16e0440Sopenharmony_ci if (isMockCapacity_) { 464f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Return mock battery capacity"); 465f16e0440Sopenharmony_ci return batteryInfo_.GetCapacity(); 466f16e0440Sopenharmony_ci } 467f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 468f16e0440Sopenharmony_ci int32_t capacity = BATTERY_FULL_CAPACITY; 469f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 470f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 471f16e0440Sopenharmony_ci return capacity; 472f16e0440Sopenharmony_ci } 473f16e0440Sopenharmony_ci iBatteryInterface_->GetCapacity(capacity); 474f16e0440Sopenharmony_ci return capacity; 475f16e0440Sopenharmony_ci} 476f16e0440Sopenharmony_ci 477f16e0440Sopenharmony_cibool BatteryService::ChangePath(const std::string path) 478f16e0440Sopenharmony_ci{ 479f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Enter"); 480f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 481f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 482f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 483f16e0440Sopenharmony_ci return false; 484f16e0440Sopenharmony_ci } 485f16e0440Sopenharmony_ci iBatteryInterface_->ChangePath(path); 486f16e0440Sopenharmony_ci return true; 487f16e0440Sopenharmony_ci} 488f16e0440Sopenharmony_ci 489f16e0440Sopenharmony_ci#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD 490f16e0440Sopenharmony_civoid BatteryService::SetLowCapacityThreshold() 491f16e0440Sopenharmony_ci{ 492f16e0440Sopenharmony_ci const std::string thers = "low_battery_thers"; 493f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 494f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 495f16e0440Sopenharmony_ci return; 496f16e0440Sopenharmony_ci } 497f16e0440Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_INFO, "set low capacity thres: criticalCapacityThreshold_ = %{public}d", 498f16e0440Sopenharmony_ci criticalCapacityThreshold_); 499f16e0440Sopenharmony_ci iBatteryInterface_->SetBatteryConfig(thers, std::to_string(criticalCapacityThreshold_)); 500f16e0440Sopenharmony_ci} 501f16e0440Sopenharmony_ci#endif 502f16e0440Sopenharmony_ci 503f16e0440Sopenharmony_ciBatteryError BatteryService::SetBatteryConfig(const std::string& sceneName, const std::string& value) 504f16e0440Sopenharmony_ci{ 505f16e0440Sopenharmony_ci if (!Permission::IsSystem() || !Permission::IsNativePermissionGranted("ohos.permission.POWER_OPTIMIZATION")) { 506f16e0440Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_INFO, "SetBatteryConfig failed, System permission intercept"); 507f16e0440Sopenharmony_ci return BatteryError::ERR_SYSTEM_API_DENIED; 508f16e0440Sopenharmony_ci } 509f16e0440Sopenharmony_ci 510f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Enter SetBatteryConfig"); 511f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 512f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 513f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 514f16e0440Sopenharmony_ci return BatteryError::ERR_FAILURE; 515f16e0440Sopenharmony_ci } 516f16e0440Sopenharmony_ci return iBatteryInterface_->SetBatteryConfig(sceneName, value) == ERR_OK ? 517f16e0440Sopenharmony_ci BatteryError::ERR_OK : BatteryError::ERR_FAILURE; 518f16e0440Sopenharmony_ci} 519f16e0440Sopenharmony_ci 520f16e0440Sopenharmony_ciBatteryError BatteryService::GetBatteryConfig(const std::string& sceneName, std::string& result) 521f16e0440Sopenharmony_ci{ 522f16e0440Sopenharmony_ci if (!Permission::IsSystem()) { 523f16e0440Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_INFO, "GetBatteryConfig failed, System permission intercept"); 524f16e0440Sopenharmony_ci return BatteryError::ERR_SYSTEM_API_DENIED; 525f16e0440Sopenharmony_ci } 526f16e0440Sopenharmony_ci 527f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Enter GetBatteryConfig"); 528f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 529f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 530f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 531f16e0440Sopenharmony_ci return BatteryError::ERR_FAILURE; 532f16e0440Sopenharmony_ci } 533f16e0440Sopenharmony_ci 534f16e0440Sopenharmony_ci int32_t ret = iBatteryInterface_->GetBatteryConfig(sceneName, result); 535f16e0440Sopenharmony_ci if (ret != ERR_OK) { 536f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "get charge config failed, key:%{public}s", sceneName.c_str()); 537f16e0440Sopenharmony_ci return BatteryError::ERR_FAILURE; 538f16e0440Sopenharmony_ci } 539f16e0440Sopenharmony_ci 540f16e0440Sopenharmony_ci return BatteryError::ERR_OK; 541f16e0440Sopenharmony_ci} 542f16e0440Sopenharmony_ci 543f16e0440Sopenharmony_ciBatteryError BatteryService::IsBatteryConfigSupported(const std::string& sceneName, bool& result) 544f16e0440Sopenharmony_ci{ 545f16e0440Sopenharmony_ci if (!Permission::IsSystem()) { 546f16e0440Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_INFO, "IsBatteryConfigSupported failed, System permission intercept"); 547f16e0440Sopenharmony_ci return BatteryError::ERR_SYSTEM_API_DENIED; 548f16e0440Sopenharmony_ci } 549f16e0440Sopenharmony_ci 550f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Enter IsBatteryConfigSupported"); 551f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 552f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 553f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 554f16e0440Sopenharmony_ci return BatteryError::ERR_FAILURE; 555f16e0440Sopenharmony_ci } 556f16e0440Sopenharmony_ci 557f16e0440Sopenharmony_ci int32_t ret = iBatteryInterface_->IsBatteryConfigSupported(sceneName, result); 558f16e0440Sopenharmony_ci if (ret != ERR_OK) { 559f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "get support charge config failed, key:%{public}s", sceneName.c_str()); 560f16e0440Sopenharmony_ci return BatteryError::ERR_FAILURE; 561f16e0440Sopenharmony_ci } 562f16e0440Sopenharmony_ci return BatteryError::ERR_OK; 563f16e0440Sopenharmony_ci} 564f16e0440Sopenharmony_ci 565f16e0440Sopenharmony_ciBatteryChargeState BatteryService::GetChargingStatus() 566f16e0440Sopenharmony_ci{ 567f16e0440Sopenharmony_ci if (isMockUnplugged_) { 568f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Return mock charge status"); 569f16e0440Sopenharmony_ci return batteryInfo_.GetChargeState(); 570f16e0440Sopenharmony_ci } 571f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 572f16e0440Sopenharmony_ci V2_0::BatteryChargeState chargeState = V2_0::BatteryChargeState(0); 573f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 574f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 575f16e0440Sopenharmony_ci return BatteryChargeState(chargeState); 576f16e0440Sopenharmony_ci } 577f16e0440Sopenharmony_ci iBatteryInterface_->GetChargeState(chargeState); 578f16e0440Sopenharmony_ci return BatteryChargeState(chargeState); 579f16e0440Sopenharmony_ci} 580f16e0440Sopenharmony_ci 581f16e0440Sopenharmony_ciBatteryHealthState BatteryService::GetHealthStatus() 582f16e0440Sopenharmony_ci{ 583f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Enter"); 584f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 585f16e0440Sopenharmony_ci V2_0::BatteryHealthState healthState = V2_0::BatteryHealthState(0); 586f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 587f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 588f16e0440Sopenharmony_ci return BatteryHealthState(healthState); 589f16e0440Sopenharmony_ci } 590f16e0440Sopenharmony_ci 591f16e0440Sopenharmony_ci iBatteryInterface_->GetHealthState(healthState); 592f16e0440Sopenharmony_ci return BatteryHealthState(healthState); 593f16e0440Sopenharmony_ci} 594f16e0440Sopenharmony_ci 595f16e0440Sopenharmony_ciBatteryPluggedType BatteryService::GetPluggedType() 596f16e0440Sopenharmony_ci{ 597f16e0440Sopenharmony_ci if (isMockUnplugged_) { 598f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "Return mock plugged type"); 599f16e0440Sopenharmony_ci return batteryInfo_.GetPluggedType(); 600f16e0440Sopenharmony_ci } 601f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 602f16e0440Sopenharmony_ci V2_0::BatteryPluggedType pluggedType = V2_0::BatteryPluggedType(0); 603f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 604f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 605f16e0440Sopenharmony_ci return BatteryPluggedType(pluggedType); 606f16e0440Sopenharmony_ci } 607f16e0440Sopenharmony_ci iBatteryInterface_->GetPluggedType(pluggedType); 608f16e0440Sopenharmony_ci return BatteryPluggedType(pluggedType); 609f16e0440Sopenharmony_ci} 610f16e0440Sopenharmony_ci 611f16e0440Sopenharmony_ciint32_t BatteryService::GetVoltage() 612f16e0440Sopenharmony_ci{ 613f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 614f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 615f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 616f16e0440Sopenharmony_ci return ERR_NO_INIT; 617f16e0440Sopenharmony_ci } 618f16e0440Sopenharmony_ci int32_t voltage = INVALID_BATT_INT_VALUE; 619f16e0440Sopenharmony_ci iBatteryInterface_->GetVoltage(voltage); 620f16e0440Sopenharmony_ci return voltage; 621f16e0440Sopenharmony_ci} 622f16e0440Sopenharmony_ci 623f16e0440Sopenharmony_cibool BatteryService::GetPresent() 624f16e0440Sopenharmony_ci{ 625f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 626f16e0440Sopenharmony_ci bool present = false; 627f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 628f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 629f16e0440Sopenharmony_ci return present; 630f16e0440Sopenharmony_ci } 631f16e0440Sopenharmony_ci 632f16e0440Sopenharmony_ci iBatteryInterface_->GetPresent(present); 633f16e0440Sopenharmony_ci return present; 634f16e0440Sopenharmony_ci} 635f16e0440Sopenharmony_ci 636f16e0440Sopenharmony_cistd::string BatteryService::GetTechnology() 637f16e0440Sopenharmony_ci{ 638f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 639f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 640f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 641f16e0440Sopenharmony_ci return ""; 642f16e0440Sopenharmony_ci } 643f16e0440Sopenharmony_ci 644f16e0440Sopenharmony_ci std::string technology; 645f16e0440Sopenharmony_ci iBatteryInterface_->GetTechnology(technology); 646f16e0440Sopenharmony_ci return technology; 647f16e0440Sopenharmony_ci} 648f16e0440Sopenharmony_ci 649f16e0440Sopenharmony_ciint32_t BatteryService::GetBatteryTemperature() 650f16e0440Sopenharmony_ci{ 651f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 652f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 653f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 654f16e0440Sopenharmony_ci return batteryInfo_.GetTemperature(); 655f16e0440Sopenharmony_ci } 656f16e0440Sopenharmony_ci int32_t temperature = INVALID_BATT_INT_VALUE; 657f16e0440Sopenharmony_ci iBatteryInterface_->GetTemperature(temperature); 658f16e0440Sopenharmony_ci return temperature; 659f16e0440Sopenharmony_ci} 660f16e0440Sopenharmony_ci 661f16e0440Sopenharmony_ciint32_t BatteryService::GetTotalEnergy() 662f16e0440Sopenharmony_ci{ 663f16e0440Sopenharmony_ci int32_t totalEnergy = INVALID_BATT_INT_VALUE; 664f16e0440Sopenharmony_ci if (!Permission::IsSystem()) { 665f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "GetTotalEnergy totalEnergy: %{public}d", totalEnergy); 666f16e0440Sopenharmony_ci return totalEnergy; 667f16e0440Sopenharmony_ci } 668f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 669f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 670f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 671f16e0440Sopenharmony_ci return batteryInfo_.GetTotalEnergy(); 672f16e0440Sopenharmony_ci } 673f16e0440Sopenharmony_ci iBatteryInterface_->GetTotalEnergy(totalEnergy); 674f16e0440Sopenharmony_ci return totalEnergy; 675f16e0440Sopenharmony_ci} 676f16e0440Sopenharmony_ci 677f16e0440Sopenharmony_ciint32_t BatteryService::GetCurrentAverage() 678f16e0440Sopenharmony_ci{ 679f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 680f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 681f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 682f16e0440Sopenharmony_ci return batteryInfo_.GetCurAverage(); 683f16e0440Sopenharmony_ci } 684f16e0440Sopenharmony_ci int32_t curAverage = INVALID_BATT_INT_VALUE; 685f16e0440Sopenharmony_ci iBatteryInterface_->GetCurrentAverage(curAverage); 686f16e0440Sopenharmony_ci return curAverage; 687f16e0440Sopenharmony_ci} 688f16e0440Sopenharmony_ci 689f16e0440Sopenharmony_ciint32_t BatteryService::GetNowCurrent() 690f16e0440Sopenharmony_ci{ 691f16e0440Sopenharmony_ci int32_t nowCurr = INVALID_BATT_INT_VALUE; 692f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 693f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 694f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 695f16e0440Sopenharmony_ci return batteryInfo_.GetNowCurrent(); 696f16e0440Sopenharmony_ci } 697f16e0440Sopenharmony_ci iBatteryInterface_->GetCurrentNow(nowCurr); 698f16e0440Sopenharmony_ci return nowCurr; 699f16e0440Sopenharmony_ci} 700f16e0440Sopenharmony_ci 701f16e0440Sopenharmony_ciint32_t BatteryService::GetRemainEnergy() 702f16e0440Sopenharmony_ci{ 703f16e0440Sopenharmony_ci int32_t remainEnergy = INVALID_BATT_INT_VALUE; 704f16e0440Sopenharmony_ci if (!Permission::IsSystem()) { 705f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_INFO, "GetRemainEnergy remainEnergy: %{public}d", remainEnergy); 706f16e0440Sopenharmony_ci return remainEnergy; 707f16e0440Sopenharmony_ci } 708f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 709f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 710f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 711f16e0440Sopenharmony_ci return batteryInfo_.GetRemainEnergy(); 712f16e0440Sopenharmony_ci } 713f16e0440Sopenharmony_ci iBatteryInterface_->GetRemainEnergy(remainEnergy); 714f16e0440Sopenharmony_ci return remainEnergy; 715f16e0440Sopenharmony_ci} 716f16e0440Sopenharmony_ci 717f16e0440Sopenharmony_ciChargeType BatteryService::GetChargeType() 718f16e0440Sopenharmony_ci{ 719f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 720f16e0440Sopenharmony_ci V2_0::ChargeType chargeType = V2_0::ChargeType::CHARGE_TYPE_NONE; 721f16e0440Sopenharmony_ci if (iBatteryInterface_ == nullptr) { 722f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 723f16e0440Sopenharmony_ci return ChargeType(chargeType); 724f16e0440Sopenharmony_ci } 725f16e0440Sopenharmony_ci 726f16e0440Sopenharmony_ci iBatteryInterface_->GetChargeType(chargeType); 727f16e0440Sopenharmony_ci return ChargeType(chargeType); 728f16e0440Sopenharmony_ci} 729f16e0440Sopenharmony_ci 730f16e0440Sopenharmony_civoid BatteryService::CalculateRemainingChargeTime(int32_t capacity, BatteryChargeState chargeState) 731f16e0440Sopenharmony_ci{ 732f16e0440Sopenharmony_ci if (capacity > BATTERY_FULL_CAPACITY) { 733f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "capacity error"); 734f16e0440Sopenharmony_ci return; 735f16e0440Sopenharmony_ci } 736f16e0440Sopenharmony_ci 737f16e0440Sopenharmony_ci if (chargeState != BatteryChargeState::CHARGE_STATE_ENABLE) { 738f16e0440Sopenharmony_ci remainTime_ = 0; 739f16e0440Sopenharmony_ci chargeFlag_ = false; 740f16e0440Sopenharmony_ci return; 741f16e0440Sopenharmony_ci } 742f16e0440Sopenharmony_ci 743f16e0440Sopenharmony_ci if (!chargeFlag_) { 744f16e0440Sopenharmony_ci lastCapacity_ = capacity; 745f16e0440Sopenharmony_ci lastTime_ = GetCurrentTime(); 746f16e0440Sopenharmony_ci chargeFlag_ = true; 747f16e0440Sopenharmony_ci } 748f16e0440Sopenharmony_ci 749f16e0440Sopenharmony_ci if (capacity < lastCapacity_) { 750f16e0440Sopenharmony_ci lastCapacity_ = capacity; 751f16e0440Sopenharmony_ci } 752f16e0440Sopenharmony_ci 753f16e0440Sopenharmony_ci if (((capacity - lastCapacity_) >= 1) && (lastCapacity_ >= 0) && chargeFlag_) { 754f16e0440Sopenharmony_ci int64_t onceTime = (GetCurrentTime() - lastTime_) / (capacity - lastCapacity_); 755f16e0440Sopenharmony_ci remainTime_ = (BATTERY_FULL_CAPACITY - capacity) * onceTime; 756f16e0440Sopenharmony_ci lastCapacity_ = capacity; 757f16e0440Sopenharmony_ci lastTime_ = GetCurrentTime(); 758f16e0440Sopenharmony_ci } 759f16e0440Sopenharmony_ci} 760f16e0440Sopenharmony_ci 761f16e0440Sopenharmony_ciint64_t BatteryService::GetRemainingChargeTime() 762f16e0440Sopenharmony_ci{ 763f16e0440Sopenharmony_ci if (!Permission::IsSystem()) { 764f16e0440Sopenharmony_ci BATTERY_HILOGW(FEATURE_BATT_INFO, "system permission denied."); 765f16e0440Sopenharmony_ci return INVALID_REMAINING_CHARGE_TIME_VALUE; 766f16e0440Sopenharmony_ci } 767f16e0440Sopenharmony_ci return remainTime_; 768f16e0440Sopenharmony_ci} 769f16e0440Sopenharmony_ci 770f16e0440Sopenharmony_cibool IsCapacityLevelDefined(int32_t capacityThreshold) 771f16e0440Sopenharmony_ci{ 772f16e0440Sopenharmony_ci return capacityThreshold != INVALID_BATT_INT_VALUE; 773f16e0440Sopenharmony_ci} 774f16e0440Sopenharmony_ci 775f16e0440Sopenharmony_ciBatteryCapacityLevel BatteryService::GetCapacityLevel() 776f16e0440Sopenharmony_ci{ 777f16e0440Sopenharmony_ci BatteryCapacityLevel batteryCapacityLevel = BatteryCapacityLevel::LEVEL_NONE; 778f16e0440Sopenharmony_ci int32_t capacity = GetCapacity(); 779f16e0440Sopenharmony_ci if (IsCapacityLevelDefined(shutdownCapacityThreshold_) && capacity > 0 && capacity <= shutdownCapacityThreshold_) { 780f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_SHUTDOWN; 781f16e0440Sopenharmony_ci } else if (IsCapacityLevelDefined(criticalCapacityThreshold_) && capacity > shutdownCapacityThreshold_ && 782f16e0440Sopenharmony_ci capacity <= criticalCapacityThreshold_) { 783f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_CRITICAL; 784f16e0440Sopenharmony_ci } else if (IsCapacityLevelDefined(warningCapacityThreshold_) && capacity > criticalCapacityThreshold_ && 785f16e0440Sopenharmony_ci capacity <= warningCapacityThreshold_) { 786f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_WARNING; 787f16e0440Sopenharmony_ci } else if (IsCapacityLevelDefined(lowCapacityThreshold_) && capacity > warningCapacityThreshold_ && 788f16e0440Sopenharmony_ci capacity <= lowCapacityThreshold_) { 789f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_LOW; 790f16e0440Sopenharmony_ci } else if (IsCapacityLevelDefined(normalCapacityThreshold_) && capacity > lowCapacityThreshold_ && 791f16e0440Sopenharmony_ci capacity <= normalCapacityThreshold_) { 792f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_NORMAL; 793f16e0440Sopenharmony_ci } else if (IsCapacityLevelDefined(highCapacityThreshold_) && capacity > normalCapacityThreshold_ && 794f16e0440Sopenharmony_ci capacity <= highCapacityThreshold_) { 795f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_HIGH; 796f16e0440Sopenharmony_ci } else if (IsCapacityLevelDefined(fullCapacityThreshold_) && capacity > highCapacityThreshold_ && 797f16e0440Sopenharmony_ci capacity <= fullCapacityThreshold_) { 798f16e0440Sopenharmony_ci batteryCapacityLevel = BatteryCapacityLevel::LEVEL_FULL; 799f16e0440Sopenharmony_ci } 800f16e0440Sopenharmony_ci return batteryCapacityLevel; 801f16e0440Sopenharmony_ci} 802f16e0440Sopenharmony_ci 803f16e0440Sopenharmony_ciint32_t BatteryService::Dump(int32_t fd, const std::vector<std::u16string> &args) 804f16e0440Sopenharmony_ci{ 805f16e0440Sopenharmony_ci if (!isBootCompleted_) { 806f16e0440Sopenharmony_ci return ERR_NO_INIT; 807f16e0440Sopenharmony_ci } 808f16e0440Sopenharmony_ci if (!Permission::IsSystem()) { 809f16e0440Sopenharmony_ci return ERR_PERMISSION_DENIED; 810f16e0440Sopenharmony_ci } 811f16e0440Sopenharmony_ci 812f16e0440Sopenharmony_ci BatteryDump& batteryDump = BatteryDump::GetInstance(); 813f16e0440Sopenharmony_ci if ((args.empty()) || (args[0].compare(u"-h") == 0)) { 814f16e0440Sopenharmony_ci batteryDump.DumpBatteryHelp(fd); 815f16e0440Sopenharmony_ci return ERR_OK; 816f16e0440Sopenharmony_ci } 817f16e0440Sopenharmony_ci bool getBatteryInfo = batteryDump.GetBatteryInfo(fd, g_service, args); 818f16e0440Sopenharmony_ci bool unplugged = batteryDump.MockUnplugged(fd, g_service, args); 819f16e0440Sopenharmony_ci bool mockedCapacity = batteryDump.MockCapacity(fd, g_service, args); 820f16e0440Sopenharmony_ci bool mockedUevent = batteryDump.MockUevent(fd, g_service, args); 821f16e0440Sopenharmony_ci bool reset = batteryDump.Reset(fd, g_service, args); 822f16e0440Sopenharmony_ci bool total = getBatteryInfo + unplugged + mockedCapacity + mockedUevent + reset; 823f16e0440Sopenharmony_ci if (!total) { 824f16e0440Sopenharmony_ci dprintf(fd, "cmd param is invalid\n"); 825f16e0440Sopenharmony_ci batteryDump.DumpBatteryHelp(fd); 826f16e0440Sopenharmony_ci return ERR_NO_INIT; 827f16e0440Sopenharmony_ci } 828f16e0440Sopenharmony_ci 829f16e0440Sopenharmony_ci return ERR_OK; 830f16e0440Sopenharmony_ci} 831f16e0440Sopenharmony_ci 832f16e0440Sopenharmony_civoid BatteryService::MockUnplugged() 833f16e0440Sopenharmony_ci{ 834f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 835f16e0440Sopenharmony_ci if (!iBatteryInterface_) { 836f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 837f16e0440Sopenharmony_ci return; 838f16e0440Sopenharmony_ci } 839f16e0440Sopenharmony_ci isMockUnplugged_ = true; 840f16e0440Sopenharmony_ci V2_0::BatteryInfo event; 841f16e0440Sopenharmony_ci iBatteryInterface_->GetBatteryInfo(event); 842f16e0440Sopenharmony_ci ConvertingEvent(event); 843f16e0440Sopenharmony_ci batteryInfo_.SetPluggedType(BatteryPluggedType::PLUGGED_TYPE_NONE); 844f16e0440Sopenharmony_ci batteryInfo_.SetPluggedMaxCurrent(0); 845f16e0440Sopenharmony_ci batteryInfo_.SetPluggedMaxVoltage(0); 846f16e0440Sopenharmony_ci batteryInfo_.SetChargeState(BatteryChargeState::CHARGE_STATE_NONE); 847f16e0440Sopenharmony_ci HandleBatteryInfo(); 848f16e0440Sopenharmony_ci} 849f16e0440Sopenharmony_ci 850f16e0440Sopenharmony_cibool BatteryService::IsMockUnplugged() 851f16e0440Sopenharmony_ci{ 852f16e0440Sopenharmony_ci return isMockUnplugged_; 853f16e0440Sopenharmony_ci} 854f16e0440Sopenharmony_ci 855f16e0440Sopenharmony_civoid BatteryService::MockCapacity(int32_t capacity) 856f16e0440Sopenharmony_ci{ 857f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 858f16e0440Sopenharmony_ci if (!iBatteryInterface_) { 859f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 860f16e0440Sopenharmony_ci return; 861f16e0440Sopenharmony_ci } 862f16e0440Sopenharmony_ci isMockCapacity_ = true; 863f16e0440Sopenharmony_ci V2_0::BatteryInfo event; 864f16e0440Sopenharmony_ci iBatteryInterface_->GetBatteryInfo(event); 865f16e0440Sopenharmony_ci ConvertingEvent(event); 866f16e0440Sopenharmony_ci batteryInfo_.SetCapacity(capacity); 867f16e0440Sopenharmony_ci HandleBatteryInfo(); 868f16e0440Sopenharmony_ci} 869f16e0440Sopenharmony_ci 870f16e0440Sopenharmony_cibool BatteryService::IsMockCapacity() 871f16e0440Sopenharmony_ci{ 872f16e0440Sopenharmony_ci return isMockCapacity_; 873f16e0440Sopenharmony_ci} 874f16e0440Sopenharmony_ci 875f16e0440Sopenharmony_civoid BatteryService::MockUevent(const std::string& uevent) 876f16e0440Sopenharmony_ci{ 877f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 878f16e0440Sopenharmony_ci if (!iBatteryInterface_) { 879f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 880f16e0440Sopenharmony_ci return; 881f16e0440Sopenharmony_ci } 882f16e0440Sopenharmony_ci isMockUevent_ = true; 883f16e0440Sopenharmony_ci V2_0::BatteryInfo event; 884f16e0440Sopenharmony_ci iBatteryInterface_->GetBatteryInfo(event); 885f16e0440Sopenharmony_ci ConvertingEvent(event); 886f16e0440Sopenharmony_ci batteryInfo_.SetUevent(uevent); 887f16e0440Sopenharmony_ci HandleBatteryInfo(); 888f16e0440Sopenharmony_ci} 889f16e0440Sopenharmony_ci 890f16e0440Sopenharmony_civoid BatteryService::Reset() 891f16e0440Sopenharmony_ci{ 892f16e0440Sopenharmony_ci std::shared_lock<std::shared_mutex> lock(mutex_); 893f16e0440Sopenharmony_ci if (!iBatteryInterface_) { 894f16e0440Sopenharmony_ci BATTERY_HILOGE(FEATURE_BATT_INFO, "iBatteryInterface_ is nullptr"); 895f16e0440Sopenharmony_ci return; 896f16e0440Sopenharmony_ci } 897f16e0440Sopenharmony_ci isMockUnplugged_ = false; 898f16e0440Sopenharmony_ci isMockCapacity_ = false; 899f16e0440Sopenharmony_ci isMockUevent_ = false; 900f16e0440Sopenharmony_ci V2_0::BatteryInfo event; 901f16e0440Sopenharmony_ci iBatteryInterface_->GetBatteryInfo(event); 902f16e0440Sopenharmony_ci ConvertingEvent(event); 903f16e0440Sopenharmony_ci HandleBatteryInfo(); 904f16e0440Sopenharmony_ci} 905f16e0440Sopenharmony_ci 906f16e0440Sopenharmony_civoid BatteryService::VibratorInit() 907f16e0440Sopenharmony_ci{ 908f16e0440Sopenharmony_ci std::shared_ptr<PowerVibrator> vibrator = PowerVibrator::GetInstance(); 909f16e0440Sopenharmony_ci vibrator->LoadConfig(BATTERY_VIBRATOR_CONFIG_FILE, 910f16e0440Sopenharmony_ci VENDOR_BATTERY_VIBRATOR_CONFIG_FILE, SYSTEM_BATTERY_VIBRATOR_CONFIG_FILE); 911f16e0440Sopenharmony_ci} 912f16e0440Sopenharmony_ci} // namespace PowerMgr 913f16e0440Sopenharmony_ci} // namespace OHOS 914