• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "power_supply_provider.h"
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <fstream>
20 #include <securec.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include "battery_log.h"
25 #include "battery_config.h"
26 #include "osal_mem.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace Battery {
31 namespace V2_0 {
32 namespace {
33 constexpr int32_t MAX_SYSFS_SIZE = 64;
34 constexpr int32_t MAX_BUFF_SIZE = 128;
35 constexpr int32_t INVALID_BATT_INT_VALUE = -1;
36 constexpr int32_t STR_TO_LONG_LEN = 10;
37 constexpr int32_t UVOL_TO_MVOL = 1000;
38 constexpr int32_t MKDIR_WAIT_TIME = 1;
39 constexpr int32_t NUM_ZERO = 0;
40 const std::string POWER_SUPPLY_BASE_PATH = "/sys/class/power_supply";
41 const std::string MOCK_POWER_SUPPLY_BASE_PATH = "/data/service/el0/battery";
42 const std::string POWER_SUPPLY_BATTERY = "Battery";
43 const std::string BATTERY_KEY_CAPACITY = "POWER_SUPPLY_CAPACITY=";
44 const std::string BATTERY_KEY_VOLTAGE = "POWER_SUPPLY_VOLTAGE_NOW=";
45 const std::string BATTERY_KEY_TEMPERATURE = "POWER_SUPPLY_TEMP=";
46 const std::string BATTERY_KEY_HEALTH = "POWER_SUPPLY_HEALTH=";
47 const std::string BATTERY_KEY_CHARGE_STATUS = "POWER_SUPPLY_STATUS=";
48 const std::string BATTERY_KEY_PRESENT = "POWER_SUPPLY_PRESENT=";
49 const std::string BATTERY_KEY_TECHNOLOGY = "POWER_SUPPLY_TECHNOLOGY=";
50 const std::string BATTERY_KEY_CHARGE_COUNTER = "POWER_SUPPLY_CHARGE_COUNTER=";
51 const std::string BATTERY_KEY_TOTAL_ENERGY = "POWER_SUPPLY_TOTAL_ENERGY=";
52 const std::string BATTERY_KEY_REMAIN_ENERGY = "POWER_SUPPLY_REMAIN_ENERGY=";
53 const std::string BATTERY_KEY_CURRENT_AVERAGE = "POWER_SUPPLY_CURRENT_AVERAGE=";
54 const std::string BATTERY_KEY_CURRENT_NOW = "POWER_SUPPLY_CURRENT_NOW=";
55 const std::string INVALID_STRING_VALUE = "invalid";
56 const std::string BATTERY_NODE_PATH = "battery";
57 }
58 
59 BatterydInfo g_batteryInfo;
60 
61 struct StringEnumMap {
62     const char* str;
63     int32_t enumVal;
64 };
65 
66 struct BatteryAssigner {
67     const char* prefix;
68     const size_t prefixLen;
69     void (*Assigner)(const char*, struct BatterydInfo*);
70 };
71 
PowerSupplyProvider()72 PowerSupplyProvider::PowerSupplyProvider()
73 {
74     path_ = POWER_SUPPLY_BASE_PATH;
75     index_ = 0;
76 }
77 
ParseInt(const char * str)78 inline int32_t PowerSupplyProvider::ParseInt(const char* str)
79 {
80     return static_cast<int32_t>(strtol(str, nullptr, STR_TO_LONG_LEN));
81 }
82 
Trim(char * str)83 inline void PowerSupplyProvider::Trim(char* str)
84 {
85     if (str == nullptr) {
86         return;
87     }
88 
89     size_t strc = strcspn(str, "\n");
90     if (strc >= strlen(str)) {
91         return;
92     }
93 
94     str[strc] = 0;
95 }
96 
CapacityAssigner(const char * str,struct BatterydInfo * info)97 inline void PowerSupplyProvider::CapacityAssigner(const char* str, struct BatterydInfo* info)
98 {
99     info->capacity_ = ParseInt(str); // default in percent format
100 }
101 
TotalEnergyAssigner(const char * str,struct BatterydInfo * info)102 inline void PowerSupplyProvider::TotalEnergyAssigner(const char* str, struct BatterydInfo* info)
103 {
104     info->totalEnergy_ = ParseInt(str);
105 }
106 
RemainEnergyAssigner(const char * str,struct BatterydInfo * info)107 inline void PowerSupplyProvider::RemainEnergyAssigner(const char* str, struct BatterydInfo* info)
108 {
109     info->remainEnergy_ = ParseInt(str);
110 }
111 
VoltageAssigner(const char * str,struct BatterydInfo * info)112 inline void PowerSupplyProvider::VoltageAssigner(const char* str, struct BatterydInfo* info)
113 {
114     info->voltage_ = ParseInt(str) / UVOL_TO_MVOL; // convert to millivolt(mV) format
115 }
116 
TemperatureAssigner(const char * str,struct BatterydInfo * info)117 inline void PowerSupplyProvider::TemperatureAssigner(const char* str, struct BatterydInfo* info)
118 {
119     info->temperature_ = ParseInt(str);
120 }
121 
HealthStateEnumConverter(const char * str)122 int32_t PowerSupplyProvider::HealthStateEnumConverter(const char* str)
123 {
124     struct StringEnumMap healthStateEnumMap[] = {
125         { "Good", BATTERY_HEALTH_GOOD },
126         { "Cold", BATTERY_HEALTH_COLD },
127         { "Warm", BATTERY_HEALTH_GOOD }, // JEITA specification
128         { "Cool", BATTERY_HEALTH_GOOD }, // JEITA specification
129         { "Hot", BATTERY_HEALTH_OVERHEAT }, // JEITA specification
130         { "Overheat", BATTERY_HEALTH_OVERHEAT },
131         { "Over voltage", BATTERY_HEALTH_OVERVOLTAGE },
132         { "Dead", BATTERY_HEALTH_DEAD },
133         { "Unknown", BATTERY_HEALTH_UNKNOWN },
134         { "Unspecified failure", BATTERY_HEALTH_UNKNOWN },
135         { nullptr, BATTERY_HEALTH_UNKNOWN },
136     };
137 
138     for (int32_t i = 0; healthStateEnumMap[i].str; ++i) {
139         if (strcmp(str, healthStateEnumMap[i].str) == 0) {
140             return healthStateEnumMap[i].enumVal;
141         }
142     }
143 
144     return BATTERY_HEALTH_UNKNOWN;
145 }
146 
HealthStateAssigner(const char * str,struct BatterydInfo * info)147 inline void PowerSupplyProvider::HealthStateAssigner(const char* str, struct BatterydInfo* info)
148 {
149     info->healthState_ = HealthStateEnumConverter(str);
150 }
151 
ChargeStateEnumConverter(const char * str)152 int32_t PowerSupplyProvider::ChargeStateEnumConverter(const char* str)
153 {
154     struct StringEnumMap chargeStateEnumMap[] = {
155         { "Discharging", CHARGE_STATE_NONE },
156         { "Charging", CHARGE_STATE_ENABLE },
157         { "Full", CHARGE_STATE_FULL },
158         { "Not charging", CHARGE_STATE_DISABLE },
159         { "Unknown", CHARGE_STATE_RESERVED },
160         { nullptr, CHARGE_STATE_RESERVED },
161     };
162 
163     for (int32_t i = 0; chargeStateEnumMap[i].str; ++i) {
164         if (strcmp(str, chargeStateEnumMap[i].str) == 0) {
165             return chargeStateEnumMap[i].enumVal;
166         }
167     }
168 
169     return CHARGE_STATE_RESERVED;
170 }
171 
ChargeTypeEumConverter(const char * str)172 int32_t PowerSupplyProvider::ChargeTypeEumConverter(const char* str)
173 {
174     struct StringEnumMap chargeTypeEnumMap[] = {
175         { "0", CHARGE_TYPE_NONE },
176         { "1", CHARGE_TYPE_WIRED_NORMAL },
177         { "2", CHARGE_TYPE_WIRED_QUICK },
178         { "3", CHARGE_TYPE_WIRED_SUPER_QUICK },
179         { "4", CHARGE_TYPE_WIRELESS_NORMAL },
180         { "5", CHARGE_TYPE_WIRELESS_QUICK },
181         { "6", CHARGE_TYPE_WIRELESS_SUPER_QUICK },
182         { nullptr, CHARGE_TYPE_NONE },
183     };
184 
185     for (int32_t i = 0; chargeTypeEnumMap[i].str; ++i) {
186         if (strcmp(str, chargeTypeEnumMap[i].str) == 0) {
187             return chargeTypeEnumMap[i].enumVal;
188         }
189     }
190 
191     return CHARGE_TYPE_NONE;
192 }
193 
ChargeStateAssigner(const char * str,struct BatterydInfo * info)194 inline void PowerSupplyProvider::ChargeStateAssigner(const char* str, struct BatterydInfo* info)
195 {
196     info->chargeState_ = ChargeStateEnumConverter(str);
197 }
198 
PresentAssigner(const char * str,struct BatterydInfo * info)199 inline void PowerSupplyProvider::PresentAssigner(const char* str, struct BatterydInfo* info)
200 {
201     info->present_ = static_cast<int8_t>(ParseInt(str));
202 }
203 
TechnologyAssigner(const char * str,struct BatterydInfo * info)204 inline void PowerSupplyProvider::TechnologyAssigner(const char* str, struct BatterydInfo* info)
205 {
206     info->technology_ = str;
207 }
208 
ChargeCounterAssigner(const char * str,struct BatterydInfo * info)209 inline void PowerSupplyProvider::ChargeCounterAssigner(const char* str, struct BatterydInfo* info)
210 {
211     info->chargeCounter_ = ParseInt(str);
212 }
213 
CurrentAverageAssigner(const char * str,struct BatterydInfo * info)214 inline void PowerSupplyProvider::CurrentAverageAssigner(const char* str, struct BatterydInfo* info)
215 {
216     info->curAverage_ = ParseInt(str);
217 }
218 
CurrentNowAssigner(const char * str,struct BatterydInfo * info)219 inline void PowerSupplyProvider::CurrentNowAssigner(const char* str, struct BatterydInfo* info)
220 {
221     info->curNow_ = ParseInt(str);
222 }
223 
FormatPath(std::string & path,size_t size,const char * format,const char * basePath,const char * name) const224 void PowerSupplyProvider::FormatPath(std::string& path,
225     size_t size, const char* format, const char* basePath, const char* name) const
226 {
227     char buff[PATH_MAX] = {0};
228     if (strcpy_s(buff, PATH_MAX, path.c_str()) != EOK) {
229         BATTERY_HILOGW(FEATURE_BATT_INFO, "failed to copy path of %{public}s", name);
230         return;
231     }
232 
233     if (snprintf_s(buff, PATH_MAX, size - 1, format, basePath, name) == -1) {
234         BATTERY_HILOGW(FEATURE_BATT_INFO, "failed to format path of %{public}s", name);
235         return;
236     }
237     path.assign(buff, strlen(buff));
238 }
239 
FormatSysfsPaths()240 void PowerSupplyProvider::FormatSysfsPaths()
241 {
242     // Format paths for power supply types
243     FormatPath(batterySysfsInfo_.capacityPath, PATH_MAX, "%s/%s/capacity", path_.c_str(),
244                nodeNamePathMap_["capacity"].c_str());
245     FormatPath(batterySysfsInfo_.voltagePath, PATH_MAX, "%s/%s/voltage_now", path_.c_str(),
246                nodeNamePathMap_["voltage_now"].c_str());
247     FormatPath(batterySysfsInfo_.temperaturePath, PATH_MAX, "%s/%s/temp", path_.c_str(),
248                nodeNamePathMap_["temp"].c_str());
249     FormatPath(batterySysfsInfo_.healthStatePath, PATH_MAX, "%s/%s/health", path_.c_str(),
250                nodeNamePathMap_["health"].c_str());
251     FormatPath(batterySysfsInfo_.chargeStatePath, PATH_MAX, "%s/%s/status", path_.c_str(),
252                nodeNamePathMap_["status"].c_str());
253     FormatPath(batterySysfsInfo_.presentPath, PATH_MAX, "%s/%s/present", path_.c_str(),
254                nodeNamePathMap_["present"].c_str());
255     FormatPath(batterySysfsInfo_.chargeCounterPath, PATH_MAX, "%s/%s/charge_counter", path_.c_str(),
256                nodeNamePathMap_["charge_counter"].c_str());
257     FormatPath(batterySysfsInfo_.technologyPath, PATH_MAX, "%s/%s/technology", path_.c_str(),
258                nodeNamePathMap_["technology"].c_str());
259     FormatPath(batterySysfsInfo_.totalEnergyPath, PATH_MAX, "%s/%s/charge_full", path_.c_str(),
260                nodeNamePathMap_["charge_full"].c_str());
261     FormatPath(batterySysfsInfo_.curAveragePath, PATH_MAX, "%s/%s/current_avg", path_.c_str(),
262                nodeNamePathMap_["current_avg"].c_str());
263     FormatPath(batterySysfsInfo_.curNowPath, PATH_MAX, "%s/%s/current_now", path_.c_str(),
264                nodeNamePathMap_["current_now"].c_str());
265     FormatPath(batterySysfsInfo_.remainEnergyPath, PATH_MAX, "%s/%s/charge_now", path_.c_str(),
266                nodeNamePathMap_["charge_now"].c_str());
267 }
268 
ReadSysfsFile(const char * path,char * buf,size_t size) const269 int32_t PowerSupplyProvider::ReadSysfsFile(const char* path, char* buf, size_t size) const
270 {
271     int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
272     if (fd < NUM_ZERO) {
273         BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to open file");
274         return HDF_ERR_IO;
275     }
276 
277     size_t readSize = read(fd, buf, size - 1);
278     buf[readSize] = '\0';
279     Trim(buf);
280     close(fd);
281 
282     return HDF_SUCCESS;
283 }
284 
ReadBatterySysfsToBuff(const char * path,char * buf,size_t size) const285 int32_t PowerSupplyProvider::ReadBatterySysfsToBuff(const char* path, char* buf, size_t size) const
286 {
287     int32_t ret = ReadSysfsFile(path, buf, size);
288     if (ret != HDF_SUCCESS) {
289         BATTERY_HILOGW(FEATURE_BATT_INFO, "read path failed, ret: %{public}d", ret);
290         return ret;
291     }
292 
293     return HDF_SUCCESS;
294 }
295 
GetPluggedTypeName(char * buf,size_t size) const296 void PowerSupplyProvider::GetPluggedTypeName(char* buf, size_t size) const
297 {
298     std::string onlineNode = "USB";
299     int32_t ret;
300     int32_t online;
301     std::string onlinePath = path_ + "/" + onlineNode + "/" + "online";
302     ret = ReadSysfsFile(onlinePath.c_str(), buf, size);
303     online = ParseInt(buf);
304     auto iter = nodeNames_.begin();
305     while (!online && iter != nodeNames_.end()) {
306         if (*iter == "USB") {
307             iter++;
308             continue;
309         }
310         onlinePath = path_ + "/" + *iter + "/" + "online";
311         ret = ReadSysfsFile(onlinePath.c_str(), buf, size);
312         if (ret != HDF_SUCCESS) {
313             BATTERY_HILOGD(FEATURE_BATT_INFO, "read online path failed in loop, ret: %{public}d", ret);
314         }
315         online = ParseInt(buf);
316         if (online) {
317             onlineNode = *iter;
318             break;
319         }
320         iter++;
321     }
322 
323     if (ret != HDF_SUCCESS) {
324         BATTERY_HILOGW(FEATURE_BATT_INFO, "read online path failed, ret: %{public}d", ret);
325         return;
326     }
327 
328     if (!online) {
329         BATTERY_HILOGW(FEATURE_BATT_INFO, "charger is not online, so no type return");
330         return;
331     }
332 
333     std::string typeNode = onlineNode;
334     std::string typePath = path_ + "/" + typeNode + "/" + "type";
335     ret = ReadSysfsFile(typePath.c_str(), buf, size);
336     if (ret != HDF_SUCCESS) {
337         BATTERY_HILOGW(FEATURE_BATT_INFO, "read type path failed, ret: %{public}d", ret);
338         return;
339     }
340     Trim(buf);
341 }
342 
PluggedTypeEnumConverter(const char * str) const343 int32_t PowerSupplyProvider::PluggedTypeEnumConverter(const char* str) const
344 {
345     struct StringEnumMap pluggedTypeEnumMap[] = {
346         { "USB", PLUGGED_TYPE_USB },
347         { "USB_PD_DRP", PLUGGED_TYPE_USB },
348         { "Wireless", PLUGGED_TYPE_WIRELESS },
349         { "Mains", PLUGGED_TYPE_AC },
350         { "UPS", PLUGGED_TYPE_AC },
351         { "USB_ACA", PLUGGED_TYPE_AC },
352         { "USB_C", PLUGGED_TYPE_AC },
353         { "USB_CDP", PLUGGED_TYPE_AC },
354         { "USB_DCP", PLUGGED_TYPE_AC },
355         { "USB_HVDCP", PLUGGED_TYPE_AC },
356         { "USB_PD", PLUGGED_TYPE_AC },
357         { "Unknown", PLUGGED_TYPE_BUTT },
358         { nullptr, PLUGGED_TYPE_BUTT },
359     };
360 
361     for (int32_t i = 0; pluggedTypeEnumMap[i].str; ++i) {
362         if (strcmp(str, pluggedTypeEnumMap[i].str) == 0) {
363             return pluggedTypeEnumMap[i].enumVal;
364         }
365     }
366     return PLUGGED_TYPE_BUTT;
367 }
368 
ParsePluggedMaxCurrent(int32_t * maxCurrent) const369 int32_t PowerSupplyProvider::ParsePluggedMaxCurrent(int32_t* maxCurrent) const
370 {
371     char buf[MAX_BUFF_SIZE] = {0};
372     GetPluggedTypeName(buf, sizeof(buf));
373     std::string currentMaxNode = POWER_SUPPLY_BATTERY;
374 
375     const auto& item = nodeNamePathMap_.find("current_max");
376     if (item != nodeNamePathMap_.end()) {
377         currentMaxNode = item->second;
378     }
379 
380     std::string currentMaxPath = POWER_SUPPLY_BASE_PATH + "/" + currentMaxNode + "/" + "current_max";
381     int32_t ret = ReadBatterySysfsToBuff(currentMaxPath.c_str(), buf, sizeof(buf));
382     if (ret != HDF_SUCCESS) {
383         return ret;
384     }
385     int32_t value = ParseInt(buf);
386     *maxCurrent = value;
387 
388     return HDF_SUCCESS;
389 }
390 
ParsePluggedMaxVoltage(int32_t * maxVoltage) const391 int32_t PowerSupplyProvider::ParsePluggedMaxVoltage(int32_t* maxVoltage) const
392 {
393     char buf[MAX_BUFF_SIZE] = {0};
394     GetPluggedTypeName(buf, sizeof(buf));
395     std::string voltageMaxNode = POWER_SUPPLY_BATTERY;
396 
397     const auto& item = nodeNamePathMap_.find("voltage_max");
398     if (item != nodeNamePathMap_.end()) {
399         voltageMaxNode = item->second;
400     }
401 
402     std::string voltageMaxPath = POWER_SUPPLY_BASE_PATH + "/" + voltageMaxNode + "/" + "voltage_max";
403     int32_t ret = ReadBatterySysfsToBuff(voltageMaxPath.c_str(), buf, sizeof(buf));
404     if (ret != HDF_SUCCESS) {
405         return ret;
406     }
407     int32_t value = ParseInt(buf);
408     *maxVoltage = value;
409 
410     return HDF_SUCCESS;
411 }
412 
UpdateInfoByReadSysFile(struct BatterydInfo * info) const413 void PowerSupplyProvider::UpdateInfoByReadSysFile(struct BatterydInfo* info) const
414 {
415     ParseCapacity(&info->capacity_);
416     ParseVoltage(&info->voltage_);
417     ParseTemperature(&info->temperature_);
418     ParseHealthState(&info->healthState_);
419     ParseChargeState(&info->chargeState_);
420     ParseChargeCounter(&info->chargeCounter_);
421     ParseCurrentNow(&info->curNow_);
422     ParseCurrentAverage(&info->curAverage_);
423     ParseRemainEnergy(&info->remainEnergy_);
424     ParseTotalEnergy(&info->totalEnergy_);
425     ParsePresent(&info->present_);
426 
427     info->pluggedType_ = PLUGGED_TYPE_NONE;
428     ParsePluggedType(&info->pluggedType_);
429 
430     info->pluggedMaxCurrent_ = INVALID_BATT_INT_VALUE;
431     ParsePluggedMaxCurrent(&info->pluggedMaxCurrent_);
432 
433     info->pluggedMaxVoltage_ = INVALID_BATT_INT_VALUE;
434     ParsePluggedMaxVoltage(&info->pluggedMaxVoltage_);
435 
436     info->technology_ = INVALID_STRING_VALUE;
437     ParseTechnology(info->technology_);
438 
439     CopyBatteryInfo(info);
440 }
441 
ParseUeventToBatterydInfo(const char * msg,struct BatterydInfo * info) const442 void PowerSupplyProvider::ParseUeventToBatterydInfo(const char* msg, struct BatterydInfo* info) const
443 {
444     static struct BatteryAssigner batteryAssigners[] = {
445         { BATTERY_KEY_CAPACITY.c_str(), BATTERY_KEY_CAPACITY.length(), CapacityAssigner },
446         { BATTERY_KEY_TOTAL_ENERGY.c_str(), BATTERY_KEY_TOTAL_ENERGY.length(), TotalEnergyAssigner },
447         { BATTERY_KEY_REMAIN_ENERGY.c_str(), BATTERY_KEY_REMAIN_ENERGY.length(), RemainEnergyAssigner },
448         { BATTERY_KEY_VOLTAGE.c_str(), BATTERY_KEY_VOLTAGE.length(), VoltageAssigner },
449         { BATTERY_KEY_TEMPERATURE.c_str(), BATTERY_KEY_TEMPERATURE.length(), TemperatureAssigner },
450         { BATTERY_KEY_HEALTH.c_str(), BATTERY_KEY_HEALTH.length(), HealthStateAssigner },
451         { BATTERY_KEY_CHARGE_STATUS.c_str(), BATTERY_KEY_CHARGE_STATUS.length(), ChargeStateAssigner },
452         { BATTERY_KEY_PRESENT.c_str(), BATTERY_KEY_PRESENT.length(), PresentAssigner },
453         { BATTERY_KEY_TECHNOLOGY.c_str(), BATTERY_KEY_TECHNOLOGY.length(), TechnologyAssigner },
454         { BATTERY_KEY_CHARGE_COUNTER.c_str(), BATTERY_KEY_CHARGE_COUNTER.length(), ChargeCounterAssigner },
455         { BATTERY_KEY_CURRENT_AVERAGE.c_str(), BATTERY_KEY_CURRENT_AVERAGE.length(), CurrentAverageAssigner },
456         { BATTERY_KEY_CURRENT_NOW.c_str(), BATTERY_KEY_CURRENT_NOW.length(), CurrentNowAssigner },
457         { nullptr, 0, nullptr } // end of the array
458     };
459 
460     while (*msg) {
461         for (int32_t i = 0; batteryAssigners[i].prefix; ++i) {
462             if (!strncmp(msg, batteryAssigners[i].prefix, batteryAssigners[i].prefixLen)) {
463                 BATTERY_HILOGD(FEATURE_BATT_INFO, "msg: %{public}s", msg);
464                 msg += batteryAssigners[i].prefixLen;
465                 batteryAssigners[i].Assigner(msg, info);
466                 break;
467             }
468         }
469         while (*msg++) {} // move to next
470     }
471 
472     info->pluggedType_ = PLUGGED_TYPE_NONE;
473     ParsePluggedType(&info->pluggedType_);
474 
475     info->pluggedMaxCurrent_ = INVALID_BATT_INT_VALUE;
476     ParsePluggedMaxCurrent(&info->pluggedMaxCurrent_);
477 
478     info->pluggedMaxVoltage_ = INVALID_BATT_INT_VALUE;
479     ParsePluggedMaxVoltage(&info->pluggedMaxVoltage_);
480 
481     info->technology_ = INVALID_STRING_VALUE;
482     ParseTechnology(info->technology_);
483 
484     CopyBatteryInfo(info);
485 }
486 
CopyBatteryInfo(const struct BatterydInfo * info) const487 void PowerSupplyProvider::CopyBatteryInfo(const struct BatterydInfo* info) const
488 {
489     g_batteryInfo.capacity_ = info->capacity_;
490     g_batteryInfo.voltage_ = info->voltage_;
491     g_batteryInfo.temperature_ = info->temperature_;
492     g_batteryInfo.healthState_ = info->healthState_;
493     g_batteryInfo.pluggedType_ = info->pluggedType_;
494     g_batteryInfo.pluggedMaxCurrent_ = info->pluggedMaxCurrent_;
495     g_batteryInfo.pluggedMaxVoltage_ = info->pluggedMaxVoltage_;
496     g_batteryInfo.chargeState_ = info->chargeState_;
497     g_batteryInfo.chargeCounter_ = info->chargeCounter_;
498     g_batteryInfo.curNow_ = info->curNow_;
499     g_batteryInfo.curAverage_ = info->curAverage_;
500     g_batteryInfo.totalEnergy_ = info->totalEnergy_;
501     g_batteryInfo.remainEnergy_ = info->remainEnergy_;
502     g_batteryInfo.present_ = info->present_;
503     g_batteryInfo.technology_ = info->technology_;
504 }
505 
SetSysFilePath(const std::string & path)506 void PowerSupplyProvider::SetSysFilePath(const std::string& path)
507 {
508     if (path.empty()) {
509         BATTERY_HILOGI(FEATURE_BATT_INFO, "path is empty");
510         return;
511     }
512     path_ = path;
513 }
514 
CreateFile(const std::string & path,const std::string & content)515 void PowerSupplyProvider::CreateFile(const std::string& path, const std::string& content)
516 {
517     if (access(path.c_str(), F_OK) == 0) {
518         return;
519     }
520 
521     std::ofstream stream(path.c_str());
522     if (!stream.is_open()) {
523         BATTERY_HILOGE(FEATURE_BATT_INFO, "cannot create file");
524         return;
525     }
526     stream << content.c_str() << std::endl;
527     stream.close();
528 }
529 
InitBatteryPath()530 void PowerSupplyProvider::InitBatteryPath()
531 {
532     std::string sysLowercaseBatteryPath = "/sys/class/power_supply/battery";
533 
534     if (access(sysLowercaseBatteryPath.c_str(), F_OK) == 0) {
535         BATTERY_HILOGI(FEATURE_BATT_INFO, "system battery path is exist");
536         return;
537     } else {
538         std::string sysCapitalBatteryPath = "/sys/class/power_supply/Battery";
539         if (access(sysCapitalBatteryPath.c_str(), F_OK) == 0) {
540             BATTERY_HILOGI(FEATURE_BATT_INFO, "system Battery path is exist");
541             return;
542         }
543         InitDefaultSysfs();
544     }
545     InitChargerSysfs();
546 }
547 
InitPowerSupplySysfs()548 int32_t PowerSupplyProvider::InitPowerSupplySysfs()
549 {
550     DIR* dir = nullptr;
551     struct dirent* entry = nullptr;
552     index_ = 0;
553 
554     dir = opendir(path_.c_str());
555     if (dir == nullptr) {
556         BATTERY_HILOGE(FEATURE_BATT_INFO, "cannot open path_");
557         return HDF_ERR_IO;
558     }
559 
560     while (true) {
561         entry = readdir(dir);
562         if (entry == nullptr) {
563             break;
564         }
565 
566         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
567             continue;
568         }
569 
570         if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
571             if (index_ >= MAX_SYSFS_SIZE) {
572                 BATTERY_HILOGW(FEATURE_BATT_INFO, "too many power supply types");
573                 break;
574             }
575             nodeNames_.emplace_back(entry->d_name);
576             index_++;
577         }
578     }
579     nodeNamePathMap_.clear();
580     TraversalNode();
581     FormatSysfsPaths();
582     BATTERY_HILOGD(FEATURE_BATT_INFO, "init power supply sysfs nodes, total count %{public}d", index_);
583     closedir(dir);
584 
585     return HDF_SUCCESS;
586 }
587 
InitChargerSysfs()588 void PowerSupplyProvider::InitChargerSysfs()
589 {
590     auto& batteryConfig = BatteryConfig::GetInstance();
591     batteryConfig.ParseConfig();
592     BatteryConfig::ChargerConfig chargerConfig = batteryConfig.GetChargerConfig();
593 
594     std::string mockCurrentLimitPath = chargerConfig.currentPath;
595     if (access(mockCurrentLimitPath.c_str(), 0) == -1) {
596         CreateFile(mockCurrentLimitPath, "0");
597     }
598 
599     std::string mockVoltageLimitPath = chargerConfig.voltagePath;
600     if (access(mockVoltageLimitPath.c_str(), 0) == -1) {
601         CreateFile(mockVoltageLimitPath, "0");
602     }
603 
604     std::string mockChargeTypePath = chargerConfig.chargeTypePath;
605     if (access(mockChargeTypePath.c_str(), 0) == -1) {
606         CreateFile(mockChargeTypePath, "0");
607     }
608 }
609 
TraversalNode()610 void PowerSupplyProvider::TraversalNode()
611 {
612     nodeNamePathMap_.insert(std::make_pair("type", ""));
613     nodeNamePathMap_.insert(std::make_pair("online", ""));
614     nodeNamePathMap_.insert(std::make_pair("current_max", ""));
615     nodeNamePathMap_.insert(std::make_pair("voltage_max", ""));
616     nodeNamePathMap_.insert(std::make_pair("capacity", ""));
617     nodeNamePathMap_.insert(std::make_pair("voltage_now", ""));
618     nodeNamePathMap_.insert(std::make_pair("temp", ""));
619     nodeNamePathMap_.insert(std::make_pair("health", ""));
620     nodeNamePathMap_.insert(std::make_pair("status", ""));
621     nodeNamePathMap_.insert(std::make_pair("present", ""));
622     nodeNamePathMap_.insert(std::make_pair("charge_counter", ""));
623     nodeNamePathMap_.insert(std::make_pair("technology", ""));
624     nodeNamePathMap_.insert(std::make_pair("charge_full", ""));
625     nodeNamePathMap_.insert(std::make_pair("current_avg", ""));
626     nodeNamePathMap_.insert(std::make_pair("current_now", ""));
627     nodeNamePathMap_.insert(std::make_pair("charge_now", ""));
628 
629     auto iter = nodeNames_.begin();
630     while (iter != nodeNames_.end()) {
631         if (*iter == "battery") {
632             CheckSubfolderNode(*iter);
633             iter = nodeNames_.erase(iter);
634         } else {
635             iter++;
636         }
637     }
638 
639     iter = nodeNames_.begin();
640     while (iter != nodeNames_.end()) {
641         if (*iter == POWER_SUPPLY_BATTERY) {
642             CheckSubfolderNode(*iter);
643             iter = nodeNames_.erase(iter);
644         } else {
645             iter++;
646         }
647     }
648 
649     for (auto& nodeName : nodeNames_) {
650         CheckSubfolderNode(nodeName);
651     }
652 }
653 
CheckSubfolderNode(const std::string & path)654 void PowerSupplyProvider::CheckSubfolderNode(const std::string& path)
655 {
656     DIR *dir = nullptr;
657     struct dirent* entry = nullptr;
658     std::string batteryPath = path_ + "/" + path;
659 
660     dir = opendir(batteryPath.c_str());
661     if (dir == nullptr) {
662         BATTERY_HILOGE(FEATURE_BATT_INFO, "subfolder file is not exist.");
663         return;
664     }
665 
666     while (true) {
667         entry = readdir(dir);
668         if (entry == nullptr) {
669             break;
670         }
671 
672         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
673             continue;
674         }
675 
676         if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
677             continue;
678         }
679 
680         if ((strcmp(entry->d_name, "type") == 0) && (nodeNamePathMap_["type"].empty()) &&
681             (strcasecmp(path.c_str(), BATTERY_NODE_PATH.c_str()) != 0)) {
682             nodeNamePathMap_["type"] = path;
683         }
684 
685         for (auto & iter : nodeNamePathMap_) {
686             if ((strcmp(entry->d_name, iter.first.c_str()) == 0) && (nodeNamePathMap_[iter.first].empty())) {
687                 nodeNamePathMap_[iter.first] = path;
688             }
689         }
690     }
691     closedir(dir);
692 }
693 
ParseCapacity(int32_t * capacity) const694 int32_t PowerSupplyProvider::ParseCapacity(int32_t* capacity) const
695 {
696     char buf[MAX_BUFF_SIZE] = {0};
697 
698     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.capacityPath.c_str(), buf, sizeof(buf));
699     if (ret != HDF_SUCCESS) {
700         return ret;
701     }
702 
703     int32_t value = ParseInt(buf);
704     *capacity = value;
705 
706     return HDF_SUCCESS;
707 }
708 
ParseTotalEnergy(int32_t * totalEnergy) const709 int32_t PowerSupplyProvider::ParseTotalEnergy(int32_t* totalEnergy) const
710 {
711     char buf[MAX_BUFF_SIZE] = {0};
712 
713     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.totalEnergyPath.c_str(), buf, sizeof(buf));
714     if (ret != HDF_SUCCESS) {
715         return ret;
716     }
717 
718     int32_t value = ParseInt(buf);
719     *totalEnergy = value;
720 
721     return HDF_SUCCESS;
722 }
723 
ParseCurrentAverage(int32_t * curAverage) const724 int32_t PowerSupplyProvider::ParseCurrentAverage(int32_t* curAverage) const
725 {
726     char buf[MAX_BUFF_SIZE] = {0};
727 
728     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.curAveragePath.c_str(), buf, sizeof(buf));
729     if (ret != HDF_SUCCESS) {
730         return ret;
731     }
732 
733     int32_t value = ParseInt(buf);
734     *curAverage = value;
735 
736     return HDF_SUCCESS;
737 }
738 
ParseCurrentNow(int32_t * curNow) const739 int32_t PowerSupplyProvider::ParseCurrentNow(int32_t* curNow) const
740 {
741     char buf[MAX_BUFF_SIZE] = {0};
742 
743     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.curNowPath.c_str(), buf, sizeof(buf));
744     if (ret != HDF_SUCCESS) {
745         return ret;
746     }
747 
748     int32_t value = ParseInt(buf);
749     *curNow = value;
750 
751     return HDF_SUCCESS;
752 }
753 
ParseRemainEnergy(int32_t * remainEnergy) const754 int32_t PowerSupplyProvider::ParseRemainEnergy(int32_t* remainEnergy) const
755 {
756     char buf[MAX_BUFF_SIZE] = {0};
757 
758     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.remainEnergyPath.c_str(), buf, sizeof(buf));
759     if (ret != HDF_SUCCESS) {
760         return ret;
761     }
762 
763     int32_t value = ParseInt(buf);
764     *remainEnergy = value;
765 
766     return HDF_SUCCESS;
767 }
768 
ParseVoltage(int32_t * voltage) const769 int32_t PowerSupplyProvider::ParseVoltage(int32_t* voltage) const
770 {
771     char buf[MAX_BUFF_SIZE] = {0};
772     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.voltagePath.c_str(), buf, sizeof(buf));
773     if (ret != HDF_SUCCESS) {
774         return ret;
775     }
776 
777     int32_t value = ParseInt(buf);
778     *voltage = value;
779 
780     return HDF_SUCCESS;
781 }
782 
ParseTemperature(int32_t * temperature) const783 int32_t PowerSupplyProvider::ParseTemperature(int32_t* temperature) const
784 {
785     char buf[MAX_BUFF_SIZE] = {0};
786     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.temperaturePath.c_str(), buf, sizeof(buf));
787     if (ret != HDF_SUCCESS) {
788         return ret;
789     }
790 
791     int32_t value = ParseInt(buf);
792     *temperature = value;
793 
794     return HDF_SUCCESS;
795 }
796 
ParseHealthState(int32_t * healthState) const797 int32_t PowerSupplyProvider::ParseHealthState(int32_t* healthState) const
798 {
799     char buf[MAX_BUFF_SIZE] = {0};
800     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.healthStatePath.c_str(), buf, sizeof(buf));
801     if (ret != HDF_SUCCESS) {
802         return ret;
803     }
804 
805     Trim(buf);
806     *healthState = HealthStateEnumConverter(buf);
807     return HDF_SUCCESS;
808 }
809 
ParsePluggedType(int32_t * pluggedType) const810 int32_t PowerSupplyProvider::ParsePluggedType(int32_t* pluggedType) const
811 {
812     char buf[MAX_BUFF_SIZE] = {0};
813     GetPluggedTypeName(buf, sizeof(buf));
814     int32_t type = PluggedTypeEnumConverter(buf);
815     if (type == PLUGGED_TYPE_BUTT) {
816         BATTERY_HILOGW(FEATURE_BATT_INFO, "not support the online type %{public}s", buf);
817         return HDF_ERR_NOT_SUPPORT;
818     }
819 
820     *pluggedType = type;
821     return HDF_SUCCESS;
822 }
823 
ParseChargeState(int32_t * chargeState) const824 int32_t PowerSupplyProvider::ParseChargeState(int32_t* chargeState) const
825 {
826     char buf[MAX_BUFF_SIZE] = {0};
827     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.chargeStatePath.c_str(), buf, sizeof(buf));
828     if (ret != HDF_SUCCESS) {
829         return ret;
830     }
831 
832     Trim(buf);
833     *chargeState = ChargeStateEnumConverter(buf);
834     return HDF_SUCCESS;
835 }
836 
ParsePresent(int8_t * present) const837 int32_t PowerSupplyProvider::ParsePresent(int8_t* present) const
838 {
839     char buf[MAX_BUFF_SIZE] = {0};
840     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.presentPath.c_str(), buf, sizeof(buf));
841     if (ret != HDF_SUCCESS) {
842         return ret;
843     }
844 
845     auto value = static_cast<int8_t>(ParseInt(buf));
846     *present = value;
847     return HDF_SUCCESS;
848 }
849 
ParseChargeCounter(int32_t * chargeCounter) const850 int32_t PowerSupplyProvider::ParseChargeCounter(int32_t* chargeCounter) const
851 {
852     char buf[MAX_BUFF_SIZE] = {0};
853     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.chargeCounterPath.c_str(), buf, sizeof(buf));
854     if (ret != HDF_SUCCESS) {
855         return ret;
856     }
857 
858     int32_t value = ParseInt(buf);
859     *chargeCounter = value;
860 
861     return HDF_SUCCESS;
862 }
863 
ParseTechnology(std::string & technology) const864 int32_t PowerSupplyProvider::ParseTechnology(std::string& technology) const
865 {
866     char buf[MAX_BUFF_SIZE] = {0};
867     int32_t ret = ReadBatterySysfsToBuff(batterySysfsInfo_.technologyPath.c_str(), buf, sizeof(buf));
868     if (ret != HDF_SUCCESS) {
869         return ret;
870     }
871 
872     technology.assign(buf, strlen(buf));
873     return HDF_SUCCESS;
874 }
875 
ParseChargeType(int32_t * chargeType,std::string & chargeTypePath) const876 int32_t PowerSupplyProvider::ParseChargeType(int32_t* chargeType, std::string& chargeTypePath) const
877 {
878     char buf[MAX_BUFF_SIZE] = {0};
879     int32_t ret = ReadBatterySysfsToBuff(chargeTypePath.c_str(), buf, sizeof(buf));
880     if (ret != HDF_SUCCESS) {
881         return ret;
882     }
883 
884     Trim(buf);
885     *chargeType = ChargeTypeEumConverter(buf);
886     return HDF_SUCCESS;
887 }
888 
GetBatteryInfo() const889 BatterydInfo PowerSupplyProvider::GetBatteryInfo() const
890 {
891     UpdateInfoByReadSysFile(&g_batteryInfo);
892     return g_batteryInfo;
893 }
894 
CreateMockTechPath(std::string & mockTechPath)895 void PowerSupplyProvider::CreateMockTechPath(std::string& mockTechPath)
896 {
897     BATTERY_HILOGI(FEATURE_BATT_INFO, "create mockFilePath path");
898     CreateFile(mockTechPath + "/capacity", "1000");
899     CreateFile(mockTechPath + "/current_avg", "1000");
900     CreateFile(mockTechPath + "/current_now", "1000");
901     CreateFile(mockTechPath + "/health", "Over voltage");
902     CreateFile(mockTechPath + "/present", "0");
903     CreateFile(mockTechPath + "/status", "Not charging");
904     CreateFile(mockTechPath + "/type", "Unknown");
905     CreateFile(mockTechPath + "/temp", "345");
906     CreateFile(mockTechPath + "/technology", "Li-ion");
907 }
908 
CreateMockChargerPath(std::string & mockChargerPath)909 void PowerSupplyProvider::CreateMockChargerPath(std::string& mockChargerPath)
910 {
911     BATTERY_HILOGI(FEATURE_BATT_INFO, "create mockFilePath path");
912     CreateFile(mockChargerPath + "/type", "USB");
913     CreateFile(mockChargerPath + "/constant_charge_current", "0");
914     CreateFile(mockChargerPath + "/health", "Good");
915     CreateFile(mockChargerPath + "/online", "1");
916     CreateFile(mockChargerPath + "/status", "Charging");
917 }
918 
CreateMockBatteryPath(std::string & mockBatteryPath)919 void PowerSupplyProvider::CreateMockBatteryPath(std::string& mockBatteryPath)
920 {
921     BATTERY_HILOGI(FEATURE_BATT_INFO, "create mockFilePath path");
922     CreateFile(mockBatteryPath + "/capacity", "11");
923     CreateFile(mockBatteryPath + "/charge_control_limit", "0");
924     CreateFile(mockBatteryPath + "/charge_counter", "4000000");
925     CreateFile(mockBatteryPath + "/charge_full", "4000000");
926     CreateFile(mockBatteryPath + "/charge_now", "4000000");
927     CreateFile(mockBatteryPath + "/constant_charge_current", "0");
928     CreateFile(mockBatteryPath + "/current_avg", "1000");
929     CreateFile(mockBatteryPath + "/current_now", "1000");
930     CreateFile(mockBatteryPath + "/health", "Good");
931     CreateFile(mockBatteryPath + "/input_current_limit", "0");
932     CreateFile(mockBatteryPath + "/online", "1");
933     CreateFile(mockBatteryPath + "/present", "0");
934     CreateFile(mockBatteryPath + "/status", "Charging");
935     CreateFile(mockBatteryPath + "/temp", "222");
936     CreateFile(mockBatteryPath + "/voltage_avg", "4123456");
937     CreateFile(mockBatteryPath + "/voltage_now", "4123456");
938     CreateFile(mockBatteryPath + "/type", "Battery");
939 }
940 
InitDefaultSysfs()941 void PowerSupplyProvider::InitDefaultSysfs()
942 {
943     std::string mockBatteryPath = MOCK_POWER_SUPPLY_BASE_PATH + "/battery";
944     std::string mockChargerPath = MOCK_POWER_SUPPLY_BASE_PATH + "/ohos_charger";
945     std::string mockTechPath = MOCK_POWER_SUPPLY_BASE_PATH + "/ohos-fgu";
946 
947     if (access(mockBatteryPath.c_str(), 0) == -1) {
948         mkdir(mockBatteryPath.c_str(), S_IRWXU | S_IRWXG);
949         sleep(MKDIR_WAIT_TIME);
950     }
951 
952     if (access(mockChargerPath.c_str(), 0) == -1) {
953         mkdir(mockChargerPath.c_str(), S_IRWXU);
954         sleep(MKDIR_WAIT_TIME);
955     }
956 
957     if (access(mockTechPath.c_str(), 0) == -1) {
958         mkdir(mockTechPath.c_str(), S_IRWXU);
959         sleep(MKDIR_WAIT_TIME);
960     }
961 
962     CreateMockTechPath(mockTechPath);
963     CreateMockChargerPath(mockChargerPath);
964     CreateMockBatteryPath(mockBatteryPath);
965     path_ = MOCK_POWER_SUPPLY_BASE_PATH;
966 }
967 
SetChargingLimit(const std::vector<ChargingLimit> & chargerLimitList,std::string & currentPath,std::string & voltagePath)968 int32_t PowerSupplyProvider::SetChargingLimit(const std::vector<ChargingLimit>& chargerLimitList,
969     std::string& currentPath, std::string& voltagePath)
970 {
971     BATTERY_HILOGD(FEATURE_BATT_INFO, "enter");
972     if (chargerLimitList.empty()) {
973         BATTERY_HILOGE(FEATURE_BATT_INFO, "the parameter is empty");
974         return HDF_ERR_INVALID_PARAM;
975     }
976 
977     std::string limitPath;
978     std::string chargeLimitStr;
979     for (const auto& iter : chargerLimitList) {
980         if (iter.type == ChargingLimitType::TYPE_CURRENT) {
981             limitPath = currentPath;
982         } else if (iter.type == ChargingLimitType::TYPE_VOLTAGE) {
983             limitPath = voltagePath;
984         }
985         chargeLimitStr = chargeLimitStr + (iter.protocol + " " + std::to_string(iter.value) + "\n");
986     }
987 
988     int32_t ret = SetConfigByPath(limitPath, chargeLimitStr);
989     if (ret < HDF_SUCCESS) {
990         return ret;
991     }
992     BATTERY_HILOGI(FEATURE_BATT_INFO, "Exit");
993     return HDF_SUCCESS;
994 }
995 
SetConfigByPath(const std::string & path,const std::string & value)996 int32_t PowerSupplyProvider::SetConfigByPath(const std::string& path, const std::string& value)
997 {
998     BATTERY_HILOGI(FEATURE_BATT_INFO, "SetConfigByPath enter, path: %{public}s, value:%{public}s",
999         path.c_str(), value.c_str());
1000     if (path.empty()) {
1001         BATTERY_HILOGE(FEATURE_BATT_INFO, "the featurePath is empty");
1002         return HDF_ERR_INVALID_PARAM;
1003     }
1004 
1005     std::fstream out(path, std::ios::out | std::ios::trunc);
1006     out << value;
1007     out.close();
1008 
1009     BATTERY_HILOGI(FEATURE_BATT_INFO, "SetConfigByPath exit");
1010     return HDF_SUCCESS;
1011 }
1012 
GetConfigByPath(const std::string & path,std::string & result)1013 int32_t PowerSupplyProvider::GetConfigByPath(const std::string& path, std::string& result)
1014 {
1015     BATTERY_HILOGI(FEATURE_BATT_INFO, "GetConfigByPath enter, path: %{public}s", path.c_str());
1016     if (path.empty()) {
1017         BATTERY_HILOGE(FEATURE_BATT_INFO, "the featurePath is empty");
1018         result = "";
1019         return HDF_ERR_INVALID_PARAM;
1020     }
1021 
1022     char buf[MAX_BUFF_SIZE] = {0};
1023     int32_t ret = ReadBatterySysfsToBuff(path.c_str(), buf, sizeof(buf));
1024     if (ret != HDF_SUCCESS) {
1025         BATTERY_HILOGE(FEATURE_BATT_INFO, "read config failed, path: %{public}s", path.c_str());
1026         result = "";
1027         return ret;
1028     }
1029     Trim(buf);
1030     result = buf;
1031     BATTERY_HILOGI(FEATURE_BATT_INFO, "GetConfigByPath exit, value:%{public}s", result.c_str());
1032     return HDF_SUCCESS;
1033 }
1034 
CheckPathExists(const std::string & path,bool & result)1035 int32_t PowerSupplyProvider::CheckPathExists(const std::string& path, bool& result)
1036 {
1037     BATTERY_HILOGI(FEATURE_BATT_INFO, "CheckPathExists enter, path: %{public}s", path.c_str());
1038     if (path.empty()) {
1039         BATTERY_HILOGE(FEATURE_BATT_INFO, "the path is empty");
1040         result = false;
1041         return HDF_ERR_INVALID_PARAM;
1042     }
1043     result = access(path.c_str(), F_OK) == 0;
1044     BATTERY_HILOGI(FEATURE_BATT_INFO, "CheckPathExists exit, value:%{public}d", result);
1045     return HDF_SUCCESS;
1046 }
1047 }  // namespace V2_0
1048 }  // namespace Battery
1049 }  // namespace HDI
1050 }  // namespace OHOS
1051