1 /*
2  * Copyright (C) 2013 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 #ifndef _HEALTHD_H_
18 #define _HEALTHD_H_
19 
20 #include <batteryservice/BatteryService.h>
21 #include <sys/types.h>
22 #include <utils/Errors.h>
23 #include <utils/String8.h>
24 
25 #include <vector>
26 
27 // periodic_chores_interval_fast, periodic_chores_interval_slow: intervals at
28 // which healthd wakes up to poll health state and perform periodic chores,
29 // in units of seconds:
30 //
31 //    periodic_chores_interval_fast is used while the device is not in
32 //    suspend, or in suspend and connected to a charger (to watch for battery
33 //    overheat due to charging).  The default value is 60 (1 minute).  Value
34 //    -1 turns off periodic chores (and wakeups) in these conditions.
35 //
36 //    periodic_chores_interval_slow is used when the device is in suspend and
37 //    not connected to a charger (to watch for a battery drained to zero
38 //    remaining capacity).  The default value is 600 (10 minutes).  Value -1
39 //    tuns off periodic chores (and wakeups) in these conditions.
40 //
41 // power_supply sysfs attribute file paths.  Set these to specific paths
42 // to use for the associated battery parameters.  healthd will search for
43 // appropriate power_supply attribute files to use for any paths left empty:
44 //
45 //    batteryStatusPath: charging status (POWER_SUPPLY_PROP_STATUS)
46 //    batteryHealthPath: battery health (POWER_SUPPLY_PROP_HEALTH)
47 //    batteryPresentPath: battery present (POWER_SUPPLY_PROP_PRESENT)
48 //    batteryCapacityPath: remaining capacity (POWER_SUPPLY_PROP_CAPACITY)
49 //    batteryVoltagePath: battery voltage (POWER_SUPPLY_PROP_VOLTAGE_NOW)
50 //    batteryTemperaturePath: battery temperature (POWER_SUPPLY_PROP_TEMP)
51 //    batteryTechnologyPath: battery technology (POWER_SUPPLY_PROP_TECHNOLOGY)
52 //    batteryCurrentNowPath: battery current (POWER_SUPPLY_PROP_CURRENT_NOW)
53 //    batteryChargeCounterPath: battery accumulated charge
54 //                                         (POWER_SUPPLY_PROP_CHARGE_COUNTER)
55 
56 struct healthd_config {
57     int periodic_chores_interval_fast;
58     int periodic_chores_interval_slow;
59 
60     android::String8 batteryStatusPath;
61     android::String8 batteryHealthPath;
62     android::String8 batteryPresentPath;
63     android::String8 batteryCapacityPath;
64     android::String8 batteryVoltagePath;
65     android::String8 batteryTemperaturePath;
66     android::String8 batteryTechnologyPath;
67     android::String8 batteryCurrentNowPath;
68     android::String8 batteryCurrentAvgPath;
69     android::String8 batteryChargeCounterPath;
70     android::String8 batteryFullChargePath;
71     android::String8 batteryCycleCountPath;
72     android::String8 batteryCapacityLevelPath;
73     android::String8 batteryChargeTimeToFullNowPath;
74     android::String8 batteryFullChargeDesignCapacityUahPath;
75     android::String8 batteryStateOfHealthPath;
76     android::String8 batteryHealthStatusPath;
77     android::String8 batteryManufacturingDatePath;
78     android::String8 batteryFirstUsageDatePath;
79     android::String8 chargingStatePath;
80     android::String8 chargingPolicyPath;
81 
82     int (*energyCounter)(int64_t *);
83     int boot_min_cap;
84     bool (*screen_on)(android::BatteryProperties *props);
85     std::vector<android::String8> ignorePowerSupplyNames;
86 };
87 
88 enum EventWakeup {
89     EVENT_NO_WAKEUP_FD,
90     EVENT_WAKEUP_FD,
91 };
92 
93 // Global helper functions
94 
95 int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup = EVENT_NO_WAKEUP_FD);
96 
97 struct healthd_mode_ops {
98     void (*init)(struct healthd_config *config);
99     int (*preparetowait)(void);
100     void (*heartbeat)(void);
101     void (*battery_update)(struct android::BatteryProperties *props);
102 };
103 
104 extern struct healthd_mode_ops *healthd_mode_ops;
105 
106 // Charger mode
107 
108 void healthd_mode_charger_init(struct healthd_config *config);
109 int healthd_mode_charger_preparetowait(void);
110 void healthd_mode_charger_heartbeat(void);
111 void healthd_mode_charger_battery_update(
112     struct android::BatteryProperties *props);
113 
114 // The following are implemented in libhealthd_board to handle board-specific
115 // behavior.
116 //
117 // healthd_board_init() is called at startup time to modify healthd's
118 // configuration according to board-specific requirements.  config
119 // points to the healthd configuration values described above.  To use default
120 // values, this function can simply return without modifying the fields of the
121 // config parameter.
122 
123 void healthd_board_init(struct healthd_config *config);
124 
125 // Process updated battery property values.  This function is called when
126 // the kernel sends updated battery status via a uevent from the power_supply
127 // subsystem, or when updated values are polled by healthd, as for periodic
128 // poll of battery state.
129 //
130 // props are the battery properties read from the kernel.  These values may
131 // be modified in this call, prior to sending the modified values to the
132 // Android runtime.
133 //
134 // Return 0 to indicate the usual kernel log battery status heartbeat message
135 // is to be logged, or non-zero to prevent logging this information.
136 
137 int healthd_board_battery_update(struct android::BatteryProperties *props);
138 
139 #endif /* _HEALTHD_H_ */
140