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 
17 #include <stdbool.h>
18 
19 #define LOG_TAG "libsuspend"
20 #include <cutils/log.h>
21 
22 #include <suspend/autosuspend.h>
23 
24 #include "autosuspend_ops.h"
25 
26 static struct autosuspend_ops *autosuspend_ops;
27 static bool autosuspend_enabled;
28 static bool autosuspend_inited;
29 
autosuspend_init(void)30 static int autosuspend_init(void)
31 {
32     if (autosuspend_inited) {
33         return 0;
34     }
35 
36     autosuspend_ops = autosuspend_earlysuspend_init();
37     if (autosuspend_ops) {
38         goto out;
39     }
40 
41 /* Remove autosleep so userspace can manager suspend/resume and keep stats */
42 #if 0
43     autosuspend_ops = autosuspend_autosleep_init();
44     if (autosuspend_ops) {
45         goto out;
46     }
47 #endif
48 
49     autosuspend_ops = autosuspend_wakeup_count_init();
50     if (autosuspend_ops) {
51         goto out;
52     }
53 
54     if (!autosuspend_ops) {
55         ALOGE("failed to initialize autosuspend\n");
56         return -1;
57     }
58 
59 out:
60     autosuspend_inited = true;
61 
62     ALOGV("autosuspend initialized\n");
63     return 0;
64 }
65 
autosuspend_enable(void)66 int autosuspend_enable(void)
67 {
68     int ret;
69 
70     ret = autosuspend_init();
71     if (ret) {
72         return ret;
73     }
74 
75     ALOGV("autosuspend_enable\n");
76 
77     if (autosuspend_enabled) {
78         return 0;
79     }
80 
81     ret = autosuspend_ops->enable();
82     if (ret) {
83         return ret;
84     }
85 
86     autosuspend_enabled = true;
87     return 0;
88 }
89 
autosuspend_disable(void)90 int autosuspend_disable(void)
91 {
92     int ret;
93 
94     ret = autosuspend_init();
95     if (ret) {
96         return ret;
97     }
98 
99     ALOGV("autosuspend_disable\n");
100 
101     if (!autosuspend_enabled) {
102         return 0;
103     }
104 
105     ret = autosuspend_ops->disable();
106     if (ret) {
107         return ret;
108     }
109 
110     autosuspend_enabled = false;
111     return 0;
112 }
113