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 <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <pthread.h> |
| 23 | #include <semaphore.h> |
Ruchi Kandoi | d3027d8 | 2015-05-13 14:57:08 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
Steve Paik | db88774 | 2017-12-20 15:09:21 -0800 | [diff] [blame^] | 25 | #include <stddef.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 26 | #include <string.h> |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 27 | #include <sys/param.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 31 | |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 32 | #include <log/log.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 33 | |
| 34 | #include "autosuspend_ops.h" |
| 35 | |
| 36 | #define SYS_POWER_STATE "/sys/power/state" |
| 37 | #define SYS_POWER_WAKEUP_COUNT "/sys/power/wakeup_count" |
| 38 | |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 39 | #define BASE_SLEEP_TIME 100000 |
| 40 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 41 | static int state_fd; |
| 42 | static int wakeup_count_fd; |
| 43 | static pthread_t suspend_thread; |
| 44 | static sem_t suspend_lockout; |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 45 | static const char* sleep_state = "mem"; |
Ruchi Kandoi | d3027d8 | 2015-05-13 14:57:08 -0700 | [diff] [blame] | 46 | static void (*wakeup_func)(bool success) = NULL; |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 47 | static int sleep_time = BASE_SLEEP_TIME; |
| 48 | |
| 49 | static void update_sleep_time(bool success) { |
| 50 | if (success) { |
| 51 | sleep_time = BASE_SLEEP_TIME; |
| 52 | return; |
| 53 | } |
| 54 | // double sleep time after each failure up to one minute |
| 55 | sleep_time = MIN(sleep_time * 2, 60000000); |
| 56 | } |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 57 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 58 | static void* suspend_thread_func(void* arg __attribute__((unused))) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 59 | char buf[80]; |
| 60 | char wakeup_count[20]; |
| 61 | int wakeup_count_len; |
| 62 | int ret; |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 63 | bool success = true; |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 64 | |
| 65 | while (1) { |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 66 | update_sleep_time(success); |
| 67 | usleep(sleep_time); |
| 68 | success = false; |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 69 | ALOGV("%s: read wakeup_count", __func__); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 70 | lseek(wakeup_count_fd, 0, SEEK_SET); |
Steve Paik | db88774 | 2017-12-20 15:09:21 -0800 | [diff] [blame^] | 71 | wakeup_count_len = |
| 72 | TEMP_FAILURE_RETRY(read(wakeup_count_fd, wakeup_count, sizeof(wakeup_count))); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 73 | if (wakeup_count_len < 0) { |
| 74 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 75 | ALOGE("Error reading from %s: %s", SYS_POWER_WAKEUP_COUNT, buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 76 | wakeup_count_len = 0; |
| 77 | continue; |
| 78 | } |
| 79 | if (!wakeup_count_len) { |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 80 | ALOGE("Empty wakeup count"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 81 | continue; |
| 82 | } |
| 83 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 84 | ALOGV("%s: wait", __func__); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 85 | ret = sem_wait(&suspend_lockout); |
| 86 | if (ret < 0) { |
| 87 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 88 | ALOGE("Error waiting on semaphore: %s", buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 89 | continue; |
| 90 | } |
| 91 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 92 | ALOGV("%s: write %*s to wakeup_count", __func__, wakeup_count_len, wakeup_count); |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 93 | ret = TEMP_FAILURE_RETRY(write(wakeup_count_fd, wakeup_count, wakeup_count_len)); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 94 | if (ret < 0) { |
| 95 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 96 | ALOGE("Error writing to %s: %s", SYS_POWER_WAKEUP_COUNT, buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 97 | } else { |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 98 | ALOGV("%s: write %s to %s", __func__, sleep_state, SYS_POWER_STATE); |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 99 | ret = TEMP_FAILURE_RETRY(write(state_fd, sleep_state, strlen(sleep_state))); |
Ajay Dudani | 0c5f7d8 | 2017-04-17 16:54:15 -0700 | [diff] [blame] | 100 | if (ret >= 0) { |
| 101 | success = true; |
Ruchi Kandoi | d3027d8 | 2015-05-13 14:57:08 -0700 | [diff] [blame] | 102 | } |
| 103 | void (*func)(bool success) = wakeup_func; |
| 104 | if (func != NULL) { |
| 105 | (*func)(success); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 109 | ALOGV("%s: release sem", __func__); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 110 | ret = sem_post(&suspend_lockout); |
| 111 | if (ret < 0) { |
| 112 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 113 | ALOGE("Error releasing semaphore: %s", buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 119 | static int autosuspend_wakeup_count_enable(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 120 | char buf[80]; |
| 121 | int ret; |
| 122 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 123 | ALOGV("autosuspend_wakeup_count_enable"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 124 | |
| 125 | ret = sem_post(&suspend_lockout); |
| 126 | |
| 127 | if (ret < 0) { |
| 128 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 129 | ALOGE("Error changing semaphore: %s", buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 132 | ALOGV("autosuspend_wakeup_count_enable done"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 137 | static int autosuspend_wakeup_count_disable(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 138 | char buf[80]; |
| 139 | int ret; |
| 140 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 141 | ALOGV("autosuspend_wakeup_count_disable"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 142 | |
| 143 | ret = sem_wait(&suspend_lockout); |
| 144 | |
| 145 | if (ret < 0) { |
| 146 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 147 | ALOGE("Error changing semaphore: %s", buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 150 | ALOGV("autosuspend_wakeup_count_disable done"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 155 | static void autosuspend_set_wakeup_callback(void (*func)(bool success)) { |
Dianne Hackborn | 9552cdf | 2014-03-07 11:00:39 -0800 | [diff] [blame] | 156 | if (wakeup_func != NULL) { |
| 157 | ALOGE("Duplicate wakeup callback applied, keeping original"); |
| 158 | return; |
| 159 | } |
| 160 | wakeup_func = func; |
| 161 | } |
| 162 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 163 | struct autosuspend_ops autosuspend_wakeup_count_ops = { |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 164 | .enable = autosuspend_wakeup_count_enable, |
| 165 | .disable = autosuspend_wakeup_count_disable, |
| 166 | .set_wakeup_callback = autosuspend_set_wakeup_callback, |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 169 | struct autosuspend_ops* autosuspend_wakeup_count_init(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 170 | int ret; |
| 171 | char buf[80]; |
| 172 | |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 173 | state_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_STATE, O_RDWR)); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 174 | if (state_fd < 0) { |
| 175 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 176 | ALOGE("Error opening %s: %s", SYS_POWER_STATE, buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 177 | goto err_open_state; |
| 178 | } |
| 179 | |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 180 | 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] | 181 | if (wakeup_count_fd < 0) { |
| 182 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 183 | ALOGE("Error opening %s: %s", SYS_POWER_WAKEUP_COUNT, buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 184 | goto err_open_wakeup_count; |
| 185 | } |
| 186 | |
| 187 | ret = sem_init(&suspend_lockout, 0, 0); |
| 188 | if (ret < 0) { |
| 189 | strerror_r(errno, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 190 | ALOGE("Error creating semaphore: %s", buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 191 | goto err_sem_init; |
| 192 | } |
| 193 | ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL); |
| 194 | if (ret) { |
| 195 | strerror_r(ret, buf, sizeof(buf)); |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 196 | ALOGE("Error creating thread: %s", buf); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 197 | goto err_pthread_create; |
| 198 | } |
| 199 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 200 | ALOGI("Selected wakeup count"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 201 | return &autosuspend_wakeup_count_ops; |
| 202 | |
| 203 | err_pthread_create: |
| 204 | sem_destroy(&suspend_lockout); |
| 205 | err_sem_init: |
| 206 | close(wakeup_count_fd); |
| 207 | err_open_wakeup_count: |
| 208 | close(state_fd); |
| 209 | err_open_state: |
| 210 | return NULL; |
| 211 | } |