From: Lennart Poettering Date: Mon, 5 Jun 2023 09:56:31 +0000 (+0200) Subject: battery-util: move battery_is_discharging_and_low() to battery-util.[ch] X-Git-Tag: v254-rc1~285^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=319c46483e3eb1a3c33a3ca97b90ba13c3987da1;p=thirdparty%2Fsystemd.git battery-util: move battery_is_discharging_and_low() to battery-util.[ch] This moves a first batch of functions from sleep-config.[ch] over to battery-util.[ch]. In the long run we should probably move even more stuff over, i.e. anything that deals with the battery sysfs driver interface. No code change. --- diff --git a/src/shared/battery-util.c b/src/shared/battery-util.c index aa93b531a05..b11b6dc9896 100644 --- a/src/shared/battery-util.c +++ b/src/shared/battery-util.c @@ -7,6 +7,8 @@ #include "string-util.h" #include "battery-util.h" +#define BATTERY_LOW_CAPACITY_LEVEL 5 + 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; @@ -176,3 +178,81 @@ int on_ac_power(void) { return true; } } + +/* Get the list of batteries */ +int battery_enumerator_new(sd_device_enumerator **ret) { + _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + int r; + + assert(ret); + + r = sd_device_enumerator_new(&e); + if (r < 0) + return r; + + r = sd_device_enumerator_add_match_subsystem(e, "power_supply", /* match = */ true); + if (r < 0) + return r; + + r = sd_device_enumerator_allow_uninitialized(e); + if (r < 0) + return r; + + r = sd_device_enumerator_add_match_sysattr(e, "type", "Battery", /* match = */ true); + if (r < 0) + return r; + + r = sd_device_enumerator_add_match_sysattr(e, "present", "1", /* match = */ true); + if (r < 0) + return r; + + r = sd_device_enumerator_add_match_sysattr(e, "scope", "Device", /* match = */ false); + if (r < 0) + return r; + + *ret = TAKE_PTR(e); + return 0; +} + +/* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */ +int battery_read_capacity_percentage(sd_device *dev) { + int battery_capacity, r; + + assert(dev); + + r = device_get_sysattr_int(dev, "capacity", &battery_capacity); + if (r < 0) + 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 battery_capacity; +} + +/* If a battery whose percentage capacity is <= 5% exists, and we're not on AC power, return success */ +int battery_is_discharging_and_low(void) { + _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; + sd_device *dev; + int r; + + /* We have not used battery capacity_level since value is set to full + * or Normal in case ACPI is not working properly. In case of no battery + * 0 will be returned and system will be suspended for 1st cycle then hibernated */ + + 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"); + if (r > 0) + return false; + + r = battery_enumerator_new(&e); + if (r < 0) + return log_debug_errno(r, "Failed to initialize battery enumerator: %m"); + + FOREACH_DEVICE(e, dev) + if (battery_read_capacity_percentage(dev) > BATTERY_LOW_CAPACITY_LEVEL) + return false; + + return true; +} diff --git a/src/shared/battery-util.h b/src/shared/battery-util.h index 4089fff0c4e..c58f30b0b15 100644 --- a/src/shared/battery-util.h +++ b/src/shared/battery-util.h @@ -1,4 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include "sd-device.h" + int on_ac_power(void); + +int battery_is_discharging_and_low(void); + +int battery_enumerator_new(sd_device_enumerator **ret); +int battery_read_capacity_percentage(sd_device *dev); diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 51aebf2f0dc..4fd129b27ef 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -44,7 +44,6 @@ #include "strv.h" #include "time-util.h" -#define BATTERY_LOW_CAPACITY_LEVEL 5 #define DISCHARGE_RATE_FILEPATH "/var/lib/systemd/sleep/battery_discharge_percentage_rate_per_hour" #define BATTERY_DISCHARGE_RATE_HASH_KEY SD_ID128_MAKE(5f,9a,20,18,38,76,46,07,8d,36,58,0b,bb,c4,e0,63) #define SYS_ENTRY_RAW_FILE_TYPE1 "/sys/firmware/dmi/entries/1-0/raw" @@ -128,41 +127,6 @@ int parse_sleep_config(SleepConfig **ret_sleep_config) { return 0; } -/* Get the list of batteries */ -static int battery_enumerator_new(sd_device_enumerator **ret) { - _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; - int r; - - assert(ret); - - r = sd_device_enumerator_new(&e); - if (r < 0) - return r; - - r = sd_device_enumerator_add_match_subsystem(e, "power_supply", /* match = */ true); - if (r < 0) - return r; - - r = sd_device_enumerator_allow_uninitialized(e); - if (r < 0) - return r; - - r = sd_device_enumerator_add_match_sysattr(e, "type", "Battery", /* match = */ true); - if (r < 0) - return r; - - r = sd_device_enumerator_add_match_sysattr(e, "present", "1", /* match = */ true); - if (r < 0) - return r; - - r = sd_device_enumerator_add_match_sysattr(e, "scope", "Device", /* match = */ false); - if (r < 0) - return r; - - *ret = TAKE_PTR(e); - return 0; -} - int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) { void *p; @@ -176,49 +140,6 @@ int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) { return PTR_TO_CAPACITY(p); } -/* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */ -static int read_battery_capacity_percentage(sd_device *dev) { - int battery_capacity, r; - - assert(dev); - - r = device_get_sysattr_int(dev, "capacity", &battery_capacity); - if (r < 0) - 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 battery_capacity; -} - -/* If a battery whose percentage capacity is <= 5% exists, and we're not on AC power, return success */ -int battery_is_discharging_and_low(void) { - _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; - sd_device *dev; - int r; - - /* We have not used battery capacity_level since value is set to full - * or Normal in case ACPI is not working properly. In case of no battery - * 0 will be returned and system will be suspended for 1st cycle then hibernated */ - - 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"); - if (r > 0) - return false; - - r = battery_enumerator_new(&e); - if (r < 0) - return log_debug_errno(r, "Failed to initialize battery enumerator: %m"); - - FOREACH_DEVICE(e, dev) - if (read_battery_capacity_percentage(dev) > BATTERY_LOW_CAPACITY_LEVEL) - return false; - - return true; -} - /* Store current capacity of each battery before suspension and timestamp */ int fetch_batteries_capacity_by_name(Hashmap **ret) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; @@ -241,7 +162,7 @@ int fetch_batteries_capacity_by_name(Hashmap **ret) { const char *battery_name; int battery_capacity; - battery_capacity = r = read_battery_capacity_percentage(dev); + battery_capacity = r = battery_read_capacity_percentage(dev); if (r < 0) continue; diff --git a/src/shared/sleep-config.h b/src/shared/sleep-config.h index 5c528b890b9..b8245b129f0 100644 --- a/src/shared/sleep-config.h +++ b/src/shared/sleep-config.h @@ -60,7 +60,6 @@ int find_hibernate_location(HibernateLocation **ret_hibernate_location); int can_sleep(SleepOperation operation); int can_sleep_disk(char **types); int can_sleep_state(char **types); -int battery_is_discharging_and_low(void); int get_total_suspend_interval(Hashmap *last_capacity, usec_t *ret); int fetch_batteries_capacity_by_name(Hashmap **ret_current_capacity); int get_capacity_by_name(Hashmap *capacities_by_name, const char *name); diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index a9445c1a619..0dc3ad35586 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -17,6 +17,7 @@ #include "sd-bus.h" #include "sd-messages.h" +#include "battery-util.h" #include "btrfs-util.h" #include "build.h" #include "bus-error.h"