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#ifndef POWERMGR_BATTERY_SERVICE_H 17f16e0440Sopenharmony_ci#define POWERMGR_BATTERY_SERVICE_H 18f16e0440Sopenharmony_ci 19f16e0440Sopenharmony_ci#include <atomic> 20f16e0440Sopenharmony_ci#include <cstdint> 21f16e0440Sopenharmony_ci#include <iosfwd> 22f16e0440Sopenharmony_ci#include <memory> 23f16e0440Sopenharmony_ci#include <shared_mutex> 24f16e0440Sopenharmony_ci#include <string> 25f16e0440Sopenharmony_ci#include <vector> 26f16e0440Sopenharmony_ci 27f16e0440Sopenharmony_ci#include "hdi_service_status_listener.h" 28f16e0440Sopenharmony_ci#include "iservmgr_hdi.h" 29f16e0440Sopenharmony_ci#include "iservstat_listener_hdi.h" 30f16e0440Sopenharmony_ci#include "refbase.h" 31f16e0440Sopenharmony_ci#include "system_ability.h" 32f16e0440Sopenharmony_ci 33f16e0440Sopenharmony_ci#include "battery_info.h" 34f16e0440Sopenharmony_ci#include "battery_light.h" 35f16e0440Sopenharmony_ci#include "battery_notify.h" 36f16e0440Sopenharmony_ci#include "battery_srv_stub.h" 37f16e0440Sopenharmony_ci#include "ibattery_srv.h" 38f16e0440Sopenharmony_ci#include "sp_singleton.h" 39f16e0440Sopenharmony_ci#include "v2_0/ibattery_interface.h" 40f16e0440Sopenharmony_ci#include "v2_0/types.h" 41f16e0440Sopenharmony_ci 42f16e0440Sopenharmony_cinamespace OHOS { 43f16e0440Sopenharmony_cinamespace PowerMgr { 44f16e0440Sopenharmony_ciclass BatteryService final : public SystemAbility, 45f16e0440Sopenharmony_ci public BatterySrvStub { 46f16e0440Sopenharmony_ciDECLARE_SYSTEM_ABILITY(BatteryService) 47f16e0440Sopenharmony_ci 48f16e0440Sopenharmony_ciDECLARE_DELAYED_SP_SINGLETON(BatteryService); 49f16e0440Sopenharmony_cipublic: 50f16e0440Sopenharmony_ci virtual void OnStart() override; 51f16e0440Sopenharmony_ci virtual void OnStop() override; 52f16e0440Sopenharmony_ci virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 53f16e0440Sopenharmony_ci 54f16e0440Sopenharmony_ci bool IsServiceReady() const 55f16e0440Sopenharmony_ci { 56f16e0440Sopenharmony_ci return ready_; 57f16e0440Sopenharmony_ci } 58f16e0440Sopenharmony_ci 59f16e0440Sopenharmony_ci int32_t Dump(int fd, const std::vector<std::u16string> &args) override; 60f16e0440Sopenharmony_ci int32_t GetCapacity() override; 61f16e0440Sopenharmony_ci BatteryChargeState GetChargingStatus() override; 62f16e0440Sopenharmony_ci BatteryHealthState GetHealthStatus() override; 63f16e0440Sopenharmony_ci BatteryPluggedType GetPluggedType() override; 64f16e0440Sopenharmony_ci int32_t GetVoltage() override; 65f16e0440Sopenharmony_ci bool GetPresent() override; 66f16e0440Sopenharmony_ci std::string GetTechnology() override; 67f16e0440Sopenharmony_ci int32_t GetTotalEnergy() override; 68f16e0440Sopenharmony_ci int32_t GetCurrentAverage() override; 69f16e0440Sopenharmony_ci int32_t GetNowCurrent() override; 70f16e0440Sopenharmony_ci int32_t GetRemainEnergy() override; 71f16e0440Sopenharmony_ci int32_t GetBatteryTemperature() override; 72f16e0440Sopenharmony_ci BatteryCapacityLevel GetCapacityLevel() override; 73f16e0440Sopenharmony_ci int64_t GetRemainingChargeTime() override; 74f16e0440Sopenharmony_ci ChargeType GetChargeType(); 75f16e0440Sopenharmony_ci bool ChangePath(const std::string path); 76f16e0440Sopenharmony_ci BatteryError SetBatteryConfig(const std::string& sceneName, const std::string& value) override; 77f16e0440Sopenharmony_ci BatteryError GetBatteryConfig(const std::string& sceneName, std::string& result) override; 78f16e0440Sopenharmony_ci BatteryError IsBatteryConfigSupported(const std::string& sceneName, bool& result) override; 79f16e0440Sopenharmony_ci void InitConfig(); 80f16e0440Sopenharmony_ci void HandleTemperature(int32_t temperature); 81f16e0440Sopenharmony_ci bool RegisterHdiStatusListener(); 82f16e0440Sopenharmony_ci bool RegisterBatteryHdiCallback(); 83f16e0440Sopenharmony_ci bool IsMockUnplugged(); 84f16e0440Sopenharmony_ci void MockUnplugged(); 85f16e0440Sopenharmony_ci bool IsMockCapacity(); 86f16e0440Sopenharmony_ci void MockCapacity(int32_t capacity); 87f16e0440Sopenharmony_ci void MockUevent(const std::string& uevent); 88f16e0440Sopenharmony_ci void Reset(); 89f16e0440Sopenharmony_ci void VibratorInit(); 90f16e0440Sopenharmony_ciprivate: 91f16e0440Sopenharmony_ci bool Init(); 92f16e0440Sopenharmony_ci void AddBootCommonEvents(); 93f16e0440Sopenharmony_ci bool FillCommonEvent(std::string& ueventName, std::string& type); 94f16e0440Sopenharmony_ci void WakeupDevice(BatteryChargeState chargeState); 95f16e0440Sopenharmony_ci void RegisterBootCompletedCallback(); 96f16e0440Sopenharmony_ci int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo& event); 97f16e0440Sopenharmony_ci void ConvertingEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo &event); 98f16e0440Sopenharmony_ci void InitBatteryInfo(); 99f16e0440Sopenharmony_ci void HandleBatteryInfo(); 100f16e0440Sopenharmony_ci void CalculateRemainingChargeTime(int32_t capacity, BatteryChargeState chargeState); 101f16e0440Sopenharmony_ci void HandleCapacity(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent); 102f16e0440Sopenharmony_ci bool IsLastPlugged(); 103f16e0440Sopenharmony_ci bool IsNowPlugged(BatteryPluggedType pluggedType); 104f16e0440Sopenharmony_ci bool IsPlugged(BatteryPluggedType pluggedType); 105f16e0440Sopenharmony_ci bool IsUnplugged(BatteryPluggedType pluggedType); 106f16e0440Sopenharmony_ci void WakeupDevice(BatteryPluggedType pluggedType); 107f16e0440Sopenharmony_ci bool IsCharging(BatteryChargeState chargeState); 108f16e0440Sopenharmony_ci bool IsInExtremePowerSaveMode(); 109f16e0440Sopenharmony_ci#ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD 110f16e0440Sopenharmony_ci void SetLowCapacityThreshold(); 111f16e0440Sopenharmony_ci#endif 112f16e0440Sopenharmony_ci bool ready_ { false }; 113f16e0440Sopenharmony_ci static std::atomic_bool isBootCompleted_; 114f16e0440Sopenharmony_ci std::shared_mutex mutex_; 115f16e0440Sopenharmony_ci std::unique_ptr<BatteryNotify> batteryNotify_ { nullptr }; 116f16e0440Sopenharmony_ci BatteryLight batteryLight_; 117f16e0440Sopenharmony_ci sptr<HDI::Battery::V2_0::IBatteryInterface> iBatteryInterface_ { nullptr }; 118f16e0440Sopenharmony_ci sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr }; 119f16e0440Sopenharmony_ci sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr }; 120f16e0440Sopenharmony_ci bool isLowPower_ { false }; 121f16e0440Sopenharmony_ci bool isMockUnplugged_ { false }; 122f16e0440Sopenharmony_ci bool isMockCapacity_ { false }; 123f16e0440Sopenharmony_ci bool isMockUevent_ { false }; 124f16e0440Sopenharmony_ci bool chargeFlag_ { false }; 125f16e0440Sopenharmony_ci int32_t commEventRetryTimes_ { 0 }; 126f16e0440Sopenharmony_ci int32_t lastCapacity_ { 0 }; 127f16e0440Sopenharmony_ci int32_t dialogId_ { INVALID_BATT_INT_VALUE }; 128f16e0440Sopenharmony_ci int32_t warnCapacity_ { INVALID_BATT_INT_VALUE }; 129f16e0440Sopenharmony_ci int32_t highTemperature_ { INT32_MAX }; 130f16e0440Sopenharmony_ci int32_t lowTemperature_ { INT32_MIN }; 131f16e0440Sopenharmony_ci int32_t shutdownCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 132f16e0440Sopenharmony_ci int32_t criticalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 133f16e0440Sopenharmony_ci int32_t warningCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 134f16e0440Sopenharmony_ci int32_t lowCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 135f16e0440Sopenharmony_ci int32_t normalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 136f16e0440Sopenharmony_ci int32_t highCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 137f16e0440Sopenharmony_ci int32_t fullCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 138f16e0440Sopenharmony_ci int64_t lastTime_ { 0 }; 139f16e0440Sopenharmony_ci int64_t remainTime_ { 0 }; 140f16e0440Sopenharmony_ci BatteryInfo batteryInfo_; 141f16e0440Sopenharmony_ci BatteryInfo lastBatteryInfo_; 142f16e0440Sopenharmony_ci}; 143f16e0440Sopenharmony_ci} // namespace PowerMgr 144f16e0440Sopenharmony_ci} // namespace OHOS 145f16e0440Sopenharmony_ci 146f16e0440Sopenharmony_ci#endif // POWERMGR_BATTERY_SERVICE_H 147