blob: 19e600f8650eafc05c648d3a1c320053c7ea8ecd [file] [log] [blame]
Sandeep Patil526f8cf2016-11-01 16:41:56 -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-common"
18#define KLOG_LEVEL 6
19
20#include <healthd/healthd.h>
21#include <healthd/BatteryMonitor.h>
22
23#include <errno.h>
24#include <libgen.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <batteryservice/BatteryService.h>
30#include <cutils/klog.h>
31#include <cutils/uevent.h>
32#include <sys/epoll.h>
33#include <sys/timerfd.h>
34#include <utils/Errors.h>
35
Yifan Hong2763df82017-09-19 17:57:50 -070036#ifdef HEALTHD_USE_HEALTH_2_0
37#include <health2/Health.h>
38#endif
39
Sandeep Patil526f8cf2016-11-01 16:41:56 -070040using namespace android;
41
42#ifndef BOARD_PERIODIC_CHORES_INTERVAL_FAST
43 // Periodic chores fast interval in seconds
44 #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
45#else
46 #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (BOARD_PERIODIC_CHORES_INTERVAL_FAST)
47#endif
48
49#ifndef BOARD_PERIODIC_CHORES_INTERVAL_SLOW
50 // Periodic chores fast interval in seconds
51 #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
52#else
53 #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
54#endif
55
56static struct healthd_config healthd_config = {
57 .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
58 .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
59 .batteryStatusPath = String8(String8::kEmptyString),
60 .batteryHealthPath = String8(String8::kEmptyString),
61 .batteryPresentPath = String8(String8::kEmptyString),
62 .batteryCapacityPath = String8(String8::kEmptyString),
63 .batteryVoltagePath = String8(String8::kEmptyString),
64 .batteryTemperaturePath = String8(String8::kEmptyString),
65 .batteryTechnologyPath = String8(String8::kEmptyString),
66 .batteryCurrentNowPath = String8(String8::kEmptyString),
67 .batteryCurrentAvgPath = String8(String8::kEmptyString),
68 .batteryChargeCounterPath = String8(String8::kEmptyString),
69 .batteryFullChargePath = String8(String8::kEmptyString),
70 .batteryCycleCountPath = String8(String8::kEmptyString),
71 .energyCounter = NULL,
72 .boot_min_cap = 0,
73 .screen_on = NULL,
74};
75
76static int eventct;
77static int epollfd;
78
79#define POWER_SUPPLY_SUBSYSTEM "power_supply"
80
81// epoll_create() parameter is actually unused
82#define MAX_EPOLL_EVENTS 40
83static int uevent_fd;
84static int wakealarm_fd;
85
86// -1 for no epoll timeout
87static int awake_poll_interval = -1;
88
89static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
90
Yifan Hong2763df82017-09-19 17:57:50 -070091#ifndef HEALTHD_USE_HEALTH_2_0
92static BatteryMonitor* gBatteryMonitor = nullptr;
93#else
94extern sp<::android::hardware::health::V2_0::IHealth> gHealth;
95#endif
Sandeep Patil526f8cf2016-11-01 16:41:56 -070096
Yifan Hong2763df82017-09-19 17:57:50 -070097struct healthd_mode_ops *healthd_mode_ops = nullptr;
Sandeep Patil526f8cf2016-11-01 16:41:56 -070098
99int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
100 struct epoll_event ev;
101
102 ev.events = EPOLLIN;
103
104 if (wakeup == EVENT_WAKEUP_FD)
105 ev.events |= EPOLLWAKEUP;
106
107 ev.data.ptr = (void *)handler;
108 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
109 KLOG_ERROR(LOG_TAG,
110 "epoll_ctl failed; errno=%d\n", errno);
111 return -1;
112 }
113
114 eventct++;
115 return 0;
116}
117
118static void wakealarm_set_interval(int interval) {
119 struct itimerspec itval;
120
121 if (wakealarm_fd == -1)
122 return;
123
124 wakealarm_wake_interval = interval;
125
126 if (interval == -1)
127 interval = 0;
128
129 itval.it_interval.tv_sec = interval;
130 itval.it_interval.tv_nsec = 0;
131 itval.it_value.tv_sec = interval;
132 itval.it_value.tv_nsec = 0;
133
134 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
135 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
136}
137
Yifan Hong2763df82017-09-19 17:57:50 -0700138#ifdef HEALTHD_USE_HEALTH_2_0
139status_t convertStatus(android::hardware::health::V2_0::Result r) {
140 using android::hardware::health::V2_0::Result;
141 switch(r) {
142 case Result::SUCCESS: return OK;
143 case Result::NOT_SUPPORTED: return BAD_VALUE;
144 case Result::NOT_FOUND: return NAME_NOT_FOUND;
145 case Result::CALLBACK_DIED: return DEAD_OBJECT;
146 case Result::UNKNOWN: // fallthrough
147 default:
148 return UNKNOWN_ERROR;
149 }
150}
151#endif
152
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700153status_t healthd_get_property(int id, struct BatteryProperty *val) {
Yifan Hong2763df82017-09-19 17:57:50 -0700154#ifndef HEALTHD_USE_HEALTH_2_0
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700155 return gBatteryMonitor->getProperty(id, val);
Yifan Hong2763df82017-09-19 17:57:50 -0700156#else
157 using android::hardware::health::V1_0::BatteryStatus;
158 using android::hardware::health::V2_0::Result;
159 val->valueInt64 = INT64_MIN;
160 status_t err = UNKNOWN_ERROR;
161 switch (id) {
162 case BATTERY_PROP_CHARGE_COUNTER: {
163 gHealth->getChargeCounter([&](Result r, int32_t v) {
164 err = convertStatus(r);
165 val->valueInt64 = v;
166 });
167 break;
168 }
169 case BATTERY_PROP_CURRENT_NOW: {
170 gHealth->getCurrentNow([&](Result r, int32_t v) {
171 err = convertStatus(r);
172 val->valueInt64 = v;
173 });
174 break;
175 }
176 case BATTERY_PROP_CURRENT_AVG: {
177 gHealth->getCurrentAverage([&](Result r, int32_t v) {
178 err = convertStatus(r);
179 val->valueInt64 = v;
180 });
181 break;
182 }
183 case BATTERY_PROP_CAPACITY: {
184 gHealth->getCapacity([&](Result r, int32_t v) {
185 err = convertStatus(r);
186 val->valueInt64 = v;
187 });
188 break;
189 }
190 case BATTERY_PROP_ENERGY_COUNTER: {
191 gHealth->getEnergyCounter([&](Result r, int64_t v) {
192 err = convertStatus(r);
193 val->valueInt64 = v;
194 });
195 break;
196 }
197 case BATTERY_PROP_BATTERY_STATUS: {
198 gHealth->getChargeStatus([&](Result r, BatteryStatus v) {
199 err = convertStatus(r);
200 val->valueInt64 = static_cast<int64_t>(v);
201 });
202 break;
203 }
204 default: {
205 err = BAD_VALUE;
206 break;
207 }
208 }
209 return err;
210#endif
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700211}
212
Yifan Hong2763df82017-09-19 17:57:50 -0700213void healthd_battery_update_internal(bool charger_online) {
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700214 // Fast wake interval when on charger (watch for overheat);
215 // slow wake interval when on battery (watch for drained battery).
216
Yifan Hong2763df82017-09-19 17:57:50 -0700217 int new_wake_interval = charger_online ? healthd_config.periodic_chores_interval_fast
218 : healthd_config.periodic_chores_interval_slow;
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700219
220 if (new_wake_interval != wakealarm_wake_interval)
221 wakealarm_set_interval(new_wake_interval);
222
223 // During awake periods poll at fast rate. If wake alarm is set at fast
224 // rate then just use the alarm; if wake alarm is set at slow rate then
225 // poll at fast rate while awake and let alarm wake up at slow rate when
226 // asleep.
227
228 if (healthd_config.periodic_chores_interval_fast == -1)
229 awake_poll_interval = -1;
230 else
231 awake_poll_interval =
232 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
233 -1 : healthd_config.periodic_chores_interval_fast * 1000;
234}
235
Yifan Hong2763df82017-09-19 17:57:50 -0700236void healthd_battery_update(void) {
237#ifndef HEALTHD_USE_HEALTH_2_0
238 healthd_battery_update_internal(gBatteryMonitor->update());
239#else
240 gHealth->update();
241#endif
242}
243
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700244void healthd_dump_battery_state(int fd) {
Yifan Hong2763df82017-09-19 17:57:50 -0700245#ifndef HEALTHD_USE_HEALTH_2_0
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700246 gBatteryMonitor->dumpState(fd);
Yifan Hong2763df82017-09-19 17:57:50 -0700247#else
248 native_handle_t* nativeHandle = native_handle_create(1, 0);
249 nativeHandle->data[0] = fd;
250 ::android::hardware::hidl_handle handle;
251 handle.setTo(nativeHandle, true /* shouldOwn */);
252 gHealth->debug(handle, {} /* options */);
253#endif
254
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700255 fsync(fd);
256}
257
258static void periodic_chores() {
259 healthd_battery_update();
260}
261
262#define UEVENT_MSG_LEN 2048
263static void uevent_event(uint32_t /*epevents*/) {
264 char msg[UEVENT_MSG_LEN+2];
265 char *cp;
266 int n;
267
268 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
269 if (n <= 0)
270 return;
271 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
272 return;
273
274 msg[n] = '\0';
275 msg[n+1] = '\0';
276 cp = msg;
277
278 while (*cp) {
279 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
280 healthd_battery_update();
281 break;
282 }
283
284 /* advance to after the next \0 */
285 while (*cp++)
286 ;
287 }
288}
289
290static void uevent_init(void) {
291 uevent_fd = uevent_open_socket(64*1024, true);
292
293 if (uevent_fd < 0) {
294 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
295 return;
296 }
297
298 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
299 if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD))
300 KLOG_ERROR(LOG_TAG,
301 "register for uevent events failed\n");
302}
303
304static void wakealarm_event(uint32_t /*epevents*/) {
305 unsigned long long wakeups;
306
307 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
308 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
309 return;
310 }
311
312 periodic_chores();
313}
314
315static void wakealarm_init(void) {
316 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
317 if (wakealarm_fd == -1) {
318 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
319 return;
320 }
321
322 if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD))
323 KLOG_ERROR(LOG_TAG,
324 "Registration of wakealarm event failed\n");
325
326 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
327}
328
329static void healthd_mainloop(void) {
330 int nevents = 0;
331 while (1) {
332 struct epoll_event events[eventct];
333 int timeout = awake_poll_interval;
334 int mode_timeout;
335
336 /* Don't wait for first timer timeout to run periodic chores */
337 if (!nevents)
338 periodic_chores();
339
340 healthd_mode_ops->heartbeat();
341
342 mode_timeout = healthd_mode_ops->preparetowait();
343 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
344 timeout = mode_timeout;
345 nevents = epoll_wait(epollfd, events, eventct, timeout);
346 if (nevents == -1) {
347 if (errno == EINTR)
348 continue;
349 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
350 break;
351 }
352
353 for (int n = 0; n < nevents; ++n) {
354 if (events[n].data.ptr)
355 (*(void (*)(int))events[n].data.ptr)(events[n].events);
356 }
357 }
358
359 return;
360}
361
362static int healthd_init() {
363 epollfd = epoll_create(MAX_EPOLL_EVENTS);
364 if (epollfd == -1) {
365 KLOG_ERROR(LOG_TAG,
366 "epoll_create failed; errno=%d\n",
367 errno);
368 return -1;
369 }
370
Yifan Hong2763df82017-09-19 17:57:50 -0700371#ifndef HEALTHD_USE_HEALTH_2_0
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700372 healthd_board_init(&healthd_config);
Yifan Hong2763df82017-09-19 17:57:50 -0700373#else
374 // healthd_board_* functions are removed in health@2.0
375#endif
376
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700377 healthd_mode_ops->init(&healthd_config);
378 wakealarm_init();
379 uevent_init();
Yifan Hong2763df82017-09-19 17:57:50 -0700380
381#ifndef HEALTHD_USE_HEALTH_2_0
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700382 gBatteryMonitor = new BatteryMonitor();
383 gBatteryMonitor->init(&healthd_config);
Yifan Hong2763df82017-09-19 17:57:50 -0700384#endif
385
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700386 return 0;
387}
388
389int healthd_main() {
390 int ret;
391
392 klog_set_level(KLOG_LEVEL);
393
394 if (!healthd_mode_ops) {
395 KLOG_ERROR("healthd ops not set, exiting\n");
396 exit(1);
397 }
398
399 ret = healthd_init();
400 if (ret) {
401 KLOG_ERROR("Initialization failed, exiting\n");
402 exit(2);
403 }
404
405 healthd_mainloop();
406 KLOG_ERROR("Main loop terminated, exiting\n");
407 return 3;
408}