From 68452174d89c4c581e842e0b2a1e02f1256f9507 Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Fri, 7 Aug 2020 18:18:51 +0900 Subject: [PATCH] Change power related global variables and functions to static Change-Id: I7a1f14ac7d7ebab5a2cefab8b070b246e62dd866 Signed-off-by: lokilee73 --- plugins/iot/display/core.c | 34 ++--- plugins/iot/display/device-interface.c | 204 +------------------------ plugins/iot/display/key-filter.c | 1 + plugins/mobile/display/core.c | 34 ++--- plugins/mobile/display/device-interface.c | 206 +------------------------ plugins/mobile/display/key-filter.c | 1 + plugins/tv/display/core.c | 34 ++--- plugins/tv/display/device-interface.c | 203 +----------------------- plugins/tv/display/key-filter.c | 1 + plugins/tv/display/state-tv.c | 3 +- plugins/wearable/display/core.c | 34 ++--- plugins/wearable/display/device-interface.c | 205 +------------------------ plugins/wearable/display/key-filter.c | 1 + src/display/device-interface.h | 30 ---- src/power/power-control.c | 229 ++++++++++++++++++++++++++++ src/power/power-control.h | 59 +++++++ 16 files changed, 366 insertions(+), 913 deletions(-) mode change 100755 => 100644 plugins/wearable/display/device-interface.c create mode 100755 src/power/power-control.c create mode 100644 src/power/power-control.h diff --git a/plugins/iot/display/core.c b/plugins/iot/display/core.c index 1813c8f..2696eaf 100644 --- a/plugins/iot/display/core.c +++ b/plugins/iot/display/core.c @@ -56,6 +56,7 @@ #include "extcon/extcon.h" #include "battery/power-supply.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "power/boot.h" #include "power/doze.h" #include "dd-display.h" @@ -700,10 +701,10 @@ void set_dim_state(bool on) void lcd_on_direct(enum device_flags flags) { - if (power_ops.get_power_lock_support() + if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); set_pm_cur_state(S_NORMAL); } @@ -992,7 +993,7 @@ static void proc_condition_lock(PMMsg *data) get_pname(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && - (power_ops.get_power_lock() == POWER_UNLOCK)) + (pm_get_power_lock() == POWER_UNLOCK)) proc_change_state(data->cond, INTERNAL_LOCK_PM); if (data->timeout > 0) { @@ -1515,9 +1516,9 @@ static int default_action(int timeout) } if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1577,9 +1578,9 @@ static int default_action(int timeout) if (backlight_ops.get_lcd_power() == DPMS_ON) lcd_off_procedure(LCD_OFF_BY_TIMEOUT); - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* sleep state : set system mode to SUSPEND */ - if (power_ops.get_wakeup_count(&wakeup_count) < 0) + if (get_wakeup_count(&wakeup_count) < 0) _E("Wakeup count read error."); if (wakeup_count < 0) { @@ -1587,7 +1588,7 @@ static int default_action(int timeout) goto go_lcd_off; } - if (power_ops.set_wakeup_count(wakeup_count) < 0) { + if (set_wakeup_count(wakeup_count) < 0) { _E("Wakeup count write error."); goto go_lcd_off; } @@ -1602,18 +1603,17 @@ go_suspend: pm_history_save(PM_LOG_SLEEP, get_pm_cur_state()); #endif broadcast_pm_suspend(); - if (power_ops.get_power_lock_support()) { - if (power_ops.enable_autosleep) - power_ops.enable_autosleep(); + if (pm_get_power_lock_support()) { + pm_enable_autosleep(); - if (power_ops.power_unlock() < 0) + if (pm_power_unlock() < 0) _E("Power unlock state error."); } else { - power_ops.suspend(); + pm_suspend(); _I("system wakeup!!"); system_wakeup_flag = true; /* Resume !! */ - if (power_ops.check_wakeup_src() == EVENT_DEVICE) + if (check_wakeup_src() == EVENT_DEVICE) /* system waked up by devices */ states[get_pm_cur_state()].trans(EVENT_DEVICE); else @@ -1623,7 +1623,7 @@ go_suspend: return 0; go_lcd_off: - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* Resume !! */ states[get_pm_cur_state()].trans(EVENT_DEVICE); } @@ -2357,9 +2357,9 @@ static void display_init(void *data) if (flags & WITHOUT_STARTNOTI) { /* start without noti */ _I("Start Power managing without noti"); - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/iot/display/device-interface.c b/plugins/iot/display/device-interface.c index 03abf73..db92d76 100644 --- a/plugins/iot/display/device-interface.c +++ b/plugins/iot/display/device-interface.c @@ -32,6 +32,7 @@ #include #include "ambient-mode.h" +#include "power/power-control.h" #include "core/log.h" #include "core/devices.h" #include "core/common.h" @@ -44,8 +45,6 @@ #include "display/display-dpms.h" #include "display/display.h" -#define SET_SUSPEND_TIME 0.5 - #define TOUCH_ON 1 #define TOUCH_OFF 0 @@ -55,12 +54,6 @@ #define LCD_PHASED_DELAY 10000 /* microsecond */ #define DUMP_MODE_WAITING_TIME 600000 /* milisecond */ -#define POWER_AUTOSLEEP_PATH "/sys/power/autosleep" -#define POWER_LOCK_PATH "/sys/power/wake_lock" -#define POWER_UNLOCK_PATH "/sys/power/wake_unlock" -#define POWER_WAKEUP_PATH "/sys/power/wakeup_count" -#define POWER_STATE_PATH "/sys/power/state" - #define DISPLAY_HAL_LIB_PATH "/usr/lib/libdisplay-hal.so" #define GESTURE_STR "gesture" @@ -75,16 +68,11 @@ #define FREEZER_VITAL_WAKEUP_CGROUP "/sys/fs/cgroup/freezer/vital_wakeup/freezer.state" struct _backlight_ops backlight_ops; -struct _power_ops power_ops; -static int mainlock_status = POWER_UNLOCK; static bool custom_status; static int custom_brightness; static int force_brightness; static int default_brightness; -static int vital_support = -2; -static int vital_service; -static bool vital_sleep; static int dpms_running_state = DPMS_SETTING_DONE; static struct display_device *display_dev; static guint release_timer; @@ -130,133 +118,6 @@ static int bl_brt(int brightness, int delay) return ret; } -static int system_suspend(void) -{ - int ret; - - _I("system suspend"); - ret = sys_set_str(POWER_STATE_PATH, "mem"); - _I("System resume: %d", ret); - return 0; -} - -static int system_enable_autosleep(void) -{ - _I("System autosleep enabled."); - return sys_set_str(POWER_AUTOSLEEP_PATH, "mem"); -} - -static int vital_mode_support(void) -{ - if (vital_support < 0) { - FILE *fp; - - fp = fopen(FREEZER_VITAL_WAKEUP_CGROUP, "r"); - if (fp == NULL) { - _E("%s open failed", FREEZER_VITAL_WAKEUP_CGROUP); - /* read max 2 times to check if this file exist */ - vital_support++; - return 0; - } - vital_support = 1; - fclose(fp); - } - return vital_support; -} - -static int suspend_other_process(int type) -{ - int ret = 0; - char buf[8]; - const char *command[1]; - - if (vital_service == type) - return ret; - - if (type == VITAL_WAKEUP && vital_service > VITAL_SLEEP) - return ret; - - vital_service = type; - - if (!vital_mode_support()) - return ret; - - if (type == VITAL_SLEEP) { - snprintf(buf, sizeof(buf), "%s", "sleep"); - command[0] = buf; - dbus_handle_method_sync_timeout(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", - command, - SET_SUSPEND_TIME*1000); - vital_sleep = true; - } else if (type == VITAL_WAKEUP) { - snprintf(buf, sizeof(buf), "%s", "wakeup"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - } else if (type == VITAL_EXIT) { - snprintf(buf, sizeof(buf), "%s", "exit"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - vital_sleep = false; - } - return ret; -} - -static int system_power_lock(void) -{ - _I("system power lock"); - suspend_other_process(VITAL_WAKEUP); - mainlock_status = POWER_LOCK; - - return sys_set_str(POWER_LOCK_PATH, "mainlock"); -} - -static int system_power_unlock(void) -{ - _I("system power unlock"); - suspend_other_process(VITAL_SLEEP); - mainlock_status = POWER_UNLOCK; - - return sys_set_str(POWER_UNLOCK_PATH, "mainlock"); -} - -static int system_get_power_lock(void) -{ - return mainlock_status; -} - -static int system_get_power_lock_support(void) -{ - static int power_lock_support = -1; - int ret; - - if (power_lock_support >= 0) - goto out; - - ret = sys_check_node(POWER_LOCK_PATH); - if (ret < 0) - power_lock_support = false; - else - power_lock_support = true; - - _I("System power lock: %s", - (power_lock_support ? "support" : "not support")); - -out: - return power_lock_support; -} - static int get_lcd_power(void) { enum display_state val; @@ -509,41 +370,6 @@ static int get_default_brt(void) return default_brightness; } -static int check_wakeup_src(void) -{ - /* TODO if nedded. - * return wackeup source. user input or device interrupts? (EVENT_DEVICE or EVENT_INPUT) - */ - return EVENT_DEVICE; -} - -static int get_wakeup_count(int *cnt) -{ - int ret; - int wakeup_count; - - if (!cnt) - return -EINVAL; - - ret = sys_get_int(POWER_WAKEUP_PATH, &wakeup_count); - if (ret < 0) - return ret; - - *cnt = wakeup_count; - return 0; -} - -static int set_wakeup_count(int cnt) -{ - int ret; - - ret = sys_set_int(POWER_WAKEUP_PATH, cnt); - if (ret < 0) - return ret; - - return 0; -} - static int get_max_brightness(void) { static int max = -1; @@ -925,16 +751,6 @@ static void _init_ops(void) backlight_ops.transit_brt = change_brightness; backlight_ops.blink = blink; backlight_ops.release_blink = release_blink; - - power_ops.suspend = system_suspend; - power_ops.enable_autosleep = system_enable_autosleep; - power_ops.power_lock = system_power_lock; - power_ops.power_unlock = system_power_unlock; - power_ops.get_power_lock = system_get_power_lock; - power_ops.get_power_lock_support = system_get_power_lock_support; - power_ops.check_wakeup_src = check_wakeup_src; - power_ops.get_wakeup_count = get_wakeup_count; - power_ops.set_wakeup_count = set_wakeup_count; } int display_service_load(void) @@ -984,24 +800,6 @@ int display_service_free(void) return 0; } -bool vital_mode(void) -{ - return vital_sleep; -} - -static int vital_state_changed(void *data) -{ - int type; - - assert(data); - - type = *(int *)data; - if (type == VITAL_EXIT) - suspend_other_process(VITAL_EXIT); - - return 0; -} - /* Dummy. Do not consider detached display state */ int is_lcdon_blocked(void) { diff --git a/plugins/iot/display/key-filter.c b/plugins/iot/display/key-filter.c index d838430..66ab61c 100644 --- a/plugins/iot/display/key-filter.c +++ b/plugins/iot/display/key-filter.c @@ -43,6 +43,7 @@ #include "led/touch-key.h" #include "apps/apps.h" #include "display/display-lock.h" +#include "power/power-control.h" #ifndef KEY_SCREENLOCK #define KEY_SCREENLOCK 0x98 diff --git a/plugins/mobile/display/core.c b/plugins/mobile/display/core.c index 78c8892..ec01102 100644 --- a/plugins/mobile/display/core.c +++ b/plugins/mobile/display/core.c @@ -56,6 +56,7 @@ #include "extcon/extcon.h" #include "battery/power-supply.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "power/boot.h" #include "power/doze.h" #include "dd-display.h" @@ -707,10 +708,10 @@ void set_dim_state(bool on) void lcd_on_direct(enum device_flags flags) { - if (power_ops.get_power_lock_support() + if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); set_pm_cur_state(S_NORMAL); } @@ -1003,7 +1004,7 @@ static void proc_condition_lock(PMMsg *data) get_pname(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && - (power_ops.get_power_lock() == POWER_UNLOCK)) + (pm_get_power_lock() == POWER_UNLOCK)) proc_change_state(data->cond, INTERNAL_LOCK_PM); if (data->timeout > 0) { @@ -1525,9 +1526,9 @@ static int default_action(int timeout) } if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1587,9 +1588,9 @@ static int default_action(int timeout) if (backlight_ops.get_lcd_power() == DPMS_ON) lcd_off_procedure(LCD_OFF_BY_TIMEOUT); - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* sleep state : set system mode to SUSPEND */ - if (power_ops.get_wakeup_count(&wakeup_count) < 0) + if (get_wakeup_count(&wakeup_count) < 0) _E("Wakeup count read error."); if (wakeup_count < 0) { @@ -1597,7 +1598,7 @@ static int default_action(int timeout) goto go_lcd_off; } - if (power_ops.set_wakeup_count(wakeup_count) < 0) { + if (set_wakeup_count(wakeup_count) < 0) { _E("Wakeup count write error."); goto go_lcd_off; } @@ -1612,18 +1613,17 @@ go_suspend: pm_history_save(PM_LOG_SLEEP, get_pm_cur_state()); #endif broadcast_pm_suspend(); - if (power_ops.get_power_lock_support()) { - if (power_ops.enable_autosleep) - power_ops.enable_autosleep(); + if (pm_get_power_lock_support()) { + pm_enable_autosleep(); - if (power_ops.power_unlock() < 0) + if (pm_power_unlock() < 0) _E("Power unlock state error."); } else { - power_ops.suspend(); + pm_suspend(); _I("system wakeup!!"); system_wakeup_flag = true; /* Resume !! */ - if (power_ops.check_wakeup_src() == EVENT_DEVICE) + if (check_wakeup_src() == EVENT_DEVICE) /* system waked up by devices */ states[get_pm_cur_state()].trans(EVENT_DEVICE); else @@ -1633,7 +1633,7 @@ go_suspend: return 0; go_lcd_off: - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* Resume !! */ states[get_pm_cur_state()].trans(EVENT_DEVICE); } @@ -2366,9 +2366,9 @@ static void display_init(void *data) if (flags & WITHOUT_STARTNOTI) { /* start without noti */ _I("Start Power managing without noti"); - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/mobile/display/device-interface.c b/plugins/mobile/display/device-interface.c index 2f259d1..8d53e2e 100644 --- a/plugins/mobile/display/device-interface.c +++ b/plugins/mobile/display/device-interface.c @@ -44,8 +44,7 @@ #include "display/display-dpms.h" #include "display/display.h" #include "power/boot.h" - -#define SET_SUSPEND_TIME 0.5 +#include "power/power-control.h" #define TOUCH_ON 1 #define TOUCH_OFF 0 @@ -56,12 +55,6 @@ #define LCD_PHASED_DELAY 10000 /* microsecond */ #define DUMP_MODE_WAITING_TIME 600000 /* milisecond */ -#define POWER_AUTOSLEEP_PATH "/sys/power/autosleep" -#define POWER_LOCK_PATH "/sys/power/wake_lock" -#define POWER_UNLOCK_PATH "/sys/power/wake_unlock" -#define POWER_WAKEUP_PATH "/sys/power/wakeup_count" -#define POWER_STATE_PATH "/sys/power/state" - #define DISPLAY_HAL_LIB_PATH "/usr/lib/libdisplay-hal.so" #define GESTURE_STR "gesture" @@ -73,19 +66,12 @@ #define PALM_STR "palm" #define UNKNOWN_STR "unknown" -#define FREEZER_VITAL_WAKEUP_CGROUP "/sys/fs/cgroup/freezer/vital_wakeup/freezer.state" - struct _backlight_ops backlight_ops; -struct _power_ops power_ops; -static int mainlock_status = POWER_UNLOCK; static bool custom_status; static int custom_brightness; static int force_brightness; static int default_brightness; -static int vital_support = -2; -static int vital_service; -static bool vital_sleep; static int dpms_running_state = DPMS_SETTING_DONE; static struct display_device *display_dev; static guint release_timer; @@ -131,133 +117,6 @@ static int bl_brt(int brightness, int delay) return ret; } -static int system_suspend(void) -{ - int ret; - - _I("system suspend"); - ret = sys_set_str(POWER_STATE_PATH, "mem"); - _I("System resume: %d", ret); - return 0; -} - -static int system_enable_autosleep(void) -{ - _I("System autosleep enabled."); - return sys_set_str(POWER_AUTOSLEEP_PATH, "mem"); -} - -static int vital_mode_support(void) -{ - if (vital_support < 0) { - FILE *fp; - - fp = fopen(FREEZER_VITAL_WAKEUP_CGROUP, "r"); - if (fp == NULL) { - _E("%s open failed", FREEZER_VITAL_WAKEUP_CGROUP); - /* read max 2 times to check if this file exist */ - vital_support++; - return 0; - } - vital_support = 1; - fclose(fp); - } - return vital_support; -} - -static int suspend_other_process(int type) -{ - int ret = 0; - char buf[8]; - const char *command[1]; - - if (vital_service == type) - return ret; - - if (type == VITAL_WAKEUP && vital_service > VITAL_SLEEP) - return ret; - - vital_service = type; - - if (!vital_mode_support()) - return ret; - - if (type == VITAL_SLEEP) { - snprintf(buf, sizeof(buf), "%s", "sleep"); - command[0] = buf; - dbus_handle_method_sync_timeout(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", - command, - SET_SUSPEND_TIME*1000); - vital_sleep = true; - } else if (type == VITAL_WAKEUP) { - snprintf(buf, sizeof(buf), "%s", "wakeup"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - } else if (type == VITAL_EXIT) { - snprintf(buf, sizeof(buf), "%s", "exit"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - vital_sleep = false; - } - return ret; -} - -static int system_power_lock(void) -{ - _I("system power lock"); - suspend_other_process(VITAL_WAKEUP); - mainlock_status = POWER_LOCK; - - return sys_set_str(POWER_LOCK_PATH, "mainlock"); -} - -static int system_power_unlock(void) -{ - _I("system power unlock"); - suspend_other_process(VITAL_SLEEP); - mainlock_status = POWER_UNLOCK; - - return sys_set_str(POWER_UNLOCK_PATH, "mainlock"); -} - -static int system_get_power_lock(void) -{ - return mainlock_status; -} - -static int system_get_power_lock_support(void) -{ - static int power_lock_support = -1; - int ret; - - if (power_lock_support >= 0) - goto out; - - ret = sys_check_node(POWER_LOCK_PATH); - if (ret < 0) - power_lock_support = false; - else - power_lock_support = true; - - _I("System power lock: %s", - (power_lock_support ? "support" : "not support")); - -out: - return power_lock_support; -} - static int get_lcd_power(void) { enum display_state val; @@ -510,41 +369,6 @@ static int get_default_brt(void) return default_brightness; } -static int check_wakeup_src(void) -{ - /* TODO if nedded. - * return wackeup source. user input or device interrupts? (EVENT_DEVICE or EVENT_INPUT) - */ - return EVENT_DEVICE; -} - -static int get_wakeup_count(int *cnt) -{ - int ret; - int wakeup_count; - - if (!cnt) - return -EINVAL; - - ret = sys_get_int(POWER_WAKEUP_PATH, &wakeup_count); - if (ret < 0) - return ret; - - *cnt = wakeup_count; - return 0; -} - -static int set_wakeup_count(int cnt) -{ - int ret; - - ret = sys_set_int(POWER_WAKEUP_PATH, cnt); - if (ret < 0) - return ret; - - return 0; -} - static int get_max_brightness(void) { static int max = -1; @@ -926,16 +750,6 @@ static void _init_ops(void) backlight_ops.transit_brt = change_brightness; backlight_ops.blink = blink; backlight_ops.release_blink = release_blink; - - power_ops.suspend = system_suspend; - power_ops.enable_autosleep = system_enable_autosleep; - power_ops.power_lock = system_power_lock; - power_ops.power_unlock = system_power_unlock; - power_ops.get_power_lock = system_get_power_lock; - power_ops.get_power_lock_support = system_get_power_lock_support; - power_ops.check_wakeup_src = check_wakeup_src; - power_ops.get_wakeup_count = get_wakeup_count; - power_ops.set_wakeup_count = set_wakeup_count; } int display_service_load(void) @@ -985,24 +799,6 @@ int display_service_free(void) return 0; } -bool vital_mode(void) -{ - return vital_sleep; -} - -static int vital_state_changed(void *data) -{ - int type; - - assert(data); - - type = *(int *)data; - if (type == VITAL_EXIT) - suspend_other_process(VITAL_EXIT); - - return 0; -} - static int booting_done(void *data) { static int done = false; diff --git a/plugins/mobile/display/key-filter.c b/plugins/mobile/display/key-filter.c index 25905cb..546d4e1 100644 --- a/plugins/mobile/display/key-filter.c +++ b/plugins/mobile/display/key-filter.c @@ -40,6 +40,7 @@ #include "shared/common.h" #include "shared/plugin.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "led/touch-key.h" #include "apps/apps.h" #include "display/display-lock.h" diff --git a/plugins/tv/display/core.c b/plugins/tv/display/core.c index 9edae8c..058fa83 100644 --- a/plugins/tv/display/core.c +++ b/plugins/tv/display/core.c @@ -56,6 +56,7 @@ #include "extcon/extcon.h" #include "battery/power-supply.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "power/boot.h" #include "power/doze.h" #include "dd-display.h" @@ -700,10 +701,10 @@ void set_dim_state(bool on) void lcd_on_direct(enum device_flags flags) { - if (power_ops.get_power_lock_support() + if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); set_pm_cur_state(S_NORMAL); } @@ -992,7 +993,7 @@ static void proc_condition_lock(PMMsg *data) get_pname(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && - (power_ops.get_power_lock() == POWER_UNLOCK)) + (pm_get_power_lock() == POWER_UNLOCK)) proc_change_state(data->cond, INTERNAL_LOCK_PM); if (data->timeout > 0) { @@ -1515,9 +1516,9 @@ static int default_action(int timeout) } if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1577,9 +1578,9 @@ static int default_action(int timeout) if (backlight_ops.get_lcd_power() == DPMS_ON) lcd_off_procedure(LCD_OFF_BY_TIMEOUT); - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* sleep state : set system mode to SUSPEND */ - if (power_ops.get_wakeup_count(&wakeup_count) < 0) + if (get_wakeup_count(&wakeup_count) < 0) _E("Wakeup count read error."); if (wakeup_count < 0) { @@ -1587,7 +1588,7 @@ static int default_action(int timeout) goto go_lcd_off; } - if (power_ops.set_wakeup_count(wakeup_count) < 0) { + if (set_wakeup_count(wakeup_count) < 0) { _E("Wakeup count write error."); goto go_lcd_off; } @@ -1602,18 +1603,17 @@ go_suspend: pm_history_save(PM_LOG_SLEEP, get_pm_cur_state()); #endif broadcast_pm_suspend(); - if (power_ops.get_power_lock_support()) { - if (power_ops.enable_autosleep) - power_ops.enable_autosleep(); + if (pm_get_power_lock_support()) { + pm_enable_autosleep(); - if (power_ops.power_unlock() < 0) + if (pm_power_unlock() < 0) _E("Power unlock state error."); } else { - power_ops.suspend(); + pm_suspend(); _I("system wakeup!!"); system_wakeup_flag = true; /* Resume !! */ - if (power_ops.check_wakeup_src() == EVENT_DEVICE) + if (check_wakeup_src() == EVENT_DEVICE) /* system waked up by devices */ states[get_pm_cur_state()].trans(EVENT_DEVICE); else @@ -1623,7 +1623,7 @@ go_suspend: return 0; go_lcd_off: - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* Resume !! */ states[get_pm_cur_state()].trans(EVENT_DEVICE); } @@ -2356,9 +2356,9 @@ static void display_init(void *data) if (flags & WITHOUT_STARTNOTI) { /* start without noti */ _I("Start Power managing without noti"); - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/tv/display/device-interface.c b/plugins/tv/display/device-interface.c index 03abf73..ebaf569 100644 --- a/plugins/tv/display/device-interface.c +++ b/plugins/tv/display/device-interface.c @@ -43,8 +43,7 @@ #include "device-node.h" #include "display/display-dpms.h" #include "display/display.h" - -#define SET_SUSPEND_TIME 0.5 +#include "power/power-control.h" #define TOUCH_ON 1 #define TOUCH_OFF 0 @@ -55,12 +54,6 @@ #define LCD_PHASED_DELAY 10000 /* microsecond */ #define DUMP_MODE_WAITING_TIME 600000 /* milisecond */ -#define POWER_AUTOSLEEP_PATH "/sys/power/autosleep" -#define POWER_LOCK_PATH "/sys/power/wake_lock" -#define POWER_UNLOCK_PATH "/sys/power/wake_unlock" -#define POWER_WAKEUP_PATH "/sys/power/wakeup_count" -#define POWER_STATE_PATH "/sys/power/state" - #define DISPLAY_HAL_LIB_PATH "/usr/lib/libdisplay-hal.so" #define GESTURE_STR "gesture" @@ -75,16 +68,11 @@ #define FREEZER_VITAL_WAKEUP_CGROUP "/sys/fs/cgroup/freezer/vital_wakeup/freezer.state" struct _backlight_ops backlight_ops; -struct _power_ops power_ops; -static int mainlock_status = POWER_UNLOCK; static bool custom_status; static int custom_brightness; static int force_brightness; static int default_brightness; -static int vital_support = -2; -static int vital_service; -static bool vital_sleep; static int dpms_running_state = DPMS_SETTING_DONE; static struct display_device *display_dev; static guint release_timer; @@ -130,133 +118,6 @@ static int bl_brt(int brightness, int delay) return ret; } -static int system_suspend(void) -{ - int ret; - - _I("system suspend"); - ret = sys_set_str(POWER_STATE_PATH, "mem"); - _I("System resume: %d", ret); - return 0; -} - -static int system_enable_autosleep(void) -{ - _I("System autosleep enabled."); - return sys_set_str(POWER_AUTOSLEEP_PATH, "mem"); -} - -static int vital_mode_support(void) -{ - if (vital_support < 0) { - FILE *fp; - - fp = fopen(FREEZER_VITAL_WAKEUP_CGROUP, "r"); - if (fp == NULL) { - _E("%s open failed", FREEZER_VITAL_WAKEUP_CGROUP); - /* read max 2 times to check if this file exist */ - vital_support++; - return 0; - } - vital_support = 1; - fclose(fp); - } - return vital_support; -} - -static int suspend_other_process(int type) -{ - int ret = 0; - char buf[8]; - const char *command[1]; - - if (vital_service == type) - return ret; - - if (type == VITAL_WAKEUP && vital_service > VITAL_SLEEP) - return ret; - - vital_service = type; - - if (!vital_mode_support()) - return ret; - - if (type == VITAL_SLEEP) { - snprintf(buf, sizeof(buf), "%s", "sleep"); - command[0] = buf; - dbus_handle_method_sync_timeout(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", - command, - SET_SUSPEND_TIME*1000); - vital_sleep = true; - } else if (type == VITAL_WAKEUP) { - snprintf(buf, sizeof(buf), "%s", "wakeup"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - } else if (type == VITAL_EXIT) { - snprintf(buf, sizeof(buf), "%s", "exit"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - vital_sleep = false; - } - return ret; -} - -static int system_power_lock(void) -{ - _I("system power lock"); - suspend_other_process(VITAL_WAKEUP); - mainlock_status = POWER_LOCK; - - return sys_set_str(POWER_LOCK_PATH, "mainlock"); -} - -static int system_power_unlock(void) -{ - _I("system power unlock"); - suspend_other_process(VITAL_SLEEP); - mainlock_status = POWER_UNLOCK; - - return sys_set_str(POWER_UNLOCK_PATH, "mainlock"); -} - -static int system_get_power_lock(void) -{ - return mainlock_status; -} - -static int system_get_power_lock_support(void) -{ - static int power_lock_support = -1; - int ret; - - if (power_lock_support >= 0) - goto out; - - ret = sys_check_node(POWER_LOCK_PATH); - if (ret < 0) - power_lock_support = false; - else - power_lock_support = true; - - _I("System power lock: %s", - (power_lock_support ? "support" : "not support")); - -out: - return power_lock_support; -} - static int get_lcd_power(void) { enum display_state val; @@ -509,41 +370,6 @@ static int get_default_brt(void) return default_brightness; } -static int check_wakeup_src(void) -{ - /* TODO if nedded. - * return wackeup source. user input or device interrupts? (EVENT_DEVICE or EVENT_INPUT) - */ - return EVENT_DEVICE; -} - -static int get_wakeup_count(int *cnt) -{ - int ret; - int wakeup_count; - - if (!cnt) - return -EINVAL; - - ret = sys_get_int(POWER_WAKEUP_PATH, &wakeup_count); - if (ret < 0) - return ret; - - *cnt = wakeup_count; - return 0; -} - -static int set_wakeup_count(int cnt) -{ - int ret; - - ret = sys_set_int(POWER_WAKEUP_PATH, cnt); - if (ret < 0) - return ret; - - return 0; -} - static int get_max_brightness(void) { static int max = -1; @@ -925,16 +751,6 @@ static void _init_ops(void) backlight_ops.transit_brt = change_brightness; backlight_ops.blink = blink; backlight_ops.release_blink = release_blink; - - power_ops.suspend = system_suspend; - power_ops.enable_autosleep = system_enable_autosleep; - power_ops.power_lock = system_power_lock; - power_ops.power_unlock = system_power_unlock; - power_ops.get_power_lock = system_get_power_lock; - power_ops.get_power_lock_support = system_get_power_lock_support; - power_ops.check_wakeup_src = check_wakeup_src; - power_ops.get_wakeup_count = get_wakeup_count; - power_ops.set_wakeup_count = set_wakeup_count; } int display_service_load(void) @@ -984,23 +800,6 @@ int display_service_free(void) return 0; } -bool vital_mode(void) -{ - return vital_sleep; -} - -static int vital_state_changed(void *data) -{ - int type; - - assert(data); - - type = *(int *)data; - if (type == VITAL_EXIT) - suspend_other_process(VITAL_EXIT); - - return 0; -} /* Dummy. Do not consider detached display state */ int is_lcdon_blocked(void) diff --git a/plugins/tv/display/key-filter.c b/plugins/tv/display/key-filter.c index a038caa..d88ff6b 100644 --- a/plugins/tv/display/key-filter.c +++ b/plugins/tv/display/key-filter.c @@ -40,6 +40,7 @@ #include "shared/common.h" #include "shared/plugin.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "led/touch-key.h" #include "apps/apps.h" #include "display/display-lock.h" diff --git a/plugins/tv/display/state-tv.c b/plugins/tv/display/state-tv.c index 663b66f..fdf48c6 100644 --- a/plugins/tv/display/state-tv.c +++ b/plugins/tv/display/state-tv.c @@ -27,6 +27,7 @@ #include "display/display-ops.h" #include "display/display-lock.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "core.h" #include "poll.h" #include "device-interface.h" @@ -431,7 +432,7 @@ static int suspend_action(int timeout) /* TODO: set wakeup count */ /* sleep state : set system mode to SUSPEND */ - power_ops.suspend(); + pm_suspend(); _I("system wakeup!!"); diff --git a/plugins/wearable/display/core.c b/plugins/wearable/display/core.c index 314e0e7..dda1739 100644 --- a/plugins/wearable/display/core.c +++ b/plugins/wearable/display/core.c @@ -57,6 +57,7 @@ #include "extcon/extcon.h" #include "battery/power-supply.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "power/boot.h" #include "power/doze.h" #include "dd-display.h" @@ -728,10 +729,10 @@ void lcd_on_direct(enum device_flags flags) { int ret; - if (power_ops.get_power_lock_support() + if (pm_get_power_lock_support() && (get_pm_cur_state() == S_SLEEP)) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); set_pm_cur_state(S_NORMAL); } @@ -1059,7 +1060,7 @@ static void proc_condition_lock(PMMsg *data) get_pname(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && - (power_ops.get_power_lock() == POWER_UNLOCK)) + (pm_get_power_lock() == POWER_UNLOCK)) proc_change_state(data->cond, INTERNAL_LOCK_PM); if (data->timeout > 0) { @@ -1599,9 +1600,9 @@ static int default_action(int timeout) } if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) { - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } set_setting_pmstate(get_pm_cur_state()); pm_cur_state = get_pm_cur_state(); @@ -1664,9 +1665,9 @@ static int default_action(int timeout) if (backlight_ops.get_lcd_power() == DPMS_ON) lcd_off_procedure(LCD_OFF_BY_TIMEOUT); - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* sleep state : set system mode to SUSPEND */ - if (power_ops.get_wakeup_count(&wakeup_count) < 0) + if (get_wakeup_count(&wakeup_count) < 0) _E("Wakeup count read error."); if (wakeup_count < 0) { @@ -1674,7 +1675,7 @@ static int default_action(int timeout) goto go_lcd_off; } - if (power_ops.set_wakeup_count(wakeup_count) < 0) { + if (set_wakeup_count(wakeup_count) < 0) { _E("Wakeup count write error."); goto go_lcd_off; } @@ -1689,18 +1690,17 @@ go_suspend: pm_history_save(PM_LOG_SLEEP, get_pm_cur_state()); #endif broadcast_pm_suspend(); - if (power_ops.get_power_lock_support()) { - if (power_ops.enable_autosleep) - power_ops.enable_autosleep(); + if (pm_get_power_lock_support()) { + pm_enable_autosleep(); - if (power_ops.power_unlock() < 0) + if (pm_power_unlock() < 0) _E("Power unlock state error."); } else { - power_ops.suspend(); + pm_suspend(); _I("system wakeup!!"); system_wakeup_flag = true; /* Resume !! */ - if (power_ops.check_wakeup_src() == EVENT_DEVICE) + if (check_wakeup_src() == EVENT_DEVICE) /* system waked up by devices */ states[get_pm_cur_state()].trans(EVENT_DEVICE); else @@ -1710,7 +1710,7 @@ go_suspend: return 0; go_lcd_off: - if (!power_ops.get_power_lock_support()) { + if (!pm_get_power_lock_support()) { /* Resume !! */ states[get_pm_cur_state()].trans(EVENT_DEVICE); } @@ -2480,9 +2480,9 @@ static void display_init(void *data) if (flags & WITHOUT_STARTNOTI) { /* start without noti */ _I("Start Power managing without noti"); - if (power_ops.get_power_lock_support()) { + if (pm_get_power_lock_support()) { broadcast_pm_wakeup(); - power_ops.power_lock(); + pm_power_lock(); } /* * Lock lcd off until booting is done. diff --git a/plugins/wearable/display/device-interface.c b/plugins/wearable/display/device-interface.c old mode 100755 new mode 100644 index 7a5fd39..224ba65 --- a/plugins/wearable/display/device-interface.c +++ b/plugins/wearable/display/device-interface.c @@ -45,8 +45,7 @@ #include "display/display.h" #include "battery-monitor.h" #include "battery/power-supply.h" - -#define SET_SUSPEND_TIME 0.5 +#include "power/power-control.h" #define TOUCH_ON 1 #define TOUCH_OFF 0 @@ -57,12 +56,6 @@ #define LCD_PHASED_DELAY 10000 /* microsecond */ #define DUMP_MODE_WAITING_TIME 600000 /* milisecond */ -#define POWER_AUTOSLEEP_PATH "/sys/power/autosleep" -#define POWER_LOCK_PATH "/sys/power/wake_lock" -#define POWER_UNLOCK_PATH "/sys/power/wake_unlock" -#define POWER_WAKEUP_PATH "/sys/power/wakeup_count" -#define POWER_STATE_PATH "/sys/power/state" - #define DISPLAY_HAL_LIB_PATH "/usr/lib/libdisplay-hal.so" #define GESTURE_STR "gesture" @@ -79,16 +72,10 @@ struct _backlight_ops backlight_ops; -struct _power_ops power_ops; - -static int mainlock_status = POWER_UNLOCK; static bool custom_status; static int custom_brightness; static int force_brightness; static int default_brightness; -static int vital_support = -2; -static int vital_service; -static bool vital_sleep; static int dpms_running_state = DPMS_SETTING_DONE; static struct display_device *display_dev; static guint release_timer; @@ -138,133 +125,6 @@ static int bl_brt(int brightness, int delay) return ret; } -static int system_suspend(void) -{ - int ret; - - _I("system suspend"); - ret = sys_set_str(POWER_STATE_PATH, "mem"); - _I("System resume: %d", ret); - return 0; -} - -static int system_enable_autosleep(void) -{ - _I("System autosleep enabled."); - return sys_set_str(POWER_AUTOSLEEP_PATH, "mem"); -} - -static int vital_mode_support(void) -{ - if (vital_support < 0) { - FILE *fp; - - fp = fopen(FREEZER_VITAL_WAKEUP_CGROUP, "r"); - if (fp == NULL) { - _E("%s open failed", FREEZER_VITAL_WAKEUP_CGROUP); - /* read max 2 times to check if this file exist */ - vital_support++; - return 0; - } - vital_support = 1; - fclose(fp); - } - return vital_support; -} - -static int suspend_other_process(int type) -{ - int ret = 0; - char buf[8]; - const char *command[1]; - - if (vital_service == type) - return ret; - - if (type == VITAL_WAKEUP && vital_service > VITAL_SLEEP) - return ret; - - vital_service = type; - - if (!vital_mode_support()) - return ret; - - if (type == VITAL_SLEEP) { - snprintf(buf, sizeof(buf), "%s", "sleep"); - command[0] = buf; - dbus_handle_method_sync_timeout(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", - command, - SET_SUSPEND_TIME*1000); - vital_sleep = true; - } else if (type == VITAL_WAKEUP) { - snprintf(buf, sizeof(buf), "%s", "wakeup"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - } else if (type == VITAL_EXIT) { - snprintf(buf, sizeof(buf), "%s", "exit"); - command[0] = buf; - ret = dbus_handle_method_async(RESOURCED_BUS_NAME, - RESOURCED_PATH_FREEZER, - RESOURCED_INTERFACE_FREEZER, - "SetSuspend", - "s", command); - vital_sleep = false; - } - return ret; -} - -static int system_power_lock(void) -{ - _I("system power lock"); - suspend_other_process(VITAL_WAKEUP); - mainlock_status = POWER_LOCK; - - return sys_set_str(POWER_LOCK_PATH, "mainlock"); -} - -static int system_power_unlock(void) -{ - _I("system power unlock"); - suspend_other_process(VITAL_SLEEP); - mainlock_status = POWER_UNLOCK; - - return sys_set_str(POWER_UNLOCK_PATH, "mainlock"); -} - -static int system_get_power_lock(void) -{ - return mainlock_status; -} - -static int system_get_power_lock_support(void) -{ - static int power_lock_support = -1; - int ret; - - if (power_lock_support >= 0) - goto out; - - ret = sys_check_node(POWER_LOCK_PATH); - if (ret < 0) - power_lock_support = false; - else - power_lock_support = true; - - _I("System power lock: %s", - (power_lock_support ? "support" : "not support")); - -out: - return power_lock_support; -} - static int get_lcd_power(void) { enum display_state val; @@ -516,41 +376,6 @@ static int get_default_brt(void) return default_brightness; } -static int check_wakeup_src(void) -{ - /* TODO if nedded. - * return wackeup source. user input or device interrupts? (EVENT_DEVICE or EVENT_INPUT) - */ - return EVENT_DEVICE; -} - -static int get_wakeup_count(int *cnt) -{ - int ret; - int wakeup_count; - - if (!cnt) - return -EINVAL; - - ret = sys_get_int(POWER_WAKEUP_PATH, &wakeup_count); - if (ret < 0) - return ret; - - *cnt = wakeup_count; - return 0; -} - -static int set_wakeup_count(int cnt) -{ - int ret; - - ret = sys_set_int(POWER_WAKEUP_PATH, cnt); - if (ret < 0) - return ret; - - return 0; -} - static int get_max_brightness(void) { static int max = -1; @@ -989,16 +814,6 @@ static void _init_ops(void) /* auto-test only function */ backlight_ops.get_brightness_raw = get_brightness; /* always fetch brightness from node even LBM mode */ - - power_ops.suspend = system_suspend; - power_ops.enable_autosleep = system_enable_autosleep; - power_ops.power_lock = system_power_lock; - power_ops.power_unlock = system_power_unlock; - power_ops.get_power_lock = system_get_power_lock; - power_ops.get_power_lock_support = system_get_power_lock_support; - power_ops.check_wakeup_src = check_wakeup_src; - power_ops.get_wakeup_count = get_wakeup_count; - power_ops.set_wakeup_count = set_wakeup_count; } int display_service_load(void) @@ -1055,11 +870,6 @@ int display_service_free(void) return 0; } -bool vital_mode(void) -{ - return vital_sleep; -} - int is_lcdon_blocked(void) { int state = backlight_ops.get_lcd_power(); @@ -1070,19 +880,6 @@ int is_lcdon_blocked(void) return LCDON_BLOCK_NONE; } -static int vital_state_changed(void *data) -{ - int type; - - assert(data); - - type = *(int *)data; - if (type == VITAL_EXIT) - suspend_other_process(VITAL_EXIT); - - return 0; -} - int init_sysfs(unsigned int flags) { _init_ops(); diff --git a/plugins/wearable/display/key-filter.c b/plugins/wearable/display/key-filter.c index c960efa..a609cd1 100644 --- a/plugins/wearable/display/key-filter.c +++ b/plugins/wearable/display/key-filter.c @@ -40,6 +40,7 @@ #include "shared/common.h" #include "shared/plugin.h" #include "power/power-handler.h" +#include "power/power-control.h" #include "led/touch-key.h" #include "apps/apps.h" #include "display/display-lock.h" diff --git a/src/display/device-interface.h b/src/display/device-interface.h index b02a997..078637b 100644 --- a/src/display/device-interface.h +++ b/src/display/device-interface.h @@ -55,22 +55,10 @@ enum { EVENT_END, }; - -/* - * Vital state enumeration - */ -enum vital_state { - VITAL_SLEEP, /* suspend state */ - VITAL_WAKEUP, /* resume state */ - VITAL_DISPLAY_WAKEUP, - VITAL_EXIT, -}; - int init_sysfs(unsigned int); int exit_sysfs(void); int display_service_load(void); int display_service_free(void); -bool vital_mode(void); struct _backlight_ops { int (*off)(enum device_flags); @@ -103,20 +91,7 @@ struct _backlight_ops { void (*release_blink)(void); }; -struct _power_ops { - int (*suspend)(void); - int (*enable_autosleep)(void); - int (*power_lock)(void); - int (*power_unlock)(void); - int (*get_power_lock)(void); - int (*get_power_lock_support)(void); - int (*check_wakeup_src)(void); - int (*get_wakeup_count)(int *cnt); - int (*set_wakeup_count)(int cnt); -}; - extern struct _backlight_ops backlight_ops; -extern struct _power_ops power_ops; enum dpms_state { DPMS_ON, /* In use */ @@ -127,11 +102,6 @@ enum dpms_state { DPMS_DETACH, /* Display detached */ }; -enum mainlock_state { - POWER_UNLOCK = 0, - POWER_LOCK, -}; - struct display_device *display_dev_get(void); bool display_dimstay_check(void); void dpms_set_running_state(int val); diff --git a/src/power/power-control.c b/src/power/power-control.c new file mode 100755 index 0000000..ccef818 --- /dev/null +++ b/src/power/power-control.c @@ -0,0 +1,229 @@ +/* + * deviced + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core/log.h" +#include "core/devices.h" +#include "core/common.h" +#include "core/device-notifier.h" +#include "vconf.h" +#include "device-node.h" +#include "display/display-dpms.h" +#include "display/display.h" +#include "power/boot.h" +#include "power-control.h" + +static int mainlock_status = POWER_UNLOCK; +static int vital_service; +static bool vital_sleep; +static int vital_support = -2; + +bool vital_mode(void) +{ + return vital_sleep; +} + +static int vital_mode_support(void) +{ + if (vital_support < 0) { + FILE *fp; + + fp = fopen(FREEZER_VITAL_WAKEUP_CGROUP, "r"); + if (fp == NULL) { + _E("%s open failed", FREEZER_VITAL_WAKEUP_CGROUP); + /* read max 2 times to check if this file exist */ + vital_support++; + return 0; + } + vital_support = 1; + fclose(fp); + } + return vital_support; +} + +int suspend_other_process(int type) +{ + int ret = 0; + char buf[8]; + const char *command[1]; + + if (vital_service == type) + return ret; + + if (type == VITAL_WAKEUP && vital_service > VITAL_SLEEP) + return ret; + + vital_service = type; + + if (!vital_mode_support()) + return ret; + + if (type == VITAL_SLEEP) { + snprintf(buf, sizeof(buf), "%s", "sleep"); + command[0] = buf; + dbus_handle_method_sync_timeout(RESOURCED_BUS_NAME, + RESOURCED_PATH_FREEZER, + RESOURCED_INTERFACE_FREEZER, + "SetSuspend", + "s", + command, + SET_SUSPEND_TIME*1000); + vital_sleep = true; + } else if (type == VITAL_WAKEUP) { + snprintf(buf, sizeof(buf), "%s", "wakeup"); + command[0] = buf; + ret = dbus_handle_method_async(RESOURCED_BUS_NAME, + RESOURCED_PATH_FREEZER, + RESOURCED_INTERFACE_FREEZER, + "SetSuspend", + "s", command); + } else if (type == VITAL_EXIT) { + snprintf(buf, sizeof(buf), "%s", "exit"); + command[0] = buf; + ret = dbus_handle_method_async(RESOURCED_BUS_NAME, + RESOURCED_PATH_FREEZER, + RESOURCED_INTERFACE_FREEZER, + "SetSuspend", + "s", command); + vital_sleep = false; + } + return ret; +} + +int vital_state_changed(void *data) +{ + int type; + + assert(data); + + type = *(int *)data; + if (type == VITAL_EXIT) + suspend_other_process(VITAL_EXIT); + + return 0; +} + +int pm_suspend(void) +{ + int ret; + + _I("system suspend"); + ret = sys_set_str(POWER_STATE_PATH, "mem"); + _I("System resume: %d", ret); + return 0; +} + +int pm_enable_autosleep(void) +{ + _I("System autosleep enabled."); + return sys_set_str(POWER_AUTOSLEEP_PATH, "mem"); +} + +int pm_power_lock(void) +{ + _I("system power lock"); + suspend_other_process(VITAL_WAKEUP); + mainlock_status = POWER_LOCK; + + return sys_set_str(POWER_LOCK_PATH, "mainlock"); +} + +int pm_get_power_lock(void) +{ + return mainlock_status; +} + +int pm_get_power_lock_support(void) +{ + static int power_lock_support = -1; + int ret; + + if (power_lock_support >= 0) + goto out; + + ret = sys_check_node(POWER_LOCK_PATH); + if (ret < 0) + power_lock_support = false; + else + power_lock_support = true; + + _I("System power lock: %s", + (power_lock_support ? "support" : "not support")); + +out: + return power_lock_support; +} + +int pm_power_unlock(void) +{ + _I("system power unlock"); + suspend_other_process(VITAL_SLEEP); + mainlock_status = POWER_UNLOCK; + + return sys_set_str(POWER_UNLOCK_PATH, "mainlock"); +} + +int check_wakeup_src(void) +{ + /* TODO if nedded. + * return wackeup source. user input or device interrupts? (EVENT_DEVICE or EVENT_INPUT) + */ + return EVENT_DEVICE; +} + +int get_wakeup_count(int *cnt) +{ + int ret; + int wakeup_count; + + if (!cnt) + return -EINVAL; + + ret = sys_get_int(POWER_WAKEUP_PATH, &wakeup_count); + if (ret < 0) + return ret; + + *cnt = wakeup_count; + return 0; +} + +int set_wakeup_count(int cnt) +{ + int ret; + + ret = sys_set_int(POWER_WAKEUP_PATH, cnt); + if (ret < 0) + return ret; + + return 0; +} diff --git a/src/power/power-control.h b/src/power/power-control.h new file mode 100644 index 0000000..f1016b4 --- /dev/null +++ b/src/power/power-control.h @@ -0,0 +1,59 @@ +/* + * deviced + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __POWER_CONTROL_H__ +#define __POWER_CONTROL_H__ + +#define POWER_AUTOSLEEP_PATH "/sys/power/autosleep" +#define POWER_LOCK_PATH "/sys/power/wake_lock" +#define POWER_UNLOCK_PATH "/sys/power/wake_unlock" +#define POWER_WAKEUP_PATH "/sys/power/wakeup_count" +#define POWER_STATE_PATH "/sys/power/state" + +#define FREEZER_VITAL_WAKEUP_CGROUP "/sys/fs/cgroup/freezer/vital_wakeup/freezer.state" + +#define SET_SUSPEND_TIME 0.5 + +enum mainlock_state { + POWER_UNLOCK = 0, + POWER_LOCK, +}; + +/* + * Vital state enumeration + */ +enum vital_state { + VITAL_SLEEP, /* suspend state */ + VITAL_WAKEUP, /* resume state */ + VITAL_DISPLAY_WAKEUP, + VITAL_EXIT, +}; + +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 pm_get_power_lock(void); +int pm_get_power_lock_support(void); +int check_wakeup_src(void); +int get_wakeup_count(int *cnt); +int set_wakeup_count(int cnt); +int suspend_other_process(int type); +#endif /* __POWER_CONTROL_H__ */ -- 2.7.4