Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 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 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 52 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 53 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 54 | #define MSEC_PER_SEC (1000LL) |
| 55 | #define NSEC_PER_MSEC (1000000LL) |
| 56 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 57 | #define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC) |
Dima Zavin | 92312a5 | 2011-09-16 13:15:47 -0700 | [diff] [blame] | 58 | #define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 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 | |
| 65 | struct key_state { |
| 66 | bool pending; |
| 67 | bool down; |
| 68 | int64_t timestamp; |
| 69 | }; |
| 70 | |
| 71 | struct power_supply { |
| 72 | struct listnode list; |
| 73 | char name[256]; |
| 74 | char type[32]; |
| 75 | bool online; |
| 76 | bool valid; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 77 | char cap_path[PATH_MAX]; |
| 78 | }; |
| 79 | |
| 80 | struct frame { |
| 81 | const char *name; |
| 82 | int disp_time; |
| 83 | int min_capacity; |
| 84 | |
| 85 | gr_surface surface; |
| 86 | }; |
| 87 | |
| 88 | struct 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; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct charger { |
| 103 | int64_t next_screen_transition; |
| 104 | int64_t next_key_check; |
| 105 | int64_t next_pwr_check; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 106 | |
| 107 | struct key_state keys[KEY_MAX + 1]; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 108 | int uevent_fd; |
| 109 | |
| 110 | struct listnode supplies; |
| 111 | int num_supplies; |
| 112 | int num_supplies_online; |
| 113 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 114 | struct animation *batt_anim; |
| 115 | gr_surface surf_unknown; |
| 116 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 117 | struct power_supply *battery; |
| 118 | }; |
| 119 | |
| 120 | struct 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 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 129 | static 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 | |
| 157 | static struct animation battery_animation = { |
| 158 | .frames = batt_anim_frames, |
| 159 | .num_frames = ARRAY_SIZE(batt_anim_frames), |
| 160 | .num_cycles = 3, |
| 161 | }; |
| 162 | |
| 163 | static struct charger charger_state = { |
| 164 | .batt_anim = &battery_animation, |
| 165 | }; |
| 166 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 167 | static int char_width; |
| 168 | static int char_height; |
| 169 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 170 | /* current time in milliseconds */ |
| 171 | static 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 | |
| 178 | static 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 | |
| 184 | static 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 | |
| 205 | err: |
| 206 | if (fd >= 0) |
| 207 | close(fd); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | static 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 | |
| 230 | err: |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | static 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 | |
| 248 | static struct power_supply *add_supply(struct charger *charger, |
| 249 | const char *name, const char *type, |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 250 | const char *path, bool online) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 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)); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 260 | snprintf(supply->cap_path, sizeof(supply->cap_path), |
| 261 | "/sys/%s/capacity", path); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 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 | |
| 269 | static 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 | |
| 278 | static 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 | |
| 322 | static 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) { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 360 | supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path, |
| 361 | online); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 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 | |
| 404 | static 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 |
| 411 | static 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 | |
| 438 | static 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 */ |
| 449 | static 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 | |
| 495 | static 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 | |
| 509 | static 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 | |
| 522 | static void android_green(void) |
| 523 | { |
| 524 | gr_color(0xa4, 0xc6, 0x39, 255); |
| 525 | } |
| 526 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 527 | /* returns the last y-offset of where the surface ends */ |
| 528 | static int draw_surface_centered(struct charger *charger, gr_surface surface) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 529 | { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 530 | int w; |
| 531 | int h; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 532 | int x; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 533 | int y; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 534 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 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 ; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 539 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 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 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 544 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 545 | static void draw_unknown(struct charger *charger) |
| 546 | { |
| 547 | int y; |
| 548 | if (charger->surf_unknown) { |
| 549 | draw_surface_centered(charger, charger->surf_unknown); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 550 | } else { |
| 551 | android_green(); |
| 552 | y = draw_text("Charging!", -1, -1); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 553 | draw_text("?\?/100", -1, y + 25); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 554 | } |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 555 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 556 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 557 | static 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); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 567 | } |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 568 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 569 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 570 | static void redraw_screen(struct charger *charger) |
| 571 | { |
| 572 | struct animation *batt_anim = charger->batt_anim; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 573 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 574 | clear_screen(); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 575 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 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); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 581 | gr_flip(); |
| 582 | } |
| 583 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 584 | static void kick_animation(struct animation *anim) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 585 | { |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 586 | anim->run = true; |
| 587 | } |
| 588 | |
| 589 | static void reset_animation(struct animation *anim) |
| 590 | { |
| 591 | anim->cur_cycle = 0; |
| 592 | anim->cur_frame = 0; |
| 593 | anim->run = false; |
| 594 | } |
| 595 | |
| 596 | static 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) |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 603 | return; |
| 604 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 605 | /* animation is over, blank screen and leave */ |
| 606 | if (batt_anim->cur_cycle == batt_anim->num_cycles) { |
| 607 | reset_animation(batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 608 | charger->next_screen_transition = -1; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 609 | gr_fb_blank(true); |
| 610 | LOGV("[%lld] animation done\n", now); |
| 611 | return; |
| 612 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 613 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 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 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | static 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 | |
| 702 | static 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 | |
| 712 | static 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) |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 732 | kick_animation(charger->batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | key->pending = false; |
| 737 | } |
| 738 | |
| 739 | static 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 | |
| 747 | static 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 | LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n", |
| 753 | now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check); |
| 754 | } else if (now >= charger->next_pwr_check) { |
| 755 | LOGI("[%lld] shutting down\n", now); |
| 756 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 757 | } else { |
| 758 | /* otherwise we already have a shutdown timer scheduled */ |
| 759 | } |
| 760 | } else { |
| 761 | /* online supply present, reset shutdown timer if set */ |
| 762 | if (charger->next_pwr_check != -1) { |
| 763 | LOGI("[%lld] device plugged in: shutdown cancelled\n", now); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 764 | kick_animation(charger->batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 765 | } |
| 766 | charger->next_pwr_check = -1; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | static void wait_next_event(struct charger *charger, int64_t now) |
| 771 | { |
| 772 | int64_t next_event = INT64_MAX; |
| 773 | int64_t timeout; |
| 774 | struct input_event ev; |
| 775 | int ret; |
| 776 | |
| 777 | LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now, |
| 778 | charger->next_screen_transition, charger->next_key_check, |
| 779 | charger->next_pwr_check); |
| 780 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 781 | if (charger->next_screen_transition != -1) |
| 782 | next_event = charger->next_screen_transition; |
| 783 | if (charger->next_key_check != -1 && charger->next_key_check < next_event) |
| 784 | next_event = charger->next_key_check; |
| 785 | if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event) |
| 786 | next_event = charger->next_pwr_check; |
| 787 | |
| 788 | if (next_event != -1 && next_event != INT64_MAX) |
| 789 | timeout = max(0, next_event - now); |
| 790 | else |
| 791 | timeout = -1; |
| 792 | LOGV("[%lld] blocking (%lld)\n", now, timeout); |
| 793 | ret = ev_wait((int)timeout); |
| 794 | if (!ret) |
| 795 | ev_dispatch(); |
| 796 | } |
| 797 | |
| 798 | static int input_callback(int fd, short revents, void *data) |
| 799 | { |
| 800 | struct charger *charger = data; |
| 801 | struct input_event ev; |
| 802 | int ret; |
| 803 | |
| 804 | ret = ev_get_input(fd, revents, &ev); |
| 805 | if (ret) |
| 806 | return -1; |
| 807 | update_input_state(charger, &ev, curr_time_ms()); |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static void event_loop(struct charger *charger) |
| 812 | { |
| 813 | int ret; |
| 814 | |
| 815 | while (true) { |
| 816 | int64_t now = curr_time_ms(); |
| 817 | |
| 818 | LOGV("[%lld] event_loop()\n", now); |
| 819 | handle_input_state(charger, now); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 820 | handle_power_supply_state(charger, now); |
| 821 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 822 | /* do screen update last in case any of the above want to start |
| 823 | * screen transitions (animations, etc) |
| 824 | */ |
| 825 | update_screen_state(charger, now); |
| 826 | |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 827 | wait_next_event(charger, now); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | int main(int argc, char **argv) |
| 832 | { |
| 833 | int ret; |
| 834 | struct charger *charger = &charger_state; |
| 835 | int64_t now = curr_time_ms() - 1; |
| 836 | int fd; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 837 | int i; |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 838 | |
| 839 | list_init(&charger->supplies); |
| 840 | |
| 841 | klog_init(); |
| 842 | klog_set_level(CHARGER_KLOG_LEVEL); |
| 843 | |
| 844 | gr_init(); |
| 845 | gr_font_size(&char_width, &char_height); |
| 846 | |
| 847 | ev_init(input_callback, charger); |
| 848 | |
| 849 | fd = uevent_open_socket(64*1024, true); |
| 850 | if (fd >= 0) { |
| 851 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 852 | ev_add_fd(fd, uevent_callback, charger); |
| 853 | } |
| 854 | charger->uevent_fd = fd; |
| 855 | coldboot(charger, "/sys/class/power_supply", "add"); |
| 856 | |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 857 | ret = res_create_surface("charger/battery_fail", &charger->surf_unknown); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 858 | if (ret < 0) { |
| 859 | LOGE("Cannot load image\n"); |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 860 | charger->surf_unknown = NULL; |
| 861 | } |
| 862 | |
| 863 | for (i = 0; i < charger->batt_anim->num_frames; i++) { |
| 864 | struct frame *frame = &charger->batt_anim->frames[i]; |
| 865 | |
| 866 | ret = res_create_surface(frame->name, &frame->surface); |
| 867 | if (ret < 0) { |
| 868 | LOGE("Cannot load image %s\n", frame->name); |
| 869 | /* TODO: free the already allocated surfaces... */ |
| 870 | charger->batt_anim->num_frames = 0; |
| 871 | charger->batt_anim->num_cycles = 1; |
| 872 | break; |
| 873 | } |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | gr_fb_blank(true); |
| 877 | |
| 878 | charger->next_screen_transition = now - 1; |
| 879 | charger->next_key_check = -1; |
| 880 | charger->next_pwr_check = -1; |
Dima Zavin | 0052abd | 2011-09-22 15:49:04 -0700 | [diff] [blame^] | 881 | reset_animation(charger->batt_anim); |
| 882 | kick_animation(charger->batt_anim); |
Dima Zavin | f48b236 | 2011-08-30 10:46:09 -0700 | [diff] [blame] | 883 | |
| 884 | event_loop(charger); |
| 885 | |
| 886 | return 0; |
| 887 | } |