mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-22 22:03:43 +03:00
sleep: simplify code a bit
- use device_get_sysattr_int(), - drop redundant log message. (cherry picked from commit 3d9ca76f368b7b198be3471dd28ed32b35114ace)
This commit is contained in:
parent
b84a05fc57
commit
a251e6aa47
@ -22,6 +22,7 @@
|
|||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
#include "conf-parser.h"
|
#include "conf-parser.h"
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
|
#include "device-private.h"
|
||||||
#include "device-util.h"
|
#include "device-util.h"
|
||||||
#include "devnum-util.h"
|
#include "devnum-util.h"
|
||||||
#include "env-util.h"
|
#include "env-util.h"
|
||||||
@ -170,18 +171,13 @@ static int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
|
|||||||
|
|
||||||
/* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */
|
/* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */
|
||||||
static int read_battery_capacity_percentage(sd_device *dev) {
|
static int read_battery_capacity_percentage(sd_device *dev) {
|
||||||
const char *power_supply_capacity;
|
|
||||||
int battery_capacity, r;
|
int battery_capacity, r;
|
||||||
|
|
||||||
assert(dev);
|
assert(dev);
|
||||||
|
|
||||||
r = sd_device_get_property_value(dev, "POWER_SUPPLY_CAPACITY", &power_supply_capacity);
|
r = device_get_sysattr_int(dev, "capacity", &battery_capacity);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_debug_errno(dev, r, "Failed to get property POWER_SUPPLY_CAPACITY: %m");
|
return log_device_debug_errno(dev, r, "Failed to read/parse POWER_SUPPLY_CAPACITY: %m");
|
||||||
|
|
||||||
r = safe_atoi(power_supply_capacity, &battery_capacity);
|
|
||||||
if (r < 0)
|
|
||||||
return log_device_debug_errno(dev, r, "Failed to parse property POWER_SUPPLY_CAPACITY: %m");
|
|
||||||
|
|
||||||
if (battery_capacity < 0 || battery_capacity > 100)
|
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");
|
||||||
@ -203,15 +199,9 @@ int battery_is_low(void) {
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
|
return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
|
||||||
|
|
||||||
FOREACH_DEVICE(e, dev) {
|
FOREACH_DEVICE(e, dev)
|
||||||
r = read_battery_capacity_percentage(dev);
|
if (read_battery_capacity_percentage(dev) > BATTERY_LOW_CAPACITY_LEVEL)
|
||||||
if (r < 0) {
|
|
||||||
log_device_debug_errno(dev, r, "Failed to get battery capacity, ignoring: %m");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (r > BATTERY_LOW_CAPACITY_LEVEL)
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user