summaryrefslogtreecommitdiffstats
path: root/src/sleep
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2023-09-21 07:45:05 +0200
committerMike Yuan <me@yhndnzj.com>2023-09-27 15:48:39 +0200
commitf3afe9dc207dd3ef89925b3e3f47a0c3c9543854 (patch)
tree018205d25d8162eaf2616231e2f578e0927d21d4 /src/sleep
parentsleep/battery-capacity: drop unused error-handling (diff)
downloadsystemd-f3afe9dc207dd3ef89925b3e3f47a0c3c9543854.tar.xz
systemd-f3afe9dc207dd3ef89925b3e3f47a0c3c9543854.zip
sleep/battery-capacity: rearrange functions
Diffstat (limited to 'src/sleep')
-rw-r--r--src/sleep/battery-capacity.c120
-rw-r--r--src/sleep/battery-capacity.h4
2 files changed, 63 insertions, 61 deletions
diff --git a/src/sleep/battery-capacity.c b/src/sleep/battery-capacity.c
index 43dc05db65..62a0746bac 100644
--- a/src/sleep/battery-capacity.c
+++ b/src/sleep/battery-capacity.c
@@ -30,66 +30,6 @@ static int PTR_TO_CAPACITY(void *p) {
return capacity;
}
-int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
- void *p;
-
- assert(capacities_by_name);
- assert(name);
-
- p = hashmap_get(capacities_by_name, name);
- if (!p)
- return -ENOENT;
-
- return PTR_TO_CAPACITY(p);
-}
-
-/* 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;
- _cleanup_hashmap_free_ Hashmap *batteries_capacity_by_name = NULL;
- int r;
-
- assert(ret);
-
- batteries_capacity_by_name = hashmap_new(&string_hash_ops_free);
- if (!batteries_capacity_by_name)
- return log_oom_debug();
-
- r = battery_enumerator_new(&e);
- if (r < 0)
- return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
-
- FOREACH_DEVICE(e, dev) {
- _cleanup_free_ char *battery_name_copy = NULL;
- const char *battery_name;
- int battery_capacity;
-
- battery_capacity = r = battery_read_capacity_percentage(dev);
- if (r < 0)
- continue;
-
- r = sd_device_get_property_value(dev, "POWER_SUPPLY_NAME", &battery_name);
- if (r < 0) {
- log_device_debug_errno(dev, r, "Failed to get POWER_SUPPLY_NAME property, ignoring: %m");
- continue;
- }
-
- battery_name_copy = strdup(battery_name);
- if (!battery_name_copy)
- return log_oom_debug();
-
- r = hashmap_put(batteries_capacity_by_name, battery_name_copy, CAPACITY_TO_PTR(battery_capacity));
- if (r < 0)
- return log_device_debug_errno(dev, r, "Failed to store battery capacity: %m");
-
- TAKE_PTR(battery_name_copy);
- }
-
- *ret = TAKE_PTR(batteries_capacity_by_name);
-
- return 0;
-}
-
static int siphash24_compress_device_sysattr(
sd_device *dev,
const char *attr,
@@ -234,6 +174,66 @@ static int put_battery_discharge_rate(int estimated_battery_discharge_rate, uint
return 0;
}
+/* 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;
+ _cleanup_hashmap_free_ Hashmap *batteries_capacity_by_name = NULL;
+ int r;
+
+ assert(ret);
+
+ batteries_capacity_by_name = hashmap_new(&string_hash_ops_free);
+ if (!batteries_capacity_by_name)
+ return log_oom_debug();
+
+ r = battery_enumerator_new(&e);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
+
+ FOREACH_DEVICE(e, dev) {
+ _cleanup_free_ char *battery_name_copy = NULL;
+ const char *battery_name;
+ int battery_capacity;
+
+ battery_capacity = r = battery_read_capacity_percentage(dev);
+ if (r < 0)
+ continue;
+
+ r = sd_device_get_property_value(dev, "POWER_SUPPLY_NAME", &battery_name);
+ if (r < 0) {
+ log_device_debug_errno(dev, r, "Failed to get POWER_SUPPLY_NAME property, ignoring: %m");
+ continue;
+ }
+
+ battery_name_copy = strdup(battery_name);
+ if (!battery_name_copy)
+ return log_oom_debug();
+
+ r = hashmap_put(batteries_capacity_by_name, battery_name_copy, CAPACITY_TO_PTR(battery_capacity));
+ if (r < 0)
+ return log_device_debug_errno(dev, r, "Failed to store battery capacity: %m");
+
+ TAKE_PTR(battery_name_copy);
+ }
+
+ *ret = TAKE_PTR(batteries_capacity_by_name);
+
+ return 0;
+}
+
+int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
+ void *p;
+
+ assert(capacities_by_name);
+ assert(name);
+
+ p = hashmap_get(capacities_by_name, name);
+ if (!p)
+ return -ENOENT;
+
+ return PTR_TO_CAPACITY(p);
+}
+
/* Estimate battery discharge rate using stored previous and current capacity over timestamp difference */
int estimate_battery_discharge_rate_per_hour(
Hashmap *last_capacity,
diff --git a/src/sleep/battery-capacity.h b/src/sleep/battery-capacity.h
index 283b0f2621..df7b06ce5a 100644
--- a/src/sleep/battery-capacity.h
+++ b/src/sleep/battery-capacity.h
@@ -4,9 +4,11 @@
#include "hashmap.h"
#include "time-util.h"
-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);
+
+int get_total_suspend_interval(Hashmap *last_capacity, usec_t *ret);
+
int estimate_battery_discharge_rate_per_hour(
Hashmap *last_capacity,
Hashmap *current_capacity,