blob: 47b1c07547a1c27c0d2d537577a485d940da77bf [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
Jenny TC9cc4ea32014-04-30 08:12:11 +0530754 /* advance frame cntr to the next valid frame only if we are charging
Dima Zavin0052abd2011-09-22 15:49:04 -0700755 * if necessary, advance cycle cntr, and reset frame cntr
756 */
Jenny TC9cc4ea32014-04-30 08:12:11 +0530757 if (charger->num_supplies_online != 0) {
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700758 batt_anim->cur_frame++;
Jenny TC9cc4ea32014-04-30 08:12:11 +0530759
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) {
767 batt_anim->cur_cycle++;
768 batt_anim->cur_frame = 0;
Dima Zavin0052abd2011-09-22 15:49:04 -0700769
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 */
Jenny TC9cc4ea32014-04-30 08:12:11 +0530773 }
774 } else {
775 /* Stop animating if we're not charging.
776 * If we stop it immediately instead of going through this loop, then
777 * the animation would stop somewhere in the middle.
778 */
779 batt_anim->cur_frame = 0;
780 batt_anim->cur_cycle++;
Dima Zavin0052abd2011-09-22 15:49:04 -0700781 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700782}
783
Dima Zavin2471a6a2011-10-12 16:16:05 -0700784static int set_key_callback(int code, int value, void *data)
Dima Zavinf48b2362011-08-30 10:46:09 -0700785{
Dima Zavin2471a6a2011-10-12 16:16:05 -0700786 struct charger *charger = data;
787 int64_t now = curr_time_ms();
788 int down = !!value;
Dima Zavinf48b2362011-08-30 10:46:09 -0700789
Dima Zavin2471a6a2011-10-12 16:16:05 -0700790 if (code > KEY_MAX)
791 return -1;
Dima Zavinf48b2362011-08-30 10:46:09 -0700792
Dima Zavin2d978c02011-10-12 16:18:23 -0700793 /* ignore events that don't modify our state */
794 if (charger->keys[code].down == down)
Dima Zavin471157a2011-10-13 13:04:20 -0700795 return 0;
Dima Zavin2d978c02011-10-12 16:18:23 -0700796
Dima Zavinf48b2362011-08-30 10:46:09 -0700797 /* only record the down even timestamp, as the amount
798 * of time the key spent not being pressed is not useful */
799 if (down)
Dima Zavin2471a6a2011-10-12 16:16:05 -0700800 charger->keys[code].timestamp = now;
801 charger->keys[code].down = down;
802 charger->keys[code].pending = true;
Dima Zavinf48b2362011-08-30 10:46:09 -0700803 if (down) {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700804 LOGV("[%lld] key[%d] down\n", now, code);
Dima Zavinf48b2362011-08-30 10:46:09 -0700805 } else {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700806 int64_t duration = now - charger->keys[code].timestamp;
Dima Zavinf48b2362011-08-30 10:46:09 -0700807 int64_t secs = duration / 1000;
808 int64_t msecs = duration - secs * 1000;
809 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
Dima Zavin2471a6a2011-10-12 16:16:05 -0700810 code, secs, msecs);
Dima Zavinf48b2362011-08-30 10:46:09 -0700811 }
Dima Zavin2471a6a2011-10-12 16:16:05 -0700812
813 return 0;
814}
815
816static void update_input_state(struct charger *charger,
817 struct input_event *ev)
818{
819 if (ev->type != EV_KEY)
820 return;
821 set_key_callback(ev->code, ev->value, charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700822}
823
824static void set_next_key_check(struct charger *charger,
825 struct key_state *key,
826 int64_t timeout)
827{
828 int64_t then = key->timestamp + timeout;
829
830 if (charger->next_key_check == -1 || then < charger->next_key_check)
831 charger->next_key_check = then;
832}
833
834static void process_key(struct charger *charger, int code, int64_t now)
835{
836 struct key_state *key = &charger->keys[code];
837 int64_t next_key_check;
838
839 if (code == KEY_POWER) {
840 if (key->down) {
841 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
842 if (now >= reboot_timeout) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700843 /* We do not currently support booting from charger mode on
844 all devices. Check the property and continue booting or reboot
845 accordingly. */
846 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
847 LOGI("[%lld] booting from charger mode\n", now);
848 property_set("sys.boot_from_charger_mode", "1");
849 } else {
850 LOGI("[%lld] rebooting\n", now);
851 android_reboot(ANDROID_RB_RESTART, 0, 0);
852 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700853 } else {
854 /* if the key is pressed but timeout hasn't expired,
855 * make sure we wake up at the right-ish time to check
856 */
857 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
858 }
859 } else {
860 /* if the power key got released, force screen state cycle */
choongryeol.lee92557132012-11-15 17:03:03 -0800861 if (key->pending) {
862 request_suspend(false);
Dima Zavin0052abd2011-09-22 15:49:04 -0700863 kick_animation(charger->batt_anim);
choongryeol.lee92557132012-11-15 17:03:03 -0800864 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700865 }
866 }
867
868 key->pending = false;
869}
870
871static void handle_input_state(struct charger *charger, int64_t now)
872{
873 process_key(charger, KEY_POWER, now);
874
875 if (charger->next_key_check != -1 && now > charger->next_key_check)
876 charger->next_key_check = -1;
877}
878
879static void handle_power_supply_state(struct charger *charger, int64_t now)
880{
881 if (charger->num_supplies_online == 0) {
choongryeol.lee92557132012-11-15 17:03:03 -0800882 request_suspend(false);
Dima Zavinf48b2362011-08-30 10:46:09 -0700883 if (charger->next_pwr_check == -1) {
884 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
885 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
886 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
887 } else if (now >= charger->next_pwr_check) {
888 LOGI("[%lld] shutting down\n", now);
889 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
890 } else {
891 /* otherwise we already have a shutdown timer scheduled */
892 }
893 } else {
894 /* online supply present, reset shutdown timer if set */
895 if (charger->next_pwr_check != -1) {
896 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700897 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700898 }
899 charger->next_pwr_check = -1;
900 }
901}
902
903static void wait_next_event(struct charger *charger, int64_t now)
904{
905 int64_t next_event = INT64_MAX;
906 int64_t timeout;
907 struct input_event ev;
908 int ret;
909
910 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
911 charger->next_screen_transition, charger->next_key_check,
912 charger->next_pwr_check);
913
Dima Zavinf48b2362011-08-30 10:46:09 -0700914 if (charger->next_screen_transition != -1)
915 next_event = charger->next_screen_transition;
916 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
917 next_event = charger->next_key_check;
918 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
919 next_event = charger->next_pwr_check;
920
921 if (next_event != -1 && next_event != INT64_MAX)
922 timeout = max(0, next_event - now);
923 else
924 timeout = -1;
925 LOGV("[%lld] blocking (%lld)\n", now, timeout);
926 ret = ev_wait((int)timeout);
927 if (!ret)
928 ev_dispatch();
929}
930
931static int input_callback(int fd, short revents, void *data)
932{
933 struct charger *charger = data;
934 struct input_event ev;
935 int ret;
936
937 ret = ev_get_input(fd, revents, &ev);
938 if (ret)
939 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700940 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700941 return 0;
942}
943
944static void event_loop(struct charger *charger)
945{
946 int ret;
947
948 while (true) {
949 int64_t now = curr_time_ms();
950
951 LOGV("[%lld] event_loop()\n", now);
952 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700953 handle_power_supply_state(charger, now);
954
Dima Zavin0052abd2011-09-22 15:49:04 -0700955 /* do screen update last in case any of the above want to start
956 * screen transitions (animations, etc)
957 */
958 update_screen_state(charger, now);
959
Dima Zavinf48b2362011-08-30 10:46:09 -0700960 wait_next_event(charger, now);
961 }
962}
963
964int main(int argc, char **argv)
965{
966 int ret;
967 struct charger *charger = &charger_state;
968 int64_t now = curr_time_ms() - 1;
969 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700970 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700971
972 list_init(&charger->supplies);
973
974 klog_init();
975 klog_set_level(CHARGER_KLOG_LEVEL);
976
Dima Zavin823ebc42011-09-29 17:20:07 -0700977 dump_last_kmsg();
978
979 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
980
Dima Zavinf48b2362011-08-30 10:46:09 -0700981 gr_init();
982 gr_font_size(&char_width, &char_height);
983
984 ev_init(input_callback, charger);
985
986 fd = uevent_open_socket(64*1024, true);
987 if (fd >= 0) {
988 fcntl(fd, F_SETFL, O_NONBLOCK);
989 ev_add_fd(fd, uevent_callback, charger);
990 }
991 charger->uevent_fd = fd;
992 coldboot(charger, "/sys/class/power_supply", "add");
993
Doug Zongker07099172014-03-17 12:23:00 -0700994 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700995 if (ret < 0) {
Doug Zongker0280f272014-03-11 08:42:09 -0700996 LOGE("Cannot load battery_fail image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700997 charger->surf_unknown = NULL;
998 }
999
Doug Zongker0280f272014-03-11 08:42:09 -07001000 charger->batt_anim = &battery_animation;
Dima Zavin0052abd2011-09-22 15:49:04 -07001001
Doug Zongker0280f272014-03-11 08:42:09 -07001002 gr_surface* scale_frames;
1003 int scale_count;
Doug Zongker07099172014-03-17 12:23:00 -07001004 ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames);
Doug Zongker0280f272014-03-11 08:42:09 -07001005 if (ret < 0) {
1006 LOGE("Cannot load battery_scale image\n");
1007 charger->batt_anim->num_frames = 0;
1008 charger->batt_anim->num_cycles = 1;
1009 } else if (scale_count != charger->batt_anim->num_frames) {
1010 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
1011 scale_count, charger->batt_anim->num_frames);
1012 charger->batt_anim->num_frames = 0;
1013 charger->batt_anim->num_cycles = 1;
1014 } else {
1015 for (i = 0; i < charger->batt_anim->num_frames; i++) {
1016 charger->batt_anim->frames[i].surface = scale_frames[i];
Dima Zavin0052abd2011-09-22 15:49:04 -07001017 }
Dima Zavinf48b2362011-08-30 10:46:09 -07001018 }
1019
Dima Zavin2471a6a2011-10-12 16:16:05 -07001020 ev_sync_key_state(set_key_callback, charger);
1021
Dima Zavin209c7b02012-10-12 15:13:52 -07001022#ifndef CHARGER_DISABLE_INIT_BLANK
Dima Zavinf48b2362011-08-30 10:46:09 -07001023 gr_fb_blank(true);
Dima Zavin209c7b02012-10-12 15:13:52 -07001024#endif
Dima Zavinf48b2362011-08-30 10:46:09 -07001025
1026 charger->next_screen_transition = now - 1;
1027 charger->next_key_check = -1;
1028 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -07001029 reset_animation(charger->batt_anim);
1030 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -07001031
1032 event_loop(charger);
1033
1034 return 0;
1035}