sleep-config: several cleanups

* Rename free_sleep_config to sleep_config_free
* Rearrange functions
* Make SleepConfig.modes and .states only contain
  operations that needs configuration
* Add missing assert
This commit is contained in:
Mike Yuan
2023-09-21 14:59:26 +08:00
parent f3afe9dc20
commit 087a25d2ca
4 changed files with 43 additions and 36 deletions

View File

@@ -22,10 +22,32 @@
#include "strv.h"
#include "time-util.h"
int parse_sleep_config(SleepConfig **ret_sleep_config) {
_cleanup_(free_sleep_configp) SleepConfig *sc = NULL;
int allow_suspend = -1, allow_hibernate = -1,
allow_s2h = -1, allow_hybrid_sleep = -1;
static const char* const sleep_operation_table[_SLEEP_OPERATION_MAX] = {
[SLEEP_SUSPEND] = "suspend",
[SLEEP_HIBERNATE] = "hibernate",
[SLEEP_HYBRID_SLEEP] = "hybrid-sleep",
[SLEEP_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate",
};
DEFINE_STRING_TABLE_LOOKUP(sleep_operation, SleepOperation);
SleepConfig* sleep_config_free(SleepConfig *sc) {
if (!sc)
return NULL;
for (SleepOperation i = 0; i < _SLEEP_OPERATION_CONFIG_MAX; i++) {
strv_free(sc->modes[i]);
strv_free(sc->states[i]);
}
return mfree(sc);
}
int parse_sleep_config(SleepConfig **ret) {
_cleanup_(sleep_config_freep) SleepConfig *sc = NULL;
int allow_suspend = -1, allow_hibernate = -1, allow_s2h = -1, allow_hybrid_sleep = -1;
assert(ret);
sc = new(SleepConfig, 1);
if (!sc)
@@ -83,7 +105,7 @@ int parse_sleep_config(SleepConfig **ret_sleep_config) {
|| !sc->states[SLEEP_HIBERNATE] || !sc->modes[SLEEP_HYBRID_SLEEP] || !sc->states[SLEEP_HYBRID_SLEEP])
return log_oom();
*ret_sleep_config = TAKE_PTR(sc);
*ret = TAKE_PTR(sc);
return 0;
}
@@ -227,7 +249,7 @@ static int can_sleep_internal(
}
int can_sleep(SleepOperation operation) {
_cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
_cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
int r;
r = parse_sleep_config(&sleep_config);
@@ -236,24 +258,3 @@ int can_sleep(SleepOperation operation) {
return can_sleep_internal(sleep_config, operation, true);
}
SleepConfig* free_sleep_config(SleepConfig *sc) {
if (!sc)
return NULL;
for (SleepOperation i = 0; i < _SLEEP_OPERATION_MAX; i++) {
strv_free(sc->modes[i]);
strv_free(sc->states[i]);
}
return mfree(sc);
}
static const char* const sleep_operation_table[_SLEEP_OPERATION_MAX] = {
[SLEEP_SUSPEND] = "suspend",
[SLEEP_HIBERNATE] = "hibernate",
[SLEEP_HYBRID_SLEEP] = "hybrid-sleep",
[SLEEP_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate",
};
DEFINE_STRING_TABLE_LOOKUP(sleep_operation, SleepOperation);

View File

@@ -9,27 +9,33 @@ typedef enum SleepOperation {
SLEEP_SUSPEND,
SLEEP_HIBERNATE,
SLEEP_HYBRID_SLEEP,
_SLEEP_OPERATION_CONFIG_MAX,
/* The operations above require configuration for mode and state. The ones below are "combined"
* operations that use config from those individual operations. */
SLEEP_SUSPEND_THEN_HIBERNATE,
_SLEEP_OPERATION_MAX,
_SLEEP_OPERATION_INVALID = -EINVAL,
} SleepOperation;
const char* sleep_operation_to_string(SleepOperation s) _const_;
SleepOperation sleep_operation_from_string(const char *s) _pure_;
typedef struct SleepConfig {
bool allow[_SLEEP_OPERATION_MAX];
char **modes[_SLEEP_OPERATION_MAX];
char **states[_SLEEP_OPERATION_MAX];
char **modes[_SLEEP_OPERATION_CONFIG_MAX];
char **states[_SLEEP_OPERATION_CONFIG_MAX];
usec_t hibernate_delay_usec;
usec_t suspend_estimation_usec;
} SleepConfig;
SleepConfig* free_sleep_config(SleepConfig *sc);
DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, free_sleep_config);
SleepConfig* sleep_config_free(SleepConfig *sc);
DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, sleep_config_free);
int parse_sleep_config(SleepConfig **sleep_config);
int can_sleep(SleepOperation operation);
int can_sleep_disk(char **types);
int can_sleep_state(char **types);
const char* sleep_operation_to_string(SleepOperation s) _const_;
SleepOperation sleep_operation_from_string(const char *s) _pure_;

View File

@@ -612,7 +612,7 @@ static int parse_argv(int argc, char *argv[]) {
}
static int run(int argc, char *argv[]) {
_cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
_cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
int r;
log_setup();

View File

@@ -10,7 +10,7 @@
#include "tests.h"
TEST(parse_sleep_config) {
_cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
_cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
assert_se(parse_sleep_config(&sleep_config) == 0);