battery: report current if known
authorPeter Wu <peter@lekensteyn.nl>
Mon, 30 Mar 2015 16:48:06 +0000 (18:48 +0200)
committerPeter Wu <peter@lekensteyn.nl>
Mon, 30 Mar 2015 16:48:06 +0000 (18:48 +0200)
The power field is not always available (take my Clevo B7130 laptop for
example). The current is reported though via "current_now".

According to the ACPI spec v5.0, sect. 10.2.2.6 _BST (Battery Status),
the reported field is "battery present rate" which is always positive
(its direction depends on the charging state).

Contents of /sys/class/power_supply/BAT0/uevent for my laptop:

    POWER_SUPPLY_NAME=BAT0
    POWER_SUPPLY_STATUS=Discharging
    POWER_SUPPLY_PRESENT=1
    POWER_SUPPLY_TECHNOLOGY=Li-ion
    POWER_SUPPLY_CYCLE_COUNT=0
    POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
    POWER_SUPPLY_VOLTAGE_NOW=11824000
    POWER_SUPPLY_CURRENT_NOW=1498000
    POWER_SUPPLY_CHARGE_FULL_DESIGN=5200000
    POWER_SUPPLY_CHARGE_FULL=5280000
    POWER_SUPPLY_CHARGE_NOW=4797000
    POWER_SUPPLY_CAPACITY=90
    POWER_SUPPLY_CAPACITY_LEVEL=Normal
    POWER_SUPPLY_MODEL_NAME=BAT
    POWER_SUPPLY_MANUFACTURER=NOTEBOOK
    POWER_SUPPLY_SERIAL_NUMBER=0001

Note for Clevo B7130 owners, the charging rate is not reported when
discharging but this can be [patched][1].

 [1]: https://github.com/Lekensteyn/acpi-stuff/blob/master/Clevo-B7130/BatteryFix.dsl

src/battery.c

index 185442c..f106da0 100644 (file)
@@ -513,13 +513,15 @@ static int read_sysfs_callback (char const *dir, /* {{{ */
                        v *= -1.0;
                battery_submit (plugin_instance, "power", v * SYSFS_FACTOR);
        }
+       if (sysfs_file_to_gauge (dir, power_supply, "current_now", &v) == 0)
+       {
+               if (discharging)
+                       v *= -1.0;
+               battery_submit (plugin_instance, "current", v * SYSFS_FACTOR);
+       }
 
        if (sysfs_file_to_gauge (dir, power_supply, "voltage_now", &v) == 0)
                battery_submit (plugin_instance, "voltage", v * SYSFS_FACTOR);
-#if 0
-       if (sysfs_file_to_gauge (dir, power_supply, "voltage_min_design", &v) == 0)
-               battery_submit (plugin_instance, "voltage", v * SYSFS_FACTOR);
-#endif
 
        return (0);
 } /* }}} int read_sysfs_callback */