The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | #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 Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 33 | #include "qemu.h" |
| 34 | #ifdef QEMU_POWER |
| 35 | #include "power_qemu.h" |
| 36 | #endif |
| 37 | |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | enum { |
| 39 | ACQUIRE_PARTIAL_WAKE_LOCK = 0, |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | RELEASE_WAKE_LOCK, |
| 41 | REQUEST_STATE, |
| 42 | OUR_FD_COUNT |
| 43 | }; |
| 44 | |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 45 | const char * const OLD_PATHS[] = { |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | "/sys/android_power/acquire_partial_wake_lock", |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | "/sys/android_power/release_wake_lock", |
| 48 | "/sys/android_power/request_state" |
| 49 | }; |
| 50 | |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 51 | const char * const NEW_PATHS[] = { |
| 52 | "/sys/power/wake_lock", |
| 53 | "/sys/power/wake_unlock", |
| 54 | "/sys/power/state" |
| 55 | }; |
| 56 | |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | const char * const AUTO_OFF_TIMEOUT_DEV = "/sys/android_power/auto_off_timeout"; |
| 58 | |
| 59 | const char * const LCD_BACKLIGHT = "/sys/class/leds/lcd-backlight/brightness"; |
| 60 | const char * const BUTTON_BACKLIGHT = "/sys/class/leds/button-backlight/brightness"; |
| 61 | const char * const KEYBOARD_BACKLIGHT = "/sys/class/leds/keyboard-backlight/brightness"; |
| 62 | |
| 63 | //XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT; |
| 64 | static int g_initialized = 0; |
| 65 | static int g_fds[OUR_FD_COUNT]; |
| 66 | static int g_error = 1; |
| 67 | |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 68 | static const char *off_state = "mem"; |
| 69 | static const char *on_state = "on"; |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
| 71 | static 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 Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 79 | static int |
| 80 | open_file_descriptors(const char * const paths[]) |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | { |
| 82 | int i; |
| 83 | for (i=0; i<OUR_FD_COUNT; i++) { |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 84 | int fd = open(paths[i], O_RDWR); |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | if (fd < 0) { |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 86 | fprintf(stderr, "fatal error opening \"%s\"\n", paths[i]); |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | g_error = errno; |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 88 | return -1; |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | } |
| 90 | g_fds[i] = fd; |
| 91 | } |
| 92 | |
| 93 | g_error = 0; |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 94 | return 0; |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static inline void |
| 98 | initialize_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 Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 104 | 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 Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | g_initialized = 1; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | acquire_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 Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | else { |
| 128 | return EINVAL; |
| 129 | } |
| 130 | |
| 131 | return write(fd, id, strlen(id)); |
| 132 | } |
| 133 | |
| 134 | int |
| 135 | release_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 | |
| 147 | int |
| 148 | set_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 | |
| 165 | static void |
| 166 | set_a_light(const char* path, int value) |
| 167 | { |
| 168 | int fd; |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 169 | static int already_warned = 0; |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | |
| 171 | // LOGI("set_a_light(%s, %d)\n", path, value); |
| 172 | |
| 173 | fd = open(path, O_RDWR); |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 174 | if (fd >= 0) { |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | 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 Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 180 | if (already_warned == 0) { |
| 181 | LOGE("set_a_light failed to open %s\n", path); |
| 182 | already_warned = 1; |
| 183 | } |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | int |
| 188 | set_light_brightness(unsigned int mask, unsigned int brightness) |
| 189 | { |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 190 | QEMU_FALLBACK(set_light_brightness(mask,brightness)); |
| 191 | |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | 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 | |
| 213 | int |
| 214 | set_screen_state(int on) |
| 215 | { |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 216 | QEMU_FALLBACK(set_screen_state(on)); |
| 217 | |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | //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 Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 230 | len = sprintf(buf, on_state); |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | else |
The Android Open Source Project | 51704be | 2008-12-17 18:05:50 -0800 | [diff] [blame] | 232 | len = sprintf(buf, off_state); |
The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | 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 | } |