1526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil/*
2526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * Copyright (C) 2016 The Android Open Source Project
3526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil *
4526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * Licensed under the Apache License, Version 2.0 (the "License");
5526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * you may not use this file except in compliance with the License.
6526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * You may obtain a copy of the License at
7526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil *
8526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil *      http://www.apache.org/licenses/LICENSE-2.0
9526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil *
10526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * Unless required by applicable law or agreed to in writing, software
11526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * distributed under the License is distributed on an "AS IS" BASIS,
12526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * See the License for the specific language governing permissions and
14526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil * limitations under the License.
15526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil */
16526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
17526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#define LOG_TAG "charger"
18526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#define KLOG_LEVEL 6
19526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
2010c2b4013a2a33d1ab91fdffc6c7f98a48a63de9Yifan Hong#include <health2/Health.h>
21526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#include <healthd/healthd.h>
22526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
23526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#include <stdlib.h>
24526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#include <string.h>
25526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#include <cutils/klog.h>
26526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
27526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilusing namespace android;
28526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
29526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil// main healthd loop
30526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilextern int healthd_main(void);
31526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
32526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil// Charger mode
33526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
34526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilextern void healthd_mode_charger_init(struct healthd_config *config);
35526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilextern int healthd_mode_charger_preparetowait(void);
36526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilextern void healthd_mode_charger_heartbeat(void);
37526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilextern void healthd_mode_charger_battery_update(
38526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    struct android::BatteryProperties *props);
39526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
40526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil// NOPs for modes that need no special action
41526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
42526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic void healthd_mode_nop_init(struct healthd_config *config);
43526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic int healthd_mode_nop_preparetowait(void);
44526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic void healthd_mode_nop_heartbeat(void);
45526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic void healthd_mode_nop_battery_update(
46526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    struct android::BatteryProperties *props);
47526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
48526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic struct healthd_mode_ops healthd_nops = {
49526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .init = healthd_mode_nop_init,
50526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .preparetowait = healthd_mode_nop_preparetowait,
51526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .heartbeat = healthd_mode_nop_heartbeat,
52526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .battery_update = healthd_mode_nop_battery_update,
53526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil};
54526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
55526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#ifdef CHARGER_NO_UI
56526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic struct healthd_mode_ops charger_ops = healthd_nops;
57526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#else
58526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic struct healthd_mode_ops charger_ops = {
59526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .init = healthd_mode_charger_init,
60526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .preparetowait = healthd_mode_charger_preparetowait,
61526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .heartbeat = healthd_mode_charger_heartbeat,
62526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    .battery_update = healthd_mode_charger_battery_update,
63526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil};
64526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil#endif
65526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
6610c2b4013a2a33d1ab91fdffc6c7f98a48a63de9Yifan Hongstatic void healthd_mode_nop_init(struct healthd_config* config) {
6710c2b4013a2a33d1ab91fdffc6c7f98a48a63de9Yifan Hong    using android::hardware::health::V2_0::implementation::Health;
6810c2b4013a2a33d1ab91fdffc6c7f98a48a63de9Yifan Hong    Health::initInstance(config);
69526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil}
70526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
71526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic int healthd_mode_nop_preparetowait(void) {
72526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    return -1;
73526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil}
74526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
75526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic void healthd_mode_nop_heartbeat(void) {
76526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil}
77526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
78526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patilstatic void healthd_mode_nop_battery_update(
79526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    struct android::BatteryProperties* /*props*/) {
80526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil}
81526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
82b7cd45f877177737958401776a440c9fd6361f46Yifan Hongint healthd_charger_main(int argc, char** argv) {
83526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    int ch;
84526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
85526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    healthd_mode_ops = &charger_ops;
86526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
87526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    while ((ch = getopt(argc, argv, "cr")) != -1) {
88526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil        switch (ch) {
89526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil            case 'c':
90526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                // -c is now a noop
91526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                break;
92526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil            case 'r':
93526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                // force nops for recovery
94526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                healthd_mode_ops = &healthd_nops;
95526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                break;
96526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil            case '?':
97526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil            default:
98526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                KLOG_ERROR(LOG_TAG, "Unrecognized charger option: %c\n",
99526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                        optopt);
100526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil                exit(1);
101526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil        }
102526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    }
103526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil
104526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil    return healthd_main();
105526f8cf339f6c215d2d89924824b674cb99cfcc9Sandeep Patil}
106b7cd45f877177737958401776a440c9fd6361f46Yifan Hong
107b7cd45f877177737958401776a440c9fd6361f46Yifan Hong#ifndef CHARGER_TEST
108b7cd45f877177737958401776a440c9fd6361f46Yifan Hongint main(int argc, char** argv) {
109b7cd45f877177737958401776a440c9fd6361f46Yifan Hong    return healthd_charger_main(argc, argv);
110b7cd45f877177737958401776a440c9fd6361f46Yifan Hong}
111b7cd45f877177737958401776a440c9fd6361f46Yifan Hong#endif
112