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