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