• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "libsuspend"
18 
19 #include <stdbool.h>
20 
21 #include <log/log.h>
22 
23 #include <suspend/autosuspend.h>
24 
25 #include "autosuspend_ops.h"
26 
27 static struct autosuspend_ops* autosuspend_ops = NULL;
28 static bool autosuspend_enabled;
29 
autosuspend_init(void)30 static int autosuspend_init(void) {
31     if (autosuspend_ops != NULL) {
32         return 0;
33     }
34 
35     autosuspend_ops = autosuspend_wakeup_count_init();
36     if (autosuspend_ops == NULL) {
37         ALOGE("failed to initialize autosuspend");
38         return -1;
39     }
40 
41     ALOGV("autosuspend initialized");
42     return 0;
43 }
44 
autosuspend_enable(void)45 int autosuspend_enable(void) {
46     int ret;
47 
48     ret = autosuspend_init();
49     if (ret) {
50         return ret;
51     }
52 
53     ALOGV("autosuspend_enable");
54 
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 
autosuspend_disable(void)68 int autosuspend_disable(void) {
69     int ret;
70 
71     ret = autosuspend_init();
72     if (ret) {
73         return ret;
74     }
75 
76     ALOGV("autosuspend_disable");
77 
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 }
90 
autosuspend_force_suspend(int timeout_ms)91 int 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 
autosuspend_set_wakeup_callback(void (* func)(bool success))104 void 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 }
116