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>
25 #include <sys/types.h>
28 #define LOG_TAG "power"
32 ACQUIRE_PARTIAL_WAKE_LOCK = 0,
37 const char * const OLD_PATHS[] = {
38 "/sys/android_power/acquire_partial_wake_lock",
39 "/sys/android_power/release_wake_lock",
42 const char * const NEW_PATHS[] = {
43 "/sys/power/wake_lock",
44 "/sys/power/wake_unlock",
47 //XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT;
48 static int g_initialized = 0;
49 static int g_fds[OUR_FD_COUNT];
50 static int g_error = -1;
53 open_file_descriptors(const char * const paths[])
56 for (i=0; i<OUR_FD_COUNT; i++) {
57 int fd = open(paths[i], O_RDWR | O_CLOEXEC);
60 fprintf(stderr, "fatal error opening \"%s\": %s\n", paths[i],
74 // XXX: should be this:
75 //pthread_once(&g_initialized, open_file_descriptors);
77 if (g_initialized == 0) {
78 if(open_file_descriptors(NEW_PATHS) < 0)
79 open_file_descriptors(OLD_PATHS);
85 acquire_wake_lock(int lock, const char* id)
89 // ALOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
91 if (g_error) return g_error;
97 if (lock != PARTIAL_WAKE_LOCK) {
101 fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK];
103 ret = write(fd, id, strlen(id));
112 release_wake_lock(const char* id)
116 // ALOGI("release_wake_lock id='%s'\n", id);
118 if (g_error) return g_error;
120 ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id));