blob: 9cb94dbdfc29abf8fb70e5760b4744af2f65acaa [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>
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <batteryservice/BatteryService.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <cutils/klog.h>
29#include <cutils/uevent.h>
30#include <sys/epoll.h>
31#include <sys/timerfd.h>
Todd Poynor7b27f272013-09-06 15:14:24 -070032#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070033
34using namespace android;
35
36// Periodic chores intervals in seconds
Todd Poynor10b235e2013-08-07 15:25:14 -070037#define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
38#define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
Todd Poynor9face5c2013-08-08 12:24:53 -070039
40static struct healthd_config healthd_config = {
41 .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
42 .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
Todd Poynorf5d30122013-08-12 17:03:35 -070043 .batteryStatusPath = String8(String8::kEmptyString),
44 .batteryHealthPath = String8(String8::kEmptyString),
45 .batteryPresentPath = String8(String8::kEmptyString),
46 .batteryCapacityPath = String8(String8::kEmptyString),
47 .batteryVoltagePath = String8(String8::kEmptyString),
48 .batteryTemperaturePath = String8(String8::kEmptyString),
49 .batteryTechnologyPath = String8(String8::kEmptyString),
50 .batteryCurrentNowPath = String8(String8::kEmptyString),
Todd Poynorbc102112013-08-27 18:11:49 -070051 .batteryCurrentAvgPath = String8(String8::kEmptyString),
Todd Poynorf5d30122013-08-12 17:03:35 -070052 .batteryChargeCounterPath = String8(String8::kEmptyString),
Todd Poynor9face5c2013-08-08 12:24:53 -070053};
Todd Poynor752faf22013-06-12 13:25:59 -070054
Todd Poynor98c23d82013-09-09 20:02:55 -070055static int eventct;
56static int epollfd;
57
Todd Poynor752faf22013-06-12 13:25:59 -070058#define POWER_SUPPLY_SUBSYSTEM "power_supply"
59
Todd Poynor98c23d82013-09-09 20:02:55 -070060// epoll_create() parameter is actually unused
61#define MAX_EPOLL_EVENTS 40
Todd Poynor752faf22013-06-12 13:25:59 -070062static int uevent_fd;
63static int wakealarm_fd;
64static int binder_fd;
65
66// -1 for no epoll timeout
67static int awake_poll_interval = -1;
68
Todd Poynor10b235e2013-08-07 15:25:14 -070069static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070070
71static BatteryMonitor* gBatteryMonitor;
72
Todd Poynorc7464c92013-09-10 12:40:00 -070073struct healthd_mode_ops *healthd_mode_ops;
74
75// Android mode
76
77extern void healthd_mode_android_init(struct healthd_config *config);
78extern int healthd_mode_android_preparetowait(void);
79extern void healthd_mode_android_battery_update(
80 struct android::BatteryProperties *props);
81
82// NOPs for modes that need no special action
83
84static void healthd_mode_nop_init(struct healthd_config *config);
85static int healthd_mode_nop_preparetowait(void);
86static void healthd_mode_nop_heartbeat(void);
87static void healthd_mode_nop_battery_update(
88 struct android::BatteryProperties *props);
89
90static struct healthd_mode_ops android_ops = {
91 .init = healthd_mode_android_init,
92 .preparetowait = healthd_mode_android_preparetowait,
93 .heartbeat = healthd_mode_nop_heartbeat,
94 .battery_update = healthd_mode_android_battery_update,
95};
96
97static struct healthd_mode_ops recovery_ops = {
98 .init = healthd_mode_nop_init,
99 .preparetowait = healthd_mode_nop_preparetowait,
100 .heartbeat = healthd_mode_nop_heartbeat,
101 .battery_update = healthd_mode_nop_battery_update,
102};
103
104static void healthd_mode_nop_init(struct healthd_config *config) {
105}
106
107static int healthd_mode_nop_preparetowait(void) {
108 return -1;
109}
110
111static void healthd_mode_nop_heartbeat(void) {
112}
113
114static void healthd_mode_nop_battery_update(
115 struct android::BatteryProperties *props) {
116}
Todd Poynor752faf22013-06-12 13:25:59 -0700117
Todd Poynor98c23d82013-09-09 20:02:55 -0700118int healthd_register_event(int fd, void (*handler)(uint32_t)) {
119 struct epoll_event ev;
120
121 ev.events = EPOLLIN | EPOLLWAKEUP;
122 ev.data.ptr = (void *)handler;
123 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
124 KLOG_ERROR(LOG_TAG,
125 "epoll_ctl failed; errno=%d\n", errno);
126 return -1;
127 }
128
129 eventct++;
130 return 0;
131}
132
Todd Poynor752faf22013-06-12 13:25:59 -0700133static void wakealarm_set_interval(int interval) {
134 struct itimerspec itval;
135
136 if (wakealarm_fd == -1)
137 return;
138
139 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -0700140
141 if (interval == -1)
142 interval = 0;
143
Todd Poynor752faf22013-06-12 13:25:59 -0700144 itval.it_interval.tv_sec = interval;
145 itval.it_interval.tv_nsec = 0;
146 itval.it_value.tv_sec = interval;
147 itval.it_value.tv_nsec = 0;
148
149 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
150 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
151}
152
Todd Poynor7b27f272013-09-06 15:14:24 -0700153status_t healthd_get_property(int id, struct BatteryProperty *val) {
154 return gBatteryMonitor->getProperty(id, val);
155}
156
157void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700158 // Fast wake interval when on charger (watch for overheat);
159 // slow wake interval when on battery (watch for drained battery).
160
161 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700162 healthd_config.periodic_chores_interval_fast :
163 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700164
165 if (new_wake_interval != wakealarm_wake_interval)
166 wakealarm_set_interval(new_wake_interval);
167
168 // During awake periods poll at fast rate. If wake alarm is set at fast
169 // rate then just use the alarm; if wake alarm is set at slow rate then
170 // poll at fast rate while awake and let alarm wake up at slow rate when
171 // asleep.
172
Todd Poynor9face5c2013-08-08 12:24:53 -0700173 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700174 awake_poll_interval = -1;
175 else
176 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700177 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
178 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700179}
180
181static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700182 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700183}
184
Todd Poynor752faf22013-06-12 13:25:59 -0700185#define UEVENT_MSG_LEN 1024
Todd Poynor98c23d82013-09-09 20:02:55 -0700186static void uevent_event(uint32_t epevents) {
Todd Poynor752faf22013-06-12 13:25:59 -0700187 char msg[UEVENT_MSG_LEN+2];
188 char *cp;
189 int n;
190
191 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
192 if (n <= 0)
193 return;
194 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
195 return;
196
197 msg[n] = '\0';
198 msg[n+1] = '\0';
199 cp = msg;
200
201 while (*cp) {
202 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700203 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700204 break;
205 }
206
207 /* advance to after the next \0 */
208 while (*cp++)
209 ;
210 }
211}
212
Todd Poynor98c23d82013-09-09 20:02:55 -0700213static void uevent_init(void) {
214 uevent_fd = uevent_open_socket(64*1024, true);
215
216 if (uevent_fd < 0) {
217 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
218 return;
219 }
220
221 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
222 if (healthd_register_event(uevent_fd, uevent_event))
223 KLOG_ERROR(LOG_TAG,
224 "register for uevent events failed\n");
225}
226
227static void wakealarm_event(uint32_t epevents) {
228 unsigned long long wakeups;
229
230 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
231 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
232 return;
233 }
234
235 periodic_chores();
236}
237
Todd Poynor752faf22013-06-12 13:25:59 -0700238static void wakealarm_init(void) {
239 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
240 if (wakealarm_fd == -1) {
241 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
242 return;
243 }
244
Todd Poynor98c23d82013-09-09 20:02:55 -0700245 if (healthd_register_event(wakealarm_fd, wakealarm_event))
246 KLOG_ERROR(LOG_TAG,
247 "Registration of wakealarm event failed\n");
248
Todd Poynor9face5c2013-08-08 12:24:53 -0700249 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700250}
251
Todd Poynor98c23d82013-09-09 20:02:55 -0700252static void healthd_mainloop(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700253 while (1) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700254 struct epoll_event events[eventct];
Todd Poynor752faf22013-06-12 13:25:59 -0700255 int nevents;
Todd Poynorc7464c92013-09-10 12:40:00 -0700256 int timeout = awake_poll_interval;
257 int mode_timeout;
Todd Poynor752faf22013-06-12 13:25:59 -0700258
Todd Poynorc7464c92013-09-10 12:40:00 -0700259 mode_timeout = healthd_mode_ops->preparetowait();
260 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
261 timeout = mode_timeout;
262 nevents = epoll_wait(epollfd, events, eventct, timeout);
Todd Poynor752faf22013-06-12 13:25:59 -0700263
264 if (nevents == -1) {
265 if (errno == EINTR)
266 continue;
267 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
268 break;
269 }
270
271 for (int n = 0; n < nevents; ++n) {
272 if (events[n].data.ptr)
Todd Poynor98c23d82013-09-09 20:02:55 -0700273 (*(void (*)(int))events[n].data.ptr)(events[n].events);
Todd Poynor752faf22013-06-12 13:25:59 -0700274 }
Todd Poynorff9ec2d2013-09-09 14:30:55 -0700275
276 if (!nevents)
277 periodic_chores();
Todd Poynorc7464c92013-09-10 12:40:00 -0700278
279 healthd_mode_ops->heartbeat();
Todd Poynor752faf22013-06-12 13:25:59 -0700280 }
281
282 return;
283}
284
Todd Poynor98c23d82013-09-09 20:02:55 -0700285static int healthd_init() {
286 epollfd = epoll_create(MAX_EPOLL_EVENTS);
287 if (epollfd == -1) {
288 KLOG_ERROR(LOG_TAG,
289 "epoll_create failed; errno=%d\n",
290 errno);
291 return -1;
292 }
293
Todd Poynorc7464c92013-09-10 12:40:00 -0700294 healthd_mode_ops->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700295 healthd_board_init(&healthd_config);
296 wakealarm_init();
297 uevent_init();
Todd Poynor98c23d82013-09-09 20:02:55 -0700298 gBatteryMonitor = new BatteryMonitor();
Todd Poynorc7464c92013-09-10 12:40:00 -0700299 gBatteryMonitor->init(&healthd_config);
Todd Poynor98c23d82013-09-09 20:02:55 -0700300 return 0;
301}
302
Todd Poynor752faf22013-06-12 13:25:59 -0700303int main(int argc, char **argv) {
304 int ch;
Todd Poynor98c23d82013-09-09 20:02:55 -0700305 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700306
307 klog_set_level(KLOG_LEVEL);
Todd Poynorc7464c92013-09-10 12:40:00 -0700308 healthd_mode_ops = &android_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700309
Todd Poynorc7464c92013-09-10 12:40:00 -0700310 while ((ch = getopt(argc, argv, "r")) != -1) {
Todd Poynor752faf22013-06-12 13:25:59 -0700311 switch (ch) {
Todd Poynorc7464c92013-09-10 12:40:00 -0700312 case 'r':
313 healthd_mode_ops = &recovery_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700314 break;
315 case '?':
316 default:
Todd Poynor98c23d82013-09-09 20:02:55 -0700317 KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
318 exit(1);
Todd Poynor752faf22013-06-12 13:25:59 -0700319 }
320 }
321
Todd Poynor98c23d82013-09-09 20:02:55 -0700322 ret = healthd_init();
323 if (ret) {
324 KLOG_ERROR("Initialization failed, exiting\n");
325 exit(2);
326 }
Todd Poynor752faf22013-06-12 13:25:59 -0700327
328 healthd_mainloop();
Todd Poynor98c23d82013-09-09 20:02:55 -0700329 KLOG_ERROR("Main loop terminated, exiting\n");
330 return 3;
Todd Poynor752faf22013-06-12 13:25:59 -0700331}