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