1 /*
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 "android.hardware.health@2.0-impl"
18 #define KLOG_LEVEL 6
19
20 #include <healthd/BatteryMonitor.h>
21 #include <healthd/healthd.h>
22
23 #include <batteryservice/BatteryService.h>
24 #include <cutils/klog.h>
25 #include <cutils/uevent.h>
26 #include <errno.h>
27 #include <libgen.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/epoll.h>
32 #include <sys/timerfd.h>
33 #include <unistd.h>
34 #include <utils/Errors.h>
35
36 #include <health2/Health.h>
37
38 using namespace android;
39
40 // Periodic chores fast interval in seconds
41 #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
42 // Periodic chores fast interval in seconds
43 #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
44
45 static struct healthd_config healthd_config = {
46 .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
47 .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
48 .batteryStatusPath = String8(String8::kEmptyString),
49 .batteryHealthPath = String8(String8::kEmptyString),
50 .batteryPresentPath = String8(String8::kEmptyString),
51 .batteryCapacityPath = String8(String8::kEmptyString),
52 .batteryVoltagePath = String8(String8::kEmptyString),
53 .batteryTemperaturePath = String8(String8::kEmptyString),
54 .batteryTechnologyPath = String8(String8::kEmptyString),
55 .batteryCurrentNowPath = String8(String8::kEmptyString),
56 .batteryCurrentAvgPath = String8(String8::kEmptyString),
57 .batteryChargeCounterPath = String8(String8::kEmptyString),
58 .batteryFullChargePath = String8(String8::kEmptyString),
59 .batteryCycleCountPath = String8(String8::kEmptyString),
60 .energyCounter = NULL,
61 .boot_min_cap = 0,
62 .screen_on = NULL,
63 };
64
65 static int eventct;
66 static int epollfd;
67
68 #define POWER_SUPPLY_SUBSYSTEM "power_supply"
69
70 static int uevent_fd;
71 static int wakealarm_fd;
72
73 // -1 for no epoll timeout
74 static int awake_poll_interval = -1;
75
76 static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
77
78 using ::android::hardware::health::V2_0::implementation::Health;
79
80 struct healthd_mode_ops* healthd_mode_ops = nullptr;
81
healthd_register_event(int fd,void (* handler)(uint32_t),EventWakeup wakeup)82 int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
83 struct epoll_event ev;
84
85 ev.events = EPOLLIN;
86
87 if (wakeup == EVENT_WAKEUP_FD) ev.events |= EPOLLWAKEUP;
88
89 ev.data.ptr = (void*)handler;
90 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
91 KLOG_ERROR(LOG_TAG, "epoll_ctl failed; errno=%d\n", errno);
92 return -1;
93 }
94
95 eventct++;
96 return 0;
97 }
98
wakealarm_set_interval(int interval)99 static void wakealarm_set_interval(int interval) {
100 struct itimerspec itval;
101
102 if (wakealarm_fd == -1) return;
103
104 wakealarm_wake_interval = interval;
105
106 if (interval == -1) interval = 0;
107
108 itval.it_interval.tv_sec = interval;
109 itval.it_interval.tv_nsec = 0;
110 itval.it_value.tv_sec = interval;
111 itval.it_value.tv_nsec = 0;
112
113 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
114 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
115 }
116
healthd_battery_update_internal(bool charger_online)117 void healthd_battery_update_internal(bool charger_online) {
118 // Fast wake interval when on charger (watch for overheat);
119 // slow wake interval when on battery (watch for drained battery).
120
121 int new_wake_interval = charger_online ? healthd_config.periodic_chores_interval_fast
122 : healthd_config.periodic_chores_interval_slow;
123
124 if (new_wake_interval != wakealarm_wake_interval) wakealarm_set_interval(new_wake_interval);
125
126 // During awake periods poll at fast rate. If wake alarm is set at fast
127 // rate then just use the alarm; if wake alarm is set at slow rate then
128 // poll at fast rate while awake and let alarm wake up at slow rate when
129 // asleep.
130
131 if (healthd_config.periodic_chores_interval_fast == -1)
132 awake_poll_interval = -1;
133 else
134 awake_poll_interval = new_wake_interval == healthd_config.periodic_chores_interval_fast
135 ? -1
136 : healthd_config.periodic_chores_interval_fast * 1000;
137 }
138
healthd_battery_update(void)139 static void healthd_battery_update(void) {
140 Health::getImplementation()->update();
141 }
142
periodic_chores()143 static void periodic_chores() {
144 healthd_battery_update();
145 }
146
147 #define UEVENT_MSG_LEN 2048
uevent_event(uint32_t)148 static void uevent_event(uint32_t /*epevents*/) {
149 char msg[UEVENT_MSG_LEN + 2];
150 char* cp;
151 int n;
152
153 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
154 if (n <= 0) return;
155 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
156 return;
157
158 msg[n] = '\0';
159 msg[n + 1] = '\0';
160 cp = msg;
161
162 while (*cp) {
163 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
164 healthd_battery_update();
165 break;
166 }
167
168 /* advance to after the next \0 */
169 while (*cp++)
170 ;
171 }
172 }
173
uevent_init(void)174 static void uevent_init(void) {
175 uevent_fd = uevent_open_socket(64 * 1024, true);
176
177 if (uevent_fd < 0) {
178 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
179 return;
180 }
181
182 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
183 if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD))
184 KLOG_ERROR(LOG_TAG, "register for uevent events failed\n");
185 }
186
wakealarm_event(uint32_t)187 static void wakealarm_event(uint32_t /*epevents*/) {
188 unsigned long long wakeups;
189
190 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
191 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
192 return;
193 }
194
195 periodic_chores();
196 }
197
wakealarm_init(void)198 static void wakealarm_init(void) {
199 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
200 if (wakealarm_fd == -1) {
201 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
202 return;
203 }
204
205 if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD))
206 KLOG_ERROR(LOG_TAG, "Registration of wakealarm event failed\n");
207
208 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
209 }
210
healthd_mainloop(void)211 static void healthd_mainloop(void) {
212 int nevents = 0;
213 while (1) {
214 struct epoll_event events[eventct];
215 int timeout = awake_poll_interval;
216 int mode_timeout;
217
218 /* Don't wait for first timer timeout to run periodic chores */
219 if (!nevents) periodic_chores();
220
221 healthd_mode_ops->heartbeat();
222
223 mode_timeout = healthd_mode_ops->preparetowait();
224 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout)) timeout = mode_timeout;
225 nevents = epoll_wait(epollfd, events, eventct, timeout);
226 if (nevents == -1) {
227 if (errno == EINTR) continue;
228 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
229 break;
230 }
231
232 for (int n = 0; n < nevents; ++n) {
233 if (events[n].data.ptr) (*(void (*)(int))events[n].data.ptr)(events[n].events);
234 }
235 }
236
237 return;
238 }
239
healthd_init()240 static int healthd_init() {
241 epollfd = epoll_create1(EPOLL_CLOEXEC);
242 if (epollfd == -1) {
243 KLOG_ERROR(LOG_TAG, "epoll_create1 failed; errno=%d\n", errno);
244 return -1;
245 }
246
247 healthd_mode_ops->init(&healthd_config);
248 wakealarm_init();
249 uevent_init();
250
251 return 0;
252 }
253
healthd_main()254 int healthd_main() {
255 int ret;
256
257 klog_set_level(KLOG_LEVEL);
258
259 if (!healthd_mode_ops) {
260 KLOG_ERROR("healthd ops not set, exiting\n");
261 exit(1);
262 }
263
264 ret = healthd_init();
265 if (ret) {
266 KLOG_ERROR("Initialization failed, exiting\n");
267 exit(2);
268 }
269
270 healthd_mainloop();
271 KLOG_ERROR("Main loop terminated, exiting\n");
272 return 3;
273 }
274