Home | History | Annotate | Download | only in libsuspend
      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 #include <stdbool.h>
     18 
     19 #define LOG_TAG "libsuspend"
     20 #include <cutils/log.h>
     21 
     22 #include <suspend/autosuspend.h>
     23 
     24 #include "autosuspend_ops.h"
     25 
     26 static struct autosuspend_ops *autosuspend_ops;
     27 static bool autosuspend_enabled;
     28 static bool autosuspend_inited;
     29 
     30 static int autosuspend_init(void)
     31 {
     32     if (autosuspend_inited) {
     33         return 0;
     34     }
     35 
     36     autosuspend_ops = autosuspend_earlysuspend_init();
     37     if (autosuspend_ops) {
     38         goto out;
     39     }
     40 
     41     autosuspend_ops = autosuspend_autosleep_init();
     42     if (autosuspend_ops) {
     43         goto out;
     44     }
     45 
     46     autosuspend_ops = autosuspend_wakeup_count_init();
     47     if (autosuspend_ops) {
     48         goto out;
     49     }
     50 
     51     if (!autosuspend_ops) {
     52         ALOGE("failed to initialize autosuspend\n");
     53         return -1;
     54     }
     55 
     56 out:
     57     autosuspend_inited = true;
     58 
     59     ALOGV("autosuspend initialized\n");
     60     return 0;
     61 }
     62 
     63 int autosuspend_enable(void)
     64 {
     65     int ret;
     66 
     67     ret = autosuspend_init();
     68     if (ret) {
     69         return ret;
     70     }
     71 
     72     ALOGV("autosuspend_enable\n");
     73 
     74     if (autosuspend_enabled) {
     75         return 0;
     76     }
     77 
     78     ret = autosuspend_ops->enable();
     79     if (ret) {
     80         return ret;
     81     }
     82 
     83     autosuspend_enabled = true;
     84     return 0;
     85 }
     86 
     87 int autosuspend_disable(void)
     88 {
     89     int ret;
     90 
     91     ret = autosuspend_init();
     92     if (ret) {
     93         return ret;
     94     }
     95 
     96     ALOGV("autosuspend_disable\n");
     97 
     98     if (!autosuspend_enabled) {
     99         return 0;
    100     }
    101 
    102     ret = autosuspend_ops->disable();
    103     if (ret) {
    104         return ret;
    105     }
    106 
    107     autosuspend_enabled = false;
    108     return 0;
    109 }
    110