linux: up-device-supply-battery: Get default charging limit from udev

Get the default charge limit through udev.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
This commit is contained in:
Jelle van der Waa 2024-07-22 19:03:10 +08:00 committed by Kate Hsuan
parent 16277c88c4
commit 3ff4d63cfc
2 changed files with 62 additions and 0 deletions

View file

@ -127,6 +127,58 @@ get_sysfs_attr_uncached (GUdevDevice *native, const gchar *key)
return g_steal_pointer (&value);
}
static gboolean
up_device_supply_battery_convert_to_double (const gchar *str_value, gdouble *value)
{
gdouble conv_value;
gchar *end = NULL;
if (str_value == NULL || value == NULL)
return FALSE;
conv_value = g_ascii_strtod (str_value, &end);
if (end == str_value || conv_value < 0.0 || conv_value > 100.0)
return FALSE;
*value = conv_value;
return TRUE;
}
static gboolean
up_device_supply_battery_get_charge_control_limits (GUdevDevice *native, UpBatteryInfo *info)
{
const gchar *charge_limit;
g_auto(GStrv) pairs = NULL;
gdouble charge_control_start_threshold;
gdouble charge_control_end_threshold;
charge_limit = g_udev_device_get_property (native, "CHARGE_LIMIT");
if (charge_limit == NULL)
return FALSE;
pairs = g_strsplit (charge_limit, ",", 0);
if (g_strv_length (pairs) != 2) {
g_warning("Could not parse CHARGE_LIMIT, expected 'number,number', got '%s'", charge_limit);
return FALSE;
}
if (!up_device_supply_battery_convert_to_double (pairs[0], &charge_control_start_threshold)) {
g_warning ("failed to convert charge_control_start_threshold: %s", pairs[0]);
return FALSE;
}
if (!up_device_supply_battery_convert_to_double (pairs[1], &charge_control_end_threshold)) {
g_warning ("failed to convert charge_control_start_threshold: %s", pairs[0]);
return FALSE;
}
info->charge_control_start_threshold = charge_control_start_threshold;
info->charge_control_end_threshold = charge_control_end_threshold;
return TRUE;
}
static gboolean
up_device_supply_battery_refresh (UpDevice *device,
UpRefreshReason reason)
@ -172,6 +224,11 @@ up_device_supply_battery_refresh (UpDevice *device,
}
info.technology = up_convert_device_technology (get_sysfs_attr_uncached (native, "technology"));
if (up_device_supply_battery_get_charge_control_limits (native, &info))
info.charge_control_enabled = TRUE;
else
info.charge_control_enabled = FALSE;
/* NOTE: We used to warn about full > design, but really that is prefectly fine to happen. */
/* Update the battery information (will only fire events for actual changes) */

View file

@ -84,6 +84,11 @@ typedef struct {
UpDeviceTechnology technology;
gdouble voltage_design;
gint charge_cycles;
/* battery charging threshold */
gboolean charge_control_enabled;
guint charge_control_start_threshold;
guint charge_control_end_threshold;
} UpBatteryInfo;