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>
36
ACQUIRE_PARTIAL_WAKE_LOCK = 0,
41
const char * const OLD_PATHS[] = {
42
"/sys/android_power/acquire_partial_wake_lock",
43
"/sys/android_power/release_wake_lock",
46
const char * const NEW_PATHS[] = {
47
"/sys/power/wake_lock",
48
"/sys/power/wake_unlock",
51
//XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT;
52
static int g_initialized = 0;
53
static int g_fds[OUR_FD_COUNT];
54
static int g_error = 1;
56
static int64_t systemTime()
59
t.tv_sec = t.tv_nsec = 0;
60
clock_gettime(CLOCK_MONOTONIC, &t);
61
return t.tv_sec*1000000000LL + t.tv_nsec;
65
open_file_descriptors(const char * const paths[])
68
for (i=0; i<OUR_FD_COUNT; i++) {
69
int fd = open(paths[i], O_RDWR);
71
fprintf(stderr, "fatal error opening \"%s\"\n", paths[i]);
85
// XXX: should be this:
86
//pthread_once(&g_initialized, open_file_descriptors);
88
if (g_initialized == 0) {
89
if(open_file_descriptors(NEW_PATHS) < 0)
90
open_file_descriptors(OLD_PATHS);
96
acquire_wake_lock(int lock, const char* id)
100
// ALOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
102
if (g_error) return g_error;
106
if (lock == PARTIAL_WAKE_LOCK) {
107
fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK];
113
return write(fd, id, strlen(id));
117
release_wake_lock(const char* id)
121
// ALOGI("release_wake_lock id='%s'\n", id);
123
if (g_error) return g_error;
125
ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id));
129
#else // HAVE_WAKE_LOCK
132
acquire_wake_lock(int lock, const char* id)
138
release_wake_lock(const char* id)
143
#endif // HAVE_WAKE_LOCK