Home | History | Annotate | Download | only in healthd
      1 /*
      2  * Copyright (C) 2016 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 "charger"
     18 #define KLOG_LEVEL 6
     19 
     20 #include <healthd/healthd.h>
     21 
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include <cutils/klog.h>
     25 
     26 using namespace android;
     27 
     28 // main healthd loop
     29 extern int healthd_main(void);
     30 
     31 // Charger mode
     32 
     33 extern void healthd_mode_charger_init(struct healthd_config *config);
     34 extern int healthd_mode_charger_preparetowait(void);
     35 extern void healthd_mode_charger_heartbeat(void);
     36 extern void healthd_mode_charger_battery_update(
     37     struct android::BatteryProperties *props);
     38 
     39 // NOPs for modes that need no special action
     40 
     41 static void healthd_mode_nop_init(struct healthd_config *config);
     42 static int healthd_mode_nop_preparetowait(void);
     43 static void healthd_mode_nop_heartbeat(void);
     44 static void healthd_mode_nop_battery_update(
     45     struct android::BatteryProperties *props);
     46 
     47 static struct healthd_mode_ops healthd_nops = {
     48     .init = healthd_mode_nop_init,
     49     .preparetowait = healthd_mode_nop_preparetowait,
     50     .heartbeat = healthd_mode_nop_heartbeat,
     51     .battery_update = healthd_mode_nop_battery_update,
     52 };
     53 
     54 #ifdef CHARGER_NO_UI
     55 static struct healthd_mode_ops charger_ops = healthd_nops;
     56 #else
     57 static struct healthd_mode_ops charger_ops = {
     58     .init = healthd_mode_charger_init,
     59     .preparetowait = healthd_mode_charger_preparetowait,
     60     .heartbeat = healthd_mode_charger_heartbeat,
     61     .battery_update = healthd_mode_charger_battery_update,
     62 };
     63 #endif
     64 
     65 static void healthd_mode_nop_init(struct healthd_config* /*config*/) {
     66 }
     67 
     68 static int healthd_mode_nop_preparetowait(void) {
     69     return -1;
     70 }
     71 
     72 static void healthd_mode_nop_heartbeat(void) {
     73 }
     74 
     75 static void healthd_mode_nop_battery_update(
     76     struct android::BatteryProperties* /*props*/) {
     77 }
     78 
     79 int main(int argc, char **argv) {
     80     int ch;
     81 
     82     healthd_mode_ops = &charger_ops;
     83 
     84     while ((ch = getopt(argc, argv, "cr")) != -1) {
     85         switch (ch) {
     86             case 'c':
     87                 // -c is now a noop
     88                 break;
     89             case 'r':
     90                 // force nops for recovery
     91                 healthd_mode_ops = &healthd_nops;
     92                 break;
     93             case '?':
     94             default:
     95                 KLOG_ERROR(LOG_TAG, "Unrecognized charger option: %c\n",
     96                         optopt);
     97                 exit(1);
     98         }
     99     }
    100 
    101     return healthd_main();
    102 }
    103