blob: 09fc0617e203d6105fdab6639bae966e28a1534b [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
Steve Paik2d441902017-12-15 13:26:28 -080031static int autosuspend_init(void) {
Colin Crossa2582c22012-05-03 17:30:16 -070032 if (autosuspend_inited) {
33 return 0;
34 }
35
Colin Crossa2582c22012-05-03 17:30:16 -070036 autosuspend_ops = autosuspend_wakeup_count_init();
37 if (autosuspend_ops) {
38 goto out;
39 }
40
41 if (!autosuspend_ops) {
Steve Paikfea16e12017-12-14 17:31:58 -080042 ALOGE("failed to initialize autosuspend");
Colin Crossa2582c22012-05-03 17:30:16 -070043 return -1;
44 }
45
46out:
Kyle Russella26b4ca2012-11-19 16:29:58 -050047 autosuspend_inited = true;
48
Steve Paikfea16e12017-12-14 17:31:58 -080049 ALOGV("autosuspend initialized");
Colin Crossa2582c22012-05-03 17:30:16 -070050 return 0;
51}
52
Steve Paik2d441902017-12-15 13:26:28 -080053int autosuspend_enable(void) {
Colin Crossa2582c22012-05-03 17:30:16 -070054 int ret;
55
56 ret = autosuspend_init();
57 if (ret) {
58 return ret;
59 }
60
Steve Paikfea16e12017-12-14 17:31:58 -080061 ALOGV("autosuspend_enable");
Colin Crossa2582c22012-05-03 17:30:16 -070062
63 if (autosuspend_enabled) {
64 return 0;
65 }
66
67 ret = autosuspend_ops->enable();
68 if (ret) {
69 return ret;
70 }
71
72 autosuspend_enabled = true;
73 return 0;
74}
75
Steve Paik2d441902017-12-15 13:26:28 -080076int autosuspend_disable(void) {
Colin Crossa2582c22012-05-03 17:30:16 -070077 int ret;
78
79 ret = autosuspend_init();
80 if (ret) {
81 return ret;
82 }
83
Steve Paikfea16e12017-12-14 17:31:58 -080084 ALOGV("autosuspend_disable");
Colin Crossa2582c22012-05-03 17:30:16 -070085
86 if (!autosuspend_enabled) {
87 return 0;
88 }
89
90 ret = autosuspend_ops->disable();
91 if (ret) {
92 return ret;
93 }
94
95 autosuspend_enabled = false;
96 return 0;
97}
Steve Paik2d441902017-12-15 13:26:28 -080098
99void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
100 int ret;
101
102 ret = autosuspend_init();
103 if (ret) {
104 return;
105 }
106
107 ALOGV("set_wakeup_callback");
108
109 autosuspend_ops->set_wakeup_callback(func);
110}