From bab81b4522e739a2f5cc3046b4d50363b806fe79 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 14 Dec 2022 18:20:05 +0900 Subject: [PATCH] power: redefine deviced-exclusive power state enum Do not use enums from device API for the deviced power module. Instead, the deviced defines its own enums prefixed with DEVICED_POWER. When the deviced receives request from device API with API enum, then it is converted to the deviced enum, and vice versa. Change-Id: Ic67ba5c1bbbb1fa8335c15fde43cb13e9705eb0c Signed-off-by: Youngjae Cho --- src/battery/lowbat-handler.c | 4 +- src/power/power-boot.c | 2 +- src/power/power-dbus.c | 162 +++++++++++++++++++++++++++++++++++++------ src/power/power-off.c | 40 +++++------ src/power/power.c | 157 +++++++++++++++++++++-------------------- src/power/power.h | 100 +++++++++++++++++--------- 6 files changed, 317 insertions(+), 148 deletions(-) diff --git a/src/battery/lowbat-handler.c b/src/battery/lowbat-handler.c index 85ccd4d..0960194 100644 --- a/src/battery/lowbat-handler.c +++ b/src/battery/lowbat-handler.c @@ -239,7 +239,7 @@ direct_launch: if (launched_poweroff == 1) { _I("Will be foreced power off."); - power_execute(DEVICE_POWER_STATE_POWEROFF); + power_execute(DEVICED_POWER_STATE_POWEROFF); return 0; } @@ -293,7 +293,7 @@ static int battery_critical_low_act(void *data) static int battery_power_off_act(void *data) { CRITICAL_LOG("Low battery power off."); - return power_execute(DEVICE_POWER_STATE_POWEROFF); + return power_execute(DEVICED_POWER_STATE_POWEROFF); } int battery_charge_err_cf_act(void *data) diff --git a/src/power/power-boot.c b/src/power/power-boot.c index 7814edb..74ec729 100644 --- a/src/power/power-boot.c +++ b/src/power/power-boot.c @@ -177,7 +177,7 @@ void initial_transition_by_boot_condition(void) retval = hal_device_board_get_boot_reason(bc.reason, sizeof(bc.reason)); if (retval != 0) { _I("Failed to get BootReason, %d. Default change state to normal.", retval); - power_request_change_state_strict(DEVICE_POWER_STATE_START, DEVICE_POWER_STATE_NORMAL, 1, NULL); + power_request_change_state_strict(DEVICED_POWER_STATE_START, DEVICED_POWER_STATE_NORMAL, 1, NULL); return; } diff --git a/src/power/power-dbus.c b/src/power/power-dbus.c index b735121..4c5a292 100644 --- a/src/power/power-dbus.c +++ b/src/power/power-dbus.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,72 @@ struct lock_node { char locktime[64]; }; +static uint64_t convert_device_state_to_deviced(device_power_state_e state) +{ + if (state == DEVICE_POWER_STATE_START) + return DEVICED_POWER_STATE_START; + if (state == DEVICE_POWER_STATE_NORMAL) + return DEVICED_POWER_STATE_NORMAL; + if (state == DEVICE_POWER_STATE_SLEEP) + return DEVICED_POWER_STATE_SLEEP; + if (state == DEVICE_POWER_STATE_POWEROFF) + return DEVICED_POWER_STATE_POWEROFF; + if (state == DEVICE_POWER_STATE_REBOOT) + return DEVICED_POWER_STATE_REBOOT; + if (state == DEVICE_POWER_STATE_EXIT) + return DEVICED_POWER_STATE_EXIT; + + return DEVICED_POWER_STATE_UNDEFINED; +} + +static uint64_t convert_device_state_bitmap_to_deviced(device_power_state_e state) +{ + uint64_t deviced_bitmap = 0; + uint64_t bitmap = (uint64_t) state; + uint64_t lsb; + + while (bitmap) { + lsb = bitmap & -bitmap; + deviced_bitmap |= convert_device_state_to_deviced(lsb); + bitmap &= ~lsb; + } + + return deviced_bitmap; +} + +static uint64_t convert_device_transient_state_to_deviced(device_power_transient_state_e state) +{ + if (state == DEVICE_POWER_TRANSIENT_STATE_RESUMING_EARLY) + return DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY; + if (state == DEVICE_POWER_TRANSIENT_STATE_RESUMING) + return DEVICED_POWER_TRANSIENT_STATE_RESUMING; + if (state == DEVICE_POWER_TRANSIENT_STATE_RESUMING_LATE) + return DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE; + if (state == DEVICE_POWER_TRANSIENT_STATE_SUSPENDING_EARLY) + return DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY; + if (state == DEVICE_POWER_TRANSIENT_STATE_SUSPENDING) + return DEVICED_POWER_TRANSIENT_STATE_SUSPENDING; + if (state == DEVICE_POWER_TRANSIENT_STATE_SUSPENDING_LATE) + return DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE; + + return DEVICED_POWER_STATE_UNDEFINED; +} + +static uint64_t convert_device_transient_state_bitmap_to_deviced(device_power_transient_state_e state) +{ + uint64_t deviced_bitmap = 0; + uint64_t bitmap = (uint64_t) state; + uint64_t lsb; + + while (bitmap) { + lsb = bitmap & -bitmap; + deviced_bitmap |= convert_device_transient_state_to_deviced(lsb); + bitmap &= ~lsb; + } + + return deviced_bitmap; +} + static void print_lock_node(void) { GList *elem; @@ -236,7 +303,7 @@ static GVariant *dbus_power_add_change_state_wait(GDBusConnection *conn, g_variant_get(param, "(t)", &state); - ret = add_change_state_wait(pid, state); + ret = add_change_state_wait(pid, convert_device_state_bitmap_to_deviced(state)); out: return g_variant_new("(i)", ret); @@ -257,7 +324,51 @@ static GVariant *dbus_power_remove_change_state_wait(GDBusConnection *conn, g_variant_get(param, "(t)", &state); - remove_change_state_wait(pid, state); + remove_change_state_wait(pid, convert_device_state_bitmap_to_deviced(state)); + +out: + return gdbus_new_g_variant_tuple(); +} + +static GVariant *dbus_power_add_change_transient_state_wait(GDBusConnection *conn, + const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, + GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) +{ + int ret = 0; + pid_t pid; + guint64 state; + + pid = gdbus_connection_get_sender_pid(conn, sender); + if (pid == -1 || kill(pid, 0) == -1) { + _E("%d process does not exist, dbus ignored.", pid); + ret = -ESRCH; + goto out; + } + + g_variant_get(param, "(t)", &state); + + ret = add_change_state_wait(pid, convert_device_transient_state_bitmap_to_deviced(state)); + +out: + return g_variant_new("(i)", ret); +} + +static GVariant *dbus_power_remove_change_transient_state_wait(GDBusConnection *conn, + const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, + GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) +{ + pid_t pid; + guint64 state; + + pid = gdbus_connection_get_sender_pid(conn, sender); + if (pid == -1 || kill(pid, 0) == -1) { + _E("%d process does not exist, dbus ignored.", pid); + goto out; + } + + g_variant_get(param, "(t)", &state); + + remove_change_state_wait(pid, convert_device_transient_state_bitmap_to_deviced(state)); out: return gdbus_new_g_variant_tuple(); @@ -291,19 +402,16 @@ static GVariant *dbus_power_change_state(GDBusConnection *conn, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) { int ret = 0; - int index; - guint64 next; + uint64_t next; + guint64 device_next; pid_t pid; char comm[128] = "Unknown"; - g_variant_get(param, "(t)", &next); - - for (index = DEVICE_POWER_STATE_MIN_INDEX; index < DEVICE_POWER_STATE_MAX_INDEX; ++index) - if (next == (1UL << index)) - break; + g_variant_get(param, "(t)", &device_next); - if (index == DEVICE_POWER_STATE_MAX_INDEX) { - _E("Invalid next state=%#lx", (long) index); + next = convert_device_state_to_deviced(device_next); + if (next == DEVICED_POWER_STATE_UNDEFINED) { + _E("Invalid next state=%#"PRIx64, device_next); ret = -EINVAL; goto out; } @@ -320,7 +428,7 @@ static GVariant *dbus_power_change_state(GDBusConnection *conn, _I("Pid=%d(%s) sent request for PowerChangeState to %s", pid, comm, state_name(next)); - power_request_change_state_strict(DEVICE_POWER_STATE_ALL, next, 0, NULL); + power_request_change_state_strict(DEVICED_POWER_STATE_ALL, next, 0, NULL); out: return g_variant_new("(i)", ret); @@ -330,17 +438,31 @@ static GVariant *dbus_power_get_state(GDBusConnection *conn, const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) { - return g_variant_new("(t)", power_get_state()); + uint64_t current = power_get_state(); + int index = DEVICED_POWER_STATE_INDEX(current); + + static const uint64_t mapping_device_state[DEVICED_POWER_STATE_MAX_INDEX] = { + [DEVICED_POWER_STATE_START_INDEX] = DEVICE_POWER_STATE_START, + [DEVICED_POWER_STATE_NORMAL_INDEX] = DEVICE_POWER_STATE_NORMAL, + [DEVICED_POWER_STATE_SLEEP_INDEX] = DEVICE_POWER_STATE_SLEEP, + [DEVICED_POWER_STATE_POWEROFF_INDEX] = DEVICE_POWER_STATE_POWEROFF, + [DEVICED_POWER_STATE_REBOOT_INDEX] = DEVICE_POWER_STATE_REBOOT, + [DEVICED_POWER_STATE_EXIT_INDEX] = DEVICE_POWER_STATE_EXIT, + }; + + return g_variant_new("(t)", mapping_device_state[index]); } static const dbus_method_s dbus_methods[] = { - { "LockCpu", "i", "i", dbus_power_lock_cpu }, - { "UnlockCpu", NULL, "i", dbus_power_unlock_cpu }, - { "AddChangeStateWait", "t", "i", dbus_power_add_change_state_wait }, - { "RemoveChangeStateWait", "t", NULL, dbus_power_remove_change_state_wait }, - { "ConfirmChangeStateWait", "t", "i", dbus_power_confirm_change_state_wait }, - { "PowerChangeState", "t", "i", dbus_power_change_state }, - { "PowerGetState", NULL, "t", dbus_power_get_state }, + { "LockCpu", "i", "i", dbus_power_lock_cpu }, + { "UnlockCpu", NULL, "i", dbus_power_unlock_cpu }, + { "AddChangeStateWait", "t", "i", dbus_power_add_change_state_wait }, + { "RemoveChangeStateWait", "t", NULL, dbus_power_remove_change_state_wait }, + { "AddChangeTransientStateWait", "t", "i", dbus_power_add_change_transient_state_wait }, + { "RemoveChangeTransientStateWait", "t", NULL, dbus_power_remove_change_transient_state_wait }, + { "ConfirmChangeStateWait", "t", "i", dbus_power_confirm_change_state_wait }, + { "PowerChangeState", "t", "i", dbus_power_change_state }, + { "PowerGetState", NULL, "t", dbus_power_get_state }, /* Add methods here */ }; diff --git a/src/power/power-off.c b/src/power/power-off.c index 77059fe..37cecc3 100644 --- a/src/power/power-off.c +++ b/src/power/power-off.c @@ -138,11 +138,11 @@ void poweroff_request_shutdown(int state) if (!is_poweroff_state(state)) return; - if (state == DEVICE_POWER_STATE_POWEROFF) + if (state == DEVICED_POWER_STATE_POWEROFF) systemd_method = "PowerOff"; - else if (state == DEVICE_POWER_STATE_REBOOT) + else if (state == DEVICED_POWER_STATE_REBOOT) systemd_method = "Reboot"; - else if (state == DEVICE_POWER_STATE_EXIT) + else if (state == DEVICED_POWER_STATE_EXIT) systemd_method = "Exit"; CRITICAL_LOG("Requested %s via systemd.", systemd_method); @@ -191,18 +191,18 @@ static void poweroff_delay_for_seconds(void) int poweroff_check_revived(void) { if (access(POWEROFF_OPTPATH_POWEROFF, F_OK) == 0) { - return DEVICE_POWER_STATE_POWEROFF; + return DEVICED_POWER_STATE_POWEROFF; } if (access(POWEROFF_OPTPATH_REBOOT, F_OK) == 0) { - return DEVICE_POWER_STATE_REBOOT; + return DEVICED_POWER_STATE_REBOOT; } if (access(POWEROFF_OPTPATH_EXIT, F_OK) == 0) { - return DEVICE_POWER_STATE_EXIT; + return DEVICED_POWER_STATE_EXIT; } - return DEVICE_POWER_STATE_NORMAL; + return DEVICED_POWER_STATE_NORMAL; } static void mark_poweroff_option(uint64_t state, const char *option) @@ -211,11 +211,11 @@ static void mark_poweroff_option(uint64_t state, const char *option) ssize_t len; const char *optpath; - if (state == DEVICE_POWER_STATE_POWEROFF) + if (state == DEVICED_POWER_STATE_POWEROFF) optpath = POWEROFF_OPTPATH_POWEROFF; - else if (state == DEVICE_POWER_STATE_REBOOT) + else if (state == DEVICED_POWER_STATE_REBOOT) optpath = POWEROFF_OPTPATH_REBOOT; - else if (state == DEVICE_POWER_STATE_EXIT) + else if (state == DEVICED_POWER_STATE_EXIT) optpath = POWEROFF_OPTPATH_EXIT; else return; @@ -300,9 +300,9 @@ void poweroff_prepare(uint64_t state, void *option) mark_poweroff_option(state, option); - if (state == DEVICE_POWER_STATE_POWEROFF) + if (state == DEVICED_POWER_STATE_POWEROFF) vconf = VCONFKEY_SYSMAN_POWER_OFF_DIRECT; - else if (state == DEVICE_POWER_STATE_REBOOT) + else if (state == DEVICED_POWER_STATE_REBOOT) vconf = VCONFKEY_SYSMAN_POWER_OFF_RESTART; vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, vconf); @@ -355,18 +355,18 @@ static GVariant *dbus_poweroff_handler(GDBusConnection *conn, get_command(ret, comm, sizeof(comm)); if (strncmp(type, "poweroff", sizeof("poweroff")) == 0) - next = DEVICE_POWER_STATE_POWEROFF; + next = DEVICED_POWER_STATE_POWEROFF; else if (strncmp(type, "reboot", sizeof("reboot")) == 0) - next = DEVICE_POWER_STATE_REBOOT; + next = DEVICED_POWER_STATE_REBOOT; else if (strncmp(type, "exit", sizeof("exit")) == 0) - next = DEVICE_POWER_STATE_EXIT; + next = DEVICED_POWER_STATE_EXIT; else { ret = -EINVAL; goto out; } CRITICAL_LOG("Poweroff pid=%d(%s) requests %s.", ret, comm, type); - power_request_change_state_strict(DEVICE_POWER_STATE_ALL, next, 9000, NULL); + power_request_change_state_strict(DEVICED_POWER_STATE_ALL, next, 9000, NULL); out: g_free(type); @@ -391,18 +391,18 @@ static GVariant *dbus_poweroff_option_handler(GDBusConnection *conn, get_command(ret, comm, sizeof(comm)); if (strncmp(type, "poweroff", sizeof("poweroff")) == 0) - next = DEVICE_POWER_STATE_POWEROFF; + next = DEVICED_POWER_STATE_POWEROFF; else if (strncmp(type, "reboot", sizeof("reboot")) == 0) - next = DEVICE_POWER_STATE_REBOOT; + next = DEVICED_POWER_STATE_REBOOT; else if (strncmp(type, "exit", sizeof("exit")) == 0) - next = DEVICE_POWER_STATE_EXIT; + next = DEVICED_POWER_STATE_EXIT; else { ret = -EINVAL; goto out; } CRITICAL_LOG("Poweroff pid=%d(%s) requests type=%s option=%s.", ret, comm, type, option); - power_request_change_state_strict(DEVICE_POWER_STATE_ALL, next, 9000, option); + power_request_change_state_strict(DEVICED_POWER_STATE_ALL, next, 9000, option); out: g_free(type); diff --git a/src/power/power.c b/src/power/power.c index 47587b0..9bef87b 100644 --- a/src/power/power.c +++ b/src/power/power.c @@ -37,12 +37,11 @@ #include "power-event-lock.h" #define POWER_CONF_FILE "/etc/deviced/power.conf" -#define POWER_STATE_INDEX(state) ((state) ? 63 - __builtin_clzll(state) : 0) #define DEFAULT_MAX_WAIT_SECOND 5 /* second */ #define MAX_DESC_LEN 256 static int delayed_init_done = 0; -static uint64_t current = DEVICE_POWER_STATE_START; /* current power state */ +static uint64_t current = DEVICED_POWER_STATE_START; /* current power state */ static int max_wait_timeout = DEFAULT_MAX_WAIT_SECOND; static GQueue *transition_queue; static struct { @@ -68,26 +67,26 @@ struct change_state_wait { }; static const uint64_t transient_scenario_suspending[] = { - DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_EARLY, - DEVICE_POWER_STATE_TRANSIENT_SUSPENDING, - DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_LATE, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE, }; static const uint64_t transient_scenario_resuming[] = { - DEVICE_POWER_STATE_TRANSIENT_RESUMING_EARLY, - DEVICE_POWER_STATE_TRANSIENT_RESUMING, - DEVICE_POWER_STATE_TRANSIENT_RESUMING_LATE, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY, + DEVICED_POWER_TRANSIENT_STATE_RESUMING, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE, }; static const struct { int max_step; const uint64_t *scenario; -} transient[DEVICE_POWER_STATE_MAX_INDEX][DEVICE_POWER_STATE_MAX_INDEX] = { - [DEVICE_POWER_STATE_NORMAL_INDEX][DEVICE_POWER_STATE_SLEEP_INDEX] = { +} transient[DEVICED_POWER_STATE_MAX_INDEX][DEVICED_POWER_STATE_MAX_INDEX] = { + [DEVICED_POWER_STATE_NORMAL_INDEX][DEVICED_POWER_STATE_SLEEP_INDEX] = { .max_step = ARRAY_SIZE(transient_scenario_suspending), .scenario = transient_scenario_suspending, }, - [DEVICE_POWER_STATE_SLEEP_INDEX][DEVICE_POWER_STATE_NORMAL_INDEX] = { + [DEVICED_POWER_STATE_SLEEP_INDEX][DEVICED_POWER_STATE_NORMAL_INDEX] = { .max_step = ARRAY_SIZE(transient_scenario_resuming), .scenario = transient_scenario_resuming, }, @@ -101,12 +100,12 @@ static uint64_t get_next_state(void); static void power_action_normal(void *data); static void power_action_sleep(void *data); static void power_action_poweroff(void *data); -static void (*const action_on_state[DEVICE_POWER_STATE_MAX_INDEX]) (void *) = { - [DEVICE_POWER_STATE_NORMAL_INDEX] = power_action_normal, - [DEVICE_POWER_STATE_SLEEP_INDEX] = power_action_sleep, - [DEVICE_POWER_STATE_POWEROFF_INDEX] = power_action_poweroff, - [DEVICE_POWER_STATE_REBOOT_INDEX] = power_action_poweroff, - [DEVICE_POWER_STATE_EXIT_INDEX] = power_action_poweroff, +static void (*const action_on_state[DEVICED_POWER_STATE_MAX_INDEX]) (void *) = { + [DEVICED_POWER_STATE_NORMAL_INDEX] = power_action_normal, + [DEVICED_POWER_STATE_SLEEP_INDEX] = power_action_sleep, + [DEVICED_POWER_STATE_POWEROFF_INDEX] = power_action_poweroff, + [DEVICED_POWER_STATE_REBOOT_INDEX] = power_action_poweroff, + [DEVICED_POWER_STATE_EXIT_INDEX] = power_action_poweroff, }; uint64_t power_get_state(void) @@ -261,8 +260,8 @@ static void power_action_normal(void *udata) static void power_action_sleep(void *udata) { - /* for DEVICE_POWER_STATE_NORMAL, DEVICE_POWER_STATE_POWEROFF, do not wake unlock */ - if (current != DEVICE_POWER_STATE_SLEEP) { + /* for DEVICED_POWER_STATE_NORMAL, DEVICED_POWER_STATE_POWEROFF, do not wake unlock */ + if (current != DEVICED_POWER_STATE_SLEEP) { _E("Ignore sleep wait done, current=%s", state_name(current)); return; } @@ -281,41 +280,47 @@ static void power_action_poweroff(void *data) static void broadcast_transition_info(void) { - const char *signame; - - uint64_t next = get_next_state(); - - if (next == DEVICE_POWER_STATE_START) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_START; - else if (next == DEVICE_POWER_STATE_NORMAL) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_NORMAL; - else if (next == DEVICE_POWER_STATE_SLEEP) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SLEEP; - else if (next == DEVICE_POWER_STATE_POWEROFF) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_POWEROFF; - else if (next == DEVICE_POWER_STATE_REBOOT) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_REBOOT; - else if (next == DEVICE_POWER_STATE_EXIT) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_EXIT; - else if (next == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_EARLY) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_EARLY; - else if (next == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING; - else if (next == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_LATE) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_LATE; - else if (next == DEVICE_POWER_STATE_TRANSIENT_RESUMING_EARLY) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING_EARLY; - else if (next == DEVICE_POWER_STATE_TRANSIENT_RESUMING) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING; - else if (next == DEVICE_POWER_STATE_TRANSIENT_RESUMING_LATE) - signame = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING_LATE; - else - return; + uint64_t curr = DEVICED_POWER_STATE_INDEX(transition_context.ti.curr); + uint64_t next = DEVICED_POWER_STATE_INDEX(get_next_state()); + + assert(DEVICED_POWER_STATE_MIN_INDEX <= curr && curr < DEVICED_POWER_TRANSIENT_STATE_MAX_INDEX); + assert(DEVICED_POWER_STATE_MIN_INDEX <= next && next < DEVICED_POWER_TRANSIENT_STATE_MAX_INDEX); + + static const char* mapping_signame[DEVICED_POWER_TRANSIENT_STATE_MAX_INDEX] = { + [DEVICED_POWER_STATE_START_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_START, + [DEVICED_POWER_STATE_NORMAL_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_NORMAL, + [DEVICED_POWER_STATE_SLEEP_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SLEEP, + [DEVICED_POWER_STATE_POWEROFF_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_POWEROFF, + [DEVICED_POWER_STATE_REBOOT_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_REBOOT, + [DEVICED_POWER_STATE_EXIT_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_EXIT, + [DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING_EARLY, + [DEVICED_POWER_TRANSIENT_STATE_RESUMING_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING, + [DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_RESUMING_LATE, + [DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_EARLY, + [DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING, + [DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE_INDEX] = DEVICED_SIGNAL_POWER_CHANGE_STATE_TO_SUSPENDING_LATE, + }; + + // convert deviced state to a correspond device enum + static const uint64_t mapping_device_state[DEVICED_POWER_TRANSIENT_STATE_MAX_INDEX] = { + [DEVICED_POWER_STATE_START_INDEX] = DEVICE_POWER_STATE_START, + [DEVICED_POWER_STATE_NORMAL_INDEX] = DEVICE_POWER_STATE_NORMAL, + [DEVICED_POWER_STATE_SLEEP_INDEX] = DEVICE_POWER_STATE_SLEEP, + [DEVICED_POWER_STATE_POWEROFF_INDEX] = DEVICE_POWER_STATE_POWEROFF, + [DEVICED_POWER_STATE_REBOOT_INDEX] = DEVICE_POWER_STATE_REBOOT, + [DEVICED_POWER_STATE_EXIT_INDEX] = DEVICE_POWER_STATE_EXIT, + [DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY_INDEX] = DEVICE_POWER_TRANSIENT_STATE_RESUMING_EARLY, + [DEVICED_POWER_TRANSIENT_STATE_RESUMING_INDEX] = DEVICE_POWER_TRANSIENT_STATE_RESUMING, + [DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE_INDEX] = DEVICE_POWER_TRANSIENT_STATE_RESUMING_LATE, + [DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY_INDEX] = DEVICE_POWER_TRANSIENT_STATE_SUSPENDING_EARLY, + [DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_INDEX] = DEVICE_POWER_TRANSIENT_STATE_SUSPENDING, + [DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE_INDEX] = DEVICE_POWER_TRANSIENT_STATE_SUSPENDING_LATE, + }; - gdbus_signal_emit(NULL, DEVICED_PATH_POWER, DEVICED_INTERFACE_POWER, signame, + gdbus_signal_emit(NULL, DEVICED_PATH_POWER, DEVICED_INTERFACE_POWER, mapping_signame[next], g_variant_new("(ttti)", - transition_context.ti.curr, - next, + mapping_device_state[curr], + mapping_device_state[next], transition_context.id, transition_context.ti.reason)); } @@ -378,8 +383,8 @@ static void enqueue_transition(const struct trans_info *ti) // it might be a transient state or a destination state static uint64_t get_next_state(void) { - int curr = POWER_STATE_INDEX(transition_context.ti.curr); - int next = POWER_STATE_INDEX(transition_context.ti.next); + int curr = DEVICED_POWER_STATE_INDEX(transition_context.ti.curr); + int next = DEVICED_POWER_STATE_INDEX(transition_context.ti.next); int step = transition_context.transient_step; if (step >= transient[curr][next].max_step) @@ -416,29 +421,29 @@ static void init_waiting_list(gpointer data, gpointer udata) static char *state_abbr_name(uint64_t state) { - if (state == DEVICE_POWER_STATE_START) + if (state == DEVICED_POWER_STATE_START) return "START"; - if (state == DEVICE_POWER_STATE_NORMAL) + if (state == DEVICED_POWER_STATE_NORMAL) return "NORMAL"; - if (state == DEVICE_POWER_STATE_SLEEP) + if (state == DEVICED_POWER_STATE_SLEEP) return "SLEEP"; - if (state == DEVICE_POWER_STATE_POWEROFF) + if (state == DEVICED_POWER_STATE_POWEROFF) return "POWEROFF"; - if (state == DEVICE_POWER_STATE_REBOOT) + if (state == DEVICED_POWER_STATE_REBOOT) return "REBOOT"; - if (state == DEVICE_POWER_STATE_EXIT) + if (state == DEVICED_POWER_STATE_EXIT) return "EXIT"; - if (state == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_EARLY) + if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY) return "SUSPENDING_EARLY"; - if (state == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING) + if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING) return "SUSPENDING"; - if (state == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_LATE) + if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE) return "SUSPENDING_LATE"; - if (state == DEVICE_POWER_STATE_TRANSIENT_RESUMING_EARLY) + if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY) return "RESUMING_EARLY"; - if (state == DEVICE_POWER_STATE_TRANSIENT_RESUMING) + if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING) return "RESUMING"; - if (state == DEVICE_POWER_STATE_TRANSIENT_RESUMING_LATE) + if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE) return "RESUMING_LATE"; return "INVALID"; @@ -451,8 +456,8 @@ static void reload_transition_sequence(GList **sequence) g_list_free(g_steal_pointer(sequence)); - curr = POWER_STATE_INDEX(transition_context.ti.curr); - next = POWER_STATE_INDEX(transition_context.ti.next); + curr = DEVICED_POWER_STATE_INDEX(transition_context.ti.curr); + next = DEVICED_POWER_STATE_INDEX(transition_context.ti.next); new_sequence = g_list_append(new_sequence, state_abbr_name(transition_context.ti.curr)); for (step = 0; step < transient[curr][next].max_step; ++step) @@ -524,8 +529,10 @@ static void trigger_transition(void) { int waiting = 0; + assert(current != DEVICED_POWER_STATE_POWEROFF); + assert(current != DEVICED_POWER_STATE_REBOOT); + assert(current != DEVICED_POWER_STATE_EXIT); assert(transition_context.max_wait_timer == 0); - assert(is_poweroff_state(current) == 0); transition_context.id = alloc_unique_id(); @@ -555,11 +562,13 @@ static void action_transition(void) // it has reached the destination state struct trans_info ti = { 0 , }; int retval; + int index; current = transition_context.ti.next; + index = DEVICED_POWER_STATE_INDEX(current); - if (action_on_state[POWER_STATE_INDEX(current)]) - action_on_state[POWER_STATE_INDEX(current)](transition_context.ti.data); + if (action_on_state[index]) + action_on_state[index](transition_context.ti.data); // transition end transition_context.ongoing = 0; @@ -633,7 +642,7 @@ static int transition_request_callback(void *data) t = l->data; // check invalid next state - if (__builtin_popcountll(t->next & DEVICE_POWER_STATE_ALL) != 1) { + if (__builtin_popcountll(t->next & DEVICED_POWER_STATE_ALL) != 1) { _E("Invalid next state, curr=%"PRIx64", next=%"PRIx64", reason=%d", t->curr, t->next, t->reason); return 0; } @@ -709,8 +718,8 @@ void power_state_init(void *data) /* Take the first transition. * - * It is determined by bootreason and bootmode to which state to transition, DEVICE_POWER_STATE_NORMAL - * or DEVICE_POWER_STATE_SLEEP. it may stay in DEVICE_POWER_STATE_START if there is no defined action for the + * It is determined by bootreason and bootmode to which state to transition, DEVICED_POWER_STATE_NORMAL + * or DEVICED_POWER_STATE_SLEEP. it may stay in DEVICED_POWER_STATE_START if there is no defined action for the * matching bootreason and bootmode. */ initial_transition_by_boot_condition(); diff --git a/src/power/power.h b/src/power/power.h index f559826..a2aefac 100644 --- a/src/power/power.h +++ b/src/power/power.h @@ -20,14 +20,52 @@ #define __POWER_STATE_MANAGER_H__ #include +#include #include + #include -#include #include "shared/log-macro.h" #include "shared/device-notifier.h" -#define POWER_STATE_NONE 0 +enum { + DEVICED_POWER_STATE_MIN_INDEX, + DEVICED_POWER_STATE_START_INDEX = DEVICED_POWER_STATE_MIN_INDEX, + DEVICED_POWER_STATE_NORMAL_INDEX, + DEVICED_POWER_STATE_SLEEP_INDEX, + DEVICED_POWER_STATE_POWEROFF_INDEX, + DEVICED_POWER_STATE_REBOOT_INDEX, + DEVICED_POWER_STATE_EXIT_INDEX, + DEVICED_POWER_STATE_MAX_INDEX, + + DEVICED_POWER_TRANSIENT_STATE_MIN_INDEX, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY_INDEX = DEVICED_POWER_TRANSIENT_STATE_MIN_INDEX, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_INDEX, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE_INDEX, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY_INDEX, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_INDEX, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE_INDEX, + DEVICED_POWER_TRANSIENT_STATE_MAX_INDEX, +}; + +#define DEVICED_POWER_STATE_UNDEFINED (0) + +#define DEVICED_POWER_STATE_START (1ULL << DEVICED_POWER_STATE_START_INDEX) +#define DEVICED_POWER_STATE_NORMAL (1ULL << DEVICED_POWER_STATE_NORMAL_INDEX) +#define DEVICED_POWER_STATE_SLEEP (1ULL << DEVICED_POWER_STATE_SLEEP_INDEX) +#define DEVICED_POWER_STATE_POWEROFF (1ULL << DEVICED_POWER_STATE_POWEROFF_INDEX) +#define DEVICED_POWER_STATE_REBOOT (1ULL << DEVICED_POWER_STATE_REBOOT_INDEX) +#define DEVICED_POWER_STATE_EXIT (1ULL << DEVICED_POWER_STATE_EXIT_INDEX) +#define DEVICED_POWER_STATE_ALL ((1ULL << DEVICED_POWER_STATE_MAX_INDEX) - (1ULL << DEVICED_POWER_STATE_MIN_INDEX)) + +#define DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY (1ULL << DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_SUSPENDING (1ULL << DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE (1ULL << DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY (1ULL << DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_RESUMING (1ULL << DEVICED_POWER_TRANSIENT_STATE_RESUMING_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE (1ULL << DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE_INDEX) + +#define DEVICED_POWER_STATE_INDEX(state) (__builtin_ctzll(state)) struct trans_info { uint64_t curr; @@ -41,52 +79,52 @@ void power_state_manager_init(void *data); static inline uint64_t convert_action_string_to_power_state(const char *str) { if (MATCH(str, "start")) - return DEVICE_POWER_STATE_START; + return DEVICED_POWER_STATE_START; else if (MATCH(str, "sleep")) - return DEVICE_POWER_STATE_SLEEP; + return DEVICED_POWER_STATE_SLEEP; else if (MATCH(str, "normal")) - return DEVICE_POWER_STATE_NORMAL; + return DEVICED_POWER_STATE_NORMAL; else if (MATCH(str, "poweroff")) - return DEVICE_POWER_STATE_POWEROFF; + return DEVICED_POWER_STATE_POWEROFF; else if (MATCH(str, "reboot")) - return DEVICE_POWER_STATE_REBOOT; + return DEVICED_POWER_STATE_REBOOT; else if (MATCH(str, "exit")) - return DEVICE_POWER_STATE_EXIT; + return DEVICED_POWER_STATE_EXIT; else if (MATCH(str, "current")) - return DEVICE_POWER_STATE_ALL; + return DEVICED_POWER_STATE_ALL; _W("Invalid power state=%s", str); - return POWER_STATE_NONE; + return DEVICED_POWER_STATE_UNDEFINED; } static inline const char *state_name(uint64_t state) { - if (state == DEVICE_POWER_STATE_START) + if (state == DEVICED_POWER_STATE_START) return "POWER_STATE_START"; - if (state == DEVICE_POWER_STATE_NORMAL) + if (state == DEVICED_POWER_STATE_NORMAL) return "POWER_STATE_NORMAL"; - if (state == DEVICE_POWER_STATE_SLEEP) + if (state == DEVICED_POWER_STATE_SLEEP) return "POWER_STATE_SLEEP"; - if (state == DEVICE_POWER_STATE_POWEROFF) + if (state == DEVICED_POWER_STATE_POWEROFF) return "POWER_STATE_POWEROFF"; - if (state == DEVICE_POWER_STATE_REBOOT) + if (state == DEVICED_POWER_STATE_REBOOT) return "POWER_STATE_REBOOT"; - if (state == DEVICE_POWER_STATE_EXIT) + if (state == DEVICED_POWER_STATE_EXIT) return "POWER_STATE_EXIT"; - if (state == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_EARLY) - return "POWER_STATE_TRANSIENT_SUSPENDING_EARLY"; - if (state == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING) - return "POWER_STATE_TRANSIENT_SUSPENDING"; - if (state == DEVICE_POWER_STATE_TRANSIENT_SUSPENDING_LATE) - return "POWER_STATE_TRANSIENT_SUSPENDING_LATE"; - if (state == DEVICE_POWER_STATE_TRANSIENT_RESUMING_EARLY) - return "POWER_STATE_TRANSIENT_RESUMING_EARLY"; - if (state == DEVICE_POWER_STATE_TRANSIENT_RESUMING) - return "POWER_STATE_TRANSIENT_RESUMING"; - if (state == DEVICE_POWER_STATE_TRANSIENT_RESUMING_LATE) - return "POWER_STATE_TRANSIENT_RESUMING_LATE"; - if (state == DEVICE_POWER_STATE_ALL) + if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY) + return "POWER_TRANSIENT_STATE_SUSPENDING_EARLY"; + if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING) + return "POWER_TRANSIENT_STATE_SUSPENDING"; + if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE) + return "POWER_TRANSIENT_STATE_SUSPENDING_LATE"; + if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY) + return "POWER_TRANSIENT_STATE_RESUMING_EARLY"; + if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING) + return "POWER_TRANSIENT_STATE_RESUMING"; + if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE) + return "POWER_TRANSIENT_STATE_RESUMING_LATE"; + if (state == DEVICED_POWER_STATE_ALL) return "POWER_STATE_ALL"; return "POWER_STATE_INVALID"; @@ -94,7 +132,7 @@ static inline const char *state_name(uint64_t state) static inline int is_poweroff_state(uint64_t state) { - return !!(state & (DEVICE_POWER_STATE_POWEROFF | DEVICE_POWER_STATE_REBOOT | DEVICE_POWER_STATE_EXIT)); + return !!(state & (DEVICED_POWER_STATE_POWEROFF | DEVICED_POWER_STATE_REBOOT | DEVICED_POWER_STATE_EXIT)); } static inline void power_request_change_state_strict(uint64_t curr, uint64_t next, int reason, void *udata) @@ -107,7 +145,7 @@ static inline void power_request_change_state_strict(uint64_t curr, uint64_t nex static inline void power_request_change_state(uint64_t next, int reason) { - power_request_change_state_strict(DEVICE_POWER_STATE_ALL, next, reason, NULL); + power_request_change_state_strict(DEVICED_POWER_STATE_ALL, next, reason, NULL); } uint64_t power_get_state(void); -- 2.7.4