up-device-supply-battery: Explicitly define the battery energy/charge unit

Before, if the energy.full is very small, the energy unit will be changed
from engery_full to charge_full. Now, we explicitly choose one of them
when the udev attribute is found.

Fixes: https://gitlab.freedesktop.org/upower/upower/-/issues/253
This commit is contained in:
Kate Hsuan 2023-11-20 16:41:18 +08:00
parent dfde9e2274
commit fe83277fbe

View file

@ -160,16 +160,17 @@ up_device_supply_battery_refresh (UpDevice *device,
info.voltage_design = up_device_supply_battery_get_design_voltage (self, native);
info.charge_cycles = g_udev_device_get_sysfs_attr_as_int_uncached (native, "cycle_count");
info.units = UP_BATTERY_UNIT_ENERGY;
info.energy.full = g_udev_device_get_sysfs_attr_as_double_uncached (native, "energy_full") / 1000000.0;
info.energy.design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "energy_full_design") / 1000000.0;
/* Assume we couldn't read anything if energy.full is extremely small */
if (info.energy.full < 0.01) {
if (g_udev_device_has_sysfs_attr (native, "energy_full") &&
g_udev_device_has_sysfs_attr (native, "energy_full_design")) {
info.units = UP_BATTERY_UNIT_ENERGY;
info.energy.full = g_udev_device_get_sysfs_attr_as_double_uncached (native, "energy_full") / 1000000.0;
info.energy.design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "energy_full_design") / 1000000.0;
} else {
info.units = UP_BATTERY_UNIT_CHARGE;
info.energy.full = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full") / 1000000.0;
info.energy.design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full_design") / 1000000.0;
}
info.technology = up_convert_device_technology (get_sysfs_attr_uncached (native, "technology"));
/* NOTE: We used to warn about full > design, but really that is prefectly fine to happen. */