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
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
36    autosuspend_inited = true;
37
38    autosuspend_ops = autosuspend_earlysuspend_init();
39    if (autosuspend_ops) {
40        goto out;
41    }
42
43    autosuspend_ops = autosuspend_autosleep_init();
44    if (autosuspend_ops) {
45        goto out;
46    }
47
48    autosuspend_ops = autosuspend_wakeup_count_init();
49    if (autosuspend_ops) {
50        goto out;
51    }
52
53    if (!autosuspend_ops) {
54        ALOGE("failed to initialize autosuspend\n");
55        return -1;
56    }
57
58out:
59    ALOGV("autosuspend initialized\n");
60    return 0;
61}
62
63int autosuspend_enable(void)
64{
65    int ret;
66
67    ret = autosuspend_init();
68    if (ret) {
69        return ret;
70    }
71
72    ALOGV("autosuspend_enable\n");
73
74    if (autosuspend_enabled) {
75        return 0;
76    }
77
78    ret = autosuspend_ops->enable();
79    if (ret) {
80        return ret;
81    }
82
83    autosuspend_enabled = true;
84    return 0;
85}
86
87int autosuspend_disable(void)
88{
89    int ret;
90
91    ret = autosuspend_init();
92    if (ret) {
93        return ret;
94    }
95
96    ALOGV("autosuspend_disable\n");
97
98    if (!autosuspend_enabled) {
99        return 0;
100    }
101
102    ret = autosuspend_ops->disable();
103    if (ret) {
104        return ret;
105    }
106
107    autosuspend_enabled = false;
108    return 0;
109}
110