blob: b87f59cd6bcaa5b13eba454da951d63f30375b97 [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
Steve Paik10b149f2018-01-03 20:27:58 -080027static struct autosuspend_ops* autosuspend_ops = NULL;
Colin Crossa2582c22012-05-03 17:30:16 -070028static bool autosuspend_enabled;
Colin Crossa2582c22012-05-03 17:30:16 -070029
Steve Paik2d441902017-12-15 13:26:28 -080030static int autosuspend_init(void) {
Steve Paik10b149f2018-01-03 20:27:58 -080031 if (autosuspend_ops != NULL) {
Colin Crossa2582c22012-05-03 17:30:16 -070032 return 0;
33 }
34
Colin Crossa2582c22012-05-03 17:30:16 -070035 autosuspend_ops = autosuspend_wakeup_count_init();
Steve Paik10b149f2018-01-03 20:27:58 -080036 if (autosuspend_ops == NULL) {
Steve Paikfea16e12017-12-14 17:31:58 -080037 ALOGE("failed to initialize autosuspend");
Colin Crossa2582c22012-05-03 17:30:16 -070038 return -1;
39 }
40
Steve Paikfea16e12017-12-14 17:31:58 -080041 ALOGV("autosuspend initialized");
Colin Crossa2582c22012-05-03 17:30:16 -070042 return 0;
43}
44
Steve Paik2d441902017-12-15 13:26:28 -080045int autosuspend_enable(void) {
Colin Crossa2582c22012-05-03 17:30:16 -070046 int ret;
47
48 ret = autosuspend_init();
49 if (ret) {
50 return ret;
51 }
52
Steve Paikfea16e12017-12-14 17:31:58 -080053 ALOGV("autosuspend_enable");
Colin Crossa2582c22012-05-03 17:30:16 -070054
55 if (autosuspend_enabled) {
56 return 0;
57 }
58
59 ret = autosuspend_ops->enable();
60 if (ret) {
61 return ret;
62 }
63
64 autosuspend_enabled = true;
65 return 0;
66}
67
Steve Paik2d441902017-12-15 13:26:28 -080068int autosuspend_disable(void) {
Colin Crossa2582c22012-05-03 17:30:16 -070069 int ret;
70
71 ret = autosuspend_init();
72 if (ret) {
73 return ret;
74 }
75
Steve Paikfea16e12017-12-14 17:31:58 -080076 ALOGV("autosuspend_disable");
Colin Crossa2582c22012-05-03 17:30:16 -070077
78 if (!autosuspend_enabled) {
79 return 0;
80 }
81
82 ret = autosuspend_ops->disable();
83 if (ret) {
84 return ret;
85 }
86
87 autosuspend_enabled = false;
88 return 0;
89}
Steve Paik2d441902017-12-15 13:26:28 -080090
Steve Paik10b149f2018-01-03 20:27:58 -080091int autosuspend_force_suspend(int timeout_ms) {
92 int ret;
93
94 ret = autosuspend_init();
95 if (ret) {
96 return ret;
97 }
98
99 ALOGV("autosuspend_force_suspend");
100
101 return autosuspend_ops->force_suspend(timeout_ms);
102}
103
Steve Paik2d441902017-12-15 13:26:28 -0800104void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
105 int ret;
106
107 ret = autosuspend_init();
108 if (ret) {
109 return;
110 }
111
112 ALOGV("set_wakeup_callback");
113
114 autosuspend_ops->set_wakeup_callback(func);
115}