blob: cc6824ab5f97bcfdade1f1143ec0151c36e3a133 [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),
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 Kandoibdf11c72014-09-25 19:44:42 -070058 .screen_on = NULL,
Todd Poynor9face5c2013-08-08 12:24:53 -070059};
Todd Poynor752faf22013-06-12 13:25:59 -070060
Todd Poynor98c23d82013-09-09 20:02:55 -070061static int eventct;
62static int epollfd;
63
Todd Poynor752faf22013-06-12 13:25:59 -070064#define POWER_SUPPLY_SUBSYSTEM "power_supply"
65
Todd Poynor98c23d82013-09-09 20:02:55 -070066// epoll_create() parameter is actually unused
67#define MAX_EPOLL_EVENTS 40
Todd Poynor752faf22013-06-12 13:25:59 -070068static int uevent_fd;
69static int wakealarm_fd;
Todd Poynor752faf22013-06-12 13:25:59 -070070
71// -1 for no epoll timeout
72static int awake_poll_interval = -1;
73
Todd Poynor10b235e2013-08-07 15:25:14 -070074static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070075
76static BatteryMonitor* gBatteryMonitor;
77
Todd Poynorc7464c92013-09-10 12:40:00 -070078struct healthd_mode_ops *healthd_mode_ops;
79
80// Android mode
81
82extern void healthd_mode_android_init(struct healthd_config *config);
83extern int healthd_mode_android_preparetowait(void);
84extern void healthd_mode_android_battery_update(
85 struct android::BatteryProperties *props);
86
Todd Poynorfea5b4d2013-09-09 12:09:08 -070087// Charger mode
88
89extern void healthd_mode_charger_init(struct healthd_config *config);
90extern int healthd_mode_charger_preparetowait(void);
91extern void healthd_mode_charger_heartbeat(void);
92extern void healthd_mode_charger_battery_update(
93 struct android::BatteryProperties *props);
94
Todd Poynorc7464c92013-09-10 12:40:00 -070095// NOPs for modes that need no special action
96
97static void healthd_mode_nop_init(struct healthd_config *config);
98static int healthd_mode_nop_preparetowait(void);
99static void healthd_mode_nop_heartbeat(void);
100static void healthd_mode_nop_battery_update(
101 struct android::BatteryProperties *props);
102
103static struct healthd_mode_ops android_ops = {
104 .init = healthd_mode_android_init,
105 .preparetowait = healthd_mode_android_preparetowait,
106 .heartbeat = healthd_mode_nop_heartbeat,
107 .battery_update = healthd_mode_android_battery_update,
108};
109
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700110static struct healthd_mode_ops charger_ops = {
111 .init = healthd_mode_charger_init,
112 .preparetowait = healthd_mode_charger_preparetowait,
113 .heartbeat = healthd_mode_charger_heartbeat,
114 .battery_update = healthd_mode_charger_battery_update,
115};
116
Todd Poynorc7464c92013-09-10 12:40:00 -0700117static struct healthd_mode_ops recovery_ops = {
118 .init = healthd_mode_nop_init,
119 .preparetowait = healthd_mode_nop_preparetowait,
120 .heartbeat = healthd_mode_nop_heartbeat,
121 .battery_update = healthd_mode_nop_battery_update,
122};
123
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700124static void healthd_mode_nop_init(struct healthd_config* /*config*/) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700125}
126
127static int healthd_mode_nop_preparetowait(void) {
128 return -1;
129}
130
131static void healthd_mode_nop_heartbeat(void) {
132}
133
134static void healthd_mode_nop_battery_update(
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700135 struct android::BatteryProperties* /*props*/) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700136}
Todd Poynor752faf22013-06-12 13:25:59 -0700137
Todd Poynor98c23d82013-09-09 20:02:55 -0700138int healthd_register_event(int fd, void (*handler)(uint32_t)) {
139 struct epoll_event ev;
140
141 ev.events = EPOLLIN | EPOLLWAKEUP;
142 ev.data.ptr = (void *)handler;
143 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
144 KLOG_ERROR(LOG_TAG,
145 "epoll_ctl failed; errno=%d\n", errno);
146 return -1;
147 }
148
149 eventct++;
150 return 0;
151}
152
Todd Poynor752faf22013-06-12 13:25:59 -0700153static void wakealarm_set_interval(int interval) {
154 struct itimerspec itval;
155
156 if (wakealarm_fd == -1)
157 return;
158
159 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -0700160
161 if (interval == -1)
162 interval = 0;
163
Todd Poynor752faf22013-06-12 13:25:59 -0700164 itval.it_interval.tv_sec = interval;
165 itval.it_interval.tv_nsec = 0;
166 itval.it_value.tv_sec = interval;
167 itval.it_value.tv_nsec = 0;
168
169 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
170 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
171}
172
Todd Poynor7b27f272013-09-06 15:14:24 -0700173status_t healthd_get_property(int id, struct BatteryProperty *val) {
174 return gBatteryMonitor->getProperty(id, val);
175}
176
177void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700178 // Fast wake interval when on charger (watch for overheat);
179 // slow wake interval when on battery (watch for drained battery).
180
181 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700182 healthd_config.periodic_chores_interval_fast :
183 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700184
185 if (new_wake_interval != wakealarm_wake_interval)
186 wakealarm_set_interval(new_wake_interval);
187
188 // During awake periods poll at fast rate. If wake alarm is set at fast
189 // rate then just use the alarm; if wake alarm is set at slow rate then
190 // poll at fast rate while awake and let alarm wake up at slow rate when
191 // asleep.
192
Todd Poynor9face5c2013-08-08 12:24:53 -0700193 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700194 awake_poll_interval = -1;
195 else
196 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700197 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
198 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700199}
200
Todd Poynor020369d2013-09-18 20:09:33 -0700201void healthd_dump_battery_state(int fd) {
202 gBatteryMonitor->dumpState(fd);
203 fsync(fd);
204}
205
Todd Poynor752faf22013-06-12 13:25:59 -0700206static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700207 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700208}
209
Ruchi Kandoi4614f502014-06-20 11:46:40 -0700210#define UEVENT_MSG_LEN 2048
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700211static void uevent_event(uint32_t /*epevents*/) {
Todd Poynor752faf22013-06-12 13:25:59 -0700212 char msg[UEVENT_MSG_LEN+2];
213 char *cp;
214 int n;
215
216 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
217 if (n <= 0)
218 return;
219 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
220 return;
221
222 msg[n] = '\0';
223 msg[n+1] = '\0';
224 cp = msg;
225
226 while (*cp) {
227 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700228 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700229 break;
230 }
231
232 /* advance to after the next \0 */
233 while (*cp++)
234 ;
235 }
236}
237
Todd Poynor98c23d82013-09-09 20:02:55 -0700238static void uevent_init(void) {
239 uevent_fd = uevent_open_socket(64*1024, true);
240
241 if (uevent_fd < 0) {
242 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
243 return;
244 }
245
246 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
247 if (healthd_register_event(uevent_fd, uevent_event))
248 KLOG_ERROR(LOG_TAG,
249 "register for uevent events failed\n");
250}
251
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700252static void wakealarm_event(uint32_t /*epevents*/) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700253 unsigned long long wakeups;
254
255 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
256 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
257 return;
258 }
259
260 periodic_chores();
261}
262
Todd Poynor752faf22013-06-12 13:25:59 -0700263static void wakealarm_init(void) {
264 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
265 if (wakealarm_fd == -1) {
266 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
267 return;
268 }
269
Todd Poynor98c23d82013-09-09 20:02:55 -0700270 if (healthd_register_event(wakealarm_fd, wakealarm_event))
271 KLOG_ERROR(LOG_TAG,
272 "Registration of wakealarm event failed\n");
273
Todd Poynor9face5c2013-08-08 12:24:53 -0700274 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700275}
276
Todd Poynor98c23d82013-09-09 20:02:55 -0700277static void healthd_mainloop(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700278 while (1) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700279 struct epoll_event events[eventct];
Todd Poynor752faf22013-06-12 13:25:59 -0700280 int nevents;
Todd Poynorc7464c92013-09-10 12:40:00 -0700281 int timeout = awake_poll_interval;
282 int mode_timeout;
Todd Poynor752faf22013-06-12 13:25:59 -0700283
Todd Poynorc7464c92013-09-10 12:40:00 -0700284 mode_timeout = healthd_mode_ops->preparetowait();
285 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
286 timeout = mode_timeout;
287 nevents = epoll_wait(epollfd, events, eventct, timeout);
Todd Poynor752faf22013-06-12 13:25:59 -0700288
289 if (nevents == -1) {
290 if (errno == EINTR)
291 continue;
292 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
293 break;
294 }
295
296 for (int n = 0; n < nevents; ++n) {
297 if (events[n].data.ptr)
Todd Poynor98c23d82013-09-09 20:02:55 -0700298 (*(void (*)(int))events[n].data.ptr)(events[n].events);
Todd Poynor752faf22013-06-12 13:25:59 -0700299 }
Todd Poynorff9ec2d2013-09-09 14:30:55 -0700300
301 if (!nevents)
302 periodic_chores();
Todd Poynorc7464c92013-09-10 12:40:00 -0700303
304 healthd_mode_ops->heartbeat();
Todd Poynor752faf22013-06-12 13:25:59 -0700305 }
306
307 return;
308}
309
Todd Poynor98c23d82013-09-09 20:02:55 -0700310static int healthd_init() {
311 epollfd = epoll_create(MAX_EPOLL_EVENTS);
312 if (epollfd == -1) {
313 KLOG_ERROR(LOG_TAG,
314 "epoll_create failed; errno=%d\n",
315 errno);
316 return -1;
317 }
318
319 healthd_board_init(&healthd_config);
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700320 healthd_mode_ops->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700321 wakealarm_init();
322 uevent_init();
Todd Poynor98c23d82013-09-09 20:02:55 -0700323 gBatteryMonitor = new BatteryMonitor();
Todd Poynorc7464c92013-09-10 12:40:00 -0700324 gBatteryMonitor->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700325 return 0;
326}
327
Todd Poynor752faf22013-06-12 13:25:59 -0700328int main(int argc, char **argv) {
329 int ch;
Todd Poynor98c23d82013-09-09 20:02:55 -0700330 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700331
332 klog_set_level(KLOG_LEVEL);
Todd Poynorc7464c92013-09-10 12:40:00 -0700333 healthd_mode_ops = &android_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700334
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700335 if (!strcmp(basename(argv[0]), "charger")) {
336 healthd_mode_ops = &charger_ops;
337 } else {
338 while ((ch = getopt(argc, argv, "cr")) != -1) {
339 switch (ch) {
340 case 'c':
341 healthd_mode_ops = &charger_ops;
342 break;
343 case 'r':
344 healthd_mode_ops = &recovery_ops;
345 break;
346 case '?':
347 default:
Todd Poynor1c3d3cd2013-11-21 11:28:12 -0800348 KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n",
349 optopt);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700350 exit(1);
351 }
Todd Poynor752faf22013-06-12 13:25:59 -0700352 }
353 }
354
Todd Poynor98c23d82013-09-09 20:02:55 -0700355 ret = healthd_init();
356 if (ret) {
357 KLOG_ERROR("Initialization failed, exiting\n");
358 exit(2);
359 }
Todd Poynor752faf22013-06-12 13:25:59 -0700360
361 healthd_mainloop();
Todd Poynor98c23d82013-09-09 20:02:55 -0700362 KLOG_ERROR("Main loop terminated, exiting\n");
363 return 3;
Todd Poynor752faf22013-06-12 13:25:59 -0700364}