Bug 100359

Summary: Add support for "capacity_level" attribute
Product: upower Reporter: Bastien Nocera <bugzilla>
Component: generalAssignee: Richard Hughes <richard>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: medium CC: benjamin.tissoires, bugzilla, peter.hutterer
Version: unspecified   
Hardware: Other   
OS: All   
Whiteboard:
i915 platform: i915 features:
Attachments: linux: Add support for "capacity_level" attribute
linux: Add support for "capacity_level" attribute
daemon: Make warning levels for devices inclusive
linux: Add support for "capacity_level" attribute
lib: Add more members to UpDeviceLevel struct
all: Add BatteryLevel property
daemon: Move a number of constants to a shared file
all: Add BatteryLevel property

Description Bastien Nocera 2017-03-23 17:44:09 UTC
See patch
Comment 1 Bastien Nocera 2017-03-23 17:44:36 UTC
Created attachment 130402 [details] [review]
linux: Add support for "capacity_level" attribute

Some devices, such as a number of wireless Logitech unifying devices
don't have a precise battery level reporting, and use the Linux
POWER_SUPPLY_CAPACITY_LEVEL_* values.

This minimal fix matches the levels against approximate percentage
values. This is good enough to make the Logitech T650 report battery
again when using the kernel HID++ battery support.
Comment 2 Bastien Nocera 2017-03-23 17:48:31 UTC
Ideally, we'd proxy the "capacity level" itself through D-Bus, but it's really quite a bit easier to convert this to an approximate percentage.

Not great for devices with only 2 levels though:
https://bugzilla.gnome.org/show_bug.cgi?id=780360

Ideas?
Comment 3 Bastien Nocera 2017-03-24 11:19:20 UTC
Created attachment 130414 [details] [review]
linux: Add support for "capacity_level" attribute

Some devices, such as a number of wireless Logitech unifying devices
don't have a precise battery level reporting, and use the Linux
POWER_SUPPLY_CAPACITY_LEVEL_* values.

This minimal fix matches the levels against approximate percentage
values. This is good enough to make the Logitech T650 report battery
again when using the kernel HID++ battery support.
Comment 4 Bastien Nocera 2017-03-24 11:23:06 UTC
(In reply to Bastien Nocera from comment #3)
> Created attachment 130414 [details] [review] [review]
> linux: Add support for "capacity_level" attribute

2 changes:
- remove the "capacity_level" support for laptop batteries (non-device batteries), it's not actually needed
- Update the code and the tests, the "capacity_level" strings have linefeeds in them...
Comment 5 Bastien Nocera 2017-03-31 09:44:39 UTC
*** Bug 96857 has been marked as a duplicate of this bug. ***
Comment 6 Bastien Nocera 2017-04-04 08:34:18 UTC
After talking to Richard on IRC, I think I'll add a patch on top of this one to export the actual "capacity_level" value to API consumers. This will allow older apps to still work with the newer kernels, and not regress in supporting devices which use it.

The API would allow front-ends to not show a full battery gauge when the reporting is too coarse to show any changes over time.
Comment 7 Bastien Nocera 2017-04-05 15:50:20 UTC
Created attachment 130694 [details] [review]
daemon: Make warning levels for devices inclusive

A 5% battery level should already be in "critical", this matches the
configurable thresholds where the bounds of critical warnings are
inclusive. This also makes it easier to create fake percentages for the
Logitech devices with coarse battery level reporting.
Comment 8 Bastien Nocera 2017-04-05 15:50:23 UTC
Created attachment 130695 [details] [review]
linux: Add support for "capacity_level" attribute

Some devices, such as a number of wireless Logitech unifying devices
don't have a precise battery level reporting, and use the Linux
POWER_SUPPLY_CAPACITY_LEVEL_* values.

This minimal fix matches the levels against approximate percentage
values. This is good enough to make the Logitech T650 report battery
again when using the kernel HID++ battery support.
Comment 9 Bastien Nocera 2017-04-05 15:50:27 UTC
Created attachment 130696 [details] [review]
lib: Add more members to UpDeviceLevel struct

Those levels will not be used to cover warning levels, but approximate
battery levels that devices can use, exported by the kernel as
POWER_SUPPLY_CAPACITY_LEVEL_* values.

See linux/power_supply.h
Comment 10 Bastien Nocera 2017-04-05 15:50:30 UTC
Created attachment 130697 [details] [review]
all: Add BatteryLevel property

Export approximate battery levels that devices can use, exported by
the kernel as POWER_SUPPLY_CAPACITY_LEVEL_* values.

This avoids bizarrely accurate values showing up in UIs when we only
have ok/warning levels of accuracy in some cases.
Comment 11 Bastien Nocera 2017-04-05 15:50:33 UTC
Created attachment 130698 [details] [review]
daemon: Move a number of constants to a shared file
Comment 12 Christian Kellner 2017-04-06 14:13:14 UTC
Comment on attachment 130694 [details] [review]
daemon: Make warning levels for devices inclusive

Review of attachment 130694 [details] [review]:
-----------------------------------------------------------------

::: src/up-daemon.c
@@ +645,4 @@
>  	 * into critical (or off) before any warnings */
>  	if (kind == UP_DEVICE_KIND_MOUSE ||
>  	    kind == UP_DEVICE_KIND_KEYBOARD) {
> +		if (percentage <= 5.0f)

This is from pre-existing code and but I am curious: percentage is a double, any reason the 'f' float suffix is used?
Comment 13 Bastien Nocera 2017-04-06 14:33:53 UTC
Created attachment 130731 [details] [review]
all: Add BatteryLevel property

Export approximate battery levels that devices can use, exported by
the kernel as POWER_SUPPLY_CAPACITY_LEVEL_* values.

This avoids bizarrely accurate values showing up in UIs when we only
have ok/warning levels of accuracy in some cases.
Comment 14 Christian Kellner 2017-04-07 08:08:29 UTC
Comment on attachment 130696 [details] [review]
lib: Add more members to UpDeviceLevel struct

Review of attachment 130696 [details] [review]:
-----------------------------------------------------------------

::: libupower-glib/up-types.c
@@ +296,4 @@
>  		return UP_DEVICE_LEVEL_CRITICAL;
>  	if (g_strcmp0 (level, "action") == 0)
>  		return UP_DEVICE_LEVEL_ACTION;
> +	if (g_strcmp0 (level, "normal") == 0)

Just for completeness: the first 'if'-case takes care of the case that level is NULL, so a non-null-safe comparison would suffice (like e.g. g_str_equal, strcmp).
Comment 15 Christian Kellner 2017-04-07 08:09:27 UTC
Looks all good to me.
Comment 16 Bastien Nocera 2017-04-10 08:39:56 UTC
Attachment 130694 [details] pushed as 27a3eea - daemon: Make warning levels for devices inclusive
Attachment 130695 [details] pushed as 6b14798 - linux: Add support for "capacity_level" attribute
Attachment 130696 [details] pushed as 4f92309 - lib: Add more members to UpDeviceLevel struct
Attachment 130698 [details] pushed as 660c8f3 - daemon: Move a number of constants to a shared file
Attachment 130731 [details] pushed as 499d05b - all: Add BatteryLevel property

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.