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