blob: 15add87d0ee22e41acc0c6c1b29e7adb09d199b5 [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>
Riley Andrewse4b7b292014-06-16 15:06:21 -070043#include <cutils/properties.h>
Dima Zavinf48b2362011-08-30 10:46:09 -070044
Iliyan Malchev40156b82012-12-06 17:06:36 -080045#ifdef CHARGER_ENABLE_SUSPEND
choongryeol.lee92557132012-11-15 17:03:03 -080046#include <suspend/autosuspend.h>
Iliyan Malchev40156b82012-12-06 17:06:36 -080047#endif
choongryeol.lee92557132012-11-15 17:03:03 -080048
Dima Zavinf48b2362011-08-30 10:46:09 -070049#include "minui/minui.h"
50
Colin Crossca0e5042014-02-13 12:25:21 -080051char *locale;
52
Dima Zavinf48b2362011-08-30 10:46:09 -070053#ifndef max
54#define max(a,b) ((a) > (b) ? (a) : (b))
55#endif
56
57#ifndef min
58#define min(a,b) ((a) < (b) ? (a) : (b))
59#endif
60
Dima Zavin0052abd2011-09-22 15:49:04 -070061#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
62
Dima Zavinf48b2362011-08-30 10:46:09 -070063#define MSEC_PER_SEC (1000LL)
64#define NSEC_PER_MSEC (1000000LL)
65
Dima Zavin0052abd2011-09-22 15:49:04 -070066#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070067#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070068#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
69
Dima Zavin1a5ca612011-09-26 14:14:19 -070070#define BATTERY_FULL_THRESH 95
71
Dima Zavin823ebc42011-09-29 17:20:07 -070072#define LAST_KMSG_PATH "/proc/last_kmsg"
73#define LAST_KMSG_MAX_SZ (32 * 1024)
74
Dima Zavinf48b2362011-08-30 10:46:09 -070075#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
76#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
77#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
78
79struct key_state {
80 bool pending;
81 bool down;
82 int64_t timestamp;
83};
84
85struct power_supply {
86 struct listnode list;
87 char name[256];
88 char type[32];
89 bool online;
90 bool valid;
Dima Zavin0052abd2011-09-22 15:49:04 -070091 char cap_path[PATH_MAX];
92};
93
94struct frame {
Dima Zavin0052abd2011-09-22 15:49:04 -070095 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 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700145 .disp_time = 750,
146 .min_capacity = 0,
147 },
148 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700149 .disp_time = 750,
150 .min_capacity = 20,
151 },
152 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700153 .disp_time = 750,
154 .min_capacity = 40,
155 },
156 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700157 .disp_time = 750,
158 .min_capacity = 60,
159 },
160 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700161 .disp_time = 750,
162 .min_capacity = 80,
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700163 .level_only = true,
Dima Zavin0052abd2011-09-22 15:49:04 -0700164 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700165 {
Dima Zavin1a5ca612011-09-26 14:14:19 -0700166 .disp_time = 750,
167 .min_capacity = BATTERY_FULL_THRESH,
168 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700169};
170
171static struct animation battery_animation = {
172 .frames = batt_anim_frames,
173 .num_frames = ARRAY_SIZE(batt_anim_frames),
174 .num_cycles = 3,
175};
176
177static struct charger charger_state = {
178 .batt_anim = &battery_animation,
179};
180
Dima Zavinf48b2362011-08-30 10:46:09 -0700181static int char_width;
182static int char_height;
183
Dima Zavinf48b2362011-08-30 10:46:09 -0700184/* current time in milliseconds */
185static int64_t curr_time_ms(void)
186{
187 struct timespec tm;
188 clock_gettime(CLOCK_MONOTONIC, &tm);
189 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
190}
191
192static void clear_screen(void)
193{
194 gr_color(0, 0, 0, 255);
Doug Zongker0280f272014-03-11 08:42:09 -0700195 gr_clear();
196}
Dima Zavinf48b2362011-08-30 10:46:09 -0700197
Dima Zavin823ebc42011-09-29 17:20:07 -0700198#define MAX_KLOG_WRITE_BUF_SZ 256
199
200static void dump_last_kmsg(void)
201{
202 char *buf;
203 char *ptr;
204 unsigned sz = 0;
205 int len;
206
207 LOGI("\n");
208 LOGI("*************** LAST KMSG ***************\n");
209 LOGI("\n");
210 buf = load_file(LAST_KMSG_PATH, &sz);
211 if (!buf || !sz) {
212 LOGI("last_kmsg not found. Cold reset?\n");
213 goto out;
214 }
215
216 len = min(sz, LAST_KMSG_MAX_SZ);
217 ptr = buf + (sz - len);
218
219 while (len > 0) {
220 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
221 char yoink;
222 char *nl;
223
224 nl = memrchr(ptr, '\n', cnt - 1);
225 if (nl)
226 cnt = nl - ptr + 1;
227
228 yoink = ptr[cnt];
229 ptr[cnt] = '\0';
Dima Zavind11e1a02011-10-12 16:13:56 -0700230 klog_write(6, "<6>%s", ptr);
Dima Zavin823ebc42011-09-29 17:20:07 -0700231 ptr[cnt] = yoink;
232
233 len -= cnt;
234 ptr += cnt;
235 }
236
237 free(buf);
238
239out:
240 LOGI("\n");
241 LOGI("************* END LAST KMSG *************\n");
242 LOGI("\n");
243}
244
Dima Zavinf48b2362011-08-30 10:46:09 -0700245static int read_file(const char *path, char *buf, size_t sz)
246{
247 int fd;
248 size_t cnt;
249
250 fd = open(path, O_RDONLY, 0);
251 if (fd < 0)
252 goto err;
253
254 cnt = read(fd, buf, sz - 1);
255 if (cnt <= 0)
256 goto err;
257 buf[cnt] = '\0';
258 if (buf[cnt - 1] == '\n') {
259 cnt--;
260 buf[cnt] = '\0';
261 }
262
263 close(fd);
264 return cnt;
265
266err:
267 if (fd >= 0)
268 close(fd);
269 return -1;
270}
271
272static int read_file_int(const char *path, int *val)
273{
274 char buf[32];
275 int ret;
276 int tmp;
277 char *end;
278
279 ret = read_file(path, buf, sizeof(buf));
280 if (ret < 0)
281 return -1;
282
283 tmp = strtol(buf, &end, 0);
284 if (end == buf ||
285 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
286 goto err;
287
288 *val = tmp;
289 return 0;
290
291err:
292 return -1;
293}
294
Dima Zavin1a5ca612011-09-26 14:14:19 -0700295static int get_battery_capacity(struct charger *charger)
296{
297 int ret;
298 int batt_cap = -1;
299
300 if (!charger->battery)
301 return -1;
302
303 ret = read_file_int(charger->battery->cap_path, &batt_cap);
304 if (ret < 0 || batt_cap > 100) {
305 batt_cap = -1;
306 }
307
308 return batt_cap;
309}
310
Dima Zavinf48b2362011-08-30 10:46:09 -0700311static struct power_supply *find_supply(struct charger *charger,
312 const char *name)
313{
314 struct listnode *node;
315 struct power_supply *supply;
316
317 list_for_each(node, &charger->supplies) {
318 supply = node_to_item(node, struct power_supply, list);
319 if (!strncmp(name, supply->name, sizeof(supply->name)))
320 return supply;
321 }
322 return NULL;
323}
324
325static struct power_supply *add_supply(struct charger *charger,
326 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700327 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700328{
329 struct power_supply *supply;
330
331 supply = calloc(1, sizeof(struct power_supply));
332 if (!supply)
333 return NULL;
334
335 strlcpy(supply->name, name, sizeof(supply->name));
336 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700337 snprintf(supply->cap_path, sizeof(supply->cap_path),
338 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700339 supply->online = online;
340 list_add_tail(&charger->supplies, &supply->list);
341 charger->num_supplies++;
342 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
343 return supply;
344}
345
346static void remove_supply(struct charger *charger, struct power_supply *supply)
347{
348 if (!supply)
349 return;
350 list_remove(&supply->list);
351 charger->num_supplies--;
352 free(supply);
353}
354
choongryeol.lee92557132012-11-15 17:03:03 -0800355#ifdef CHARGER_ENABLE_SUSPEND
356static int request_suspend(bool enable)
357{
358 if (enable)
359 return autosuspend_enable();
360 else
361 return autosuspend_disable();
362}
363#else
364static int request_suspend(bool enable)
365{
366 return 0;
367}
368#endif
369
Dima Zavinf48b2362011-08-30 10:46:09 -0700370static void parse_uevent(const char *msg, struct uevent *uevent)
371{
372 uevent->action = "";
373 uevent->path = "";
374 uevent->subsystem = "";
375 uevent->ps_name = "";
376 uevent->ps_online = "";
377 uevent->ps_type = "";
378
379 /* currently ignoring SEQNUM */
380 while (*msg) {
381#ifdef DEBUG_UEVENTS
382 LOGV("uevent str: %s\n", msg);
383#endif
384 if (!strncmp(msg, "ACTION=", 7)) {
385 msg += 7;
386 uevent->action = msg;
387 } else if (!strncmp(msg, "DEVPATH=", 8)) {
388 msg += 8;
389 uevent->path = msg;
390 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
391 msg += 10;
392 uevent->subsystem = msg;
393 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
394 msg += 18;
395 uevent->ps_name = msg;
396 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
397 msg += 20;
398 uevent->ps_online = msg;
399 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
400 msg += 18;
401 uevent->ps_type = msg;
402 }
403
404 /* advance to after the next \0 */
405 while (*msg++)
406 ;
407 }
408
409 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
410 uevent->action, uevent->path, uevent->subsystem,
411 uevent->ps_name, uevent->ps_type, uevent->ps_online);
412}
413
414static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
415{
416 int online;
417 char ps_type[32];
418 struct power_supply *supply = NULL;
419 int i;
420 bool was_online = false;
421 bool battery = false;
422
423 if (uevent->ps_type[0] == '\0') {
424 char *path;
425 int ret;
426
427 if (uevent->path[0] == '\0')
428 return;
429 ret = asprintf(&path, "/sys/%s/type", uevent->path);
430 if (ret <= 0)
431 return;
432 ret = read_file(path, ps_type, sizeof(ps_type));
433 free(path);
434 if (ret < 0)
435 return;
436 } else {
437 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
438 }
439
440 if (!strncmp(ps_type, "Battery", 7))
441 battery = true;
442
443 online = atoi(uevent->ps_online);
444 supply = find_supply(charger, uevent->ps_name);
445 if (supply) {
446 was_online = supply->online;
447 supply->online = online;
448 }
449
450 if (!strcmp(uevent->action, "add")) {
451 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700452 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
453 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700454 if (!supply) {
455 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
456 uevent->ps_type, online);
457 return;
458 }
459 /* only pick up the first battery for now */
460 if (battery && !charger->battery)
461 charger->battery = supply;
462 } else {
463 LOGE("supply '%s' already exists..\n", uevent->ps_name);
464 }
465 } else if (!strcmp(uevent->action, "remove")) {
466 if (supply) {
467 if (charger->battery == supply)
468 charger->battery = NULL;
469 remove_supply(charger, supply);
470 supply = NULL;
471 }
472 } else if (!strcmp(uevent->action, "change")) {
473 if (!supply) {
474 LOGE("power supply '%s' not found ('%s' %d)\n",
475 uevent->ps_name, ps_type, online);
476 return;
477 }
478 } else {
479 return;
480 }
481
482 /* allow battery to be managed in the supply list but make it not
483 * contribute to online power supplies. */
484 if (!battery) {
485 if (was_online && !online)
486 charger->num_supplies_online--;
487 else if (supply && !was_online && online)
488 charger->num_supplies_online++;
489 }
490
491 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
492 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
493 uevent->action, charger->num_supplies_online, charger->num_supplies);
494}
495
496static void process_uevent(struct charger *charger, struct uevent *uevent)
497{
498 if (!strcmp(uevent->subsystem, "power_supply"))
499 process_ps_uevent(charger, uevent);
500}
501
502#define UEVENT_MSG_LEN 1024
503static int handle_uevent_fd(struct charger *charger, int fd)
504{
505 char msg[UEVENT_MSG_LEN+2];
506 int n;
507
508 if (fd < 0)
509 return -1;
510
511 while (true) {
512 struct uevent uevent;
513
514 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
515 if (n <= 0)
516 break;
517 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
518 continue;
519
520 msg[n] = '\0';
521 msg[n+1] = '\0';
522
523 parse_uevent(msg, &uevent);
524 process_uevent(charger, &uevent);
525 }
526
527 return 0;
528}
529
530static int uevent_callback(int fd, short revents, void *data)
531{
532 struct charger *charger = data;
533
534 if (!(revents & POLLIN))
535 return -1;
536 return handle_uevent_fd(charger, fd);
537}
538
539/* force the kernel to regenerate the change events for the existing
540 * devices, if valid */
541static void do_coldboot(struct charger *charger, DIR *d, const char *event,
542 bool follow_links, int max_depth)
543{
544 struct dirent *de;
545 int dfd, fd;
546
547 dfd = dirfd(d);
548
549 fd = openat(dfd, "uevent", O_WRONLY);
550 if (fd >= 0) {
551 write(fd, event, strlen(event));
552 close(fd);
553 handle_uevent_fd(charger, charger->uevent_fd);
554 }
555
556 while ((de = readdir(d)) && max_depth > 0) {
557 DIR *d2;
558
559 LOGV("looking at '%s'\n", de->d_name);
560
561 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
562 de->d_name[0] == '.') {
563 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
564 de->d_name, de->d_type, max_depth, follow_links);
565 continue;
566 }
567 LOGV("can descend into '%s'\n", de->d_name);
568
569 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
570 if (fd < 0) {
571 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
572 errno, strerror(errno));
573 continue;
574 }
575
576 d2 = fdopendir(fd);
577 if (d2 == 0)
578 close(fd);
579 else {
580 LOGV("opened '%s'\n", de->d_name);
581 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
582 closedir(d2);
583 }
584 }
585}
586
587static void coldboot(struct charger *charger, const char *path,
588 const char *event)
589{
590 char str[256];
591
592 LOGV("doing coldboot '%s' in '%s'\n", event, path);
593 DIR *d = opendir(path);
594 if (d) {
595 snprintf(str, sizeof(str), "%s\n", event);
596 do_coldboot(charger, d, str, true, 1);
597 closedir(d);
598 }
599}
600
601static int draw_text(const char *str, int x, int y)
602{
603 int str_len_px = gr_measure(str);
604
605 if (x < 0)
606 x = (gr_fb_width() - str_len_px) / 2;
607 if (y < 0)
608 y = (gr_fb_height() - char_height) / 2;
Doug Zongker12c45fb2013-03-07 13:35:23 -0800609 gr_text(x, y, str, 0);
Dima Zavinf48b2362011-08-30 10:46:09 -0700610
611 return y + char_height;
612}
613
614static void android_green(void)
615{
616 gr_color(0xa4, 0xc6, 0x39, 255);
617}
618
Dima Zavin0052abd2011-09-22 15:49:04 -0700619/* returns the last y-offset of where the surface ends */
620static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700621{
Dima Zavin0052abd2011-09-22 15:49:04 -0700622 int w;
623 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700624 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700625 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700626
Dima Zavin0052abd2011-09-22 15:49:04 -0700627 w = gr_get_width(surface);
628 h = gr_get_height(surface);
629 x = (gr_fb_width() - w) / 2 ;
630 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700631
Dima Zavin0052abd2011-09-22 15:49:04 -0700632 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
633 gr_blit(surface, 0, 0, w, h, x, y);
634 return y + h;
635}
Dima Zavinf48b2362011-08-30 10:46:09 -0700636
Dima Zavin0052abd2011-09-22 15:49:04 -0700637static void draw_unknown(struct charger *charger)
638{
639 int y;
640 if (charger->surf_unknown) {
641 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700642 } else {
643 android_green();
644 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700645 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700646 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700647}
Dima Zavinf48b2362011-08-30 10:46:09 -0700648
Dima Zavin0052abd2011-09-22 15:49:04 -0700649static void draw_battery(struct charger *charger)
650{
651 struct animation *batt_anim = charger->batt_anim;
652 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
653
654 if (batt_anim->num_frames != 0) {
655 draw_surface_centered(charger, frame->surface);
Doug Zongker0280f272014-03-11 08:42:09 -0700656 LOGV("drawing frame #%d min_cap=%d time=%d\n",
657 batt_anim->cur_frame, frame->min_capacity,
Dima Zavin0052abd2011-09-22 15:49:04 -0700658 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700659 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700660}
Dima Zavinf48b2362011-08-30 10:46:09 -0700661
Dima Zavin0052abd2011-09-22 15:49:04 -0700662static void redraw_screen(struct charger *charger)
663{
664 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700665
Dima Zavin0052abd2011-09-22 15:49:04 -0700666 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700667
Dima Zavin0052abd2011-09-22 15:49:04 -0700668 /* try to display *something* */
669 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
670 draw_unknown(charger);
671 else
672 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700673 gr_flip();
674}
675
Dima Zavin0052abd2011-09-22 15:49:04 -0700676static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700677{
Dima Zavin0052abd2011-09-22 15:49:04 -0700678 anim->run = true;
679}
680
681static void reset_animation(struct animation *anim)
682{
683 anim->cur_cycle = 0;
684 anim->cur_frame = 0;
685 anim->run = false;
686}
687
688static void update_screen_state(struct charger *charger, int64_t now)
689{
690 struct animation *batt_anim = charger->batt_anim;
691 int cur_frame;
692 int disp_time;
693
694 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700695 return;
696
Dima Zavin0052abd2011-09-22 15:49:04 -0700697 /* animation is over, blank screen and leave */
698 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
699 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700700 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700701 gr_fb_blank(true);
702 LOGV("[%lld] animation done\n", now);
Devin Kimfd8e6502012-12-07 09:25:06 -0800703 if (charger->num_supplies_online > 0)
704 request_suspend(true);
Dima Zavin0052abd2011-09-22 15:49:04 -0700705 return;
706 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700707
Dima Zavin0052abd2011-09-22 15:49:04 -0700708 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
709
710 /* animation starting, set up the animation */
711 if (batt_anim->cur_frame == 0) {
712 int batt_cap;
713 int ret;
714
715 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700716 batt_cap = get_battery_capacity(charger);
717 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700718 int i;
719
720 /* find first frame given current capacity */
721 for (i = 1; i < batt_anim->num_frames; i++) {
722 if (batt_cap < batt_anim->frames[i].min_capacity)
723 break;
724 }
725 batt_anim->cur_frame = i - 1;
726
727 /* show the first frame for twice as long */
728 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
729 }
730
731 batt_anim->capacity = batt_cap;
732 }
733
734 /* unblank the screen on first cycle */
735 if (batt_anim->cur_cycle == 0)
736 gr_fb_blank(false);
737
738 /* draw the new frame (@ cur_frame) */
739 redraw_screen(charger);
740
741 /* if we don't have anim frames, we only have one image, so just bump
742 * the cycle counter and exit
743 */
744 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
745 LOGV("[%lld] animation missing or unknown battery status\n", now);
746 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
747 batt_anim->cur_cycle++;
748 return;
749 }
750
751 /* schedule next screen transition */
752 charger->next_screen_transition = now + disp_time;
753
754 /* advance frame cntr to the next valid frame
755 * if necessary, advance cycle cntr, and reset frame cntr
756 */
757 batt_anim->cur_frame++;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700758
759 /* if the frame is used for level-only, that is only show it when it's
760 * the current level, skip it during the animation.
761 */
762 while (batt_anim->cur_frame < batt_anim->num_frames &&
763 batt_anim->frames[batt_anim->cur_frame].level_only)
764 batt_anim->cur_frame++;
765 if (batt_anim->cur_frame >= batt_anim->num_frames) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700766 batt_anim->cur_cycle++;
767 batt_anim->cur_frame = 0;
768
769 /* don't reset the cycle counter, since we use that as a signal
770 * in a test above to check if animation is over
771 */
772 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700773}
774
Dima Zavin2471a6a2011-10-12 16:16:05 -0700775static int set_key_callback(int code, int value, void *data)
Dima Zavinf48b2362011-08-30 10:46:09 -0700776{
Dima Zavin2471a6a2011-10-12 16:16:05 -0700777 struct charger *charger = data;
778 int64_t now = curr_time_ms();
779 int down = !!value;
Dima Zavinf48b2362011-08-30 10:46:09 -0700780
Dima Zavin2471a6a2011-10-12 16:16:05 -0700781 if (code > KEY_MAX)
782 return -1;
Dima Zavinf48b2362011-08-30 10:46:09 -0700783
Dima Zavin2d978c02011-10-12 16:18:23 -0700784 /* ignore events that don't modify our state */
785 if (charger->keys[code].down == down)
Dima Zavin471157a2011-10-13 13:04:20 -0700786 return 0;
Dima Zavin2d978c02011-10-12 16:18:23 -0700787
Dima Zavinf48b2362011-08-30 10:46:09 -0700788 /* only record the down even timestamp, as the amount
789 * of time the key spent not being pressed is not useful */
790 if (down)
Dima Zavin2471a6a2011-10-12 16:16:05 -0700791 charger->keys[code].timestamp = now;
792 charger->keys[code].down = down;
793 charger->keys[code].pending = true;
Dima Zavinf48b2362011-08-30 10:46:09 -0700794 if (down) {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700795 LOGV("[%lld] key[%d] down\n", now, code);
Dima Zavinf48b2362011-08-30 10:46:09 -0700796 } else {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700797 int64_t duration = now - charger->keys[code].timestamp;
Dima Zavinf48b2362011-08-30 10:46:09 -0700798 int64_t secs = duration / 1000;
799 int64_t msecs = duration - secs * 1000;
800 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
Dima Zavin2471a6a2011-10-12 16:16:05 -0700801 code, secs, msecs);
Dima Zavinf48b2362011-08-30 10:46:09 -0700802 }
Dima Zavin2471a6a2011-10-12 16:16:05 -0700803
804 return 0;
805}
806
807static void update_input_state(struct charger *charger,
808 struct input_event *ev)
809{
810 if (ev->type != EV_KEY)
811 return;
812 set_key_callback(ev->code, ev->value, charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700813}
814
815static void set_next_key_check(struct charger *charger,
816 struct key_state *key,
817 int64_t timeout)
818{
819 int64_t then = key->timestamp + timeout;
820
821 if (charger->next_key_check == -1 || then < charger->next_key_check)
822 charger->next_key_check = then;
823}
824
825static void process_key(struct charger *charger, int code, int64_t now)
826{
827 struct key_state *key = &charger->keys[code];
828 int64_t next_key_check;
829
830 if (code == KEY_POWER) {
831 if (key->down) {
832 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
833 if (now >= reboot_timeout) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700834 /* We do not currently support booting from charger mode on
835 all devices. Check the property and continue booting or reboot
836 accordingly. */
837 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
838 LOGI("[%lld] booting from charger mode\n", now);
839 property_set("sys.boot_from_charger_mode", "1");
840 } else {
841 LOGI("[%lld] rebooting\n", now);
842 android_reboot(ANDROID_RB_RESTART, 0, 0);
843 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700844 } else {
845 /* if the key is pressed but timeout hasn't expired,
846 * make sure we wake up at the right-ish time to check
847 */
848 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
849 }
850 } else {
851 /* if the power key got released, force screen state cycle */
choongryeol.lee92557132012-11-15 17:03:03 -0800852 if (key->pending) {
853 request_suspend(false);
Dima Zavin0052abd2011-09-22 15:49:04 -0700854 kick_animation(charger->batt_anim);
choongryeol.lee92557132012-11-15 17:03:03 -0800855 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700856 }
857 }
858
859 key->pending = false;
860}
861
862static void handle_input_state(struct charger *charger, int64_t now)
863{
864 process_key(charger, KEY_POWER, now);
865
866 if (charger->next_key_check != -1 && now > charger->next_key_check)
867 charger->next_key_check = -1;
868}
869
870static void handle_power_supply_state(struct charger *charger, int64_t now)
871{
872 if (charger->num_supplies_online == 0) {
choongryeol.lee92557132012-11-15 17:03:03 -0800873 request_suspend(false);
Dima Zavinf48b2362011-08-30 10:46:09 -0700874 if (charger->next_pwr_check == -1) {
875 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
876 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
877 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
878 } else if (now >= charger->next_pwr_check) {
879 LOGI("[%lld] shutting down\n", now);
880 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
881 } else {
882 /* otherwise we already have a shutdown timer scheduled */
883 }
884 } else {
885 /* online supply present, reset shutdown timer if set */
886 if (charger->next_pwr_check != -1) {
887 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700888 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700889 }
890 charger->next_pwr_check = -1;
891 }
892}
893
894static void wait_next_event(struct charger *charger, int64_t now)
895{
896 int64_t next_event = INT64_MAX;
897 int64_t timeout;
898 struct input_event ev;
899 int ret;
900
901 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
902 charger->next_screen_transition, charger->next_key_check,
903 charger->next_pwr_check);
904
Dima Zavinf48b2362011-08-30 10:46:09 -0700905 if (charger->next_screen_transition != -1)
906 next_event = charger->next_screen_transition;
907 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
908 next_event = charger->next_key_check;
909 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
910 next_event = charger->next_pwr_check;
911
912 if (next_event != -1 && next_event != INT64_MAX)
913 timeout = max(0, next_event - now);
914 else
915 timeout = -1;
916 LOGV("[%lld] blocking (%lld)\n", now, timeout);
917 ret = ev_wait((int)timeout);
918 if (!ret)
919 ev_dispatch();
920}
921
922static int input_callback(int fd, short revents, void *data)
923{
924 struct charger *charger = data;
925 struct input_event ev;
926 int ret;
927
928 ret = ev_get_input(fd, revents, &ev);
929 if (ret)
930 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700931 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700932 return 0;
933}
934
935static void event_loop(struct charger *charger)
936{
937 int ret;
938
939 while (true) {
940 int64_t now = curr_time_ms();
941
942 LOGV("[%lld] event_loop()\n", now);
943 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700944 handle_power_supply_state(charger, now);
945
Dima Zavin0052abd2011-09-22 15:49:04 -0700946 /* do screen update last in case any of the above want to start
947 * screen transitions (animations, etc)
948 */
949 update_screen_state(charger, now);
950
Dima Zavinf48b2362011-08-30 10:46:09 -0700951 wait_next_event(charger, now);
952 }
953}
954
955int main(int argc, char **argv)
956{
957 int ret;
958 struct charger *charger = &charger_state;
959 int64_t now = curr_time_ms() - 1;
960 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700961 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700962
963 list_init(&charger->supplies);
964
965 klog_init();
966 klog_set_level(CHARGER_KLOG_LEVEL);
967
Dima Zavin823ebc42011-09-29 17:20:07 -0700968 dump_last_kmsg();
969
970 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
971
Dima Zavinf48b2362011-08-30 10:46:09 -0700972 gr_init();
973 gr_font_size(&char_width, &char_height);
974
975 ev_init(input_callback, charger);
976
977 fd = uevent_open_socket(64*1024, true);
978 if (fd >= 0) {
979 fcntl(fd, F_SETFL, O_NONBLOCK);
980 ev_add_fd(fd, uevent_callback, charger);
981 }
982 charger->uevent_fd = fd;
983 coldboot(charger, "/sys/class/power_supply", "add");
984
Doug Zongker07099172014-03-17 12:23:00 -0700985 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700986 if (ret < 0) {
Doug Zongker0280f272014-03-11 08:42:09 -0700987 LOGE("Cannot load battery_fail image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700988 charger->surf_unknown = NULL;
989 }
990
Doug Zongker0280f272014-03-11 08:42:09 -0700991 charger->batt_anim = &battery_animation;
Dima Zavin0052abd2011-09-22 15:49:04 -0700992
Doug Zongker0280f272014-03-11 08:42:09 -0700993 gr_surface* scale_frames;
994 int scale_count;
Doug Zongker07099172014-03-17 12:23:00 -0700995 ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames);
Doug Zongker0280f272014-03-11 08:42:09 -0700996 if (ret < 0) {
997 LOGE("Cannot load battery_scale image\n");
998 charger->batt_anim->num_frames = 0;
999 charger->batt_anim->num_cycles = 1;
1000 } else if (scale_count != charger->batt_anim->num_frames) {
1001 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
1002 scale_count, charger->batt_anim->num_frames);
1003 charger->batt_anim->num_frames = 0;
1004 charger->batt_anim->num_cycles = 1;
1005 } else {
1006 for (i = 0; i < charger->batt_anim->num_frames; i++) {
1007 charger->batt_anim->frames[i].surface = scale_frames[i];
Dima Zavin0052abd2011-09-22 15:49:04 -07001008 }
Dima Zavinf48b2362011-08-30 10:46:09 -07001009 }
1010
Dima Zavin2471a6a2011-10-12 16:16:05 -07001011 ev_sync_key_state(set_key_callback, charger);
1012
Dima Zavin209c7b02012-10-12 15:13:52 -07001013#ifndef CHARGER_DISABLE_INIT_BLANK
Dima Zavinf48b2362011-08-30 10:46:09 -07001014 gr_fb_blank(true);
Dima Zavin209c7b02012-10-12 15:13:52 -07001015#endif
Dima Zavinf48b2362011-08-30 10:46:09 -07001016
1017 charger->next_screen_transition = now - 1;
1018 charger->next_key_check = -1;
1019 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -07001020 reset_animation(charger->batt_anim);
1021 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -07001022
1023 event_loop(charger);
1024
1025 return 0;
1026}