Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 021ef494 authored by Ramakrishnan Ganesh's avatar Ramakrishnan Ganesh Committed by Steve Kondik
Browse files

healthd: Reinitialize mChargerNames for every battery update

Booting up the device without the usb sets the usb power supply
type as UNKNOWN. Due to this mChargerNames gets incorrectly
initialized at bootup. The value of usb power supply type changes
at run-time. So it makes sense to update mChargerNames everytime
we have a battery related update.

Change-Id: I2ec9f9a420ca61814d43c316b418ce94de3691bc
parent 7d56df60
Loading
Loading
Loading
Loading
+54 −25
Original line number Diff line number Diff line
@@ -210,18 +210,37 @@ bool BatteryMonitor::update(void) {
    if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
        props.batteryTechnology = String8(buf);

    unsigned int i;

    for (i = 0; i < mChargerNames.size(); i++) {
    // reinitialize the mChargerNames vector everytime there is an update
    String8 path;
        path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
                          mChargerNames[i].string());
    DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
    if (dir == NULL) {
        KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
    } else {
        struct dirent* entry;
        // reconstruct the charger strings
        mChargerNames.clear();
        while ((entry = readdir(dir))) {
            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(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));
                    if (readFromFile(path, buf, SIZE) > 0) {
                        if (buf[0] != '0') {
                            path.clear();
                            path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
                                  mChargerNames[i].string());
                                              name);
                            switch(readPowerSupplyType(path)) {
                            case ANDROID_POWER_SUPPLY_TYPE_AC:
                                props.chargerAcOnline = true;
@@ -234,11 +253,21 @@ bool BatteryMonitor::update(void) {
                                break;
                            default:
                                KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
                                 mChargerNames[i].string());
                                             name);
                            }
                        }
                    }
                }
                break;
            case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
                break;
            default:
                KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
                                 name);
            } //switch
        } //while
        closedir(dir);
    }//else

    logthis = !healthd_board_battery_update(&props);