Linux 3.10
Android 4.4
http://blog.csdn.net/u013686019/article/details/53691897
system/core/libsuspend
autosuspend_ops.h (system\core\libsuspend)
struct autosuspend_ops {
int (*enable)(void);
int (*disable)(void);
};
autosuspend.c (system\core\libsuspend)
static int autosuspend_init(void)
{
if (autosuspend_inited) {
return 0;
}
autosuspend_ops = autosuspend_earlysuspend_init();
if (autosuspend_ops) {
goto out;
}
autosuspend_ops = autosuspend_autosleep_init();
if (autosuspend_ops) {
goto out;
}
autosuspend_ops = autosuspend_wakeup_count_init();
if (autosuspend_ops) {
goto out;
}
if (!autosuspend_ops) {
ALOGE("failed to initialize autosuspend\n");
return -1;
}
out:
autosuspend_inited = true;
ALOGV("autosuspend initialized\n");
return 0;
}
autosuspend_ops = autosuspend_earlysuspend_init();
autosuspend_ops = autosuspend_autosleep_init();
autosuspend_ops = autosuspend_wakeup_count_init();
autosuspend_earlysuspend.c (system\core\libsuspend)
struct autosuspend_ops *autosuspend_earlysuspend_init(void)
{
// #define EARLYSUSPEND_SYS_POWER_STATE "/sys/power/state"
sPowerStatefd = open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR);
// 向/sys/power/state文件写入"on"
ret = write(sPowerStatefd, "on", 2);
// 写入"on"的时候失败,所以跳转到err_write并退出被函数
if (ret < 0) {
ALOGW("Error writing 'on' to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
goto err_write;
}
ALOGI("Selected early suspend\n");
start_earlysuspend_thread();
return &autosuspend_earlysuspend_ops;
err_write:
close(sPowerStatefd);
return NULL;
}
autosuspend_autosleep.c (system\core\libsuspend)
struct autosuspend_ops *autosuspend_autosleep_init(void)
{
int ret;
char buf[80];
// #define SYS_POWER_AUTOSLEEP "/sys/power/autosleep"
autosleep_fd = open(SYS_POWER_AUTOSLEEP, O_WRONLY);
if (autosleep_fd < 0) {
return NULL;
}
ALOGI("Selected autosleep\n");
autosuspend_autosleep_disable();
return &autosuspend_autosleep_ops;
}
autosuspend_autosleep.c (system\core\libsuspend)
struct autosuspend_ops autosuspend_autosleep_ops = {
.enable = autosuspend_autosleep_enable,
.disable = autosuspend_autosleep_disable,
};
static int autosuspend_autosleep_enable(void)
{
// static const char *sleep_state = "mem";
ret = write(autosleep_fd, sleep_state, strlen(sleep_state));
return 0;
}
static int autosuspend_autosleep_disable(void)
{
// static const char *on_state = "off";
ret = write(autosleep_fd, on_state, strlen(on_state));
return 0;
}
autosuspend.c (system\core\libsuspend)
int autosuspend_enable(void)
{
ret = autosuspend_init();
if (autosuspend_enabled) {
return 0;
}
ret = autosuspend_ops->enable();
autosuspend_enabled = true;
return 0;
}
int autosuspend_disable(void)
{
ret = autosuspend_init();
if (!autosuspend_enabled) {
return 0;
}
ret = autosuspend_ops->disable();
autosuspend_enabled = false;
return 0;
}
com_android_server_power_PowerManagerService.cpp (frameworks\base\services\jni)
static void nativeSetAutoSuspend(JNIEnv *env, jclass clazz, jboolean enable) {
if (enable) {
ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_enable() while turning screen off");
autosuspend_enable();
} else {
ALOGD_IF_SLOW(100, "Excessive delay in autosuspend_disable() while turning screen on");
autosuspend_disable();
}
}