blob: 62f4d1163d74353b185829083d6c41475a073989 [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
57#define POWER_SUPPLY_SUBSYSTEM "power_supply"
58
59// epoll events: uevent, wakealarm, binder
60#define MAX_EPOLL_EVENTS 3
61static int uevent_fd;
62static int wakealarm_fd;
63static int binder_fd;
64
65// -1 for no epoll timeout
66static int awake_poll_interval = -1;
67
Todd Poynor10b235e2013-08-07 15:25:14 -070068static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
Todd Poynor752faf22013-06-12 13:25:59 -070069
70static BatteryMonitor* gBatteryMonitor;
71
72static bool nosvcmgr;
73
74static void wakealarm_set_interval(int interval) {
75 struct itimerspec itval;
76
77 if (wakealarm_fd == -1)
78 return;
79
80 wakealarm_wake_interval = interval;
Todd Poynor10b235e2013-08-07 15:25:14 -070081
82 if (interval == -1)
83 interval = 0;
84
Todd Poynor752faf22013-06-12 13:25:59 -070085 itval.it_interval.tv_sec = interval;
86 itval.it_interval.tv_nsec = 0;
87 itval.it_value.tv_sec = interval;
88 itval.it_value.tv_nsec = 0;
89
90 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
91 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
92}
93
Todd Poynor7b27f272013-09-06 15:14:24 -070094status_t healthd_get_property(int id, struct BatteryProperty *val) {
95 return gBatteryMonitor->getProperty(id, val);
96}
97
98void healthd_battery_update(void) {
Todd Poynor752faf22013-06-12 13:25:59 -070099 // Fast wake interval when on charger (watch for overheat);
100 // slow wake interval when on battery (watch for drained battery).
101
102 int new_wake_interval = gBatteryMonitor->update() ?
Todd Poynor9face5c2013-08-08 12:24:53 -0700103 healthd_config.periodic_chores_interval_fast :
104 healthd_config.periodic_chores_interval_slow;
Todd Poynor752faf22013-06-12 13:25:59 -0700105
106 if (new_wake_interval != wakealarm_wake_interval)
107 wakealarm_set_interval(new_wake_interval);
108
109 // During awake periods poll at fast rate. If wake alarm is set at fast
110 // rate then just use the alarm; if wake alarm is set at slow rate then
111 // poll at fast rate while awake and let alarm wake up at slow rate when
112 // asleep.
113
Todd Poynor9face5c2013-08-08 12:24:53 -0700114 if (healthd_config.periodic_chores_interval_fast == -1)
Todd Poynor10b235e2013-08-07 15:25:14 -0700115 awake_poll_interval = -1;
116 else
117 awake_poll_interval =
Todd Poynor9face5c2013-08-08 12:24:53 -0700118 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
119 -1 : healthd_config.periodic_chores_interval_fast * 1000;
Todd Poynor752faf22013-06-12 13:25:59 -0700120}
121
122static void periodic_chores() {
Todd Poynor7b27f272013-09-06 15:14:24 -0700123 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700124}
125
126static void uevent_init(void) {
127 uevent_fd = uevent_open_socket(64*1024, true);
128
129 if (uevent_fd >= 0)
130 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
131 else
132 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
133}
134
135#define UEVENT_MSG_LEN 1024
136static void uevent_event(void) {
137 char msg[UEVENT_MSG_LEN+2];
138 char *cp;
139 int n;
140
141 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
142 if (n <= 0)
143 return;
144 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
145 return;
146
147 msg[n] = '\0';
148 msg[n+1] = '\0';
149 cp = msg;
150
151 while (*cp) {
152 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
Todd Poynor7b27f272013-09-06 15:14:24 -0700153 healthd_battery_update();
Todd Poynor752faf22013-06-12 13:25:59 -0700154 break;
155 }
156
157 /* advance to after the next \0 */
158 while (*cp++)
159 ;
160 }
161}
162
163static void wakealarm_init(void) {
164 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
165 if (wakealarm_fd == -1) {
166 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
167 return;
168 }
169
Todd Poynor9face5c2013-08-08 12:24:53 -0700170 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
Todd Poynor752faf22013-06-12 13:25:59 -0700171}
172
173static void wakealarm_event(void) {
174 unsigned long long wakeups;
175
176 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
177 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm_fd failed\n");
178 return;
179 }
180
181 periodic_chores();
182}
183
184static void binder_init(void) {
185 ProcessState::self()->setThreadPoolMaxThreadCount(0);
186 IPCThreadState::self()->disableBackgroundScheduling(true);
187 IPCThreadState::self()->setupPolling(&binder_fd);
188}
189
190static void binder_event(void) {
191 IPCThreadState::self()->handlePolledCommands();
192}
193
194static void healthd_mainloop(void) {
195 struct epoll_event ev;
196 int epollfd;
197 int maxevents = 0;
198
199 epollfd = epoll_create(MAX_EPOLL_EVENTS);
200 if (epollfd == -1) {
201 KLOG_ERROR(LOG_TAG,
202 "healthd_mainloop: epoll_create failed; errno=%d\n",
203 errno);
204 return;
205 }
206
207 if (uevent_fd >= 0) {
208 ev.events = EPOLLIN | EPOLLWAKEUP;
209 ev.data.ptr = (void *)uevent_event;
210 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, uevent_fd, &ev) == -1)
211 KLOG_ERROR(LOG_TAG,
212 "healthd_mainloop: epoll_ctl for uevent_fd failed; errno=%d\n",
213 errno);
214 else
215 maxevents++;
216 }
217
218 if (wakealarm_fd >= 0) {
219 ev.events = EPOLLIN | EPOLLWAKEUP;
220 ev.data.ptr = (void *)wakealarm_event;
221 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, wakealarm_fd, &ev) == -1)
222 KLOG_ERROR(LOG_TAG,
223 "healthd_mainloop: epoll_ctl for wakealarm_fd failed; errno=%d\n",
224 errno);
225 else
226 maxevents++;
227 }
228
229 if (binder_fd >= 0) {
230 ev.events = EPOLLIN | EPOLLWAKEUP;
231 ev.data.ptr= (void *)binder_event;
232 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, binder_fd, &ev) == -1)
233 KLOG_ERROR(LOG_TAG,
234 "healthd_mainloop: epoll_ctl for binder_fd failed; errno=%d\n",
235 errno);
236 else
237 maxevents++;
238 }
239
240 while (1) {
241 struct epoll_event events[maxevents];
242 int nevents;
243
244 IPCThreadState::self()->flushCommands();
245 nevents = epoll_wait(epollfd, events, maxevents, awake_poll_interval);
246
247 if (nevents == -1) {
248 if (errno == EINTR)
249 continue;
250 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
251 break;
252 }
253
254 for (int n = 0; n < nevents; ++n) {
255 if (events[n].data.ptr)
256 (*(void (*)())events[n].data.ptr)();
257 }
Todd Poynorff9ec2d2013-09-09 14:30:55 -0700258
259 if (!nevents)
260 periodic_chores();
Todd Poynor752faf22013-06-12 13:25:59 -0700261 }
262
263 return;
264}
265
266int main(int argc, char **argv) {
267 int ch;
268
269 klog_set_level(KLOG_LEVEL);
270
271 while ((ch = getopt(argc, argv, "n")) != -1) {
272 switch (ch) {
273 case 'n':
274 nosvcmgr = true;
275 break;
276 case '?':
277 default:
278 KLOG_WARNING(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
279 }
280 }
281
Todd Poynor9face5c2013-08-08 12:24:53 -0700282 healthd_board_init(&healthd_config);
Todd Poynor752faf22013-06-12 13:25:59 -0700283 wakealarm_init();
284 uevent_init();
285 binder_init();
286 gBatteryMonitor = new BatteryMonitor();
Todd Poynorf5d30122013-08-12 17:03:35 -0700287 gBatteryMonitor->init(&healthd_config, nosvcmgr);
Todd Poynor752faf22013-06-12 13:25:59 -0700288
289 healthd_mainloop();
290 return 0;
291}