Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 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 | |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "libsuspend" |
| 18 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 21 | #include <log/log.h> |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 22 | |
| 23 | #include <suspend/autosuspend.h> |
| 24 | |
| 25 | #include "autosuspend_ops.h" |
| 26 | |
| 27 | static struct autosuspend_ops *autosuspend_ops; |
| 28 | static bool autosuspend_enabled; |
| 29 | static bool autosuspend_inited; |
| 30 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame^] | 31 | static int autosuspend_init(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 32 | if (autosuspend_inited) { |
| 33 | return 0; |
| 34 | } |
| 35 | |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 36 | autosuspend_ops = autosuspend_wakeup_count_init(); |
| 37 | if (autosuspend_ops) { |
| 38 | goto out; |
| 39 | } |
| 40 | |
| 41 | if (!autosuspend_ops) { |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 42 | ALOGE("failed to initialize autosuspend"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | out: |
Kyle Russell | a26b4ca | 2012-11-19 16:29:58 -0500 | [diff] [blame] | 47 | autosuspend_inited = true; |
| 48 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 49 | ALOGV("autosuspend initialized"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame^] | 53 | int autosuspend_enable(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 54 | int ret; |
| 55 | |
| 56 | ret = autosuspend_init(); |
| 57 | if (ret) { |
| 58 | return ret; |
| 59 | } |
| 60 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 61 | ALOGV("autosuspend_enable"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 62 | |
| 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 Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame^] | 76 | int autosuspend_disable(void) { |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 77 | int ret; |
| 78 | |
| 79 | ret = autosuspend_init(); |
| 80 | if (ret) { |
| 81 | return ret; |
| 82 | } |
| 83 | |
Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 84 | ALOGV("autosuspend_disable"); |
Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 85 | |
| 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 Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame^] | 98 | |
| 99 | void 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 | } |