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 "v2_0/ibattery_interface.h" 26 27 namespace OHOS { 28 namespace HDI { 29 namespace Battery { 30 namespace V2_0 { 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 int32_t SetConfigByPath(const std::string& path, const std::string& value); 102 int32_t GetConfigByPath(const std::string& path, std::string& result); 103 int32_t CheckPathExists(const std::string& path, bool& result); 104 105 private: 106 struct BatterySysfsInfo { 107 char* name; 108 std::string capacityPath; 109 std::string voltagePath; 110 std::string temperaturePath; 111 std::string healthStatePath; 112 std::string chargeStatePath; 113 std::string presentPath; 114 std::string technologyPath; 115 std::string chargeCounterPath; 116 std::string totalEnergyPath; 117 std::string curAveragePath; 118 std::string curNowPath; 119 std::string remainEnergyPath; 120 std::string uevent; 121 } batterySysfsInfo_; 122 123 static inline int32_t ParseInt(const char* str); 124 static inline void Trim(char* str); 125 static inline void CapacityAssigner(const char* str, struct BatterydInfo* info); 126 static inline void TotalEnergyAssigner(const char* str, struct BatterydInfo* info); 127 static inline void CurrentAverageAssigner(const char* str, struct BatterydInfo* info); 128 static inline void CurrentNowAssigner(const char* str, struct BatterydInfo* info); 129 static inline void RemainEnergyAssigner(const char* str, struct BatterydInfo* info); 130 static inline void VoltageAssigner(const char* str, struct BatterydInfo* info); 131 static inline void TemperatureAssigner(const char* str, struct BatterydInfo* info); 132 static int32_t HealthStateEnumConverter(const char* str); 133 static inline void HealthStateAssigner(const char* str, struct BatterydInfo* info); 134 static int32_t ChargeStateEnumConverter(const char* str); 135 static inline void ChargeStateAssigner(const char* str, struct BatterydInfo* info); 136 static inline void PresentAssigner(const char* str, struct BatterydInfo* info); 137 static inline void TechnologyAssigner(const char* str, struct BatterydInfo* info); 138 static inline void ChargeCounterAssigner(const char* str, struct BatterydInfo* info); 139 static int32_t ChargeTypeEumConverter(const char* str); 140 141 void TraversalNode(); 142 void CheckSubfolderNode(const std::string& path); 143 void FormatPath(std::string& path, size_t size, const char* format, const char* basePath, const char* name) const; 144 void FormatSysfsPaths(); 145 int32_t ReadSysfsFile(const char* path, char* buf, size_t size) const; 146 int32_t ReadBatterySysfsToBuff(const char* path, char* buf, size_t size) const; 147 void GetPluggedTypeName(char* buf, size_t size) const; 148 int32_t PluggedTypeEnumConverter(const char* str) const; 149 int32_t ParsePluggedMaxCurrent(int32_t* maxCurrent) const; 150 int32_t ParsePluggedMaxVoltage(int32_t* maxVoltage) const; 151 void CopyBatteryInfo(const struct BatterydInfo* info) const; 152 void CreateFile(const std::string& path, const std::string& content); 153 void CreateMockTechPath(std::string& mockTechPath); 154 void CreateMockChargerPath(std::string& mockChargerPath); 155 void CreateMockBatteryPath(std::string& mockBatteryPath); 156 void CreateMockChargeTypePath(std::string& mockChargeTypePath); 157 int32_t ReadFileToMap(std::map<std::string, std::string>& chargingLimitMap, std::string chargingLimitPath); 158 int32_t WriteConf(std::string path); 159 std::vector<std::string> nodeNames_; 160 std::map<std::string, std::string> nodeNamePathMap_; 161 std::string path_; 162 int32_t index_; 163 }; 164 } // namespace V2_0 165 } // namespace Battery 166 } // namespace HDI 167 } // namespace OHOS 168 169 #endif // POWER_SUPPLY_PROVIDER_H 170