healthd_mode_charger.cpp revision 9a11aaabf1ef8722c41d1057312be7a716badd66
1/* 2 * Copyright (C) 2011-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#include <dirent.h> 18#include <errno.h> 19#include <fcntl.h> 20#include <inttypes.h> 21#include <linux/input.h> 22#include <stdbool.h> 23#include <stdio.h> 24#include <stdlib.h> 25#include <string.h> 26#include <sys/epoll.h> 27#include <sys/stat.h> 28#include <sys/types.h> 29#include <sys/un.h> 30#include <time.h> 31#include <unistd.h> 32 33#include <sys/socket.h> 34#include <linux/netlink.h> 35 36#include <batteryservice/BatteryService.h> 37#include <cutils/android_reboot.h> 38#include <cutils/klog.h> 39#include <cutils/misc.h> 40#include <cutils/uevent.h> 41#include <cutils/properties.h> 42 43#ifdef CHARGER_ENABLE_SUSPEND 44#include <suspend/autosuspend.h> 45#endif 46 47#include "minui/minui.h" 48 49#include "healthd.h" 50 51char *locale; 52 53#ifndef max 54#define max(a,b) ((a) > (b) ? (a) : (b)) 55#endif 56 57#ifndef min 58#define min(a,b) ((a) < (b) ? (a) : (b)) 59#endif 60 61#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) 62 63#define MSEC_PER_SEC (1000LL) 64#define NSEC_PER_MSEC (1000000LL) 65 66#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC) 67#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) 68#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) 69 70#define BATTERY_FULL_THRESH 95 71 72#define LAST_KMSG_PATH "/proc/last_kmsg" 73#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops" 74#define LAST_KMSG_MAX_SZ (32 * 1024) 75 76#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) 77#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) 78#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) 79 80struct key_state { 81 bool pending; 82 bool down; 83 int64_t timestamp; 84}; 85 86struct frame { 87 int disp_time; 88 int min_capacity; 89 bool level_only; 90 91 gr_surface surface; 92}; 93 94struct animation { 95 bool run; 96 97 struct frame *frames; 98 int cur_frame; 99 int num_frames; 100 101 int cur_cycle; 102 int num_cycles; 103 104 /* current capacity being animated */ 105 int capacity; 106}; 107 108struct charger { 109 bool have_battery_state; 110 bool charger_connected; 111 int64_t next_screen_transition; 112 int64_t next_key_check; 113 int64_t next_pwr_check; 114 115 struct key_state keys[KEY_MAX + 1]; 116 117 struct animation *batt_anim; 118 gr_surface surf_unknown; 119 int boot_min_cap; 120}; 121 122static struct frame batt_anim_frames[] = { 123 { 124 .disp_time = 750, 125 .min_capacity = 0, 126 .level_only = false, 127 .surface = NULL, 128 }, 129 { 130 .disp_time = 750, 131 .min_capacity = 20, 132 .level_only = false, 133 .surface = NULL, 134 }, 135 { 136 .disp_time = 750, 137 .min_capacity = 40, 138 .level_only = false, 139 .surface = NULL, 140 }, 141 { 142 .disp_time = 750, 143 .min_capacity = 60, 144 .level_only = false, 145 .surface = NULL, 146 }, 147 { 148 .disp_time = 750, 149 .min_capacity = 80, 150 .level_only = true, 151 .surface = NULL, 152 }, 153 { 154 .disp_time = 750, 155 .min_capacity = BATTERY_FULL_THRESH, 156 .level_only = false, 157 .surface = NULL, 158 }, 159}; 160 161static struct animation battery_animation = { 162 .run = false, 163 .frames = batt_anim_frames, 164 .cur_frame = 0, 165 .num_frames = ARRAY_SIZE(batt_anim_frames), 166 .cur_cycle = 0, 167 .num_cycles = 3, 168 .capacity = 0, 169}; 170 171static struct charger charger_state; 172static struct healthd_config *healthd_config; 173static struct android::BatteryProperties *batt_prop; 174static int char_width; 175static int char_height; 176static bool minui_inited; 177 178/* current time in milliseconds */ 179static int64_t curr_time_ms(void) 180{ 181 struct timespec tm; 182 clock_gettime(CLOCK_MONOTONIC, &tm); 183 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC); 184} 185 186static void clear_screen(void) 187{ 188 gr_color(0, 0, 0, 255); 189 gr_clear(); 190} 191 192#define MAX_KLOG_WRITE_BUF_SZ 256 193 194static void dump_last_kmsg(void) 195{ 196 char *buf; 197 char *ptr; 198 unsigned sz = 0; 199 int len; 200 201 LOGW("\n"); 202 LOGW("*************** LAST KMSG ***************\n"); 203 LOGW("\n"); 204 buf = (char *)load_file(LAST_KMSG_PSTORE_PATH, &sz); 205 206 if (!buf || !sz) { 207 buf = (char *)load_file(LAST_KMSG_PATH, &sz); 208 if (!buf || !sz) { 209 LOGW("last_kmsg not found. Cold reset?\n"); 210 goto out; 211 } 212 } 213 214 len = min(sz, LAST_KMSG_MAX_SZ); 215 ptr = buf + (sz - len); 216 217 while (len > 0) { 218 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ); 219 char yoink; 220 char *nl; 221 222 nl = (char *)memrchr(ptr, '\n', cnt - 1); 223 if (nl) 224 cnt = nl - ptr + 1; 225 226 yoink = ptr[cnt]; 227 ptr[cnt] = '\0'; 228 klog_write(6, "<4>%s", ptr); 229 ptr[cnt] = yoink; 230 231 len -= cnt; 232 ptr += cnt; 233 } 234 235 free(buf); 236 237out: 238 LOGW("\n"); 239 LOGW("************* END LAST KMSG *************\n"); 240 LOGW("\n"); 241} 242 243#ifdef CHARGER_ENABLE_SUSPEND 244static int request_suspend(bool enable) 245{ 246 if (enable) 247 return autosuspend_enable(); 248 else 249 return autosuspend_disable(); 250} 251#else 252static int request_suspend(bool /*enable*/) 253{ 254 return 0; 255} 256#endif 257 258static int draw_text(const char *str, int x, int y) 259{ 260 int str_len_px = gr_measure(str); 261 262 if (x < 0) 263 x = (gr_fb_width() - str_len_px) / 2; 264 if (y < 0) 265 y = (gr_fb_height() - char_height) / 2; 266 gr_text(x, y, str, 0); 267 268 return y + char_height; 269} 270 271static void android_green(void) 272{ 273 gr_color(0xa4, 0xc6, 0x39, 255); 274} 275 276/* returns the last y-offset of where the surface ends */ 277static int draw_surface_centered(struct charger* /*charger*/, gr_surface surface) 278{ 279 int w; 280 int h; 281 int x; 282 int y; 283 284 w = gr_get_width(surface); 285 h = gr_get_height(surface); 286 x = (gr_fb_width() - w) / 2 ; 287 y = (gr_fb_height() - h) / 2 ; 288 289 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y); 290 gr_blit(surface, 0, 0, w, h, x, y); 291 return y + h; 292} 293 294static void draw_unknown(struct charger *charger) 295{ 296 int y; 297 if (charger->surf_unknown) { 298 draw_surface_centered(charger, charger->surf_unknown); 299 } else { 300 android_green(); 301 y = draw_text("Charging!", -1, -1); 302 draw_text("?\?/100", -1, y + 25); 303 } 304} 305 306static void draw_battery(struct charger *charger) 307{ 308 struct animation *batt_anim = charger->batt_anim; 309 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame]; 310 311 if (batt_anim->num_frames != 0) { 312 draw_surface_centered(charger, frame->surface); 313 LOGV("drawing frame #%d min_cap=%d time=%d\n", 314 batt_anim->cur_frame, frame->min_capacity, 315 frame->disp_time); 316 } 317} 318 319static void redraw_screen(struct charger *charger) 320{ 321 struct animation *batt_anim = charger->batt_anim; 322 323 clear_screen(); 324 325 /* try to display *something* */ 326 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0) 327 draw_unknown(charger); 328 else 329 draw_battery(charger); 330 gr_flip(); 331} 332 333static void kick_animation(struct animation *anim) 334{ 335 anim->run = true; 336} 337 338static void reset_animation(struct animation *anim) 339{ 340 anim->cur_cycle = 0; 341 anim->cur_frame = 0; 342 anim->run = false; 343} 344 345static void update_screen_state(struct charger *charger, int64_t now) 346{ 347 struct animation *batt_anim = charger->batt_anim; 348 int cur_frame; 349 int disp_time; 350 351 if (!batt_anim->run || now < charger->next_screen_transition) 352 return; 353 354 if (!minui_inited) { 355 356 if (healthd_config && healthd_config->screen_on) { 357 if (!healthd_config->screen_on(batt_prop)) { 358 LOGV("[%" PRId64 "] leave screen off\n", now); 359 batt_anim->run = false; 360 charger->next_screen_transition = -1; 361 if (charger->charger_connected) 362 request_suspend(true); 363 return; 364 } 365 } 366 367 gr_init(); 368 gr_font_size(&char_width, &char_height); 369 370#ifndef CHARGER_DISABLE_INIT_BLANK 371 gr_fb_blank(true); 372#endif 373 minui_inited = true; 374 } 375 376 /* animation is over, blank screen and leave */ 377 if (batt_anim->cur_cycle == batt_anim->num_cycles) { 378 reset_animation(batt_anim); 379 charger->next_screen_transition = -1; 380 gr_fb_blank(true); 381 LOGV("[%" PRId64 "] animation done\n", now); 382 if (charger->charger_connected) 383 request_suspend(true); 384 return; 385 } 386 387 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time; 388 389 /* animation starting, set up the animation */ 390 if (batt_anim->cur_frame == 0) { 391 int ret; 392 393 LOGV("[%" PRId64 "] animation starting\n", now); 394 if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) { 395 int i; 396 397 /* find first frame given current capacity */ 398 for (i = 1; i < batt_anim->num_frames; i++) { 399 if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity) 400 break; 401 } 402 batt_anim->cur_frame = i - 1; 403 404 /* show the first frame for twice as long */ 405 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2; 406 } 407 if (batt_prop) 408 batt_anim->capacity = batt_prop->batteryLevel; 409 } 410 411 /* unblank the screen on first cycle */ 412 if (batt_anim->cur_cycle == 0) 413 gr_fb_blank(false); 414 415 /* draw the new frame (@ cur_frame) */ 416 redraw_screen(charger); 417 418 /* if we don't have anim frames, we only have one image, so just bump 419 * the cycle counter and exit 420 */ 421 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) { 422 LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now); 423 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; 424 batt_anim->cur_cycle++; 425 return; 426 } 427 428 /* schedule next screen transition */ 429 charger->next_screen_transition = now + disp_time; 430 431 /* advance frame cntr to the next valid frame only if we are charging 432 * if necessary, advance cycle cntr, and reset frame cntr 433 */ 434 if (charger->charger_connected) { 435 batt_anim->cur_frame++; 436 437 /* if the frame is used for level-only, that is only show it when it's 438 * the current level, skip it during the animation. 439 */ 440 while (batt_anim->cur_frame < batt_anim->num_frames && 441 batt_anim->frames[batt_anim->cur_frame].level_only) 442 batt_anim->cur_frame++; 443 if (batt_anim->cur_frame >= batt_anim->num_frames) { 444 batt_anim->cur_cycle++; 445 batt_anim->cur_frame = 0; 446 447 /* don't reset the cycle counter, since we use that as a signal 448 * in a test above to check if animation is over 449 */ 450 } 451 } else { 452 /* Stop animating if we're not charging. 453 * If we stop it immediately instead of going through this loop, then 454 * the animation would stop somewhere in the middle. 455 */ 456 batt_anim->cur_frame = 0; 457 batt_anim->cur_cycle++; 458 } 459} 460 461static int set_key_callback(int code, int value, void *data) 462{ 463 struct charger *charger = (struct charger *)data; 464 int64_t now = curr_time_ms(); 465 int down = !!value; 466 467 if (code > KEY_MAX) 468 return -1; 469 470 /* ignore events that don't modify our state */ 471 if (charger->keys[code].down == down) 472 return 0; 473 474 /* only record the down even timestamp, as the amount 475 * of time the key spent not being pressed is not useful */ 476 if (down) 477 charger->keys[code].timestamp = now; 478 charger->keys[code].down = down; 479 charger->keys[code].pending = true; 480 if (down) { 481 LOGV("[%" PRId64 "] key[%d] down\n", now, code); 482 } else { 483 int64_t duration = now - charger->keys[code].timestamp; 484 int64_t secs = duration / 1000; 485 int64_t msecs = duration - secs * 1000; 486 LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n", 487 now, code, secs, msecs); 488 } 489 490 return 0; 491} 492 493static void update_input_state(struct charger *charger, 494 struct input_event *ev) 495{ 496 if (ev->type != EV_KEY) 497 return; 498 set_key_callback(ev->code, ev->value, charger); 499} 500 501static void set_next_key_check(struct charger *charger, 502 struct key_state *key, 503 int64_t timeout) 504{ 505 int64_t then = key->timestamp + timeout; 506 507 if (charger->next_key_check == -1 || then < charger->next_key_check) 508 charger->next_key_check = then; 509} 510 511static void process_key(struct charger *charger, int code, int64_t now) 512{ 513 struct key_state *key = &charger->keys[code]; 514 int64_t next_key_check; 515 516 if (code == KEY_POWER) { 517 if (key->down) { 518 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME; 519 if (now >= reboot_timeout) { 520 /* We do not currently support booting from charger mode on 521 all devices. Check the property and continue booting or reboot 522 accordingly. */ 523 if (property_get_bool("ro.enable_boot_charger_mode", false)) { 524 LOGW("[%" PRId64 "] booting from charger mode\n", now); 525 property_set("sys.boot_from_charger_mode", "1"); 526 } else { 527 if (charger->batt_anim->capacity >= charger->boot_min_cap) { 528 LOGW("[%" PRId64 "] rebooting\n", now); 529 android_reboot(ANDROID_RB_RESTART, 0, 0); 530 } else { 531 LOGV("[%lld] ignore power-button press, battery level " 532 "less than minimum\n", now); 533 } 534 } 535 } else { 536 /* if the key is pressed but timeout hasn't expired, 537 * make sure we wake up at the right-ish time to check 538 */ 539 set_next_key_check(charger, key, POWER_ON_KEY_TIME); 540 541 /* Turn on the display and kick animation on power-key press 542 * rather than on key release 543 */ 544 kick_animation(charger->batt_anim); 545 request_suspend(false); 546 } 547 } else { 548 /* if the power key got released, force screen state cycle */ 549 if (key->pending) { 550 kick_animation(charger->batt_anim); 551 } 552 } 553 } 554 555 key->pending = false; 556} 557 558static void handle_input_state(struct charger *charger, int64_t now) 559{ 560 process_key(charger, KEY_POWER, now); 561 562 if (charger->next_key_check != -1 && now > charger->next_key_check) 563 charger->next_key_check = -1; 564} 565 566static void handle_power_supply_state(struct charger *charger, int64_t now) 567{ 568 if (!charger->have_battery_state) 569 return; 570 571 if (!charger->charger_connected) { 572 573 /* Last cycle would have stopped at the extreme top of battery-icon 574 * Need to show the correct level corresponding to capacity. 575 */ 576 kick_animation(charger->batt_anim); 577 request_suspend(false); 578 if (charger->next_pwr_check == -1) { 579 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME; 580 LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n", 581 now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check); 582 } else if (now >= charger->next_pwr_check) { 583 LOGW("[%" PRId64 "] shutting down\n", now); 584 android_reboot(ANDROID_RB_POWEROFF, 0, 0); 585 } else { 586 /* otherwise we already have a shutdown timer scheduled */ 587 } 588 } else { 589 /* online supply present, reset shutdown timer if set */ 590 if (charger->next_pwr_check != -1) { 591 LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now); 592 kick_animation(charger->batt_anim); 593 } 594 charger->next_pwr_check = -1; 595 } 596} 597 598void healthd_mode_charger_heartbeat() 599{ 600 struct charger *charger = &charger_state; 601 int64_t now = curr_time_ms(); 602 int ret; 603 604 handle_input_state(charger, now); 605 handle_power_supply_state(charger, now); 606 607 /* do screen update last in case any of the above want to start 608 * screen transitions (animations, etc) 609 */ 610 update_screen_state(charger, now); 611} 612 613void healthd_mode_charger_battery_update( 614 struct android::BatteryProperties *props) 615{ 616 struct charger *charger = &charger_state; 617 618 charger->charger_connected = 619 props->chargerAcOnline || props->chargerUsbOnline || 620 props->chargerWirelessOnline; 621 622 if (!charger->have_battery_state) { 623 charger->have_battery_state = true; 624 charger->next_screen_transition = curr_time_ms() - 1; 625 reset_animation(charger->batt_anim); 626 kick_animation(charger->batt_anim); 627 } 628 batt_prop = props; 629} 630 631int healthd_mode_charger_preparetowait(void) 632{ 633 struct charger *charger = &charger_state; 634 int64_t now = curr_time_ms(); 635 int64_t next_event = INT64_MAX; 636 int64_t timeout; 637 struct input_event ev; 638 int ret; 639 640 LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now, 641 charger->next_screen_transition, charger->next_key_check, 642 charger->next_pwr_check); 643 644 if (charger->next_screen_transition != -1) 645 next_event = charger->next_screen_transition; 646 if (charger->next_key_check != -1 && charger->next_key_check < next_event) 647 next_event = charger->next_key_check; 648 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event) 649 next_event = charger->next_pwr_check; 650 651 if (next_event != -1 && next_event != INT64_MAX) 652 timeout = max(0, next_event - now); 653 else 654 timeout = -1; 655 656 return (int)timeout; 657} 658 659static int input_callback(int fd, unsigned int epevents, void *data) 660{ 661 struct charger *charger = (struct charger *)data; 662 struct input_event ev; 663 int ret; 664 665 ret = ev_get_input(fd, epevents, &ev); 666 if (ret) 667 return -1; 668 update_input_state(charger, &ev); 669 return 0; 670} 671 672static void charger_event_handler(uint32_t /*epevents*/) 673{ 674 int ret; 675 676 ret = ev_wait(-1); 677 if (!ret) 678 ev_dispatch(); 679} 680 681void healthd_mode_charger_init(struct healthd_config* config) 682{ 683 int ret; 684 struct charger *charger = &charger_state; 685 int i; 686 int epollfd; 687 688 dump_last_kmsg(); 689 690 LOGW("--------------- STARTING CHARGER MODE ---------------\n"); 691 692 ret = ev_init(input_callback, charger); 693 if (!ret) { 694 epollfd = ev_get_epollfd(); 695 healthd_register_event(epollfd, charger_event_handler); 696 } 697 698 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown); 699 if (ret < 0) { 700 LOGE("Cannot load battery_fail image\n"); 701 charger->surf_unknown = NULL; 702 } 703 704 charger->batt_anim = &battery_animation; 705 706 gr_surface* scale_frames; 707 int scale_count; 708 ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames); 709 if (ret < 0) { 710 LOGE("Cannot load battery_scale image\n"); 711 charger->batt_anim->num_frames = 0; 712 charger->batt_anim->num_cycles = 1; 713 } else if (scale_count != charger->batt_anim->num_frames) { 714 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n", 715 scale_count, charger->batt_anim->num_frames); 716 charger->batt_anim->num_frames = 0; 717 charger->batt_anim->num_cycles = 1; 718 } else { 719 for (i = 0; i < charger->batt_anim->num_frames; i++) { 720 charger->batt_anim->frames[i].surface = scale_frames[i]; 721 } 722 } 723 724 ev_sync_key_state(set_key_callback, charger); 725 726 charger->next_screen_transition = -1; 727 charger->next_key_check = -1; 728 charger->next_pwr_check = -1; 729 healthd_config = config; 730 charger->boot_min_cap = config->boot_min_cap; 731} 732