blob: f7c79c49693a39342251e370ef07b2198527b244 [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)
Todd Poynor9face5c2013-08-08 12:24:53 -070040
41static struct healthd_config healthd_config = {
42 .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
43 .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
Todd Poynorf5d30122013-08-12 17:03:35 -070044 .batteryStatusPath = String8(String8::kEmptyString),
45 .batteryHealthPath = String8(String8::kEmptyString),
46 .batteryPresentPath = String8(String8::kEmptyString),
47 .batteryCapacityPath = String8(String8::kEmptyString),
48 .batteryVoltagePath = String8(String8::kEmptyString),
49 .batteryTemperaturePath = String8(String8::kEmptyString),
50 .batteryTechnologyPath = String8(String8::kEmptyString),
51 .batteryCurrentNowPath = String8(String8::kEmptyString),
Todd Poynorbc102112013-08-27 18:11:49 -070052 .batteryCurrentAvgPath = String8(String8::kEmptyString),
Todd Poynorf5d30122013-08-12 17:03:35 -070053 .batteryChargeCounterPath = String8(String8::kEmptyString),
Todd Poynor9face5c2013-08-08 12:24:53 -070054};
Todd Poynor752faf22013-06-12 13:25:59 -070055
56#define POWER_SUPPLY_SUBSYSTEM "power_supply"
57
58// epoll events: uevent, wakealarm, binder
59#define MAX_EPOLL_EVENTS 3
60static int uevent_fd;
61static int wakealarm_fd;
62static int binder_fd;
63
64// -1 for no epoll timeout
65static int awake_poll_interval = -1;
66
Todd Poynor10b235e2013-08-07 15:25:14 -070067static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070068
69static BatteryMonitor* gBatteryMonitor;
70
71static bool nosvcmgr;
72
73static void wakealarm_set_interval(int interval) {
74 struct itimerspec itval;
75
76 if (wakealarm_fd == -1)
77 return;
78
79 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -070080
81 if (interval == -1)
82 interval = 0;
83
Todd Poynor752faf22013-06-12 13:25:59 -070084 itval.it_interval.tv_sec = interval;
85 itval.it_interval.tv_nsec = 0;
86 itval.it_value.tv_sec = interval;
87 itval.it_value.tv_nsec = 0;
88
89 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
90 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
91}
92
93static void battery_update(void) {
94 // Fast wake interval when on charger (watch for overheat);
95 // slow wake interval when on battery (watch for drained battery).
96
97 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -070098 healthd_config.periodic_chores_interval_fast :
99 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700100
101 if (new_wake_interval != wakealarm_wake_interval)
102 wakealarm_set_interval(new_wake_interval);
103
104 // During awake periods poll at fast rate. If wake alarm is set at fast
105 // rate then just use the alarm; if wake alarm is set at slow rate then
106 // poll at fast rate while awake and let alarm wake up at slow rate when
107 // asleep.
108
Todd Poynor9face5c2013-08-08 12:24:53 -0700109 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700110 awake_poll_interval = -1;
111 else
112 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700113 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
114 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700115}
116
117static void periodic_chores() {
118 battery_update();
119}
120
121static void uevent_init(void) {
122 uevent_fd = uevent_open_socket(64*1024, true);
123
124 if (uevent_fd >= 0)
125 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
126 else
127 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
128}
129
130#define UEVENT_MSG_LEN 1024
131static void uevent_event(void) {
132 char msg[UEVENT_MSG_LEN+2];
133 char *cp;
134 int n;
135
136 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
137 if (n <= 0)
138 return;
139 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
140 return;
141
142 msg[n] = '\0';
143 msg[n+1] = '\0';
144 cp = msg;
145
146 while (*cp) {
147 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
148 battery_update();
149 break;
150 }
151
152 /* advance to after the next \0 */
153 while (*cp++)
154 ;
155 }
156}
157
158static void wakealarm_init(void) {
159 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
160 if (wakealarm_fd == -1) {
161 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
162 return;
163 }
164
Todd Poynor9face5c2013-08-08 12:24:53 -0700165 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700166}
167
168static void wakealarm_event(void) {
169 unsigned long long wakeups;
170
171 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
172 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm_fd failed\n");
173 return;
174 }
175
176 periodic_chores();
177}
178
179static void binder_init(void) {
180 ProcessState::self()->setThreadPoolMaxThreadCount(0);
181 IPCThreadState::self()->disableBackgroundScheduling(true);
182 IPCThreadState::self()->setupPolling(&binder_fd);
183}
184
185static void binder_event(void) {
186 IPCThreadState::self()->handlePolledCommands();
187}
188
189static void healthd_mainloop(void) {
190 struct epoll_event ev;
191 int epollfd;
192 int maxevents = 0;
193
194 epollfd = epoll_create(MAX_EPOLL_EVENTS);
195 if (epollfd == -1) {
196 KLOG_ERROR(LOG_TAG,
197 "healthd_mainloop: epoll_create failed; errno=%d\n",
198 errno);
199 return;
200 }
201
202 if (uevent_fd >= 0) {
203 ev.events = EPOLLIN | EPOLLWAKEUP;
204 ev.data.ptr = (void *)uevent_event;
205 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, uevent_fd, &ev) == -1)
206 KLOG_ERROR(LOG_TAG,
207 "healthd_mainloop: epoll_ctl for uevent_fd failed; errno=%d\n",
208 errno);
209 else
210 maxevents++;
211 }
212
213 if (wakealarm_fd >= 0) {
214 ev.events = EPOLLIN | EPOLLWAKEUP;
215 ev.data.ptr = (void *)wakealarm_event;
216 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, wakealarm_fd, &ev) == -1)
217 KLOG_ERROR(LOG_TAG,
218 "healthd_mainloop: epoll_ctl for wakealarm_fd failed; errno=%d\n",
219 errno);
220 else
221 maxevents++;
222 }
223
224 if (binder_fd >= 0) {
225 ev.events = EPOLLIN | EPOLLWAKEUP;
226 ev.data.ptr= (void *)binder_event;
227 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, binder_fd, &ev) == -1)
228 KLOG_ERROR(LOG_TAG,
229 "healthd_mainloop: epoll_ctl for binder_fd failed; errno=%d\n",
230 errno);
231 else
232 maxevents++;
233 }
234
235 while (1) {
236 struct epoll_event events[maxevents];
237 int nevents;
238
239 IPCThreadState::self()->flushCommands();
240 nevents = epoll_wait(epollfd, events, maxevents, awake_poll_interval);
241
242 if (nevents == -1) {
243 if (errno == EINTR)
244 continue;
245 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
246 break;
247 }
248
249 for (int n = 0; n < nevents; ++n) {
250 if (events[n].data.ptr)
251 (*(void (*)())events[n].data.ptr)();
252 }
253 }
254
255 return;
256}
257
258int main(int argc, char **argv) {
259 int ch;
260
261 klog_set_level(KLOG_LEVEL);
262
263 while ((ch = getopt(argc, argv, "n")) != -1) {
264 switch (ch) {
265 case 'n':
266 nosvcmgr = true;
267 break;
268 case '?':
269 default:
270 KLOG_WARNING(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
271 }
272 }
273
Todd Poynor9face5c2013-08-08 12:24:53 -0700274 healthd_board_init(&healthd_config);
Todd Poynor752faf22013-06-12 13:25:59 -0700275 wakealarm_init();
276 uevent_init();
277 binder_init();
278 gBatteryMonitor = new BatteryMonitor();
Todd Poynorf5d30122013-08-12 17:03:35 -0700279 gBatteryMonitor->init(&healthd_config, nosvcmgr);
Todd Poynor752faf22013-06-12 13:25:59 -0700280
281 healthd_mainloop();
282 return 0;
283}