blob: 97ed51df54f42a127ca48f7c8bd55bd33345fb88 [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
Yabin Cuie98e1772016-02-17 12:21:34 -080020#include <healthd/healthd.h>
21#include <healthd/BatteryMonitor.h>
Todd Poynor752faf22013-06-12 13:25:59 -070022
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),
Ruchi Kandoicc338802015-08-24 13:01:16 -070055 .batteryFullChargePath = String8(String8::kEmptyString),
56 .batteryCycleCountPath = String8(String8::kEmptyString),
Todd Poynore14b37e2014-05-20 13:54:40 -070057 .energyCounter = NULL,
Ruchi Kandoia84b1f62014-10-21 18:24:11 -070058 .boot_min_cap = 0,
Ruchi Kandoibdf11c72014-09-25 19:44:42 -070059 .screen_on = NULL,
Todd Poynor9face5c2013-08-08 12:24:53 -070060};
Todd Poynor752faf22013-06-12 13:25:59 -070061
Todd Poynor98c23d82013-09-09 20:02:55 -070062static int eventct;
63static int epollfd;
64
Todd Poynor752faf22013-06-12 13:25:59 -070065#define POWER_SUPPLY_SUBSYSTEM "power_supply"
66
Todd Poynor98c23d82013-09-09 20:02:55 -070067// epoll_create() parameter is actually unused
68#define MAX_EPOLL_EVENTS 40
Todd Poynor752faf22013-06-12 13:25:59 -070069static int uevent_fd;
70static int wakealarm_fd;
Todd Poynor752faf22013-06-12 13:25:59 -070071
72// -1 for no epoll timeout
73static int awake_poll_interval = -1;
74
Todd Poynor10b235e2013-08-07 15:25:14 -070075static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070076
77static BatteryMonitor* gBatteryMonitor;
78
Todd Poynorc7464c92013-09-10 12:40:00 -070079struct healthd_mode_ops *healthd_mode_ops;
80
81// Android mode
82
83extern void healthd_mode_android_init(struct healthd_config *config);
84extern int healthd_mode_android_preparetowait(void);
85extern void healthd_mode_android_battery_update(
86 struct android::BatteryProperties *props);
87
Todd Poynorfea5b4d2013-09-09 12:09:08 -070088// Charger mode
89
90extern void healthd_mode_charger_init(struct healthd_config *config);
91extern int healthd_mode_charger_preparetowait(void);
92extern void healthd_mode_charger_heartbeat(void);
93extern void healthd_mode_charger_battery_update(
94 struct android::BatteryProperties *props);
95
Todd Poynorc7464c92013-09-10 12:40:00 -070096// NOPs for modes that need no special action
97
98static void healthd_mode_nop_init(struct healthd_config *config);
99static int healthd_mode_nop_preparetowait(void);
100static void healthd_mode_nop_heartbeat(void);
101static void healthd_mode_nop_battery_update(
102 struct android::BatteryProperties *props);
103
104static 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 Poynorfea5b4d2013-09-09 12:09:08 -0700111static struct healthd_mode_ops charger_ops = {
Todd Poynor7c5a3e12016-02-12 19:53:15 -0800112#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 Poynorfea5b4d2013-09-09 12:09:08 -0700118 .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 Poynor7c5a3e12016-02-12 19:53:15 -0800122#endif
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700123};
124
Todd Poynorc7464c92013-09-10 12:40:00 -0700125static 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 Salyzyn6f5b47f2014-05-15 15:00:59 -0700132static void healthd_mode_nop_init(struct healthd_config* /*config*/) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700133}
134
135static int healthd_mode_nop_preparetowait(void) {
136 return -1;
137}
138
139static void healthd_mode_nop_heartbeat(void) {
140}
141
142static void healthd_mode_nop_battery_update(
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700143 struct android::BatteryProperties* /*props*/) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700144}
Todd Poynor752faf22013-06-12 13:25:59 -0700145
Tim Murraye89ea5e2016-10-18 16:35:15 -0700146int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700147 struct epoll_event ev;
148
Tim Murraye89ea5e2016-10-18 16:35:15 -0700149 ev.events = EPOLLIN;
150
151 if (wakeup == EVENT_WAKEUP_FD)
152 ev.events |= EPOLLWAKEUP;
153
Todd Poynor98c23d82013-09-09 20:02:55 -0700154 ev.data.ptr = (void *)handler;
155 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
156 KLOG_ERROR(LOG_TAG,
157 "epoll_ctl failed; errno=%d\n", errno);
158 return -1;
159 }
160
161 eventct++;
162 return 0;
163}
164
Todd Poynor752faf22013-06-12 13:25:59 -0700165static void wakealarm_set_interval(int interval) {
166 struct itimerspec itval;
167
168 if (wakealarm_fd == -1)
169 return;
170
171 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -0700172
173 if (interval == -1)
174 interval = 0;
175
Todd Poynor752faf22013-06-12 13:25:59 -0700176 itval.it_interval.tv_sec = interval;
177 itval.it_interval.tv_nsec = 0;
178 itval.it_value.tv_sec = interval;
179 itval.it_value.tv_nsec = 0;
180
181 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
182 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
183}
184
Todd Poynor7b27f272013-09-06 15:14:24 -0700185status_t healthd_get_property(int id, struct BatteryProperty *val) {
186 return gBatteryMonitor->getProperty(id, val);
187}
188
189void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700190 // Fast wake interval when on charger (watch for overheat);
191 // slow wake interval when on battery (watch for drained battery).
192
193 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700194 healthd_config.periodic_chores_interval_fast :
195 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700196
197 if (new_wake_interval != wakealarm_wake_interval)
198 wakealarm_set_interval(new_wake_interval);
199
200 // During awake periods poll at fast rate. If wake alarm is set at fast
201 // rate then just use the alarm; if wake alarm is set at slow rate then
202 // poll at fast rate while awake and let alarm wake up at slow rate when
203 // asleep.
204
Todd Poynor9face5c2013-08-08 12:24:53 -0700205 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700206 awake_poll_interval = -1;
207 else
208 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700209 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
210 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700211}
212
Todd Poynor020369d2013-09-18 20:09:33 -0700213void healthd_dump_battery_state(int fd) {
214 gBatteryMonitor->dumpState(fd);
215 fsync(fd);
216}
217
Todd Poynor752faf22013-06-12 13:25:59 -0700218static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700219 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700220}
221
Ruchi Kandoi4614f502014-06-20 11:46:40 -0700222#define UEVENT_MSG_LEN 2048
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700223static void uevent_event(uint32_t /*epevents*/) {
Todd Poynor752faf22013-06-12 13:25:59 -0700224 char msg[UEVENT_MSG_LEN+2];
225 char *cp;
226 int n;
227
228 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
229 if (n <= 0)
230 return;
231 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
232 return;
233
234 msg[n] = '\0';
235 msg[n+1] = '\0';
236 cp = msg;
237
238 while (*cp) {
239 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700240 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700241 break;
242 }
243
244 /* advance to after the next \0 */
245 while (*cp++)
246 ;
247 }
248}
249
Todd Poynor98c23d82013-09-09 20:02:55 -0700250static void uevent_init(void) {
251 uevent_fd = uevent_open_socket(64*1024, true);
252
253 if (uevent_fd < 0) {
254 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
255 return;
256 }
257
258 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
Tim Murraye89ea5e2016-10-18 16:35:15 -0700259 if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD))
Todd Poynor98c23d82013-09-09 20:02:55 -0700260 KLOG_ERROR(LOG_TAG,
261 "register for uevent events failed\n");
262}
263
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700264static void wakealarm_event(uint32_t /*epevents*/) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700265 unsigned long long wakeups;
266
267 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
268 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
269 return;
270 }
271
272 periodic_chores();
273}
274
Todd Poynor752faf22013-06-12 13:25:59 -0700275static void wakealarm_init(void) {
276 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
277 if (wakealarm_fd == -1) {
278 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
279 return;
280 }
281
Tim Murraye89ea5e2016-10-18 16:35:15 -0700282 if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD))
Todd Poynor98c23d82013-09-09 20:02:55 -0700283 KLOG_ERROR(LOG_TAG,
284 "Registration of wakealarm event failed\n");
285
Todd Poynor9face5c2013-08-08 12:24:53 -0700286 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700287}
288
Todd Poynor98c23d82013-09-09 20:02:55 -0700289static void healthd_mainloop(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700290 while (1) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700291 struct epoll_event events[eventct];
Todd Poynor752faf22013-06-12 13:25:59 -0700292 int nevents;
Todd Poynorc7464c92013-09-10 12:40:00 -0700293 int timeout = awake_poll_interval;
294 int mode_timeout;
Todd Poynor752faf22013-06-12 13:25:59 -0700295
Todd Poynorc7464c92013-09-10 12:40:00 -0700296 mode_timeout = healthd_mode_ops->preparetowait();
297 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
298 timeout = mode_timeout;
299 nevents = epoll_wait(epollfd, events, eventct, timeout);
Todd Poynor752faf22013-06-12 13:25:59 -0700300 if (nevents == -1) {
301 if (errno == EINTR)
302 continue;
303 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
304 break;
305 }
306
307 for (int n = 0; n < nevents; ++n) {
308 if (events[n].data.ptr)
Todd Poynor98c23d82013-09-09 20:02:55 -0700309 (*(void (*)(int))events[n].data.ptr)(events[n].events);
Todd Poynor752faf22013-06-12 13:25:59 -0700310 }
Todd Poynorff9ec2d2013-09-09 14:30:55 -0700311
312 if (!nevents)
313 periodic_chores();
Todd Poynorc7464c92013-09-10 12:40:00 -0700314
315 healthd_mode_ops->heartbeat();
Todd Poynor752faf22013-06-12 13:25:59 -0700316 }
317
318 return;
319}
320
Todd Poynor98c23d82013-09-09 20:02:55 -0700321static int healthd_init() {
322 epollfd = epoll_create(MAX_EPOLL_EVENTS);
323 if (epollfd == -1) {
324 KLOG_ERROR(LOG_TAG,
325 "epoll_create failed; errno=%d\n",
326 errno);
327 return -1;
328 }
329
330 healthd_board_init(&healthd_config);
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700331 healthd_mode_ops->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700332 wakealarm_init();
333 uevent_init();
Todd Poynor98c23d82013-09-09 20:02:55 -0700334 gBatteryMonitor = new BatteryMonitor();
Todd Poynorc7464c92013-09-10 12:40:00 -0700335 gBatteryMonitor->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700336 return 0;
337}
338
Todd Poynor752faf22013-06-12 13:25:59 -0700339int main(int argc, char **argv) {
340 int ch;
Todd Poynor98c23d82013-09-09 20:02:55 -0700341 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700342
343 klog_set_level(KLOG_LEVEL);
Todd Poynorc7464c92013-09-10 12:40:00 -0700344 healthd_mode_ops = &android_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700345
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700346 if (!strcmp(basename(argv[0]), "charger")) {
347 healthd_mode_ops = &charger_ops;
348 } else {
349 while ((ch = getopt(argc, argv, "cr")) != -1) {
350 switch (ch) {
351 case 'c':
352 healthd_mode_ops = &charger_ops;
353 break;
354 case 'r':
355 healthd_mode_ops = &recovery_ops;
356 break;
357 case '?':
358 default:
Todd Poynor1c3d3cd2013-11-21 11:28:12 -0800359 KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n",
360 optopt);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700361 exit(1);
362 }
Todd Poynor752faf22013-06-12 13:25:59 -0700363 }
364 }
365
Todd Poynor98c23d82013-09-09 20:02:55 -0700366 ret = healthd_init();
367 if (ret) {
368 KLOG_ERROR("Initialization failed, exiting\n");
369 exit(2);
370 }
Todd Poynor752faf22013-06-12 13:25:59 -0700371
372 healthd_mainloop();
Todd Poynor98c23d82013-09-09 20:02:55 -0700373 KLOG_ERROR("Main loop terminated, exiting\n");
374 return 3;
Todd Poynor752faf22013-06-12 13:25:59 -0700375}