| 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 |  | 
| Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 27 | static struct autosuspend_ops* autosuspend_ops = NULL; | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 28 | static bool autosuspend_enabled; | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 29 |  | 
| Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 30 | static int autosuspend_init(void) { | 
| Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 31 |     if (autosuspend_ops != NULL) { | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 32 |         return 0; | 
 | 33 |     } | 
 | 34 |  | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 35 |     autosuspend_ops = autosuspend_wakeup_count_init(); | 
| Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 36 |     if (autosuspend_ops == NULL) { | 
| Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 37 |         ALOGE("failed to initialize autosuspend"); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 38 |         return -1; | 
 | 39 |     } | 
 | 40 |  | 
| Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 41 |     ALOGV("autosuspend initialized"); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 42 |     return 0; | 
 | 43 | } | 
 | 44 |  | 
| Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 45 | int autosuspend_enable(void) { | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 46 |     int ret; | 
 | 47 |  | 
 | 48 |     ret = autosuspend_init(); | 
 | 49 |     if (ret) { | 
 | 50 |         return ret; | 
 | 51 |     } | 
 | 52 |  | 
| Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 53 |     ALOGV("autosuspend_enable"); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 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 |  | 
| Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 68 | int autosuspend_disable(void) { | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 69 |     int ret; | 
 | 70 |  | 
 | 71 |     ret = autosuspend_init(); | 
 | 72 |     if (ret) { | 
 | 73 |         return ret; | 
 | 74 |     } | 
 | 75 |  | 
| Steve Paik | fea16e1 | 2017-12-14 17:31:58 -0800 | [diff] [blame] | 76 |     ALOGV("autosuspend_disable"); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 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 | } | 
| Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 90 |  | 
| Steve Paik | 10b149f | 2018-01-03 20:27:58 -0800 | [diff] [blame] | 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 |  | 
| Steve Paik | 2d44190 | 2017-12-15 13:26:28 -0800 | [diff] [blame] | 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 | } |