blob: 1c144b34d750d89ac906a59bef3ce7282dc45b3f [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 Cuif6dbc6d2016-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
Nick Vaccaro1f1a6fd2016-10-21 19:16:40 -070038#ifndef BOARD_PERIODIC_CHORES_INTERVAL_FAST
39 // Periodic chores fast interval in seconds
40 #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
41#else
42 #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (BOARD_PERIODIC_CHORES_INTERVAL_FAST)
43#endif
44
45#ifndef BOARD_PERIODIC_CHORES_INTERVAL_SLOW
46 // Periodic chores fast interval in seconds
47 #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
48#else
49 #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
50#endif
Todd Poynor9face5c2013-08-08 12:24:53 -070051
52static struct healthd_config healthd_config = {
53 .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
54 .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
Todd Poynorf5d30122013-08-12 17:03:35 -070055 .batteryStatusPath = String8(String8::kEmptyString),
56 .batteryHealthPath = String8(String8::kEmptyString),
57 .batteryPresentPath = String8(String8::kEmptyString),
58 .batteryCapacityPath = String8(String8::kEmptyString),
59 .batteryVoltagePath = String8(String8::kEmptyString),
60 .batteryTemperaturePath = String8(String8::kEmptyString),
61 .batteryTechnologyPath = String8(String8::kEmptyString),
62 .batteryCurrentNowPath = String8(String8::kEmptyString),
Todd Poynorbc102112013-08-27 18:11:49 -070063 .batteryCurrentAvgPath = String8(String8::kEmptyString),
Todd Poynorf5d30122013-08-12 17:03:35 -070064 .batteryChargeCounterPath = String8(String8::kEmptyString),
Ruchi Kandoicc338802015-08-24 13:01:16 -070065 .batteryFullChargePath = String8(String8::kEmptyString),
66 .batteryCycleCountPath = String8(String8::kEmptyString),
Todd Poynore14b37e2014-05-20 13:54:40 -070067 .energyCounter = NULL,
Ruchi Kandoia84b1f62014-10-21 18:24:11 -070068 .boot_min_cap = 0,
Ruchi Kandoibdf11c72014-09-25 19:44:42 -070069 .screen_on = NULL,
Todd Poynor9face5c2013-08-08 12:24:53 -070070};
Todd Poynor752faf22013-06-12 13:25:59 -070071
Todd Poynor98c23d82013-09-09 20:02:55 -070072static int eventct;
73static int epollfd;
74
Todd Poynor752faf22013-06-12 13:25:59 -070075#define POWER_SUPPLY_SUBSYSTEM "power_supply"
76
Todd Poynor98c23d82013-09-09 20:02:55 -070077// epoll_create() parameter is actually unused
78#define MAX_EPOLL_EVENTS 40
Todd Poynor752faf22013-06-12 13:25:59 -070079static int uevent_fd;
80static int wakealarm_fd;
Todd Poynor752faf22013-06-12 13:25:59 -070081
82// -1 for no epoll timeout
83static int awake_poll_interval = -1;
84
Todd Poynor10b235e2013-08-07 15:25:14 -070085static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070086
87static BatteryMonitor* gBatteryMonitor;
88
Todd Poynorc7464c92013-09-10 12:40:00 -070089struct healthd_mode_ops *healthd_mode_ops;
90
91// Android mode
92
93extern void healthd_mode_android_init(struct healthd_config *config);
94extern int healthd_mode_android_preparetowait(void);
95extern void healthd_mode_android_battery_update(
96 struct android::BatteryProperties *props);
97
Todd Poynorfea5b4d2013-09-09 12:09:08 -070098// Charger mode
99
100extern void healthd_mode_charger_init(struct healthd_config *config);
101extern int healthd_mode_charger_preparetowait(void);
102extern void healthd_mode_charger_heartbeat(void);
103extern void healthd_mode_charger_battery_update(
104 struct android::BatteryProperties *props);
105
Todd Poynorc7464c92013-09-10 12:40:00 -0700106// NOPs for modes that need no special action
107
108static void healthd_mode_nop_init(struct healthd_config *config);
109static int healthd_mode_nop_preparetowait(void);
110static void healthd_mode_nop_heartbeat(void);
111static void healthd_mode_nop_battery_update(
112 struct android::BatteryProperties *props);
113
114static struct healthd_mode_ops android_ops = {
115 .init = healthd_mode_android_init,
116 .preparetowait = healthd_mode_android_preparetowait,
117 .heartbeat = healthd_mode_nop_heartbeat,
118 .battery_update = healthd_mode_android_battery_update,
119};
120
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700121static struct healthd_mode_ops charger_ops = {
122 .init = healthd_mode_charger_init,
123 .preparetowait = healthd_mode_charger_preparetowait,
124 .heartbeat = healthd_mode_charger_heartbeat,
125 .battery_update = healthd_mode_charger_battery_update,
126};
127
Todd Poynorc7464c92013-09-10 12:40:00 -0700128static struct healthd_mode_ops recovery_ops = {
129 .init = healthd_mode_nop_init,
130 .preparetowait = healthd_mode_nop_preparetowait,
131 .heartbeat = healthd_mode_nop_heartbeat,
132 .battery_update = healthd_mode_nop_battery_update,
133};
134
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700135static void healthd_mode_nop_init(struct healthd_config* /*config*/) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700136}
137
138static int healthd_mode_nop_preparetowait(void) {
139 return -1;
140}
141
142static void healthd_mode_nop_heartbeat(void) {
143}
144
145static void healthd_mode_nop_battery_update(
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700146 struct android::BatteryProperties* /*props*/) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700147}
Todd Poynor752faf22013-06-12 13:25:59 -0700148
Tim Murraye89ea5e2016-10-18 16:35:15 -0700149int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700150 struct epoll_event ev;
151
Tim Murraye89ea5e2016-10-18 16:35:15 -0700152 ev.events = EPOLLIN;
153
154 if (wakeup == EVENT_WAKEUP_FD)
155 ev.events |= EPOLLWAKEUP;
156
Todd Poynor98c23d82013-09-09 20:02:55 -0700157 ev.data.ptr = (void *)handler;
158 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
159 KLOG_ERROR(LOG_TAG,
160 "epoll_ctl failed; errno=%d\n", errno);
161 return -1;
162 }
163
164 eventct++;
165 return 0;
166}
167
Todd Poynor752faf22013-06-12 13:25:59 -0700168static void wakealarm_set_interval(int interval) {
169 struct itimerspec itval;
170
171 if (wakealarm_fd == -1)
172 return;
173
174 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -0700175
176 if (interval == -1)
177 interval = 0;
178
Todd Poynor752faf22013-06-12 13:25:59 -0700179 itval.it_interval.tv_sec = interval;
180 itval.it_interval.tv_nsec = 0;
181 itval.it_value.tv_sec = interval;
182 itval.it_value.tv_nsec = 0;
183
184 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
185 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
186}
187
Todd Poynor7b27f272013-09-06 15:14:24 -0700188status_t healthd_get_property(int id, struct BatteryProperty *val) {
189 return gBatteryMonitor->getProperty(id, val);
190}
191
192void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700193 // Fast wake interval when on charger (watch for overheat);
194 // slow wake interval when on battery (watch for drained battery).
195
196 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700197 healthd_config.periodic_chores_interval_fast :
198 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700199
200 if (new_wake_interval != wakealarm_wake_interval)
201 wakealarm_set_interval(new_wake_interval);
202
203 // During awake periods poll at fast rate. If wake alarm is set at fast
204 // rate then just use the alarm; if wake alarm is set at slow rate then
205 // poll at fast rate while awake and let alarm wake up at slow rate when
206 // asleep.
207
Todd Poynor9face5c2013-08-08 12:24:53 -0700208 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700209 awake_poll_interval = -1;
210 else
211 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700212 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
213 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700214}
215
Todd Poynor020369d2013-09-18 20:09:33 -0700216void healthd_dump_battery_state(int fd) {
217 gBatteryMonitor->dumpState(fd);
218 fsync(fd);
219}
220
Todd Poynor752faf22013-06-12 13:25:59 -0700221static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700222 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700223}
224
Ruchi Kandoi4614f502014-06-20 11:46:40 -0700225#define UEVENT_MSG_LEN 2048
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700226static void uevent_event(uint32_t /*epevents*/) {
Todd Poynor752faf22013-06-12 13:25:59 -0700227 char msg[UEVENT_MSG_LEN+2];
228 char *cp;
229 int n;
230
231 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
232 if (n <= 0)
233 return;
234 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
235 return;
236
237 msg[n] = '\0';
238 msg[n+1] = '\0';
239 cp = msg;
240
241 while (*cp) {
242 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700243 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700244 break;
245 }
246
247 /* advance to after the next \0 */
248 while (*cp++)
249 ;
250 }
251}
252
Todd Poynor98c23d82013-09-09 20:02:55 -0700253static void uevent_init(void) {
254 uevent_fd = uevent_open_socket(64*1024, true);
255
256 if (uevent_fd < 0) {
257 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
258 return;
259 }
260
261 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
Tim Murraye89ea5e2016-10-18 16:35:15 -0700262 if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD))
Todd Poynor98c23d82013-09-09 20:02:55 -0700263 KLOG_ERROR(LOG_TAG,
264 "register for uevent events failed\n");
265}
266
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700267static void wakealarm_event(uint32_t /*epevents*/) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700268 unsigned long long wakeups;
269
270 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
271 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
272 return;
273 }
274
275 periodic_chores();
276}
277
Todd Poynor752faf22013-06-12 13:25:59 -0700278static void wakealarm_init(void) {
279 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
280 if (wakealarm_fd == -1) {
281 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
282 return;
283 }
284
Tim Murraye89ea5e2016-10-18 16:35:15 -0700285 if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD))
Todd Poynor98c23d82013-09-09 20:02:55 -0700286 KLOG_ERROR(LOG_TAG,
287 "Registration of wakealarm event failed\n");
288
Todd Poynor9face5c2013-08-08 12:24:53 -0700289 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700290}
291
Todd Poynor98c23d82013-09-09 20:02:55 -0700292static void healthd_mainloop(void) {
Nick Vaccaro6f831492016-10-26 19:33:48 -0700293 int nevents = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700294 while (1) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700295 struct epoll_event events[eventct];
Todd Poynorc7464c92013-09-10 12:40:00 -0700296 int timeout = awake_poll_interval;
297 int mode_timeout;
Todd Poynor752faf22013-06-12 13:25:59 -0700298
Nick Vaccaro6f831492016-10-26 19:33:48 -0700299 /* Don't wait for first timer timeout to run periodic chores */
300 if (!nevents)
301 periodic_chores();
302
303 healthd_mode_ops->heartbeat();
304
Todd Poynorc7464c92013-09-10 12:40:00 -0700305 mode_timeout = healthd_mode_ops->preparetowait();
306 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
307 timeout = mode_timeout;
308 nevents = epoll_wait(epollfd, events, eventct, timeout);
Todd Poynor752faf22013-06-12 13:25:59 -0700309 if (nevents == -1) {
310 if (errno == EINTR)
311 continue;
312 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
313 break;
314 }
315
316 for (int n = 0; n < nevents; ++n) {
317 if (events[n].data.ptr)
Todd Poynor98c23d82013-09-09 20:02:55 -0700318 (*(void (*)(int))events[n].data.ptr)(events[n].events);
Todd Poynor752faf22013-06-12 13:25:59 -0700319 }
320 }
321
322 return;
323}
324
Todd Poynor98c23d82013-09-09 20:02:55 -0700325static int healthd_init() {
326 epollfd = epoll_create(MAX_EPOLL_EVENTS);
327 if (epollfd == -1) {
328 KLOG_ERROR(LOG_TAG,
329 "epoll_create failed; errno=%d\n",
330 errno);
331 return -1;
332 }
333
334 healthd_board_init(&healthd_config);
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700335 healthd_mode_ops->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700336 wakealarm_init();
337 uevent_init();
Todd Poynor98c23d82013-09-09 20:02:55 -0700338 gBatteryMonitor = new BatteryMonitor();
Todd Poynorc7464c92013-09-10 12:40:00 -0700339 gBatteryMonitor->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700340 return 0;
341}
342
Todd Poynor752faf22013-06-12 13:25:59 -0700343int main(int argc, char **argv) {
344 int ch;
Todd Poynor98c23d82013-09-09 20:02:55 -0700345 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700346
347 klog_set_level(KLOG_LEVEL);
Todd Poynorc7464c92013-09-10 12:40:00 -0700348 healthd_mode_ops = &android_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700349
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700350 if (!strcmp(basename(argv[0]), "charger")) {
351 healthd_mode_ops = &charger_ops;
352 } else {
353 while ((ch = getopt(argc, argv, "cr")) != -1) {
354 switch (ch) {
355 case 'c':
356 healthd_mode_ops = &charger_ops;
357 break;
358 case 'r':
359 healthd_mode_ops = &recovery_ops;
360 break;
361 case '?':
362 default:
Todd Poynor1c3d3cd2013-11-21 11:28:12 -0800363 KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n",
364 optopt);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700365 exit(1);
366 }
Todd Poynor752faf22013-06-12 13:25:59 -0700367 }
368 }
369
Todd Poynor98c23d82013-09-09 20:02:55 -0700370 ret = healthd_init();
371 if (ret) {
372 KLOG_ERROR("Initialization failed, exiting\n");
373 exit(2);
374 }
Todd Poynor752faf22013-06-12 13:25:59 -0700375
376 healthd_mainloop();
Todd Poynor98c23d82013-09-09 20:02:55 -0700377 KLOG_ERROR("Main loop terminated, exiting\n");
378 return 3;
Todd Poynor752faf22013-06-12 13:25:59 -0700379}