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