Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

0% battery reported because _AVG postfix is ignored in battery uevent #452

Open
crozone opened this issue Apr 27, 2021 · 0 comments · May be fixed by #453
Open

0% battery reported because _AVG postfix is ignored in battery uevent #452

crozone opened this issue Apr 27, 2021 · 0 comments · May be fixed by #453

Comments

@crozone
Copy link

crozone commented Apr 27, 2021

Issue:

The battery_info module reports 0% for some battery power supply drivers. This is because the _AVG postfix is not handled.

Detail:

According to the specification laid out in https://www.kernel.org/doc/Documentation/power/power_supply_class.txt, battery uevent can report values with two potential postfixes:

Postfixes:
_AVG - hardware averaged value, use it if your hardware is really able to
report averaged values.
_NOW - momentary/instantaneous values.

Currently only the _NOW postfix is handled, so drivers that report hardware averaged values and not momentary/instantaneous values will not work.

This breaks when used with the pmu_battery module on Apple iBooks. For example, the iBook Clamshell with pmu_battery exports this uevent at /sys/class/power_supply/PMU_battery_0/uevent:

POWER_SUPPLY_NAME=PMU_battery_0
POWER_SUPPLY_TYPE=Unknown
POWER_SUPPLY_STATUS=Charging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_MODEL_NAME=Smart
POWER_SUPPLY_ENERGY_AVG=391000
POWER_SUPPLY_ENERGY_FULL=3965000
POWER_SUPPLY_CURRENT_AVG=1204000
POWER_SUPPLY_VOLTAGE_AVG=14432000
POWER_SUPPLY_TIME_TO_EMPTY_AVG=10686

The following code should probably be modified to handle the _AVG postfix, and prioritize the _AVG postfix version over the _NOW postfix version:

for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
if (*walk == '\n') {
last = walk + 1;
continue;
}
if (*walk != '=')
continue;
if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW=")) {
watt_as_unit = true;
batt_info->remaining = atoi(walk + 1);
batt_info->percentage_remaining = -1;
} else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) {
watt_as_unit = false;
batt_info->remaining = atoi(walk + 1);
batt_info->percentage_remaining = -1;
} else if (BEGINS_WITH(last, "POWER_SUPPLY_CAPACITY=") && batt_info->remaining == -1) {
batt_info->percentage_remaining = atoi(walk + 1);
} else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW="))
batt_info->present_rate = abs(atoi(walk + 1));
else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW="))
voltage = abs(atoi(walk + 1));
else if (BEGINS_WITH(last, "POWER_SUPPLY_TIME_TO_EMPTY_NOW="))
batt_info->seconds_remaining = abs(atoi(walk + 1)) * 60;
/* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
* it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
* unit instead of μAh. We will calculate it as we need it
* later. */
else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW="))
batt_info->present_rate = abs(atoi(walk + 1));
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
batt_info->status = CS_CHARGING;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
batt_info->status = CS_FULL;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging") || BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Not charging"))
batt_info->status = CS_DISCHARGING;
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
batt_info->status = CS_UNKNOWN;
else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN="))
batt_info->full_design = atoi(walk + 1);
else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") ||
BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
batt_info->full_last = atoi(walk + 1);
}

@crozone crozone changed the title Battery info module does not look for uevent AVG postfix 0% battery reported because _AVG postfix is ignored in battery uevent Apr 27, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant