Merge pull request #24420 from keszybz/mouse-power
Rework on_ac_power()
This commit is contained in:
@@ -18,6 +18,7 @@ static inline int device_new_from_watch_handle(sd_device **ret, int wd) {
|
||||
}
|
||||
|
||||
int device_get_property_bool(sd_device *device, const char *key);
|
||||
int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value);
|
||||
int device_get_sysattr_bool(sd_device *device, const char *sysattr);
|
||||
int device_get_device_id(sd_device *device, const char **ret);
|
||||
int device_get_devlink_priority(sd_device *device, int *ret);
|
||||
|
||||
@@ -2206,6 +2206,25 @@ _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int device_get_sysattr_unsigned(sd_device *device, const char *sysattr, unsigned *ret_value) {
|
||||
const char *value;
|
||||
int r;
|
||||
|
||||
r = sd_device_get_sysattr_value(device, sysattr, &value);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
unsigned v;
|
||||
r = safe_atou(value, &v);
|
||||
if (r < 0)
|
||||
return log_device_debug_errno(device, r, "Failed to parse '%s' attribute: %m", sysattr);
|
||||
|
||||
if (ret_value)
|
||||
*ret_value = v;
|
||||
/* We return "true" if the value is positive. */
|
||||
return v > 0;
|
||||
}
|
||||
|
||||
int device_get_sysattr_bool(sd_device *device, const char *sysattr) {
|
||||
const char *value;
|
||||
int r;
|
||||
|
||||
@@ -180,6 +180,13 @@ static void test_sd_device_one(sd_device *d) {
|
||||
|
||||
r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
|
||||
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
|
||||
|
||||
if (r > 0) {
|
||||
unsigned x;
|
||||
|
||||
assert_se(device_get_sysattr_unsigned(d, "name_assign_type", NULL) >= 0);
|
||||
assert_se(device_get_sysattr_unsigned(d, "name_assign_type", &x) >= 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(sd_device_enumerator_devices) {
|
||||
|
||||
+36
-38
@@ -661,7 +661,7 @@ static int device_is_power_sink(sd_device *device) {
|
||||
|
||||
int on_ac_power(void) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
bool found_offline = false, found_online = false, found_battery = false;
|
||||
bool found_ac_online = false, found_battery = false;
|
||||
sd_device *d;
|
||||
int r;
|
||||
|
||||
@@ -678,22 +678,15 @@ int on_ac_power(void) {
|
||||
return r;
|
||||
|
||||
FOREACH_DEVICE(e, d) {
|
||||
const char *val;
|
||||
unsigned v;
|
||||
|
||||
r = sd_device_get_sysattr_value(d, "type", &val);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Failed to read 'type' sysfs attribute, ignoring: %m");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* We assume every power source is AC, except for batteries. See
|
||||
/* See
|
||||
* https://github.com/torvalds/linux/blob/4eef766b7d4d88f0b984781bc1bcb574a6eafdc7/include/linux/power_supply.h#L176
|
||||
* for defined power source types. Also see:
|
||||
* https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-power */
|
||||
if (streq(val, "Battery")) {
|
||||
found_battery = true;
|
||||
log_device_debug(d, "The power supply is battery, ignoring.");
|
||||
|
||||
const char *val;
|
||||
r = sd_device_get_sysattr_value(d, "type", &val);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Failed to read 'type' sysfs attribute, ignoring device: %m");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -702,43 +695,48 @@ int on_ac_power(void) {
|
||||
r = device_is_power_sink(d);
|
||||
if (r <= 0) {
|
||||
if (r < 0)
|
||||
log_device_debug_errno(d, r, "Failed to determine the current power role, ignoring: %m");
|
||||
log_device_debug_errno(d, r, "Failed to determine the current power role, ignoring device: %m");
|
||||
else
|
||||
log_device_debug(d, "USB power supply is in source mode, ignoring.");
|
||||
log_device_debug(d, "USB power supply is in source mode, ignoring device.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
r = sd_device_get_sysattr_value(d, "online", &val);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Failed to read 'online' sysfs attribute, ignoring: %m");
|
||||
bool is_battery = streq(val, "Battery");
|
||||
if (is_battery) {
|
||||
r = sd_device_get_sysattr_value(d, "scope", &val);
|
||||
if (r < 0)
|
||||
log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m");
|
||||
else if (streq(val, "Device")) {
|
||||
log_device_debug(d, "The power supply is a device battery, ignoring.");
|
||||
continue;
|
||||
}
|
||||
|
||||
found_battery = true;
|
||||
log_device_debug(d, "The power supply is battery.");
|
||||
continue;
|
||||
}
|
||||
|
||||
r = safe_atou(val, &v);
|
||||
r = device_get_sysattr_unsigned(d, "online", NULL);
|
||||
if (r < 0) {
|
||||
log_device_debug_errno(d, r, "Failed to parse 'online' attribute, ignoring: %m");
|
||||
log_device_debug_errno(d, r, "Failed to query 'online' sysfs attribute: %m");
|
||||
continue;
|
||||
}
|
||||
} else if (r > 0) /* At least 1 and 2 are defined as different types of 'online' */
|
||||
found_ac_online = true;
|
||||
|
||||
if (v > 0) /* At least 1 and 2 are defined as different types of 'online' */
|
||||
found_online = true;
|
||||
else
|
||||
found_offline = true;
|
||||
|
||||
log_device_debug(d, "The power supply is currently %s.", v > 0 ? "online" : "offline");
|
||||
log_device_debug(d, "The power supply is currently %s.", r > 0 ? "online" : "offline");
|
||||
}
|
||||
|
||||
if (found_online)
|
||||
log_debug("Found at least one online non-battery power supply, system is running on AC power.");
|
||||
else if (!found_offline)
|
||||
log_debug("Found no offline non-battery power supply, assuming system is running on AC power.");
|
||||
else if (!found_battery)
|
||||
log_debug("Found no battery, assuming system is running on AC power.");
|
||||
else
|
||||
log_debug("All non-battery power supplies are offline, assuming system is running with battery.");
|
||||
|
||||
return found_online || !found_offline || !found_battery;
|
||||
if (found_ac_online) {
|
||||
log_debug("Found at least one online non-battery power supply, system is running on AC.");
|
||||
return true;
|
||||
} else if (found_battery) {
|
||||
log_debug("Found battery and no online power sources, assuming system is running from battery.");
|
||||
return false;
|
||||
} else {
|
||||
log_debug("No power supply reported online and no battery, assuming system is running on AC.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool udev_available(void) {
|
||||
|
||||
Reference in New Issue
Block a user