blob: edd100711c7813a472666443ebda8d906ced6289 [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
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
26static struct autosuspend_ops *autosuspend_ops;
27static bool autosuspend_enabled;
28static bool autosuspend_inited;
29
30static int autosuspend_init(void)
31{
32 if (autosuspend_inited) {
33 return 0;
34 }
35
Colin Crossa2582c22012-05-03 17:30:16 -070036 autosuspend_ops = autosuspend_earlysuspend_init();
37 if (autosuspend_ops) {
38 goto out;
39 }
40
Todd Poynor6f268912014-03-04 13:11:21 -080041/* Remove autosleep so userspace can manager suspend/resume and keep stats */
42#if 0
Colin Crossa2582c22012-05-03 17:30:16 -070043 autosuspend_ops = autosuspend_autosleep_init();
44 if (autosuspend_ops) {
45 goto out;
46 }
Todd Poynor6f268912014-03-04 13:11:21 -080047#endif
Colin Crossa2582c22012-05-03 17:30:16 -070048
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
59out:
Kyle Russella26b4ca2012-11-19 16:29:58 -050060 autosuspend_inited = true;
61
Colin Crossa2582c22012-05-03 17:30:16 -070062 ALOGV("autosuspend initialized\n");
63 return 0;
64}
65
66int 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
90int 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}