| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 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 | #define LOG_TAG "healthd" | 
|  | 18 | #define KLOG_LEVEL 6 | 
|  | 19 |  | 
| Yabin Cui | e98e177 | 2016-02-17 12:21:34 -0800 | [diff] [blame] | 20 | #include <healthd/healthd.h> | 
|  | 21 | #include <healthd/BatteryMonitor.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <errno.h> | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 24 | #include <libgen.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 25 | #include <stdio.h> | 
|  | 26 | #include <stdlib.h> | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 27 | #include <string.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 28 | #include <unistd.h> | 
|  | 29 | #include <batteryservice/BatteryService.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 30 | #include <cutils/klog.h> | 
|  | 31 | #include <cutils/uevent.h> | 
|  | 32 | #include <sys/epoll.h> | 
|  | 33 | #include <sys/timerfd.h> | 
| Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 34 | #include <utils/Errors.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | using namespace android; | 
|  | 37 |  | 
| Nick Vaccaro | 1f1a6fd | 2016-10-21 19:16:40 -0700 | [diff] [blame] | 38 | #ifndef BOARD_PERIODIC_CHORES_INTERVAL_FAST | 
|  | 39 | // Periodic chores fast interval in seconds | 
|  | 40 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1) | 
|  | 41 | #else | 
|  | 42 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (BOARD_PERIODIC_CHORES_INTERVAL_FAST) | 
|  | 43 | #endif | 
|  | 44 |  | 
|  | 45 | #ifndef BOARD_PERIODIC_CHORES_INTERVAL_SLOW | 
|  | 46 | // Periodic chores fast interval in seconds | 
|  | 47 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10) | 
|  | 48 | #else | 
|  | 49 | #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (BOARD_PERIODIC_CHORES_INTERVAL_SLOW) | 
|  | 50 | #endif | 
| Todd Poynor | 9face5c | 2013-08-08 12:24:53 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | static struct healthd_config healthd_config = { | 
|  | 53 | .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST, | 
|  | 54 | .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW, | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 55 | .batteryStatusPath = String8(String8::kEmptyString), | 
|  | 56 | .batteryHealthPath = String8(String8::kEmptyString), | 
|  | 57 | .batteryPresentPath = String8(String8::kEmptyString), | 
|  | 58 | .batteryCapacityPath = String8(String8::kEmptyString), | 
|  | 59 | .batteryVoltagePath = String8(String8::kEmptyString), | 
|  | 60 | .batteryTemperaturePath = String8(String8::kEmptyString), | 
|  | 61 | .batteryTechnologyPath = String8(String8::kEmptyString), | 
|  | 62 | .batteryCurrentNowPath = String8(String8::kEmptyString), | 
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 63 | .batteryCurrentAvgPath = String8(String8::kEmptyString), | 
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 64 | .batteryChargeCounterPath = String8(String8::kEmptyString), | 
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 65 | .batteryFullChargePath = String8(String8::kEmptyString), | 
|  | 66 | .batteryCycleCountPath = String8(String8::kEmptyString), | 
| Todd Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 67 | .energyCounter = NULL, | 
| Ruchi Kandoi | a84b1f6 | 2014-10-21 18:24:11 -0700 | [diff] [blame] | 68 | .boot_min_cap = 0, | 
| Ruchi Kandoi | bdf11c7 | 2014-09-25 19:44:42 -0700 | [diff] [blame] | 69 | .screen_on = NULL, | 
| Todd Poynor | 9face5c | 2013-08-08 12:24:53 -0700 | [diff] [blame] | 70 | }; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 71 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 72 | static int eventct; | 
|  | 73 | static int epollfd; | 
|  | 74 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 75 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" | 
|  | 76 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 77 | // epoll_create() parameter is actually unused | 
|  | 78 | #define MAX_EPOLL_EVENTS 40 | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 79 | static int uevent_fd; | 
|  | 80 | static int wakealarm_fd; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 81 |  | 
|  | 82 | // -1 for no epoll timeout | 
|  | 83 | static int awake_poll_interval = -1; | 
|  | 84 |  | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 85 | static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | static BatteryMonitor* gBatteryMonitor; | 
|  | 88 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 89 | struct healthd_mode_ops *healthd_mode_ops; | 
|  | 90 |  | 
|  | 91 | // Android mode | 
|  | 92 |  | 
|  | 93 | extern void healthd_mode_android_init(struct healthd_config *config); | 
|  | 94 | extern int healthd_mode_android_preparetowait(void); | 
|  | 95 | extern void healthd_mode_android_battery_update( | 
|  | 96 | struct android::BatteryProperties *props); | 
|  | 97 |  | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 98 | // Charger mode | 
|  | 99 |  | 
|  | 100 | extern void healthd_mode_charger_init(struct healthd_config *config); | 
|  | 101 | extern int healthd_mode_charger_preparetowait(void); | 
|  | 102 | extern void healthd_mode_charger_heartbeat(void); | 
|  | 103 | extern void healthd_mode_charger_battery_update( | 
|  | 104 | struct android::BatteryProperties *props); | 
|  | 105 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 106 | // NOPs for modes that need no special action | 
|  | 107 |  | 
|  | 108 | static void healthd_mode_nop_init(struct healthd_config *config); | 
|  | 109 | static int healthd_mode_nop_preparetowait(void); | 
|  | 110 | static void healthd_mode_nop_heartbeat(void); | 
|  | 111 | static void healthd_mode_nop_battery_update( | 
|  | 112 | struct android::BatteryProperties *props); | 
|  | 113 |  | 
|  | 114 | static struct healthd_mode_ops android_ops = { | 
|  | 115 | .init = healthd_mode_android_init, | 
|  | 116 | .preparetowait = healthd_mode_android_preparetowait, | 
|  | 117 | .heartbeat = healthd_mode_nop_heartbeat, | 
|  | 118 | .battery_update = healthd_mode_android_battery_update, | 
|  | 119 | }; | 
|  | 120 |  | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 121 | static struct healthd_mode_ops charger_ops = { | 
| Todd Poynor | 7c5a3e1 | 2016-02-12 19:53:15 -0800 | [diff] [blame] | 122 | #ifdef CHARGER_NO_UI | 
|  | 123 | .init = healthd_mode_nop_init, | 
|  | 124 | .preparetowait = healthd_mode_nop_preparetowait, | 
|  | 125 | .heartbeat = healthd_mode_nop_heartbeat, | 
|  | 126 | .battery_update = healthd_mode_nop_battery_update, | 
|  | 127 | #else | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 128 | .init = healthd_mode_charger_init, | 
|  | 129 | .preparetowait = healthd_mode_charger_preparetowait, | 
|  | 130 | .heartbeat = healthd_mode_charger_heartbeat, | 
|  | 131 | .battery_update = healthd_mode_charger_battery_update, | 
| Todd Poynor | 7c5a3e1 | 2016-02-12 19:53:15 -0800 | [diff] [blame] | 132 | #endif | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 135 | static struct healthd_mode_ops recovery_ops = { | 
|  | 136 | .init = healthd_mode_nop_init, | 
|  | 137 | .preparetowait = healthd_mode_nop_preparetowait, | 
|  | 138 | .heartbeat = healthd_mode_nop_heartbeat, | 
|  | 139 | .battery_update = healthd_mode_nop_battery_update, | 
|  | 140 | }; | 
|  | 141 |  | 
| Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 142 | static void healthd_mode_nop_init(struct healthd_config* /*config*/) { | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
|  | 145 | static int healthd_mode_nop_preparetowait(void) { | 
|  | 146 | return -1; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | static void healthd_mode_nop_heartbeat(void) { | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | static void healthd_mode_nop_battery_update( | 
| Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 153 | struct android::BatteryProperties* /*props*/) { | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 154 | } | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 155 |  | 
| Tim Murray | e89ea5e | 2016-10-18 16:35:15 -0700 | [diff] [blame] | 156 | int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) { | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 157 | struct epoll_event ev; | 
|  | 158 |  | 
| Tim Murray | e89ea5e | 2016-10-18 16:35:15 -0700 | [diff] [blame] | 159 | ev.events = EPOLLIN; | 
|  | 160 |  | 
|  | 161 | if (wakeup == EVENT_WAKEUP_FD) | 
|  | 162 | ev.events |= EPOLLWAKEUP; | 
|  | 163 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 164 | ev.data.ptr = (void *)handler; | 
|  | 165 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) { | 
|  | 166 | KLOG_ERROR(LOG_TAG, | 
|  | 167 | "epoll_ctl failed; errno=%d\n", errno); | 
|  | 168 | return -1; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | eventct++; | 
|  | 172 | return 0; | 
|  | 173 | } | 
|  | 174 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 175 | static void wakealarm_set_interval(int interval) { | 
|  | 176 | struct itimerspec itval; | 
|  | 177 |  | 
|  | 178 | if (wakealarm_fd == -1) | 
|  | 179 | return; | 
|  | 180 |  | 
|  | 181 | wakealarm_wake_interval = interval; | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 182 |  | 
|  | 183 | if (interval == -1) | 
|  | 184 | interval = 0; | 
|  | 185 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 186 | itval.it_interval.tv_sec = interval; | 
|  | 187 | itval.it_interval.tv_nsec = 0; | 
|  | 188 | itval.it_value.tv_sec = interval; | 
|  | 189 | itval.it_value.tv_nsec = 0; | 
|  | 190 |  | 
|  | 191 | if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1) | 
|  | 192 | KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n"); | 
|  | 193 | } | 
|  | 194 |  | 
| Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 195 | status_t healthd_get_property(int id, struct BatteryProperty *val) { | 
|  | 196 | return gBatteryMonitor->getProperty(id, val); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | void healthd_battery_update(void) { | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 200 | // Fast wake interval when on charger (watch for overheat); | 
|  | 201 | // slow wake interval when on battery (watch for drained battery). | 
|  | 202 |  | 
|  | 203 | int new_wake_interval = gBatteryMonitor->update() ? | 
| Todd Poynor | 9face5c | 2013-08-08 12:24:53 -0700 | [diff] [blame] | 204 | healthd_config.periodic_chores_interval_fast : | 
|  | 205 | healthd_config.periodic_chores_interval_slow; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 206 |  | 
|  | 207 | if (new_wake_interval != wakealarm_wake_interval) | 
|  | 208 | wakealarm_set_interval(new_wake_interval); | 
|  | 209 |  | 
|  | 210 | // During awake periods poll at fast rate.  If wake alarm is set at fast | 
|  | 211 | // rate then just use the alarm; if wake alarm is set at slow rate then | 
|  | 212 | // poll at fast rate while awake and let alarm wake up at slow rate when | 
|  | 213 | // asleep. | 
|  | 214 |  | 
| Todd Poynor | 9face5c | 2013-08-08 12:24:53 -0700 | [diff] [blame] | 215 | if (healthd_config.periodic_chores_interval_fast == -1) | 
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 216 | awake_poll_interval = -1; | 
|  | 217 | else | 
|  | 218 | awake_poll_interval = | 
| Todd Poynor | 9face5c | 2013-08-08 12:24:53 -0700 | [diff] [blame] | 219 | new_wake_interval == healthd_config.periodic_chores_interval_fast ? | 
|  | 220 | -1 : healthd_config.periodic_chores_interval_fast * 1000; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 223 | void healthd_dump_battery_state(int fd) { | 
|  | 224 | gBatteryMonitor->dumpState(fd); | 
|  | 225 | fsync(fd); | 
|  | 226 | } | 
|  | 227 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 228 | static void periodic_chores() { | 
| Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 229 | healthd_battery_update(); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 230 | } | 
|  | 231 |  | 
| Ruchi Kandoi | 4614f50 | 2014-06-20 11:46:40 -0700 | [diff] [blame] | 232 | #define UEVENT_MSG_LEN 2048 | 
| Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 233 | static void uevent_event(uint32_t /*epevents*/) { | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 234 | char msg[UEVENT_MSG_LEN+2]; | 
|  | 235 | char *cp; | 
|  | 236 | int n; | 
|  | 237 |  | 
|  | 238 | n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN); | 
|  | 239 | if (n <= 0) | 
|  | 240 | return; | 
|  | 241 | if (n >= UEVENT_MSG_LEN)   /* overflow -- discard */ | 
|  | 242 | return; | 
|  | 243 |  | 
|  | 244 | msg[n] = '\0'; | 
|  | 245 | msg[n+1] = '\0'; | 
|  | 246 | cp = msg; | 
|  | 247 |  | 
|  | 248 | while (*cp) { | 
|  | 249 | if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) { | 
| Todd Poynor | 7b27f27 | 2013-09-06 15:14:24 -0700 | [diff] [blame] | 250 | healthd_battery_update(); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 251 | break; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | /* advance to after the next \0 */ | 
|  | 255 | while (*cp++) | 
|  | 256 | ; | 
|  | 257 | } | 
|  | 258 | } | 
|  | 259 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 260 | static void uevent_init(void) { | 
|  | 261 | uevent_fd = uevent_open_socket(64*1024, true); | 
|  | 262 |  | 
|  | 263 | if (uevent_fd < 0) { | 
|  | 264 | KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n"); | 
|  | 265 | return; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | fcntl(uevent_fd, F_SETFL, O_NONBLOCK); | 
| Tim Murray | e89ea5e | 2016-10-18 16:35:15 -0700 | [diff] [blame] | 269 | if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD)) | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 270 | KLOG_ERROR(LOG_TAG, | 
|  | 271 | "register for uevent events failed\n"); | 
|  | 272 | } | 
|  | 273 |  | 
| Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 274 | static void wakealarm_event(uint32_t /*epevents*/) { | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 275 | unsigned long long wakeups; | 
|  | 276 |  | 
|  | 277 | if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) { | 
|  | 278 | KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n"); | 
|  | 279 | return; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | periodic_chores(); | 
|  | 283 | } | 
|  | 284 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 285 | static void wakealarm_init(void) { | 
|  | 286 | wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK); | 
|  | 287 | if (wakealarm_fd == -1) { | 
|  | 288 | KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n"); | 
|  | 289 | return; | 
|  | 290 | } | 
|  | 291 |  | 
| Tim Murray | e89ea5e | 2016-10-18 16:35:15 -0700 | [diff] [blame] | 292 | if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD)) | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 293 | KLOG_ERROR(LOG_TAG, | 
|  | 294 | "Registration of wakealarm event failed\n"); | 
|  | 295 |  | 
| Todd Poynor | 9face5c | 2013-08-08 12:24:53 -0700 | [diff] [blame] | 296 | wakealarm_set_interval(healthd_config.periodic_chores_interval_fast); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 297 | } | 
|  | 298 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 299 | static void healthd_mainloop(void) { | 
| Nick Vaccaro | 6f83149 | 2016-10-26 19:33:48 -0700 | [diff] [blame] | 300 | int nevents = 0; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 301 | while (1) { | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 302 | struct epoll_event events[eventct]; | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 303 | int timeout = awake_poll_interval; | 
|  | 304 | int mode_timeout; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 305 |  | 
| Nick Vaccaro | 6f83149 | 2016-10-26 19:33:48 -0700 | [diff] [blame] | 306 | /* Don't wait for first timer timeout to run periodic chores */ | 
|  | 307 | if (!nevents) | 
|  | 308 | periodic_chores(); | 
|  | 309 |  | 
|  | 310 | healthd_mode_ops->heartbeat(); | 
|  | 311 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 312 | mode_timeout = healthd_mode_ops->preparetowait(); | 
|  | 313 | if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout)) | 
|  | 314 | timeout = mode_timeout; | 
|  | 315 | nevents = epoll_wait(epollfd, events, eventct, timeout); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 316 | if (nevents == -1) { | 
|  | 317 | if (errno == EINTR) | 
|  | 318 | continue; | 
|  | 319 | KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n"); | 
|  | 320 | break; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | for (int n = 0; n < nevents; ++n) { | 
|  | 324 | if (events[n].data.ptr) | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 325 | (*(void (*)(int))events[n].data.ptr)(events[n].events); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 326 | } | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | return; | 
|  | 330 | } | 
|  | 331 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 332 | static int healthd_init() { | 
|  | 333 | epollfd = epoll_create(MAX_EPOLL_EVENTS); | 
|  | 334 | if (epollfd == -1) { | 
|  | 335 | KLOG_ERROR(LOG_TAG, | 
|  | 336 | "epoll_create failed; errno=%d\n", | 
|  | 337 | errno); | 
|  | 338 | return -1; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | healthd_board_init(&healthd_config); | 
| Ruchi Kandoi | bdf11c7 | 2014-09-25 19:44:42 -0700 | [diff] [blame] | 342 | healthd_mode_ops->init(&healthd_config); | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 343 | wakealarm_init(); | 
|  | 344 | uevent_init(); | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 345 | gBatteryMonitor = new BatteryMonitor(); | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 346 | gBatteryMonitor->init(&healthd_config); | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 347 | return 0; | 
|  | 348 | } | 
|  | 349 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 350 | int main(int argc, char **argv) { | 
|  | 351 | int ch; | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 352 | int ret; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 353 |  | 
|  | 354 | klog_set_level(KLOG_LEVEL); | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 355 | healthd_mode_ops = &android_ops; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 356 |  | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 357 | if (!strcmp(basename(argv[0]), "charger")) { | 
|  | 358 | healthd_mode_ops = &charger_ops; | 
|  | 359 | } else { | 
|  | 360 | while ((ch = getopt(argc, argv, "cr")) != -1) { | 
|  | 361 | switch (ch) { | 
|  | 362 | case 'c': | 
|  | 363 | healthd_mode_ops = &charger_ops; | 
|  | 364 | break; | 
|  | 365 | case 'r': | 
|  | 366 | healthd_mode_ops = &recovery_ops; | 
|  | 367 | break; | 
|  | 368 | case '?': | 
|  | 369 | default: | 
| Todd Poynor | 1c3d3cd | 2013-11-21 11:28:12 -0800 | [diff] [blame] | 370 | KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", | 
|  | 371 | optopt); | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 372 | exit(1); | 
|  | 373 | } | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 377 | ret = healthd_init(); | 
|  | 378 | if (ret) { | 
|  | 379 | KLOG_ERROR("Initialization failed, exiting\n"); | 
|  | 380 | exit(2); | 
|  | 381 | } | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 382 |  | 
|  | 383 | healthd_mainloop(); | 
| Todd Poynor | 98c23d8 | 2013-09-09 20:02:55 -0700 | [diff] [blame] | 384 | KLOG_ERROR("Main loop terminated, exiting\n"); | 
|  | 385 | return 3; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 386 | } |