blob: 89178fa714f8acb70b7c08a8324f2e0d0daf9cd7 [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>
34
35using namespace android;
36
37// Periodic chores intervals in seconds
Todd Poynor10b235e2013-08-07 15:25:14 -070038#define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
39#define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
40static int periodic_chores_interval_fast =
41 DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
42static int periodic_chores_interval_slow =
43 DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW;
Todd Poynor752faf22013-06-12 13:25:59 -070044
45#define POWER_SUPPLY_SUBSYSTEM "power_supply"
46
47// epoll events: uevent, wakealarm, binder
48#define MAX_EPOLL_EVENTS 3
49static int uevent_fd;
50static int wakealarm_fd;
51static int binder_fd;
52
53// -1 for no epoll timeout
54static int awake_poll_interval = -1;
55
Todd Poynor10b235e2013-08-07 15:25:14 -070056static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070057
58static BatteryMonitor* gBatteryMonitor;
59
60static bool nosvcmgr;
61
62static void wakealarm_set_interval(int interval) {
63 struct itimerspec itval;
64
65 if (wakealarm_fd == -1)
66 return;
67
68 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -070069
70 if (interval == -1)
71 interval = 0;
72
Todd Poynor752faf22013-06-12 13:25:59 -070073 itval.it_interval.tv_sec = interval;
74 itval.it_interval.tv_nsec = 0;
75 itval.it_value.tv_sec = interval;
76 itval.it_value.tv_nsec = 0;
77
78 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
79 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
80}
81
82static void battery_update(void) {
83 // Fast wake interval when on charger (watch for overheat);
84 // slow wake interval when on battery (watch for drained battery).
85
86 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor10b235e2013-08-07 15:25:14 -070087 periodic_chores_interval_fast : periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -070088
89 if (new_wake_interval != wakealarm_wake_interval)
90 wakealarm_set_interval(new_wake_interval);
91
92 // During awake periods poll at fast rate. If wake alarm is set at fast
93 // rate then just use the alarm; if wake alarm is set at slow rate then
94 // poll at fast rate while awake and let alarm wake up at slow rate when
95 // asleep.
96
Todd Poynor10b235e2013-08-07 15:25:14 -070097 if (periodic_chores_interval_fast == -1)
98 awake_poll_interval = -1;
99 else
100 awake_poll_interval =
101 new_wake_interval == periodic_chores_interval_fast ?
102 -1 : periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700103}
104
105static void periodic_chores() {
106 battery_update();
107}
108
109static void uevent_init(void) {
110 uevent_fd = uevent_open_socket(64*1024, true);
111
112 if (uevent_fd >= 0)
113 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
114 else
115 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
116}
117
118#define UEVENT_MSG_LEN 1024
119static void uevent_event(void) {
120 char msg[UEVENT_MSG_LEN+2];
121 char *cp;
122 int n;
123
124 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
125 if (n <= 0)
126 return;
127 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
128 return;
129
130 msg[n] = '\0';
131 msg[n+1] = '\0';
132 cp = msg;
133
134 while (*cp) {
135 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
136 battery_update();
137 break;
138 }
139
140 /* advance to after the next \0 */
141 while (*cp++)
142 ;
143 }
144}
145
146static void wakealarm_init(void) {
147 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
148 if (wakealarm_fd == -1) {
149 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
150 return;
151 }
152
Todd Poynor10b235e2013-08-07 15:25:14 -0700153 healthd_board_poll_intervals(&periodic_chores_interval_fast,
154 &periodic_chores_interval_slow);
155
156 wakealarm_set_interval(periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700157}
158
159static void wakealarm_event(void) {
160 unsigned long long wakeups;
161
162 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
163 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm_fd failed\n");
164 return;
165 }
166
167 periodic_chores();
168}
169
170static void binder_init(void) {
171 ProcessState::self()->setThreadPoolMaxThreadCount(0);
172 IPCThreadState::self()->disableBackgroundScheduling(true);
173 IPCThreadState::self()->setupPolling(&binder_fd);
174}
175
176static void binder_event(void) {
177 IPCThreadState::self()->handlePolledCommands();
178}
179
180static void healthd_mainloop(void) {
181 struct epoll_event ev;
182 int epollfd;
183 int maxevents = 0;
184
185 epollfd = epoll_create(MAX_EPOLL_EVENTS);
186 if (epollfd == -1) {
187 KLOG_ERROR(LOG_TAG,
188 "healthd_mainloop: epoll_create failed; errno=%d\n",
189 errno);
190 return;
191 }
192
193 if (uevent_fd >= 0) {
194 ev.events = EPOLLIN | EPOLLWAKEUP;
195 ev.data.ptr = (void *)uevent_event;
196 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, uevent_fd, &ev) == -1)
197 KLOG_ERROR(LOG_TAG,
198 "healthd_mainloop: epoll_ctl for uevent_fd failed; errno=%d\n",
199 errno);
200 else
201 maxevents++;
202 }
203
204 if (wakealarm_fd >= 0) {
205 ev.events = EPOLLIN | EPOLLWAKEUP;
206 ev.data.ptr = (void *)wakealarm_event;
207 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, wakealarm_fd, &ev) == -1)
208 KLOG_ERROR(LOG_TAG,
209 "healthd_mainloop: epoll_ctl for wakealarm_fd failed; errno=%d\n",
210 errno);
211 else
212 maxevents++;
213 }
214
215 if (binder_fd >= 0) {
216 ev.events = EPOLLIN | EPOLLWAKEUP;
217 ev.data.ptr= (void *)binder_event;
218 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, binder_fd, &ev) == -1)
219 KLOG_ERROR(LOG_TAG,
220 "healthd_mainloop: epoll_ctl for binder_fd failed; errno=%d\n",
221 errno);
222 else
223 maxevents++;
224 }
225
226 while (1) {
227 struct epoll_event events[maxevents];
228 int nevents;
229
230 IPCThreadState::self()->flushCommands();
231 nevents = epoll_wait(epollfd, events, maxevents, awake_poll_interval);
232
233 if (nevents == -1) {
234 if (errno == EINTR)
235 continue;
236 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
237 break;
238 }
239
240 for (int n = 0; n < nevents; ++n) {
241 if (events[n].data.ptr)
242 (*(void (*)())events[n].data.ptr)();
243 }
244 }
245
246 return;
247}
248
249int main(int argc, char **argv) {
250 int ch;
251
252 klog_set_level(KLOG_LEVEL);
253
254 while ((ch = getopt(argc, argv, "n")) != -1) {
255 switch (ch) {
256 case 'n':
257 nosvcmgr = true;
258 break;
259 case '?':
260 default:
261 KLOG_WARNING(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
262 }
263 }
264
265 wakealarm_init();
266 uevent_init();
267 binder_init();
268 gBatteryMonitor = new BatteryMonitor();
269 gBatteryMonitor->init(nosvcmgr);
270
271 healthd_mainloop();
272 return 0;
273}