system/corennnnn
修订版 | 9067003e1c7ae017e32fb6845295c47064a9a7fe (tree) |
---|---|
时间 | 2016-12-09 16:22:09 |
作者 | Jaap Jan Meijer <jjmeijer88@gmai...> |
Commiter | Chih-Wei Huang |
libsuspend: make sleep state configurable and add a fallback
This patch allows the user to set the sleep state target from
Android properties for both wakeup_count and earlysuspend methods.
It also includes a fallback state if the default state is not
available and the user didn't set the sleep.state property.
Signed-off-by: Jaap Jan Meijer <jjmeijer88@gmail.com>
@@ -14,15 +14,22 @@ | ||
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | |
17 | +#include <errno.h> | |
18 | +#include <fcntl.h> | |
17 | 19 | #include <stdbool.h> |
20 | +#include <string.h> | |
18 | 21 | |
19 | 22 | #define LOG_TAG "libsuspend" |
20 | 23 | #include <cutils/log.h> |
24 | +#include <cutils/properties.h> | |
21 | 25 | |
22 | 26 | #include <suspend/autosuspend.h> |
23 | 27 | |
24 | 28 | #include "autosuspend_ops.h" |
25 | 29 | |
30 | +static const char *default_sleep_state = "mem"; | |
31 | +static const char *fallback_sleep_state = "freeze"; | |
32 | + | |
26 | 33 | static struct autosuspend_ops *autosuspend_ops; |
27 | 34 | static bool autosuspend_enabled; |
28 | 35 | static bool autosuspend_inited; |
@@ -110,3 +117,34 @@ int autosuspend_disable(void) | ||
110 | 117 | autosuspend_enabled = false; |
111 | 118 | return 0; |
112 | 119 | } |
120 | + | |
121 | +static bool sleep_state_available(const char *state) | |
122 | +{ | |
123 | + char buf[64]; | |
124 | + int fd = TEMP_FAILURE_RETRY(open(SYS_POWER_STATE, O_RDONLY)); | |
125 | + if (fd < 0) { | |
126 | + ALOGE("Error reading power state: %s", SYS_POWER_STATE); | |
127 | + return false; | |
128 | + } | |
129 | + TEMP_FAILURE_RETRY(read(fd, buf, 64)); | |
130 | + close(fd); | |
131 | + return !!strstr(buf, state); | |
132 | +} | |
133 | + | |
134 | +const char *get_sleep_state() | |
135 | +{ | |
136 | + static char sleep_state[PROPERTY_VALUE_MAX] = ""; | |
137 | + | |
138 | + if (!sleep_state[0]) { | |
139 | + if (property_get("sleep.state", sleep_state, NULL) > 0) { | |
140 | + ALOGD("autosuspend using sleep.state property (%s)", sleep_state); | |
141 | + } else if (sleep_state_available(default_sleep_state)) { | |
142 | + ALOGD("autosuspend using default sleep_state (%s)", default_sleep_state); | |
143 | + strncpy(sleep_state, default_sleep_state, PROPERTY_VALUE_MAX); | |
144 | + } else { | |
145 | + ALOGW("autosuspend \"%s\" unavailable, using fallback sleep.state (%s)", default_sleep_state, fallback_sleep_state); | |
146 | + strncpy(sleep_state, fallback_sleep_state, PROPERTY_VALUE_MAX); | |
147 | + } | |
148 | + } | |
149 | + return sleep_state; | |
150 | +} |
@@ -33,7 +33,6 @@ | ||
33 | 33 | #define EARLYSUSPEND_WAIT_FOR_FB_WAKE "/sys/power/wait_for_fb_wake" |
34 | 34 | |
35 | 35 | static int sPowerStatefd; |
36 | -static const char *pwr_state_mem = "mem"; | |
37 | 36 | static const char *pwr_state_on = "on"; |
38 | 37 | static pthread_t earlysuspend_thread; |
39 | 38 | static pthread_mutex_t earlysuspend_mutex = PTHREAD_MUTEX_INITIALIZER; |
@@ -116,12 +115,13 @@ static void *earlysuspend_thread_func(void __unused *arg) | ||
116 | 115 | static int autosuspend_earlysuspend_enable(void) |
117 | 116 | { |
118 | 117 | int ret; |
118 | + const char *sleep_state = get_sleep_state(); | |
119 | 119 | |
120 | 120 | ALOGI("autosuspend_earlysuspend_enable"); |
121 | 121 | |
122 | - ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem))); | |
122 | + ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, sleep_state, strlen(sleep_state))); | |
123 | 123 | if (ret < 0) { |
124 | - log_err("writing %s to %s", pwr_state_mem, SYS_POWER_STATE); | |
124 | + log_err("writing %s to %s", sleep_state, SYS_POWER_STATE); | |
125 | 125 | return ret; |
126 | 126 | } |
127 | 127 |
@@ -28,4 +28,6 @@ struct autosuspend_ops *autosuspend_autosleep_init(void); | ||
28 | 28 | struct autosuspend_ops *autosuspend_earlysuspend_init(void); |
29 | 29 | struct autosuspend_ops *autosuspend_wakeup_count_init(void); |
30 | 30 | |
31 | +const char *get_sleep_state(); | |
32 | + | |
31 | 33 | #endif |
@@ -37,7 +37,6 @@ static int state_fd; | ||
37 | 37 | static int wakeup_count_fd; |
38 | 38 | static pthread_t suspend_thread; |
39 | 39 | static sem_t suspend_lockout; |
40 | -static const char *sleep_state = "mem"; | |
41 | 40 | static void (*wakeup_func)(bool success) = NULL; |
42 | 41 | |
43 | 42 | static void *suspend_thread_func(void *arg __attribute__((unused))) |
@@ -80,6 +79,7 @@ static void *suspend_thread_func(void *arg __attribute__((unused))) | ||
80 | 79 | strerror_r(errno, buf, sizeof(buf)); |
81 | 80 | ALOGE("Error writing to %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf); |
82 | 81 | } else { |
82 | + const char *sleep_state = get_sleep_state(); | |
83 | 83 | ALOGV("%s: write %s to %s\n", __func__, sleep_state, SYS_POWER_STATE); |
84 | 84 | ret = TEMP_FAILURE_RETRY(write(state_fd, sleep_state, strlen(sleep_state))); |
85 | 85 | if (ret < 0) { |