• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_errors.h"
38 #include "battery_srv_stub.h"
39 #include "battery_xcollie.h"
40 #include "ibattery_srv.h"
41 #include "sp_singleton.h"
42 #include "v2_0/ibattery_interface.h"
43 #include "v2_0/types.h"
44 
45 namespace OHOS {
46 namespace PowerMgr {
47 class BatteryService final : public SystemAbility,
48     public BatterySrvStub {
49 DECLARE_SYSTEM_ABILITY(BatteryService)
50 
51 DECLARE_DELAYED_SP_SINGLETON(BatteryService);
52 public:
53     virtual void OnStart() override;
54     virtual void OnStop() override;
55     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
56 
IsServiceReady()57     bool IsServiceReady() const
58     {
59         return ready_;
60     }
61 
IsBootCompleted()62     bool IsBootCompleted() const
63     {
64         return isBootCompleted_.load();
65     }
66 
67     int32_t Dump(int fd, const std::vector<std::u16string> &args) override;
68     ChargeType GetChargeType();
69     bool ChangePath(const std::string path);
70 private:
71     int32_t GetCapacityInner();
72     BatteryChargeState GetChargingStatusInner();
73     BatteryHealthState GetHealthStatusInner();
74     BatteryPluggedType GetPluggedTypeInner();
75     int32_t GetVoltageInner();
76     bool GetPresentInner();
77     std::string GetTechnologyInner();
78     int32_t GetTotalEnergyInner();
79     int32_t GetCurrentAverageInner();
80     int32_t GetNowCurrentInner();
81     int32_t GetRemainEnergyInner();
82     int32_t GetBatteryTemperatureInner();
83     BatteryCapacityLevel GetCapacityLevelInner();
84     int64_t GetRemainingChargeTimeInner();
85     BatteryError SetBatteryConfigInner(const std::string& sceneName, const std::string& value);
86     BatteryError GetBatteryConfigInner(const std::string& sceneName, std::string& result);
87     BatteryError IsBatteryConfigSupportedInner(const std::string& sceneName, bool& result);
88 public:
89     int32_t GetCapacity(int32_t& capacity) override;
90     int32_t GetChargingStatus(uint32_t& chargeState) override;
91     int32_t GetHealthStatus(uint32_t& healthState) override;
92     int32_t GetPluggedType(uint32_t& pluggedType) override;
93     int32_t GetVoltage(int32_t& voltage) override;
94     int32_t GetPresent(bool& present) override;
95     int32_t GetTechnology(std::string& technology) override;
96     int32_t GetTotalEnergy(int32_t& totalEnergy) override;
97     int32_t GetCurrentAverage(int32_t& curAverage) override;
98     int32_t GetNowCurrent(int32_t& nowCurr) override;
99     int32_t GetRemainEnergy(int32_t& remainEnergy) override;
100     int32_t GetBatteryTemperature(int32_t& temperature) override;
101     int32_t GetCapacityLevel(uint32_t& batteryCapacityLevel) override;
102     int32_t GetRemainingChargeTime(int64_t& remainTime) override;
103     int32_t SetBatteryConfig(const std::string& sceneName, const std::string& value, int32_t& batteryErr) override;
104     int32_t GetBatteryConfig(const std::string& sceneName, std::string& result, int32_t& batteryErr) override;
105     int32_t IsBatteryConfigSupported(const std::string& featureName, bool& result, int32_t& batteryErr) override;
106 
107     void InitConfig();
108     void HandleTemperature(int32_t temperature);
109     bool RegisterHdiStatusListener();
110     bool RegisterBatteryHdiCallback();
111     bool IsMockUnplugged();
112     void MockUnplugged();
113     bool IsMockCapacity();
114     void MockCapacity(int32_t capacity);
115     void MockUevent(const std::string& uevent);
116     void Reset();
117     void VibratorInit();
118 private:
119     bool Init();
120     void AddBootCommonEvents();
121     bool FillCommonEvent(std::string& ueventName, std::string& type);
122     void WakeupDevice(BatteryChargeState chargeState);
123     void RegisterBootCompletedCallback();
124     int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo& event);
125     void ConvertingEvent(const OHOS::HDI::Battery::V2_0::BatteryInfo &event);
126     void InitBatteryInfo();
127     void HandleBatteryInfo();
128     void CalculateRemainingChargeTime(int32_t capacity, BatteryChargeState chargeState);
129     void HandleCapacity(int32_t capacity, BatteryChargeState chargeState, bool isBatteryPresent);
130     void HandleCapacityExt(int32_t capacity, BatteryChargeState chargeState);
131     bool IsLastPlugged();
132     bool IsNowPlugged(BatteryPluggedType pluggedType);
133     bool IsPlugged(BatteryPluggedType pluggedType);
134     bool IsUnplugged(BatteryPluggedType pluggedType);
135     void WakeupDevice(BatteryPluggedType pluggedType);
136     bool IsCharging(BatteryChargeState chargeState);
137     bool IsInExtremePowerSaveMode();
138     void CreateShutdownGuard();
139     void LockShutdownGuard();
140     void UnlockShutdownGuard();
141 #ifdef BATTERY_MANAGER_SET_LOW_CAPACITY_THRESHOLD
142     void SetLowCapacityThreshold();
143 #endif
144     bool CapacityLevelCompare(int32_t capacity, int32_t minCapacity, int32_t maxCapacity);
145     bool ready_ { false };
146     static std::atomic_bool isBootCompleted_;
147     std::shared_mutex mutex_;
148     std::unique_ptr<BatteryNotify> batteryNotify_ { nullptr };
149     BatteryLight batteryLight_;
150     sptr<HDI::Battery::V2_0::IBatteryInterface> iBatteryInterface_ { nullptr };
151     sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> hdiServiceMgr_ { nullptr };
152     sptr<HdiServiceStatusListener::IServStatListener> hdiServStatListener_ { nullptr };
153     bool isLowPower_ { false };
154     bool isMockUnplugged_ { false };
155     bool isMockCapacity_ { false };
156     bool isMockUevent_ { false };
157     bool chargeFlag_ { false };
158     std::atomic_bool isBatteryHdiReady_ { false };
159     std::atomic_bool isCommonEventReady_ { false };
160     int32_t commEventRetryTimes_ { 0 };
161     int32_t lastCapacity_ { 0 };
162     int32_t dialogId_ { INVALID_BATT_INT_VALUE };
163     int32_t warnCapacity_ { INVALID_BATT_INT_VALUE };
164     int32_t highTemperature_ { INT32_MAX };
165     int32_t lowTemperature_ { INT32_MIN };
166     int32_t shutdownCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
167     int32_t criticalCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
168     int32_t warningCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
169     int32_t lowCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
170     int32_t normalCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
171     int32_t highCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
172     int32_t fullCapacityThreshold_ = { INVALID_BATT_INT_VALUE };
173     int64_t lastTime_ { 0 };
174     int64_t remainTime_ { 0 };
175     BatteryInfo batteryInfo_;
176     BatteryInfo lastBatteryInfo_;
177     std::mutex shutdownGuardMutex_;
178 };
179 } // namespace PowerMgr
180 } // namespace OHOS
181 
182 #endif // POWERMGR_BATTERY_SERVICE_H
183