2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <hardware_legacy/power.h>
27 #include <sys/types.h>
30 #define LOG_TAG "power"
31 #include <utils/Log.h>
35 #include "power_qemu.h"
39 ACQUIRE_PARTIAL_WAKE_LOCK = 0,
45 const char * const OLD_PATHS[] = {
46 "/sys/android_power/acquire_partial_wake_lock",
47 "/sys/android_power/release_wake_lock",
48 "/sys/android_power/request_state"
51 const char * const NEW_PATHS[] = {
52 "/sys/power/wake_lock",
53 "/sys/power/wake_unlock",
57 const char * const AUTO_OFF_TIMEOUT_DEV = "/sys/android_power/auto_off_timeout";
59 //XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT;
60 static int g_initialized = 0;
61 static int g_fds[OUR_FD_COUNT];
62 static int g_error = 1;
64 static const char *off_state = "mem";
65 static const char *on_state = "on";
67 static int64_t systemTime()
70 t.tv_sec = t.tv_nsec = 0;
71 clock_gettime(CLOCK_MONOTONIC, &t);
72 return t.tv_sec*1000000000LL + t.tv_nsec;
76 open_file_descriptors(const char * const paths[])
79 for (i=0; i<OUR_FD_COUNT; i++) {
80 int fd = open(paths[i], O_RDWR);
82 fprintf(stderr, "fatal error opening \"%s\"\n", paths[i]);
96 // XXX: should be this:
97 //pthread_once(&g_initialized, open_file_descriptors);
99 if (g_initialized == 0) {
100 if(open_file_descriptors(NEW_PATHS) < 0) {
101 open_file_descriptors(OLD_PATHS);
103 off_state = "standby";
110 acquire_wake_lock(int lock, const char* id)
114 // LOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
116 if (g_error) return g_error;
120 if (lock == PARTIAL_WAKE_LOCK) {
121 fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK];
127 return write(fd, id, strlen(id));
131 release_wake_lock(const char* id)
135 // LOGI("release_wake_lock id='%s'\n", id);
137 if (g_error) return g_error;
139 ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id));
144 set_last_user_activity_timeout(int64_t delay)
146 // LOGI("set_last_user_activity_timeout delay=%d\n", ((int)(delay)));
148 int fd = open(AUTO_OFF_TIMEOUT_DEV, O_RDWR);
152 len = sprintf(buf, "%d", ((int)(delay)));
153 len = write(fd, buf, len);
162 set_screen_state(int on)
164 QEMU_FALLBACK(set_screen_state(on));
166 LOGI("*** set_screen_state %d", on);
170 //LOGI("go_to_sleep eventTime=%lld now=%lld g_error=%s\n", eventTime,
171 // systemTime(), strerror(g_error));
173 if (g_error) return g_error;
178 len = sprintf(buf, on_state);
180 len = sprintf(buf, off_state);
181 len = write(g_fds[REQUEST_STATE], buf, len);
183 LOGE("Failed setting last user activity: g_error=%d\n", g_error);