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