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 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 40 | static int state_fd; |
| 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 | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 49 | static const 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"; |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 54 | |
| 55 | static 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 Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 61 | sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME); |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 62 | } |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 63 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 64 | static void* suspend_thread_func(void* arg __attribute__((unused))) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 65 | int ret; |
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 | |
| 68 | while (1) { |
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"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 87 | ret = sem_wait(&suspend_lockout); |
| 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 | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 115 | static int autosuspend_wakeup_count_enable(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 116 | int ret; |
| 117 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 118 | LOG(VERBOSE) << "autosuspend_wakeup_count_enable"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 119 | |
| 120 | ret = sem_post(&suspend_lockout); |
| 121 | |
| 122 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 123 | PLOG(ERROR) << "error changing semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 126 | LOG(VERBOSE) << "autosuspend_wakeup_count_enable done"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 131 | static int autosuspend_wakeup_count_disable(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 132 | int ret; |
| 133 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 134 | LOG(VERBOSE) << "autosuspend_wakeup_count_disable"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 135 | |
| 136 | ret = sem_wait(&suspend_lockout); |
| 137 | |
| 138 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 139 | PLOG(ERROR) << "error changing semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 142 | LOG(VERBOSE) << "autosuspend_wakeup_count_disable done"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 147 | static void autosuspend_set_wakeup_callback(void (*func)(bool success)) { |
Dianne Hackborn | 9552cdf | 2014-03-07 11:00:39 -0800 | [diff] [blame] | 148 | if (wakeup_func != NULL) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 149 | LOG(ERROR) << "duplicate wakeup callback applied, keeping original"; |
Dianne Hackborn | 9552cdf | 2014-03-07 11:00:39 -0800 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | wakeup_func = func; |
| 153 | } |
| 154 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 155 | struct autosuspend_ops autosuspend_wakeup_count_ops = { |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 156 | .enable = autosuspend_wakeup_count_enable, |
| 157 | .disable = autosuspend_wakeup_count_disable, |
| 158 | .set_wakeup_callback = autosuspend_set_wakeup_callback, |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 161 | struct autosuspend_ops* autosuspend_wakeup_count_init(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 162 | int ret; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 163 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 164 | state_fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_RDWR)); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 165 | if (state_fd < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 166 | PLOG(ERROR) << "error opening " << sys_power_state; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 167 | goto err_open_state; |
| 168 | } |
| 169 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 170 | wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_RDWR)); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 171 | if (wakeup_count_fd < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 172 | PLOG(ERROR) << "error opening " << sys_power_wakeup_count; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 173 | goto err_open_wakeup_count; |
| 174 | } |
| 175 | |
| 176 | ret = sem_init(&suspend_lockout, 0, 0); |
| 177 | if (ret < 0) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 178 | PLOG(ERROR) << "error creating semaphore"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 179 | goto err_sem_init; |
| 180 | } |
| 181 | ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL); |
| 182 | if (ret) { |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 183 | LOG(ERROR) << "error creating thread: " << strerror(ret); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 184 | goto err_pthread_create; |
| 185 | } |
| 186 | |
Steve Paik | d5dc807 | 2017-12-21 12:44:46 -0800 | [diff] [blame^] | 187 | LOG(INFO) << "selected wakeup count"; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 188 | return &autosuspend_wakeup_count_ops; |
| 189 | |
| 190 | err_pthread_create: |
| 191 | sem_destroy(&suspend_lockout); |
| 192 | err_sem_init: |
| 193 | close(wakeup_count_fd); |
| 194 | err_open_wakeup_count: |
| 195 | close(state_fd); |
| 196 | err_open_state: |
| 197 | return NULL; |
| 198 | } |