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