| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-device.h" |
| 4 |
|
| 5 |
#include "battery-util.h" |
| 6 |
#include "device-private.h" |
| 7 |
#include "device-util.h" |
| 8 |
#include "string-util.h" |
| 9 |
|
| 10 |
#define BATTERY_LOW_CAPACITY_LEVEL 5 |
| 11 |
|
| 12 |
static int device_is_power_sink(sd_device *device) { |
| 13 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 14 |
bool found_source = false, found_sink = false; |
| 15 |
sd_device *parent; |
| 16 |
int r; |
| 17 |
|
| 18 |
assert(device); |
| 19 |
|
| 20 |
/* USB-C power supply device has two power roles: source or sink. See, |
| 21 |
* https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-typec */ |
| 22 |
|
| 23 |
r = sd_device_enumerator_new(&e); |
| 24 |
if (r < 0) |
| 25 |
return r; |
| 26 |
|
| 27 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 28 |
if (r < 0) |
| 29 |
return r; |
| 30 |
|
| 31 |
r = sd_device_enumerator_add_match_subsystem(e, "typec", true); |
| 32 |
if (r < 0) |
| 33 |
return r; |
| 34 |
|
| 35 |
r = sd_device_get_parent(device, &parent); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
r = sd_device_enumerator_add_match_parent(e, parent); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
|
| 43 |
FOREACH_DEVICE(e, d) { |
| 44 |
const char *val; |
| 45 |
|
| 46 |
r = device_get_sysattr_safe_string(d, "power_role", &val); |
| 47 |
if (r < 0) { |
| 48 |
if (r != -ENOENT) |
| 49 |
log_device_debug_errno(d, r, "Failed to read 'power_role' sysfs attribute, ignoring: %m"); |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
if (strstr(val, "[source]")) { |
| 54 |
found_source = true; |
| 55 |
log_device_debug(d, "The USB type-C port is in power source mode."); |
| 56 |
} else if (strstr(val, "[sink]")) { |
| 57 |
found_sink = true; |
| 58 |
log_device_debug(d, "The USB type-C port is in power sink mode."); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if (found_sink) |
| 63 |
log_device_debug(device, "The USB type-C device has at least one port in power sink mode."); |
| 64 |
else if (!found_source) |
| 65 |
log_device_debug(device, "The USB type-C device has no port in power source mode, assuming the device is in power sink mode."); |
| 66 |
else |
| 67 |
log_device_debug(device, "All USB type-C ports are in power source mode."); |
| 68 |
|
| 69 |
return found_sink || !found_source; |
| 70 |
} |
| 71 |
|
| 72 |
static bool battery_is_discharging(sd_device *d) { |
| 73 |
const char *val; |
| 74 |
int r; |
| 75 |
|
| 76 |
assert(d); |
| 77 |
|
| 78 |
r = device_get_sysattr_streq(d, "scope", "Device"); |
| 79 |
if (r < 0 && r != -ENOENT) |
| 80 |
log_device_debug_errno(d, r, "Failed to read 'scope' sysfs attribute, ignoring: %m"); |
| 81 |
if (r > 0) { |
| 82 |
log_device_debug(d, "The power supply is a device battery, ignoring device."); |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
r = device_get_sysattr_bool(d, "present"); |
| 87 |
if (r < 0) |
| 88 |
log_device_debug_errno(d, r, "Failed to read 'present' sysfs attribute, assuming the battery is present: %m"); |
| 89 |
else if (r == 0) { |
| 90 |
log_device_debug(d, "The battery is not present, ignoring the power supply."); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
/* Possible values: "Unknown", "Charging", "Discharging", "Not charging", "Full" */ |
| 95 |
r = device_get_sysattr_safe_string(d, "status", &val); |
| 96 |
if (r < 0) { |
| 97 |
log_device_debug_errno(d, r, "Failed to read 'status' sysfs attribute, assuming the battery is discharging: %m"); |
| 98 |
return true; |
| 99 |
} |
| 100 |
if (!streq(val, "Discharging")) { |
| 101 |
log_device_debug(d, "The battery status is '%s', assuming the battery is not used as a power source of this machine.", val); |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
int on_ac_power(void) { |
| 109 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 110 |
bool found_ac_online = false, found_discharging_battery = false; |
| 111 |
int r; |
| 112 |
|
| 113 |
r = sd_device_enumerator_new(&e); |
| 114 |
if (r < 0) |
| 115 |
return r; |
| 116 |
|
| 117 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
|
| 121 |
r = sd_device_enumerator_add_match_subsystem(e, "power_supply", true); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
|
| 125 |
FOREACH_DEVICE(e, d) { |
| 126 |
/* See |
| 127 |
* https://github.com/torvalds/linux/blob/4eef766b7d4d88f0b984781bc1bcb574a6eafdc7/include/linux/power_supply.h#L176 |
| 128 |
* for defined power source types. Also see: |
| 129 |
* https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-power */ |
| 130 |
|
| 131 |
const char *val; |
| 132 |
r = device_get_sysattr_safe_string(d, "type", &val); |
| 133 |
if (r < 0) { |
| 134 |
log_device_debug_errno(d, r, "Failed to read 'type' sysfs attribute, ignoring device: %m"); |
| 135 |
continue; |
| 136 |
} |
| 137 |
|
| 138 |
/* Ignore USB-C power supply in source mode. See issue #21988. */ |
| 139 |
if (streq(val, "USB")) { |
| 140 |
r = device_is_power_sink(d); |
| 141 |
if (r <= 0) { |
| 142 |
if (r < 0) |
| 143 |
log_device_debug_errno(d, r, "Failed to determine the current power role, ignoring device: %m"); |
| 144 |
else |
| 145 |
log_device_debug(d, "USB power supply is in source mode, ignoring device."); |
| 146 |
continue; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (streq(val, "Battery")) { |
| 151 |
if (battery_is_discharging(d)) { |
| 152 |
found_discharging_battery = true; |
| 153 |
log_device_debug(d, "The power supply is a battery and currently discharging."); |
| 154 |
} |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
r = device_get_sysattr_unsigned(d, "online", NULL); |
| 159 |
if (r < 0) { |
| 160 |
log_device_debug_errno(d, r, "Failed to query 'online' sysfs attribute, ignoring device: %m"); |
| 161 |
continue; |
| 162 |
} else if (r > 0) /* At least 1 and 2 are defined as different types of 'online' */ |
| 163 |
found_ac_online = true; |
| 164 |
|
| 165 |
log_device_debug(d, "The power supply is currently %s.", r > 0 ? "online" : "offline"); |
| 166 |
} |
| 167 |
|
| 168 |
if (found_ac_online) { |
| 169 |
log_debug("Found at least one online non-battery power supply, system is running on AC."); |
| 170 |
return true; |
| 171 |
} else if (found_discharging_battery) { |
| 172 |
log_debug("Found at least one discharging battery and no online power sources, assuming system is running from battery."); |
| 173 |
return false; |
| 174 |
} else { |
| 175 |
log_debug("No power supply reported online and no discharging battery found, assuming system is running on AC."); |
| 176 |
return true; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* Get the list of batteries */ |
| 181 |
int battery_enumerator_new(sd_device_enumerator **ret) { |
| 182 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 183 |
int r; |
| 184 |
|
| 185 |
assert(ret); |
| 186 |
|
| 187 |
r = sd_device_enumerator_new(&e); |
| 188 |
if (r < 0) |
| 189 |
return r; |
| 190 |
|
| 191 |
r = sd_device_enumerator_add_match_subsystem(e, "power_supply", /* match= */ true); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
r = sd_device_enumerator_add_match_sysattr(e, "type", "Battery", /* match= */ true); |
| 200 |
if (r < 0) |
| 201 |
return r; |
| 202 |
|
| 203 |
r = sd_device_enumerator_add_match_sysattr(e, "present", "1", /* match= */ true); |
| 204 |
if (r < 0) |
| 205 |
return r; |
| 206 |
|
| 207 |
r = sd_device_enumerator_add_match_sysattr(e, "scope", "Device", /* match= */ false); |
| 208 |
if (r < 0) |
| 209 |
return r; |
| 210 |
|
| 211 |
*ret = TAKE_PTR(e); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
/* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */ |
| 216 |
int battery_read_capacity_percentage(sd_device *dev) { |
| 217 |
int battery_capacity, r; |
| 218 |
|
| 219 |
assert(dev); |
| 220 |
|
| 221 |
r = device_get_sysattr_int(dev, "capacity", &battery_capacity); |
| 222 |
if (r < 0) |
| 223 |
return log_device_debug_errno(dev, r, "Failed to read/parse POWER_SUPPLY_CAPACITY: %m"); |
| 224 |
|
| 225 |
if (battery_capacity < 0 || battery_capacity > 100) |
| 226 |
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity: %d", battery_capacity); |
| 227 |
|
| 228 |
return battery_capacity; |
| 229 |
} |
| 230 |
|
| 231 |
/* If a battery whose percentage capacity is <= 5% exists, and we're not on AC power, return success */ |
| 232 |
int battery_is_discharging_and_low(void) { |
| 233 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 234 |
bool unsure = false, found_low = false; |
| 235 |
int r; |
| 236 |
|
| 237 |
/* We have not used battery capacity_level since value is set to full |
| 238 |
* or Normal in case ACPI is not working properly. In case of no battery |
| 239 |
* 0 will be returned and system will be suspended for 1st cycle then hibernated */ |
| 240 |
|
| 241 |
r = on_ac_power(); |
| 242 |
if (r < 0) |
| 243 |
log_warning_errno(r, "Failed to check if the system is running on AC, assuming it is not: %m"); |
| 244 |
if (r > 0) |
| 245 |
return false; |
| 246 |
|
| 247 |
r = battery_enumerator_new(&e); |
| 248 |
if (r < 0) |
| 249 |
return log_error_errno(r, "Failed to initialize battery enumerator: %m"); |
| 250 |
|
| 251 |
FOREACH_DEVICE(e, dev) { |
| 252 |
int level; |
| 253 |
|
| 254 |
level = battery_read_capacity_percentage(dev); |
| 255 |
if (level < 0) { |
| 256 |
unsure = true; |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
if (level > BATTERY_LOW_CAPACITY_LEVEL) { /* Found a charged battery */ |
| 261 |
log_device_full(dev, |
| 262 |
found_low ? LOG_INFO : LOG_DEBUG, |
| 263 |
"Found battery with capacity above threshold (%d%% > %d%%).", |
| 264 |
level, BATTERY_LOW_CAPACITY_LEVEL); |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
log_device_info(dev, |
| 269 |
"Found battery with capacity below threshold (%d%% <= %d%%).", |
| 270 |
level, BATTERY_LOW_CAPACITY_LEVEL); |
| 271 |
found_low = true; |
| 272 |
} |
| 273 |
|
| 274 |
/* If we found a battery whose state we couldn't read, don't assume we are in low battery state */ |
| 275 |
if (unsure) { |
| 276 |
log_notice("Found battery with unreadable state, assuming not in low battery state."); |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
/* If found neither charged nor low batteries, assume that we aren't in low battery state */ |
| 281 |
return found_low; |
| 282 |
} |