diff --git a/libsuspend/Android.bp b/libsuspend/Android.bp index fa06dc45f..b3e36c2bd 100644 --- a/libsuspend/Android.bp +++ b/libsuspend/Android.bp @@ -14,6 +14,7 @@ cc_library { export_include_dirs: ["include"], local_include_dirs: ["include"], shared_libs: [ + "libbase", "liblog", "libcutils", ], diff --git a/libsuspend/autosuspend_wakeup_count.cpp b/libsuspend/autosuspend_wakeup_count.cpp index 30f842738..cfca76521 100644 --- a/libsuspend/autosuspend_wakeup_count.cpp +++ b/libsuspend/autosuspend_wakeup_count.cpp @@ -17,7 +17,6 @@ #define LOG_TAG "libsuspend" //#define LOG_NDEBUG 0 -#include #include #include #include @@ -29,22 +28,29 @@ #include #include -#include +#include +#include +#include #include "autosuspend_ops.h" -#define SYS_POWER_STATE "/sys/power/state" -#define SYS_POWER_WAKEUP_COUNT "/sys/power/wakeup_count" - #define BASE_SLEEP_TIME 100000 +#define MAX_SLEEP_TIME 60000000 static int state_fd; static int wakeup_count_fd; + +using android::base::ReadFdToString; +using android::base::Trim; +using android::base::WriteStringToFd; + static pthread_t suspend_thread; static sem_t suspend_lockout; static const char* sleep_state = "mem"; static void (*wakeup_func)(bool success) = NULL; static int sleep_time = BASE_SLEEP_TIME; +static constexpr char sys_power_state[] = "/sys/power/state"; +static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count"; static void update_sleep_time(bool success) { if (success) { @@ -52,13 +58,10 @@ static void update_sleep_time(bool success) { return; } // double sleep time after each failure up to one minute - sleep_time = MIN(sleep_time * 2, 60000000); + sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME); } static void* suspend_thread_func(void* arg __attribute__((unused))) { - char buf[80]; - char wakeup_count[20]; - int wakeup_count_len; int ret; bool success = true; @@ -66,95 +69,84 @@ static void* suspend_thread_func(void* arg __attribute__((unused))) { update_sleep_time(success); usleep(sleep_time); success = false; - ALOGV("%s: read wakeup_count", __func__); + LOG(VERBOSE) << "read wakeup_count"; lseek(wakeup_count_fd, 0, SEEK_SET); - wakeup_count_len = - TEMP_FAILURE_RETRY(read(wakeup_count_fd, wakeup_count, sizeof(wakeup_count))); - if (wakeup_count_len < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error reading from %s: %s", SYS_POWER_WAKEUP_COUNT, buf); - wakeup_count_len = 0; - continue; - } - if (!wakeup_count_len) { - ALOGE("Empty wakeup count"); + std::string wakeup_count; + if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) { + PLOG(ERROR) << "error reading from " << sys_power_wakeup_count; continue; } - ALOGV("%s: wait", __func__); + wakeup_count = Trim(wakeup_count); + if (wakeup_count.empty()) { + LOG(ERROR) << "empty wakeup count"; + continue; + } + + LOG(VERBOSE) << "wait"; ret = sem_wait(&suspend_lockout); if (ret < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error waiting on semaphore: %s", buf); + PLOG(ERROR) << "error waiting on semaphore"; continue; } - ALOGV("%s: write %*s to wakeup_count", __func__, wakeup_count_len, wakeup_count); - ret = TEMP_FAILURE_RETRY(write(wakeup_count_fd, wakeup_count, wakeup_count_len)); - if (ret < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error writing to %s: %s", SYS_POWER_WAKEUP_COUNT, buf); - } else { - ALOGV("%s: write %s to %s", __func__, sleep_state, SYS_POWER_STATE); - ret = TEMP_FAILURE_RETRY(write(state_fd, sleep_state, strlen(sleep_state))); - if (ret >= 0) { - success = true; - } + 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); + void (*func)(bool success) = wakeup_func; if (func != NULL) { (*func)(success); } + } else { + PLOG(ERROR) << "error writing to " << sys_power_wakeup_count; } - ALOGV("%s: release sem", __func__); + LOG(VERBOSE) << "release sem"; ret = sem_post(&suspend_lockout); if (ret < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error releasing semaphore: %s", buf); + PLOG(ERROR) << "error releasing semaphore"; } } return NULL; } static int autosuspend_wakeup_count_enable(void) { - char buf[80]; int ret; - ALOGV("autosuspend_wakeup_count_enable"); + LOG(VERBOSE) << "autosuspend_wakeup_count_enable"; ret = sem_post(&suspend_lockout); if (ret < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error changing semaphore: %s", buf); + PLOG(ERROR) << "error changing semaphore"; } - ALOGV("autosuspend_wakeup_count_enable done"); + LOG(VERBOSE) << "autosuspend_wakeup_count_enable done"; return ret; } static int autosuspend_wakeup_count_disable(void) { - char buf[80]; int ret; - ALOGV("autosuspend_wakeup_count_disable"); + LOG(VERBOSE) << "autosuspend_wakeup_count_disable"; ret = sem_wait(&suspend_lockout); if (ret < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error changing semaphore: %s", buf); + PLOG(ERROR) << "error changing semaphore"; } - ALOGV("autosuspend_wakeup_count_disable done"); + LOG(VERBOSE) << "autosuspend_wakeup_count_disable done"; return ret; } static void autosuspend_set_wakeup_callback(void (*func)(bool success)) { if (wakeup_func != NULL) { - ALOGE("Duplicate wakeup callback applied, keeping original"); + LOG(ERROR) << "duplicate wakeup callback applied, keeping original"; return; } wakeup_func = func; @@ -168,36 +160,31 @@ struct autosuspend_ops autosuspend_wakeup_count_ops = { struct autosuspend_ops* autosuspend_wakeup_count_init(void) { int ret; - char buf[80]; - state_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_STATE, O_RDWR)); + state_fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_RDWR)); if (state_fd < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error opening %s: %s", SYS_POWER_STATE, buf); + PLOG(ERROR) << "error opening " << sys_power_state; goto err_open_state; } - wakeup_count_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_WAKEUP_COUNT, O_RDWR)); + wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_RDWR)); if (wakeup_count_fd < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error opening %s: %s", SYS_POWER_WAKEUP_COUNT, buf); + PLOG(ERROR) << "error opening " << sys_power_wakeup_count; goto err_open_wakeup_count; } ret = sem_init(&suspend_lockout, 0, 0); if (ret < 0) { - strerror_r(errno, buf, sizeof(buf)); - ALOGE("Error creating semaphore: %s", buf); + PLOG(ERROR) << "error creating semaphore"; goto err_sem_init; } ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL); if (ret) { - strerror_r(ret, buf, sizeof(buf)); - ALOGE("Error creating thread: %s", buf); + LOG(ERROR) << "error creating thread: " << strerror(ret); goto err_pthread_create; } - ALOGI("Selected wakeup count"); + LOG(INFO) << "selected wakeup count"; return &autosuspend_wakeup_count_ops; err_pthread_create: