The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 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> |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <pthread.h> |
| 27 | |
| 28 | #define LOG_TAG "power" |
Dan Willemsen | 24230cd | 2017-04-07 14:15:02 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 31 | enum { |
| 32 | ACQUIRE_PARTIAL_WAKE_LOCK = 0, |
| 33 | RELEASE_WAKE_LOCK, |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 34 | OUR_FD_COUNT |
| 35 | }; |
| 36 | |
| 37 | const char * const OLD_PATHS[] = { |
| 38 | "/sys/android_power/acquire_partial_wake_lock", |
| 39 | "/sys/android_power/release_wake_lock", |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | const char * const NEW_PATHS[] = { |
| 43 | "/sys/power/wake_lock", |
| 44 | "/sys/power/wake_unlock", |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 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]; |
Rom Lemarchand | ae2b826 | 2015-12-10 08:22:00 -0800 | [diff] [blame] | 50 | static int g_error = -1; |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 51 | |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 52 | static int |
| 53 | open_file_descriptors(const char * const paths[]) |
| 54 | { |
| 55 | int i; |
| 56 | for (i=0; i<OUR_FD_COUNT; i++) { |
Nick Kralevich | b3624b2 | 2015-05-08 12:13:51 -0700 | [diff] [blame] | 57 | int fd = open(paths[i], O_RDWR | O_CLOEXEC); |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 58 | if (fd < 0) { |
Rom Lemarchand | ae2b826 | 2015-12-10 08:22:00 -0800 | [diff] [blame] | 59 | g_error = -errno; |
| 60 | fprintf(stderr, "fatal error opening \"%s\": %s\n", paths[i], |
| 61 | strerror(errno)); |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 62 | return -1; |
| 63 | } |
| 64 | g_fds[i] = fd; |
| 65 | } |
| 66 | |
| 67 | g_error = 0; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static inline void |
| 72 | initialize_fds(void) |
| 73 | { |
| 74 | // XXX: should be this: |
| 75 | //pthread_once(&g_initialized, open_file_descriptors); |
| 76 | // XXX: not this: |
| 77 | if (g_initialized == 0) { |
Todd Poynor | 77d789d | 2012-02-11 00:44:22 -0800 | [diff] [blame] | 78 | if(open_file_descriptors(NEW_PATHS) < 0) |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 79 | open_file_descriptors(OLD_PATHS); |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 80 | g_initialized = 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | acquire_wake_lock(int lock, const char* id) |
| 86 | { |
| 87 | initialize_fds(); |
| 88 | |
Steve Block | 6f211ce | 2012-01-04 20:07:20 +0000 | [diff] [blame] | 89 | // ALOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id); |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 90 | |
| 91 | if (g_error) return g_error; |
| 92 | |
| 93 | int fd; |
Rom Lemarchand | ae2b826 | 2015-12-10 08:22:00 -0800 | [diff] [blame] | 94 | size_t len; |
| 95 | ssize_t ret; |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 96 | |
Rom Lemarchand | ae2b826 | 2015-12-10 08:22:00 -0800 | [diff] [blame] | 97 | if (lock != PARTIAL_WAKE_LOCK) { |
| 98 | return -EINVAL; |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Rom Lemarchand | ae2b826 | 2015-12-10 08:22:00 -0800 | [diff] [blame] | 101 | fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK]; |
| 102 | |
| 103 | ret = write(fd, id, strlen(id)); |
| 104 | if (ret < 0) { |
| 105 | return -errno; |
| 106 | } |
| 107 | |
| 108 | return ret; |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int |
| 112 | release_wake_lock(const char* id) |
| 113 | { |
| 114 | initialize_fds(); |
| 115 | |
Steve Block | 6f211ce | 2012-01-04 20:07:20 +0000 | [diff] [blame] | 116 | // ALOGI("release_wake_lock id='%s'\n", id); |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 117 | |
| 118 | if (g_error) return g_error; |
| 119 | |
| 120 | ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id)); |
Rom Lemarchand | ae2b826 | 2015-12-10 08:22:00 -0800 | [diff] [blame] | 121 | if (len < 0) { |
| 122 | return -errno; |
| 123 | } |
| 124 | return len; |
The Android Open Source Project | cc49016 | 2009-03-03 19:32:14 -0800 | [diff] [blame] | 125 | } |