The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | #include <hardware/power.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <errno.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <unistd.h> |
| 7 | #include <sys/time.h> |
| 8 | #include <time.h> |
| 9 | #include <errno.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <pthread.h> |
| 14 | |
| 15 | #define LOG_TAG "power" |
| 16 | #include <utils/Log.h> |
| 17 | |
| 18 | enum { |
| 19 | ACQUIRE_PARTIAL_WAKE_LOCK = 0, |
| 20 | ACQUIRE_FULL_WAKE_LOCK, |
| 21 | RELEASE_WAKE_LOCK, |
| 22 | REQUEST_STATE, |
| 23 | OUR_FD_COUNT |
| 24 | }; |
| 25 | |
| 26 | const char * const PATHS[] = { |
| 27 | "/sys/android_power/acquire_partial_wake_lock", |
| 28 | "/sys/android_power/acquire_full_wake_lock", |
| 29 | "/sys/android_power/release_wake_lock", |
| 30 | "/sys/android_power/request_state" |
| 31 | }; |
| 32 | |
| 33 | const char * const AUTO_OFF_TIMEOUT_DEV = "/sys/android_power/auto_off_timeout"; |
| 34 | |
| 35 | const char * const LCD_BACKLIGHT = "/sys/class/leds/lcd-backlight/brightness"; |
| 36 | const char * const BUTTON_BACKLIGHT = "/sys/class/leds/button-backlight/brightness"; |
| 37 | const char * const KEYBOARD_BACKLIGHT = "/sys/class/leds/keyboard-backlight/brightness"; |
| 38 | |
| 39 | //XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT; |
| 40 | static int g_initialized = 0; |
| 41 | static int g_fds[OUR_FD_COUNT]; |
| 42 | static int g_error = 1; |
| 43 | |
| 44 | |
| 45 | static int64_t systemTime() |
| 46 | { |
| 47 | struct timespec t; |
| 48 | t.tv_sec = t.tv_nsec = 0; |
| 49 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 50 | return t.tv_sec*1000000000LL + t.tv_nsec; |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | open_file_descriptors(void) |
| 55 | { |
| 56 | int i; |
| 57 | for (i=0; i<OUR_FD_COUNT; i++) { |
| 58 | int fd = open(PATHS[i], O_RDWR); |
| 59 | if (fd < 0) { |
| 60 | fprintf(stderr, "fatal error opening \"%s\"\n", PATHS[i]); |
| 61 | g_error = errno; |
| 62 | return; |
| 63 | } |
| 64 | g_fds[i] = fd; |
| 65 | } |
| 66 | |
| 67 | g_error = 0; |
| 68 | } |
| 69 | |
| 70 | static inline void |
| 71 | initialize_fds(void) |
| 72 | { |
| 73 | // XXX: should be this: |
| 74 | //pthread_once(&g_initialized, open_file_descriptors); |
| 75 | // XXX: not this: |
| 76 | if (g_initialized == 0) { |
| 77 | open_file_descriptors(); |
| 78 | g_initialized = 1; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | acquire_wake_lock(int lock, const char* id) |
| 84 | { |
| 85 | initialize_fds(); |
| 86 | |
| 87 | // LOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id); |
| 88 | |
| 89 | if (g_error) return g_error; |
| 90 | |
| 91 | int fd; |
| 92 | |
| 93 | if (lock == PARTIAL_WAKE_LOCK) { |
| 94 | fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK]; |
| 95 | } |
| 96 | else if (lock == FULL_WAKE_LOCK) { |
| 97 | fd = g_fds[ACQUIRE_FULL_WAKE_LOCK]; |
| 98 | } |
| 99 | else { |
| 100 | return EINVAL; |
| 101 | } |
| 102 | |
| 103 | return write(fd, id, strlen(id)); |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | release_wake_lock(const char* id) |
| 108 | { |
| 109 | initialize_fds(); |
| 110 | |
| 111 | // LOGI("release_wake_lock id='%s'\n", id); |
| 112 | |
| 113 | if (g_error) return g_error; |
| 114 | |
| 115 | ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id)); |
| 116 | return len >= 0; |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | set_last_user_activity_timeout(int64_t delay) |
| 121 | { |
| 122 | // LOGI("set_last_user_activity_timeout delay=%d\n", ((int)(delay))); |
| 123 | |
| 124 | int fd = open(AUTO_OFF_TIMEOUT_DEV, O_RDWR); |
| 125 | if (fd >= 0) { |
| 126 | char buf[32]; |
| 127 | ssize_t len; |
| 128 | len = sprintf(buf, "%d", ((int)(delay))); |
| 129 | len = write(fd, buf, len); |
| 130 | close(fd); |
| 131 | return 0; |
| 132 | } else { |
| 133 | return errno; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | set_a_light(const char* path, int value) |
| 139 | { |
| 140 | int fd; |
| 141 | |
| 142 | // LOGI("set_a_light(%s, %d)\n", path, value); |
| 143 | |
| 144 | fd = open(path, O_RDWR); |
| 145 | if (fd) { |
| 146 | char buffer[20]; |
| 147 | int bytes = sprintf(buffer, "%d\n", value); |
| 148 | write(fd, buffer, bytes); |
| 149 | close(fd); |
| 150 | } else { |
| 151 | LOGE("set_a_light failed to open %s\n", path); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int |
| 156 | set_light_brightness(unsigned int mask, unsigned int brightness) |
| 157 | { |
| 158 | initialize_fds(); |
| 159 | |
| 160 | // LOGI("set_light_brightness mask=0x%08x brightness=%d now=%lld g_error=%s\n", |
| 161 | // mask, brightness, systemTime(), strerror(g_error)); |
| 162 | |
| 163 | if (mask & KEYBOARD_LIGHT) { |
| 164 | set_a_light(KEYBOARD_BACKLIGHT, brightness); |
| 165 | } |
| 166 | |
| 167 | if (mask & SCREEN_LIGHT) { |
| 168 | set_a_light(LCD_BACKLIGHT, brightness); |
| 169 | } |
| 170 | |
| 171 | if (mask & BUTTON_LIGHT) { |
| 172 | set_a_light(BUTTON_BACKLIGHT, brightness); |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | int |
| 180 | set_screen_state(int on) |
| 181 | { |
| 182 | //LOGI("*** set_screen_state %d", on); |
| 183 | |
| 184 | initialize_fds(); |
| 185 | |
| 186 | //LOGI("go_to_sleep eventTime=%lld now=%lld g_error=%s\n", eventTime, |
| 187 | // systemTime(), strerror(g_error)); |
| 188 | |
| 189 | if (g_error) return g_error; |
| 190 | |
| 191 | char buf[32]; |
| 192 | int len; |
| 193 | if(on) |
| 194 | len = sprintf(buf, "wake"); |
| 195 | else |
| 196 | len = sprintf(buf, "standby"); |
| 197 | len = write(g_fds[REQUEST_STATE], buf, len); |
| 198 | if(len < 0) { |
| 199 | LOGE("Failed setting last user activity: g_error=%d\n", g_error); |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | |