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