Skip to content

Commit

Permalink
healthd: Reinitialize mChargerNames for every battery update
Browse files Browse the repository at this point in the history
Booting up the device without usb, the kernel sets the usb power supply
type as UNKNOWN. The type of usb power supply changes at run-time as
various chargers are plugged in/out. However, healthd initilizes the
charger list only at bootup. Change it such that it checks for charger
type changes with every battery or usb uevent.

While at it, the kernel may have a power supply type which is not known
to healthd. This is perfectly fine. Update healthd to not print a
warning.

Change-Id: I2ec9f9a420ca61814d43c316b418ce94de3691bc
(cherry picked from commit 282857e89d591b6704e5d69ec2ae1e53a5de74cf)
  • Loading branch information
Abhijeet Dharmapurikar authored and adithya2306 committed Sep 16, 2020
1 parent c6f9508 commit a27509c
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions healthd/BatteryMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,8 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String
return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;

auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
if (!ret) {
KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
if (!ret)
*ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
}

return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
}
Expand Down Expand Up @@ -304,6 +302,40 @@ void BatteryMonitor::updateValues(void) {

double MaxPower = 0;

// Rescan for the available charger types
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
if (dir == NULL) {
KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
} else {
struct dirent* entry;
String8 path;

mChargerNames.clear();

while ((entry = readdir(dir.get()))) {
const char* name = entry->d_name;

if (!strcmp(name, ".") || !strcmp(name, ".."))
continue;

// Look for "type" file in each subdirectory
path.clear();
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
switch(BatteryMonitor::readPowerSupplyType(path)) {
case ANDROID_POWER_SUPPLY_TYPE_AC:
case ANDROID_POWER_SUPPLY_TYPE_USB:
case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
path.clear();
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
if (access(path.string(), R_OK) == 0)
mChargerNames.add(String8(name));
break;
default:
break;
}
}
}

for (size_t i = 0; i < mChargerNames.size(); i++) {
String8 path;
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
Expand Down

0 comments on commit a27509c

Please sign in to comment.