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 60 int32_t Dump(int fd, const std::vector<std::u16string> &args) override; 61 int32_t GetCapacity() override; 62 BatteryChargeState GetChargingStatus() override; 63 BatteryHealthState GetHealthStatus() override; 64 BatteryPluggedType GetPluggedType() override; 65 int32_t GetVoltage() override; 66 bool GetPresent() override; 67 std::string GetTechnology() override; 68 int32_t GetTotalEnergy() override; 69 int32_t GetCurrentAverage() override; 70 int32_t GetNowCurrent() override; 71 int32_t GetRemainEnergy() override; 72 int32_t GetBatteryTemperature() override; 73 BatteryCapacityLevel GetCapacityLevel() override; 74 int64_t GetRemainingChargeTime() override; 75 ChargeType GetChargeType(); 76 bool ChangePath(const std::string path); 77 int32_t SetBatteryConfig(const std::string& sceneName, const std::string& value) override; 78 std::string GetBatteryConfig(const std::string& sceneName) override; 79 bool IsBatteryConfigSupported(const std::string& sceneName) override; 80 void InitConfig(); 81 void HandleTemperature(int32_t temperature); 82 bool RegisterHdiStatusListener(); 83 bool RegisterBatteryHdiCallback(); 84 bool IsMockUnplugged(); 85 void MockUnplugged(); 86 bool IsMockCapacity(); 87 void MockCapacity(int32_t capacity); 88 void Reset(); 89 void VibratorInit(); 90 private: 91 bool Init(); 92 void WakeupDevice(BatteryChargeState chargeState); 93 void RegisterBootCompletedCallback(); 94 int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo& event); 95 void ConvertingEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo &event); 96 void HandleBatteryInfo(); 97 void CalculateRemainingChargeTime(int32_t capacity, BatteryChargeState chargeState); 98 void HandleCapacity(int32_t capacity, BatteryChargeState chargeState); 99 bool IsLastPlugged(); 100 bool IsNowPlugged(BatteryPluggedType pluggedType); 101 bool IsPlugged(BatteryPluggedType pluggedType); 102 bool IsUnplugged(BatteryPluggedType pluggedType); 103 void WakeupDevice(BatteryPluggedType pluggedType); 104 #ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD 105 void SetLowCapacityThreshold(); 106 #endif 107 bool ready_ { false }; 108 static std::atomic_bool isBootCompleted_; 109 std::shared_mutex mutex_; 110 std::unique_ptr<BatteryNotify> batteryNotify_ { nullptr }; 111 BatteryLight batteryLight_; 112 sptr<HDI::Battery::V2_0::IBatteryInterface> iBatteryInterface_ { nullptr }; 113 sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr }; 114 sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr }; 115 bool isLowPower_ { false }; 116 bool isMockUnplugged_ { false }; 117 bool isMockCapacity_ { false }; 118 bool chargeFlag_ { false }; 119 int32_t commEventRetryTimes_ { 0 }; 120 int32_t lastCapacity_ { 0 }; 121 int32_t dialogId_ { INVALID_BATT_INT_VALUE }; 122 int32_t warnCapacity_ { INVALID_BATT_INT_VALUE }; 123 int32_t highTemperature_ { INT32_MAX }; 124 int32_t lowTemperature_ { INT32_MIN }; 125 int32_t shutdownCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 126 int32_t criticalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 127 int32_t warningCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 128 int32_t lowCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 129 int32_t normalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 130 int32_t highCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 131 int32_t fullCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 132 int64_t lastTime_ { 0 }; 133 int64_t remainTime_ { 0 }; 134 BatteryInfo batteryInfo_; 135 BatteryInfo lastBatteryInfo_; 136 }; 137 } // namespace PowerMgr 138 } // namespace OHOS 139 140 #endif // POWERMGR_BATTERY_SERVICE_H 141