blob: 3006644611bf1ff455abaca598d3118e89fc596c [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#include <health2/Health.h>
Yifan Hong2763df82017-09-19 17:57:50 -070037
Sandeep Patil526f8cf2016-11-01 16:41:56 -070038using namespace android;
39
Yifan Hong25eb37a2017-11-13 13:31:35 -080040// 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)
Sandeep Patil526f8cf2016-11-01 16:41:56 -070044
45static 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
65static int eventct;
66static int epollfd;
67
68#define POWER_SUPPLY_SUBSYSTEM "power_supply"
69
70// epoll_create() parameter is actually unused
71#define MAX_EPOLL_EVENTS 40
72static int uevent_fd;
73static int wakealarm_fd;
74
75// -1 for no epoll timeout
76static int awake_poll_interval = -1;
77
78static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
79
Yifan Hong9cffa8a2017-11-07 14:59:14 -080080using ::android::hardware::health::V2_0::implementation::Health;
Sandeep Patil526f8cf2016-11-01 16:41:56 -070081
Yifan Hong2763df82017-09-19 17:57:50 -070082struct healthd_mode_ops *healthd_mode_ops = nullptr;
Sandeep Patil526f8cf2016-11-01 16:41:56 -070083
84int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
85 struct epoll_event ev;
86
87 ev.events = EPOLLIN;
88
89 if (wakeup == EVENT_WAKEUP_FD)
90 ev.events |= EPOLLWAKEUP;
91
92 ev.data.ptr = (void *)handler;
93 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
94 KLOG_ERROR(LOG_TAG,
95 "epoll_ctl failed; errno=%d\n", errno);
96 return -1;
97 }
98
99 eventct++;
100 return 0;
101}
102
103static void wakealarm_set_interval(int interval) {
104 struct itimerspec itval;
105
106 if (wakealarm_fd == -1)
107 return;
108
109 wakealarm_wake_interval = interval;
110
111 if (interval == -1)
112 interval = 0;
113
114 itval.it_interval.tv_sec = interval;
115 itval.it_interval.tv_nsec = 0;
116 itval.it_value.tv_sec = interval;
117 itval.it_value.tv_nsec = 0;
118
119 if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
120 KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
121}
122
Yifan Hong2763df82017-09-19 17:57:50 -0700123void healthd_battery_update_internal(bool charger_online) {
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700124 // Fast wake interval when on charger (watch for overheat);
125 // slow wake interval when on battery (watch for drained battery).
126
Yifan Hong2763df82017-09-19 17:57:50 -0700127 int new_wake_interval = charger_online ? healthd_config.periodic_chores_interval_fast
128 : healthd_config.periodic_chores_interval_slow;
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700129
130 if (new_wake_interval != wakealarm_wake_interval)
131 wakealarm_set_interval(new_wake_interval);
132
133 // During awake periods poll at fast rate. If wake alarm is set at fast
134 // rate then just use the alarm; if wake alarm is set at slow rate then
135 // poll at fast rate while awake and let alarm wake up at slow rate when
136 // asleep.
137
138 if (healthd_config.periodic_chores_interval_fast == -1)
139 awake_poll_interval = -1;
140 else
141 awake_poll_interval =
142 new_wake_interval == healthd_config.periodic_chores_interval_fast ?
143 -1 : healthd_config.periodic_chores_interval_fast * 1000;
144}
145
Yifan Hong25eb37a2017-11-13 13:31:35 -0800146static void healthd_battery_update(void) {
Yifan Hong9cffa8a2017-11-07 14:59:14 -0800147 Health::getImplementation()->update();
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700148}
149
150static void periodic_chores() {
151 healthd_battery_update();
152}
153
154#define UEVENT_MSG_LEN 2048
155static void uevent_event(uint32_t /*epevents*/) {
156 char msg[UEVENT_MSG_LEN+2];
157 char *cp;
158 int n;
159
160 n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
161 if (n <= 0)
162 return;
163 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
164 return;
165
166 msg[n] = '\0';
167 msg[n+1] = '\0';
168 cp = msg;
169
170 while (*cp) {
171 if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
172 healthd_battery_update();
173 break;
174 }
175
176 /* advance to after the next \0 */
177 while (*cp++)
178 ;
179 }
180}
181
182static void uevent_init(void) {
183 uevent_fd = uevent_open_socket(64*1024, true);
184
185 if (uevent_fd < 0) {
186 KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
187 return;
188 }
189
190 fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
191 if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD))
192 KLOG_ERROR(LOG_TAG,
193 "register for uevent events failed\n");
194}
195
196static void wakealarm_event(uint32_t /*epevents*/) {
197 unsigned long long wakeups;
198
199 if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
200 KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
201 return;
202 }
203
204 periodic_chores();
205}
206
207static void wakealarm_init(void) {
208 wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
209 if (wakealarm_fd == -1) {
210 KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
211 return;
212 }
213
214 if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD))
215 KLOG_ERROR(LOG_TAG,
216 "Registration of wakealarm event failed\n");
217
218 wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
219}
220
221static void healthd_mainloop(void) {
222 int nevents = 0;
223 while (1) {
224 struct epoll_event events[eventct];
225 int timeout = awake_poll_interval;
226 int mode_timeout;
227
228 /* Don't wait for first timer timeout to run periodic chores */
229 if (!nevents)
230 periodic_chores();
231
232 healthd_mode_ops->heartbeat();
233
234 mode_timeout = healthd_mode_ops->preparetowait();
235 if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
236 timeout = mode_timeout;
237 nevents = epoll_wait(epollfd, events, eventct, timeout);
238 if (nevents == -1) {
239 if (errno == EINTR)
240 continue;
241 KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
242 break;
243 }
244
245 for (int n = 0; n < nevents; ++n) {
246 if (events[n].data.ptr)
247 (*(void (*)(int))events[n].data.ptr)(events[n].events);
248 }
249 }
250
251 return;
252}
253
254static int healthd_init() {
255 epollfd = epoll_create(MAX_EPOLL_EVENTS);
256 if (epollfd == -1) {
257 KLOG_ERROR(LOG_TAG,
258 "epoll_create failed; errno=%d\n",
259 errno);
260 return -1;
261 }
262
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700263 healthd_mode_ops->init(&healthd_config);
264 wakealarm_init();
265 uevent_init();
Yifan Hong2763df82017-09-19 17:57:50 -0700266
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700267 return 0;
268}
269
270int healthd_main() {
271 int ret;
272
273 klog_set_level(KLOG_LEVEL);
274
275 if (!healthd_mode_ops) {
276 KLOG_ERROR("healthd ops not set, exiting\n");
277 exit(1);
278 }
279
280 ret = healthd_init();
281 if (ret) {
282 KLOG_ERROR("Initialization failed, exiting\n");
283 exit(2);
284 }
285
286 healthd_mainloop();
287 KLOG_ERROR("Main loop terminated, exiting\n");
288 return 3;
289}