From: Youngjae Cho Date: Fri, 1 Apr 2022 08:25:40 +0000 (+0900) Subject: power: refactor power operations X-Git-Tag: accepted/tizen/unified/20220526.144053~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b7c7cb5e26b13fd8f0b885daf08e09428aa998f1;p=platform%2Fcore%2Fsystem%2Fdeviced.git power: refactor power operations Fix wake lock/unlock operation to have prefix of "power". And it replaces indirect invocation through device_notify(), which is often in iot-headless. Add functions for add/remove poweroff wait for internal usage. It is similar to the existing add/remove dbus methods, but only differs in that the function can be invoked by the deviced itself. Change-Id: Ide3ceb5f894b376901637992d66401a110eafbf0 Signed-off-by: Youngjae Cho --- diff --git a/plugins/iot-headed/display/core.c b/plugins/iot-headed/display/core.c index 71376ad..12cad43 100644 --- a/plugins/iot-headed/display/core.c +++ b/plugins/iot-headed/display/core.c @@ -722,7 +722,7 @@ void lcd_on_direct(enum device_flags flags) if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); set_pm_cur_state(S_NORMAL); } @@ -1446,7 +1446,7 @@ static int default_action(int timeout) if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1532,9 +1532,9 @@ go_suspend: #endif broadcast_pm_suspend(); if (pm_get_power_lock_support()) { - pm_enable_autosleep(); + power_enable_autosleep(); - if (pm_power_unlock() < 0) + if (power_release_wakelock() < 0) _E("Power unlock state error."); } else { pm_suspend(); @@ -2178,7 +2178,7 @@ static void display_init(void *data) _I("Start Power managing without noti"); if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/iot-headless/power/power-dbus.c b/plugins/iot-headless/power/power-dbus.c index 8143f3c..0cbc34f 100644 --- a/plugins/iot-headless/power/power-dbus.c +++ b/plugins/iot-headless/power/power-dbus.c @@ -30,6 +30,7 @@ #include "shared/log.h" #include "shared/device-notifier.h" +#include "power/power-control.h" #include "power-state-manager.h" #include "power-state-wait.h" @@ -58,23 +59,6 @@ static void print_lock_node(void) node->pid, node->comm, node->locktime, node->timeout); } -/* check the lock_list is empty. - * if it is, then request wake unlock */ -static void try_wake_unlock(void) -{ - if (SYS_G_LIST_LENGTH(lock_list) == 0) - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_UNLOCK, NULL); - else - print_lock_node(); -} - -static void try_wake_lock(void) -{ - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, NULL); -} - -/* remove given node from lock list. - * after removing the node, request wake unlock if possible */ static void remove_lock_node(struct lock_node *node) { if (!node) @@ -88,7 +72,12 @@ static void remove_lock_node(struct lock_node *node) SYS_G_LIST_REMOVE(lock_list, node); free(node); - try_wake_unlock(); + /* Check the lock_list is empty after removing the node. + * If it is, then request wake unlock */ + if (SYS_G_LIST_LENGTH(lock_list) == 0) + power_release_wakelock(); + else + print_lock_node(); } static gboolean lock_expired_cb(void *data) @@ -190,7 +179,7 @@ static GVariant *dbus_power_lock_cpu(GDBusConnection *conn, } _D("pid=%d(%s) request CPU lock for %dms", pid, node->comm, timeout); - try_wake_lock(); + power_acquire_wakelock(); if (timeout > 0) node->timer_id = g_timeout_add(timeout, lock_expired_cb, node); diff --git a/plugins/iot-headless/power/power-state-manager.c b/plugins/iot-headless/power/power-state-manager.c index a2e90ad..3bf6d42 100644 --- a/plugins/iot-headless/power/power-state-manager.c +++ b/plugins/iot-headless/power/power-state-manager.c @@ -26,6 +26,7 @@ #include "shared/devices.h" #include "shared/device-notifier.h" #include "shared/log.h" +#include "power/power-control.h" #include "power-state-manager.h" #include "power-dbus.h" #include "power-state-wait.h" @@ -58,7 +59,7 @@ static void psm_wake_unlock(void) return; } - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_UNLOCK, NULL); + power_release_wakelock(); } static void psm_trigger_poweroff(void) @@ -70,7 +71,7 @@ static void psm_trigger_poweroff(void) return; if (power_device->execute) { - device_notify(DEVICE_NOTIFIER_REQUEST_DISABLE_AUTOSLEEP, NULL); + power_disable_autosleep(); power_device->execute("poweroff"); } } @@ -103,7 +104,7 @@ static void psm_transition_start_to_normal(const struct trans_info *ti) { current = PSM_NORMAL; - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, NULL); + power_acquire_wakelock(); broadcast_transition_info(ti); } @@ -113,7 +114,7 @@ static void psm_transition_start_to_sleep(const struct trans_info *ti) current = PSM_SLEEP; - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, NULL); + power_acquire_wakelock(); broadcast_transition_info(ti); waiting = update_change_state_wait(state_transition_counter, ti, psm_wake_unlock); if (waiting > 0) { @@ -150,7 +151,7 @@ static void psm_transition_sleep_to_normal(const struct trans_info *ti) { current = PSM_NORMAL; - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, NULL); + power_acquire_wakelock(); broadcast_transition_info(ti); update_change_state_wait(state_transition_counter, ti, NULL); } @@ -159,7 +160,7 @@ static void psm_transition_sleep_to_sleep(const struct trans_info *ti) { int waiting; - device_notify(DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, NULL); + power_acquire_wakelock(); broadcast_transition_info(ti); waiting = update_change_state_wait(state_transition_counter, ti, psm_wake_unlock); if (waiting > 0) { @@ -277,7 +278,7 @@ static int delayed_init_cb(void *data) /* Deferred autosleep enable, * This prevents system go suspend during booting */ - device_notify(DEVICE_NOTIFIER_REQUEST_ENABLE_AUTOSLEEP, NULL); + power_enable_autosleep(); return 0; } diff --git a/plugins/mobile/display/core.c b/plugins/mobile/display/core.c index 7180885..0f12f68 100644 --- a/plugins/mobile/display/core.c +++ b/plugins/mobile/display/core.c @@ -729,7 +729,7 @@ void lcd_on_direct(enum device_flags flags) if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); set_pm_cur_state(S_NORMAL); } @@ -1456,7 +1456,7 @@ static int default_action(int timeout) if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1542,9 +1542,9 @@ go_suspend: #endif broadcast_pm_suspend(); if (pm_get_power_lock_support()) { - pm_enable_autosleep(); + power_enable_autosleep(); - if (pm_power_unlock() < 0) + if (power_release_wakelock() < 0) _E("Power unlock state error."); } else { pm_suspend(); @@ -2184,7 +2184,7 @@ static void display_init(void *data) _I("Start Power managing without noti"); if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/tv/display/core.c b/plugins/tv/display/core.c index 33b3775..8dcdbea 100644 --- a/plugins/tv/display/core.c +++ b/plugins/tv/display/core.c @@ -722,7 +722,7 @@ void lcd_on_direct(enum device_flags flags) if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); set_pm_cur_state(S_NORMAL); } @@ -1446,7 +1446,7 @@ static int default_action(int timeout) if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1532,9 +1532,9 @@ go_suspend: #endif broadcast_pm_suspend(); if (pm_get_power_lock_support()) { - pm_enable_autosleep(); + power_enable_autosleep(); - if (pm_power_unlock() < 0) + if (power_release_wakelock() < 0) _E("Power unlock state error."); } else { pm_suspend(); @@ -2175,7 +2175,7 @@ static void display_init(void *data) _I("Start Power managing without noti"); if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/wearable/display/core.c b/plugins/wearable/display/core.c index bd6349c..8dd7d48 100644 --- a/plugins/wearable/display/core.c +++ b/plugins/wearable/display/core.c @@ -939,7 +939,7 @@ void lcd_on_direct(enum device_flags flags) if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); set_pm_cur_state(S_NORMAL); } @@ -1719,7 +1719,7 @@ static int default_action(int timeout) if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1808,9 +1808,9 @@ go_suspend: #endif broadcast_pm_suspend(); if (pm_get_power_lock_support()) { - pm_enable_autosleep(); + power_enable_autosleep(); - if (pm_power_unlock() < 0) + if (power_release_wakelock() < 0) _E("Power unlock state error."); } else { pm_suspend(); @@ -2505,7 +2505,7 @@ static void display_init(void *data) _I("Start Power managing without noti"); if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - pm_power_lock(); + power_acquire_wakelock(); } /* * Lock lcd off until booting is done. diff --git a/src/display/core.h b/src/display/core.h index 816e00b..c8062ac 100644 --- a/src/display/core.h +++ b/src/display/core.h @@ -53,27 +53,8 @@ #define CHECK_OPS(d, op) (d != NULL && d->op != NULL) #ifdef ENABLE_PM_LOG +#include "power/power-control.h" #define MAX_LOG_COUNT 250 - -enum pm_log_type { - PM_LOG_MIN = 0, - PM_LOG_KEY_PRESS = PM_LOG_MIN, /* key log */ - PM_LOG_KEY_LONG_PRESS, - PM_LOG_KEY_RELEASE, - PM_LOG_LCD_ON, /* lcd log */ - PM_LOG_LCD_ON_COMPLETE, - PM_LOG_LCD_ON_FAIL, - PM_LOG_LCD_DIM, - PM_LOG_LCD_DIM_FAIL, - PM_LOG_LCD_OFF, - PM_LOG_LCD_OFF_COMPLETE, - PM_LOG_LCD_OFF_FAIL, - PM_LOG_LCD_CONTROL_FAIL, - PM_LOG_SLEEP, - PM_LOG_MAX -}; - -void pm_history_save(enum pm_log_type, int); #endif /* diff --git a/src/power/power-control.c b/src/power/power-control.c index 91d07b9..e4dbb1e 100644 --- a/src/power/power-control.c +++ b/src/power/power-control.c @@ -212,20 +212,23 @@ int pm_suspend(void) return 0; } -int pm_enable_autosleep(void) +int power_enable_autosleep(void) { _I("System autosleep enabled."); return sys_set_str(POWER_AUTOSLEEP_PATH, "mem"); } -int pm_disable_autosleep(void) +int power_disable_autosleep(void) { _I("System autosleep disabled."); return sys_set_str(POWER_AUTOSLEEP_PATH, "off"); } -int pm_power_lock(void) +int power_acquire_wakelock(void) { + if (mainlock_status == POWER_LOCK) + return 0; + _I("system power lock"); suspend_other_process(VITAL_WAKEUP); mainlock_status = POWER_LOCK; @@ -259,8 +262,11 @@ out: return power_lock_support; } -int pm_power_unlock(void) +int power_release_wakelock(void) { + if (mainlock_status == POWER_UNLOCK) + return 0; + _I("system power unlock"); suspend_other_process(VITAL_SLEEP); mainlock_status = POWER_UNLOCK; @@ -303,33 +309,6 @@ int set_wakeup_count(int cnt) return 0; } -static int __pm_enable_autosleep(void *data) -{ - return pm_enable_autosleep(); -} - -static int __pm_disable_autosleep(void *data) -{ - - return pm_disable_autosleep(); -} - -static int __pm_power_lock(void *data) -{ - if (mainlock_status == POWER_UNLOCK) - pm_power_lock(); - - return 0; -} - -static int __pm_power_unlock(void *data) -{ - if (mainlock_status == POWER_LOCK) - pm_power_unlock(); - - return 0; -} - static int load_sleep_config(struct parse_result *result, void *user_data) { if (!MATCH(result->section, "PowerState")) @@ -357,13 +336,6 @@ static void power_control_init(void *data) if (check_default(plugin_device_ops)) return; - /* if there is plugin of power-control, - * register notifier for the plugin module */ - register_notifier(DEVICE_NOTIFIER_REQUEST_ENABLE_AUTOSLEEP, __pm_enable_autosleep); - register_notifier(DEVICE_NOTIFIER_REQUEST_DISABLE_AUTOSLEEP, __pm_disable_autosleep); - register_notifier(DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, __pm_power_lock); - register_notifier(DEVICE_NOTIFIER_REQUEST_WAKE_UNLOCK, __pm_power_unlock); - _D("[%s] Initialization.", plugin_device_ops->name); if (plugin_device_ops->init) plugin_device_ops->init(NULL); diff --git a/src/power/power-control.h b/src/power/power-control.h index e4d1a18..ead8e98 100644 --- a/src/power/power-control.h +++ b/src/power/power-control.h @@ -21,7 +21,6 @@ #include "display/core.h" - #define POWER_AUTOSLEEP_PATH "/sys/power/autosleep" #define POWER_LOCK_PATH "/sys/power/wake_lock" #define POWER_UNLOCK_PATH "/sys/power/wake_unlock" @@ -47,6 +46,25 @@ enum vital_state { VITAL_EXIT, }; +enum pm_log_type { + PM_LOG_MIN = 0, + PM_LOG_KEY_PRESS = PM_LOG_MIN, /* key log */ + PM_LOG_KEY_LONG_PRESS, + PM_LOG_KEY_RELEASE, + PM_LOG_LCD_ON, /* lcd log */ + PM_LOG_LCD_ON_COMPLETE, + PM_LOG_LCD_ON_FAIL, + PM_LOG_LCD_DIM, + PM_LOG_LCD_DIM_FAIL, + PM_LOG_LCD_OFF, + PM_LOG_LCD_OFF_COMPLETE, + PM_LOG_LCD_OFF_FAIL, + PM_LOG_LCD_CONTROL_FAIL, + PM_LOG_SLEEP, + PM_LOG_MAX +}; + + extern bool timeout_sleep_support; #ifdef ENABLE_PM_LOG @@ -57,9 +75,10 @@ void pm_history_print(int fd, int count); bool vital_mode(void); int vital_state_changed(void *data); int pm_suspend(void); -int pm_enable_autosleep(void); -int pm_power_lock(void); -int pm_power_unlock(void); +int power_enable_autosleep(void); +int power_disable_autosleep(void); +int power_acquire_wakelock(void); +int power_release_wakelock(void); int pm_get_power_lock(void); int pm_get_power_lock_support(void); int check_wakeup_src(void); diff --git a/src/power/power-handler.c b/src/power/power-handler.c index 9e09401..7464068 100644 --- a/src/power/power-handler.c +++ b/src/power/power-handler.c @@ -331,6 +331,36 @@ static void poweroff_remove_handle(pid_t pid) free(handle); } +static int poweroff_add_handle(pid_t pid) +{ + struct poweroff_handle *handle; + GList *l; + + SYS_G_LIST_FOREACH(poweroff_handles, l, handle) { + if (handle->pid == pid) + break; + } + + if (handle) + poweroff_remove_handle(pid); + + _D("Make a new handle."); + handle = (struct poweroff_handle *)malloc(sizeof(struct poweroff_handle)); + if (handle == NULL) { + _E("Not enough memory."); + return -ENOMEM; + } + + handle->pid = pid; + handle->timeout_id = 0; + handle->timeout = POWEROFF_WAIT_MAX; + + SYS_G_LIST_APPEND(poweroff_handles, handle); + _D("Add a new poweroff timer. pid=%d timeout=%d timeout_id=%d)", handle->pid, handle->timeout, handle->timeout_id); + + return 0; +} + static gboolean poweroff_wait_timeout_cb(void *data) { char timeout[50] = {0,}; @@ -598,10 +628,8 @@ static GVariant *add_poweroff_time(GDBusConnection *conn, const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) { - struct poweroff_handle *handle; int ret; pid_t pid; - GList *l; if (poweroff_stage >= POWEROFF_WAIT_OTHERS) { _E("It's too late. Poweroff is already in waiting stage."); @@ -617,30 +645,7 @@ static GVariant *add_poweroff_time(GDBusConnection *conn, CRITICAL_LOG("PID %d requested to a poweroff timer.", pid); - SYS_G_LIST_FOREACH(poweroff_handles, l, handle) { - if (handle->pid == pid) - break; - } - - if (handle) - poweroff_remove_handle(pid); - - _D("Make a new handle."); - handle = (struct poweroff_handle *)malloc(sizeof(struct poweroff_handle)); - if (handle == NULL) { - _E("Not enough memory."); - ret = -1; - goto out; - } - - handle->pid = pid; - handle->timeout_id = 0; - handle->timeout = POWEROFF_WAIT_MAX; - - SYS_G_LIST_APPEND(poweroff_handles, handle); - _D("Add a new poweroff timer. pid=%d timeout=%d timeout_id=%d)", handle->pid, handle->timeout, handle->timeout_id); - - ret = 0; + ret = poweroff_add_handle(pid); out: return g_variant_new("(i)", ret); @@ -727,6 +732,16 @@ static int add_poweroff_option(enum poweroff_type type, const char *option) return 0; } +int poweroff_add_wait(pid_t pid) +{ + return poweroff_add_handle(pid); +} + +void poweroff_remove_wait(pid_t pid) +{ + g_idle_add(poweroff_wait_timeout_cb, (void *)(intptr_t) pid); +} + static int load_config(struct parse_result *result, void *user_data) { enum poweroff_type type; diff --git a/src/power/power-handler.h b/src/power/power-handler.h index 48c0927..8c7e16b 100644 --- a/src/power/power-handler.h +++ b/src/power/power-handler.h @@ -19,6 +19,8 @@ #ifndef __POWER_HANDLE_H__ #define __POWER_HANDLE_H__ +#include + #define POWER_OPS_NAME "power" #define POWER_POWEROFF "poweroff" @@ -57,7 +59,9 @@ struct poweroff_handle { }; int check_power_flag(void); -void poweroff(void); void poweroff_request_shutdown(void); +int poweroff_add_wait(pid_t pid); +void poweroff_remove_wait(pid_t pid); + #endif /* __POWER_HANDLE_H__ */ diff --git a/src/shared/device-notifier.h b/src/shared/device-notifier.h index 6ccae1a..3a24a2f 100644 --- a/src/shared/device-notifier.h +++ b/src/shared/device-notifier.h @@ -71,10 +71,6 @@ enum device_notifier_type { /* Purpose of calling methods of different modules * Use prefix DEVICE_NOTIFIER_REQUEST */ - DEVICE_NOTIFIER_REQUEST_ENABLE_AUTOSLEEP, - DEVICE_NOTIFIER_REQUEST_DISABLE_AUTOSLEEP, - DEVICE_NOTIFIER_REQUEST_WAKE_LOCK, - DEVICE_NOTIFIER_REQUEST_WAKE_UNLOCK, DEVICE_NOTIFIER_REQUEST_TRANSITION_STATE, DEVICE_NOTIFIER_MAX, };