Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame^] | 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 <linux/input.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/epoll.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/un.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <sys/socket.h> |
| 33 | #include <linux/netlink.h> |
| 34 | |
| 35 | #include <batteryservice/BatteryService.h> |
| 36 | #include <cutils/android_reboot.h> |
| 37 | #include <cutils/klog.h> |
| 38 | #include <cutils/misc.h> |
| 39 | |
| 40 | #ifdef CHARGER_ENABLE_SUSPEND |
| 41 | #include <suspend/autosuspend.h> |
| 42 | #endif |
| 43 | |
| 44 | #include "minui/minui.h" |
| 45 | |
| 46 | #include "healthd.h" |
| 47 | |
| 48 | #ifndef max |
| 49 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
| 50 | #endif |
| 51 | |
| 52 | #ifndef min |
| 53 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
| 54 | #endif |
| 55 | |
| 56 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 57 | |
| 58 | #define MSEC_PER_SEC (1000LL) |
| 59 | #define NSEC_PER_MSEC (1000000LL) |
| 60 | |
| 61 | #define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC) |
| 62 | #define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) |
| 63 | #define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC) |
| 64 | |
| 65 | #define BATTERY_FULL_THRESH 95 |
| 66 | |
| 67 | #define LAST_KMSG_PATH "/proc/last_kmsg" |
| 68 | #define LAST_KMSG_MAX_SZ (32 * 1024) |
| 69 | |
| 70 | #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) |
| 71 | #define LOGI(x...) do { KLOG_INFO("charger", x); } while (0) |
| 72 | #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) |
| 73 | |
| 74 | struct key_state { |
| 75 | bool pending; |
| 76 | bool down; |
| 77 | int64_t timestamp; |
| 78 | }; |
| 79 | |
| 80 | struct frame { |
| 81 | const char *name; |
| 82 | int disp_time; |
| 83 | int min_capacity; |
| 84 | bool level_only; |
| 85 | |
| 86 | gr_surface surface; |
| 87 | }; |
| 88 | |
| 89 | struct animation { |
| 90 | bool run; |
| 91 | |
| 92 | struct frame *frames; |
| 93 | int cur_frame; |
| 94 | int num_frames; |
| 95 | |
| 96 | int cur_cycle; |
| 97 | int num_cycles; |
| 98 | |
| 99 | /* current capacity being animated */ |
| 100 | int capacity; |
| 101 | }; |
| 102 | |
| 103 | struct charger { |
| 104 | bool have_battery_state; |
| 105 | bool charger_connected; |
| 106 | int capacity; |
| 107 | int64_t next_screen_transition; |
| 108 | int64_t next_key_check; |
| 109 | int64_t next_pwr_check; |
| 110 | |
| 111 | struct key_state keys[KEY_MAX + 1]; |
| 112 | |
| 113 | struct animation *batt_anim; |
| 114 | gr_surface surf_unknown; |
| 115 | }; |
| 116 | |
| 117 | static struct frame batt_anim_frames[] = { |
| 118 | { |
| 119 | .name = "charger/battery_0", |
| 120 | .disp_time = 750, |
| 121 | .min_capacity = 0, |
| 122 | .level_only = false, |
| 123 | .surface = NULL, |
| 124 | }, |
| 125 | { |
| 126 | .name = "charger/battery_1", |
| 127 | .disp_time = 750, |
| 128 | .min_capacity = 20, |
| 129 | .level_only = false, |
| 130 | .surface = NULL, |
| 131 | }, |
| 132 | { |
| 133 | .name = "charger/battery_2", |
| 134 | .disp_time = 750, |
| 135 | .min_capacity = 40, |
| 136 | .level_only = false, |
| 137 | .surface = NULL, |
| 138 | }, |
| 139 | { |
| 140 | .name = "charger/battery_3", |
| 141 | .disp_time = 750, |
| 142 | .min_capacity = 60, |
| 143 | .level_only = false, |
| 144 | .surface = NULL, |
| 145 | }, |
| 146 | { |
| 147 | .name = "charger/battery_4", |
| 148 | .disp_time = 750, |
| 149 | .min_capacity = 80, |
| 150 | .level_only = true, |
| 151 | .surface = NULL, |
| 152 | }, |
| 153 | { |
| 154 | .name = "charger/battery_5", |
| 155 | .disp_time = 750, |
| 156 | .min_capacity = BATTERY_FULL_THRESH, |
| 157 | .level_only = false, |
| 158 | .surface = NULL, |
| 159 | }, |
| 160 | }; |
| 161 | |
| 162 | static struct animation battery_animation = { |
| 163 | .run = false, |
| 164 | .frames = batt_anim_frames, |
| 165 | .cur_frame = 0, |
| 166 | .num_frames = ARRAY_SIZE(batt_anim_frames), |
| 167 | .cur_cycle = 0, |
| 168 | .num_cycles = 3, |
| 169 | .capacity = 0, |
| 170 | }; |
| 171 | |
| 172 | static struct charger charger_state; |
| 173 | |
| 174 | static int char_width; |
| 175 | static int char_height; |
| 176 | |
| 177 | /* current time in milliseconds */ |
| 178 | static int64_t curr_time_ms(void) |
| 179 | { |
| 180 | struct timespec tm; |
| 181 | clock_gettime(CLOCK_MONOTONIC, &tm); |
| 182 | return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC); |
| 183 | } |
| 184 | |
| 185 | static void clear_screen(void) |
| 186 | { |
| 187 | gr_color(0, 0, 0, 255); |
| 188 | gr_fill(0, 0, gr_fb_width(), gr_fb_height()); |
| 189 | }; |
| 190 | |
| 191 | #define MAX_KLOG_WRITE_BUF_SZ 256 |
| 192 | |
| 193 | static void dump_last_kmsg(void) |
| 194 | { |
| 195 | char *buf; |
| 196 | char *ptr; |
| 197 | unsigned sz = 0; |
| 198 | int len; |
| 199 | |
| 200 | LOGI("\n"); |
| 201 | LOGI("*************** LAST KMSG ***************\n"); |
| 202 | LOGI("\n"); |
| 203 | buf = (char *)load_file(LAST_KMSG_PATH, &sz); |
| 204 | if (!buf || !sz) { |
| 205 | LOGI("last_kmsg not found. Cold reset?\n"); |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | len = min(sz, LAST_KMSG_MAX_SZ); |
| 210 | ptr = buf + (sz - len); |
| 211 | |
| 212 | while (len > 0) { |
| 213 | int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ); |
| 214 | char yoink; |
| 215 | char *nl; |
| 216 | |
| 217 | nl = (char *)memrchr(ptr, '\n', cnt - 1); |
| 218 | if (nl) |
| 219 | cnt = nl - ptr + 1; |
| 220 | |
| 221 | yoink = ptr[cnt]; |
| 222 | ptr[cnt] = '\0'; |
| 223 | klog_write(6, "<6>%s", ptr); |
| 224 | ptr[cnt] = yoink; |
| 225 | |
| 226 | len -= cnt; |
| 227 | ptr += cnt; |
| 228 | } |
| 229 | |
| 230 | free(buf); |
| 231 | |
| 232 | out: |
| 233 | LOGI("\n"); |
| 234 | LOGI("************* END LAST KMSG *************\n"); |
| 235 | LOGI("\n"); |
| 236 | } |
| 237 | |
| 238 | static int get_battery_capacity() |
| 239 | { |
| 240 | return charger_state.capacity; |
| 241 | } |
| 242 | |
| 243 | #ifdef CHARGER_ENABLE_SUSPEND |
| 244 | static int request_suspend(bool enable) |
| 245 | { |
| 246 | if (enable) |
| 247 | return autosuspend_enable(); |
| 248 | else |
| 249 | return autosuspend_disable(); |
| 250 | } |
| 251 | #else |
| 252 | static int request_suspend(bool enable) |
| 253 | { |
| 254 | return 0; |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | static 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 | |
| 271 | static 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 */ |
| 277 | static 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 | |
| 294 | static 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 | |
| 306 | static 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 name=%s min_cap=%d time=%d\n", |
| 314 | batt_anim->cur_frame, frame->name, frame->min_capacity, |
| 315 | frame->disp_time); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static 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 | |
| 333 | static void kick_animation(struct animation *anim) |
| 334 | { |
| 335 | anim->run = true; |
| 336 | } |
| 337 | |
| 338 | static void reset_animation(struct animation *anim) |
| 339 | { |
| 340 | anim->cur_cycle = 0; |
| 341 | anim->cur_frame = 0; |
| 342 | anim->run = false; |
| 343 | } |
| 344 | |
| 345 | static 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 | /* animation is over, blank screen and leave */ |
| 355 | if (batt_anim->cur_cycle == batt_anim->num_cycles) { |
| 356 | reset_animation(batt_anim); |
| 357 | charger->next_screen_transition = -1; |
| 358 | gr_fb_blank(true); |
| 359 | LOGV("[%lld] animation done\n", now); |
| 360 | if (!charger->charger_connected) |
| 361 | request_suspend(true); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time; |
| 366 | |
| 367 | /* animation starting, set up the animation */ |
| 368 | if (batt_anim->cur_frame == 0) { |
| 369 | int batt_cap; |
| 370 | int ret; |
| 371 | |
| 372 | LOGV("[%lld] animation starting\n", now); |
| 373 | batt_cap = get_battery_capacity(); |
| 374 | if (batt_cap >= 0 && batt_anim->num_frames != 0) { |
| 375 | int i; |
| 376 | |
| 377 | /* find first frame given current capacity */ |
| 378 | for (i = 1; i < batt_anim->num_frames; i++) { |
| 379 | if (batt_cap < batt_anim->frames[i].min_capacity) |
| 380 | break; |
| 381 | } |
| 382 | batt_anim->cur_frame = i - 1; |
| 383 | |
| 384 | /* show the first frame for twice as long */ |
| 385 | disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2; |
| 386 | } |
| 387 | |
| 388 | batt_anim->capacity = batt_cap; |
| 389 | } |
| 390 | |
| 391 | /* unblank the screen on first cycle */ |
| 392 | if (batt_anim->cur_cycle == 0) |
| 393 | gr_fb_blank(false); |
| 394 | |
| 395 | /* draw the new frame (@ cur_frame) */ |
| 396 | redraw_screen(charger); |
| 397 | |
| 398 | /* if we don't have anim frames, we only have one image, so just bump |
| 399 | * the cycle counter and exit |
| 400 | */ |
| 401 | if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) { |
| 402 | LOGV("[%lld] animation missing or unknown battery status\n", now); |
| 403 | charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME; |
| 404 | batt_anim->cur_cycle++; |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | /* schedule next screen transition */ |
| 409 | charger->next_screen_transition = now + disp_time; |
| 410 | |
| 411 | /* advance frame cntr to the next valid frame |
| 412 | * if necessary, advance cycle cntr, and reset frame cntr |
| 413 | */ |
| 414 | batt_anim->cur_frame++; |
| 415 | |
| 416 | /* if the frame is used for level-only, that is only show it when it's |
| 417 | * the current level, skip it during the animation. |
| 418 | */ |
| 419 | while (batt_anim->cur_frame < batt_anim->num_frames && |
| 420 | batt_anim->frames[batt_anim->cur_frame].level_only) |
| 421 | batt_anim->cur_frame++; |
| 422 | if (batt_anim->cur_frame >= batt_anim->num_frames) { |
| 423 | batt_anim->cur_cycle++; |
| 424 | batt_anim->cur_frame = 0; |
| 425 | |
| 426 | /* don't reset the cycle counter, since we use that as a signal |
| 427 | * in a test above to check if animation is over |
| 428 | */ |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static int set_key_callback(int code, int value, void *data) |
| 433 | { |
| 434 | struct charger *charger = (struct charger *)data; |
| 435 | int64_t now = curr_time_ms(); |
| 436 | int down = !!value; |
| 437 | |
| 438 | if (code > KEY_MAX) |
| 439 | return -1; |
| 440 | |
| 441 | /* ignore events that don't modify our state */ |
| 442 | if (charger->keys[code].down == down) |
| 443 | return 0; |
| 444 | |
| 445 | /* only record the down even timestamp, as the amount |
| 446 | * of time the key spent not being pressed is not useful */ |
| 447 | if (down) |
| 448 | charger->keys[code].timestamp = now; |
| 449 | charger->keys[code].down = down; |
| 450 | charger->keys[code].pending = true; |
| 451 | if (down) { |
| 452 | LOGV("[%lld] key[%d] down\n", now, code); |
| 453 | } else { |
| 454 | int64_t duration = now - charger->keys[code].timestamp; |
| 455 | int64_t secs = duration / 1000; |
| 456 | int64_t msecs = duration - secs * 1000; |
| 457 | LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now, |
| 458 | code, secs, msecs); |
| 459 | } |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static void update_input_state(struct charger *charger, |
| 465 | struct input_event *ev) |
| 466 | { |
| 467 | if (ev->type != EV_KEY) |
| 468 | return; |
| 469 | set_key_callback(ev->code, ev->value, charger); |
| 470 | } |
| 471 | |
| 472 | static void set_next_key_check(struct charger *charger, |
| 473 | struct key_state *key, |
| 474 | int64_t timeout) |
| 475 | { |
| 476 | int64_t then = key->timestamp + timeout; |
| 477 | |
| 478 | if (charger->next_key_check == -1 || then < charger->next_key_check) |
| 479 | charger->next_key_check = then; |
| 480 | } |
| 481 | |
| 482 | static void process_key(struct charger *charger, int code, int64_t now) |
| 483 | { |
| 484 | struct key_state *key = &charger->keys[code]; |
| 485 | int64_t next_key_check; |
| 486 | |
| 487 | if (code == KEY_POWER) { |
| 488 | if (key->down) { |
| 489 | int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME; |
| 490 | if (now >= reboot_timeout) { |
| 491 | LOGI("[%lld] rebooting\n", now); |
| 492 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 493 | } else { |
| 494 | /* if the key is pressed but timeout hasn't expired, |
| 495 | * make sure we wake up at the right-ish time to check |
| 496 | */ |
| 497 | set_next_key_check(charger, key, POWER_ON_KEY_TIME); |
| 498 | } |
| 499 | } else { |
| 500 | /* if the power key got released, force screen state cycle */ |
| 501 | if (key->pending) { |
| 502 | request_suspend(false); |
| 503 | kick_animation(charger->batt_anim); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | key->pending = false; |
| 509 | } |
| 510 | |
| 511 | static void handle_input_state(struct charger *charger, int64_t now) |
| 512 | { |
| 513 | process_key(charger, KEY_POWER, now); |
| 514 | |
| 515 | if (charger->next_key_check != -1 && now > charger->next_key_check) |
| 516 | charger->next_key_check = -1; |
| 517 | } |
| 518 | |
| 519 | static void handle_power_supply_state(struct charger *charger, int64_t now) |
| 520 | { |
| 521 | if (!charger->have_battery_state) |
| 522 | return; |
| 523 | |
| 524 | if (!charger->charger_connected) { |
| 525 | request_suspend(false); |
| 526 | if (charger->next_pwr_check == -1) { |
| 527 | charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME; |
| 528 | LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n", |
| 529 | now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check); |
| 530 | } else if (now >= charger->next_pwr_check) { |
| 531 | LOGI("[%lld] shutting down\n", now); |
| 532 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 533 | } else { |
| 534 | /* otherwise we already have a shutdown timer scheduled */ |
| 535 | } |
| 536 | } else { |
| 537 | /* online supply present, reset shutdown timer if set */ |
| 538 | if (charger->next_pwr_check != -1) { |
| 539 | LOGI("[%lld] device plugged in: shutdown cancelled\n", now); |
| 540 | kick_animation(charger->batt_anim); |
| 541 | } |
| 542 | charger->next_pwr_check = -1; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void healthd_mode_charger_heartbeat() |
| 547 | { |
| 548 | struct charger *charger = &charger_state; |
| 549 | int64_t now = curr_time_ms(); |
| 550 | int ret; |
| 551 | |
| 552 | handle_input_state(charger, now); |
| 553 | handle_power_supply_state(charger, now); |
| 554 | |
| 555 | /* do screen update last in case any of the above want to start |
| 556 | * screen transitions (animations, etc) |
| 557 | */ |
| 558 | update_screen_state(charger, now); |
| 559 | } |
| 560 | |
| 561 | void healthd_mode_charger_battery_update( |
| 562 | struct android::BatteryProperties *props) |
| 563 | { |
| 564 | struct charger *charger = &charger_state; |
| 565 | |
| 566 | charger->charger_connected = |
| 567 | props->chargerAcOnline || props->chargerUsbOnline || |
| 568 | props->chargerWirelessOnline; |
| 569 | charger->capacity = props->batteryLevel; |
| 570 | |
| 571 | if (!charger->have_battery_state) { |
| 572 | charger->have_battery_state = true; |
| 573 | charger->next_screen_transition = curr_time_ms() - 1; |
| 574 | reset_animation(charger->batt_anim); |
| 575 | kick_animation(charger->batt_anim); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | int healthd_mode_charger_preparetowait(void) |
| 580 | { |
| 581 | struct charger *charger = &charger_state; |
| 582 | int64_t now = curr_time_ms(); |
| 583 | int64_t next_event = INT64_MAX; |
| 584 | int64_t timeout; |
| 585 | struct input_event ev; |
| 586 | int ret; |
| 587 | |
| 588 | LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now, |
| 589 | charger->next_screen_transition, charger->next_key_check, |
| 590 | charger->next_pwr_check); |
| 591 | |
| 592 | if (charger->next_screen_transition != -1) |
| 593 | next_event = charger->next_screen_transition; |
| 594 | if (charger->next_key_check != -1 && charger->next_key_check < next_event) |
| 595 | next_event = charger->next_key_check; |
| 596 | if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event) |
| 597 | next_event = charger->next_pwr_check; |
| 598 | |
| 599 | if (next_event != -1 && next_event != INT64_MAX) |
| 600 | timeout = max(0, next_event - now); |
| 601 | else |
| 602 | timeout = -1; |
| 603 | |
| 604 | return (int)timeout; |
| 605 | } |
| 606 | |
| 607 | static int input_callback(int fd, unsigned int epevents, void *data) |
| 608 | { |
| 609 | struct charger *charger = (struct charger *)data; |
| 610 | struct input_event ev; |
| 611 | int ret; |
| 612 | |
| 613 | ret = ev_get_input(fd, epevents, &ev); |
| 614 | if (ret) |
| 615 | return -1; |
| 616 | update_input_state(charger, &ev); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static void charger_event_handler(uint32_t epevents) |
| 621 | { |
| 622 | int ret; |
| 623 | |
| 624 | ret = ev_wait(-1); |
| 625 | if (!ret) |
| 626 | ev_dispatch(); |
| 627 | } |
| 628 | |
| 629 | void healthd_mode_charger_init(struct healthd_config *config) |
| 630 | { |
| 631 | int ret; |
| 632 | struct charger *charger = &charger_state; |
| 633 | int i; |
| 634 | int epollfd; |
| 635 | |
| 636 | dump_last_kmsg(); |
| 637 | |
| 638 | LOGI("--------------- STARTING CHARGER MODE ---------------\n"); |
| 639 | |
| 640 | gr_init(); |
| 641 | gr_font_size(&char_width, &char_height); |
| 642 | |
| 643 | ret = ev_init(input_callback, charger); |
| 644 | if (!ret) { |
| 645 | epollfd = ev_get_epollfd(); |
| 646 | healthd_register_event(epollfd, charger_event_handler); |
| 647 | } |
| 648 | |
| 649 | ret = res_create_surface("charger/battery_fail", &charger->surf_unknown); |
| 650 | if (ret < 0) { |
| 651 | LOGE("Cannot load image\n"); |
| 652 | charger->surf_unknown = NULL; |
| 653 | } |
| 654 | |
| 655 | charger->batt_anim = &battery_animation; |
| 656 | |
| 657 | for (i = 0; i < charger->batt_anim->num_frames; i++) { |
| 658 | struct frame *frame = &charger->batt_anim->frames[i]; |
| 659 | |
| 660 | ret = res_create_surface(frame->name, &frame->surface); |
| 661 | if (ret < 0) { |
| 662 | LOGE("Cannot load image %s\n", frame->name); |
| 663 | /* TODO: free the already allocated surfaces... */ |
| 664 | charger->batt_anim->num_frames = 0; |
| 665 | charger->batt_anim->num_cycles = 1; |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | ev_sync_key_state(set_key_callback, charger); |
| 671 | |
| 672 | #ifndef CHARGER_DISABLE_INIT_BLANK |
| 673 | gr_fb_blank(true); |
| 674 | #endif |
| 675 | |
| 676 | charger->next_screen_transition = -1; |
| 677 | charger->next_key_check = -1; |
| 678 | charger->next_pwr_check = -1; |
| 679 | } |