blob: 66b961da57757b9d60ace264924136dada475724 [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
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 Poynor10b235e2013-08-07 15:25:14 -070020#include "healthd.h"
Todd Poynor752faf22013-06-12 13:25:59 -070021#include "BatteryMonitor.h"
22
23#include <errno.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070024#include <libgen.h>
Todd Poynor752faf22013-06-12 13:25:59 -070025#include <stdio.h>
26#include <stdlib.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070027#include <string.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
29#include <batteryservice/BatteryService.h>
Todd Poynor752faf22013-06-12 13:25:59 -070030#include <cutils/klog.h>
31#include <cutils/uevent.h>
32#include <sys/epoll.h>
33#include <sys/timerfd.h>
Todd Poynor7b27f272013-09-06 15:14:24 -070034#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070035
36using namespace android;
37
38// Periodic chores intervals in seconds
Todd Poynor10b235e2013-08-07 15:25:14 -070039#define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
40#define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
Todd Poynor9face5c2013-08-08 12:24:53 -070041
42static 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 Poynorf5d30122013-08-12 17:03:35 -070045 .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 Poynorbc102112013-08-27 18:11:49 -070053 .batteryCurrentAvgPath = String8(String8::kEmptyString),
Todd Poynorf5d30122013-08-12 17:03:35 -070054 .batteryChargeCounterPath = String8(String8::kEmptyString),
Todd Poynor9face5c2013-08-08 12:24:53 -070055};
Todd Poynor752faf22013-06-12 13:25:59 -070056
Todd Poynor98c23d82013-09-09 20:02:55 -070057static int eventct;
58static int epollfd;
59
Todd Poynor752faf22013-06-12 13:25:59 -070060#define POWER_SUPPLY_SUBSYSTEM "power_supply"
61
Todd Poynor98c23d82013-09-09 20:02:55 -070062// epoll_create() parameter is actually unused
63#define MAX_EPOLL_EVENTS 40
Todd Poynor752faf22013-06-12 13:25:59 -070064static int uevent_fd;
65static int wakealarm_fd;
66static int binder_fd;
67
68// -1 for no epoll timeout
69static int awake_poll_interval = -1;
70
Todd Poynor10b235e2013-08-07 15:25:14 -070071static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070072
73static BatteryMonitor* gBatteryMonitor;
74
Todd Poynorc7464c92013-09-10 12:40:00 -070075struct healthd_mode_ops *healthd_mode_ops;
76
77// Android mode
78
79extern void healthd_mode_android_init(struct healthd_config *config);
80extern int healthd_mode_android_preparetowait(void);
81extern void healthd_mode_android_battery_update(
82 struct android::BatteryProperties *props);
83
Todd Poynorfea5b4d2013-09-09 12:09:08 -070084// Charger mode
85
86extern void healthd_mode_charger_init(struct healthd_config *config);
87extern int healthd_mode_charger_preparetowait(void);
88extern void healthd_mode_charger_heartbeat(void);
89extern void healthd_mode_charger_battery_update(
90 struct android::BatteryProperties *props);
91
Todd Poynorc7464c92013-09-10 12:40:00 -070092// NOPs for modes that need no special action
93
94static void healthd_mode_nop_init(struct healthd_config *config);
95static int healthd_mode_nop_preparetowait(void);
96static void healthd_mode_nop_heartbeat(void);
97static void healthd_mode_nop_battery_update(
98 struct android::BatteryProperties *props);
99
100static struct healthd_mode_ops android_ops = {
101 .init = healthd_mode_android_init,
102 .preparetowait = healthd_mode_android_preparetowait,
103 .heartbeat = healthd_mode_nop_heartbeat,
104 .battery_update = healthd_mode_android_battery_update,
105};
106
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700107static struct healthd_mode_ops charger_ops = {
108 .init = healthd_mode_charger_init,
109 .preparetowait = healthd_mode_charger_preparetowait,
110 .heartbeat = healthd_mode_charger_heartbeat,
111 .battery_update = healthd_mode_charger_battery_update,
112};
113
Todd Poynorc7464c92013-09-10 12:40:00 -0700114static struct healthd_mode_ops recovery_ops = {
115 .init = healthd_mode_nop_init,
116 .preparetowait = healthd_mode_nop_preparetowait,
117 .heartbeat = healthd_mode_nop_heartbeat,
118 .battery_update = healthd_mode_nop_battery_update,
119};
120
121static void healthd_mode_nop_init(struct healthd_config *config) {
122}
123
124static int healthd_mode_nop_preparetowait(void) {
125 return -1;
126}
127
128static void healthd_mode_nop_heartbeat(void) {
129}
130
131static void healthd_mode_nop_battery_update(
132 struct android::BatteryProperties *props) {
133}
Todd Poynor752faf22013-06-12 13:25:59 -0700134
Todd Poynor98c23d82013-09-09 20:02:55 -0700135int healthd_register_event(int fd, void (*handler)(uint32_t)) {
136 struct epoll_event ev;
137
138 ev.events = EPOLLIN | EPOLLWAKEUP;
139 ev.data.ptr = (void *)handler;
140 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
141 KLOG_ERROR(LOG_TAG,
142 "epoll_ctl failed; errno=%d\n", errno);
143 return -1;
144 }
145
146 eventct++;
147 return 0;
148}
149
Todd Poynor752faf22013-06-12 13:25:59 -0700150static void wakealarm_set_interval(int interval) {
151 struct itimerspec itval;
152
153 if (wakealarm_fd == -1)
154 return;
155
156 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -0700157
158 if (interval == -1)
159 interval = 0;
160
Todd Poynor752faf22013-06-12 13:25:59 -0700161 itval.it_interval.tv_sec = interval;
162 itval.it_interval.tv_nsec = 0;
163 itval.it_value.tv_sec = interval;
164 itval.it_value.tv_nsec = 0;
165
166 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
167 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
168}
169
Todd Poynor7b27f272013-09-06 15:14:24 -0700170status_t healthd_get_property(int id, struct BatteryProperty *val) {
171 return gBatteryMonitor->getProperty(id, val);
172}
173
174void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700175 // Fast wake interval when on charger (watch for overheat);
176 // slow wake interval when on battery (watch for drained battery).
177
178 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700179 healthd_config.periodic_chores_interval_fast :
180 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700181
182 if (new_wake_interval != wakealarm_wake_interval)
183 wakealarm_set_interval(new_wake_interval);
184
185 // During awake periods poll at fast rate. If wake alarm is set at fast
186 // rate then just use the alarm; if wake alarm is set at slow rate then
187 // poll at fast rate while awake and let alarm wake up at slow rate when
188 // asleep.
189
Todd Poynor9face5c2013-08-08 12:24:53 -0700190 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700191 awake_poll_interval = -1;
192 else
193 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700194 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
195 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700196}
197
198static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700199 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700200}
201
Todd Poynor752faf22013-06-12 13:25:59 -0700202#define UEVENT_MSG_LEN 1024
Todd Poynor98c23d82013-09-09 20:02:55 -0700203static void uevent_event(uint32_t epevents) {
Todd Poynor752faf22013-06-12 13:25:59 -0700204 char msg[UEVENT_MSG_LEN+2];
205 char *cp;
206 int n;
207
208 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
209 if (n <= 0)
210 return;
211 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
212 return;
213
214 msg[n] = '\0';
215 msg[n+1] = '\0';
216 cp = msg;
217
218 while (*cp) {
219 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700220 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700221 break;
222 }
223
224 /* advance to after the next \0 */
225 while (*cp++)
226 ;
227 }
228}
229
Todd Poynor98c23d82013-09-09 20:02:55 -0700230static void uevent_init(void) {
231 uevent_fd = uevent_open_socket(64*1024, true);
232
233 if (uevent_fd < 0) {
234 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
235 return;
236 }
237
238 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
239 if (healthd_register_event(uevent_fd, uevent_event))
240 KLOG_ERROR(LOG_TAG,
241 "register for uevent events failed\n");
242}
243
244static void wakealarm_event(uint32_t epevents) {
245 unsigned long long wakeups;
246
247 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
248 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
249 return;
250 }
251
252 periodic_chores();
253}
254
Todd Poynor752faf22013-06-12 13:25:59 -0700255static void wakealarm_init(void) {
256 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
257 if (wakealarm_fd == -1) {
258 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
259 return;
260 }
261
Todd Poynor98c23d82013-09-09 20:02:55 -0700262 if (healthd_register_event(wakealarm_fd, wakealarm_event))
263 KLOG_ERROR(LOG_TAG,
264 "Registration of wakealarm event failed\n");
265
Todd Poynor9face5c2013-08-08 12:24:53 -0700266 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700267}
268
Todd Poynor98c23d82013-09-09 20:02:55 -0700269static void healthd_mainloop(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700270 while (1) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700271 struct epoll_event events[eventct];
Todd Poynor752faf22013-06-12 13:25:59 -0700272 int nevents;
Todd Poynorc7464c92013-09-10 12:40:00 -0700273 int timeout = awake_poll_interval;
274 int mode_timeout;
Todd Poynor752faf22013-06-12 13:25:59 -0700275
Todd Poynorc7464c92013-09-10 12:40:00 -0700276 mode_timeout = healthd_mode_ops->preparetowait();
277 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
278 timeout = mode_timeout;
279 nevents = epoll_wait(epollfd, events, eventct, timeout);
Todd Poynor752faf22013-06-12 13:25:59 -0700280
281 if (nevents == -1) {
282 if (errno == EINTR)
283 continue;
284 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
285 break;
286 }
287
288 for (int n = 0; n < nevents; ++n) {
289 if (events[n].data.ptr)
Todd Poynor98c23d82013-09-09 20:02:55 -0700290 (*(void (*)(int))events[n].data.ptr)(events[n].events);
Todd Poynor752faf22013-06-12 13:25:59 -0700291 }
Todd Poynorff9ec2d2013-09-09 14:30:55 -0700292
293 if (!nevents)
294 periodic_chores();
Todd Poynorc7464c92013-09-10 12:40:00 -0700295
296 healthd_mode_ops->heartbeat();
Todd Poynor752faf22013-06-12 13:25:59 -0700297 }
298
299 return;
300}
301
Todd Poynor98c23d82013-09-09 20:02:55 -0700302static int healthd_init() {
303 epollfd = epoll_create(MAX_EPOLL_EVENTS);
304 if (epollfd == -1) {
305 KLOG_ERROR(LOG_TAG,
306 "epoll_create failed; errno=%d\n",
307 errno);
308 return -1;
309 }
310
Todd Poynorc7464c92013-09-10 12:40:00 -0700311 healthd_mode_ops->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700312 healthd_board_init(&healthd_config);
313 wakealarm_init();
314 uevent_init();
Todd Poynor98c23d82013-09-09 20:02:55 -0700315 gBatteryMonitor = new BatteryMonitor();
Todd Poynorc7464c92013-09-10 12:40:00 -0700316 gBatteryMonitor->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700317 return 0;
318}
319
Todd Poynor752faf22013-06-12 13:25:59 -0700320int main(int argc, char **argv) {
321 int ch;
Todd Poynor98c23d82013-09-09 20:02:55 -0700322 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700323
324 klog_set_level(KLOG_LEVEL);
Todd Poynorc7464c92013-09-10 12:40:00 -0700325 healthd_mode_ops = &android_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700326
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700327 if (!strcmp(basename(argv[0]), "charger")) {
328 healthd_mode_ops = &charger_ops;
329 } else {
330 while ((ch = getopt(argc, argv, "cr")) != -1) {
331 switch (ch) {
332 case 'c':
333 healthd_mode_ops = &charger_ops;
334 break;
335 case 'r':
336 healthd_mode_ops = &recovery_ops;
337 break;
338 case '?':
339 default:
340 KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
341 exit(1);
342 }
Todd Poynor752faf22013-06-12 13:25:59 -0700343 }
344 }
345
Todd Poynor98c23d82013-09-09 20:02:55 -0700346 ret = healthd_init();
347 if (ret) {
348 KLOG_ERROR("Initialization failed, exiting\n");
349 exit(2);
350 }
Todd Poynor752faf22013-06-12 13:25:59 -0700351
352 healthd_mainloop();
Todd Poynor98c23d82013-09-09 20:02:55 -0700353 KLOG_ERROR("Main loop terminated, exiting\n");
354 return 3;
Todd Poynor752faf22013-06-12 13:25:59 -0700355}