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