blob: 8f9169d4b6d5e4cb5dc9cd830241931b7ee55006 [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
Iliyan Malchev40156b82012-12-06 17:06:36 -080044#ifdef CHARGER_ENABLE_SUSPEND
choongryeol.lee92557132012-11-15 17:03:03 -080045#include <suspend/autosuspend.h>
Iliyan Malchev40156b82012-12-06 17:06:36 -080046#endif
choongryeol.lee92557132012-11-15 17:03:03 -080047
Dima Zavinf48b2362011-08-30 10:46:09 -070048#include "minui/minui.h"
49
Colin Crossca0e5042014-02-13 12:25:21 -080050char *locale;
51
Dima Zavinf48b2362011-08-30 10:46:09 -070052#ifndef max
53#define max(a,b) ((a) > (b) ? (a) : (b))
54#endif
55
56#ifndef min
57#define min(a,b) ((a) < (b) ? (a) : (b))
58#endif
59
Dima Zavin0052abd2011-09-22 15:49:04 -070060#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
61
Dima Zavinf48b2362011-08-30 10:46:09 -070062#define MSEC_PER_SEC (1000LL)
63#define NSEC_PER_MSEC (1000000LL)
64
Dima Zavin0052abd2011-09-22 15:49:04 -070065#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070066#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070067#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
68
Dima Zavin1a5ca612011-09-26 14:14:19 -070069#define BATTERY_FULL_THRESH 95
70
Dima Zavin823ebc42011-09-29 17:20:07 -070071#define LAST_KMSG_PATH "/proc/last_kmsg"
72#define LAST_KMSG_MAX_SZ (32 * 1024)
73
Dima Zavinf48b2362011-08-30 10:46:09 -070074#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
75#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
76#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
77
78struct key_state {
79 bool pending;
80 bool down;
81 int64_t timestamp;
82};
83
84struct power_supply {
85 struct listnode list;
86 char name[256];
87 char type[32];
88 bool online;
89 bool valid;
Dima Zavin0052abd2011-09-22 15:49:04 -070090 char cap_path[PATH_MAX];
91};
92
93struct frame {
94 const char *name;
95 int disp_time;
96 int min_capacity;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -070097 bool level_only;
Dima Zavin0052abd2011-09-22 15:49:04 -070098
99 gr_surface surface;
100};
101
102struct 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 Zavinf48b2362011-08-30 10:46:09 -0700114};
115
116struct charger {
117 int64_t next_screen_transition;
118 int64_t next_key_check;
119 int64_t next_pwr_check;
Dima Zavinf48b2362011-08-30 10:46:09 -0700120
121 struct key_state keys[KEY_MAX + 1];
Dima Zavinf48b2362011-08-30 10:46:09 -0700122 int uevent_fd;
123
124 struct listnode supplies;
125 int num_supplies;
126 int num_supplies_online;
127
Dima Zavin0052abd2011-09-22 15:49:04 -0700128 struct animation *batt_anim;
129 gr_surface surf_unknown;
130
Dima Zavinf48b2362011-08-30 10:46:09 -0700131 struct power_supply *battery;
132};
133
134struct 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 Zavin0052abd2011-09-22 15:49:04 -0700143static struct frame batt_anim_frames[] = {
144 {
145 .name = "charger/battery_0",
146 .disp_time = 750,
147 .min_capacity = 0,
148 },
149 {
150 .name = "charger/battery_1",
151 .disp_time = 750,
152 .min_capacity = 20,
153 },
154 {
155 .name = "charger/battery_2",
156 .disp_time = 750,
157 .min_capacity = 40,
158 },
159 {
160 .name = "charger/battery_3",
161 .disp_time = 750,
162 .min_capacity = 60,
163 },
164 {
165 .name = "charger/battery_4",
166 .disp_time = 750,
167 .min_capacity = 80,
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700168 .level_only = true,
Dima Zavin0052abd2011-09-22 15:49:04 -0700169 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700170 {
171 .name = "charger/battery_5",
172 .disp_time = 750,
173 .min_capacity = BATTERY_FULL_THRESH,
174 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700175};
176
177static struct animation battery_animation = {
178 .frames = batt_anim_frames,
179 .num_frames = ARRAY_SIZE(batt_anim_frames),
180 .num_cycles = 3,
181};
182
183static struct charger charger_state = {
184 .batt_anim = &battery_animation,
185};
186
Dima Zavinf48b2362011-08-30 10:46:09 -0700187static int char_width;
188static int char_height;
189
Dima Zavinf48b2362011-08-30 10:46:09 -0700190/* current time in milliseconds */
191static int64_t curr_time_ms(void)
192{
193 struct timespec tm;
194 clock_gettime(CLOCK_MONOTONIC, &tm);
195 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
196}
197
198static void clear_screen(void)
199{
200 gr_color(0, 0, 0, 255);
201 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
202};
203
Dima Zavin823ebc42011-09-29 17:20:07 -0700204#define MAX_KLOG_WRITE_BUF_SZ 256
205
206static void dump_last_kmsg(void)
207{
208 char *buf;
209 char *ptr;
210 unsigned sz = 0;
211 int len;
212
213 LOGI("\n");
214 LOGI("*************** LAST KMSG ***************\n");
215 LOGI("\n");
216 buf = load_file(LAST_KMSG_PATH, &sz);
217 if (!buf || !sz) {
218 LOGI("last_kmsg not found. Cold reset?\n");
219 goto out;
220 }
221
222 len = min(sz, LAST_KMSG_MAX_SZ);
223 ptr = buf + (sz - len);
224
225 while (len > 0) {
226 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
227 char yoink;
228 char *nl;
229
230 nl = memrchr(ptr, '\n', cnt - 1);
231 if (nl)
232 cnt = nl - ptr + 1;
233
234 yoink = ptr[cnt];
235 ptr[cnt] = '\0';
Dima Zavind11e1a02011-10-12 16:13:56 -0700236 klog_write(6, "<6>%s", ptr);
Dima Zavin823ebc42011-09-29 17:20:07 -0700237 ptr[cnt] = yoink;
238
239 len -= cnt;
240 ptr += cnt;
241 }
242
243 free(buf);
244
245out:
246 LOGI("\n");
247 LOGI("************* END LAST KMSG *************\n");
248 LOGI("\n");
249}
250
Dima Zavinf48b2362011-08-30 10:46:09 -0700251static int read_file(const char *path, char *buf, size_t sz)
252{
253 int fd;
254 size_t cnt;
255
256 fd = open(path, O_RDONLY, 0);
257 if (fd < 0)
258 goto err;
259
260 cnt = read(fd, buf, sz - 1);
261 if (cnt <= 0)
262 goto err;
263 buf[cnt] = '\0';
264 if (buf[cnt - 1] == '\n') {
265 cnt--;
266 buf[cnt] = '\0';
267 }
268
269 close(fd);
270 return cnt;
271
272err:
273 if (fd >= 0)
274 close(fd);
275 return -1;
276}
277
278static int read_file_int(const char *path, int *val)
279{
280 char buf[32];
281 int ret;
282 int tmp;
283 char *end;
284
285 ret = read_file(path, buf, sizeof(buf));
286 if (ret < 0)
287 return -1;
288
289 tmp = strtol(buf, &end, 0);
290 if (end == buf ||
291 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
292 goto err;
293
294 *val = tmp;
295 return 0;
296
297err:
298 return -1;
299}
300
Dima Zavin1a5ca612011-09-26 14:14:19 -0700301static int get_battery_capacity(struct charger *charger)
302{
303 int ret;
304 int batt_cap = -1;
305
306 if (!charger->battery)
307 return -1;
308
309 ret = read_file_int(charger->battery->cap_path, &batt_cap);
310 if (ret < 0 || batt_cap > 100) {
311 batt_cap = -1;
312 }
313
314 return batt_cap;
315}
316
Dima Zavinf48b2362011-08-30 10:46:09 -0700317static struct power_supply *find_supply(struct charger *charger,
318 const char *name)
319{
320 struct listnode *node;
321 struct power_supply *supply;
322
323 list_for_each(node, &charger->supplies) {
324 supply = node_to_item(node, struct power_supply, list);
325 if (!strncmp(name, supply->name, sizeof(supply->name)))
326 return supply;
327 }
328 return NULL;
329}
330
331static struct power_supply *add_supply(struct charger *charger,
332 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700333 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700334{
335 struct power_supply *supply;
336
337 supply = calloc(1, sizeof(struct power_supply));
338 if (!supply)
339 return NULL;
340
341 strlcpy(supply->name, name, sizeof(supply->name));
342 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700343 snprintf(supply->cap_path, sizeof(supply->cap_path),
344 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700345 supply->online = online;
346 list_add_tail(&charger->supplies, &supply->list);
347 charger->num_supplies++;
348 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
349 return supply;
350}
351
352static void remove_supply(struct charger *charger, struct power_supply *supply)
353{
354 if (!supply)
355 return;
356 list_remove(&supply->list);
357 charger->num_supplies--;
358 free(supply);
359}
360
choongryeol.lee92557132012-11-15 17:03:03 -0800361#ifdef CHARGER_ENABLE_SUSPEND
362static int request_suspend(bool enable)
363{
364 if (enable)
365 return autosuspend_enable();
366 else
367 return autosuspend_disable();
368}
369#else
370static int request_suspend(bool enable)
371{
372 return 0;
373}
374#endif
375
Dima Zavinf48b2362011-08-30 10:46:09 -0700376static void parse_uevent(const char *msg, struct uevent *uevent)
377{
378 uevent->action = "";
379 uevent->path = "";
380 uevent->subsystem = "";
381 uevent->ps_name = "";
382 uevent->ps_online = "";
383 uevent->ps_type = "";
384
385 /* currently ignoring SEQNUM */
386 while (*msg) {
387#ifdef DEBUG_UEVENTS
388 LOGV("uevent str: %s\n", msg);
389#endif
390 if (!strncmp(msg, "ACTION=", 7)) {
391 msg += 7;
392 uevent->action = msg;
393 } else if (!strncmp(msg, "DEVPATH=", 8)) {
394 msg += 8;
395 uevent->path = msg;
396 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
397 msg += 10;
398 uevent->subsystem = msg;
399 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
400 msg += 18;
401 uevent->ps_name = msg;
402 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
403 msg += 20;
404 uevent->ps_online = msg;
405 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
406 msg += 18;
407 uevent->ps_type = msg;
408 }
409
410 /* advance to after the next \0 */
411 while (*msg++)
412 ;
413 }
414
415 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
416 uevent->action, uevent->path, uevent->subsystem,
417 uevent->ps_name, uevent->ps_type, uevent->ps_online);
418}
419
420static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
421{
422 int online;
423 char ps_type[32];
424 struct power_supply *supply = NULL;
425 int i;
426 bool was_online = false;
427 bool battery = false;
428
429 if (uevent->ps_type[0] == '\0') {
430 char *path;
431 int ret;
432
433 if (uevent->path[0] == '\0')
434 return;
435 ret = asprintf(&path, "/sys/%s/type", uevent->path);
436 if (ret <= 0)
437 return;
438 ret = read_file(path, ps_type, sizeof(ps_type));
439 free(path);
440 if (ret < 0)
441 return;
442 } else {
443 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
444 }
445
446 if (!strncmp(ps_type, "Battery", 7))
447 battery = true;
448
449 online = atoi(uevent->ps_online);
450 supply = find_supply(charger, uevent->ps_name);
451 if (supply) {
452 was_online = supply->online;
453 supply->online = online;
454 }
455
456 if (!strcmp(uevent->action, "add")) {
457 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700458 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
459 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700460 if (!supply) {
461 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
462 uevent->ps_type, online);
463 return;
464 }
465 /* only pick up the first battery for now */
466 if (battery && !charger->battery)
467 charger->battery = supply;
468 } else {
469 LOGE("supply '%s' already exists..\n", uevent->ps_name);
470 }
471 } else if (!strcmp(uevent->action, "remove")) {
472 if (supply) {
473 if (charger->battery == supply)
474 charger->battery = NULL;
475 remove_supply(charger, supply);
476 supply = NULL;
477 }
478 } else if (!strcmp(uevent->action, "change")) {
479 if (!supply) {
480 LOGE("power supply '%s' not found ('%s' %d)\n",
481 uevent->ps_name, ps_type, online);
482 return;
483 }
484 } else {
485 return;
486 }
487
488 /* allow battery to be managed in the supply list but make it not
489 * contribute to online power supplies. */
490 if (!battery) {
491 if (was_online && !online)
492 charger->num_supplies_online--;
493 else if (supply && !was_online && online)
494 charger->num_supplies_online++;
495 }
496
497 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
498 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
499 uevent->action, charger->num_supplies_online, charger->num_supplies);
500}
501
502static void process_uevent(struct charger *charger, struct uevent *uevent)
503{
504 if (!strcmp(uevent->subsystem, "power_supply"))
505 process_ps_uevent(charger, uevent);
506}
507
508#define UEVENT_MSG_LEN 1024
509static int handle_uevent_fd(struct charger *charger, int fd)
510{
511 char msg[UEVENT_MSG_LEN+2];
512 int n;
513
514 if (fd < 0)
515 return -1;
516
517 while (true) {
518 struct uevent uevent;
519
520 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
521 if (n <= 0)
522 break;
523 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
524 continue;
525
526 msg[n] = '\0';
527 msg[n+1] = '\0';
528
529 parse_uevent(msg, &uevent);
530 process_uevent(charger, &uevent);
531 }
532
533 return 0;
534}
535
536static int uevent_callback(int fd, short revents, void *data)
537{
538 struct charger *charger = data;
539
540 if (!(revents & POLLIN))
541 return -1;
542 return handle_uevent_fd(charger, fd);
543}
544
545/* force the kernel to regenerate the change events for the existing
546 * devices, if valid */
547static void do_coldboot(struct charger *charger, DIR *d, const char *event,
548 bool follow_links, int max_depth)
549{
550 struct dirent *de;
551 int dfd, fd;
552
553 dfd = dirfd(d);
554
555 fd = openat(dfd, "uevent", O_WRONLY);
556 if (fd >= 0) {
557 write(fd, event, strlen(event));
558 close(fd);
559 handle_uevent_fd(charger, charger->uevent_fd);
560 }
561
562 while ((de = readdir(d)) && max_depth > 0) {
563 DIR *d2;
564
565 LOGV("looking at '%s'\n", de->d_name);
566
567 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
568 de->d_name[0] == '.') {
569 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
570 de->d_name, de->d_type, max_depth, follow_links);
571 continue;
572 }
573 LOGV("can descend into '%s'\n", de->d_name);
574
575 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
576 if (fd < 0) {
577 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
578 errno, strerror(errno));
579 continue;
580 }
581
582 d2 = fdopendir(fd);
583 if (d2 == 0)
584 close(fd);
585 else {
586 LOGV("opened '%s'\n", de->d_name);
587 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
588 closedir(d2);
589 }
590 }
591}
592
593static void coldboot(struct charger *charger, const char *path,
594 const char *event)
595{
596 char str[256];
597
598 LOGV("doing coldboot '%s' in '%s'\n", event, path);
599 DIR *d = opendir(path);
600 if (d) {
601 snprintf(str, sizeof(str), "%s\n", event);
602 do_coldboot(charger, d, str, true, 1);
603 closedir(d);
604 }
605}
606
607static int draw_text(const char *str, int x, int y)
608{
609 int str_len_px = gr_measure(str);
610
611 if (x < 0)
612 x = (gr_fb_width() - str_len_px) / 2;
613 if (y < 0)
614 y = (gr_fb_height() - char_height) / 2;
Doug Zongker12c45fb2013-03-07 13:35:23 -0800615 gr_text(x, y, str, 0);
Dima Zavinf48b2362011-08-30 10:46:09 -0700616
617 return y + char_height;
618}
619
620static void android_green(void)
621{
622 gr_color(0xa4, 0xc6, 0x39, 255);
623}
624
Dima Zavin0052abd2011-09-22 15:49:04 -0700625/* returns the last y-offset of where the surface ends */
626static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700627{
Dima Zavin0052abd2011-09-22 15:49:04 -0700628 int w;
629 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700630 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700631 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700632
Dima Zavin0052abd2011-09-22 15:49:04 -0700633 w = gr_get_width(surface);
634 h = gr_get_height(surface);
635 x = (gr_fb_width() - w) / 2 ;
636 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700637
Dima Zavin0052abd2011-09-22 15:49:04 -0700638 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
639 gr_blit(surface, 0, 0, w, h, x, y);
640 return y + h;
641}
Dima Zavinf48b2362011-08-30 10:46:09 -0700642
Dima Zavin0052abd2011-09-22 15:49:04 -0700643static void draw_unknown(struct charger *charger)
644{
645 int y;
646 if (charger->surf_unknown) {
647 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700648 } else {
649 android_green();
650 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700651 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700652 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700653}
Dima Zavinf48b2362011-08-30 10:46:09 -0700654
Dima Zavin0052abd2011-09-22 15:49:04 -0700655static void draw_battery(struct charger *charger)
656{
657 struct animation *batt_anim = charger->batt_anim;
658 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
659
660 if (batt_anim->num_frames != 0) {
661 draw_surface_centered(charger, frame->surface);
662 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
663 batt_anim->cur_frame, frame->name, frame->min_capacity,
664 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700665 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700666}
Dima Zavinf48b2362011-08-30 10:46:09 -0700667
Dima Zavin0052abd2011-09-22 15:49:04 -0700668static void redraw_screen(struct charger *charger)
669{
670 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700671
Dima Zavin0052abd2011-09-22 15:49:04 -0700672 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700673
Dima Zavin0052abd2011-09-22 15:49:04 -0700674 /* try to display *something* */
675 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
676 draw_unknown(charger);
677 else
678 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700679 gr_flip();
680}
681
Dima Zavin0052abd2011-09-22 15:49:04 -0700682static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700683{
Dima Zavin0052abd2011-09-22 15:49:04 -0700684 anim->run = true;
685}
686
687static void reset_animation(struct animation *anim)
688{
689 anim->cur_cycle = 0;
690 anim->cur_frame = 0;
691 anim->run = false;
692}
693
694static void update_screen_state(struct charger *charger, int64_t now)
695{
696 struct animation *batt_anim = charger->batt_anim;
697 int cur_frame;
698 int disp_time;
699
700 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700701 return;
702
Dima Zavin0052abd2011-09-22 15:49:04 -0700703 /* animation is over, blank screen and leave */
704 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
705 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700706 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700707 gr_fb_blank(true);
708 LOGV("[%lld] animation done\n", now);
Devin Kimfd8e6502012-12-07 09:25:06 -0800709 if (charger->num_supplies_online > 0)
710 request_suspend(true);
Dima Zavin0052abd2011-09-22 15:49:04 -0700711 return;
712 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700713
Dima Zavin0052abd2011-09-22 15:49:04 -0700714 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
715
716 /* animation starting, set up the animation */
717 if (batt_anim->cur_frame == 0) {
718 int batt_cap;
719 int ret;
720
721 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700722 batt_cap = get_battery_capacity(charger);
723 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700724 int i;
725
726 /* find first frame given current capacity */
727 for (i = 1; i < batt_anim->num_frames; i++) {
728 if (batt_cap < batt_anim->frames[i].min_capacity)
729 break;
730 }
731 batt_anim->cur_frame = i - 1;
732
733 /* show the first frame for twice as long */
734 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
735 }
736
737 batt_anim->capacity = batt_cap;
738 }
739
740 /* unblank the screen on first cycle */
741 if (batt_anim->cur_cycle == 0)
742 gr_fb_blank(false);
743
744 /* draw the new frame (@ cur_frame) */
745 redraw_screen(charger);
746
747 /* if we don't have anim frames, we only have one image, so just bump
748 * the cycle counter and exit
749 */
750 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
751 LOGV("[%lld] animation missing or unknown battery status\n", now);
752 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
753 batt_anim->cur_cycle++;
754 return;
755 }
756
757 /* schedule next screen transition */
758 charger->next_screen_transition = now + disp_time;
759
760 /* advance frame cntr to the next valid frame
761 * if necessary, advance cycle cntr, and reset frame cntr
762 */
763 batt_anim->cur_frame++;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700764
765 /* if the frame is used for level-only, that is only show it when it's
766 * the current level, skip it during the animation.
767 */
768 while (batt_anim->cur_frame < batt_anim->num_frames &&
769 batt_anim->frames[batt_anim->cur_frame].level_only)
770 batt_anim->cur_frame++;
771 if (batt_anim->cur_frame >= batt_anim->num_frames) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700772 batt_anim->cur_cycle++;
773 batt_anim->cur_frame = 0;
774
775 /* don't reset the cycle counter, since we use that as a signal
776 * in a test above to check if animation is over
777 */
778 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700779}
780
Dima Zavin2471a6a2011-10-12 16:16:05 -0700781static int set_key_callback(int code, int value, void *data)
Dima Zavinf48b2362011-08-30 10:46:09 -0700782{
Dima Zavin2471a6a2011-10-12 16:16:05 -0700783 struct charger *charger = data;
784 int64_t now = curr_time_ms();
785 int down = !!value;
Dima Zavinf48b2362011-08-30 10:46:09 -0700786
Dima Zavin2471a6a2011-10-12 16:16:05 -0700787 if (code > KEY_MAX)
788 return -1;
Dima Zavinf48b2362011-08-30 10:46:09 -0700789
Dima Zavin2d978c02011-10-12 16:18:23 -0700790 /* ignore events that don't modify our state */
791 if (charger->keys[code].down == down)
Dima Zavin471157a2011-10-13 13:04:20 -0700792 return 0;
Dima Zavin2d978c02011-10-12 16:18:23 -0700793
Dima Zavinf48b2362011-08-30 10:46:09 -0700794 /* only record the down even timestamp, as the amount
795 * of time the key spent not being pressed is not useful */
796 if (down)
Dima Zavin2471a6a2011-10-12 16:16:05 -0700797 charger->keys[code].timestamp = now;
798 charger->keys[code].down = down;
799 charger->keys[code].pending = true;
Dima Zavinf48b2362011-08-30 10:46:09 -0700800 if (down) {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700801 LOGV("[%lld] key[%d] down\n", now, code);
Dima Zavinf48b2362011-08-30 10:46:09 -0700802 } else {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700803 int64_t duration = now - charger->keys[code].timestamp;
Dima Zavinf48b2362011-08-30 10:46:09 -0700804 int64_t secs = duration / 1000;
805 int64_t msecs = duration - secs * 1000;
806 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
Dima Zavin2471a6a2011-10-12 16:16:05 -0700807 code, secs, msecs);
Dima Zavinf48b2362011-08-30 10:46:09 -0700808 }
Dima Zavin2471a6a2011-10-12 16:16:05 -0700809
810 return 0;
811}
812
813static void update_input_state(struct charger *charger,
814 struct input_event *ev)
815{
816 if (ev->type != EV_KEY)
817 return;
818 set_key_callback(ev->code, ev->value, charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700819}
820
821static void set_next_key_check(struct charger *charger,
822 struct key_state *key,
823 int64_t timeout)
824{
825 int64_t then = key->timestamp + timeout;
826
827 if (charger->next_key_check == -1 || then < charger->next_key_check)
828 charger->next_key_check = then;
829}
830
831static void process_key(struct charger *charger, int code, int64_t now)
832{
833 struct key_state *key = &charger->keys[code];
834 int64_t next_key_check;
835
836 if (code == KEY_POWER) {
837 if (key->down) {
838 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
839 if (now >= reboot_timeout) {
840 LOGI("[%lld] rebooting\n", now);
841 android_reboot(ANDROID_RB_RESTART, 0, 0);
842 } else {
843 /* if the key is pressed but timeout hasn't expired,
844 * make sure we wake up at the right-ish time to check
845 */
846 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
847 }
848 } else {
849 /* if the power key got released, force screen state cycle */
choongryeol.lee92557132012-11-15 17:03:03 -0800850 if (key->pending) {
851 request_suspend(false);
Dima Zavin0052abd2011-09-22 15:49:04 -0700852 kick_animation(charger->batt_anim);
choongryeol.lee92557132012-11-15 17:03:03 -0800853 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700854 }
855 }
856
857 key->pending = false;
858}
859
860static void handle_input_state(struct charger *charger, int64_t now)
861{
862 process_key(charger, KEY_POWER, now);
863
864 if (charger->next_key_check != -1 && now > charger->next_key_check)
865 charger->next_key_check = -1;
866}
867
868static void handle_power_supply_state(struct charger *charger, int64_t now)
869{
870 if (charger->num_supplies_online == 0) {
choongryeol.lee92557132012-11-15 17:03:03 -0800871 request_suspend(false);
Dima Zavinf48b2362011-08-30 10:46:09 -0700872 if (charger->next_pwr_check == -1) {
873 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
874 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
875 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
876 } else if (now >= charger->next_pwr_check) {
877 LOGI("[%lld] shutting down\n", now);
878 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
879 } else {
880 /* otherwise we already have a shutdown timer scheduled */
881 }
882 } else {
883 /* online supply present, reset shutdown timer if set */
884 if (charger->next_pwr_check != -1) {
885 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700886 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700887 }
888 charger->next_pwr_check = -1;
889 }
890}
891
892static void wait_next_event(struct charger *charger, int64_t now)
893{
894 int64_t next_event = INT64_MAX;
895 int64_t timeout;
896 struct input_event ev;
897 int ret;
898
899 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
900 charger->next_screen_transition, charger->next_key_check,
901 charger->next_pwr_check);
902
Dima Zavinf48b2362011-08-30 10:46:09 -0700903 if (charger->next_screen_transition != -1)
904 next_event = charger->next_screen_transition;
905 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
906 next_event = charger->next_key_check;
907 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
908 next_event = charger->next_pwr_check;
909
910 if (next_event != -1 && next_event != INT64_MAX)
911 timeout = max(0, next_event - now);
912 else
913 timeout = -1;
914 LOGV("[%lld] blocking (%lld)\n", now, timeout);
915 ret = ev_wait((int)timeout);
916 if (!ret)
917 ev_dispatch();
918}
919
920static int input_callback(int fd, short revents, void *data)
921{
922 struct charger *charger = data;
923 struct input_event ev;
924 int ret;
925
926 ret = ev_get_input(fd, revents, &ev);
927 if (ret)
928 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700929 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700930 return 0;
931}
932
933static void event_loop(struct charger *charger)
934{
935 int ret;
936
937 while (true) {
938 int64_t now = curr_time_ms();
939
940 LOGV("[%lld] event_loop()\n", now);
941 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700942 handle_power_supply_state(charger, now);
943
Dima Zavin0052abd2011-09-22 15:49:04 -0700944 /* do screen update last in case any of the above want to start
945 * screen transitions (animations, etc)
946 */
947 update_screen_state(charger, now);
948
Dima Zavinf48b2362011-08-30 10:46:09 -0700949 wait_next_event(charger, now);
950 }
951}
952
953int main(int argc, char **argv)
954{
955 int ret;
956 struct charger *charger = &charger_state;
957 int64_t now = curr_time_ms() - 1;
958 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700959 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700960
961 list_init(&charger->supplies);
962
963 klog_init();
964 klog_set_level(CHARGER_KLOG_LEVEL);
965
Dima Zavin823ebc42011-09-29 17:20:07 -0700966 dump_last_kmsg();
967
968 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
969
Dima Zavinf48b2362011-08-30 10:46:09 -0700970 gr_init();
971 gr_font_size(&char_width, &char_height);
972
973 ev_init(input_callback, charger);
974
975 fd = uevent_open_socket(64*1024, true);
976 if (fd >= 0) {
977 fcntl(fd, F_SETFL, O_NONBLOCK);
978 ev_add_fd(fd, uevent_callback, charger);
979 }
980 charger->uevent_fd = fd;
981 coldboot(charger, "/sys/class/power_supply", "add");
982
Dima Zavin0052abd2011-09-22 15:49:04 -0700983 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700984 if (ret < 0) {
985 LOGE("Cannot load image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700986 charger->surf_unknown = NULL;
987 }
988
989 for (i = 0; i < charger->batt_anim->num_frames; i++) {
990 struct frame *frame = &charger->batt_anim->frames[i];
991
992 ret = res_create_surface(frame->name, &frame->surface);
993 if (ret < 0) {
994 LOGE("Cannot load image %s\n", frame->name);
995 /* TODO: free the already allocated surfaces... */
996 charger->batt_anim->num_frames = 0;
997 charger->batt_anim->num_cycles = 1;
998 break;
999 }
Dima Zavinf48b2362011-08-30 10:46:09 -07001000 }
1001
Dima Zavin2471a6a2011-10-12 16:16:05 -07001002 ev_sync_key_state(set_key_callback, charger);
1003
Dima Zavin209c7b02012-10-12 15:13:52 -07001004#ifndef CHARGER_DISABLE_INIT_BLANK
Dima Zavinf48b2362011-08-30 10:46:09 -07001005 gr_fb_blank(true);
Dima Zavin209c7b02012-10-12 15:13:52 -07001006#endif
Dima Zavinf48b2362011-08-30 10:46:09 -07001007
1008 charger->next_screen_transition = now - 1;
1009 charger->next_key_check = -1;
1010 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -07001011 reset_animation(charger->batt_anim);
1012 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -07001013
1014 event_loop(charger);
1015
1016 return 0;
1017}