static int device_is_power_sink(sd_device *device) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
bool found_source = false, found_sink = false;
- sd_device *parent, *d;
+ sd_device *parent;
int r;
assert(device);
int on_ac_power(void) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
bool found_ac_online = false, found_discharging_battery = false;
- sd_device *d;
int r;
r = sd_device_enumerator_new(&e);
return log_device_debug_errno(dev, r, "Failed to read/parse POWER_SUPPLY_CAPACITY: %m");
if (battery_capacity < 0 || battery_capacity > 100)
- return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity");
+ return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity: %d", battery_capacity);
return battery_capacity;
}
int battery_is_discharging_and_low(void) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
bool unsure = false, found_low = false;
- sd_device *dev;
int r;
/* We have not used battery capacity_level since value is set to full
r = on_ac_power();
if (r < 0)
- log_debug_errno(r, "Failed to check if the system is running on AC, assuming it is not: %m");
+ log_warning_errno(r, "Failed to check if the system is running on AC, assuming it is not: %m");
if (r > 0)
return false;
r = battery_enumerator_new(&e);
if (r < 0)
- return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
+ return log_error_errno(r, "Failed to initialize battery enumerator: %m");
FOREACH_DEVICE(e, dev) {
int level;
continue;
}
- if (level > BATTERY_LOW_CAPACITY_LEVEL) /* Found a charged battery */
+ if (level > BATTERY_LOW_CAPACITY_LEVEL) { /* Found a charged battery */
+ log_device_full(dev,
+ found_low ? LOG_INFO : LOG_DEBUG,
+ "Found battery with capacity above threshold (%d%% > %d%%).",
+ level, BATTERY_LOW_CAPACITY_LEVEL);
return false;
+ }
+ log_device_info(dev,
+ "Found battery with capacity below threshold (%d%% <= %d%%).",
+ level, BATTERY_LOW_CAPACITY_LEVEL);
found_low = true;
}
/* If we found a battery whose state we couldn't read, don't assume we are in low battery state */
- if (unsure)
+ if (unsure) {
+ log_notice("Found battery with unreadable state, assuming not in low battery state.");
return false;
+ }
- /* Found no charged battery, but did find low batteries */
- if (found_low)
- return true;
-
- /* Found neither charged nor low batteries? let's return that we aren't on low battery state */
- return false;
+ /* If found neither charged nor low batteries, assume that we aren't in low battery state */
+ return found_low;
}