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 <limits.h> 22 #include <map> 23 #include <vector> 24 #include "batteryd_api.h" 25 26 namespace OHOS { 27 namespace HDI { 28 namespace Battery { 29 namespace V1_0 { 30 const int MAX_SYSFS_SIZE = 64; 31 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 PowerSupplyProvider(); ~PowerSupplyProvider()64 virtual ~PowerSupplyProvider() {} 65 66 int32_t InitPowerSupplySysfs(void); 67 void InitDefaultSysfs(void); 68 int32_t ParseCapacity(int32_t* capacity) const; 69 int32_t ParseTotalEnergy(int32_t* capacity) const; 70 int32_t ParseCurrentAverage(int32_t* curAverage) const; 71 int32_t ParseCurrentNow(int32_t* curNow) const; 72 int32_t ParseRemainEnergy(int32_t* remainEnergy) const; 73 int32_t ParseVoltage(int32_t* voltage) const; 74 int32_t ParseTemperature(int32_t* temperature) const; 75 int32_t ParseHealthState(int32_t* healthState) const; 76 int32_t ParsePluggedType(int32_t* pluggedType) const; 77 int32_t ParseChargeState(int32_t* chargeState) const; 78 int32_t ParseChargeCounter(int32_t* chargeCounter) const; 79 int32_t ParsePresent(int8_t* present) const; 80 int32_t ParseTechnology(std::string& technology) const; 81 BatterydInfo GetBatteryInfo() const; 82 void ParseUeventToBatterydInfo(const char* msg, struct BatterydInfo* info) const; 83 void UpdateInfoByReadSysFile(struct BatterydInfo* info) const; 84 void SetSysFilePath(const std::string& path); 85 void InitBatteryPath(); 86 87 private: 88 struct PowerSupplySysfsInfo { 89 char* name; 90 std::string typePath; 91 std::string onlinePath; 92 std::string currentMaxPath; 93 std::string voltageMaxPath; 94 } powerSupplySysfsInfos_; // [MAX_SYSFS_SIZE]; 95 96 struct BatterySysfsInfo { 97 char* name; 98 std::string capacityPath; 99 std::string voltagePath; 100 std::string temperaturePath; 101 std::string healthStatePath; 102 std::string chargeStatePath; 103 std::string presentPath; 104 std::string technologyPath; 105 std::string chargeCounterPath; 106 std::string totalEnergyPath; 107 std::string curAveragePath; 108 std::string curNowPath; 109 std::string remainEnergyPath; 110 } batterySysfsInfo_; 111 112 static inline int32_t ParseInt(const char* str); 113 inline void Trim(char* str) const; 114 static inline void CapacityAssigner(const char* str, struct BatterydInfo* info); 115 static inline void TotalEnergyAssigner(const char* str, struct BatterydInfo* info); 116 static inline void CurrentAverageAssigner(const char* str, struct BatterydInfo* info); 117 static inline void CurrentNowAssigner(const char* str, struct BatterydInfo* info); 118 static inline void RemainEnergyAssigner(const char* str, struct BatterydInfo* info); 119 static inline void VoltageAssigner(const char* str, struct BatterydInfo* info); 120 static inline void TemperatureAssigner(const char* str, struct BatterydInfo* info); 121 static int32_t HealthStateEnumConverter(const char* str); 122 static inline void HealthStateAssigner(const char* str, struct BatterydInfo* info); 123 static int32_t ChargeStateEnumConverter(const char* str); 124 static inline void ChargeStateAssigner(const char* str, struct BatterydInfo* info); 125 static inline void PresentAssigner(const char* str, struct BatterydInfo* info); 126 static inline void TechnologyAssigner(const char* str, struct BatterydInfo* info); 127 static inline void ChargeCounterAssigner(const char* str, struct BatterydInfo* info); 128 129 void TraversalNode(); 130 void CheckSubfolderNode(const std::string& path); 131 void FormatPath(std::string& path, size_t size, const char* format, const char* basePath, const char* name) const; 132 void FormatSysfsPaths(struct PowerSupplySysfsInfo* info); 133 int32_t ReadSysfsFile(const char* path, char* buf, size_t size) const; 134 int32_t ReadBatterySysfsToBuff(const char* path, char* buf, size_t size) const; 135 void GetPluggedTypeName(char* buf, size_t size) const; 136 int32_t PluggedTypeEnumConverter(const char* str) const; 137 int32_t ParsePluggedMaxCurrent(int32_t* maxCurrent) const; 138 int32_t ParsePluggedMaxVoltage(int32_t* maxVoltage) const; 139 void CopyBatteryInfo(const struct BatterydInfo* info) const; 140 std::string CreateFile(std::string path, std::string content); 141 std::vector<std::string> filenodeName_; 142 std::map<std::string, std::string> nodeInfo_; 143 std::string path_; 144 int32_t index_; 145 }; 146 } // namespace V1_0 147 } // namespace Battery 148 } // namespace HDI 149 } // namespace OHOS 150 151 #endif // POWER_SUPPLY_PROVIDER_H 152