1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef POWERMGR_BATTERY_SERVICE_H 17 #define POWERMGR_BATTERY_SERVICE_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <iosfwd> 22 #include <memory> 23 #include <shared_mutex> 24 #include <string> 25 #include <vector> 26 27 #include "event_runner.h" 28 #include "hdi_service_status_listener.h" 29 #include "iservmgr_hdi.h" 30 #include "iservstat_listener_hdi.h" 31 #include "refbase.h" 32 #include "system_ability.h" 33 34 #include "battery_info.h" 35 #include "battery_light.h" 36 #include "battery_notify.h" 37 #include "battery_srv_stub.h" 38 #include "ibattery_srv.h" 39 #include "sp_singleton.h" 40 #include "v2_0/ibattery_interface.h" 41 #include "v2_0/types.h" 42 43 namespace OHOS { 44 namespace PowerMgr { 45 class BatteryService final : public SystemAbility, 46 public BatterySrvStub { 47 DECLARE_SYSTEM_ABILITY(BatteryService) 48 49 DECLARE_DELAYED_SP_SINGLETON(BatteryService); 50 public: 51 virtual void OnStart() override; 52 virtual void OnStop() override; 53 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 54 IsServiceReady()55 bool IsServiceReady() const 56 { 57 return ready_; 58 } 59 IsBootCompleted()60 bool IsBootCompleted() const 61 { 62 return isBootCompleted_.load(); 63 } 64 65 int32_t Dump(int fd, const std::vector<std::u16string> &args) override; 66 int32_t GetCapacity() override; 67 BatteryChargeState GetChargingStatus() override; 68 BatteryHealthState GetHealthStatus() override; 69 BatteryPluggedType GetPluggedType() override; 70 int32_t GetVoltage() override; 71 bool GetPresent() override; 72 std::string GetTechnology() override; 73 int32_t GetTotalEnergy() override; 74 int32_t GetCurrentAverage() override; 75 int32_t GetNowCurrent() override; 76 int32_t GetRemainEnergy() override; 77 int32_t GetBatteryTemperature() override; 78 BatteryCapacityLevel GetCapacityLevel() override; 79 int64_t GetRemainingChargeTime() override; 80 ChargeType GetChargeType(); 81 bool ChangePath(const std::string path); 82 BatteryError SetBatteryConfig(const std::string& sceneName, const std::string& value) override; 83 BatteryError GetBatteryConfig(const std::string& sceneName, std::string& result) override; 84 BatteryError IsBatteryConfigSupported(const std::string& sceneName, bool& result) override; 85 void InitConfig(); 86 void HandleTemperature(int32_t temperature); 87 bool RegisterHdiStatusListener(); 88 bool RegisterBatteryHdiCallback(); 89 bool IsMockUnplugged(); 90 void MockUnplugged(); 91 bool IsMockCapacity(); 92 void MockCapacity(int32_t capacity); 93 void MockUevent(const std::string& uevent); 94 void Reset(); 95 void VibratorInit(); 96 private: 97 bool Init(); 98 void AddBootCommonEvents(); 99 bool FillCommonEvent(std::string& ueventName, std::string& type); 100 void WakeupDevice(BatteryChargeState chargeState); 101 void RegisterBootCompletedCallback(); 102 int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo& event); 103 void ConvertingEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo &event); 104 void InitBatteryInfo(); 105 void HandleBatteryInfo(); 106 void CalculateRemainingChargeTime(int32_t capacity, BatteryChargeState chargeState); 107 void HandleCapacity(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent); 108 void HandleCapacityExt(int32_t capacity, BatteryChargeState chargeState); 109 bool IsLastPlugged(); 110 bool IsNowPlugged(BatteryPluggedType pluggedType); 111 bool IsPlugged(BatteryPluggedType pluggedType); 112 bool IsUnplugged(BatteryPluggedType pluggedType); 113 void WakeupDevice(BatteryPluggedType pluggedType); 114 bool IsCharging(BatteryChargeState chargeState); 115 bool IsInExtremePowerSaveMode(); 116 void CreateShutdownGuard(); 117 void LockShutdownGuard(); 118 void UnlockShutdownGuard(); 119 #ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD 120 void SetLowCapacityThreshold(); 121 #endif 122 bool ready_ { false }; 123 static std::atomic_bool isBootCompleted_; 124 std::shared_mutex mutex_; 125 std::unique_ptr<BatteryNotify> batteryNotify_ { nullptr }; 126 BatteryLight batteryLight_; 127 sptr<HDI::Battery::V2_0::IBatteryInterface> iBatteryInterface_ { nullptr }; 128 sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr }; 129 sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr }; 130 bool isLowPower_ { false }; 131 bool isMockUnplugged_ { false }; 132 bool isMockCapacity_ { false }; 133 bool isMockUevent_ { false }; 134 bool chargeFlag_ { false }; 135 int32_t commEventRetryTimes_ { 0 }; 136 int32_t lastCapacity_ { 0 }; 137 int32_t dialogId_ { INVALID_BATT_INT_VALUE }; 138 int32_t warnCapacity_ { INVALID_BATT_INT_VALUE }; 139 int32_t highTemperature_ { INT32_MAX }; 140 int32_t lowTemperature_ { INT32_MIN }; 141 int32_t shutdownCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 142 int32_t criticalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 143 int32_t warningCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 144 int32_t lowCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 145 int32_t normalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 146 int32_t highCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 147 int32_t fullCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 148 int64_t lastTime_ { 0 }; 149 int64_t remainTime_ { 0 }; 150 BatteryInfo batteryInfo_; 151 BatteryInfo lastBatteryInfo_; 152 std::mutex shutdownGuardMutex_; 153 }; 154 } // namespace PowerMgr 155 } // namespace OHOS 156 157 #endif // POWERMGR_BATTERY_SERVICE_H 158