charger.c revision e0c70dad4b4d417182e4c0ba8adae199f2622582
1/* 2 * Copyright (C) 2011 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//#define DEBUG_UEVENTS 18#define CHARGER_KLOG_LEVEL 6 19 20#include <dirent.h> 21#include <errno.h> 22#include <fcntl.h> 23#include <linux/input.h> 24#include <linux/netlink.h> 25#include <stdbool.h> 26#include <stdio.h> 27#include <stdlib.h> 28#include <string.h> 29#include <sys/poll.h> 30#include <sys/socket.h> 31#include <sys/stat.h> 32#include <sys/types.h> 33#include <sys/un.h> 34#include <time.h> 35#include <unistd.h> 36 37#include <cutils/android_reboot.h> 38#include <cutils/klog.h> 39#include <cutils/list.h> 40#include <cutils/uevent.h> 41 42#include "minui/minui.h" 43 44#ifndef max 45#define max(a,b) ((a) > (b) ? (a) : (b)) 46#endif 47 48#ifndef min 49#define min(a,b) ((a) < (b) ? (a) : (b)) 50#endif 51 52#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) 53 54#define MSEC_PER_SEC (1000LL) 55#define NSEC_PER_MSEC (1000000LL) 56 57#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC) 58#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) 59#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) 60 61#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) 62#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0) 63#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) 64 65struct key_state { 66 bool pending; 67 bool down; 68 int64_t timestamp; 69}; 70 71struct power_supply { 72 struct listnode list; 73 char name[256]; 74 char type[32]; 75 bool online; 76 bool valid; 77 char cap_path[PATH_MAX]; 78}; 79 80struct frame { 81 const char *name; 82 int disp_time; 83 int min_capacity; 84 85 gr_surface surface; 86}; 87 88struct animation { 89 bool run; 90 91 struct frame *frames; 92 int cur_frame; 93 int num_frames; 94 95 int cur_cycle; 96 int num_cycles; 97 98 /* current capacity being animated */ 99 int capacity; 100}; 101 102struct charger { 103 int64_t next_screen_transition; 104 int64_t next_key_check; 105 int64_t next_pwr_check; 106 107 struct key_state keys[KEY_MAX + 1]; 108 int uevent_fd; 109 110 struct listnode supplies; 111 int num_supplies; 112 int num_supplies_online; 113 114 struct animation *batt_anim; 115 gr_surface surf_unknown; 116 117 struct power_supply *battery; 118}; 119 120struct uevent { 121 const char *action; 122 const char *path; 123 const char *subsystem; 124 const char *ps_name; 125 const char *ps_type; 126 const char *ps_online; 127}; 128 129static struct frame batt_anim_frames[] = { 130 { 131 .name = "charger/battery_0", 132 .disp_time = 750, 133 .min_capacity = 0, 134 }, 135 { 136 .name = "charger/battery_1", 137 .disp_time = 750, 138 .min_capacity = 20, 139 }, 140 { 141 .name = "charger/battery_2", 142 .disp_time = 750, 143 .min_capacity = 40, 144 }, 145 { 146 .name = "charger/battery_3", 147 .disp_time = 750, 148 .min_capacity = 60, 149 }, 150 { 151 .name = "charger/battery_4", 152 .disp_time = 750, 153 .min_capacity = 80, 154 }, 155}; 156 157static struct animation battery_animation = { 158 .frames = batt_anim_frames, 159 .num_frames = ARRAY_SIZE(batt_anim_frames), 160 .num_cycles = 3, 161}; 162 163static struct charger charger_state = { 164 .batt_anim = &battery_animation, 165}; 166 167static int char_width; 168static int char_height; 169 170/* current time in milliseconds */ 171static int64_t curr_time_ms(void) 172{ 173 struct timespec tm; 174 clock_gettime(CLOCK_MONOTONIC, &tm); 175 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC); 176} 177 178static void clear_screen(void) 179{ 180 gr_color(0, 0, 0, 255); 181 gr_fill(0, 0, gr_fb_width(), gr_fb_height()); 182}; 183 184static int read_file(const char *path, char *buf, size_t sz) 185{ 186 int fd; 187 size_t cnt; 188 189 fd = open(path, O_RDONLY, 0); 190 if (fd < 0) 191 goto err; 192 193 cnt = read(fd, buf, sz - 1); 194 if (cnt <= 0) 195 goto err; 196 buf[cnt] = '\0'; 197 if (buf[cnt - 1] == '\n') { 198 cnt--; 199 buf[cnt] = '\0'; 200 } 201 202 close(fd); 203 return cnt; 204 205err: 206 if (fd >= 0) 207 close(fd); 208 return -1; 209} 210 211static int read_file_int(const char *path, int *val) 212{ 213 char buf[32]; 214 int ret; 215 int tmp; 216 char *end; 217 218 ret = read_file(path, buf, sizeof(buf)); 219 if (ret < 0) 220 return -1; 221 222 tmp = strtol(buf, &end, 0); 223 if (end == buf || 224 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0'))) 225 goto err; 226 227 *val = tmp; 228 return 0; 229 230err: 231 return -1; 232} 233 234static struct power_supply *find_supply(struct charger *charger, 235 const char *name) 236{ 237 struct listnode *node; 238 struct power_supply *supply; 239 240 list_for_each(node, &charger->supplies) { 241 supply = node_to_item(node, struct power_supply, list); 242 if (!strncmp(name, supply->name, sizeof(supply->name))) 243 return supply; 244 } 245 return NULL; 246} 247 248static struct power_supply *add_supply(struct charger *charger, 249 const char *name, const char *type, 250 const char *path, bool online) 251{ 252 struct power_supply *supply; 253 254 supply = calloc(1, sizeof(struct power_supply)); 255 if (!supply) 256 return NULL; 257 258 strlcpy(supply->name, name, sizeof(supply->name)); 259 strlcpy(supply->type, type, sizeof(supply->type)); 260 snprintf(supply->cap_path, sizeof(supply->cap_path), 261 "/sys/%s/capacity", path); 262 supply->online = online; 263 list_add_tail(&charger->supplies, &supply->list); 264 charger->num_supplies++; 265 LOGV("... added %s %s %d\n", supply->name, supply->type, online); 266 return supply; 267} 268 269static void remove_supply(struct charger *charger, struct power_supply *supply) 270{ 271 if (!supply) 272 return; 273 list_remove(&supply->list); 274 charger->num_supplies--; 275 free(supply); 276} 277 278static void parse_uevent(const char *msg, struct uevent *uevent) 279{ 280 uevent->action = ""; 281 uevent->path = ""; 282 uevent->subsystem = ""; 283 uevent->ps_name = ""; 284 uevent->ps_online = ""; 285 uevent->ps_type = ""; 286 287 /* currently ignoring SEQNUM */ 288 while (*msg) { 289#ifdef DEBUG_UEVENTS 290 LOGV("uevent str: %s\n", msg); 291#endif 292 if (!strncmp(msg, "ACTION=", 7)) { 293 msg += 7; 294 uevent->action = msg; 295 } else if (!strncmp(msg, "DEVPATH=", 8)) { 296 msg += 8; 297 uevent->path = msg; 298 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) { 299 msg += 10; 300 uevent->subsystem = msg; 301 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) { 302 msg += 18; 303 uevent->ps_name = msg; 304 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) { 305 msg += 20; 306 uevent->ps_online = msg; 307 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) { 308 msg += 18; 309 uevent->ps_type = msg; 310 } 311 312 /* advance to after the next \0 */ 313 while (*msg++) 314 ; 315 } 316 317 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n", 318 uevent->action, uevent->path, uevent->subsystem, 319 uevent->ps_name, uevent->ps_type, uevent->ps_online); 320} 321 322static void process_ps_uevent(struct charger *charger, struct uevent *uevent) 323{ 324 int online; 325 char ps_type[32]; 326 struct power_supply *supply = NULL; 327 int i; 328 bool was_online = false; 329 bool battery = false; 330 331 if (uevent->ps_type[0] == '\0') { 332 char *path; 333 int ret; 334 335 if (uevent->path[0] == '\0') 336 return; 337 ret = asprintf(&path, "/sys/%s/type", uevent->path); 338 if (ret <= 0) 339 return; 340 ret = read_file(path, ps_type, sizeof(ps_type)); 341 free(path); 342 if (ret < 0) 343 return; 344 } else { 345 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type)); 346 } 347 348 if (!strncmp(ps_type, "Battery", 7)) 349 battery = true; 350 351 online = atoi(uevent->ps_online); 352 supply = find_supply(charger, uevent->ps_name); 353 if (supply) { 354 was_online = supply->online; 355 supply->online = online; 356 } 357 358 if (!strcmp(uevent->action, "add")) { 359 if (!supply) { 360 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path, 361 online); 362 if (!supply) { 363 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name, 364 uevent->ps_type, online); 365 return; 366 } 367 /* only pick up the first battery for now */ 368 if (battery && !charger->battery) 369 charger->battery = supply; 370 } else { 371 LOGE("supply '%s' already exists..\n", uevent->ps_name); 372 } 373 } else if (!strcmp(uevent->action, "remove")) { 374 if (supply) { 375 if (charger->battery == supply) 376 charger->battery = NULL; 377 remove_supply(charger, supply); 378 supply = NULL; 379 } 380 } else if (!strcmp(uevent->action, "change")) { 381 if (!supply) { 382 LOGE("power supply '%s' not found ('%s' %d)\n", 383 uevent->ps_name, ps_type, online); 384 return; 385 } 386 } else { 387 return; 388 } 389 390 /* allow battery to be managed in the supply list but make it not 391 * contribute to online power supplies. */ 392 if (!battery) { 393 if (was_online && !online) 394 charger->num_supplies_online--; 395 else if (supply && !was_online && online) 396 charger->num_supplies_online++; 397 } 398 399 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n", 400 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline", 401 uevent->action, charger->num_supplies_online, charger->num_supplies); 402} 403 404static void process_uevent(struct charger *charger, struct uevent *uevent) 405{ 406 if (!strcmp(uevent->subsystem, "power_supply")) 407 process_ps_uevent(charger, uevent); 408} 409 410#define UEVENT_MSG_LEN 1024 411static int handle_uevent_fd(struct charger *charger, int fd) 412{ 413 char msg[UEVENT_MSG_LEN+2]; 414 int n; 415 416 if (fd < 0) 417 return -1; 418 419 while (true) { 420 struct uevent uevent; 421 422 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN); 423 if (n <= 0) 424 break; 425 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */ 426 continue; 427 428 msg[n] = '\0'; 429 msg[n+1] = '\0'; 430 431 parse_uevent(msg, &uevent); 432 process_uevent(charger, &uevent); 433 } 434 435 return 0; 436} 437 438static int uevent_callback(int fd, short revents, void *data) 439{ 440 struct charger *charger = data; 441 442 if (!(revents & POLLIN)) 443 return -1; 444 return handle_uevent_fd(charger, fd); 445} 446 447/* force the kernel to regenerate the change events for the existing 448 * devices, if valid */ 449static void do_coldboot(struct charger *charger, DIR *d, const char *event, 450 bool follow_links, int max_depth) 451{ 452 struct dirent *de; 453 int dfd, fd; 454 455 dfd = dirfd(d); 456 457 fd = openat(dfd, "uevent", O_WRONLY); 458 if (fd >= 0) { 459 write(fd, event, strlen(event)); 460 close(fd); 461 handle_uevent_fd(charger, charger->uevent_fd); 462 } 463 464 while ((de = readdir(d)) && max_depth > 0) { 465 DIR *d2; 466 467 LOGV("looking at '%s'\n", de->d_name); 468 469 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) || 470 de->d_name[0] == '.') { 471 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n", 472 de->d_name, de->d_type, max_depth, follow_links); 473 continue; 474 } 475 LOGV("can descend into '%s'\n", de->d_name); 476 477 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 478 if (fd < 0) { 479 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name, 480 errno, strerror(errno)); 481 continue; 482 } 483 484 d2 = fdopendir(fd); 485 if (d2 == 0) 486 close(fd); 487 else { 488 LOGV("opened '%s'\n", de->d_name); 489 do_coldboot(charger, d2, event, follow_links, max_depth - 1); 490 closedir(d2); 491 } 492 } 493} 494 495static void coldboot(struct charger *charger, const char *path, 496 const char *event) 497{ 498 char str[256]; 499 500 LOGV("doing coldboot '%s' in '%s'\n", event, path); 501 DIR *d = opendir(path); 502 if (d) { 503 snprintf(str, sizeof(str), "%s\n", event); 504 do_coldboot(charger, d, str, true, 1); 505 closedir(d); 506 } 507} 508 509static int draw_text(const char *str, int x, int y) 510{ 511 int str_len_px = gr_measure(str); 512 513 if (x < 0) 514 x = (gr_fb_width() - str_len_px) / 2; 515 if (y < 0) 516 y = (gr_fb_height() - char_height) / 2; 517 gr_text(x, y, str); 518 519 return y + char_height; 520} 521 522static void android_green(void) 523{ 524 gr_color(0xa4, 0xc6, 0x39, 255); 525} 526 527/* returns the last y-offset of where the surface ends */ 528static int draw_surface_centered(struct charger *charger, gr_surface surface) 529{ 530 int w; 531 int h; 532 int x; 533 int y; 534 535 w = gr_get_width(surface); 536 h = gr_get_height(surface); 537 x = (gr_fb_width() - w) / 2 ; 538 y = (gr_fb_height() - h) / 2 ; 539 540 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y); 541 gr_blit(surface, 0, 0, w, h, x, y); 542 return y + h; 543} 544 545static void draw_unknown(struct charger *charger) 546{ 547 int y; 548 if (charger->surf_unknown) { 549 draw_surface_centered(charger, charger->surf_unknown); 550 } else { 551 android_green(); 552 y = draw_text("Charging!", -1, -1); 553 draw_text("?\?/100", -1, y + 25); 554 } 555} 556 557static void draw_battery(struct charger *charger) 558{ 559 struct animation *batt_anim = charger->batt_anim; 560 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame]; 561 562 if (batt_anim->num_frames != 0) { 563 draw_surface_centered(charger, frame->surface); 564 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n", 565 batt_anim->cur_frame, frame->name, frame->min_capacity, 566 frame->disp_time); 567 } 568} 569 570static void redraw_screen(struct charger *charger) 571{ 572 struct animation *batt_anim = charger->batt_anim; 573 574 clear_screen(); 575 576 /* try to display *something* */ 577 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0) 578 draw_unknown(charger); 579 else 580 draw_battery(charger); 581 gr_flip(); 582} 583 584static void kick_animation(struct animation *anim) 585{ 586 anim->run = true; 587} 588 589static void reset_animation(struct animation *anim) 590{ 591 anim->cur_cycle = 0; 592 anim->cur_frame = 0; 593 anim->run = false; 594} 595 596static void update_screen_state(struct charger *charger, int64_t now) 597{ 598 struct animation *batt_anim = charger->batt_anim; 599 int cur_frame; 600 int disp_time; 601 602 if (!batt_anim->run || now < charger->next_screen_transition) 603 return; 604 605 /* animation is over, blank screen and leave */ 606 if (batt_anim->cur_cycle == batt_anim->num_cycles) { 607 reset_animation(batt_anim); 608 charger->next_screen_transition = -1; 609 gr_fb_blank(true); 610 LOGV("[%lld] animation done\n", now); 611 return; 612 } 613 614 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time; 615 616 /* animation starting, set up the animation */ 617 if (batt_anim->cur_frame == 0) { 618 int batt_cap; 619 int ret; 620 621 LOGV("[%lld] animation starting\n", now); 622 ret = read_file_int(charger->battery->cap_path, &batt_cap); 623 if (ret < 0 || batt_cap > 100) { 624 batt_cap = -1; 625 } else if (batt_anim->num_frames != 0) { 626 int i; 627 628 /* find first frame given current capacity */ 629 for (i = 1; i < batt_anim->num_frames; i++) { 630 if (batt_cap < batt_anim->frames[i].min_capacity) 631 break; 632 } 633 batt_anim->cur_frame = i - 1; 634 635 /* show the first frame for twice as long */ 636 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2; 637 } 638 639 batt_anim->capacity = batt_cap; 640 } 641 642 /* unblank the screen on first cycle */ 643 if (batt_anim->cur_cycle == 0) 644 gr_fb_blank(false); 645 646 /* draw the new frame (@ cur_frame) */ 647 redraw_screen(charger); 648 649 /* if we don't have anim frames, we only have one image, so just bump 650 * the cycle counter and exit 651 */ 652 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) { 653 LOGV("[%lld] animation missing or unknown battery status\n", now); 654 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; 655 batt_anim->cur_cycle++; 656 return; 657 } 658 659 /* schedule next screen transition */ 660 charger->next_screen_transition = now + disp_time; 661 662 /* advance frame cntr to the next valid frame 663 * if necessary, advance cycle cntr, and reset frame cntr 664 */ 665 batt_anim->cur_frame++; 666 if (batt_anim->cur_frame == batt_anim->num_frames) { 667 batt_anim->cur_cycle++; 668 batt_anim->cur_frame = 0; 669 670 /* don't reset the cycle counter, since we use that as a signal 671 * in a test above to check if animation is over 672 */ 673 } 674} 675 676static void update_input_state(struct charger *charger, 677 struct input_event *ev, 678 int64_t now) 679{ 680 int down = !!ev->value; 681 682 if (ev->type != EV_KEY || ev->code > KEY_MAX) 683 return; 684 685 /* only record the down even timestamp, as the amount 686 * of time the key spent not being pressed is not useful */ 687 if (down) 688 charger->keys[ev->code].timestamp = now; 689 charger->keys[ev->code].down = down; 690 charger->keys[ev->code].pending = true; 691 if (down) { 692 LOGV("[%lld] key[%d] down\n", now, ev->code); 693 } else { 694 int64_t duration = now - charger->keys[ev->code].timestamp; 695 int64_t secs = duration / 1000; 696 int64_t msecs = duration - secs * 1000; 697 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now, 698 ev->code, secs, msecs); 699 } 700} 701 702static void set_next_key_check(struct charger *charger, 703 struct key_state *key, 704 int64_t timeout) 705{ 706 int64_t then = key->timestamp + timeout; 707 708 if (charger->next_key_check == -1 || then < charger->next_key_check) 709 charger->next_key_check = then; 710} 711 712static void process_key(struct charger *charger, int code, int64_t now) 713{ 714 struct key_state *key = &charger->keys[code]; 715 int64_t next_key_check; 716 717 if (code == KEY_POWER) { 718 if (key->down) { 719 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME; 720 if (now >= reboot_timeout) { 721 LOGI("[%lld] rebooting\n", now); 722 android_reboot(ANDROID_RB_RESTART, 0, 0); 723 } else { 724 /* if the key is pressed but timeout hasn't expired, 725 * make sure we wake up at the right-ish time to check 726 */ 727 set_next_key_check(charger, key, POWER_ON_KEY_TIME); 728 } 729 } else { 730 /* if the power key got released, force screen state cycle */ 731 if (key->pending) 732 kick_animation(charger->batt_anim); 733 } 734 } 735 736 key->pending = false; 737} 738 739static void handle_input_state(struct charger *charger, int64_t now) 740{ 741 process_key(charger, KEY_POWER, now); 742 743 if (charger->next_key_check != -1 && now > charger->next_key_check) 744 charger->next_key_check = -1; 745} 746 747static void handle_power_supply_state(struct charger *charger, int64_t now) 748{ 749 if (charger->num_supplies_online == 0) { 750 if (charger->next_pwr_check == -1) { 751 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME; 752 kick_animation(charger->batt_anim); 753 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n", 754 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check); 755 } else if (now >= charger->next_pwr_check) { 756 LOGI("[%lld] shutting down\n", now); 757 android_reboot(ANDROID_RB_POWEROFF, 0, 0); 758 } else { 759 /* otherwise we already have a shutdown timer scheduled */ 760 } 761 } else { 762 /* online supply present, reset shutdown timer if set */ 763 if (charger->next_pwr_check != -1) { 764 LOGI("[%lld] device plugged in: shutdown cancelled\n", now); 765 kick_animation(charger->batt_anim); 766 } 767 charger->next_pwr_check = -1; 768 } 769} 770 771static void wait_next_event(struct charger *charger, int64_t now) 772{ 773 int64_t next_event = INT64_MAX; 774 int64_t timeout; 775 struct input_event ev; 776 int ret; 777 778 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now, 779 charger->next_screen_transition, charger->next_key_check, 780 charger->next_pwr_check); 781 782 if (charger->next_screen_transition != -1) 783 next_event = charger->next_screen_transition; 784 if (charger->next_key_check != -1 && charger->next_key_check < next_event) 785 next_event = charger->next_key_check; 786 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event) 787 next_event = charger->next_pwr_check; 788 789 if (next_event != -1 && next_event != INT64_MAX) 790 timeout = max(0, next_event - now); 791 else 792 timeout = -1; 793 LOGV("[%lld] blocking (%lld)\n", now, timeout); 794 ret = ev_wait((int)timeout); 795 if (!ret) 796 ev_dispatch(); 797} 798 799static int input_callback(int fd, short revents, void *data) 800{ 801 struct charger *charger = data; 802 struct input_event ev; 803 int ret; 804 805 ret = ev_get_input(fd, revents, &ev); 806 if (ret) 807 return -1; 808 update_input_state(charger, &ev, curr_time_ms()); 809 return 0; 810} 811 812static void event_loop(struct charger *charger) 813{ 814 int ret; 815 816 while (true) { 817 int64_t now = curr_time_ms(); 818 819 LOGV("[%lld] event_loop()\n", now); 820 handle_input_state(charger, now); 821 handle_power_supply_state(charger, now); 822 823 /* do screen update last in case any of the above want to start 824 * screen transitions (animations, etc) 825 */ 826 update_screen_state(charger, now); 827 828 wait_next_event(charger, now); 829 } 830} 831 832int main(int argc, char **argv) 833{ 834 int ret; 835 struct charger *charger = &charger_state; 836 int64_t now = curr_time_ms() - 1; 837 int fd; 838 int i; 839 840 list_init(&charger->supplies); 841 842 klog_init(); 843 klog_set_level(CHARGER_KLOG_LEVEL); 844 845 gr_init(); 846 gr_font_size(&char_width, &char_height); 847 848 ev_init(input_callback, charger); 849 850 fd = uevent_open_socket(64*1024, true); 851 if (fd >= 0) { 852 fcntl(fd, F_SETFL, O_NONBLOCK); 853 ev_add_fd(fd, uevent_callback, charger); 854 } 855 charger->uevent_fd = fd; 856 coldboot(charger, "/sys/class/power_supply", "add"); 857 858 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown); 859 if (ret < 0) { 860 LOGE("Cannot load image\n"); 861 charger->surf_unknown = NULL; 862 } 863 864 for (i = 0; i < charger->batt_anim->num_frames; i++) { 865 struct frame *frame = &charger->batt_anim->frames[i]; 866 867 ret = res_create_surface(frame->name, &frame->surface); 868 if (ret < 0) { 869 LOGE("Cannot load image %s\n", frame->name); 870 /* TODO: free the already allocated surfaces... */ 871 charger->batt_anim->num_frames = 0; 872 charger->batt_anim->num_cycles = 1; 873 break; 874 } 875 } 876 877 gr_fb_blank(true); 878 879 charger->next_screen_transition = now - 1; 880 charger->next_key_check = -1; 881 charger->next_pwr_check = -1; 882 reset_animation(charger->batt_anim); 883 kick_animation(charger->batt_anim); 884 885 event_loop(charger); 886 887 return 0; 888} 889