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 
17 #define LOG_TAG "libsuspend"
18 //#define LOG_NDEBUG 0
19 
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <semaphore.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <sys/param.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
33 #include <android-base/strings.h>
34 
35 #include "autosuspend_ops.h"
36 
37 #define BASE_SLEEP_TIME 100000
38 #define MAX_SLEEP_TIME 60000000
39 
40 static int state_fd = -1;
41 static int wakeup_count_fd;
42 
43 using android::base::ReadFdToString;
44 using android::base::Trim;
45 using android::base::WriteStringToFd;
46 
47 static pthread_t suspend_thread;
48 static sem_t suspend_lockout;
49 static constexpr char sleep_state[] = "mem";
50 static void (*wakeup_func)(bool success) = NULL;
51 static int sleep_time = BASE_SLEEP_TIME;
52 static constexpr char sys_power_state[] = "/sys/power/state";
53 static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count";
54 static bool autosuspend_is_init = false;
55 
update_sleep_time(bool success)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
62     sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME);
63 }
64 
suspend_thread_func(void * arg)65 static void* suspend_thread_func(void* arg __attribute__((unused))) {
66     bool success = true;
67 
68     while (true) {
69         update_sleep_time(success);
70         usleep(sleep_time);
71         success = false;
72         LOG(VERBOSE) << "read wakeup_count";
73         lseek(wakeup_count_fd, 0, SEEK_SET);
74         std::string wakeup_count;
75         if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) {
76             PLOG(ERROR) << "error reading from " << sys_power_wakeup_count;
77             continue;
78         }
79 
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";
87         int ret = sem_wait(&suspend_lockout);
88         if (ret < 0) {
89             PLOG(ERROR) << "error waiting on semaphore";
90             continue;
91         }
92 
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 
98             void (*func)(bool success) = wakeup_func;
99             if (func != NULL) {
100                 (*func)(success);
101             }
102         } else {
103             PLOG(ERROR) << "error writing to " << sys_power_wakeup_count;
104         }
105 
106         LOG(VERBOSE) << "release sem";
107         ret = sem_post(&suspend_lockout);
108         if (ret < 0) {
109             PLOG(ERROR) << "error releasing semaphore";
110         }
111     }
112     return NULL;
113 }
114 
init_state_fd(void)115 static int init_state_fd(void) {
116     if (state_fd >= 0) {
117         return 0;
118     }
119 
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 
autosuspend_init(void)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 
autosuspend_wakeup_count_enable(void)171 static int autosuspend_wakeup_count_enable(void) {
172     LOG(VERBOSE) << "autosuspend_wakeup_count_enable";
173 
174     int ret = autosuspend_init();
175     if (ret < 0) {
176         LOG(ERROR) << "autosuspend_init failed";
177         return ret;
178     }
179 
180     ret = sem_post(&suspend_lockout);
181     if (ret < 0) {
182         PLOG(ERROR) << "error changing semaphore";
183     }
184 
185     LOG(VERBOSE) << "autosuspend_wakeup_count_enable done";
186 
187     return ret;
188 }
189 
autosuspend_wakeup_count_disable(void)190 static int autosuspend_wakeup_count_disable(void) {
191     LOG(VERBOSE) << "autosuspend_wakeup_count_disable";
192 
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);
198 
199     if (ret < 0) {
200         PLOG(ERROR) << "error changing semaphore";
201     }
202 
203     LOG(VERBOSE) << "autosuspend_wakeup_count_disable done";
204 
205     return ret;
206 }
207 
force_suspend(int timeout_ms)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 
autosuspend_set_wakeup_callback(void (* func)(bool success))219 static void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
220     if (wakeup_func != NULL) {
221         LOG(ERROR) << "duplicate wakeup callback applied, keeping original";
222         return;
223     }
224     wakeup_func = func;
225 }
226 
227 struct autosuspend_ops autosuspend_wakeup_count_ops = {
228     .enable = autosuspend_wakeup_count_enable,
229     .disable = autosuspend_wakeup_count_disable,
230     .force_suspend = force_suspend,
231     .set_wakeup_callback = autosuspend_set_wakeup_callback,
232 };
233 
autosuspend_wakeup_count_init(void)234 struct autosuspend_ops* autosuspend_wakeup_count_init(void) {
235     return &autosuspend_wakeup_count_ops;
236 }
237