blob: e3cadb1ddf196db22997f092adda361abb4733a9 [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 {
Dima Zavin0052abd2011-09-22 15:49:04 -070094 int disp_time;
95 int min_capacity;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -070096 bool level_only;
Dima Zavin0052abd2011-09-22 15:49:04 -070097
98 gr_surface surface;
99};
100
101struct animation {
102 bool run;
103
104 struct frame *frames;
105 int cur_frame;
106 int num_frames;
107
108 int cur_cycle;
109 int num_cycles;
110
111 /* current capacity being animated */
112 int capacity;
Dima Zavinf48b2362011-08-30 10:46:09 -0700113};
114
115struct charger {
116 int64_t next_screen_transition;
117 int64_t next_key_check;
118 int64_t next_pwr_check;
Dima Zavinf48b2362011-08-30 10:46:09 -0700119
120 struct key_state keys[KEY_MAX + 1];
Dima Zavinf48b2362011-08-30 10:46:09 -0700121 int uevent_fd;
122
123 struct listnode supplies;
124 int num_supplies;
125 int num_supplies_online;
126
Dima Zavin0052abd2011-09-22 15:49:04 -0700127 struct animation *batt_anim;
128 gr_surface surf_unknown;
129
Dima Zavinf48b2362011-08-30 10:46:09 -0700130 struct power_supply *battery;
131};
132
133struct uevent {
134 const char *action;
135 const char *path;
136 const char *subsystem;
137 const char *ps_name;
138 const char *ps_type;
139 const char *ps_online;
140};
141
Dima Zavin0052abd2011-09-22 15:49:04 -0700142static struct frame batt_anim_frames[] = {
143 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700144 .disp_time = 750,
145 .min_capacity = 0,
146 },
147 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700148 .disp_time = 750,
149 .min_capacity = 20,
150 },
151 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700152 .disp_time = 750,
153 .min_capacity = 40,
154 },
155 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700156 .disp_time = 750,
157 .min_capacity = 60,
158 },
159 {
Dima Zavin0052abd2011-09-22 15:49:04 -0700160 .disp_time = 750,
161 .min_capacity = 80,
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700162 .level_only = true,
Dima Zavin0052abd2011-09-22 15:49:04 -0700163 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700164 {
Dima Zavin1a5ca612011-09-26 14:14:19 -0700165 .disp_time = 750,
166 .min_capacity = BATTERY_FULL_THRESH,
167 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700168};
169
170static struct animation battery_animation = {
171 .frames = batt_anim_frames,
172 .num_frames = ARRAY_SIZE(batt_anim_frames),
173 .num_cycles = 3,
174};
175
176static struct charger charger_state = {
177 .batt_anim = &battery_animation,
178};
179
Dima Zavinf48b2362011-08-30 10:46:09 -0700180static int char_width;
181static int char_height;
182
Dima Zavinf48b2362011-08-30 10:46:09 -0700183/* current time in milliseconds */
184static int64_t curr_time_ms(void)
185{
186 struct timespec tm;
187 clock_gettime(CLOCK_MONOTONIC, &tm);
188 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
189}
190
191static void clear_screen(void)
192{
193 gr_color(0, 0, 0, 255);
Doug Zongker0280f272014-03-11 08:42:09 -0700194 gr_clear();
195}
Dima Zavinf48b2362011-08-30 10:46:09 -0700196
Dima Zavin823ebc42011-09-29 17:20:07 -0700197#define MAX_KLOG_WRITE_BUF_SZ 256
198
199static void dump_last_kmsg(void)
200{
201 char *buf;
202 char *ptr;
203 unsigned sz = 0;
204 int len;
205
206 LOGI("\n");
207 LOGI("*************** LAST KMSG ***************\n");
208 LOGI("\n");
209 buf = load_file(LAST_KMSG_PATH, &sz);
210 if (!buf || !sz) {
211 LOGI("last_kmsg not found. Cold reset?\n");
212 goto out;
213 }
214
215 len = min(sz, LAST_KMSG_MAX_SZ);
216 ptr = buf + (sz - len);
217
218 while (len > 0) {
219 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
220 char yoink;
221 char *nl;
222
223 nl = memrchr(ptr, '\n', cnt - 1);
224 if (nl)
225 cnt = nl - ptr + 1;
226
227 yoink = ptr[cnt];
228 ptr[cnt] = '\0';
Dima Zavind11e1a02011-10-12 16:13:56 -0700229 klog_write(6, "<6>%s", ptr);
Dima Zavin823ebc42011-09-29 17:20:07 -0700230 ptr[cnt] = yoink;
231
232 len -= cnt;
233 ptr += cnt;
234 }
235
236 free(buf);
237
238out:
239 LOGI("\n");
240 LOGI("************* END LAST KMSG *************\n");
241 LOGI("\n");
242}
243
Dima Zavinf48b2362011-08-30 10:46:09 -0700244static int read_file(const char *path, char *buf, size_t sz)
245{
246 int fd;
247 size_t cnt;
248
249 fd = open(path, O_RDONLY, 0);
250 if (fd < 0)
251 goto err;
252
253 cnt = read(fd, buf, sz - 1);
254 if (cnt <= 0)
255 goto err;
256 buf[cnt] = '\0';
257 if (buf[cnt - 1] == '\n') {
258 cnt--;
259 buf[cnt] = '\0';
260 }
261
262 close(fd);
263 return cnt;
264
265err:
266 if (fd >= 0)
267 close(fd);
268 return -1;
269}
270
271static int read_file_int(const char *path, int *val)
272{
273 char buf[32];
274 int ret;
275 int tmp;
276 char *end;
277
278 ret = read_file(path, buf, sizeof(buf));
279 if (ret < 0)
280 return -1;
281
282 tmp = strtol(buf, &end, 0);
283 if (end == buf ||
284 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
285 goto err;
286
287 *val = tmp;
288 return 0;
289
290err:
291 return -1;
292}
293
Dima Zavin1a5ca612011-09-26 14:14:19 -0700294static int get_battery_capacity(struct charger *charger)
295{
296 int ret;
297 int batt_cap = -1;
298
299 if (!charger->battery)
300 return -1;
301
302 ret = read_file_int(charger->battery->cap_path, &batt_cap);
303 if (ret < 0 || batt_cap > 100) {
304 batt_cap = -1;
305 }
306
307 return batt_cap;
308}
309
Dima Zavinf48b2362011-08-30 10:46:09 -0700310static struct power_supply *find_supply(struct charger *charger,
311 const char *name)
312{
313 struct listnode *node;
314 struct power_supply *supply;
315
316 list_for_each(node, &charger->supplies) {
317 supply = node_to_item(node, struct power_supply, list);
318 if (!strncmp(name, supply->name, sizeof(supply->name)))
319 return supply;
320 }
321 return NULL;
322}
323
324static struct power_supply *add_supply(struct charger *charger,
325 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700326 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700327{
328 struct power_supply *supply;
329
330 supply = calloc(1, sizeof(struct power_supply));
331 if (!supply)
332 return NULL;
333
334 strlcpy(supply->name, name, sizeof(supply->name));
335 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700336 snprintf(supply->cap_path, sizeof(supply->cap_path),
337 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700338 supply->online = online;
339 list_add_tail(&charger->supplies, &supply->list);
340 charger->num_supplies++;
341 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
342 return supply;
343}
344
345static void remove_supply(struct charger *charger, struct power_supply *supply)
346{
347 if (!supply)
348 return;
349 list_remove(&supply->list);
350 charger->num_supplies--;
351 free(supply);
352}
353
choongryeol.lee92557132012-11-15 17:03:03 -0800354#ifdef CHARGER_ENABLE_SUSPEND
355static int request_suspend(bool enable)
356{
357 if (enable)
358 return autosuspend_enable();
359 else
360 return autosuspend_disable();
361}
362#else
363static int request_suspend(bool enable)
364{
365 return 0;
366}
367#endif
368
Dima Zavinf48b2362011-08-30 10:46:09 -0700369static void parse_uevent(const char *msg, struct uevent *uevent)
370{
371 uevent->action = "";
372 uevent->path = "";
373 uevent->subsystem = "";
374 uevent->ps_name = "";
375 uevent->ps_online = "";
376 uevent->ps_type = "";
377
378 /* currently ignoring SEQNUM */
379 while (*msg) {
380#ifdef DEBUG_UEVENTS
381 LOGV("uevent str: %s\n", msg);
382#endif
383 if (!strncmp(msg, "ACTION=", 7)) {
384 msg += 7;
385 uevent->action = msg;
386 } else if (!strncmp(msg, "DEVPATH=", 8)) {
387 msg += 8;
388 uevent->path = msg;
389 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
390 msg += 10;
391 uevent->subsystem = msg;
392 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
393 msg += 18;
394 uevent->ps_name = msg;
395 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
396 msg += 20;
397 uevent->ps_online = msg;
398 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
399 msg += 18;
400 uevent->ps_type = msg;
401 }
402
403 /* advance to after the next \0 */
404 while (*msg++)
405 ;
406 }
407
408 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
409 uevent->action, uevent->path, uevent->subsystem,
410 uevent->ps_name, uevent->ps_type, uevent->ps_online);
411}
412
413static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
414{
415 int online;
416 char ps_type[32];
417 struct power_supply *supply = NULL;
418 int i;
419 bool was_online = false;
420 bool battery = false;
421
422 if (uevent->ps_type[0] == '\0') {
423 char *path;
424 int ret;
425
426 if (uevent->path[0] == '\0')
427 return;
428 ret = asprintf(&path, "/sys/%s/type", uevent->path);
429 if (ret <= 0)
430 return;
431 ret = read_file(path, ps_type, sizeof(ps_type));
432 free(path);
433 if (ret < 0)
434 return;
435 } else {
436 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
437 }
438
439 if (!strncmp(ps_type, "Battery", 7))
440 battery = true;
441
442 online = atoi(uevent->ps_online);
443 supply = find_supply(charger, uevent->ps_name);
444 if (supply) {
445 was_online = supply->online;
446 supply->online = online;
447 }
448
449 if (!strcmp(uevent->action, "add")) {
450 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700451 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
452 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700453 if (!supply) {
454 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
455 uevent->ps_type, online);
456 return;
457 }
458 /* only pick up the first battery for now */
459 if (battery && !charger->battery)
460 charger->battery = supply;
461 } else {
462 LOGE("supply '%s' already exists..\n", uevent->ps_name);
463 }
464 } else if (!strcmp(uevent->action, "remove")) {
465 if (supply) {
466 if (charger->battery == supply)
467 charger->battery = NULL;
468 remove_supply(charger, supply);
469 supply = NULL;
470 }
471 } else if (!strcmp(uevent->action, "change")) {
472 if (!supply) {
473 LOGE("power supply '%s' not found ('%s' %d)\n",
474 uevent->ps_name, ps_type, online);
475 return;
476 }
477 } else {
478 return;
479 }
480
481 /* allow battery to be managed in the supply list but make it not
482 * contribute to online power supplies. */
483 if (!battery) {
484 if (was_online && !online)
485 charger->num_supplies_online--;
486 else if (supply && !was_online && online)
487 charger->num_supplies_online++;
488 }
489
490 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
491 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
492 uevent->action, charger->num_supplies_online, charger->num_supplies);
493}
494
495static void process_uevent(struct charger *charger, struct uevent *uevent)
496{
497 if (!strcmp(uevent->subsystem, "power_supply"))
498 process_ps_uevent(charger, uevent);
499}
500
501#define UEVENT_MSG_LEN 1024
502static int handle_uevent_fd(struct charger *charger, int fd)
503{
504 char msg[UEVENT_MSG_LEN+2];
505 int n;
506
507 if (fd < 0)
508 return -1;
509
510 while (true) {
511 struct uevent uevent;
512
513 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
514 if (n <= 0)
515 break;
516 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
517 continue;
518
519 msg[n] = '\0';
520 msg[n+1] = '\0';
521
522 parse_uevent(msg, &uevent);
523 process_uevent(charger, &uevent);
524 }
525
526 return 0;
527}
528
529static int uevent_callback(int fd, short revents, void *data)
530{
531 struct charger *charger = data;
532
533 if (!(revents & POLLIN))
534 return -1;
535 return handle_uevent_fd(charger, fd);
536}
537
538/* force the kernel to regenerate the change events for the existing
539 * devices, if valid */
540static void do_coldboot(struct charger *charger, DIR *d, const char *event,
541 bool follow_links, int max_depth)
542{
543 struct dirent *de;
544 int dfd, fd;
545
546 dfd = dirfd(d);
547
548 fd = openat(dfd, "uevent", O_WRONLY);
549 if (fd >= 0) {
550 write(fd, event, strlen(event));
551 close(fd);
552 handle_uevent_fd(charger, charger->uevent_fd);
553 }
554
555 while ((de = readdir(d)) && max_depth > 0) {
556 DIR *d2;
557
558 LOGV("looking at '%s'\n", de->d_name);
559
560 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
561 de->d_name[0] == '.') {
562 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
563 de->d_name, de->d_type, max_depth, follow_links);
564 continue;
565 }
566 LOGV("can descend into '%s'\n", de->d_name);
567
568 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
569 if (fd < 0) {
570 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
571 errno, strerror(errno));
572 continue;
573 }
574
575 d2 = fdopendir(fd);
576 if (d2 == 0)
577 close(fd);
578 else {
579 LOGV("opened '%s'\n", de->d_name);
580 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
581 closedir(d2);
582 }
583 }
584}
585
586static void coldboot(struct charger *charger, const char *path,
587 const char *event)
588{
589 char str[256];
590
591 LOGV("doing coldboot '%s' in '%s'\n", event, path);
592 DIR *d = opendir(path);
593 if (d) {
594 snprintf(str, sizeof(str), "%s\n", event);
595 do_coldboot(charger, d, str, true, 1);
596 closedir(d);
597 }
598}
599
600static int draw_text(const char *str, int x, int y)
601{
602 int str_len_px = gr_measure(str);
603
604 if (x < 0)
605 x = (gr_fb_width() - str_len_px) / 2;
606 if (y < 0)
607 y = (gr_fb_height() - char_height) / 2;
Doug Zongker12c45fb2013-03-07 13:35:23 -0800608 gr_text(x, y, str, 0);
Dima Zavinf48b2362011-08-30 10:46:09 -0700609
610 return y + char_height;
611}
612
613static void android_green(void)
614{
615 gr_color(0xa4, 0xc6, 0x39, 255);
616}
617
Dima Zavin0052abd2011-09-22 15:49:04 -0700618/* returns the last y-offset of where the surface ends */
619static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700620{
Dima Zavin0052abd2011-09-22 15:49:04 -0700621 int w;
622 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700623 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700624 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700625
Dima Zavin0052abd2011-09-22 15:49:04 -0700626 w = gr_get_width(surface);
627 h = gr_get_height(surface);
628 x = (gr_fb_width() - w) / 2 ;
629 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700630
Dima Zavin0052abd2011-09-22 15:49:04 -0700631 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
632 gr_blit(surface, 0, 0, w, h, x, y);
633 return y + h;
634}
Dima Zavinf48b2362011-08-30 10:46:09 -0700635
Dima Zavin0052abd2011-09-22 15:49:04 -0700636static void draw_unknown(struct charger *charger)
637{
638 int y;
639 if (charger->surf_unknown) {
640 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700641 } else {
642 android_green();
643 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700644 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700645 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700646}
Dima Zavinf48b2362011-08-30 10:46:09 -0700647
Dima Zavin0052abd2011-09-22 15:49:04 -0700648static void draw_battery(struct charger *charger)
649{
650 struct animation *batt_anim = charger->batt_anim;
651 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
652
653 if (batt_anim->num_frames != 0) {
654 draw_surface_centered(charger, frame->surface);
Doug Zongker0280f272014-03-11 08:42:09 -0700655 LOGV("drawing frame #%d min_cap=%d time=%d\n",
656 batt_anim->cur_frame, frame->min_capacity,
Dima Zavin0052abd2011-09-22 15:49:04 -0700657 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700658 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700659}
Dima Zavinf48b2362011-08-30 10:46:09 -0700660
Dima Zavin0052abd2011-09-22 15:49:04 -0700661static void redraw_screen(struct charger *charger)
662{
663 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700664
Dima Zavin0052abd2011-09-22 15:49:04 -0700665 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700666
Dima Zavin0052abd2011-09-22 15:49:04 -0700667 /* try to display *something* */
668 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
669 draw_unknown(charger);
670 else
671 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700672 gr_flip();
673}
674
Dima Zavin0052abd2011-09-22 15:49:04 -0700675static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700676{
Dima Zavin0052abd2011-09-22 15:49:04 -0700677 anim->run = true;
678}
679
680static void reset_animation(struct animation *anim)
681{
682 anim->cur_cycle = 0;
683 anim->cur_frame = 0;
684 anim->run = false;
685}
686
687static void update_screen_state(struct charger *charger, int64_t now)
688{
689 struct animation *batt_anim = charger->batt_anim;
690 int cur_frame;
691 int disp_time;
692
693 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700694 return;
695
Dima Zavin0052abd2011-09-22 15:49:04 -0700696 /* animation is over, blank screen and leave */
697 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
698 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700699 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700700 gr_fb_blank(true);
701 LOGV("[%lld] animation done\n", now);
Devin Kimfd8e6502012-12-07 09:25:06 -0800702 if (charger->num_supplies_online > 0)
703 request_suspend(true);
Dima Zavin0052abd2011-09-22 15:49:04 -0700704 return;
705 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700706
Dima Zavin0052abd2011-09-22 15:49:04 -0700707 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
708
709 /* animation starting, set up the animation */
710 if (batt_anim->cur_frame == 0) {
711 int batt_cap;
712 int ret;
713
714 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700715 batt_cap = get_battery_capacity(charger);
716 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700717 int i;
718
719 /* find first frame given current capacity */
720 for (i = 1; i < batt_anim->num_frames; i++) {
721 if (batt_cap < batt_anim->frames[i].min_capacity)
722 break;
723 }
724 batt_anim->cur_frame = i - 1;
725
726 /* show the first frame for twice as long */
727 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
728 }
729
730 batt_anim->capacity = batt_cap;
731 }
732
733 /* unblank the screen on first cycle */
734 if (batt_anim->cur_cycle == 0)
735 gr_fb_blank(false);
736
737 /* draw the new frame (@ cur_frame) */
738 redraw_screen(charger);
739
740 /* if we don't have anim frames, we only have one image, so just bump
741 * the cycle counter and exit
742 */
743 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
744 LOGV("[%lld] animation missing or unknown battery status\n", now);
745 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
746 batt_anim->cur_cycle++;
747 return;
748 }
749
750 /* schedule next screen transition */
751 charger->next_screen_transition = now + disp_time;
752
753 /* advance frame cntr to the next valid frame
754 * if necessary, advance cycle cntr, and reset frame cntr
755 */
756 batt_anim->cur_frame++;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700757
758 /* if the frame is used for level-only, that is only show it when it's
759 * the current level, skip it during the animation.
760 */
761 while (batt_anim->cur_frame < batt_anim->num_frames &&
762 batt_anim->frames[batt_anim->cur_frame].level_only)
763 batt_anim->cur_frame++;
764 if (batt_anim->cur_frame >= batt_anim->num_frames) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700765 batt_anim->cur_cycle++;
766 batt_anim->cur_frame = 0;
767
768 /* don't reset the cycle counter, since we use that as a signal
769 * in a test above to check if animation is over
770 */
771 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700772}
773
Dima Zavin2471a6a2011-10-12 16:16:05 -0700774static int set_key_callback(int code, int value, void *data)
Dima Zavinf48b2362011-08-30 10:46:09 -0700775{
Dima Zavin2471a6a2011-10-12 16:16:05 -0700776 struct charger *charger = data;
777 int64_t now = curr_time_ms();
778 int down = !!value;
Dima Zavinf48b2362011-08-30 10:46:09 -0700779
Dima Zavin2471a6a2011-10-12 16:16:05 -0700780 if (code > KEY_MAX)
781 return -1;
Dima Zavinf48b2362011-08-30 10:46:09 -0700782
Dima Zavin2d978c02011-10-12 16:18:23 -0700783 /* ignore events that don't modify our state */
784 if (charger->keys[code].down == down)
Dima Zavin471157a2011-10-13 13:04:20 -0700785 return 0;
Dima Zavin2d978c02011-10-12 16:18:23 -0700786
Dima Zavinf48b2362011-08-30 10:46:09 -0700787 /* only record the down even timestamp, as the amount
788 * of time the key spent not being pressed is not useful */
789 if (down)
Dima Zavin2471a6a2011-10-12 16:16:05 -0700790 charger->keys[code].timestamp = now;
791 charger->keys[code].down = down;
792 charger->keys[code].pending = true;
Dima Zavinf48b2362011-08-30 10:46:09 -0700793 if (down) {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700794 LOGV("[%lld] key[%d] down\n", now, code);
Dima Zavinf48b2362011-08-30 10:46:09 -0700795 } else {
Dima Zavin2471a6a2011-10-12 16:16:05 -0700796 int64_t duration = now - charger->keys[code].timestamp;
Dima Zavinf48b2362011-08-30 10:46:09 -0700797 int64_t secs = duration / 1000;
798 int64_t msecs = duration - secs * 1000;
799 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
Dima Zavin2471a6a2011-10-12 16:16:05 -0700800 code, secs, msecs);
Dima Zavinf48b2362011-08-30 10:46:09 -0700801 }
Dima Zavin2471a6a2011-10-12 16:16:05 -0700802
803 return 0;
804}
805
806static void update_input_state(struct charger *charger,
807 struct input_event *ev)
808{
809 if (ev->type != EV_KEY)
810 return;
811 set_key_callback(ev->code, ev->value, charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700812}
813
814static void set_next_key_check(struct charger *charger,
815 struct key_state *key,
816 int64_t timeout)
817{
818 int64_t then = key->timestamp + timeout;
819
820 if (charger->next_key_check == -1 || then < charger->next_key_check)
821 charger->next_key_check = then;
822}
823
824static void process_key(struct charger *charger, int code, int64_t now)
825{
826 struct key_state *key = &charger->keys[code];
827 int64_t next_key_check;
828
829 if (code == KEY_POWER) {
830 if (key->down) {
831 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
832 if (now >= reboot_timeout) {
833 LOGI("[%lld] rebooting\n", now);
834 android_reboot(ANDROID_RB_RESTART, 0, 0);
835 } else {
836 /* if the key is pressed but timeout hasn't expired,
837 * make sure we wake up at the right-ish time to check
838 */
839 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
840 }
841 } else {
842 /* if the power key got released, force screen state cycle */
choongryeol.lee92557132012-11-15 17:03:03 -0800843 if (key->pending) {
844 request_suspend(false);
Dima Zavin0052abd2011-09-22 15:49:04 -0700845 kick_animation(charger->batt_anim);
choongryeol.lee92557132012-11-15 17:03:03 -0800846 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700847 }
848 }
849
850 key->pending = false;
851}
852
853static void handle_input_state(struct charger *charger, int64_t now)
854{
855 process_key(charger, KEY_POWER, now);
856
857 if (charger->next_key_check != -1 && now > charger->next_key_check)
858 charger->next_key_check = -1;
859}
860
861static void handle_power_supply_state(struct charger *charger, int64_t now)
862{
863 if (charger->num_supplies_online == 0) {
choongryeol.lee92557132012-11-15 17:03:03 -0800864 request_suspend(false);
Dima Zavinf48b2362011-08-30 10:46:09 -0700865 if (charger->next_pwr_check == -1) {
866 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
867 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
868 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
869 } else if (now >= charger->next_pwr_check) {
870 LOGI("[%lld] shutting down\n", now);
871 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
872 } else {
873 /* otherwise we already have a shutdown timer scheduled */
874 }
875 } else {
876 /* online supply present, reset shutdown timer if set */
877 if (charger->next_pwr_check != -1) {
878 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700879 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700880 }
881 charger->next_pwr_check = -1;
882 }
883}
884
885static void wait_next_event(struct charger *charger, int64_t now)
886{
887 int64_t next_event = INT64_MAX;
888 int64_t timeout;
889 struct input_event ev;
890 int ret;
891
892 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
893 charger->next_screen_transition, charger->next_key_check,
894 charger->next_pwr_check);
895
Dima Zavinf48b2362011-08-30 10:46:09 -0700896 if (charger->next_screen_transition != -1)
897 next_event = charger->next_screen_transition;
898 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
899 next_event = charger->next_key_check;
900 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
901 next_event = charger->next_pwr_check;
902
903 if (next_event != -1 && next_event != INT64_MAX)
904 timeout = max(0, next_event - now);
905 else
906 timeout = -1;
907 LOGV("[%lld] blocking (%lld)\n", now, timeout);
908 ret = ev_wait((int)timeout);
909 if (!ret)
910 ev_dispatch();
911}
912
913static int input_callback(int fd, short revents, void *data)
914{
915 struct charger *charger = data;
916 struct input_event ev;
917 int ret;
918
919 ret = ev_get_input(fd, revents, &ev);
920 if (ret)
921 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700922 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700923 return 0;
924}
925
926static void event_loop(struct charger *charger)
927{
928 int ret;
929
930 while (true) {
931 int64_t now = curr_time_ms();
932
933 LOGV("[%lld] event_loop()\n", now);
934 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700935 handle_power_supply_state(charger, now);
936
Dima Zavin0052abd2011-09-22 15:49:04 -0700937 /* do screen update last in case any of the above want to start
938 * screen transitions (animations, etc)
939 */
940 update_screen_state(charger, now);
941
Dima Zavinf48b2362011-08-30 10:46:09 -0700942 wait_next_event(charger, now);
943 }
944}
945
946int main(int argc, char **argv)
947{
948 int ret;
949 struct charger *charger = &charger_state;
950 int64_t now = curr_time_ms() - 1;
951 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700952 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700953
954 list_init(&charger->supplies);
955
956 klog_init();
957 klog_set_level(CHARGER_KLOG_LEVEL);
958
Dima Zavin823ebc42011-09-29 17:20:07 -0700959 dump_last_kmsg();
960
961 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
962
Dima Zavinf48b2362011-08-30 10:46:09 -0700963 gr_init();
964 gr_font_size(&char_width, &char_height);
965
966 ev_init(input_callback, charger);
967
968 fd = uevent_open_socket(64*1024, true);
969 if (fd >= 0) {
970 fcntl(fd, F_SETFL, O_NONBLOCK);
971 ev_add_fd(fd, uevent_callback, charger);
972 }
973 charger->uevent_fd = fd;
974 coldboot(charger, "/sys/class/power_supply", "add");
975
Doug Zongker07099172014-03-17 12:23:00 -0700976 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700977 if (ret < 0) {
Doug Zongker0280f272014-03-11 08:42:09 -0700978 LOGE("Cannot load battery_fail image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700979 charger->surf_unknown = NULL;
980 }
981
Doug Zongker0280f272014-03-11 08:42:09 -0700982 charger->batt_anim = &battery_animation;
Dima Zavin0052abd2011-09-22 15:49:04 -0700983
Doug Zongker0280f272014-03-11 08:42:09 -0700984 gr_surface* scale_frames;
985 int scale_count;
Doug Zongker07099172014-03-17 12:23:00 -0700986 ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames);
Doug Zongker0280f272014-03-11 08:42:09 -0700987 if (ret < 0) {
988 LOGE("Cannot load battery_scale image\n");
989 charger->batt_anim->num_frames = 0;
990 charger->batt_anim->num_cycles = 1;
991 } else if (scale_count != charger->batt_anim->num_frames) {
992 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
993 scale_count, charger->batt_anim->num_frames);
994 charger->batt_anim->num_frames = 0;
995 charger->batt_anim->num_cycles = 1;
996 } else {
997 for (i = 0; i < charger->batt_anim->num_frames; i++) {
998 charger->batt_anim->frames[i].surface = scale_frames[i];
Dima Zavin0052abd2011-09-22 15:49:04 -0700999 }
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}