blob: b63312cc2fbc333641cd77fdd0ff7f9d666008ba [file] [log] [blame]
Dima Zavinf48b2362011-08-30 10:46:09 -07001/*
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>
Dima Zavin823ebc42011-09-29 17:20:07 -070040#include <cutils/misc.h>
Dima Zavinf48b2362011-08-30 10:46:09 -070041#include <cutils/uevent.h>
42
43#include "minui/minui.h"
44
45#ifndef max
46#define max(a,b) ((a) > (b) ? (a) : (b))
47#endif
48
49#ifndef min
50#define min(a,b) ((a) < (b) ? (a) : (b))
51#endif
52
Dima Zavin0052abd2011-09-22 15:49:04 -070053#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
54
Dima Zavinf48b2362011-08-30 10:46:09 -070055#define MSEC_PER_SEC (1000LL)
56#define NSEC_PER_MSEC (1000000LL)
57
Dima Zavin0052abd2011-09-22 15:49:04 -070058#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070059#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070060#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
61
Dima Zavin1a5ca612011-09-26 14:14:19 -070062#define BATTERY_FULL_THRESH 95
63
Dima Zavin823ebc42011-09-29 17:20:07 -070064#define LAST_KMSG_PATH "/proc/last_kmsg"
65#define LAST_KMSG_MAX_SZ (32 * 1024)
66
Dima Zavinf48b2362011-08-30 10:46:09 -070067#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
68#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
69#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
70
71struct key_state {
72 bool pending;
73 bool down;
74 int64_t timestamp;
75};
76
77struct power_supply {
78 struct listnode list;
79 char name[256];
80 char type[32];
81 bool online;
82 bool valid;
Dima Zavin0052abd2011-09-22 15:49:04 -070083 char cap_path[PATH_MAX];
84};
85
86struct frame {
87 const char *name;
88 int disp_time;
89 int min_capacity;
90
91 gr_surface surface;
92};
93
94struct animation {
95 bool run;
96
97 struct frame *frames;
98 int cur_frame;
99 int num_frames;
100
101 int cur_cycle;
102 int num_cycles;
103
104 /* current capacity being animated */
105 int capacity;
Dima Zavinf48b2362011-08-30 10:46:09 -0700106};
107
108struct charger {
109 int64_t next_screen_transition;
110 int64_t next_key_check;
111 int64_t next_pwr_check;
Dima Zavinf48b2362011-08-30 10:46:09 -0700112
113 struct key_state keys[KEY_MAX + 1];
Dima Zavinf48b2362011-08-30 10:46:09 -0700114 int uevent_fd;
115
116 struct listnode supplies;
117 int num_supplies;
118 int num_supplies_online;
119
Dima Zavin0052abd2011-09-22 15:49:04 -0700120 struct animation *batt_anim;
121 gr_surface surf_unknown;
122
Dima Zavinf48b2362011-08-30 10:46:09 -0700123 struct power_supply *battery;
124};
125
126struct uevent {
127 const char *action;
128 const char *path;
129 const char *subsystem;
130 const char *ps_name;
131 const char *ps_type;
132 const char *ps_online;
133};
134
Dima Zavin0052abd2011-09-22 15:49:04 -0700135static struct frame batt_anim_frames[] = {
136 {
137 .name = "charger/battery_0",
138 .disp_time = 750,
139 .min_capacity = 0,
140 },
141 {
142 .name = "charger/battery_1",
143 .disp_time = 750,
144 .min_capacity = 20,
145 },
146 {
147 .name = "charger/battery_2",
148 .disp_time = 750,
149 .min_capacity = 40,
150 },
151 {
152 .name = "charger/battery_3",
153 .disp_time = 750,
154 .min_capacity = 60,
155 },
156 {
157 .name = "charger/battery_4",
158 .disp_time = 750,
159 .min_capacity = 80,
160 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700161 {
162 .name = "charger/battery_5",
163 .disp_time = 750,
164 .min_capacity = BATTERY_FULL_THRESH,
165 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700166};
167
168static struct animation battery_animation = {
169 .frames = batt_anim_frames,
170 .num_frames = ARRAY_SIZE(batt_anim_frames),
171 .num_cycles = 3,
172};
173
174static struct charger charger_state = {
175 .batt_anim = &battery_animation,
176};
177
Dima Zavinf48b2362011-08-30 10:46:09 -0700178static int char_width;
179static int char_height;
180
Dima Zavinf48b2362011-08-30 10:46:09 -0700181/* current time in milliseconds */
182static int64_t curr_time_ms(void)
183{
184 struct timespec tm;
185 clock_gettime(CLOCK_MONOTONIC, &tm);
186 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
187}
188
189static void clear_screen(void)
190{
191 gr_color(0, 0, 0, 255);
192 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
193};
194
Dima Zavin823ebc42011-09-29 17:20:07 -0700195#define MAX_KLOG_WRITE_BUF_SZ 256
196
197static void dump_last_kmsg(void)
198{
199 char *buf;
200 char *ptr;
201 unsigned sz = 0;
202 int len;
203
204 LOGI("\n");
205 LOGI("*************** LAST KMSG ***************\n");
206 LOGI("\n");
207 buf = load_file(LAST_KMSG_PATH, &sz);
208 if (!buf || !sz) {
209 LOGI("last_kmsg not found. Cold reset?\n");
210 goto out;
211 }
212
213 len = min(sz, LAST_KMSG_MAX_SZ);
214 ptr = buf + (sz - len);
215
216 while (len > 0) {
217 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
218 char yoink;
219 char *nl;
220
221 nl = memrchr(ptr, '\n', cnt - 1);
222 if (nl)
223 cnt = nl - ptr + 1;
224
225 yoink = ptr[cnt];
226 ptr[cnt] = '\0';
Dima Zavind11e1a02011-10-12 16:13:56 -0700227 klog_write(6, "<6>%s", ptr);
Dima Zavin823ebc42011-09-29 17:20:07 -0700228 ptr[cnt] = yoink;
229
230 len -= cnt;
231 ptr += cnt;
232 }
233
234 free(buf);
235
236out:
237 LOGI("\n");
238 LOGI("************* END LAST KMSG *************\n");
239 LOGI("\n");
240}
241
Dima Zavinf48b2362011-08-30 10:46:09 -0700242static int read_file(const char *path, char *buf, size_t sz)
243{
244 int fd;
245 size_t cnt;
246
247 fd = open(path, O_RDONLY, 0);
248 if (fd < 0)
249 goto err;
250
251 cnt = read(fd, buf, sz - 1);
252 if (cnt <= 0)
253 goto err;
254 buf[cnt] = '\0';
255 if (buf[cnt - 1] == '\n') {
256 cnt--;
257 buf[cnt] = '\0';
258 }
259
260 close(fd);
261 return cnt;
262
263err:
264 if (fd >= 0)
265 close(fd);
266 return -1;
267}
268
269static int read_file_int(const char *path, int *val)
270{
271 char buf[32];
272 int ret;
273 int tmp;
274 char *end;
275
276 ret = read_file(path, buf, sizeof(buf));
277 if (ret < 0)
278 return -1;
279
280 tmp = strtol(buf, &end, 0);
281 if (end == buf ||
282 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
283 goto err;
284
285 *val = tmp;
286 return 0;
287
288err:
289 return -1;
290}
291
Dima Zavin1a5ca612011-09-26 14:14:19 -0700292static int get_battery_capacity(struct charger *charger)
293{
294 int ret;
295 int batt_cap = -1;
296
297 if (!charger->battery)
298 return -1;
299
300 ret = read_file_int(charger->battery->cap_path, &batt_cap);
301 if (ret < 0 || batt_cap > 100) {
302 batt_cap = -1;
303 }
304
305 return batt_cap;
306}
307
Dima Zavinf48b2362011-08-30 10:46:09 -0700308static struct power_supply *find_supply(struct charger *charger,
309 const char *name)
310{
311 struct listnode *node;
312 struct power_supply *supply;
313
314 list_for_each(node, &charger->supplies) {
315 supply = node_to_item(node, struct power_supply, list);
316 if (!strncmp(name, supply->name, sizeof(supply->name)))
317 return supply;
318 }
319 return NULL;
320}
321
322static struct power_supply *add_supply(struct charger *charger,
323 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700324 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700325{
326 struct power_supply *supply;
327
328 supply = calloc(1, sizeof(struct power_supply));
329 if (!supply)
330 return NULL;
331
332 strlcpy(supply->name, name, sizeof(supply->name));
333 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700334 snprintf(supply->cap_path, sizeof(supply->cap_path),
335 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700336 supply->online = online;
337 list_add_tail(&charger->supplies, &supply->list);
338 charger->num_supplies++;
339 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
340 return supply;
341}
342
343static void remove_supply(struct charger *charger, struct power_supply *supply)
344{
345 if (!supply)
346 return;
347 list_remove(&supply->list);
348 charger->num_supplies--;
349 free(supply);
350}
351
352static void parse_uevent(const char *msg, struct uevent *uevent)
353{
354 uevent->action = "";
355 uevent->path = "";
356 uevent->subsystem = "";
357 uevent->ps_name = "";
358 uevent->ps_online = "";
359 uevent->ps_type = "";
360
361 /* currently ignoring SEQNUM */
362 while (*msg) {
363#ifdef DEBUG_UEVENTS
364 LOGV("uevent str: %s\n", msg);
365#endif
366 if (!strncmp(msg, "ACTION=", 7)) {
367 msg += 7;
368 uevent->action = msg;
369 } else if (!strncmp(msg, "DEVPATH=", 8)) {
370 msg += 8;
371 uevent->path = msg;
372 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
373 msg += 10;
374 uevent->subsystem = msg;
375 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
376 msg += 18;
377 uevent->ps_name = msg;
378 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
379 msg += 20;
380 uevent->ps_online = msg;
381 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
382 msg += 18;
383 uevent->ps_type = msg;
384 }
385
386 /* advance to after the next \0 */
387 while (*msg++)
388 ;
389 }
390
391 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
392 uevent->action, uevent->path, uevent->subsystem,
393 uevent->ps_name, uevent->ps_type, uevent->ps_online);
394}
395
396static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
397{
398 int online;
399 char ps_type[32];
400 struct power_supply *supply = NULL;
401 int i;
402 bool was_online = false;
403 bool battery = false;
404
405 if (uevent->ps_type[0] == '\0') {
406 char *path;
407 int ret;
408
409 if (uevent->path[0] == '\0')
410 return;
411 ret = asprintf(&path, "/sys/%s/type", uevent->path);
412 if (ret <= 0)
413 return;
414 ret = read_file(path, ps_type, sizeof(ps_type));
415 free(path);
416 if (ret < 0)
417 return;
418 } else {
419 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
420 }
421
422 if (!strncmp(ps_type, "Battery", 7))
423 battery = true;
424
425 online = atoi(uevent->ps_online);
426 supply = find_supply(charger, uevent->ps_name);
427 if (supply) {
428 was_online = supply->online;
429 supply->online = online;
430 }
431
432 if (!strcmp(uevent->action, "add")) {
433 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700434 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
435 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700436 if (!supply) {
437 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
438 uevent->ps_type, online);
439 return;
440 }
441 /* only pick up the first battery for now */
442 if (battery && !charger->battery)
443 charger->battery = supply;
444 } else {
445 LOGE("supply '%s' already exists..\n", uevent->ps_name);
446 }
447 } else if (!strcmp(uevent->action, "remove")) {
448 if (supply) {
449 if (charger->battery == supply)
450 charger->battery = NULL;
451 remove_supply(charger, supply);
452 supply = NULL;
453 }
454 } else if (!strcmp(uevent->action, "change")) {
455 if (!supply) {
456 LOGE("power supply '%s' not found ('%s' %d)\n",
457 uevent->ps_name, ps_type, online);
458 return;
459 }
460 } else {
461 return;
462 }
463
464 /* allow battery to be managed in the supply list but make it not
465 * contribute to online power supplies. */
466 if (!battery) {
467 if (was_online && !online)
468 charger->num_supplies_online--;
469 else if (supply && !was_online && online)
470 charger->num_supplies_online++;
471 }
472
473 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
474 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
475 uevent->action, charger->num_supplies_online, charger->num_supplies);
476}
477
478static void process_uevent(struct charger *charger, struct uevent *uevent)
479{
480 if (!strcmp(uevent->subsystem, "power_supply"))
481 process_ps_uevent(charger, uevent);
482}
483
484#define UEVENT_MSG_LEN 1024
485static int handle_uevent_fd(struct charger *charger, int fd)
486{
487 char msg[UEVENT_MSG_LEN+2];
488 int n;
489
490 if (fd < 0)
491 return -1;
492
493 while (true) {
494 struct uevent uevent;
495
496 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
497 if (n <= 0)
498 break;
499 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
500 continue;
501
502 msg[n] = '\0';
503 msg[n+1] = '\0';
504
505 parse_uevent(msg, &uevent);
506 process_uevent(charger, &uevent);
507 }
508
509 return 0;
510}
511
512static int uevent_callback(int fd, short revents, void *data)
513{
514 struct charger *charger = data;
515
516 if (!(revents & POLLIN))
517 return -1;
518 return handle_uevent_fd(charger, fd);
519}
520
521/* force the kernel to regenerate the change events for the existing
522 * devices, if valid */
523static void do_coldboot(struct charger *charger, DIR *d, const char *event,
524 bool follow_links, int max_depth)
525{
526 struct dirent *de;
527 int dfd, fd;
528
529 dfd = dirfd(d);
530
531 fd = openat(dfd, "uevent", O_WRONLY);
532 if (fd >= 0) {
533 write(fd, event, strlen(event));
534 close(fd);
535 handle_uevent_fd(charger, charger->uevent_fd);
536 }
537
538 while ((de = readdir(d)) && max_depth > 0) {
539 DIR *d2;
540
541 LOGV("looking at '%s'\n", de->d_name);
542
543 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
544 de->d_name[0] == '.') {
545 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
546 de->d_name, de->d_type, max_depth, follow_links);
547 continue;
548 }
549 LOGV("can descend into '%s'\n", de->d_name);
550
551 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
552 if (fd < 0) {
553 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
554 errno, strerror(errno));
555 continue;
556 }
557
558 d2 = fdopendir(fd);
559 if (d2 == 0)
560 close(fd);
561 else {
562 LOGV("opened '%s'\n", de->d_name);
563 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
564 closedir(d2);
565 }
566 }
567}
568
569static void coldboot(struct charger *charger, const char *path,
570 const char *event)
571{
572 char str[256];
573
574 LOGV("doing coldboot '%s' in '%s'\n", event, path);
575 DIR *d = opendir(path);
576 if (d) {
577 snprintf(str, sizeof(str), "%s\n", event);
578 do_coldboot(charger, d, str, true, 1);
579 closedir(d);
580 }
581}
582
583static int draw_text(const char *str, int x, int y)
584{
585 int str_len_px = gr_measure(str);
586
587 if (x < 0)
588 x = (gr_fb_width() - str_len_px) / 2;
589 if (y < 0)
590 y = (gr_fb_height() - char_height) / 2;
591 gr_text(x, y, str);
592
593 return y + char_height;
594}
595
596static void android_green(void)
597{
598 gr_color(0xa4, 0xc6, 0x39, 255);
599}
600
Dima Zavin0052abd2011-09-22 15:49:04 -0700601/* returns the last y-offset of where the surface ends */
602static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700603{
Dima Zavin0052abd2011-09-22 15:49:04 -0700604 int w;
605 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700606 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700607 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700608
Dima Zavin0052abd2011-09-22 15:49:04 -0700609 w = gr_get_width(surface);
610 h = gr_get_height(surface);
611 x = (gr_fb_width() - w) / 2 ;
612 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700613
Dima Zavin0052abd2011-09-22 15:49:04 -0700614 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
615 gr_blit(surface, 0, 0, w, h, x, y);
616 return y + h;
617}
Dima Zavinf48b2362011-08-30 10:46:09 -0700618
Dima Zavin0052abd2011-09-22 15:49:04 -0700619static void draw_unknown(struct charger *charger)
620{
621 int y;
622 if (charger->surf_unknown) {
623 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700624 } else {
625 android_green();
626 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700627 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700628 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700629}
Dima Zavinf48b2362011-08-30 10:46:09 -0700630
Dima Zavin0052abd2011-09-22 15:49:04 -0700631static void draw_battery(struct charger *charger)
632{
633 struct animation *batt_anim = charger->batt_anim;
634 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
635
636 if (batt_anim->num_frames != 0) {
637 draw_surface_centered(charger, frame->surface);
638 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
639 batt_anim->cur_frame, frame->name, frame->min_capacity,
640 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700641 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700642}
Dima Zavinf48b2362011-08-30 10:46:09 -0700643
Dima Zavin0052abd2011-09-22 15:49:04 -0700644static void redraw_screen(struct charger *charger)
645{
646 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700647
Dima Zavin0052abd2011-09-22 15:49:04 -0700648 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700649
Dima Zavin0052abd2011-09-22 15:49:04 -0700650 /* try to display *something* */
651 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
652 draw_unknown(charger);
653 else
654 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700655 gr_flip();
656}
657
Dima Zavin0052abd2011-09-22 15:49:04 -0700658static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700659{
Dima Zavin0052abd2011-09-22 15:49:04 -0700660 anim->run = true;
661}
662
663static void reset_animation(struct animation *anim)
664{
665 anim->cur_cycle = 0;
666 anim->cur_frame = 0;
667 anim->run = false;
668}
669
670static void update_screen_state(struct charger *charger, int64_t now)
671{
672 struct animation *batt_anim = charger->batt_anim;
673 int cur_frame;
674 int disp_time;
675
676 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700677 return;
678
Dima Zavin0052abd2011-09-22 15:49:04 -0700679 /* animation is over, blank screen and leave */
680 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
681 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700682 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700683 gr_fb_blank(true);
684 LOGV("[%lld] animation done\n", now);
685 return;
686 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700687
Dima Zavin0052abd2011-09-22 15:49:04 -0700688 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
689
690 /* animation starting, set up the animation */
691 if (batt_anim->cur_frame == 0) {
692 int batt_cap;
693 int ret;
694
695 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700696 batt_cap = get_battery_capacity(charger);
697 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700698 int i;
699
700 /* find first frame given current capacity */
701 for (i = 1; i < batt_anim->num_frames; i++) {
702 if (batt_cap < batt_anim->frames[i].min_capacity)
703 break;
704 }
705 batt_anim->cur_frame = i - 1;
706
707 /* show the first frame for twice as long */
708 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
709 }
710
711 batt_anim->capacity = batt_cap;
712 }
713
714 /* unblank the screen on first cycle */
715 if (batt_anim->cur_cycle == 0)
716 gr_fb_blank(false);
717
718 /* draw the new frame (@ cur_frame) */
719 redraw_screen(charger);
720
721 /* if we don't have anim frames, we only have one image, so just bump
722 * the cycle counter and exit
723 */
724 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
725 LOGV("[%lld] animation missing or unknown battery status\n", now);
726 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
727 batt_anim->cur_cycle++;
728 return;
729 }
730
731 /* schedule next screen transition */
732 charger->next_screen_transition = now + disp_time;
733
734 /* advance frame cntr to the next valid frame
735 * if necessary, advance cycle cntr, and reset frame cntr
736 */
737 batt_anim->cur_frame++;
738 if (batt_anim->cur_frame == batt_anim->num_frames) {
739 batt_anim->cur_cycle++;
740 batt_anim->cur_frame = 0;
741
742 /* don't reset the cycle counter, since we use that as a signal
743 * in a test above to check if animation is over
744 */
745 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700746}
747
Dima Zavin2471a6a2011-10-12 16:16:05 -0700748static int set_key_callback(int code, int value, void *data)
Dima Zavinf48b2362011-08-30 10:46:09 -0700749{
Dima Zavin2471a6a2011-10-12 16:16:05 -0700750 struct charger *charger = data;
751 int64_t now = curr_time_ms();
752 int down = !!value;
Dima Zavinf48b2362011-08-30 10:46:09 -0700753
Dima Zavin2471a6a2011-10-12 16:16:05 -0700754 if (code > KEY_MAX)
755 return -1;
Dima Zavinf48b2362011-08-30 10:46:09 -0700756
757 /* only record the down even timestamp, as the amount
758 * of time the key spent not being pressed is not useful */
759 if (down)
Dima Zavin2471a6a2011-10-12 16:16:05 -0700760 charger->keys[code].timestamp = now;
761 charger->keys[code].down = down;
762 charger->keys[code].pending = true;
Dima Zavinf48b2362011-08-30 10:46:09 -0700763 if (down) {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700764 LOGV("[%lld] key[%d] down\n", now, code);
Dima Zavinf48b2362011-08-30 10:46:09 -0700765 } else {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700766 int64_t duration = now - charger->keys[code].timestamp;
Dima Zavinf48b2362011-08-30 10:46:09 -0700767 int64_t secs = duration / 1000;
768 int64_t msecs = duration - secs * 1000;
769 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
Dima Zavin2471a6a2011-10-12 16:16:05 -0700770 code, secs, msecs);
Dima Zavinf48b2362011-08-30 10:46:09 -0700771 }
Dima Zavin2471a6a2011-10-12 16:16:05 -0700772
773 return 0;
774}
775
776static void update_input_state(struct charger *charger,
777 struct input_event *ev)
778{
779 if (ev->type != EV_KEY)
780 return;
781 set_key_callback(ev->code, ev->value, charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700782}
783
784static void set_next_key_check(struct charger *charger,
785 struct key_state *key,
786 int64_t timeout)
787{
788 int64_t then = key->timestamp + timeout;
789
790 if (charger->next_key_check == -1 || then < charger->next_key_check)
791 charger->next_key_check = then;
792}
793
794static void process_key(struct charger *charger, int code, int64_t now)
795{
796 struct key_state *key = &charger->keys[code];
797 int64_t next_key_check;
798
799 if (code == KEY_POWER) {
800 if (key->down) {
801 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
802 if (now >= reboot_timeout) {
803 LOGI("[%lld] rebooting\n", now);
804 android_reboot(ANDROID_RB_RESTART, 0, 0);
805 } else {
806 /* if the key is pressed but timeout hasn't expired,
807 * make sure we wake up at the right-ish time to check
808 */
809 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
810 }
811 } else {
812 /* if the power key got released, force screen state cycle */
813 if (key->pending)
Dima Zavin0052abd2011-09-22 15:49:04 -0700814 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700815 }
816 }
817
818 key->pending = false;
819}
820
821static void handle_input_state(struct charger *charger, int64_t now)
822{
823 process_key(charger, KEY_POWER, now);
824
825 if (charger->next_key_check != -1 && now > charger->next_key_check)
826 charger->next_key_check = -1;
827}
828
829static void handle_power_supply_state(struct charger *charger, int64_t now)
830{
831 if (charger->num_supplies_online == 0) {
832 if (charger->next_pwr_check == -1) {
833 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
834 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
835 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
836 } else if (now >= charger->next_pwr_check) {
837 LOGI("[%lld] shutting down\n", now);
838 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
839 } else {
840 /* otherwise we already have a shutdown timer scheduled */
841 }
842 } else {
843 /* online supply present, reset shutdown timer if set */
844 if (charger->next_pwr_check != -1) {
845 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700846 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700847 }
848 charger->next_pwr_check = -1;
849 }
850}
851
852static void wait_next_event(struct charger *charger, int64_t now)
853{
854 int64_t next_event = INT64_MAX;
855 int64_t timeout;
856 struct input_event ev;
857 int ret;
858
859 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
860 charger->next_screen_transition, charger->next_key_check,
861 charger->next_pwr_check);
862
Dima Zavinf48b2362011-08-30 10:46:09 -0700863 if (charger->next_screen_transition != -1)
864 next_event = charger->next_screen_transition;
865 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
866 next_event = charger->next_key_check;
867 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
868 next_event = charger->next_pwr_check;
869
870 if (next_event != -1 && next_event != INT64_MAX)
871 timeout = max(0, next_event - now);
872 else
873 timeout = -1;
874 LOGV("[%lld] blocking (%lld)\n", now, timeout);
875 ret = ev_wait((int)timeout);
876 if (!ret)
877 ev_dispatch();
878}
879
880static int input_callback(int fd, short revents, void *data)
881{
882 struct charger *charger = data;
883 struct input_event ev;
884 int ret;
885
886 ret = ev_get_input(fd, revents, &ev);
887 if (ret)
888 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700889 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700890 return 0;
891}
892
893static void event_loop(struct charger *charger)
894{
895 int ret;
896
897 while (true) {
898 int64_t now = curr_time_ms();
899
900 LOGV("[%lld] event_loop()\n", now);
901 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700902 handle_power_supply_state(charger, now);
903
Dima Zavin0052abd2011-09-22 15:49:04 -0700904 /* do screen update last in case any of the above want to start
905 * screen transitions (animations, etc)
906 */
907 update_screen_state(charger, now);
908
Dima Zavinf48b2362011-08-30 10:46:09 -0700909 wait_next_event(charger, now);
910 }
911}
912
913int main(int argc, char **argv)
914{
915 int ret;
916 struct charger *charger = &charger_state;
917 int64_t now = curr_time_ms() - 1;
918 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700919 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700920
921 list_init(&charger->supplies);
922
923 klog_init();
924 klog_set_level(CHARGER_KLOG_LEVEL);
925
Dima Zavin823ebc42011-09-29 17:20:07 -0700926 dump_last_kmsg();
927
928 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
929
Dima Zavinf48b2362011-08-30 10:46:09 -0700930 gr_init();
931 gr_font_size(&char_width, &char_height);
932
933 ev_init(input_callback, charger);
934
935 fd = uevent_open_socket(64*1024, true);
936 if (fd >= 0) {
937 fcntl(fd, F_SETFL, O_NONBLOCK);
938 ev_add_fd(fd, uevent_callback, charger);
939 }
940 charger->uevent_fd = fd;
941 coldboot(charger, "/sys/class/power_supply", "add");
942
Dima Zavin0052abd2011-09-22 15:49:04 -0700943 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700944 if (ret < 0) {
945 LOGE("Cannot load image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700946 charger->surf_unknown = NULL;
947 }
948
949 for (i = 0; i < charger->batt_anim->num_frames; i++) {
950 struct frame *frame = &charger->batt_anim->frames[i];
951
952 ret = res_create_surface(frame->name, &frame->surface);
953 if (ret < 0) {
954 LOGE("Cannot load image %s\n", frame->name);
955 /* TODO: free the already allocated surfaces... */
956 charger->batt_anim->num_frames = 0;
957 charger->batt_anim->num_cycles = 1;
958 break;
959 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700960 }
961
Dima Zavin2471a6a2011-10-12 16:16:05 -0700962 ev_sync_key_state(set_key_callback, charger);
963
Dima Zavinf48b2362011-08-30 10:46:09 -0700964 gr_fb_blank(true);
965
966 charger->next_screen_transition = now - 1;
967 charger->next_key_check = -1;
968 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700969 reset_animation(charger->batt_anim);
970 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700971
972 event_loop(charger);
973
974 return 0;
975}