blob: 35094c2277bb813e362b74a004a79e8cf106c6da [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>
28#include <binder/IPCThreadState.h>
29#include <binder/ProcessState.h>
30#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
75static bool nosvcmgr;
76
Todd Poynor98c23d82013-09-09 20:02:55 -070077int healthd_register_event(int fd, void (*handler)(uint32_t)) {
78 struct epoll_event ev;
79
80 ev.events = EPOLLIN | EPOLLWAKEUP;
81 ev.data.ptr = (void *)handler;
82 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
83 KLOG_ERROR(LOG_TAG,
84 "epoll_ctl failed; errno=%d\n", errno);
85 return -1;
86 }
87
88 eventct++;
89 return 0;
90}
91
Todd Poynor752faf22013-06-12 13:25:59 -070092static void wakealarm_set_interval(int interval) {
93 struct itimerspec itval;
94
95 if (wakealarm_fd == -1)
96 return;
97
98 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -070099
100 if (interval == -1)
101 interval = 0;
102
Todd Poynor752faf22013-06-12 13:25:59 -0700103 itval.it_interval.tv_sec = interval;
104 itval.it_interval.tv_nsec = 0;
105 itval.it_value.tv_sec = interval;
106 itval.it_value.tv_nsec = 0;
107
108 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
109 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
110}
111
Todd Poynor7b27f272013-09-06 15:14:24 -0700112status_t healthd_get_property(int id, struct BatteryProperty *val) {
113 return gBatteryMonitor->getProperty(id, val);
114}
115
116void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700117 // Fast wake interval when on charger (watch for overheat);
118 // slow wake interval when on battery (watch for drained battery).
119
120 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700121 healthd_config.periodic_chores_interval_fast :
122 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700123
124 if (new_wake_interval != wakealarm_wake_interval)
125 wakealarm_set_interval(new_wake_interval);
126
127 // During awake periods poll at fast rate. If wake alarm is set at fast
128 // rate then just use the alarm; if wake alarm is set at slow rate then
129 // poll at fast rate while awake and let alarm wake up at slow rate when
130 // asleep.
131
Todd Poynor9face5c2013-08-08 12:24:53 -0700132 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700133 awake_poll_interval = -1;
134 else
135 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700136 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
137 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700138}
139
140static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700141 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700142}
143
Todd Poynor752faf22013-06-12 13:25:59 -0700144#define UEVENT_MSG_LEN 1024
Todd Poynor98c23d82013-09-09 20:02:55 -0700145static void uevent_event(uint32_t epevents) {
Todd Poynor752faf22013-06-12 13:25:59 -0700146 char msg[UEVENT_MSG_LEN+2];
147 char *cp;
148 int n;
149
150 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
151 if (n <= 0)
152 return;
153 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
154 return;
155
156 msg[n] = '\0';
157 msg[n+1] = '\0';
158 cp = msg;
159
160 while (*cp) {
161 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700162 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700163 break;
164 }
165
166 /* advance to after the next \0 */
167 while (*cp++)
168 ;
169 }
170}
171
Todd Poynor98c23d82013-09-09 20:02:55 -0700172static void uevent_init(void) {
173 uevent_fd = uevent_open_socket(64*1024, true);
174
175 if (uevent_fd < 0) {
176 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
177 return;
178 }
179
180 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
181 if (healthd_register_event(uevent_fd, uevent_event))
182 KLOG_ERROR(LOG_TAG,
183 "register for uevent events failed\n");
184}
185
186static void wakealarm_event(uint32_t epevents) {
187 unsigned long long wakeups;
188
189 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
190 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
191 return;
192 }
193
194 periodic_chores();
195}
196
Todd Poynor752faf22013-06-12 13:25:59 -0700197static void wakealarm_init(void) {
198 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
199 if (wakealarm_fd == -1) {
200 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
201 return;
202 }
203
Todd Poynor98c23d82013-09-09 20:02:55 -0700204 if (healthd_register_event(wakealarm_fd, wakealarm_event))
205 KLOG_ERROR(LOG_TAG,
206 "Registration of wakealarm event failed\n");
207
Todd Poynor9face5c2013-08-08 12:24:53 -0700208 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700209}
210
Todd Poynor98c23d82013-09-09 20:02:55 -0700211static void binder_event(uint32_t revents) {
Todd Poynor752faf22013-06-12 13:25:59 -0700212 IPCThreadState::self()->handlePolledCommands();
213}
214
Todd Poynor98c23d82013-09-09 20:02:55 -0700215static void binder_init(void) {
216 int binder_fd;
Todd Poynor752faf22013-06-12 13:25:59 -0700217
Todd Poynor98c23d82013-09-09 20:02:55 -0700218 ProcessState::self()->setThreadPoolMaxThreadCount(0);
219 IPCThreadState::self()->disableBackgroundScheduling(true);
220 IPCThreadState::self()->setupPolling(&binder_fd);
Todd Poynor752faf22013-06-12 13:25:59 -0700221
222 if (binder_fd >= 0) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700223 if (healthd_register_event(binder_fd, binder_event))
Todd Poynor752faf22013-06-12 13:25:59 -0700224 KLOG_ERROR(LOG_TAG,
Todd Poynor98c23d82013-09-09 20:02:55 -0700225 "Register for binder events failed\n");
226 }
227}
Todd Poynor752faf22013-06-12 13:25:59 -0700228
Todd Poynor98c23d82013-09-09 20:02:55 -0700229static void healthd_mainloop(void) {
Todd Poynor752faf22013-06-12 13:25:59 -0700230 while (1) {
Todd Poynor98c23d82013-09-09 20:02:55 -0700231 struct epoll_event events[eventct];
Todd Poynor752faf22013-06-12 13:25:59 -0700232 int nevents;
233
234 IPCThreadState::self()->flushCommands();
Todd Poynor98c23d82013-09-09 20:02:55 -0700235
236 nevents = epoll_wait(epollfd, events, eventct, awake_poll_interval);
Todd Poynor752faf22013-06-12 13:25:59 -0700237
238 if (nevents == -1) {
239 if (errno == EINTR)
240 continue;
241 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
242 break;
243 }
244
245 for (int n = 0; n < nevents; ++n) {
246 if (events[n].data.ptr)
Todd Poynor98c23d82013-09-09 20:02:55 -0700247 (*(void (*)(int))events[n].data.ptr)(events[n].events);
Todd Poynor752faf22013-06-12 13:25:59 -0700248 }
Todd Poynorff9ec2d2013-09-09 14:30:55 -0700249
250 if (!nevents)
251 periodic_chores();
Todd Poynor752faf22013-06-12 13:25:59 -0700252 }
253
254 return;
255}
256
Todd Poynor98c23d82013-09-09 20:02:55 -0700257static int healthd_init() {
258 epollfd = epoll_create(MAX_EPOLL_EVENTS);
259 if (epollfd == -1) {
260 KLOG_ERROR(LOG_TAG,
261 "epoll_create failed; errno=%d\n",
262 errno);
263 return -1;
264 }
265
266 healthd_board_init(&healthd_config);
267 wakealarm_init();
268 uevent_init();
269 binder_init();
270 gBatteryMonitor = new BatteryMonitor();
271 gBatteryMonitor->init(&healthd_config, nosvcmgr);
272 return 0;
273}
274
Todd Poynor752faf22013-06-12 13:25:59 -0700275int main(int argc, char **argv) {
276 int ch;
Todd Poynor98c23d82013-09-09 20:02:55 -0700277 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700278
279 klog_set_level(KLOG_LEVEL);
280
281 while ((ch = getopt(argc, argv, "n")) != -1) {
282 switch (ch) {
283 case 'n':
284 nosvcmgr = true;
285 break;
286 case '?':
287 default:
Todd Poynor98c23d82013-09-09 20:02:55 -0700288 KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
289 exit(1);
Todd Poynor752faf22013-06-12 13:25:59 -0700290 }
291 }
292
Todd Poynor98c23d82013-09-09 20:02:55 -0700293 ret = healthd_init();
294 if (ret) {
295 KLOG_ERROR("Initialization failed, exiting\n");
296 exit(2);
297 }
Todd Poynor752faf22013-06-12 13:25:59 -0700298
299 healthd_mainloop();
Todd Poynor98c23d82013-09-09 20:02:55 -0700300 KLOG_ERROR("Main loop terminated, exiting\n");
301 return 3;
Todd Poynor752faf22013-06-12 13:25:59 -0700302}