blob: cfca76521ce2da3b263084b034b5091b0ed5af70 [file] [log] [blame]
Colin Crossa2582c22012-05-03 17:30:16 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070017#define LOG_TAG "libsuspend"
18//#define LOG_NDEBUG 0
19
Colin Crossa2582c22012-05-03 17:30:16 -070020#include <fcntl.h>
21#include <pthread.h>
22#include <semaphore.h>
Ruchi Kandoid3027d82015-05-13 14:57:08 -070023#include <stdbool.h>
Steve Paikdb887742017-12-20 15:09:21 -080024#include <stddef.h>
Colin Crossa2582c22012-05-03 17:30:16 -070025#include <string.h>
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070026#include <sys/param.h>
Colin Crossa2582c22012-05-03 17:30:16 -070027#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30
Steve Paikd5dc8072017-12-21 12:44:46 -080031#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/strings.h>
Colin Crossa2582c22012-05-03 17:30:16 -070034
35#include "autosuspend_ops.h"
36
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070037#define BASE_SLEEP_TIME 100000
Steve Paikd5dc8072017-12-21 12:44:46 -080038#define MAX_SLEEP_TIME 60000000
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070039
Colin Crossa2582c22012-05-03 17:30:16 -070040static int state_fd;
41static int wakeup_count_fd;
Steve Paikd5dc8072017-12-21 12:44:46 -080042
43using android::base::ReadFdToString;
44using android::base::Trim;
45using android::base::WriteStringToFd;
46
Colin Crossa2582c22012-05-03 17:30:16 -070047static pthread_t suspend_thread;
48static sem_t suspend_lockout;
Steve Paik2d441902017-12-15 13:26:28 -080049static const char* sleep_state = "mem";
Ruchi Kandoid3027d82015-05-13 14:57:08 -070050static void (*wakeup_func)(bool success) = NULL;
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070051static int sleep_time = BASE_SLEEP_TIME;
Steve Paikd5dc8072017-12-21 12:44:46 -080052static constexpr char sys_power_state[] = "/sys/power/state";
53static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count";
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070054
55static void update_sleep_time(bool success) {
56 if (success) {
57 sleep_time = BASE_SLEEP_TIME;
58 return;
59 }
60 // double sleep time after each failure up to one minute
Steve Paikd5dc8072017-12-21 12:44:46 -080061 sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME);
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070062}
Colin Crossa2582c22012-05-03 17:30:16 -070063
Steve Paik2d441902017-12-15 13:26:28 -080064static void* suspend_thread_func(void* arg __attribute__((unused))) {
Colin Crossa2582c22012-05-03 17:30:16 -070065 int ret;
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070066 bool success = true;
Colin Crossa2582c22012-05-03 17:30:16 -070067
68 while (1) {
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070069 update_sleep_time(success);
70 usleep(sleep_time);
71 success = false;
Steve Paikd5dc8072017-12-21 12:44:46 -080072 LOG(VERBOSE) << "read wakeup_count";
Colin Crossa2582c22012-05-03 17:30:16 -070073 lseek(wakeup_count_fd, 0, SEEK_SET);
Steve Paikd5dc8072017-12-21 12:44:46 -080074 std::string wakeup_count;
75 if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) {
76 PLOG(ERROR) << "error reading from " << sys_power_wakeup_count;
Colin Crossa2582c22012-05-03 17:30:16 -070077 continue;
78 }
79
Steve Paikd5dc8072017-12-21 12:44:46 -080080 wakeup_count = Trim(wakeup_count);
81 if (wakeup_count.empty()) {
82 LOG(ERROR) << "empty wakeup count";
83 continue;
84 }
85
86 LOG(VERBOSE) << "wait";
Colin Crossa2582c22012-05-03 17:30:16 -070087 ret = sem_wait(&suspend_lockout);
88 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -080089 PLOG(ERROR) << "error waiting on semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -070090 continue;
91 }
92
Steve Paikd5dc8072017-12-21 12:44:46 -080093 LOG(VERBOSE) << "write " << wakeup_count << " to wakeup_count";
94 if (WriteStringToFd(wakeup_count, wakeup_count_fd)) {
95 LOG(VERBOSE) << "write " << sleep_state << " to " << sys_power_state;
96 success = WriteStringToFd(sleep_state, state_fd);
97
Ruchi Kandoid3027d82015-05-13 14:57:08 -070098 void (*func)(bool success) = wakeup_func;
99 if (func != NULL) {
100 (*func)(success);
Colin Crossa2582c22012-05-03 17:30:16 -0700101 }
Steve Paikd5dc8072017-12-21 12:44:46 -0800102 } else {
103 PLOG(ERROR) << "error writing to " << sys_power_wakeup_count;
Colin Crossa2582c22012-05-03 17:30:16 -0700104 }
105
Steve Paikd5dc8072017-12-21 12:44:46 -0800106 LOG(VERBOSE) << "release sem";
Colin Crossa2582c22012-05-03 17:30:16 -0700107 ret = sem_post(&suspend_lockout);
108 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800109 PLOG(ERROR) << "error releasing semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700110 }
111 }
112 return NULL;
113}
114
Steve Paik2d441902017-12-15 13:26:28 -0800115static int autosuspend_wakeup_count_enable(void) {
Colin Crossa2582c22012-05-03 17:30:16 -0700116 int ret;
117
Steve Paikd5dc8072017-12-21 12:44:46 -0800118 LOG(VERBOSE) << "autosuspend_wakeup_count_enable";
Colin Crossa2582c22012-05-03 17:30:16 -0700119
120 ret = sem_post(&suspend_lockout);
121
122 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800123 PLOG(ERROR) << "error changing semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700124 }
125
Steve Paikd5dc8072017-12-21 12:44:46 -0800126 LOG(VERBOSE) << "autosuspend_wakeup_count_enable done";
Colin Crossa2582c22012-05-03 17:30:16 -0700127
128 return ret;
129}
130
Steve Paik2d441902017-12-15 13:26:28 -0800131static int autosuspend_wakeup_count_disable(void) {
Colin Crossa2582c22012-05-03 17:30:16 -0700132 int ret;
133
Steve Paikd5dc8072017-12-21 12:44:46 -0800134 LOG(VERBOSE) << "autosuspend_wakeup_count_disable";
Colin Crossa2582c22012-05-03 17:30:16 -0700135
136 ret = sem_wait(&suspend_lockout);
137
138 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800139 PLOG(ERROR) << "error changing semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700140 }
141
Steve Paikd5dc8072017-12-21 12:44:46 -0800142 LOG(VERBOSE) << "autosuspend_wakeup_count_disable done";
Colin Crossa2582c22012-05-03 17:30:16 -0700143
144 return ret;
145}
146
Steve Paik2d441902017-12-15 13:26:28 -0800147static void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
Dianne Hackborn9552cdf2014-03-07 11:00:39 -0800148 if (wakeup_func != NULL) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800149 LOG(ERROR) << "duplicate wakeup callback applied, keeping original";
Dianne Hackborn9552cdf2014-03-07 11:00:39 -0800150 return;
151 }
152 wakeup_func = func;
153}
154
Colin Crossa2582c22012-05-03 17:30:16 -0700155struct autosuspend_ops autosuspend_wakeup_count_ops = {
Steve Paik2d441902017-12-15 13:26:28 -0800156 .enable = autosuspend_wakeup_count_enable,
157 .disable = autosuspend_wakeup_count_disable,
158 .set_wakeup_callback = autosuspend_set_wakeup_callback,
Colin Crossa2582c22012-05-03 17:30:16 -0700159};
160
Steve Paik2d441902017-12-15 13:26:28 -0800161struct autosuspend_ops* autosuspend_wakeup_count_init(void) {
Colin Crossa2582c22012-05-03 17:30:16 -0700162 int ret;
Colin Crossa2582c22012-05-03 17:30:16 -0700163
Steve Paikd5dc8072017-12-21 12:44:46 -0800164 state_fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_RDWR));
Colin Crossa2582c22012-05-03 17:30:16 -0700165 if (state_fd < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800166 PLOG(ERROR) << "error opening " << sys_power_state;
Colin Crossa2582c22012-05-03 17:30:16 -0700167 goto err_open_state;
168 }
169
Steve Paikd5dc8072017-12-21 12:44:46 -0800170 wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_RDWR));
Colin Crossa2582c22012-05-03 17:30:16 -0700171 if (wakeup_count_fd < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800172 PLOG(ERROR) << "error opening " << sys_power_wakeup_count;
Colin Crossa2582c22012-05-03 17:30:16 -0700173 goto err_open_wakeup_count;
174 }
175
176 ret = sem_init(&suspend_lockout, 0, 0);
177 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800178 PLOG(ERROR) << "error creating semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700179 goto err_sem_init;
180 }
181 ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
182 if (ret) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800183 LOG(ERROR) << "error creating thread: " << strerror(ret);
Colin Crossa2582c22012-05-03 17:30:16 -0700184 goto err_pthread_create;
185 }
186
Steve Paikd5dc8072017-12-21 12:44:46 -0800187 LOG(INFO) << "selected wakeup count";
Colin Crossa2582c22012-05-03 17:30:16 -0700188 return &autosuspend_wakeup_count_ops;
189
190err_pthread_create:
191 sem_destroy(&suspend_lockout);
192err_sem_init:
193 close(wakeup_count_fd);
194err_open_wakeup_count:
195 close(state_fd);
196err_open_state:
197 return NULL;
198}