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