]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
battery-util: move battery_is_discharging_and_low() to battery-util.[ch]
authorLennart Poettering <lennart@poettering.net>
Mon, 5 Jun 2023 09:56:31 +0000 (11:56 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 5 Jun 2023 10:19:32 +0000 (12:19 +0200)
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.

src/shared/battery-util.c
src/shared/battery-util.h
src/shared/sleep-config.c
src/shared/sleep-config.h
src/sleep/sleep.c

index aa93b531a05e704b6433d1098ac37e945721a92d..b11b6dc98960cc8e38b7f5fa037a2f5d028575f1 100644 (file)
@@ -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;
+}
index 4089fff0c4ec3213dd90f37266121f59a8d0dc21..c58f30b0b155c76952820bdc28143a8759a82bb0 100644 (file)
@@ -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);
index 51aebf2f0dcde9380357f293b5ce6b4b44fb7ffd..4fd129b27efe5815e822e3a4c5f531e17ce02769 100644 (file)
@@ -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;
 
index 5c528b890b944ea01ff30ffe4738bd5f2d961239..b8245b129f03c950fd448c7e394aa777625b2d87 100644 (file)
@@ -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);
index a9445c1a619d37fba2588ccd3ef64414b212f288..0dc3ad35586e2ad9af2b82854601807c4c5c9dbf 100644 (file)
@@ -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"