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