OSDN Git Service

legacy: Add asec binder transaction support to c++ wrappers
[android-x86/hardware-libhardware_legacy.git] / power / power.c
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  */
16 #include <hardware_legacy/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
33 #include "qemu.h"
34 #ifdef QEMU_POWER
35 #include "power_qemu.h"
36 #endif
37
38 enum {
39     ACQUIRE_PARTIAL_WAKE_LOCK = 0,
40     RELEASE_WAKE_LOCK,
41     REQUEST_STATE,
42     OUR_FD_COUNT
43 };
44
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"
49 };
50
51 const char * const NEW_PATHS[] = {
52     "/sys/power/wake_lock",
53     "/sys/power/wake_unlock",
54     "/sys/power/state"
55 };
56
57 const char * const AUTO_OFF_TIMEOUT_DEV = "/sys/android_power/auto_off_timeout";
58
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;
63
64 static const char *off_state = "mem";
65 static const char *on_state = "on";
66
67 static int64_t systemTime()
68 {
69     struct timespec t;
70     t.tv_sec = t.tv_nsec = 0;
71     clock_gettime(CLOCK_MONOTONIC, &t);
72     return t.tv_sec*1000000000LL + t.tv_nsec;
73 }
74
75 static int
76 open_file_descriptors(const char * const paths[])
77 {
78     int i;
79     for (i=0; i<OUR_FD_COUNT; i++) {
80         int fd = open(paths[i], O_RDWR);
81         if (fd < 0) {
82             fprintf(stderr, "fatal error opening \"%s\"\n", paths[i]);
83             g_error = errno;
84             return -1;
85         }
86         g_fds[i] = fd;
87     }
88
89     g_error = 0;
90     return 0;
91 }
92
93 static inline void
94 initialize_fds(void)
95 {
96     // XXX: should be this:
97     //pthread_once(&g_initialized, open_file_descriptors);
98     // XXX: not this:
99     if (g_initialized == 0) {
100         if(open_file_descriptors(NEW_PATHS) < 0) {
101             open_file_descriptors(OLD_PATHS);
102             on_state = "wake";
103             off_state = "standby";
104         }
105         g_initialized = 1;
106     }
107 }
108
109 int
110 acquire_wake_lock(int lock, const char* id)
111 {
112     initialize_fds();
113
114 //    LOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
115
116     if (g_error) return g_error;
117
118     int fd;
119
120     if (lock == PARTIAL_WAKE_LOCK) {
121         fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK];
122     }
123     else {
124         return EINVAL;
125     }
126
127     return write(fd, id, strlen(id));
128 }
129
130 int
131 release_wake_lock(const char* id)
132 {
133     initialize_fds();
134
135 //    LOGI("release_wake_lock id='%s'\n", id);
136
137     if (g_error) return g_error;
138
139     ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id));
140     return len >= 0;
141 }
142
143 int
144 set_last_user_activity_timeout(int64_t delay)
145 {
146 //    LOGI("set_last_user_activity_timeout delay=%d\n", ((int)(delay)));
147
148     int fd = open(AUTO_OFF_TIMEOUT_DEV, O_RDWR);
149     if (fd >= 0) {
150         char buf[32];
151         ssize_t len;
152         len = sprintf(buf, "%d", ((int)(delay)));
153         len = write(fd, buf, len);
154         close(fd);
155         return 0;
156     } else {
157         return errno;
158     }
159 }
160
161 int
162 set_screen_state(int on)
163 {
164     QEMU_FALLBACK(set_screen_state(on));
165
166     LOGI("*** set_screen_state %d", on);
167
168     initialize_fds();
169
170     //LOGI("go_to_sleep eventTime=%lld now=%lld g_error=%s\n", eventTime,
171       //      systemTime(), strerror(g_error));
172
173     if (g_error) return g_error;
174
175     char buf[32];
176     int len;
177     if(on)
178         len = sprintf(buf, on_state);
179     else
180         len = sprintf(buf, off_state);
181     len = write(g_fds[REQUEST_STATE], buf, len);
182     if(len < 0) {
183         LOGE("Failed setting last user activity: g_error=%d\n", g_error);
184     }
185     return 0;
186 }