2aacef
From d46e822fad4b5d43d5e53e21c7de3168bb547ded Mon Sep 17 00:00:00 2001
2aacef
From: Yu Watanabe <watanabe.yu+github@gmail.com>
2aacef
Date: Mon, 14 Nov 2022 02:46:53 +0900
2aacef
Subject: [PATCH] sleep: simplify code a bit
2aacef
2aacef
- use device_get_sysattr_int(),
2aacef
- drop redundant log message.
2aacef
2aacef
(cherry picked from commit 3d9ca76f368b7b198be3471dd28ed32b35114ace)
2aacef
2aacef
Related: #2151612
2aacef
---
2aacef
 src/shared/sleep-config.c | 20 +++++---------------
2aacef
 1 file changed, 5 insertions(+), 15 deletions(-)
2aacef
2aacef
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c
2aacef
index 74653effa2..25c3bd2925 100644
2aacef
--- a/src/shared/sleep-config.c
2aacef
+++ b/src/shared/sleep-config.c
2aacef
@@ -22,6 +22,7 @@
2aacef
 #include "btrfs-util.h"
2aacef
 #include "conf-parser.h"
2aacef
 #include "def.h"
2aacef
+#include "device-private.h"
2aacef
 #include "device-util.h"
2aacef
 #include "devnum-util.h"
2aacef
 #include "env-util.h"
2aacef
@@ -170,18 +171,13 @@ static int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
2aacef
 
2aacef
 /* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */
2aacef
 static int read_battery_capacity_percentage(sd_device *dev) {
2aacef
-        const char *power_supply_capacity;
2aacef
         int battery_capacity, r;
2aacef
 
2aacef
         assert(dev);
2aacef
 
2aacef
-        r = sd_device_get_property_value(dev, "POWER_SUPPLY_CAPACITY", &power_supply_capacity);
2aacef
+        r = device_get_sysattr_int(dev, "capacity", &battery_capacity);
2aacef
         if (r < 0)
2aacef
-                return log_device_debug_errno(dev, r, "Failed to read battery capacity: %m");
2aacef
-
2aacef
-        r = safe_atoi(power_supply_capacity, &battery_capacity);
2aacef
-        if (r < 0)
2aacef
-                return log_device_debug_errno(dev, r, "Failed to parse battery capacity: %m");
2aacef
+                return log_device_debug_errno(dev, r, "Failed to read/parse POWER_SUPPLY_CAPACITY: %m");
2aacef
 
2aacef
         if (battery_capacity < 0 || battery_capacity > 100)
2aacef
                 return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity");
2aacef
@@ -203,15 +199,9 @@ int battery_is_low(void) {
2aacef
         if (r < 0)
2aacef
                 return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
2aacef
 
2aacef
-        FOREACH_DEVICE(e, dev) {
2aacef
-                r = read_battery_capacity_percentage(dev);
2aacef
-                if (r < 0) {
2aacef
-                        log_device_debug_errno(dev, r, "Failed to get battery capacity, ignoring: %m");
2aacef
-                        continue;
2aacef
-                }
2aacef
-                if (r > BATTERY_LOW_CAPACITY_LEVEL)
2aacef
+        FOREACH_DEVICE(e, dev)
2aacef
+                if (read_battery_capacity_percentage(dev) > BATTERY_LOW_CAPACITY_LEVEL)
2aacef
                         return false;
2aacef
-        }
2aacef
 
2aacef
         return true;
2aacef
 }