blob: d3fb45fcfda57f88b0b3ffd2a9bc64cead7cbc9e [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 <errno.h>
21#include <fcntl.h>
22#include <pthread.h>
23#include <semaphore.h>
24#include <stddef.h>
Ruchi Kandoid3027d82015-05-13 14:57:08 -070025#include <stdbool.h>
Colin Crossa2582c22012-05-03 17:30:16 -070026#include <string.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070031#include <android/log.h>
Colin Crossa2582c22012-05-03 17:30:16 -070032
33#include "autosuspend_ops.h"
34
35#define SYS_POWER_STATE "/sys/power/state"
36#define SYS_POWER_WAKEUP_COUNT "/sys/power/wakeup_count"
37
38static int state_fd;
39static int wakeup_count_fd;
40static pthread_t suspend_thread;
41static sem_t suspend_lockout;
42static const char *sleep_state = "mem";
Ruchi Kandoid3027d82015-05-13 14:57:08 -070043static void (*wakeup_func)(bool success) = NULL;
Colin Crossa2582c22012-05-03 17:30:16 -070044
Edwin Vane62f9ffa2012-07-26 15:26:46 -040045static void *suspend_thread_func(void *arg __attribute__((unused)))
Colin Crossa2582c22012-05-03 17:30:16 -070046{
47 char buf[80];
48 char wakeup_count[20];
49 int wakeup_count_len;
50 int ret;
Ruchi Kandoid3027d82015-05-13 14:57:08 -070051 bool success;
Colin Crossa2582c22012-05-03 17:30:16 -070052
53 while (1) {
54 usleep(100000);
55 ALOGV("%s: read wakeup_count\n", __func__);
56 lseek(wakeup_count_fd, 0, SEEK_SET);
Jeff Brown0446e162015-05-15 14:56:03 -070057 wakeup_count_len = TEMP_FAILURE_RETRY(read(wakeup_count_fd, wakeup_count,
58 sizeof(wakeup_count)));
Colin Crossa2582c22012-05-03 17:30:16 -070059 if (wakeup_count_len < 0) {
60 strerror_r(errno, buf, sizeof(buf));
61 ALOGE("Error reading from %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf);
62 wakeup_count_len = 0;
63 continue;
64 }
65 if (!wakeup_count_len) {
66 ALOGE("Empty wakeup count\n");
67 continue;
68 }
69
70 ALOGV("%s: wait\n", __func__);
71 ret = sem_wait(&suspend_lockout);
72 if (ret < 0) {
73 strerror_r(errno, buf, sizeof(buf));
74 ALOGE("Error waiting on semaphore: %s\n", buf);
75 continue;
76 }
77
Ruchi Kandoid3027d82015-05-13 14:57:08 -070078 success = true;
Colin Crossa2582c22012-05-03 17:30:16 -070079 ALOGV("%s: write %*s to wakeup_count\n", __func__, wakeup_count_len, wakeup_count);
Jeff Brown0446e162015-05-15 14:56:03 -070080 ret = TEMP_FAILURE_RETRY(write(wakeup_count_fd, wakeup_count, wakeup_count_len));
Colin Crossa2582c22012-05-03 17:30:16 -070081 if (ret < 0) {
82 strerror_r(errno, buf, sizeof(buf));
83 ALOGE("Error writing to %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf);
84 } else {
85 ALOGV("%s: write %s to %s\n", __func__, sleep_state, SYS_POWER_STATE);
Jeff Brown0446e162015-05-15 14:56:03 -070086 ret = TEMP_FAILURE_RETRY(write(state_fd, sleep_state, strlen(sleep_state)));
Colin Crossa2582c22012-05-03 17:30:16 -070087 if (ret < 0) {
Ruchi Kandoid3027d82015-05-13 14:57:08 -070088 success = false;
89 }
90 void (*func)(bool success) = wakeup_func;
91 if (func != NULL) {
92 (*func)(success);
Colin Crossa2582c22012-05-03 17:30:16 -070093 }
94 }
95
96 ALOGV("%s: release sem\n", __func__);
97 ret = sem_post(&suspend_lockout);
98 if (ret < 0) {
99 strerror_r(errno, buf, sizeof(buf));
100 ALOGE("Error releasing semaphore: %s\n", buf);
101 }
102 }
103 return NULL;
104}
105
106static int autosuspend_wakeup_count_enable(void)
107{
108 char buf[80];
109 int ret;
110
111 ALOGV("autosuspend_wakeup_count_enable\n");
112
113 ret = sem_post(&suspend_lockout);
114
115 if (ret < 0) {
116 strerror_r(errno, buf, sizeof(buf));
117 ALOGE("Error changing semaphore: %s\n", buf);
118 }
119
120 ALOGV("autosuspend_wakeup_count_enable done\n");
121
122 return ret;
123}
124
125static int autosuspend_wakeup_count_disable(void)
126{
127 char buf[80];
128 int ret;
129
130 ALOGV("autosuspend_wakeup_count_disable\n");
131
132 ret = sem_wait(&suspend_lockout);
133
134 if (ret < 0) {
135 strerror_r(errno, buf, sizeof(buf));
136 ALOGE("Error changing semaphore: %s\n", buf);
137 }
138
139 ALOGV("autosuspend_wakeup_count_disable done\n");
140
141 return ret;
142}
143
Ruchi Kandoid3027d82015-05-13 14:57:08 -0700144void set_wakeup_callback(void (*func)(bool success))
Dianne Hackborn9552cdf2014-03-07 11:00:39 -0800145{
146 if (wakeup_func != NULL) {
147 ALOGE("Duplicate wakeup callback applied, keeping original");
148 return;
149 }
150 wakeup_func = func;
151}
152
Colin Crossa2582c22012-05-03 17:30:16 -0700153struct autosuspend_ops autosuspend_wakeup_count_ops = {
154 .enable = autosuspend_wakeup_count_enable,
155 .disable = autosuspend_wakeup_count_disable,
156};
157
158struct autosuspend_ops *autosuspend_wakeup_count_init(void)
159{
160 int ret;
161 char buf[80];
162
Jeff Brown0446e162015-05-15 14:56:03 -0700163 state_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_STATE, O_RDWR));
Colin Crossa2582c22012-05-03 17:30:16 -0700164 if (state_fd < 0) {
165 strerror_r(errno, buf, sizeof(buf));
166 ALOGE("Error opening %s: %s\n", SYS_POWER_STATE, buf);
167 goto err_open_state;
168 }
169
Jeff Brown0446e162015-05-15 14:56:03 -0700170 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) {
172 strerror_r(errno, buf, sizeof(buf));
173 ALOGE("Error opening %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf);
174 goto err_open_wakeup_count;
175 }
176
177 ret = sem_init(&suspend_lockout, 0, 0);
178 if (ret < 0) {
179 strerror_r(errno, buf, sizeof(buf));
180 ALOGE("Error creating semaphore: %s\n", buf);
181 goto err_sem_init;
182 }
183 ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
184 if (ret) {
185 strerror_r(ret, buf, sizeof(buf));
186 ALOGE("Error creating thread: %s\n", buf);
187 goto err_pthread_create;
188 }
189
190 ALOGI("Selected wakeup count\n");
191 return &autosuspend_wakeup_count_ops;
192
193err_pthread_create:
194 sem_destroy(&suspend_lockout);
195err_sem_init:
196 close(wakeup_count_fd);
197err_open_wakeup_count:
198 close(state_fd);
199err_open_state:
200 return NULL;
201}