blob: 1bacc81f82943088daa797baabec432628589a14 [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>
Dima Zavinf48b2362011-08-30 10:46:09 -070024#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/poll.h>
Dima Zavinf48b2362011-08-30 10:46:09 -070029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/un.h>
32#include <time.h>
33#include <unistd.h>
34
Elliott Hughesb77d3d72012-09-11 18:52:40 -070035#include <sys/socket.h>
36#include <linux/netlink.h>
37
Dima Zavinf48b2362011-08-30 10:46:09 -070038#include <cutils/android_reboot.h>
39#include <cutils/klog.h>
40#include <cutils/list.h>
Dima Zavin823ebc42011-09-29 17:20:07 -070041#include <cutils/misc.h>
Dima Zavinf48b2362011-08-30 10:46:09 -070042#include <cutils/uevent.h>
43
choongryeol.lee92557132012-11-15 17:03:03 -080044#include <suspend/autosuspend.h>
45
Dima Zavinf48b2362011-08-30 10:46:09 -070046#include "minui/minui.h"
47
48#ifndef max
49#define max(a,b) ((a) > (b) ? (a) : (b))
50#endif
51
52#ifndef min
53#define min(a,b) ((a) < (b) ? (a) : (b))
54#endif
55
Dima Zavin0052abd2011-09-22 15:49:04 -070056#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
57
Dima Zavinf48b2362011-08-30 10:46:09 -070058#define MSEC_PER_SEC (1000LL)
59#define NSEC_PER_MSEC (1000000LL)
60
Dima Zavin0052abd2011-09-22 15:49:04 -070061#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070062#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070063#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
64
Dima Zavin1a5ca612011-09-26 14:14:19 -070065#define BATTERY_FULL_THRESH 95
66
Dima Zavin823ebc42011-09-29 17:20:07 -070067#define LAST_KMSG_PATH "/proc/last_kmsg"
68#define LAST_KMSG_MAX_SZ (32 * 1024)
69
Dima Zavinf48b2362011-08-30 10:46:09 -070070#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
71#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
72#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
73
74struct key_state {
75 bool pending;
76 bool down;
77 int64_t timestamp;
78};
79
80struct power_supply {
81 struct listnode list;
82 char name[256];
83 char type[32];
84 bool online;
85 bool valid;
Dima Zavin0052abd2011-09-22 15:49:04 -070086 char cap_path[PATH_MAX];
87};
88
89struct frame {
90 const char *name;
91 int disp_time;
92 int min_capacity;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -070093 bool level_only;
Dima Zavin0052abd2011-09-22 15:49:04 -070094
95 gr_surface surface;
96};
97
98struct animation {
99 bool run;
100
101 struct frame *frames;
102 int cur_frame;
103 int num_frames;
104
105 int cur_cycle;
106 int num_cycles;
107
108 /* current capacity being animated */
109 int capacity;
Dima Zavinf48b2362011-08-30 10:46:09 -0700110};
111
112struct charger {
113 int64_t next_screen_transition;
114 int64_t next_key_check;
115 int64_t next_pwr_check;
Dima Zavinf48b2362011-08-30 10:46:09 -0700116
117 struct key_state keys[KEY_MAX + 1];
Dima Zavinf48b2362011-08-30 10:46:09 -0700118 int uevent_fd;
119
120 struct listnode supplies;
121 int num_supplies;
122 int num_supplies_online;
123
Dima Zavin0052abd2011-09-22 15:49:04 -0700124 struct animation *batt_anim;
125 gr_surface surf_unknown;
126
Dima Zavinf48b2362011-08-30 10:46:09 -0700127 struct power_supply *battery;
128};
129
130struct uevent {
131 const char *action;
132 const char *path;
133 const char *subsystem;
134 const char *ps_name;
135 const char *ps_type;
136 const char *ps_online;
137};
138
Dima Zavin0052abd2011-09-22 15:49:04 -0700139static struct frame batt_anim_frames[] = {
140 {
141 .name = "charger/battery_0",
142 .disp_time = 750,
143 .min_capacity = 0,
144 },
145 {
146 .name = "charger/battery_1",
147 .disp_time = 750,
148 .min_capacity = 20,
149 },
150 {
151 .name = "charger/battery_2",
152 .disp_time = 750,
153 .min_capacity = 40,
154 },
155 {
156 .name = "charger/battery_3",
157 .disp_time = 750,
158 .min_capacity = 60,
159 },
160 {
161 .name = "charger/battery_4",
162 .disp_time = 750,
163 .min_capacity = 80,
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700164 .level_only = true,
Dima Zavin0052abd2011-09-22 15:49:04 -0700165 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700166 {
167 .name = "charger/battery_5",
168 .disp_time = 750,
169 .min_capacity = BATTERY_FULL_THRESH,
170 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700171};
172
173static struct animation battery_animation = {
174 .frames = batt_anim_frames,
175 .num_frames = ARRAY_SIZE(batt_anim_frames),
176 .num_cycles = 3,
177};
178
179static struct charger charger_state = {
180 .batt_anim = &battery_animation,
181};
182
Dima Zavinf48b2362011-08-30 10:46:09 -0700183static int char_width;
184static int char_height;
185
Dima Zavinf48b2362011-08-30 10:46:09 -0700186/* current time in milliseconds */
187static int64_t curr_time_ms(void)
188{
189 struct timespec tm;
190 clock_gettime(CLOCK_MONOTONIC, &tm);
191 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
192}
193
194static void clear_screen(void)
195{
196 gr_color(0, 0, 0, 255);
197 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
198};
199
Dima Zavin823ebc42011-09-29 17:20:07 -0700200#define MAX_KLOG_WRITE_BUF_SZ 256
201
202static void dump_last_kmsg(void)
203{
204 char *buf;
205 char *ptr;
206 unsigned sz = 0;
207 int len;
208
209 LOGI("\n");
210 LOGI("*************** LAST KMSG ***************\n");
211 LOGI("\n");
212 buf = load_file(LAST_KMSG_PATH, &sz);
213 if (!buf || !sz) {
214 LOGI("last_kmsg not found. Cold reset?\n");
215 goto out;
216 }
217
218 len = min(sz, LAST_KMSG_MAX_SZ);
219 ptr = buf + (sz - len);
220
221 while (len > 0) {
222 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
223 char yoink;
224 char *nl;
225
226 nl = memrchr(ptr, '\n', cnt - 1);
227 if (nl)
228 cnt = nl - ptr + 1;
229
230 yoink = ptr[cnt];
231 ptr[cnt] = '\0';
Dima Zavind11e1a02011-10-12 16:13:56 -0700232 klog_write(6, "<6>%s", ptr);
Dima Zavin823ebc42011-09-29 17:20:07 -0700233 ptr[cnt] = yoink;
234
235 len -= cnt;
236 ptr += cnt;
237 }
238
239 free(buf);
240
241out:
242 LOGI("\n");
243 LOGI("************* END LAST KMSG *************\n");
244 LOGI("\n");
245}
246
Dima Zavinf48b2362011-08-30 10:46:09 -0700247static int read_file(const char *path, char *buf, size_t sz)
248{
249 int fd;
250 size_t cnt;
251
252 fd = open(path, O_RDONLY, 0);
253 if (fd < 0)
254 goto err;
255
256 cnt = read(fd, buf, sz - 1);
257 if (cnt <= 0)
258 goto err;
259 buf[cnt] = '\0';
260 if (buf[cnt - 1] == '\n') {
261 cnt--;
262 buf[cnt] = '\0';
263 }
264
265 close(fd);
266 return cnt;
267
268err:
269 if (fd >= 0)
270 close(fd);
271 return -1;
272}
273
274static int read_file_int(const char *path, int *val)
275{
276 char buf[32];
277 int ret;
278 int tmp;
279 char *end;
280
281 ret = read_file(path, buf, sizeof(buf));
282 if (ret < 0)
283 return -1;
284
285 tmp = strtol(buf, &end, 0);
286 if (end == buf ||
287 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
288 goto err;
289
290 *val = tmp;
291 return 0;
292
293err:
294 return -1;
295}
296
Dima Zavin1a5ca612011-09-26 14:14:19 -0700297static int get_battery_capacity(struct charger *charger)
298{
299 int ret;
300 int batt_cap = -1;
301
302 if (!charger->battery)
303 return -1;
304
305 ret = read_file_int(charger->battery->cap_path, &batt_cap);
306 if (ret < 0 || batt_cap > 100) {
307 batt_cap = -1;
308 }
309
310 return batt_cap;
311}
312
Dima Zavinf48b2362011-08-30 10:46:09 -0700313static struct power_supply *find_supply(struct charger *charger,
314 const char *name)
315{
316 struct listnode *node;
317 struct power_supply *supply;
318
319 list_for_each(node, &charger->supplies) {
320 supply = node_to_item(node, struct power_supply, list);
321 if (!strncmp(name, supply->name, sizeof(supply->name)))
322 return supply;
323 }
324 return NULL;
325}
326
327static struct power_supply *add_supply(struct charger *charger,
328 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700329 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700330{
331 struct power_supply *supply;
332
333 supply = calloc(1, sizeof(struct power_supply));
334 if (!supply)
335 return NULL;
336
337 strlcpy(supply->name, name, sizeof(supply->name));
338 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700339 snprintf(supply->cap_path, sizeof(supply->cap_path),
340 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700341 supply->online = online;
342 list_add_tail(&charger->supplies, &supply->list);
343 charger->num_supplies++;
344 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
345 return supply;
346}
347
348static void remove_supply(struct charger *charger, struct power_supply *supply)
349{
350 if (!supply)
351 return;
352 list_remove(&supply->list);
353 charger->num_supplies--;
354 free(supply);
355}
356
choongryeol.lee92557132012-11-15 17:03:03 -0800357#ifdef CHARGER_ENABLE_SUSPEND
358static int request_suspend(bool enable)
359{
360 if (enable)
361 return autosuspend_enable();
362 else
363 return autosuspend_disable();
364}
365#else
366static int request_suspend(bool enable)
367{
368 return 0;
369}
370#endif
371
Dima Zavinf48b2362011-08-30 10:46:09 -0700372static void parse_uevent(const char *msg, struct uevent *uevent)
373{
374 uevent->action = "";
375 uevent->path = "";
376 uevent->subsystem = "";
377 uevent->ps_name = "";
378 uevent->ps_online = "";
379 uevent->ps_type = "";
380
381 /* currently ignoring SEQNUM */
382 while (*msg) {
383#ifdef DEBUG_UEVENTS
384 LOGV("uevent str: %s\n", msg);
385#endif
386 if (!strncmp(msg, "ACTION=", 7)) {
387 msg += 7;
388 uevent->action = msg;
389 } else if (!strncmp(msg, "DEVPATH=", 8)) {
390 msg += 8;
391 uevent->path = msg;
392 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
393 msg += 10;
394 uevent->subsystem = msg;
395 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
396 msg += 18;
397 uevent->ps_name = msg;
398 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
399 msg += 20;
400 uevent->ps_online = msg;
401 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
402 msg += 18;
403 uevent->ps_type = msg;
404 }
405
406 /* advance to after the next \0 */
407 while (*msg++)
408 ;
409 }
410
411 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
412 uevent->action, uevent->path, uevent->subsystem,
413 uevent->ps_name, uevent->ps_type, uevent->ps_online);
414}
415
416static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
417{
418 int online;
419 char ps_type[32];
420 struct power_supply *supply = NULL;
421 int i;
422 bool was_online = false;
423 bool battery = false;
424
425 if (uevent->ps_type[0] == '\0') {
426 char *path;
427 int ret;
428
429 if (uevent->path[0] == '\0')
430 return;
431 ret = asprintf(&path, "/sys/%s/type", uevent->path);
432 if (ret <= 0)
433 return;
434 ret = read_file(path, ps_type, sizeof(ps_type));
435 free(path);
436 if (ret < 0)
437 return;
438 } else {
439 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
440 }
441
442 if (!strncmp(ps_type, "Battery", 7))
443 battery = true;
444
445 online = atoi(uevent->ps_online);
446 supply = find_supply(charger, uevent->ps_name);
447 if (supply) {
448 was_online = supply->online;
449 supply->online = online;
450 }
451
452 if (!strcmp(uevent->action, "add")) {
453 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700454 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
455 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700456 if (!supply) {
457 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
458 uevent->ps_type, online);
459 return;
460 }
461 /* only pick up the first battery for now */
462 if (battery && !charger->battery)
463 charger->battery = supply;
464 } else {
465 LOGE("supply '%s' already exists..\n", uevent->ps_name);
466 }
467 } else if (!strcmp(uevent->action, "remove")) {
468 if (supply) {
469 if (charger->battery == supply)
470 charger->battery = NULL;
471 remove_supply(charger, supply);
472 supply = NULL;
473 }
474 } else if (!strcmp(uevent->action, "change")) {
475 if (!supply) {
476 LOGE("power supply '%s' not found ('%s' %d)\n",
477 uevent->ps_name, ps_type, online);
478 return;
479 }
480 } else {
481 return;
482 }
483
484 /* allow battery to be managed in the supply list but make it not
485 * contribute to online power supplies. */
486 if (!battery) {
487 if (was_online && !online)
488 charger->num_supplies_online--;
489 else if (supply && !was_online && online)
490 charger->num_supplies_online++;
491 }
492
493 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
494 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
495 uevent->action, charger->num_supplies_online, charger->num_supplies);
496}
497
498static void process_uevent(struct charger *charger, struct uevent *uevent)
499{
500 if (!strcmp(uevent->subsystem, "power_supply"))
501 process_ps_uevent(charger, uevent);
502}
503
504#define UEVENT_MSG_LEN 1024
505static int handle_uevent_fd(struct charger *charger, int fd)
506{
507 char msg[UEVENT_MSG_LEN+2];
508 int n;
509
510 if (fd < 0)
511 return -1;
512
513 while (true) {
514 struct uevent uevent;
515
516 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
517 if (n <= 0)
518 break;
519 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
520 continue;
521
522 msg[n] = '\0';
523 msg[n+1] = '\0';
524
525 parse_uevent(msg, &uevent);
526 process_uevent(charger, &uevent);
527 }
528
529 return 0;
530}
531
532static int uevent_callback(int fd, short revents, void *data)
533{
534 struct charger *charger = data;
535
536 if (!(revents & POLLIN))
537 return -1;
538 return handle_uevent_fd(charger, fd);
539}
540
541/* force the kernel to regenerate the change events for the existing
542 * devices, if valid */
543static void do_coldboot(struct charger *charger, DIR *d, const char *event,
544 bool follow_links, int max_depth)
545{
546 struct dirent *de;
547 int dfd, fd;
548
549 dfd = dirfd(d);
550
551 fd = openat(dfd, "uevent", O_WRONLY);
552 if (fd >= 0) {
553 write(fd, event, strlen(event));
554 close(fd);
555 handle_uevent_fd(charger, charger->uevent_fd);
556 }
557
558 while ((de = readdir(d)) && max_depth > 0) {
559 DIR *d2;
560
561 LOGV("looking at '%s'\n", de->d_name);
562
563 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
564 de->d_name[0] == '.') {
565 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
566 de->d_name, de->d_type, max_depth, follow_links);
567 continue;
568 }
569 LOGV("can descend into '%s'\n", de->d_name);
570
571 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
572 if (fd < 0) {
573 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
574 errno, strerror(errno));
575 continue;
576 }
577
578 d2 = fdopendir(fd);
579 if (d2 == 0)
580 close(fd);
581 else {
582 LOGV("opened '%s'\n", de->d_name);
583 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
584 closedir(d2);
585 }
586 }
587}
588
589static void coldboot(struct charger *charger, const char *path,
590 const char *event)
591{
592 char str[256];
593
594 LOGV("doing coldboot '%s' in '%s'\n", event, path);
595 DIR *d = opendir(path);
596 if (d) {
597 snprintf(str, sizeof(str), "%s\n", event);
598 do_coldboot(charger, d, str, true, 1);
599 closedir(d);
600 }
601}
602
603static int draw_text(const char *str, int x, int y)
604{
605 int str_len_px = gr_measure(str);
606
607 if (x < 0)
608 x = (gr_fb_width() - str_len_px) / 2;
609 if (y < 0)
610 y = (gr_fb_height() - char_height) / 2;
611 gr_text(x, y, str);
612
613 return y + char_height;
614}
615
616static void android_green(void)
617{
618 gr_color(0xa4, 0xc6, 0x39, 255);
619}
620
Dima Zavin0052abd2011-09-22 15:49:04 -0700621/* returns the last y-offset of where the surface ends */
622static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700623{
Dima Zavin0052abd2011-09-22 15:49:04 -0700624 int w;
625 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700626 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700627 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700628
Dima Zavin0052abd2011-09-22 15:49:04 -0700629 w = gr_get_width(surface);
630 h = gr_get_height(surface);
631 x = (gr_fb_width() - w) / 2 ;
632 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700633
Dima Zavin0052abd2011-09-22 15:49:04 -0700634 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
635 gr_blit(surface, 0, 0, w, h, x, y);
636 return y + h;
637}
Dima Zavinf48b2362011-08-30 10:46:09 -0700638
Dima Zavin0052abd2011-09-22 15:49:04 -0700639static void draw_unknown(struct charger *charger)
640{
641 int y;
642 if (charger->surf_unknown) {
643 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700644 } else {
645 android_green();
646 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700647 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700648 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700649}
Dima Zavinf48b2362011-08-30 10:46:09 -0700650
Dima Zavin0052abd2011-09-22 15:49:04 -0700651static void draw_battery(struct charger *charger)
652{
653 struct animation *batt_anim = charger->batt_anim;
654 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
655
656 if (batt_anim->num_frames != 0) {
657 draw_surface_centered(charger, frame->surface);
658 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
659 batt_anim->cur_frame, frame->name, frame->min_capacity,
660 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700661 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700662}
Dima Zavinf48b2362011-08-30 10:46:09 -0700663
Dima Zavin0052abd2011-09-22 15:49:04 -0700664static void redraw_screen(struct charger *charger)
665{
666 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700667
Dima Zavin0052abd2011-09-22 15:49:04 -0700668 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700669
Dima Zavin0052abd2011-09-22 15:49:04 -0700670 /* try to display *something* */
671 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
672 draw_unknown(charger);
673 else
674 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700675 gr_flip();
676}
677
Dima Zavin0052abd2011-09-22 15:49:04 -0700678static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700679{
Dima Zavin0052abd2011-09-22 15:49:04 -0700680 anim->run = true;
681}
682
683static void reset_animation(struct animation *anim)
684{
685 anim->cur_cycle = 0;
686 anim->cur_frame = 0;
687 anim->run = false;
688}
689
690static void update_screen_state(struct charger *charger, int64_t now)
691{
692 struct animation *batt_anim = charger->batt_anim;
693 int cur_frame;
694 int disp_time;
695
696 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700697 return;
698
Dima Zavin0052abd2011-09-22 15:49:04 -0700699 /* animation is over, blank screen and leave */
700 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
701 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700702 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700703 gr_fb_blank(true);
704 LOGV("[%lld] animation done\n", now);
choongryeol.lee92557132012-11-15 17:03:03 -0800705 request_suspend(true);
Dima Zavin0052abd2011-09-22 15:49:04 -0700706 return;
707 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700708
Dima Zavin0052abd2011-09-22 15:49:04 -0700709 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
710
711 /* animation starting, set up the animation */
712 if (batt_anim->cur_frame == 0) {
713 int batt_cap;
714 int ret;
715
716 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700717 batt_cap = get_battery_capacity(charger);
718 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700719 int i;
720
721 /* find first frame given current capacity */
722 for (i = 1; i < batt_anim->num_frames; i++) {
723 if (batt_cap < batt_anim->frames[i].min_capacity)
724 break;
725 }
726 batt_anim->cur_frame = i - 1;
727
728 /* show the first frame for twice as long */
729 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
730 }
731
732 batt_anim->capacity = batt_cap;
733 }
734
735 /* unblank the screen on first cycle */
736 if (batt_anim->cur_cycle == 0)
737 gr_fb_blank(false);
738
739 /* draw the new frame (@ cur_frame) */
740 redraw_screen(charger);
741
742 /* if we don't have anim frames, we only have one image, so just bump
743 * the cycle counter and exit
744 */
745 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
746 LOGV("[%lld] animation missing or unknown battery status\n", now);
747 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
748 batt_anim->cur_cycle++;
749 return;
750 }
751
752 /* schedule next screen transition */
753 charger->next_screen_transition = now + disp_time;
754
755 /* advance frame cntr to the next valid frame
756 * if necessary, advance cycle cntr, and reset frame cntr
757 */
758 batt_anim->cur_frame++;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700759
760 /* if the frame is used for level-only, that is only show it when it's
761 * the current level, skip it during the animation.
762 */
763 while (batt_anim->cur_frame < batt_anim->num_frames &&
764 batt_anim->frames[batt_anim->cur_frame].level_only)
765 batt_anim->cur_frame++;
766 if (batt_anim->cur_frame >= batt_anim->num_frames) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700767 batt_anim->cur_cycle++;
768 batt_anim->cur_frame = 0;
769
770 /* don't reset the cycle counter, since we use that as a signal
771 * in a test above to check if animation is over
772 */
773 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700774}
775
Dima Zavin2471a6a2011-10-12 16:16:05 -0700776static int set_key_callback(int code, int value, void *data)
Dima Zavinf48b2362011-08-30 10:46:09 -0700777{
Dima Zavin2471a6a2011-10-12 16:16:05 -0700778 struct charger *charger = data;
779 int64_t now = curr_time_ms();
780 int down = !!value;
Dima Zavinf48b2362011-08-30 10:46:09 -0700781
Dima Zavin2471a6a2011-10-12 16:16:05 -0700782 if (code > KEY_MAX)
783 return -1;
Dima Zavinf48b2362011-08-30 10:46:09 -0700784
Dima Zavin2d978c02011-10-12 16:18:23 -0700785 /* ignore events that don't modify our state */
786 if (charger->keys[code].down == down)
Dima Zavin471157a2011-10-13 13:04:20 -0700787 return 0;
Dima Zavin2d978c02011-10-12 16:18:23 -0700788
Dima Zavinf48b2362011-08-30 10:46:09 -0700789 /* only record the down even timestamp, as the amount
790 * of time the key spent not being pressed is not useful */
791 if (down)
Dima Zavin2471a6a2011-10-12 16:16:05 -0700792 charger->keys[code].timestamp = now;
793 charger->keys[code].down = down;
794 charger->keys[code].pending = true;
Dima Zavinf48b2362011-08-30 10:46:09 -0700795 if (down) {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700796 LOGV("[%lld] key[%d] down\n", now, code);
Dima Zavinf48b2362011-08-30 10:46:09 -0700797 } else {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700798 int64_t duration = now - charger->keys[code].timestamp;
Dima Zavinf48b2362011-08-30 10:46:09 -0700799 int64_t secs = duration / 1000;
800 int64_t msecs = duration - secs * 1000;
801 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
Dima Zavin2471a6a2011-10-12 16:16:05 -0700802 code, secs, msecs);
Dima Zavinf48b2362011-08-30 10:46:09 -0700803 }
Dima Zavin2471a6a2011-10-12 16:16:05 -0700804
805 return 0;
806}
807
808static void update_input_state(struct charger *charger,
809 struct input_event *ev)
810{
811 if (ev->type != EV_KEY)
812 return;
813 set_key_callback(ev->code, ev->value, charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700814}
815
816static void set_next_key_check(struct charger *charger,
817 struct key_state *key,
818 int64_t timeout)
819{
820 int64_t then = key->timestamp + timeout;
821
822 if (charger->next_key_check == -1 || then < charger->next_key_check)
823 charger->next_key_check = then;
824}
825
826static void process_key(struct charger *charger, int code, int64_t now)
827{
828 struct key_state *key = &charger->keys[code];
829 int64_t next_key_check;
830
831 if (code == KEY_POWER) {
832 if (key->down) {
833 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
834 if (now >= reboot_timeout) {
835 LOGI("[%lld] rebooting\n", now);
836 android_reboot(ANDROID_RB_RESTART, 0, 0);
837 } else {
838 /* if the key is pressed but timeout hasn't expired,
839 * make sure we wake up at the right-ish time to check
840 */
841 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
842 }
843 } else {
844 /* if the power key got released, force screen state cycle */
choongryeol.lee92557132012-11-15 17:03:03 -0800845 if (key->pending) {
846 request_suspend(false);
Dima Zavin0052abd2011-09-22 15:49:04 -0700847 kick_animation(charger->batt_anim);
choongryeol.lee92557132012-11-15 17:03:03 -0800848 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700849 }
850 }
851
852 key->pending = false;
853}
854
855static void handle_input_state(struct charger *charger, int64_t now)
856{
857 process_key(charger, KEY_POWER, now);
858
859 if (charger->next_key_check != -1 && now > charger->next_key_check)
860 charger->next_key_check = -1;
861}
862
863static void handle_power_supply_state(struct charger *charger, int64_t now)
864{
865 if (charger->num_supplies_online == 0) {
choongryeol.lee92557132012-11-15 17:03:03 -0800866 request_suspend(false);
Dima Zavinf48b2362011-08-30 10:46:09 -0700867 if (charger->next_pwr_check == -1) {
868 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
869 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
870 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
871 } else if (now >= charger->next_pwr_check) {
872 LOGI("[%lld] shutting down\n", now);
873 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
874 } else {
875 /* otherwise we already have a shutdown timer scheduled */
876 }
877 } else {
878 /* online supply present, reset shutdown timer if set */
879 if (charger->next_pwr_check != -1) {
880 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700881 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700882 }
883 charger->next_pwr_check = -1;
884 }
885}
886
887static void wait_next_event(struct charger *charger, int64_t now)
888{
889 int64_t next_event = INT64_MAX;
890 int64_t timeout;
891 struct input_event ev;
892 int ret;
893
894 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
895 charger->next_screen_transition, charger->next_key_check,
896 charger->next_pwr_check);
897
Dima Zavinf48b2362011-08-30 10:46:09 -0700898 if (charger->next_screen_transition != -1)
899 next_event = charger->next_screen_transition;
900 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
901 next_event = charger->next_key_check;
902 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
903 next_event = charger->next_pwr_check;
904
905 if (next_event != -1 && next_event != INT64_MAX)
906 timeout = max(0, next_event - now);
907 else
908 timeout = -1;
909 LOGV("[%lld] blocking (%lld)\n", now, timeout);
910 ret = ev_wait((int)timeout);
911 if (!ret)
912 ev_dispatch();
913}
914
915static int input_callback(int fd, short revents, void *data)
916{
917 struct charger *charger = data;
918 struct input_event ev;
919 int ret;
920
921 ret = ev_get_input(fd, revents, &ev);
922 if (ret)
923 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700924 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700925 return 0;
926}
927
928static void event_loop(struct charger *charger)
929{
930 int ret;
931
932 while (true) {
933 int64_t now = curr_time_ms();
934
935 LOGV("[%lld] event_loop()\n", now);
936 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700937 handle_power_supply_state(charger, now);
938
Dima Zavin0052abd2011-09-22 15:49:04 -0700939 /* do screen update last in case any of the above want to start
940 * screen transitions (animations, etc)
941 */
942 update_screen_state(charger, now);
943
Dima Zavinf48b2362011-08-30 10:46:09 -0700944 wait_next_event(charger, now);
945 }
946}
947
948int main(int argc, char **argv)
949{
950 int ret;
951 struct charger *charger = &charger_state;
952 int64_t now = curr_time_ms() - 1;
953 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700954 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700955
956 list_init(&charger->supplies);
957
958 klog_init();
959 klog_set_level(CHARGER_KLOG_LEVEL);
960
Dima Zavin823ebc42011-09-29 17:20:07 -0700961 dump_last_kmsg();
962
963 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
964
Dima Zavinf48b2362011-08-30 10:46:09 -0700965 gr_init();
966 gr_font_size(&char_width, &char_height);
967
968 ev_init(input_callback, charger);
969
970 fd = uevent_open_socket(64*1024, true);
971 if (fd >= 0) {
972 fcntl(fd, F_SETFL, O_NONBLOCK);
973 ev_add_fd(fd, uevent_callback, charger);
974 }
975 charger->uevent_fd = fd;
976 coldboot(charger, "/sys/class/power_supply", "add");
977
Dima Zavin0052abd2011-09-22 15:49:04 -0700978 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700979 if (ret < 0) {
980 LOGE("Cannot load image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700981 charger->surf_unknown = NULL;
982 }
983
984 for (i = 0; i < charger->batt_anim->num_frames; i++) {
985 struct frame *frame = &charger->batt_anim->frames[i];
986
987 ret = res_create_surface(frame->name, &frame->surface);
988 if (ret < 0) {
989 LOGE("Cannot load image %s\n", frame->name);
990 /* TODO: free the already allocated surfaces... */
991 charger->batt_anim->num_frames = 0;
992 charger->batt_anim->num_cycles = 1;
993 break;
994 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700995 }
996
Dima Zavin2471a6a2011-10-12 16:16:05 -0700997 ev_sync_key_state(set_key_callback, charger);
998
Dima Zavin209c7b02012-10-12 15:13:52 -0700999#ifndef CHARGER_DISABLE_INIT_BLANK
Dima Zavinf48b2362011-08-30 10:46:09 -07001000 gr_fb_blank(true);
Dima Zavin209c7b02012-10-12 15:13:52 -07001001#endif
Dima Zavinf48b2362011-08-30 10:46:09 -07001002
1003 charger->next_screen_transition = now - 1;
1004 charger->next_key_check = -1;
1005 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -07001006 reset_animation(charger->batt_anim);
1007 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -07001008
1009 event_loop(charger);
1010
1011 return 0;
1012}