2012-05-04 00:30:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-09-28 17:07:20 +00:00
|
|
|
#define LOG_TAG "libsuspend"
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
2012-05-04 00:30:16 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
2015-05-13 21:57:08 +00:00
|
|
|
#include <stdbool.h>
|
2017-12-20 23:09:21 +00:00
|
|
|
#include <stddef.h>
|
2012-05-04 00:30:16 +00:00
|
|
|
#include <string.h>
|
2017-04-17 23:54:15 +00:00
|
|
|
#include <sys/param.h>
|
2012-05-04 00:30:16 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-12-21 20:44:46 +00:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/strings.h>
|
2012-05-04 00:30:16 +00:00
|
|
|
|
|
|
|
#include "autosuspend_ops.h"
|
|
|
|
|
2017-04-17 23:54:15 +00:00
|
|
|
#define BASE_SLEEP_TIME 100000
|
2017-12-21 20:44:46 +00:00
|
|
|
#define MAX_SLEEP_TIME 60000000
|
2017-04-17 23:54:15 +00:00
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
static int state_fd = -1;
|
2012-05-04 00:30:16 +00:00
|
|
|
static int wakeup_count_fd;
|
2017-12-21 20:44:46 +00:00
|
|
|
|
|
|
|
using android::base::ReadFdToString;
|
|
|
|
using android::base::Trim;
|
|
|
|
using android::base::WriteStringToFd;
|
|
|
|
|
2012-05-04 00:30:16 +00:00
|
|
|
static pthread_t suspend_thread;
|
|
|
|
static sem_t suspend_lockout;
|
2018-01-04 04:27:58 +00:00
|
|
|
static constexpr char sleep_state[] = "mem";
|
2015-05-13 21:57:08 +00:00
|
|
|
static void (*wakeup_func)(bool success) = NULL;
|
2017-04-17 23:54:15 +00:00
|
|
|
static int sleep_time = BASE_SLEEP_TIME;
|
2017-12-21 20:44:46 +00:00
|
|
|
static constexpr char sys_power_state[] = "/sys/power/state";
|
|
|
|
static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count";
|
2018-01-04 04:27:58 +00:00
|
|
|
static bool autosuspend_is_init = false;
|
2017-04-17 23:54:15 +00:00
|
|
|
|
|
|
|
static void update_sleep_time(bool success) {
|
|
|
|
if (success) {
|
|
|
|
sleep_time = BASE_SLEEP_TIME;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// double sleep time after each failure up to one minute
|
2017-12-21 20:44:46 +00:00
|
|
|
sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME);
|
2017-04-17 23:54:15 +00:00
|
|
|
}
|
2012-05-04 00:30:16 +00:00
|
|
|
|
2017-12-15 21:26:28 +00:00
|
|
|
static void* suspend_thread_func(void* arg __attribute__((unused))) {
|
2017-04-17 23:54:15 +00:00
|
|
|
bool success = true;
|
2012-05-04 00:30:16 +00:00
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
while (true) {
|
2017-04-17 23:54:15 +00:00
|
|
|
update_sleep_time(success);
|
|
|
|
usleep(sleep_time);
|
|
|
|
success = false;
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "read wakeup_count";
|
2012-05-04 00:30:16 +00:00
|
|
|
lseek(wakeup_count_fd, 0, SEEK_SET);
|
2017-12-21 20:44:46 +00:00
|
|
|
std::string wakeup_count;
|
|
|
|
if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) {
|
|
|
|
PLOG(ERROR) << "error reading from " << sys_power_wakeup_count;
|
2012-05-04 00:30:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-21 20:44:46 +00:00
|
|
|
|
|
|
|
wakeup_count = Trim(wakeup_count);
|
|
|
|
if (wakeup_count.empty()) {
|
|
|
|
LOG(ERROR) << "empty wakeup count";
|
2012-05-04 00:30:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "wait";
|
2018-01-04 04:27:58 +00:00
|
|
|
int ret = sem_wait(&suspend_lockout);
|
2012-05-04 00:30:16 +00:00
|
|
|
if (ret < 0) {
|
2017-12-21 20:44:46 +00:00
|
|
|
PLOG(ERROR) << "error waiting on semaphore";
|
2012-05-04 00:30:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "write " << wakeup_count << " to wakeup_count";
|
|
|
|
if (WriteStringToFd(wakeup_count, wakeup_count_fd)) {
|
|
|
|
LOG(VERBOSE) << "write " << sleep_state << " to " << sys_power_state;
|
|
|
|
success = WriteStringToFd(sleep_state, state_fd);
|
|
|
|
|
2015-05-13 21:57:08 +00:00
|
|
|
void (*func)(bool success) = wakeup_func;
|
|
|
|
if (func != NULL) {
|
|
|
|
(*func)(success);
|
2012-05-04 00:30:16 +00:00
|
|
|
}
|
2017-12-21 20:44:46 +00:00
|
|
|
} else {
|
|
|
|
PLOG(ERROR) << "error writing to " << sys_power_wakeup_count;
|
2012-05-04 00:30:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "release sem";
|
2012-05-04 00:30:16 +00:00
|
|
|
ret = sem_post(&suspend_lockout);
|
|
|
|
if (ret < 0) {
|
2017-12-21 20:44:46 +00:00
|
|
|
PLOG(ERROR) << "error releasing semaphore";
|
2012-05-04 00:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
static int init_state_fd(void) {
|
|
|
|
if (state_fd >= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_CLOEXEC | O_RDWR));
|
|
|
|
if (fd < 0) {
|
|
|
|
PLOG(ERROR) << "error opening " << sys_power_state;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_fd = fd;
|
|
|
|
LOG(INFO) << "init_state_fd success";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int autosuspend_init(void) {
|
|
|
|
if (autosuspend_is_init) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = init_state_fd();
|
|
|
|
if (ret < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_CLOEXEC | O_RDWR));
|
|
|
|
if (wakeup_count_fd < 0) {
|
|
|
|
PLOG(ERROR) << "error opening " << sys_power_wakeup_count;
|
|
|
|
goto err_open_wakeup_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sem_init(&suspend_lockout, 0, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
PLOG(ERROR) << "error creating suspend_lockout semaphore";
|
|
|
|
goto err_sem_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
|
|
|
|
if (ret) {
|
|
|
|
LOG(ERROR) << "error creating thread: " << strerror(ret);
|
|
|
|
goto err_pthread_create;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(VERBOSE) << "autosuspend_init success";
|
|
|
|
autosuspend_is_init = true;
|
|
|
|
return 0;
|
2012-05-04 00:30:16 +00:00
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
err_pthread_create:
|
|
|
|
sem_destroy(&suspend_lockout);
|
|
|
|
err_sem_init:
|
|
|
|
close(wakeup_count_fd);
|
|
|
|
err_open_wakeup_count:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int autosuspend_wakeup_count_enable(void) {
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "autosuspend_wakeup_count_enable";
|
2012-05-04 00:30:16 +00:00
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
int ret = autosuspend_init();
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG(ERROR) << "autosuspend_init failed";
|
|
|
|
return ret;
|
|
|
|
}
|
2012-05-04 00:30:16 +00:00
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
ret = sem_post(&suspend_lockout);
|
2012-05-04 00:30:16 +00:00
|
|
|
if (ret < 0) {
|
2017-12-21 20:44:46 +00:00
|
|
|
PLOG(ERROR) << "error changing semaphore";
|
2012-05-04 00:30:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "autosuspend_wakeup_count_enable done";
|
2012-05-04 00:30:16 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-15 21:26:28 +00:00
|
|
|
static int autosuspend_wakeup_count_disable(void) {
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "autosuspend_wakeup_count_disable";
|
2012-05-04 00:30:16 +00:00
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
if (!autosuspend_is_init) {
|
|
|
|
return 0; // always successful if no thread is running yet
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = sem_wait(&suspend_lockout);
|
2012-05-04 00:30:16 +00:00
|
|
|
|
|
|
|
if (ret < 0) {
|
2017-12-21 20:44:46 +00:00
|
|
|
PLOG(ERROR) << "error changing semaphore";
|
2012-05-04 00:30:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(VERBOSE) << "autosuspend_wakeup_count_disable done";
|
2012-05-04 00:30:16 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:27:58 +00:00
|
|
|
static int force_suspend(int timeout_ms) {
|
|
|
|
LOG(VERBOSE) << "force_suspend called with timeout: " << timeout_ms;
|
|
|
|
|
|
|
|
int ret = init_state_fd();
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WriteStringToFd(sleep_state, state_fd) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2017-12-15 21:26:28 +00:00
|
|
|
static void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
|
2014-03-07 19:00:39 +00:00
|
|
|
if (wakeup_func != NULL) {
|
2017-12-21 20:44:46 +00:00
|
|
|
LOG(ERROR) << "duplicate wakeup callback applied, keeping original";
|
2014-03-07 19:00:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
wakeup_func = func;
|
|
|
|
}
|
|
|
|
|
2012-05-04 00:30:16 +00:00
|
|
|
struct autosuspend_ops autosuspend_wakeup_count_ops = {
|
2017-12-15 21:26:28 +00:00
|
|
|
.enable = autosuspend_wakeup_count_enable,
|
|
|
|
.disable = autosuspend_wakeup_count_disable,
|
2018-01-04 04:27:58 +00:00
|
|
|
.force_suspend = force_suspend,
|
2017-12-15 21:26:28 +00:00
|
|
|
.set_wakeup_callback = autosuspend_set_wakeup_callback,
|
2012-05-04 00:30:16 +00:00
|
|
|
};
|
|
|
|
|
2017-12-15 21:26:28 +00:00
|
|
|
struct autosuspend_ops* autosuspend_wakeup_count_init(void) {
|
2012-05-04 00:30:16 +00:00
|
|
|
return &autosuspend_wakeup_count_ops;
|
|
|
|
}
|