Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The CyanogenMod 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 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <time.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <cutils/android_reboot.h> |
| 29 | #include <cutils/klog.h> |
| 30 | #include <cutils/misc.h> |
| 31 | #include <cutils/uevent.h> |
| 32 | #include <cutils/properties.h> |
| 33 | |
| 34 | #include <pthread.h> |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 35 | #include <linux/rtc.h> |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 36 | #include <linux/time.h> |
| 37 | #include <sys/epoll.h> |
| 38 | #include <sys/timerfd.h> |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 39 | |
Luca Stefani | cc7a8a1 | 2016-08-25 14:41:17 +0200 | [diff] [blame] | 40 | #include "healthd/healthd.h" |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 41 | #include "minui/minui.h" |
| 42 | |
| 43 | #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) |
| 44 | #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) |
| 45 | #define LOGI(x...) do { KLOG_INFO("charger", x); } while (0) |
| 46 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) |
| 47 | |
| 48 | struct frame { |
| 49 | int min_capacity; |
| 50 | GRSurface *surface; |
| 51 | }; |
| 52 | |
| 53 | struct animation { |
| 54 | struct frame *frames; |
| 55 | int cur_frame; |
| 56 | int num_frames; |
| 57 | }; |
| 58 | |
| 59 | static struct animation anim = { |
| 60 | .frames = NULL, |
| 61 | .cur_frame = 0, |
| 62 | .num_frames = 0, |
| 63 | }; |
| 64 | |
| 65 | static bool font_inited; |
| 66 | |
| 67 | static int draw_surface_centered(GRSurface* surface) |
| 68 | { |
| 69 | int w, h, x, y; |
| 70 | |
| 71 | w = gr_get_width(surface); |
| 72 | h = gr_get_height(surface); |
| 73 | x = (gr_fb_width() - w) / 2 ; |
| 74 | y = (gr_fb_height() - h) / 2 ; |
| 75 | |
| 76 | gr_blit(surface, 0, 0, w, h, x, y); |
| 77 | return y + h; |
| 78 | } |
| 79 | |
| 80 | #define STR_LEN 64 |
| 81 | static void draw_capacity(int capacity) |
| 82 | { |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 83 | char cap_str[STR_LEN]; |
| 84 | snprintf(cap_str, (STR_LEN - 1), "%d%%", capacity); |
Michael W | 3c68b13 | 2016-08-16 13:06:45 +0200 | [diff] [blame] | 85 | |
| 86 | struct frame *f = &anim.frames[0]; |
| 87 | int font_x, font_y; |
| 88 | gr_font_size(&font_x, &font_y); |
| 89 | int w = gr_measure(cap_str); |
| 90 | int h = gr_get_height(f->surface); |
| 91 | int x = (gr_fb_width() - w) / 2; |
| 92 | int y = (gr_fb_height() + h) / 2; |
| 93 | |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 94 | gr_color(255, 255, 255, 255); |
Michael W | 3c68b13 | 2016-08-16 13:06:45 +0200 | [diff] [blame] | 95 | gr_text(x, y + font_y / 2, cap_str, 0); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #ifdef QCOM_HARDWARE |
| 99 | enum alarm_time_type { |
| 100 | ALARM_TIME, |
| 101 | RTC_TIME, |
| 102 | }; |
| 103 | |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 104 | static int alarm_get_time(enum alarm_time_type time_type, |
| 105 | time_t *secs) |
| 106 | { |
| 107 | struct tm tm; |
| 108 | unsigned int cmd; |
| 109 | int rc, fd = -1; |
| 110 | |
| 111 | if (!secs) |
| 112 | return -1; |
| 113 | |
| 114 | fd = open("/dev/rtc0", O_RDONLY); |
| 115 | if (fd < 0) { |
| 116 | LOGE("Can't open rtc devfs node\n"); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | switch (time_type) { |
| 121 | case ALARM_TIME: |
| 122 | cmd = RTC_ALM_READ; |
| 123 | break; |
| 124 | case RTC_TIME: |
| 125 | cmd = RTC_RD_TIME; |
| 126 | break; |
| 127 | default: |
| 128 | LOGE("Invalid time type\n"); |
| 129 | goto err; |
| 130 | } |
| 131 | |
| 132 | rc = ioctl(fd, cmd, &tm); |
| 133 | if (rc < 0) { |
| 134 | LOGE("Unable to get time\n"); |
| 135 | goto err; |
| 136 | } |
| 137 | |
| 138 | *secs = mktime(&tm) + tm.tm_gmtoff; |
| 139 | if (*secs < 0) { |
| 140 | LOGE("Invalid seconds = %ld\n", *secs); |
| 141 | goto err; |
| 142 | } |
| 143 | |
| 144 | close(fd); |
| 145 | return 0; |
| 146 | |
| 147 | err: |
| 148 | close(fd); |
| 149 | return -1; |
| 150 | } |
| 151 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 152 | static void alarm_reboot(void) |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 153 | { |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 154 | LOGI("alarm time is up, reboot the phone!\n"); |
| 155 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, |
| 156 | LINUX_REBOOT_CMD_RESTART2, "rtc"); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static int alarm_set_reboot_time_and_wait(time_t secs) |
| 160 | { |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 161 | int rc, epollfd, nevents; |
| 162 | int fd = 0; |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 163 | struct timespec ts; |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 164 | epoll_event event, events[1]; |
| 165 | struct itimerspec itval; |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 166 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 167 | epollfd = epoll_create(1); |
| 168 | if (epollfd < 0) { |
| 169 | LOGE("epoll_create failed\n"); |
| 170 | goto err; |
| 171 | } |
| 172 | |
| 173 | fd = timerfd_create(CLOCK_REALTIME_ALARM, 0); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 174 | if (fd < 0) { |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 175 | LOGE("timerfd_create failed\n"); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 176 | goto err; |
| 177 | } |
| 178 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 179 | event.events = EPOLLIN | EPOLLWAKEUP; |
| 180 | event.data.ptr = (void *)alarm_reboot; |
| 181 | rc = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 182 | if (rc < 0) { |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 183 | LOGE("epoll_ctl(EPOLL_CTL_ADD) failed \n"); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 184 | goto err; |
| 185 | } |
| 186 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 187 | itval.it_value.tv_sec = secs; |
| 188 | itval.it_value.tv_nsec = 0; |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 189 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 190 | itval.it_interval.tv_sec = 0; |
| 191 | itval.it_interval.tv_nsec = 0; |
| 192 | |
| 193 | rc = timerfd_settime(fd, TFD_TIMER_ABSTIME, &itval, NULL); |
| 194 | if (rc < 0) { |
| 195 | LOGE("timerfd_settime failed %d\n",rc); |
| 196 | goto err; |
| 197 | } |
| 198 | |
| 199 | nevents = epoll_wait(epollfd, events, 1, -1); |
| 200 | |
| 201 | if (nevents <= 0) { |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 202 | LOGE("Unable to wait on alarm\n"); |
| 203 | goto err; |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 204 | } else { |
| 205 | (*(void (*)())events[0].data.ptr)(); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 208 | close(epollfd); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 209 | close(fd); |
| 210 | return 0; |
| 211 | |
| 212 | err: |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 213 | if (epollfd > 0) |
| 214 | close(epollfd); |
| 215 | |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 216 | if (fd >= 0) |
| 217 | close(fd); |
| 218 | return -1; |
| 219 | } |
| 220 | |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 221 | /* |
| 222 | * 10s the estimated time from timestamp of alarm thread start |
| 223 | * to timestamp of android boot completed. |
| 224 | */ |
| 225 | #define TIME_DELTA 10 |
| 226 | |
| 227 | /* seconds of 1 minute*/ |
| 228 | #define ONE_MINUTE 60 |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 229 | static void *alarm_thread(void *) |
| 230 | { |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 231 | time_t rtc_secs, alarm_secs; |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 232 | int rc; |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 233 | timespec ts; |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * to support power off alarm, the time |
| 237 | * stored in alarm register at latest |
| 238 | * shutdown time should be some time |
| 239 | * earlier than the actual alarm time |
| 240 | * set by user |
| 241 | */ |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 242 | rc = alarm_get_time(ALARM_TIME, &alarm_secs); |
| 243 | if (rc < 0 || !alarm_secs) |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 244 | goto err; |
| 245 | |
| 246 | rc = alarm_get_time(RTC_TIME, &rtc_secs); |
Luca Stefani | cff3cea | 2017-09-08 09:13:06 -0400 | [diff] [blame] | 247 | if (rc < 0 || !rtc_secs) |
| 248 | goto err; |
| 249 | LOGI("alarm time in rtc is %ld, rtc time is %ld\n", alarm_secs, rtc_secs); |
| 250 | |
| 251 | if (alarm_secs <= rtc_secs) { |
| 252 | clock_gettime(CLOCK_BOOTTIME, &ts); |
| 253 | |
| 254 | /* |
| 255 | * It is possible that last power off alarm time is up at this point. |
| 256 | * (alarm_secs + ONE_MINUTE) is the final alarm time to fire. |
| 257 | * (rtc_secs + ts.tv_sec + TIME_DELTA) is the estimated time of next |
| 258 | * boot completed to fire alarm. |
| 259 | * If the final alarm time is less than the estimated time of next boot |
| 260 | * completed to fire, that means it is not able to fire the last power |
| 261 | * off alarm at the right time, so just miss it. |
| 262 | */ |
| 263 | if (alarm_secs + ONE_MINUTE < rtc_secs + ts.tv_sec + TIME_DELTA) { |
| 264 | LOGE("alarm is missed\n"); |
| 265 | goto err; |
| 266 | } |
| 267 | |
| 268 | alarm_reboot(); |
| 269 | } |
| 270 | |
| 271 | rc = alarm_set_reboot_time_and_wait(alarm_secs); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 272 | if (rc < 0) |
| 273 | goto err; |
| 274 | |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 275 | err: |
| 276 | LOGE("Exit from alarm thread\n"); |
| 277 | return NULL; |
| 278 | } |
| 279 | #endif |
| 280 | |
| 281 | void healthd_board_init(struct healthd_config*) |
| 282 | { |
| 283 | pthread_t tid; |
| 284 | char value[PROP_VALUE_MAX]; |
| 285 | int rc = 0, scale_count = 0, i; |
| 286 | GRSurface **scale_frames; |
Luca Stefani | cc7a8a1 | 2016-08-25 14:41:17 +0200 | [diff] [blame] | 287 | int scale_fps; // Not in use (charger/cm_battery_scale doesn't have FPS text |
| 288 | // chunk). We are using hard-coded frame.disp_time instead. |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 289 | |
| 290 | rc = res_create_multi_display_surface("charger/cm_battery_scale", |
Luca Stefani | cc7a8a1 | 2016-08-25 14:41:17 +0200 | [diff] [blame] | 291 | &scale_fps, &scale_count, &scale_frames); |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 292 | if (rc < 0) { |
| 293 | LOGE("%s: Unable to load battery scale image", __func__); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | anim.frames = new frame[scale_count]; |
| 298 | anim.num_frames = scale_count; |
| 299 | for (i = 0; i < anim.num_frames; i++) { |
| 300 | anim.frames[i].surface = scale_frames[i]; |
| 301 | anim.frames[i].min_capacity = 100/(scale_count-1) * i; |
| 302 | } |
| 303 | |
| 304 | #ifdef QCOM_HARDWARE |
| 305 | property_get("ro.bootmode", value, ""); |
| 306 | if (!strcmp("charger", value)) { |
| 307 | rc = pthread_create(&tid, NULL, alarm_thread, NULL); |
| 308 | if (rc < 0) |
| 309 | LOGE("Create alarm thread failed\n"); |
| 310 | } |
| 311 | #endif |
| 312 | } |
| 313 | |
| 314 | int healthd_board_battery_update(struct android::BatteryProperties*) |
| 315 | { |
| 316 | // return 0 to log periodic polled battery status to kernel log |
| 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | void healthd_board_mode_charger_draw_battery( |
| 321 | struct android::BatteryProperties *batt_prop) |
| 322 | { |
| 323 | int start_frame = 0; |
| 324 | int capacity = -1; |
| 325 | |
| 326 | if (!font_inited) { |
| 327 | font_inited = true; |
| 328 | } |
| 329 | |
| 330 | if (batt_prop && batt_prop->batteryLevel >= 0) { |
| 331 | capacity = batt_prop->batteryLevel; |
| 332 | } |
| 333 | |
| 334 | if (anim.num_frames == 0 || capacity < 0) { |
| 335 | LOGE("%s: Unable to draw battery", __func__); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | // Find starting frame to display based on current capacity |
| 340 | for (start_frame = 1; start_frame < anim.num_frames; start_frame++) { |
| 341 | if (capacity < anim.frames[start_frame].min_capacity) |
| 342 | break; |
| 343 | } |
| 344 | // Always start from the level just below the current capacity |
| 345 | start_frame--; |
| 346 | |
| 347 | if (anim.cur_frame < start_frame) |
| 348 | anim.cur_frame = start_frame; |
| 349 | |
| 350 | draw_surface_centered(anim.frames[anim.cur_frame].surface); |
| 351 | draw_capacity(capacity); |
| 352 | // Move to next frame, with max possible frame at max_idx |
| 353 | anim.cur_frame = ((anim.cur_frame + 1) % anim.num_frames); |
| 354 | } |
| 355 | |
| 356 | void healthd_board_mode_charger_battery_update( |
| 357 | struct android::BatteryProperties*) |
| 358 | { |
| 359 | } |
| 360 | |
Adrian DC | 638e2f4 | 2016-08-14 11:44:31 +0200 | [diff] [blame] | 361 | #ifdef HEALTHD_BACKLIGHT_PATH |
| 362 | #ifndef HEALTHD_BACKLIGHT_LEVEL |
| 363 | #define HEALTHD_BACKLIGHT_LEVEL 100 |
| 364 | #endif |
| 365 | |
| 366 | void healthd_board_mode_charger_set_backlight(bool on) |
| 367 | { |
| 368 | int fd; |
| 369 | char buffer[10]; |
| 370 | |
| 371 | memset(buffer, '\0', sizeof(buffer)); |
| 372 | fd = open(HEALTHD_BACKLIGHT_PATH, O_RDWR); |
| 373 | if (fd < 0) { |
| 374 | LOGE("Could not open backlight node : %s\n", strerror(errno)); |
| 375 | return; |
| 376 | } |
| 377 | LOGV("Enabling backlight\n"); |
| 378 | snprintf(buffer, sizeof(buffer), "%d\n", on ? HEALTHD_BACKLIGHT_LEVEL : 0); |
| 379 | if (write(fd, buffer, strlen(buffer)) < 0) { |
| 380 | LOGE("Could not write to backlight : %s\n", strerror(errno)); |
| 381 | } |
| 382 | close(fd); |
| 383 | |
| 384 | #ifdef HEALTHD_SECONDARY_BACKLIGHT_PATH |
| 385 | fd = open(HEALTHD_SECONDARY_BACKLIGHT_PATH, O_RDWR); |
| 386 | if (fd < 0) { |
| 387 | LOGE("Could not open second backlight node : %s\n", strerror(errno)); |
| 388 | return; |
| 389 | } |
| 390 | LOGV("Enabling secondary backlight\n"); |
| 391 | if (write(fd, buffer, strlen(buffer)) < 0) { |
| 392 | LOGE("Could not write to second backlight : %s\n", strerror(errno)); |
| 393 | return; |
| 394 | } |
| 395 | close(fd); |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | #else |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 400 | void healthd_board_mode_charger_set_backlight(bool) |
| 401 | { |
| 402 | } |
Adrian DC | 638e2f4 | 2016-08-14 11:44:31 +0200 | [diff] [blame] | 403 | #endif |
Steve Kondik | 0444c76 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 404 | |
| 405 | void healthd_board_mode_charger_init(void) |
| 406 | { |
| 407 | } |