blob: 4c5ebd30c85d278ac7d761930e8800908ac4a960 [file] [log] [blame]
The Android Open Source Project51704be2008-12-17 18:05:50 -08001/*
2 * Copyright (C) 2008 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 */
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070016#include <hardware/power.h>
17#include <fcntl.h>
18#include <errno.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <sys/time.h>
23#include <time.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <pthread.h>
29
30#define LOG_TAG "power"
31#include <utils/Log.h>
32
The Android Open Source Project51704be2008-12-17 18:05:50 -080033#include "qemu.h"
34#ifdef QEMU_POWER
35#include "power_qemu.h"
36#endif
37
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070038enum {
39 ACQUIRE_PARTIAL_WAKE_LOCK = 0,
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070040 RELEASE_WAKE_LOCK,
41 REQUEST_STATE,
42 OUR_FD_COUNT
43};
44
The Android Open Source Project51704be2008-12-17 18:05:50 -080045const char * const OLD_PATHS[] = {
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070046 "/sys/android_power/acquire_partial_wake_lock",
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070047 "/sys/android_power/release_wake_lock",
48 "/sys/android_power/request_state"
49};
50
The Android Open Source Project51704be2008-12-17 18:05:50 -080051const char * const NEW_PATHS[] = {
52 "/sys/power/wake_lock",
53 "/sys/power/wake_unlock",
54 "/sys/power/state"
55};
56
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070057const char * const AUTO_OFF_TIMEOUT_DEV = "/sys/android_power/auto_off_timeout";
58
59const char * const LCD_BACKLIGHT = "/sys/class/leds/lcd-backlight/brightness";
60const char * const BUTTON_BACKLIGHT = "/sys/class/leds/button-backlight/brightness";
61const char * const KEYBOARD_BACKLIGHT = "/sys/class/leds/keyboard-backlight/brightness";
62
63//XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT;
64static int g_initialized = 0;
65static int g_fds[OUR_FD_COUNT];
66static int g_error = 1;
67
The Android Open Source Project51704be2008-12-17 18:05:50 -080068static const char *off_state = "mem";
69static const char *on_state = "on";
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070070
71static int64_t systemTime()
72{
73 struct timespec t;
74 t.tv_sec = t.tv_nsec = 0;
75 clock_gettime(CLOCK_MONOTONIC, &t);
76 return t.tv_sec*1000000000LL + t.tv_nsec;
77}
78
The Android Open Source Project51704be2008-12-17 18:05:50 -080079static int
80open_file_descriptors(const char * const paths[])
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070081{
82 int i;
83 for (i=0; i<OUR_FD_COUNT; i++) {
The Android Open Source Project51704be2008-12-17 18:05:50 -080084 int fd = open(paths[i], O_RDWR);
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070085 if (fd < 0) {
The Android Open Source Project51704be2008-12-17 18:05:50 -080086 fprintf(stderr, "fatal error opening \"%s\"\n", paths[i]);
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070087 g_error = errno;
The Android Open Source Project51704be2008-12-17 18:05:50 -080088 return -1;
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070089 }
90 g_fds[i] = fd;
91 }
92
93 g_error = 0;
The Android Open Source Project51704be2008-12-17 18:05:50 -080094 return 0;
The Android Open Source Projectd6054a32008-10-21 07:00:00 -070095}
96
97static inline void
98initialize_fds(void)
99{
100 // XXX: should be this:
101 //pthread_once(&g_initialized, open_file_descriptors);
102 // XXX: not this:
103 if (g_initialized == 0) {
The Android Open Source Project51704be2008-12-17 18:05:50 -0800104 if(open_file_descriptors(NEW_PATHS) < 0) {
105 open_file_descriptors(OLD_PATHS);
106 on_state = "wake";
107 off_state = "standby";
108 }
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700109 g_initialized = 1;
110 }
111}
112
113int
114acquire_wake_lock(int lock, const char* id)
115{
116 initialize_fds();
117
118// LOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
119
120 if (g_error) return g_error;
121
122 int fd;
123
124 if (lock == PARTIAL_WAKE_LOCK) {
125 fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK];
126 }
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700127 else {
128 return EINVAL;
129 }
130
131 return write(fd, id, strlen(id));
132}
133
134int
135release_wake_lock(const char* id)
136{
137 initialize_fds();
138
139// LOGI("release_wake_lock id='%s'\n", id);
140
141 if (g_error) return g_error;
142
143 ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id));
144 return len >= 0;
145}
146
147int
148set_last_user_activity_timeout(int64_t delay)
149{
150// LOGI("set_last_user_activity_timeout delay=%d\n", ((int)(delay)));
151
152 int fd = open(AUTO_OFF_TIMEOUT_DEV, O_RDWR);
153 if (fd >= 0) {
154 char buf[32];
155 ssize_t len;
156 len = sprintf(buf, "%d", ((int)(delay)));
157 len = write(fd, buf, len);
158 close(fd);
159 return 0;
160 } else {
161 return errno;
162 }
163}
164
165static void
166set_a_light(const char* path, int value)
167{
168 int fd;
The Android Open Source Project51704be2008-12-17 18:05:50 -0800169 static int already_warned = 0;
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700170
171 // LOGI("set_a_light(%s, %d)\n", path, value);
172
173 fd = open(path, O_RDWR);
The Android Open Source Project51704be2008-12-17 18:05:50 -0800174 if (fd >= 0) {
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700175 char buffer[20];
176 int bytes = sprintf(buffer, "%d\n", value);
177 write(fd, buffer, bytes);
178 close(fd);
179 } else {
The Android Open Source Project51704be2008-12-17 18:05:50 -0800180 if (already_warned == 0) {
181 LOGE("set_a_light failed to open %s\n", path);
182 already_warned = 1;
183 }
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700184 }
185}
186
187int
188set_light_brightness(unsigned int mask, unsigned int brightness)
189{
The Android Open Source Project51704be2008-12-17 18:05:50 -0800190 QEMU_FALLBACK(set_light_brightness(mask,brightness));
191
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700192 initialize_fds();
193
194// LOGI("set_light_brightness mask=0x%08x brightness=%d now=%lld g_error=%s\n",
195// mask, brightness, systemTime(), strerror(g_error));
196
197 if (mask & KEYBOARD_LIGHT) {
198 set_a_light(KEYBOARD_BACKLIGHT, brightness);
199 }
200
201 if (mask & SCREEN_LIGHT) {
202 set_a_light(LCD_BACKLIGHT, brightness);
203 }
204
205 if (mask & BUTTON_LIGHT) {
206 set_a_light(BUTTON_BACKLIGHT, brightness);
207 }
208
209 return 0;
210}
211
212
213int
214set_screen_state(int on)
215{
The Android Open Source Project51704be2008-12-17 18:05:50 -0800216 QEMU_FALLBACK(set_screen_state(on));
217
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700218 //LOGI("*** set_screen_state %d", on);
219
220 initialize_fds();
221
222 //LOGI("go_to_sleep eventTime=%lld now=%lld g_error=%s\n", eventTime,
223 // systemTime(), strerror(g_error));
224
225 if (g_error) return g_error;
226
227 char buf[32];
228 int len;
229 if(on)
The Android Open Source Project51704be2008-12-17 18:05:50 -0800230 len = sprintf(buf, on_state);
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700231 else
The Android Open Source Project51704be2008-12-17 18:05:50 -0800232 len = sprintf(buf, off_state);
The Android Open Source Projectd6054a32008-10-21 07:00:00 -0700233 len = write(g_fds[REQUEST_STATE], buf, len);
234 if(len < 0) {
235 LOGE("Failed setting last user activity: g_error=%d\n", g_error);
236 }
237 return 0;
238}