1 /* 2 * Copyright (c) 2021-2022 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 <cstdint> 20 #include <mutex> 21 #include <iosfwd> 22 #include <string> 23 #include <memory> 24 #include <vector> 25 #include "refbase.h" 26 #include "event_runner.h" 27 #include "sp_singleton.h" 28 #include "system_ability.h" 29 #include "hdi_service_status_listener.h" 30 #include "iservmgr_hdi.h" 31 #include "iservstat_listener_hdi.h" 32 #include "ibattery_srv.h" 33 #include "v1_1/ibattery_interface.h" 34 #include "v1_1/types.h" 35 #include "battery_info.h" 36 #include "battery_light.h" 37 #include "battery_service_event_handler.h" 38 #include "battery_notify.h" 39 #include "battery_srv_stub.h" 40 41 namespace OHOS { 42 namespace PowerMgr { 43 class BatteryService final : public SystemAbility, 44 public BatterySrvStub { 45 DECLARE_SYSTEM_ABILITY(BatteryService) 46 47 DECLARE_DELAYED_SP_SINGLETON(BatteryService); 48 public: 49 virtual void OnStart() override; 50 virtual void OnStop() override; 51 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 52 IsServiceReady()53 bool IsServiceReady() const 54 { 55 return ready_; 56 } 57 GetHandler()58 std::shared_ptr<BatteryServiceEventHandler> GetHandler() const 59 { 60 return handler_; 61 } 62 63 int32_t Dump(int fd, const std::vector<std::u16string> &args) override; 64 int32_t GetCapacity() override; 65 BatteryChargeState GetChargingStatus() override; 66 BatteryHealthState GetHealthStatus() override; 67 BatteryPluggedType GetPluggedType() override; 68 int32_t GetVoltage() override; 69 bool GetPresent() override; 70 std::string GetTechnology() override; 71 int32_t GetTotalEnergy() override; 72 int32_t GetCurrentAverage() override; 73 int32_t GetNowCurrent() override; 74 int32_t GetRemainEnergy() override; 75 int32_t GetBatteryTemperature() override; 76 BatteryCapacityLevel GetCapacityLevel() override; 77 int64_t GetRemainingChargeTime() override; 78 void ChangePath(const std::string path); 79 void InitConfig(); 80 void HandleTemperature(int32_t temperature); 81 bool RegisterHdiStatusListener(); 82 bool RegisterBatteryHdiCallback(); 83 bool IsMockUnplugged(); 84 void MockUnplugged(bool isUnplugged); 85 bool ShowBatteryDialog(); 86 bool DestoryBatteryDialog(); 87 private: 88 bool Init(); 89 bool IsLastPlugged(); 90 bool IsNowPlugged(BatteryPluggedType pluggedType); 91 bool IsPlugged(BatteryPluggedType pluggedType); 92 bool IsUnplugged(BatteryPluggedType pluggedType); 93 void WakeupDevice(BatteryPluggedType pluggedType); 94 int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V1_1::BatteryInfo& event); 95 void ConvertingEvent(const OHOS::HDI::Battery::V1_1::BatteryInfo &event); 96 void HandleBatteryInfo(); 97 void SendEvent(int32_t event, int64_t delayTime); 98 void CalculateRemainingChargeTime(int32_t capacity, BatteryChargeState chargeState); 99 void HandlePopupEvent(int32_t capacity); 100 void HandleCapacity(int32_t capacity, BatteryChargeState chargeState); 101 bool ready_ { false }; 102 std::mutex mutex_; 103 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_ { nullptr }; 104 std::shared_ptr<BatteryServiceEventHandler> handler_ { nullptr }; 105 std::unique_ptr<BatteryNotify> batteryNotify_ { nullptr }; 106 BatteryLight batteryLight_; 107 sptr<HDI::Battery::V1_1::IBatteryInterface> iBatteryInterface_ { nullptr }; 108 sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr }; 109 sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr }; 110 bool isLowPower_ { false }; 111 bool isMockUnplugged_ { false }; 112 bool chargeFlag_ { false }; 113 int32_t commEventRetryTimes_ { 0 }; 114 int32_t lastCapacity_ { 0 }; 115 int32_t dialogId_ { INVALID_BATT_INT_VALUE }; 116 int32_t warnCapacity_ { INVALID_BATT_INT_VALUE }; 117 int32_t highTemperature_ { INT32_MAX }; 118 int32_t lowTemperature_ { INT32_MIN }; 119 int32_t shutdownCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 120 int32_t criticalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 121 int32_t warningCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 122 int32_t lowCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 123 int32_t normalCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 124 int32_t highCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 125 int32_t fullCapacityThreshold_ = { INVALID_BATT_INT_VALUE }; 126 int64_t lastTime_ { 0 }; 127 int64_t remainTime_ { 0 }; 128 BatteryInfo batteryInfo_; 129 BatteryInfo lastBatteryInfo_; 130 }; 131 } // namespace PowerMgr 132 } // namespace OHOS 133 134 #endif // POWERMGR_BATTERY_SERVICE_H