From fe83277fbef85287e7a69b4dd9b141b84ddb1d65 Mon Sep 17 00:00:00 2001 From: Kate Hsuan Date: Mon, 20 Nov 2023 16:41:18 +0800 Subject: 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 --- src/linux/up-device-supply-battery.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/linux/up-device-supply-battery.c b/src/linux/up-device-supply-battery.c index 456f692..c2f47ae 100644 --- a/src/linux/up-device-supply-battery.c +++ b/src/linux/up-device-supply-battery.c @@ -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. */ -- cgit v1.2.3