]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sleep-config.h
Merge pull request #31648 from neighbourhoodie/review-content
[thirdparty/systemd.git] / src / shared / sleep-config.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "strv.h"
5 #include "time-util.h"
6
7 typedef enum SleepOperation {
8 SLEEP_SUSPEND,
9 SLEEP_HIBERNATE,
10 SLEEP_HYBRID_SLEEP,
11 _SLEEP_OPERATION_CONFIG_MAX,
12 /* The operations above require configuration for mode and state. The ones below are "combined"
13 * operations that use config from those individual operations. */
14
15 SLEEP_SUSPEND_THEN_HIBERNATE,
16
17 _SLEEP_OPERATION_MAX,
18 _SLEEP_OPERATION_INVALID = -EINVAL,
19 } SleepOperation;
20
21 const char* sleep_operation_to_string(SleepOperation s) _const_;
22 SleepOperation sleep_operation_from_string(const char *s) _pure_;
23
24 static inline bool SLEEP_OPERATION_IS_HIBERNATION(SleepOperation operation) {
25 return IN_SET(operation, SLEEP_HIBERNATE, SLEEP_HYBRID_SLEEP);
26 }
27
28 typedef struct SleepConfig {
29 bool allow[_SLEEP_OPERATION_MAX];
30
31 char **states[_SLEEP_OPERATION_CONFIG_MAX];
32 char **modes[_SLEEP_OPERATION_CONFIG_MAX]; /* Power mode after writing hibernation image (/sys/power/disk) */
33 char **mem_modes; /* /sys/power/mem_sleep */
34
35 usec_t hibernate_delay_usec;
36 usec_t suspend_estimation_usec;
37 } SleepConfig;
38
39 SleepConfig* sleep_config_free(SleepConfig *sc);
40 DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, sleep_config_free);
41
42 int parse_sleep_config(SleepConfig **sleep_config);
43
44 static inline bool SLEEP_NEEDS_MEM_SLEEP(const SleepConfig *sc, SleepOperation operation) {
45 assert(sc);
46 assert(operation >= 0 && operation < _SLEEP_OPERATION_CONFIG_MAX);
47
48 /* As per https://docs.kernel.org/admin-guide/pm/sleep-states.html#basic-sysfs-interfaces-for-system-suspend-and-hibernation,
49 * /sys/power/mem_sleep is honored if /sys/power/state is set to "mem" (common for suspend)
50 * or /sys/power/disk is set to "suspend" (hybrid-sleep). */
51
52 return strv_contains(sc->states[operation], "mem") ||
53 strv_contains(sc->modes[operation], "suspend");
54 }
55
56 typedef enum SleepSupport {
57 SLEEP_SUPPORTED,
58 SLEEP_DISABLED, /* Disabled in SleepConfig.allow */
59 SLEEP_NOT_CONFIGURED, /* SleepConfig.states is not configured */
60 SLEEP_STATE_OR_MODE_NOT_SUPPORTED, /* SleepConfig.states/modes are not supported by kernel */
61 SLEEP_RESUME_NOT_SUPPORTED,
62 SLEEP_NOT_ENOUGH_SWAP_SPACE,
63 SLEEP_ALARM_NOT_SUPPORTED, /* CLOCK_BOOTTIME_ALARM is unsupported by kernel (only used by s2h) */
64 } SleepSupport;
65
66 int sleep_supported_full(SleepOperation operation, SleepSupport *ret_support);
67 static inline int sleep_supported(SleepOperation operation) {
68 return sleep_supported_full(operation, NULL);
69 }
70
71 /* Only for test-sleep-config */
72 int sleep_state_supported(char * const *states);
73 int sleep_mode_supported(const char *path, char * const *modes);