Steve Kondik | 7595620 | 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> |
| 35 | #include <linux/android_alarm.h> |
| 36 | #include <sys/timerfd.h> |
| 37 | #include <linux/rtc.h> |
| 38 | |
| 39 | #include "healthd.h" |
| 40 | #include "minui/minui.h" |
| 41 | |
| 42 | #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) |
| 43 | #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) |
| 44 | #define LOGI(x...) do { KLOG_INFO("charger", x); } while (0) |
| 45 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) |
| 46 | |
| 47 | struct frame { |
| 48 | int min_capacity; |
| 49 | GRSurface *surface; |
| 50 | }; |
| 51 | |
| 52 | struct animation { |
| 53 | struct frame *frames; |
| 54 | int cur_frame; |
| 55 | int num_frames; |
| 56 | }; |
| 57 | |
| 58 | static struct animation anim = { |
| 59 | .frames = NULL, |
| 60 | .cur_frame = 0, |
| 61 | .num_frames = 0, |
| 62 | }; |
| 63 | |
| 64 | static bool font_inited; |
| 65 | |
| 66 | static int draw_surface_centered(GRSurface* surface) |
| 67 | { |
| 68 | int w, h, x, y; |
| 69 | |
| 70 | w = gr_get_width(surface); |
| 71 | h = gr_get_height(surface); |
| 72 | x = (gr_fb_width() - w) / 2 ; |
| 73 | y = (gr_fb_height() - h) / 2 ; |
| 74 | |
| 75 | gr_blit(surface, 0, 0, w, h, x, y); |
| 76 | return y + h; |
| 77 | } |
| 78 | |
| 79 | #define STR_LEN 64 |
| 80 | static void draw_capacity(int capacity) |
| 81 | { |
| 82 | struct frame *f = &anim.frames[0]; |
| 83 | int w = gr_get_width(f->surface); |
| 84 | int h = gr_get_height(f->surface); |
| 85 | int x = (gr_fb_width() - w) / 2 ; |
| 86 | int y = (gr_fb_height() + h) / 2; |
| 87 | |
| 88 | char cap_str[STR_LEN]; |
| 89 | snprintf(cap_str, (STR_LEN - 1), "%d%%", capacity); |
| 90 | gr_color(255, 255, 255, 255); |
| 91 | gr_text(x, y * 1.05, cap_str, 0); |
| 92 | } |
| 93 | |
| 94 | #ifdef QCOM_HARDWARE |
| 95 | enum alarm_time_type { |
| 96 | ALARM_TIME, |
| 97 | RTC_TIME, |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * shouldn't be changed after |
| 102 | * reading from alarm register |
| 103 | */ |
| 104 | static time_t alm_secs; |
| 105 | |
| 106 | static int alarm_get_time(enum alarm_time_type time_type, |
| 107 | time_t *secs) |
| 108 | { |
| 109 | struct tm tm; |
| 110 | unsigned int cmd; |
| 111 | int rc, fd = -1; |
| 112 | |
| 113 | if (!secs) |
| 114 | return -1; |
| 115 | |
| 116 | fd = open("/dev/rtc0", O_RDONLY); |
| 117 | if (fd < 0) { |
| 118 | LOGE("Can't open rtc devfs node\n"); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | switch (time_type) { |
| 123 | case ALARM_TIME: |
| 124 | cmd = RTC_ALM_READ; |
| 125 | break; |
| 126 | case RTC_TIME: |
| 127 | cmd = RTC_RD_TIME; |
| 128 | break; |
| 129 | default: |
| 130 | LOGE("Invalid time type\n"); |
| 131 | goto err; |
| 132 | } |
| 133 | |
| 134 | rc = ioctl(fd, cmd, &tm); |
| 135 | if (rc < 0) { |
| 136 | LOGE("Unable to get time\n"); |
| 137 | goto err; |
| 138 | } |
| 139 | |
| 140 | *secs = mktime(&tm) + tm.tm_gmtoff; |
| 141 | if (*secs < 0) { |
| 142 | LOGE("Invalid seconds = %ld\n", *secs); |
| 143 | goto err; |
| 144 | } |
| 145 | |
| 146 | close(fd); |
| 147 | return 0; |
| 148 | |
| 149 | err: |
| 150 | close(fd); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | #define ERR_SECS 2 |
| 155 | static int alarm_is_alm_expired() |
| 156 | { |
| 157 | int rc; |
| 158 | time_t rtc_secs; |
| 159 | |
| 160 | rc = alarm_get_time(RTC_TIME, &rtc_secs); |
| 161 | if (rc < 0) |
| 162 | return 0; |
| 163 | |
| 164 | return (alm_secs >= rtc_secs - ERR_SECS && |
| 165 | alm_secs <= rtc_secs + ERR_SECS) ? 1 : 0; |
| 166 | } |
| 167 | |
| 168 | static int timerfd_set_reboot_time_and_wait(time_t secs) |
| 169 | { |
| 170 | int fd; |
| 171 | int ret = -1; |
| 172 | fd = timerfd_create(CLOCK_REALTIME_ALARM, 0); |
| 173 | if (fd < 0) { |
| 174 | LOGE("Can't open timerfd alarm node\n"); |
| 175 | goto err_return; |
| 176 | } |
| 177 | |
| 178 | struct itimerspec spec; |
| 179 | memset(&spec, 0, sizeof(spec)); |
| 180 | spec.it_value.tv_sec = secs; |
| 181 | |
| 182 | if (timerfd_settime(fd, 0 /* relative */, &spec, NULL)) { |
| 183 | LOGE("Can't set timerfd alarm\n"); |
| 184 | goto err_close; |
| 185 | } |
| 186 | |
| 187 | uint64_t unused; |
| 188 | if (read(fd, &unused, sizeof(unused)) < 0) { |
| 189 | LOGE("Wait alarm error\n"); |
| 190 | goto err_close; |
| 191 | } |
| 192 | |
| 193 | ret = 0; |
| 194 | err_close: |
| 195 | close(fd); |
| 196 | err_return: |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static int alarm_set_reboot_time_and_wait(time_t secs) |
| 201 | { |
| 202 | int rc, fd; |
| 203 | struct timespec ts; |
| 204 | |
| 205 | fd = open("/dev/alarm", O_RDWR); |
| 206 | if (fd < 0) { |
| 207 | LOGE("Can't open alarm devfs node, trying timerfd\n"); |
| 208 | return timerfd_set_reboot_time_and_wait(secs); |
| 209 | } |
| 210 | |
| 211 | /* get the elapsed realtime from boot time to now */ |
| 212 | rc = ioctl(fd, ANDROID_ALARM_GET_TIME( |
| 213 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts); |
| 214 | if (rc < 0) { |
| 215 | LOGE("Unable to get elapsed realtime\n"); |
| 216 | goto err; |
| 217 | } |
| 218 | |
| 219 | /* calculate the elapsed time from boot time to reboot time */ |
| 220 | ts.tv_sec += secs; |
| 221 | ts.tv_nsec = 0; |
| 222 | |
| 223 | rc = ioctl(fd, ANDROID_ALARM_SET( |
| 224 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts); |
| 225 | if (rc < 0) { |
| 226 | LOGE("Unable to set reboot time to %ld\n", secs); |
| 227 | goto err; |
| 228 | } |
| 229 | |
| 230 | do { |
| 231 | rc = ioctl(fd, ANDROID_ALARM_WAIT); |
| 232 | } while ((rc < 0 && errno == EINTR) || !alarm_is_alm_expired()); |
| 233 | |
| 234 | if (rc <= 0) { |
| 235 | LOGE("Unable to wait on alarm\n"); |
| 236 | goto err; |
| 237 | } |
| 238 | |
| 239 | close(fd); |
| 240 | return 0; |
| 241 | |
| 242 | err: |
| 243 | if (fd >= 0) |
| 244 | close(fd); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | static void *alarm_thread(void *) |
| 249 | { |
| 250 | time_t rtc_secs, rb_secs; |
| 251 | int rc; |
| 252 | |
| 253 | /* |
| 254 | * to support power off alarm, the time |
| 255 | * stored in alarm register at latest |
| 256 | * shutdown time should be some time |
| 257 | * earlier than the actual alarm time |
| 258 | * set by user |
| 259 | */ |
| 260 | rc = alarm_get_time(ALARM_TIME, &alm_secs); |
Adrian DC | e91605c | 2016-08-14 11:40:05 +0200 | [diff] [blame^] | 261 | LOGI("RTC Alarm %ld\n", alm_secs); |
Steve Kondik | 7595620 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 262 | if (rc < 0 || !alm_secs) |
| 263 | goto err; |
| 264 | |
| 265 | rc = alarm_get_time(RTC_TIME, &rtc_secs); |
Adrian DC | e91605c | 2016-08-14 11:40:05 +0200 | [diff] [blame^] | 266 | LOGI("RTC Clock %ld\n", rtc_secs); |
Steve Kondik | 7595620 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 267 | if (rc < 0) |
| 268 | goto err; |
| 269 | |
| 270 | /* |
| 271 | * calculate the reboot time after which |
| 272 | * the phone will reboot |
| 273 | */ |
| 274 | rb_secs = alm_secs - rtc_secs; |
| 275 | if (rb_secs <= 0) |
| 276 | goto err; |
| 277 | |
| 278 | rc = alarm_set_reboot_time_and_wait(rb_secs); |
| 279 | if (rc < 0) |
| 280 | goto err; |
| 281 | |
| 282 | LOGI("Exit from power off charging, reboot the phone!\n"); |
| 283 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 284 | |
| 285 | err: |
| 286 | LOGE("Exit from alarm thread\n"); |
| 287 | return NULL; |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | void healthd_board_init(struct healthd_config*) |
| 292 | { |
| 293 | pthread_t tid; |
| 294 | char value[PROP_VALUE_MAX]; |
| 295 | int rc = 0, scale_count = 0, i; |
| 296 | GRSurface **scale_frames; |
| 297 | |
| 298 | rc = res_create_multi_display_surface("charger/cm_battery_scale", |
| 299 | &scale_count, &scale_frames); |
| 300 | if (rc < 0) { |
| 301 | LOGE("%s: Unable to load battery scale image", __func__); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | anim.frames = new frame[scale_count]; |
| 306 | anim.num_frames = scale_count; |
| 307 | for (i = 0; i < anim.num_frames; i++) { |
| 308 | anim.frames[i].surface = scale_frames[i]; |
| 309 | anim.frames[i].min_capacity = 100/(scale_count-1) * i; |
| 310 | } |
| 311 | |
| 312 | #ifdef QCOM_HARDWARE |
| 313 | property_get("ro.bootmode", value, ""); |
| 314 | if (!strcmp("charger", value)) { |
| 315 | rc = pthread_create(&tid, NULL, alarm_thread, NULL); |
| 316 | if (rc < 0) |
| 317 | LOGE("Create alarm thread failed\n"); |
| 318 | } |
| 319 | #endif |
| 320 | } |
| 321 | |
| 322 | int healthd_board_battery_update(struct android::BatteryProperties*) |
| 323 | { |
| 324 | // return 0 to log periodic polled battery status to kernel log |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | void healthd_board_mode_charger_draw_battery( |
| 329 | struct android::BatteryProperties *batt_prop) |
| 330 | { |
| 331 | int start_frame = 0; |
| 332 | int capacity = -1; |
| 333 | |
| 334 | if (!font_inited) { |
| 335 | gr_set_font("log"); |
| 336 | font_inited = true; |
| 337 | } |
| 338 | |
| 339 | if (batt_prop && batt_prop->batteryLevel >= 0) { |
| 340 | capacity = batt_prop->batteryLevel; |
| 341 | } |
| 342 | |
| 343 | if (anim.num_frames == 0 || capacity < 0) { |
| 344 | LOGE("%s: Unable to draw battery", __func__); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | // Find starting frame to display based on current capacity |
| 349 | for (start_frame = 1; start_frame < anim.num_frames; start_frame++) { |
| 350 | if (capacity < anim.frames[start_frame].min_capacity) |
| 351 | break; |
| 352 | } |
| 353 | // Always start from the level just below the current capacity |
| 354 | start_frame--; |
| 355 | |
| 356 | if (anim.cur_frame < start_frame) |
| 357 | anim.cur_frame = start_frame; |
| 358 | |
| 359 | draw_surface_centered(anim.frames[anim.cur_frame].surface); |
| 360 | draw_capacity(capacity); |
| 361 | // Move to next frame, with max possible frame at max_idx |
| 362 | anim.cur_frame = ((anim.cur_frame + 1) % anim.num_frames); |
| 363 | } |
| 364 | |
| 365 | void healthd_board_mode_charger_battery_update( |
| 366 | struct android::BatteryProperties*) |
| 367 | { |
| 368 | } |
| 369 | |
Adrian DC | cadd0be | 2016-08-14 11:44:31 +0200 | [diff] [blame] | 370 | #ifdef HEALTHD_BACKLIGHT_PATH |
| 371 | #ifndef HEALTHD_BACKLIGHT_LEVEL |
| 372 | #define HEALTHD_BACKLIGHT_LEVEL 100 |
| 373 | #endif |
| 374 | |
| 375 | void healthd_board_mode_charger_set_backlight(bool on) |
| 376 | { |
| 377 | int fd; |
| 378 | char buffer[10]; |
| 379 | |
| 380 | memset(buffer, '\0', sizeof(buffer)); |
| 381 | fd = open(HEALTHD_BACKLIGHT_PATH, O_RDWR); |
| 382 | if (fd < 0) { |
| 383 | LOGE("Could not open backlight node : %s\n", strerror(errno)); |
| 384 | return; |
| 385 | } |
| 386 | LOGV("Enabling backlight\n"); |
| 387 | snprintf(buffer, sizeof(buffer), "%d\n", on ? HEALTHD_BACKLIGHT_LEVEL : 0); |
| 388 | if (write(fd, buffer, strlen(buffer)) < 0) { |
| 389 | LOGE("Could not write to backlight : %s\n", strerror(errno)); |
| 390 | } |
| 391 | close(fd); |
| 392 | |
| 393 | #ifdef HEALTHD_SECONDARY_BACKLIGHT_PATH |
| 394 | fd = open(HEALTHD_SECONDARY_BACKLIGHT_PATH, O_RDWR); |
| 395 | if (fd < 0) { |
| 396 | LOGE("Could not open second backlight node : %s\n", strerror(errno)); |
| 397 | return; |
| 398 | } |
| 399 | LOGV("Enabling secondary backlight\n"); |
| 400 | if (write(fd, buffer, strlen(buffer)) < 0) { |
| 401 | LOGE("Could not write to second backlight : %s\n", strerror(errno)); |
| 402 | return; |
| 403 | } |
| 404 | close(fd); |
| 405 | #endif |
| 406 | } |
| 407 | |
| 408 | #else |
Steve Kondik | 7595620 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 409 | void healthd_board_mode_charger_set_backlight(bool) |
| 410 | { |
| 411 | } |
Adrian DC | cadd0be | 2016-08-14 11:44:31 +0200 | [diff] [blame] | 412 | #endif |
Steve Kondik | 7595620 | 2016-08-07 23:21:29 -0700 | [diff] [blame] | 413 | |
| 414 | void healthd_board_mode_charger_init(void) |
| 415 | { |
| 416 | } |