blob: 1d6c4349e66ed9ab6e617980ac26063751cb2d15 [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 <stdbool.h>
20
Mark Salyzyn30f991f2017-01-10 13:19:54 -080021#include <log/log.h>
Colin Crossa2582c22012-05-03 17:30:16 -070022
23#include <suspend/autosuspend.h>
24
25#include "autosuspend_ops.h"
26
27static struct autosuspend_ops *autosuspend_ops;
28static bool autosuspend_enabled;
29static bool autosuspend_inited;
30
31static int autosuspend_init(void)
32{
33 if (autosuspend_inited) {
34 return 0;
35 }
36
Colin Crossa2582c22012-05-03 17:30:16 -070037 autosuspend_ops = autosuspend_wakeup_count_init();
38 if (autosuspend_ops) {
39 goto out;
40 }
41
42 if (!autosuspend_ops) {
Steve Paikfea16e12017-12-14 17:31:58 -080043 ALOGE("failed to initialize autosuspend");
Colin Crossa2582c22012-05-03 17:30:16 -070044 return -1;
45 }
46
47out:
Kyle Russella26b4ca2012-11-19 16:29:58 -050048 autosuspend_inited = true;
49
Steve Paikfea16e12017-12-14 17:31:58 -080050 ALOGV("autosuspend initialized");
Colin Crossa2582c22012-05-03 17:30:16 -070051 return 0;
52}
53
54int autosuspend_enable(void)
55{
56 int ret;
57
58 ret = autosuspend_init();
59 if (ret) {
60 return ret;
61 }
62
Steve Paikfea16e12017-12-14 17:31:58 -080063 ALOGV("autosuspend_enable");
Colin Crossa2582c22012-05-03 17:30:16 -070064
65 if (autosuspend_enabled) {
66 return 0;
67 }
68
69 ret = autosuspend_ops->enable();
70 if (ret) {
71 return ret;
72 }
73
74 autosuspend_enabled = true;
75 return 0;
76}
77
78int autosuspend_disable(void)
79{
80 int ret;
81
82 ret = autosuspend_init();
83 if (ret) {
84 return ret;
85 }
86
Steve Paikfea16e12017-12-14 17:31:58 -080087 ALOGV("autosuspend_disable");
Colin Crossa2582c22012-05-03 17:30:16 -070088
89 if (!autosuspend_enabled) {
90 return 0;
91 }
92
93 ret = autosuspend_ops->disable();
94 if (ret) {
95 return ret;
96 }
97
98 autosuspend_enabled = false;
99 return 0;
100}