• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 POWER_SUPPLY_PROVIDER_H
17 #define POWER_SUPPLY_PROVIDER_H
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <climits>
22 #include <map>
23 #include <vector>
24 #include "batteryd_api.h"
25 #include "v1_2/ibattery_interface.h"
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace Battery {
30 namespace V1_2 {
31 class PowerSupplyProvider {
32 public:
33     // Keep it same as the BatteryHealthState in battery_info.h
34     enum BatteryHealthState {
35         BATTERY_HEALTH_UNKNOWN = 0,
36         BATTERY_HEALTH_GOOD,
37         BATTERY_HEALTH_OVERHEAT,
38         BATTERY_HEALTH_OVERVOLTAGE,
39         BATTERY_HEALTH_COLD,
40         BATTERY_HEALTH_DEAD,
41         BATTERY_HEALTH_RESERVED,
42     };
43 
44     // Keep it same as the BatteryChargeState in battery_info.h
45     enum BatteryChargeState {
46         CHARGE_STATE_NONE = 0,
47         CHARGE_STATE_ENABLE,
48         CHARGE_STATE_DISABLE,
49         CHARGE_STATE_FULL,
50         CHARGE_STATE_RESERVED,
51     };
52 
53     // Keep it same as the BatteryPluggedType in battery_info.h
54     enum BatteryPluggedType {
55         PLUGGED_TYPE_NONE = 0,
56         PLUGGED_TYPE_AC,
57         PLUGGED_TYPE_USB,
58         PLUGGED_TYPE_WIRELESS,
59         PLUGGED_TYPE_BUTT
60     };
61 
62     // Keep it same as the ChargeType in charger.h
63     enum ChargeType {
64         CHARGE_TYPE_NONE = 0,
65         CHARGE_TYPE_WIRED_NORMAL,
66         CHARGE_TYPE_WIRED_QUICK,
67         CHARGE_TYPE_WIRED_SUPER_QUICK,
68         CHARGE_TYPE_WIRELESS_NORMAL,
69         CHARGE_TYPE_WIRELESS_QUICK,
70         CHARGE_TYPE_WIRELESS_SUPER_QUICK
71     };
72 
73     PowerSupplyProvider();
74     virtual ~PowerSupplyProvider() = default;
75 
76     int32_t InitPowerSupplySysfs();
77     void InitDefaultSysfs();
78     void InitChargerSysfs();
79     int32_t ParseCapacity(int32_t* capacity) const;
80     int32_t ParseTotalEnergy(int32_t* capacity) const;
81     int32_t ParseCurrentAverage(int32_t* curAverage) const;
82     int32_t ParseCurrentNow(int32_t* curNow) const;
83     int32_t ParseRemainEnergy(int32_t* remainEnergy) const;
84     int32_t ParseVoltage(int32_t* voltage) const;
85     int32_t ParseTemperature(int32_t* temperature) const;
86     int32_t ParseHealthState(int32_t* healthState) const;
87     int32_t ParsePluggedType(int32_t* pluggedType) const;
88     int32_t ParseChargeState(int32_t* chargeState) const;
89     int32_t ParseChargeCounter(int32_t* chargeCounter) const;
90     int32_t ParsePresent(int8_t* present) const;
91     int32_t ParseTechnology(std::string& technology) const;
92     int32_t ParseChargeType(int32_t* chargeType, std::string& chargeTypePath) const;
93     BatterydInfo GetBatteryInfo() const;
94     void ParseUeventToBatterydInfo(const char* msg, struct BatterydInfo* info) const;
95     void UpdateInfoByReadSysFile(struct BatterydInfo* info) const;
96     void SetSysFilePath(const std::string& path);
97     void InitBatteryPath();
98     int32_t SetChargingLimit(const std::vector<ChargingLimit>& chargingLimit,
99         std::string& currentPath, std::string& voltagePath);
100 
101 private:
102     struct BatterySysfsInfo {
103         char* name;
104         std::string capacityPath;
105         std::string voltagePath;
106         std::string temperaturePath;
107         std::string healthStatePath;
108         std::string chargeStatePath;
109         std::string presentPath;
110         std::string technologyPath;
111         std::string chargeCounterPath;
112         std::string totalEnergyPath;
113         std::string curAveragePath;
114         std::string curNowPath;
115         std::string remainEnergyPath;
116     } batterySysfsInfo_;
117 
118     static inline int32_t ParseInt(const char* str);
119     static inline void Trim(char* str);
120     static inline void CapacityAssigner(const char* str, struct BatterydInfo* info);
121     static inline void TotalEnergyAssigner(const char* str, struct BatterydInfo* info);
122     static inline void CurrentAverageAssigner(const char* str, struct BatterydInfo* info);
123     static inline void CurrentNowAssigner(const char* str, struct BatterydInfo* info);
124     static inline void RemainEnergyAssigner(const char* str, struct BatterydInfo* info);
125     static inline void VoltageAssigner(const char* str, struct BatterydInfo* info);
126     static inline void TemperatureAssigner(const char* str, struct BatterydInfo* info);
127     static int32_t HealthStateEnumConverter(const char* str);
128     static inline void HealthStateAssigner(const char* str, struct BatterydInfo* info);
129     static int32_t ChargeStateEnumConverter(const char* str);
130     static inline void ChargeStateAssigner(const char* str, struct BatterydInfo* info);
131     static inline void PresentAssigner(const char* str, struct BatterydInfo* info);
132     static inline void TechnologyAssigner(const char* str, struct BatterydInfo* info);
133     static inline void ChargeCounterAssigner(const char* str, struct BatterydInfo* info);
134     static int32_t ChargeTypeEumConverter(const char* str);
135 
136     void TraversalNode();
137     void CheckSubfolderNode(const std::string& path);
138     void FormatPath(std::string& path, size_t size, const char* format, const char* basePath, const char* name) const;
139     void FormatSysfsPaths();
140     int32_t ReadSysfsFile(const char* path, char* buf, size_t size) const;
141     int32_t ReadBatterySysfsToBuff(const char* path, char* buf, size_t size) const;
142     void GetPluggedTypeName(char* buf, size_t size) const;
143     int32_t PluggedTypeEnumConverter(const char* str) const;
144     int32_t ParsePluggedMaxCurrent(int32_t* maxCurrent) const;
145     int32_t ParsePluggedMaxVoltage(int32_t* maxVoltage) const;
146     void CopyBatteryInfo(const struct BatterydInfo* info) const;
147     void CreateFile(const std::string& path, const std::string& content);
148     void CreateMockTechPath(std::string& mockTechPath);
149     void CreateMockChargerPath(std::string& mockChargerPath);
150     void CreateMockBatteryPath(std::string& mockBatteryPath);
151     void CreateMockChargeTypePath(std::string& mockChargeTypePath);
152     int32_t ReadFileToMap(std::map<std::string, std::string>& chargingLimitMap, std::string chargingLimitPath);
153     int32_t WriteChargingLimit(std::string chargingLimitPath, std::string& configStr);
154     std::vector<std::string> nodeNames_;
155     std::map<std::string, std::string> nodeNamePathMap_;
156     std::string path_;
157     int32_t index_;
158 };
159 }  // namespace V1_2
160 }  // namespace Battery
161 }  // namespace HDI
162 }  // namespace OHOS
163 
164 #endif // POWER_SUPPLY_PROVIDER_H
165