blob: 97109ac2c82a4dab70719e07f7434c0dce8cfe0a [file] [log] [blame]
Colin Crossa2582c22012-05-03 17:30:16 -07001/*
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 Salyzyn66ce3e02016-09-28 10:07:20 -070017#define LOG_TAG "libsuspend"
18
Colin Crossa2582c22012-05-03 17:30:16 -070019#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 Salyzyn66ce3e02016-09-28 10:07:20 -070027#include <android/log.h>
Colin Crossa2582c22012-05-03 17:30:16 -070028
29#include "autosuspend_ops.h"
30
31#define SYS_POWER_AUTOSLEEP "/sys/power/autosleep"
32
33static int autosleep_fd;
34static const char *sleep_state = "mem";
35static const char *on_state = "off";
36
37static int autosuspend_autosleep_enable(void)
38{
39 char buf[80];
40 int ret;
41
42 ALOGV("autosuspend_autosleep_enable\n");
43
Jeff Brown0446e162015-05-15 14:56:03 -070044 ret = TEMP_FAILURE_RETRY(write(autosleep_fd, sleep_state, strlen(sleep_state)));
Colin Crossa2582c22012-05-03 17:30:16 -070045 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
55err:
56 return ret;
57}
58
59static int autosuspend_autosleep_disable(void)
60{
61 char buf[80];
62 int ret;
63
64 ALOGV("autosuspend_autosleep_disable\n");
65
Jeff Brown0446e162015-05-15 14:56:03 -070066 ret = TEMP_FAILURE_RETRY(write(autosleep_fd, on_state, strlen(on_state)));
Colin Crossa2582c22012-05-03 17:30:16 -070067 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
77err:
78 return ret;
79}
80
81struct autosuspend_ops autosuspend_autosleep_ops = {
82 .enable = autosuspend_autosleep_enable,
83 .disable = autosuspend_autosleep_disable,
84};
85
86struct autosuspend_ops *autosuspend_autosleep_init(void)
87{
Colin Crossa2582c22012-05-03 17:30:16 -070088 char buf[80];
89
Jeff Brown0446e162015-05-15 14:56:03 -070090 autosleep_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_AUTOSLEEP, O_WRONLY));
Colin Crossa2582c22012-05-03 17:30:16 -070091 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 Crossb9886552012-09-21 18:39:17 -070098
99 autosuspend_autosleep_disable();
100
Colin Crossa2582c22012-05-03 17:30:16 -0700101 return &autosuspend_autosleep_ops;
102}