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