healthd_mode_charger.cpp revision fdce0878f1fac332adda2bbe8686f4e9cebeab39
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 <android-base/file.h> 34#include <android-base/stringprintf.h> 35 36#include <sys/socket.h> 37#include <linux/netlink.h> 38 39#include <batteryservice/BatteryService.h> 40#include <cutils/android_reboot.h> 41#include <cutils/klog.h> 42#include <cutils/misc.h> 43#include <cutils/uevent.h> 44#include <cutils/properties.h> 45 46#ifdef CHARGER_ENABLE_SUSPEND 47#include <suspend/autosuspend.h> 48#endif 49 50#include "animation.h" 51#include "AnimationParser.h" 52#include "minui/minui.h" 53 54#include <healthd/healthd.h> 55 56using namespace android; 57 58char *locale; 59 60#ifndef max 61#define max(a,b) ((a) > (b) ? (a) : (b)) 62#endif 63 64#ifndef min 65#define min(a,b) ((a) < (b) ? (a) : (b)) 66#endif 67 68#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) 69 70#define MSEC_PER_SEC (1000LL) 71#define NSEC_PER_MSEC (1000000LL) 72 73#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC) 74#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) 75#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) 76 77#define LAST_KMSG_PATH "/proc/last_kmsg" 78#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops" 79#define LAST_KMSG_MAX_SZ (32 * 1024) 80 81#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) 82#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) 83#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) 84 85static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt"; 86 87struct key_state { 88 bool pending; 89 bool down; 90 int64_t timestamp; 91}; 92 93struct charger { 94 bool have_battery_state; 95 bool charger_connected; 96 int64_t next_screen_transition; 97 int64_t next_key_check; 98 int64_t next_pwr_check; 99 100 struct key_state keys[KEY_MAX + 1]; 101 102 struct animation *batt_anim; 103 GRSurface* surf_unknown; 104 int boot_min_cap; 105}; 106 107static const struct animation BASE_ANIMATION = { 108 .text_clock = { 109 .pos_x = 0, 110 .pos_y = 0, 111 112 .color_r = 255, 113 .color_g = 255, 114 .color_b = 255, 115 .color_a = 255, 116 117 .font = nullptr, 118 }, 119 .text_percent = { 120 .pos_x = 0, 121 .pos_y = 0, 122 123 .color_r = 255, 124 .color_g = 255, 125 .color_b = 255, 126 .color_a = 255, 127 }, 128 129 .run = false, 130 131 .frames = nullptr, 132 .cur_frame = 0, 133 .num_frames = 0, 134 .first_frame_repeats = 2, 135 136 .cur_cycle = 0, 137 .num_cycles = 3, 138 139 .cur_level = 0, 140 .cur_status = BATTERY_STATUS_UNKNOWN, 141}; 142 143 144static struct animation::frame default_animation_frames[] = { 145 { 146 .disp_time = 750, 147 .min_level = 0, 148 .max_level = 19, 149 .surface = NULL, 150 }, 151 { 152 .disp_time = 750, 153 .min_level = 0, 154 .max_level = 39, 155 .surface = NULL, 156 }, 157 { 158 .disp_time = 750, 159 .min_level = 0, 160 .max_level = 59, 161 .surface = NULL, 162 }, 163 { 164 .disp_time = 750, 165 .min_level = 0, 166 .max_level = 79, 167 .surface = NULL, 168 }, 169 { 170 .disp_time = 750, 171 .min_level = 80, 172 .max_level = 95, 173 .surface = NULL, 174 }, 175 { 176 .disp_time = 750, 177 .min_level = 0, 178 .max_level = 100, 179 .surface = NULL, 180 }, 181}; 182 183static struct animation battery_animation = BASE_ANIMATION; 184 185static struct charger charger_state; 186static struct healthd_config *healthd_config; 187static struct android::BatteryProperties *batt_prop; 188static int char_width; 189static int char_height; 190static bool minui_inited; 191 192/* current time in milliseconds */ 193static int64_t curr_time_ms(void) 194{ 195 struct timespec tm; 196 clock_gettime(CLOCK_MONOTONIC, &tm); 197 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC); 198} 199 200static void clear_screen(void) 201{ 202 gr_color(0, 0, 0, 255); 203 gr_clear(); 204} 205 206#define MAX_KLOG_WRITE_BUF_SZ 256 207 208static void dump_last_kmsg(void) 209{ 210 char *buf; 211 char *ptr; 212 unsigned sz = 0; 213 int len; 214 215 LOGW("\n"); 216 LOGW("*************** LAST KMSG ***************\n"); 217 LOGW("\n"); 218 buf = (char *)load_file(LAST_KMSG_PSTORE_PATH, &sz); 219 220 if (!buf || !sz) { 221 buf = (char *)load_file(LAST_KMSG_PATH, &sz); 222 if (!buf || !sz) { 223 LOGW("last_kmsg not found. Cold reset?\n"); 224 goto out; 225 } 226 } 227 228 len = min(sz, LAST_KMSG_MAX_SZ); 229 ptr = buf + (sz - len); 230 231 while (len > 0) { 232 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ); 233 char yoink; 234 char *nl; 235 236 nl = (char *)memrchr(ptr, '\n', cnt - 1); 237 if (nl) 238 cnt = nl - ptr + 1; 239 240 yoink = ptr[cnt]; 241 ptr[cnt] = '\0'; 242 klog_write(6, "<4>%s", ptr); 243 ptr[cnt] = yoink; 244 245 len -= cnt; 246 ptr += cnt; 247 } 248 249 free(buf); 250 251out: 252 LOGW("\n"); 253 LOGW("************* END LAST KMSG *************\n"); 254 LOGW("\n"); 255} 256 257#ifdef CHARGER_ENABLE_SUSPEND 258static int request_suspend(bool enable) 259{ 260 if (enable) 261 return autosuspend_enable(); 262 else 263 return autosuspend_disable(); 264} 265#else 266static int request_suspend(bool /*enable*/) 267{ 268 return 0; 269} 270#endif 271 272static int draw_text(const char *str, int x, int y) 273{ 274 int str_len_px = gr_measure(gr_sys_font(), str); 275 276 if (x < 0) 277 x = (gr_fb_width() - str_len_px) / 2; 278 if (y < 0) 279 y = (gr_fb_height() - char_height) / 2; 280 gr_text(gr_sys_font(), x, y, str, 0); 281 282 return y + char_height; 283} 284 285static void android_green(void) 286{ 287 gr_color(0xa4, 0xc6, 0x39, 255); 288} 289 290// Negative x or y coordinates position the text away from the opposite edge that positive ones do. 291void determine_xy(const animation::text_field& field, const int length, int* x, int* y) 292{ 293 *x = field.pos_x; 294 *y = field.pos_y; 295 296 int str_len_px = length * field.font->char_width; 297 if (field.pos_x == CENTER_VAL) { 298 *x = (gr_fb_width() - str_len_px) / 2; 299 } else if (field.pos_x >= 0) { 300 *x = field.pos_x; 301 } else { // position from max edge 302 *x = gr_fb_width() + field.pos_x - str_len_px; 303 } 304 305 if (field.pos_y == CENTER_VAL) { 306 *y = (gr_fb_height() - field.font->char_height) / 2; 307 } else if (field.pos_y >= 0) { 308 *y = field.pos_y; 309 } else { // position from max edge 310 *y = gr_fb_height() + field.pos_y - field.font->char_height; 311 } 312} 313 314static void draw_clock(const animation& anim) 315{ 316 static constexpr char CLOCK_FORMAT[] = "%H:%M"; 317 static constexpr int CLOCK_LENGTH = 6; 318 319 const animation::text_field& field = anim.text_clock; 320 321 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) return; 322 323 time_t rawtime; 324 time(&rawtime); 325 struct tm* time_info = localtime(&rawtime); 326 327 char clock_str[CLOCK_LENGTH]; 328 size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info); 329 if (length != CLOCK_LENGTH - 1) { 330 LOGE("Could not format time\n"); 331 return; 332 } 333 334 int x, y; 335 determine_xy(field, length, &x, &y); 336 337 LOGV("drawing clock %s %d %d\n", clock_str, x, y); 338 gr_color(field.color_r, field.color_g, field.color_b, field.color_a); 339 gr_text(field.font, x, y, clock_str, false); 340} 341 342static void draw_percent(const animation& anim) 343{ 344 int cur_level = anim.cur_level; 345 if (anim.cur_status == BATTERY_STATUS_FULL) { 346 cur_level = 100; 347 } 348 349 if (cur_level <= 0) return; 350 351 const animation::text_field& field = anim.text_percent; 352 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) { 353 return; 354 } 355 356 std::string str = base::StringPrintf("%d%%", cur_level); 357 358 int x, y; 359 determine_xy(field, str.size(), &x, &y); 360 361 LOGV("drawing percent %s %d %d\n", str.c_str(), x, y); 362 gr_color(field.color_r, field.color_g, field.color_b, field.color_a); 363 gr_text(field.font, x, y, str.c_str(), false); 364} 365 366/* returns the last y-offset of where the surface ends */ 367static int draw_surface_centered(GRSurface* surface) 368{ 369 int w; 370 int h; 371 int x; 372 int y; 373 374 w = gr_get_width(surface); 375 h = gr_get_height(surface); 376 x = (gr_fb_width() - w) / 2 ; 377 y = (gr_fb_height() - h) / 2 ; 378 379 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y); 380 gr_blit(surface, 0, 0, w, h, x, y); 381 return y + h; 382} 383 384static void draw_unknown(struct charger *charger) 385{ 386 int y; 387 if (charger->surf_unknown) { 388 draw_surface_centered(charger->surf_unknown); 389 } else { 390 android_green(); 391 y = draw_text("Charging!", -1, -1); 392 draw_text("?\?/100", -1, y + 25); 393 } 394} 395 396static void draw_battery(const struct charger* charger) 397{ 398 const struct animation& anim = *charger->batt_anim; 399 const struct animation::frame& frame = anim.frames[anim.cur_frame]; 400 401 if (anim.num_frames != 0) { 402 draw_surface_centered(frame.surface); 403 LOGV("drawing frame #%d min_cap=%d time=%d\n", 404 anim.cur_frame, frame.min_level, 405 frame.disp_time); 406 } 407 draw_clock(anim); 408 draw_percent(anim); 409} 410 411static void redraw_screen(struct charger *charger) 412{ 413 struct animation *batt_anim = charger->batt_anim; 414 415 clear_screen(); 416 417 /* try to display *something* */ 418 if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0) 419 draw_unknown(charger); 420 else 421 draw_battery(charger); 422 gr_flip(); 423} 424 425static void kick_animation(struct animation *anim) 426{ 427 anim->run = true; 428} 429 430static void reset_animation(struct animation *anim) 431{ 432 anim->cur_cycle = 0; 433 anim->cur_frame = 0; 434 anim->run = false; 435} 436 437static void init_status_display(struct animation* anim) 438{ 439 int res; 440 441 if (!anim->text_clock.font_file.empty()) { 442 if ((res = 443 gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) { 444 LOGE("Could not load time font (%d)\n", res); 445 } 446 } 447 448 if (!anim->text_percent.font_file.empty()) { 449 if ((res = 450 gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) { 451 LOGE("Could not load percent font (%d)\n", res); 452 } 453 } 454} 455 456static void update_screen_state(struct charger *charger, int64_t now) 457{ 458 struct animation *batt_anim = charger->batt_anim; 459 int disp_time; 460 461 if (!batt_anim->run || now < charger->next_screen_transition) return; 462 463 if (!minui_inited) { 464 if (healthd_config && healthd_config->screen_on) { 465 if (!healthd_config->screen_on(batt_prop)) { 466 LOGV("[%" PRId64 "] leave screen off\n", now); 467 batt_anim->run = false; 468 charger->next_screen_transition = -1; 469 if (charger->charger_connected) 470 request_suspend(true); 471 return; 472 } 473 } 474 475 gr_init(); 476 gr_font_size(gr_sys_font(), &char_width, &char_height); 477 init_status_display(batt_anim); 478 479#ifndef CHARGER_DISABLE_INIT_BLANK 480 gr_fb_blank(true); 481#endif 482 minui_inited = true; 483 } 484 485 /* animation is over, blank screen and leave */ 486 if (batt_anim->num_cycles > 0 && batt_anim->cur_cycle == batt_anim->num_cycles) { 487 reset_animation(batt_anim); 488 charger->next_screen_transition = -1; 489 gr_fb_blank(true); 490 LOGV("[%" PRId64 "] animation done\n", now); 491 if (charger->charger_connected) 492 request_suspend(true); 493 return; 494 } 495 496 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time; 497 498 /* animation starting, set up the animation */ 499 if (batt_anim->cur_frame == 0) { 500 501 LOGV("[%" PRId64 "] animation starting\n", now); 502 if (batt_prop) { 503 batt_anim->cur_level = batt_prop->batteryLevel; 504 batt_anim->cur_status = batt_prop->batteryStatus; 505 if (batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) { 506 /* find first frame given current battery level */ 507 for (int i = 0; i < batt_anim->num_frames; i++) { 508 if (batt_anim->cur_level >= batt_anim->frames[i].min_level && 509 batt_anim->cur_level <= batt_anim->frames[i].max_level) { 510 batt_anim->cur_frame = i; 511 break; 512 } 513 } 514 515 // repeat the first frame first_frame_repeats times 516 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 517 batt_anim->first_frame_repeats; 518 } 519 } 520 } 521 522 /* unblank the screen on first cycle */ 523 if (batt_anim->cur_cycle == 0) 524 gr_fb_blank(false); 525 526 /* draw the new frame (@ cur_frame) */ 527 redraw_screen(charger); 528 529 /* if we don't have anim frames, we only have one image, so just bump 530 * the cycle counter and exit 531 */ 532 if (batt_anim->num_frames == 0 || batt_anim->cur_level < 0) { 533 LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now); 534 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; 535 batt_anim->cur_cycle++; 536 return; 537 } 538 539 /* schedule next screen transition */ 540 charger->next_screen_transition = now + disp_time; 541 542 /* advance frame cntr to the next valid frame only if we are charging 543 * if necessary, advance cycle cntr, and reset frame cntr 544 */ 545 if (charger->charger_connected) { 546 batt_anim->cur_frame++; 547 548 while (batt_anim->cur_frame < batt_anim->num_frames && 549 (batt_anim->cur_level < batt_anim->frames[batt_anim->cur_frame].min_level || 550 batt_anim->cur_level > batt_anim->frames[batt_anim->cur_frame].max_level)) { 551 batt_anim->cur_frame++; 552 } 553 if (batt_anim->cur_frame >= batt_anim->num_frames) { 554 batt_anim->cur_cycle++; 555 batt_anim->cur_frame = 0; 556 557 /* don't reset the cycle counter, since we use that as a signal 558 * in a test above to check if animation is over 559 */ 560 } 561 } else { 562 /* Stop animating if we're not charging. 563 * If we stop it immediately instead of going through this loop, then 564 * the animation would stop somewhere in the middle. 565 */ 566 batt_anim->cur_frame = 0; 567 batt_anim->cur_cycle++; 568 } 569} 570 571static int set_key_callback(int code, int value, void *data) 572{ 573 struct charger *charger = (struct charger *)data; 574 int64_t now = curr_time_ms(); 575 int down = !!value; 576 577 if (code > KEY_MAX) 578 return -1; 579 580 /* ignore events that don't modify our state */ 581 if (charger->keys[code].down == down) 582 return 0; 583 584 /* only record the down even timestamp, as the amount 585 * of time the key spent not being pressed is not useful */ 586 if (down) 587 charger->keys[code].timestamp = now; 588 charger->keys[code].down = down; 589 charger->keys[code].pending = true; 590 if (down) { 591 LOGV("[%" PRId64 "] key[%d] down\n", now, code); 592 } else { 593 int64_t duration = now - charger->keys[code].timestamp; 594 int64_t secs = duration / 1000; 595 int64_t msecs = duration - secs * 1000; 596 LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n", 597 now, code, secs, msecs); 598 } 599 600 return 0; 601} 602 603static void update_input_state(struct charger *charger, 604 struct input_event *ev) 605{ 606 if (ev->type != EV_KEY) 607 return; 608 set_key_callback(ev->code, ev->value, charger); 609} 610 611static void set_next_key_check(struct charger *charger, 612 struct key_state *key, 613 int64_t timeout) 614{ 615 int64_t then = key->timestamp + timeout; 616 617 if (charger->next_key_check == -1 || then < charger->next_key_check) 618 charger->next_key_check = then; 619} 620 621static void process_key(struct charger *charger, int code, int64_t now) 622{ 623 struct key_state *key = &charger->keys[code]; 624 625 if (code == KEY_POWER) { 626 if (key->down) { 627 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME; 628 if (now >= reboot_timeout) { 629 /* We do not currently support booting from charger mode on 630 all devices. Check the property and continue booting or reboot 631 accordingly. */ 632 if (property_get_bool("ro.enable_boot_charger_mode", false)) { 633 LOGW("[%" PRId64 "] booting from charger mode\n", now); 634 property_set("sys.boot_from_charger_mode", "1"); 635 } else { 636 if (charger->batt_anim->cur_level >= charger->boot_min_cap) { 637 LOGW("[%" PRId64 "] rebooting\n", now); 638 android_reboot(ANDROID_RB_RESTART, 0, 0); 639 } else { 640 LOGV("[%" PRId64 "] ignore power-button press, battery level " 641 "less than minimum\n", now); 642 } 643 } 644 } else { 645 /* if the key is pressed but timeout hasn't expired, 646 * make sure we wake up at the right-ish time to check 647 */ 648 set_next_key_check(charger, key, POWER_ON_KEY_TIME); 649 650 /* Turn on the display and kick animation on power-key press 651 * rather than on key release 652 */ 653 kick_animation(charger->batt_anim); 654 request_suspend(false); 655 } 656 } else { 657 /* if the power key got released, force screen state cycle */ 658 if (key->pending) { 659 kick_animation(charger->batt_anim); 660 } 661 } 662 } 663 664 key->pending = false; 665} 666 667static void handle_input_state(struct charger *charger, int64_t now) 668{ 669 process_key(charger, KEY_POWER, now); 670 671 if (charger->next_key_check != -1 && now > charger->next_key_check) 672 charger->next_key_check = -1; 673} 674 675static void handle_power_supply_state(struct charger *charger, int64_t now) 676{ 677 if (!charger->have_battery_state) 678 return; 679 680 if (!charger->charger_connected) { 681 682 /* Last cycle would have stopped at the extreme top of battery-icon 683 * Need to show the correct level corresponding to capacity. 684 */ 685 kick_animation(charger->batt_anim); 686 request_suspend(false); 687 if (charger->next_pwr_check == -1) { 688 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME; 689 LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n", 690 now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check); 691 } else if (now >= charger->next_pwr_check) { 692 LOGW("[%" PRId64 "] shutting down\n", now); 693 android_reboot(ANDROID_RB_POWEROFF, 0, 0); 694 } else { 695 /* otherwise we already have a shutdown timer scheduled */ 696 } 697 } else { 698 /* online supply present, reset shutdown timer if set */ 699 if (charger->next_pwr_check != -1) { 700 LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now); 701 kick_animation(charger->batt_anim); 702 } 703 charger->next_pwr_check = -1; 704 } 705} 706 707void healthd_mode_charger_heartbeat() 708{ 709 struct charger *charger = &charger_state; 710 int64_t now = curr_time_ms(); 711 712 handle_input_state(charger, now); 713 handle_power_supply_state(charger, now); 714 715 /* do screen update last in case any of the above want to start 716 * screen transitions (animations, etc) 717 */ 718 update_screen_state(charger, now); 719} 720 721void healthd_mode_charger_battery_update( 722 struct android::BatteryProperties *props) 723{ 724 struct charger *charger = &charger_state; 725 726 charger->charger_connected = 727 props->chargerAcOnline || props->chargerUsbOnline || 728 props->chargerWirelessOnline; 729 730 if (!charger->have_battery_state) { 731 charger->have_battery_state = true; 732 charger->next_screen_transition = curr_time_ms() - 1; 733 reset_animation(charger->batt_anim); 734 kick_animation(charger->batt_anim); 735 } 736 batt_prop = props; 737} 738 739int healthd_mode_charger_preparetowait(void) 740{ 741 struct charger *charger = &charger_state; 742 int64_t now = curr_time_ms(); 743 int64_t next_event = INT64_MAX; 744 int64_t timeout; 745 746 LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now, 747 charger->next_screen_transition, charger->next_key_check, 748 charger->next_pwr_check); 749 750 if (charger->next_screen_transition != -1) 751 next_event = charger->next_screen_transition; 752 if (charger->next_key_check != -1 && charger->next_key_check < next_event) 753 next_event = charger->next_key_check; 754 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event) 755 next_event = charger->next_pwr_check; 756 757 if (next_event != -1 && next_event != INT64_MAX) 758 timeout = max(0, next_event - now); 759 else 760 timeout = -1; 761 762 return (int)timeout; 763} 764 765static int input_callback(int fd, unsigned int epevents, void *data) 766{ 767 struct charger *charger = (struct charger *)data; 768 struct input_event ev; 769 int ret; 770 771 ret = ev_get_input(fd, epevents, &ev); 772 if (ret) 773 return -1; 774 update_input_state(charger, &ev); 775 return 0; 776} 777 778static void charger_event_handler(uint32_t /*epevents*/) 779{ 780 int ret; 781 782 ret = ev_wait(-1); 783 if (!ret) 784 ev_dispatch(); 785} 786 787animation* init_animation() 788{ 789 bool parse_success; 790 791 std::string content; 792 if (base::ReadFileToString(animation_desc_path, &content)) { 793 parse_success = parse_animation_desc(content, &battery_animation); 794 } else { 795 LOGW("Could not open animation description at %s\n", animation_desc_path); 796 parse_success = false; 797 } 798 799 if (!parse_success) { 800 LOGW("Could not parse animation description. Using default animation.\n"); 801 battery_animation = BASE_ANIMATION; 802 battery_animation.animation_file.assign("charger/battery_scale"); 803 battery_animation.frames = default_animation_frames; 804 battery_animation.num_frames = ARRAY_SIZE(default_animation_frames); 805 } 806 if (battery_animation.fail_file.empty()) { 807 battery_animation.fail_file.assign("charger/battery_fail"); 808 } 809 810 LOGV("Animation Description:\n"); 811 LOGV(" animation: %d %d '%s' (%d)\n", 812 battery_animation.num_cycles, battery_animation.first_frame_repeats, 813 battery_animation.animation_file.c_str(), battery_animation.num_frames); 814 LOGV(" fail_file: '%s'\n", battery_animation.fail_file.c_str()); 815 LOGV(" clock: %d %d %d %d %d %d '%s'\n", 816 battery_animation.text_clock.pos_x, battery_animation.text_clock.pos_y, 817 battery_animation.text_clock.color_r, battery_animation.text_clock.color_g, 818 battery_animation.text_clock.color_b, battery_animation.text_clock.color_a, 819 battery_animation.text_clock.font_file.c_str()); 820 LOGV(" percent: %d %d %d %d %d %d '%s'\n", 821 battery_animation.text_percent.pos_x, battery_animation.text_percent.pos_y, 822 battery_animation.text_percent.color_r, battery_animation.text_percent.color_g, 823 battery_animation.text_percent.color_b, battery_animation.text_percent.color_a, 824 battery_animation.text_percent.font_file.c_str()); 825 for (int i = 0; i < battery_animation.num_frames; i++) { 826 LOGV(" frame %.2d: %d %d %d\n", i, battery_animation.frames[i].disp_time, 827 battery_animation.frames[i].min_level, battery_animation.frames[i].max_level); 828 } 829 830 return &battery_animation; 831} 832 833void healthd_mode_charger_init(struct healthd_config* config) 834{ 835 int ret; 836 struct charger *charger = &charger_state; 837 int i; 838 int epollfd; 839 840 dump_last_kmsg(); 841 842 LOGW("--------------- STARTING CHARGER MODE ---------------\n"); 843 844 ret = ev_init(input_callback, charger); 845 if (!ret) { 846 epollfd = ev_get_epollfd(); 847 healthd_register_event(epollfd, charger_event_handler, EVENT_WAKEUP_FD); 848 } 849 850 struct animation* anim = init_animation(); 851 charger->batt_anim = anim; 852 853 ret = res_create_display_surface(anim->fail_file.c_str(), &charger->surf_unknown); 854 if (ret < 0) { 855 LOGE("Cannot load custom battery_fail image. Reverting to built in.\n"); 856 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown); 857 if (ret < 0) { 858 LOGE("Cannot load built in battery_fail image\n"); 859 charger->surf_unknown = NULL; 860 } 861 } 862 863 GRSurface** scale_frames; 864 int scale_count; 865 int scale_fps; // Not in use (charger/battery_scale doesn't have FPS text 866 // chunk). We are using hard-coded frame.disp_time instead. 867 ret = res_create_multi_display_surface(anim->animation_file.c_str(), 868 &scale_count, &scale_fps, &scale_frames); 869 if (ret < 0) { 870 LOGE("Cannot load battery_scale image\n"); 871 anim->num_frames = 0; 872 anim->num_cycles = 1; 873 } else if (scale_count != anim->num_frames) { 874 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n", 875 scale_count, anim->num_frames); 876 anim->num_frames = 0; 877 anim->num_cycles = 1; 878 } else { 879 for (i = 0; i < anim->num_frames; i++) { 880 anim->frames[i].surface = scale_frames[i]; 881 } 882 } 883 ev_sync_key_state(set_key_callback, charger); 884 885 charger->next_screen_transition = -1; 886 charger->next_key_check = -1; 887 charger->next_pwr_check = -1; 888 healthd_config = config; 889 charger->boot_min_cap = config->boot_min_cap; 890} 891