sleep: use current charge level to decide suspension

If battery current charge percentage is below 5% hibernate directly.
Else initial suspend interval is set for HibernateDelaySec. On wakeup
estimate battery discharge rate per hour and if battery charge
percentage is not below 5% system is suspended else hibernated.
This commit is contained in:
Sonali Srivastava
2022-07-20 14:46:43 +05:30
parent 9f3a3ac70f
commit 96d662fa4c
3 changed files with 98 additions and 23 deletions
+38
View File
@@ -102,6 +102,44 @@ int parse_sleep_config(SleepConfig **ret_sleep_config) {
return 0;
}
/* If battery percentage capacity is less than equal to 5% return success */
int battery_is_low(void) {
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 = read_battery_capacity_percentage();
if (r == -ENOENT)
return false;
if (r < 0)
return r;
return r <= 5;
}
/* Battery percentage capacity fetched from capacity file and if in range 0-100 then returned */
int read_battery_capacity_percentage(void) {
_cleanup_free_ char *bat_cap = NULL;
int battery_capacity, r;
r = read_one_line_file("/sys/class/power_supply/BAT0/capacity", &bat_cap);
if (r == -ENOENT)
return log_debug_errno(r, "/sys/class/power_supply/BAT0/capacity is unavailable. Assuming no battery exists: %m");
if (r < 0)
return log_debug_errno(r, "Failed to read /sys/class/power_supply/BAT0/capacity: %m");
r = safe_atoi(bat_cap, &battery_capacity);
if (r < 0)
return log_debug_errno(r, "Failed to parse battery capacity: %m");
if (battery_capacity < 0 || battery_capacity > 100)
return log_debug_errno(SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity");
return battery_capacity;
}
int can_sleep_state(char **types) {
_cleanup_free_ char *text = NULL;
int r;
+2
View File
@@ -55,6 +55,8 @@ 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 read_battery_capacity_percentage(void);
int battery_is_low(void);
const char* sleep_operation_to_string(SleepOperation s) _const_;
SleepOperation sleep_operation_from_string(const char *s) _pure_;