Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame^] | 17 | #define LOG_TAG "libsuspend" |
| 18 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stddef.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame^] | 27 | #include <android/log.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 28 | |
| 29 | #include "autosuspend_ops.h" |
| 30 | |
| 31 | #define SYS_POWER_AUTOSLEEP "/sys/power/autosleep" |
| 32 | |
| 33 | static int autosleep_fd; |
| 34 | static const char *sleep_state = "mem"; |
| 35 | static const char *on_state = "off"; |
| 36 | |
| 37 | static int autosuspend_autosleep_enable(void) |
| 38 | { |
| 39 | char buf[80]; |
| 40 | int ret; |
| 41 | |
| 42 | ALOGV("autosuspend_autosleep_enable\n"); |
| 43 | |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 44 | ret = TEMP_FAILURE_RETRY(write(autosleep_fd, sleep_state, strlen(sleep_state))); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 45 | if (ret < 0) { |
| 46 | strerror_r(errno, buf, sizeof(buf)); |
| 47 | ALOGE("Error writing to %s: %s\n", SYS_POWER_AUTOSLEEP, buf); |
| 48 | goto err; |
| 49 | } |
| 50 | |
| 51 | ALOGV("autosuspend_autosleep_enable done\n"); |
| 52 | |
| 53 | return 0; |
| 54 | |
| 55 | err: |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | static int autosuspend_autosleep_disable(void) |
| 60 | { |
| 61 | char buf[80]; |
| 62 | int ret; |
| 63 | |
| 64 | ALOGV("autosuspend_autosleep_disable\n"); |
| 65 | |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 66 | ret = TEMP_FAILURE_RETRY(write(autosleep_fd, on_state, strlen(on_state))); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 67 | if (ret < 0) { |
| 68 | strerror_r(errno, buf, sizeof(buf)); |
| 69 | ALOGE("Error writing to %s: %s\n", SYS_POWER_AUTOSLEEP, buf); |
| 70 | goto err; |
| 71 | } |
| 72 | |
| 73 | ALOGV("autosuspend_autosleep_disable done\n"); |
| 74 | |
| 75 | return 0; |
| 76 | |
| 77 | err: |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | struct autosuspend_ops autosuspend_autosleep_ops = { |
| 82 | .enable = autosuspend_autosleep_enable, |
| 83 | .disable = autosuspend_autosleep_disable, |
| 84 | }; |
| 85 | |
| 86 | struct autosuspend_ops *autosuspend_autosleep_init(void) |
| 87 | { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 88 | char buf[80]; |
| 89 | |
Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 90 | autosleep_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_AUTOSLEEP, O_WRONLY)); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 91 | if (autosleep_fd < 0) { |
| 92 | strerror_r(errno, buf, sizeof(buf)); |
| 93 | ALOGE("Error opening %s: %s\n", SYS_POWER_AUTOSLEEP, buf); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | ALOGI("Selected autosleep\n"); |
Colin Cross | b988655 | 2012-09-21 18:39:17 -0700 | [diff] [blame] | 98 | |
| 99 | autosuspend_autosleep_disable(); |
| 100 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 101 | return &autosuspend_autosleep_ops; |
| 102 | } |