Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "libsuspend" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <pthread.h> |
| 22 | #include <semaphore.h> |
Ruchi Kandoi | d3027d8 | 2015-05-13 14:57:08 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
Steve Paik | db88774 | 2017-12-20 15:09:21 -0800 | [diff] [blame] | 24 | #include <stddef.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 25 | #include <string.h> |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 26 | #include <sys/param.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/strings.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "autosuspend_ops.h" |
| 36 | |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 37 | #define BASE_SLEEP_TIME 100000 |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 38 | #define MAX_SLEEP_TIME 60000000 |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 39 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 40 | static int state_fd = -1; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 41 | static int wakeup_count_fd; |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 42 | |
| 43 | using android::base::ReadFdToString; |
| 44 | using android::base::Trim; |
| 45 | using android::base::WriteStringToFd; |
| 46 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 47 | static pthread_t suspend_thread; |
| 48 | static sem_t suspend_lockout; |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 49 | static constexpr char sleep_state[] = "mem"; |
Ruchi Kandoi | d3027d8 | 2015-05-13 14:57:08 -0700 | [diff] [blame] | 50 | static void (*wakeup_func)(bool success) = NULL; |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 51 | static int sleep_time = BASE_SLEEP_TIME; |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 52 | static constexpr char sys_power_state[] = "/sys/power/state"; |
| 53 | static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count"; |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 54 | static bool autosuspend_is_init = false; |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 55 | |
| 56 | static void update_sleep_time(bool success) { |
| 57 | if (success) { |
| 58 | sleep_time = BASE_SLEEP_TIME; |
| 59 | return; |
| 60 | } |
| 61 | // double sleep time after each failure up to one minute |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 62 | sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME); |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 63 | } |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 64 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 65 | static void* suspend_thread_func(void* arg __attribute__((unused))) { |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 66 | bool success = true; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 67 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 68 | while (true) { |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 69 | update_sleep_time(success); |
| 70 | usleep(sleep_time); |
| 71 | success = false; |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 72 | LOG(VERBOSE) << "read wakeup_count"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 73 | lseek(wakeup_count_fd, 0, SEEK_SET); |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 74 | std::string wakeup_count; |
| 75 | if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) { |
| 76 | PLOG(ERROR) << "error reading from " << sys_power_wakeup_count; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 77 | continue; |
| 78 | } |
| 79 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 80 | wakeup_count = Trim(wakeup_count); |
| 81 | if (wakeup_count.empty()) { |
| 82 | LOG(ERROR) << "empty wakeup count"; |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | LOG(VERBOSE) << "wait"; |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 87 | int ret = sem_wait(&suspend_lockout); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 88 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 89 | PLOG(ERROR) << "error waiting on semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 90 | continue; |
| 91 | } |
| 92 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 93 | 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 Kandoi | d3027d8 | 2015-05-13 14:57:08 -0700 | [diff] [blame] | 98 | void (*func)(bool success) = wakeup_func; |
| 99 | if (func != NULL) { |
| 100 | (*func)(success); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 101 | } |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 102 | } else { |
| 103 | PLOG(ERROR) << "error writing to " << sys_power_wakeup_count; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 106 | LOG(VERBOSE) << "release sem"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 107 | ret = sem_post(&suspend_lockout); |
| 108 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 109 | PLOG(ERROR) << "error releasing semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 115 | static int init_state_fd(void) { |
| 116 | if (state_fd >= 0) { |
| 117 | return 0; |
| 118 | } |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 119 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 120 | int fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_CLOEXEC | O_RDWR)); |
| 121 | if (fd < 0) { |
| 122 | PLOG(ERROR) << "error opening " << sys_power_state; |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | state_fd = fd; |
| 127 | LOG(INFO) << "init_state_fd success"; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int autosuspend_init(void) { |
| 132 | if (autosuspend_is_init) { |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int ret = init_state_fd(); |
| 137 | if (ret < 0) { |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_CLOEXEC | O_RDWR)); |
| 142 | if (wakeup_count_fd < 0) { |
| 143 | PLOG(ERROR) << "error opening " << sys_power_wakeup_count; |
| 144 | goto err_open_wakeup_count; |
| 145 | } |
| 146 | |
| 147 | ret = sem_init(&suspend_lockout, 0, 0); |
| 148 | if (ret < 0) { |
| 149 | PLOG(ERROR) << "error creating suspend_lockout semaphore"; |
| 150 | goto err_sem_init; |
| 151 | } |
| 152 | |
| 153 | ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL); |
| 154 | if (ret) { |
| 155 | LOG(ERROR) << "error creating thread: " << strerror(ret); |
| 156 | goto err_pthread_create; |
| 157 | } |
| 158 | |
| 159 | LOG(VERBOSE) << "autosuspend_init success"; |
| 160 | autosuspend_is_init = true; |
| 161 | return 0; |
| 162 | |
| 163 | err_pthread_create: |
| 164 | sem_destroy(&suspend_lockout); |
| 165 | err_sem_init: |
| 166 | close(wakeup_count_fd); |
| 167 | err_open_wakeup_count: |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | static int autosuspend_wakeup_count_enable(void) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 172 | LOG(VERBOSE) << "autosuspend_wakeup_count_enable"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 173 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 174 | int ret = autosuspend_init(); |
| 175 | if (ret < 0) { |
| 176 | LOG(ERROR) << "autosuspend_init failed"; |
| 177 | return ret; |
| 178 | } |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 179 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 180 | ret = sem_post(&suspend_lockout); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 181 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 182 | PLOG(ERROR) << "error changing semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 185 | LOG(VERBOSE) << "autosuspend_wakeup_count_enable done"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 190 | static int autosuspend_wakeup_count_disable(void) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 191 | LOG(VERBOSE) << "autosuspend_wakeup_count_disable"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 192 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 193 | if (!autosuspend_is_init) { |
| 194 | return 0; // always successful if no thread is running yet |
| 195 | } |
| 196 | |
| 197 | int ret = sem_wait(&suspend_lockout); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 198 | |
| 199 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 200 | PLOG(ERROR) << "error changing semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 203 | LOG(VERBOSE) << "autosuspend_wakeup_count_disable done"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 208 | static int force_suspend(int timeout_ms) { |
| 209 | LOG(VERBOSE) << "force_suspend called with timeout: " << timeout_ms; |
| 210 | |
| 211 | int ret = init_state_fd(); |
| 212 | if (ret < 0) { |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | return WriteStringToFd(sleep_state, state_fd) ? 0 : -1; |
| 217 | } |
| 218 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 219 | static void autosuspend_set_wakeup_callback(void (*func)(bool success)) { |
Dianne Hackborn | 9552cdf | 2014-03-07 11:00:39 -0800 | [diff] [blame] | 220 | if (wakeup_func != NULL) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame] | 221 | LOG(ERROR) << "duplicate wakeup callback applied, keeping original"; |
Dianne Hackborn | 9552cdf | 2014-03-07 11:00:39 -0800 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | wakeup_func = func; |
| 225 | } |
| 226 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 227 | struct autosuspend_ops autosuspend_wakeup_count_ops = { |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 228 | .enable = autosuspend_wakeup_count_enable, |
| 229 | .disable = autosuspend_wakeup_count_disable, |
Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 230 | .force_suspend = force_suspend, |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 231 | .set_wakeup_callback = autosuspend_set_wakeup_callback, |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 234 | struct autosuspend_ops* autosuspend_wakeup_count_init(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 235 | return &autosuspend_wakeup_count_ops; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 236 | } |