1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#ifndef POWER_SUPPLY_PROVIDER_H 17094332d3Sopenharmony_ci#define POWER_SUPPLY_PROVIDER_H 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ci#include <cstdio> 20094332d3Sopenharmony_ci#include <cstring> 21094332d3Sopenharmony_ci#include <climits> 22094332d3Sopenharmony_ci#include <map> 23094332d3Sopenharmony_ci#include <vector> 24094332d3Sopenharmony_ci#include <mutex> 25094332d3Sopenharmony_ci#include "batteryd_api.h" 26094332d3Sopenharmony_ci#include "v2_0/ibattery_interface.h" 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_cinamespace OHOS { 29094332d3Sopenharmony_cinamespace HDI { 30094332d3Sopenharmony_cinamespace Battery { 31094332d3Sopenharmony_cinamespace V2_0 { 32094332d3Sopenharmony_ciclass PowerSupplyProvider { 33094332d3Sopenharmony_cipublic: 34094332d3Sopenharmony_ci // Keep it same as the BatteryHealthState in battery_info.h 35094332d3Sopenharmony_ci enum BatteryHealthState { 36094332d3Sopenharmony_ci BATTERY_HEALTH_UNKNOWN = 0, 37094332d3Sopenharmony_ci BATTERY_HEALTH_GOOD, 38094332d3Sopenharmony_ci BATTERY_HEALTH_OVERHEAT, 39094332d3Sopenharmony_ci BATTERY_HEALTH_OVERVOLTAGE, 40094332d3Sopenharmony_ci BATTERY_HEALTH_COLD, 41094332d3Sopenharmony_ci BATTERY_HEALTH_DEAD, 42094332d3Sopenharmony_ci BATTERY_HEALTH_RESERVED, 43094332d3Sopenharmony_ci }; 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_ci // Keep it same as the BatteryChargeState in battery_info.h 46094332d3Sopenharmony_ci enum BatteryChargeState { 47094332d3Sopenharmony_ci CHARGE_STATE_NONE = 0, 48094332d3Sopenharmony_ci CHARGE_STATE_ENABLE, 49094332d3Sopenharmony_ci CHARGE_STATE_DISABLE, 50094332d3Sopenharmony_ci CHARGE_STATE_FULL, 51094332d3Sopenharmony_ci CHARGE_STATE_RESERVED, 52094332d3Sopenharmony_ci }; 53094332d3Sopenharmony_ci 54094332d3Sopenharmony_ci // Keep it same as the BatteryPluggedType in battery_info.h 55094332d3Sopenharmony_ci enum BatteryPluggedType { 56094332d3Sopenharmony_ci PLUGGED_TYPE_NONE = 0, 57094332d3Sopenharmony_ci PLUGGED_TYPE_AC, 58094332d3Sopenharmony_ci PLUGGED_TYPE_USB, 59094332d3Sopenharmony_ci PLUGGED_TYPE_WIRELESS, 60094332d3Sopenharmony_ci PLUGGED_TYPE_BUTT 61094332d3Sopenharmony_ci }; 62094332d3Sopenharmony_ci 63094332d3Sopenharmony_ci // Keep it same as the ChargeType in charger.h 64094332d3Sopenharmony_ci enum ChargeType { 65094332d3Sopenharmony_ci CHARGE_TYPE_NONE = 0, 66094332d3Sopenharmony_ci CHARGE_TYPE_WIRED_NORMAL, 67094332d3Sopenharmony_ci CHARGE_TYPE_WIRED_QUICK, 68094332d3Sopenharmony_ci CHARGE_TYPE_WIRED_SUPER_QUICK, 69094332d3Sopenharmony_ci CHARGE_TYPE_WIRELESS_NORMAL, 70094332d3Sopenharmony_ci CHARGE_TYPE_WIRELESS_QUICK, 71094332d3Sopenharmony_ci CHARGE_TYPE_WIRELESS_SUPER_QUICK 72094332d3Sopenharmony_ci }; 73094332d3Sopenharmony_ci 74094332d3Sopenharmony_ci PowerSupplyProvider(); 75094332d3Sopenharmony_ci virtual ~PowerSupplyProvider(); 76094332d3Sopenharmony_ci 77094332d3Sopenharmony_ci int32_t InitPowerSupplySysfs(); 78094332d3Sopenharmony_ci void InitDefaultSysfs(); 79094332d3Sopenharmony_ci void InitChargerSysfs(); 80094332d3Sopenharmony_ci int32_t ParseCapacity(int32_t* capacity); 81094332d3Sopenharmony_ci int32_t ParseTotalEnergy(int32_t* capacity); 82094332d3Sopenharmony_ci int32_t ParseCurrentAverage(int32_t* curAverage); 83094332d3Sopenharmony_ci int32_t ParseCurrentNow(int32_t* curNow); 84094332d3Sopenharmony_ci int32_t ParseRemainEnergy(int32_t* remainEnergy); 85094332d3Sopenharmony_ci int32_t ParseVoltage(int32_t* voltage); 86094332d3Sopenharmony_ci int32_t ParseTemperature(int32_t* temperature); 87094332d3Sopenharmony_ci int32_t ParseHealthState(int32_t* healthState); 88094332d3Sopenharmony_ci int32_t ParsePluggedType(int32_t* pluggedType); 89094332d3Sopenharmony_ci int32_t ParseChargeState(int32_t* chargeState); 90094332d3Sopenharmony_ci int32_t ParseChargeCounter(int32_t* chargeCounter); 91094332d3Sopenharmony_ci int32_t ParsePresent(int8_t* present); 92094332d3Sopenharmony_ci int32_t ParseTechnology(std::string& technology); 93094332d3Sopenharmony_ci int32_t ParseChargeType(int32_t* chargeType, std::string& chargeTypePath); 94094332d3Sopenharmony_ci BatterydInfo GetBatteryInfo(); 95094332d3Sopenharmony_ci void ParseUeventToBatterydInfo(const char* msg, struct BatterydInfo* info); 96094332d3Sopenharmony_ci void UpdateInfoByReadSysFile(struct BatterydInfo* info); 97094332d3Sopenharmony_ci void SetSysFilePath(const std::string& path); 98094332d3Sopenharmony_ci void InitBatteryPath(); 99094332d3Sopenharmony_ci int32_t SetChargingLimit(const std::vector<ChargingLimit>& chargingLimit, 100094332d3Sopenharmony_ci std::string& currentPath, std::string& voltagePath); 101094332d3Sopenharmony_ci 102094332d3Sopenharmony_ci int32_t SetConfigByPath(const std::string& path, const std::string& value); 103094332d3Sopenharmony_ci int32_t GetConfigByPath(const std::string& path, std::string& result); 104094332d3Sopenharmony_ci int32_t CheckPathExists(const std::string& path, bool& result); 105094332d3Sopenharmony_ci 106094332d3Sopenharmony_ciprivate: 107094332d3Sopenharmony_ci struct BatterySysfsInfo { 108094332d3Sopenharmony_ci char* name = nullptr; 109094332d3Sopenharmony_ci std::string capacityPath; 110094332d3Sopenharmony_ci std::string voltagePath; 111094332d3Sopenharmony_ci std::string temperaturePath; 112094332d3Sopenharmony_ci std::string healthStatePath; 113094332d3Sopenharmony_ci std::string chargeStatePath; 114094332d3Sopenharmony_ci std::string presentPath; 115094332d3Sopenharmony_ci std::string technologyPath; 116094332d3Sopenharmony_ci std::string chargeCounterPath; 117094332d3Sopenharmony_ci std::string totalEnergyPath; 118094332d3Sopenharmony_ci std::string curAveragePath; 119094332d3Sopenharmony_ci std::string curNowPath; 120094332d3Sopenharmony_ci std::string remainEnergyPath; 121094332d3Sopenharmony_ci std::string uevent; 122094332d3Sopenharmony_ci } batterySysfsInfo_; 123094332d3Sopenharmony_ci 124094332d3Sopenharmony_ci static inline int32_t ParseInt(const char* str); 125094332d3Sopenharmony_ci static inline void Trim(char* str); 126094332d3Sopenharmony_ci static inline void CapacityAssigner(const char* str, struct BatterydInfo* info); 127094332d3Sopenharmony_ci static inline void TotalEnergyAssigner(const char* str, struct BatterydInfo* info); 128094332d3Sopenharmony_ci static inline void CurrentAverageAssigner(const char* str, struct BatterydInfo* info); 129094332d3Sopenharmony_ci static inline void CurrentNowAssigner(const char* str, struct BatterydInfo* info); 130094332d3Sopenharmony_ci static inline void RemainEnergyAssigner(const char* str, struct BatterydInfo* info); 131094332d3Sopenharmony_ci static inline void VoltageAssigner(const char* str, struct BatterydInfo* info); 132094332d3Sopenharmony_ci static inline void TemperatureAssigner(const char* str, struct BatterydInfo* info); 133094332d3Sopenharmony_ci static int32_t HealthStateEnumConverter(const char* str); 134094332d3Sopenharmony_ci static inline void HealthStateAssigner(const char* str, struct BatterydInfo* info); 135094332d3Sopenharmony_ci static int32_t ChargeStateEnumConverter(const char* str); 136094332d3Sopenharmony_ci static inline void ChargeStateAssigner(const char* str, struct BatterydInfo* info); 137094332d3Sopenharmony_ci static inline void PresentAssigner(const char* str, struct BatterydInfo* info); 138094332d3Sopenharmony_ci static inline void TechnologyAssigner(const char* str, struct BatterydInfo* info); 139094332d3Sopenharmony_ci static inline void ChargeCounterAssigner(const char* str, struct BatterydInfo* info); 140094332d3Sopenharmony_ci static int32_t ChargeTypeEumConverter(const char* str); 141094332d3Sopenharmony_ci 142094332d3Sopenharmony_ci void TraversalNode(); 143094332d3Sopenharmony_ci void CheckSubfolderNode(const std::string& path); 144094332d3Sopenharmony_ci void FormatPath(std::string& path, size_t size, const char* format, const char* basePath, const char* name) const; 145094332d3Sopenharmony_ci void FormatSysfsPaths(); 146094332d3Sopenharmony_ci int32_t ReadSysfsFile(const char* path, char* buf, size_t size); 147094332d3Sopenharmony_ci int32_t ReadBatterySysfsToBuff(const char* path, char* buf, size_t size); 148094332d3Sopenharmony_ci void GetPluggedTypeName(char* buf, size_t size); 149094332d3Sopenharmony_ci int32_t PluggedTypeEnumConverter(const char* str) const; 150094332d3Sopenharmony_ci int32_t ParsePluggedMaxCurrent(int32_t* maxCurrent); 151094332d3Sopenharmony_ci int32_t ParsePluggedMaxVoltage(int32_t* maxVoltage); 152094332d3Sopenharmony_ci void CopyBatteryInfo(const struct BatterydInfo* info) const; 153094332d3Sopenharmony_ci void CreateFile(const std::string& path, const std::string& content); 154094332d3Sopenharmony_ci void CreateMockTechPath(std::string& mockTechPath); 155094332d3Sopenharmony_ci void CreateMockChargerPath(std::string& mockChargerPath); 156094332d3Sopenharmony_ci void CreateMockBatteryPath(std::string& mockBatteryPath); 157094332d3Sopenharmony_ci void CreateMockChargeTypePath(std::string& mockChargeTypePath); 158094332d3Sopenharmony_ci int32_t ReadFileToMap(std::map<std::string, std::string>& chargingLimitMap, std::string chargingLimitPath); 159094332d3Sopenharmony_ci int32_t WriteConf(std::string path); 160094332d3Sopenharmony_ci std::vector<std::string> nodeNames_; 161094332d3Sopenharmony_ci std::map<std::string, std::string> nodeNamePathMap_; 162094332d3Sopenharmony_ci std::map<std::string, int32_t> nodeCacheFiles_; 163094332d3Sopenharmony_ci std::string path_; 164094332d3Sopenharmony_ci int32_t index_; 165094332d3Sopenharmony_ci std::mutex mutex_; 166094332d3Sopenharmony_ci}; 167094332d3Sopenharmony_ci} // namespace V2_0 168094332d3Sopenharmony_ci} // namespace Battery 169094332d3Sopenharmony_ci} // namespace HDI 170094332d3Sopenharmony_ci} // namespace OHOS 171094332d3Sopenharmony_ci 172094332d3Sopenharmony_ci#endif // POWER_SUPPLY_PROVIDER_H 173