blob: dda24ae54149ecffdff90e0aa7ce8ae81059778d [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
50#ifndef max
51#define max(a,b) ((a) > (b) ? (a) : (b))
52#endif
53
54#ifndef min
55#define min(a,b) ((a) < (b) ? (a) : (b))
56#endif
57
Dima Zavin0052abd2011-09-22 15:49:04 -070058#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
59
Dima Zavinf48b2362011-08-30 10:46:09 -070060#define MSEC_PER_SEC (1000LL)
61#define NSEC_PER_MSEC (1000000LL)
62
Dima Zavin0052abd2011-09-22 15:49:04 -070063#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070064#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070065#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
66
Dima Zavin1a5ca612011-09-26 14:14:19 -070067#define BATTERY_FULL_THRESH 95
68
Dima Zavin823ebc42011-09-29 17:20:07 -070069#define LAST_KMSG_PATH "/proc/last_kmsg"
Todd Poynor0af90f62013-11-22 16:35:32 -080070#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops"
Dima Zavin823ebc42011-09-29 17:20:07 -070071#define LAST_KMSG_MAX_SZ (32 * 1024)
72
Dima Zavinf48b2362011-08-30 10:46:09 -070073#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
74#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
75#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
76
77struct key_state {
78 bool pending;
79 bool down;
80 int64_t timestamp;
81};
82
83struct power_supply {
84 struct listnode list;
85 char name[256];
86 char type[32];
87 bool online;
88 bool valid;
Dima Zavin0052abd2011-09-22 15:49:04 -070089 char cap_path[PATH_MAX];
90};
91
92struct frame {
93 const char *name;
94 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 {
144 .name = "charger/battery_0",
145 .disp_time = 750,
146 .min_capacity = 0,
147 },
148 {
149 .name = "charger/battery_1",
150 .disp_time = 750,
151 .min_capacity = 20,
152 },
153 {
154 .name = "charger/battery_2",
155 .disp_time = 750,
156 .min_capacity = 40,
157 },
158 {
159 .name = "charger/battery_3",
160 .disp_time = 750,
161 .min_capacity = 60,
162 },
163 {
164 .name = "charger/battery_4",
165 .disp_time = 750,
166 .min_capacity = 80,
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700167 .level_only = true,
Dima Zavin0052abd2011-09-22 15:49:04 -0700168 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700169 {
170 .name = "charger/battery_5",
171 .disp_time = 750,
172 .min_capacity = BATTERY_FULL_THRESH,
173 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700174};
175
176static struct animation battery_animation = {
177 .frames = batt_anim_frames,
178 .num_frames = ARRAY_SIZE(batt_anim_frames),
179 .num_cycles = 3,
180};
181
182static struct charger charger_state = {
183 .batt_anim = &battery_animation,
184};
185
Dima Zavinf48b2362011-08-30 10:46:09 -0700186static int char_width;
187static int char_height;
188
Dima Zavinf48b2362011-08-30 10:46:09 -0700189/* current time in milliseconds */
190static int64_t curr_time_ms(void)
191{
192 struct timespec tm;
193 clock_gettime(CLOCK_MONOTONIC, &tm);
194 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
195}
196
197static void clear_screen(void)
198{
199 gr_color(0, 0, 0, 255);
200 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
201};
202
Dima Zavin823ebc42011-09-29 17:20:07 -0700203#define MAX_KLOG_WRITE_BUF_SZ 256
204
205static void dump_last_kmsg(void)
206{
207 char *buf;
208 char *ptr;
209 unsigned sz = 0;
210 int len;
211
212 LOGI("\n");
213 LOGI("*************** LAST KMSG ***************\n");
214 LOGI("\n");
Todd Poynor0af90f62013-11-22 16:35:32 -0800215 buf = load_file(LAST_KMSG_PSTORE_PATH, &sz);
216
Dima Zavin823ebc42011-09-29 17:20:07 -0700217 if (!buf || !sz) {
Todd Poynor0af90f62013-11-22 16:35:32 -0800218 buf = load_file(LAST_KMSG_PATH, &sz);
219 if (!buf || !sz) {
220 LOGI("last_kmsg not found. Cold reset?\n");
221 goto out;
222 }
Dima Zavin823ebc42011-09-29 17:20:07 -0700223 }
224
225 len = min(sz, LAST_KMSG_MAX_SZ);
226 ptr = buf + (sz - len);
227
228 while (len > 0) {
229 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
230 char yoink;
231 char *nl;
232
233 nl = memrchr(ptr, '\n', cnt - 1);
234 if (nl)
235 cnt = nl - ptr + 1;
236
237 yoink = ptr[cnt];
238 ptr[cnt] = '\0';
Dima Zavind11e1a02011-10-12 16:13:56 -0700239 klog_write(6, "<6>%s", ptr);
Dima Zavin823ebc42011-09-29 17:20:07 -0700240 ptr[cnt] = yoink;
241
242 len -= cnt;
243 ptr += cnt;
244 }
245
246 free(buf);
247
248out:
249 LOGI("\n");
250 LOGI("************* END LAST KMSG *************\n");
251 LOGI("\n");
252}
253
Dima Zavinf48b2362011-08-30 10:46:09 -0700254static int read_file(const char *path, char *buf, size_t sz)
255{
256 int fd;
257 size_t cnt;
258
259 fd = open(path, O_RDONLY, 0);
260 if (fd < 0)
261 goto err;
262
263 cnt = read(fd, buf, sz - 1);
264 if (cnt <= 0)
265 goto err;
266 buf[cnt] = '\0';
267 if (buf[cnt - 1] == '\n') {
268 cnt--;
269 buf[cnt] = '\0';
270 }
271
272 close(fd);
273 return cnt;
274
275err:
276 if (fd >= 0)
277 close(fd);
278 return -1;
279}
280
281static int read_file_int(const char *path, int *val)
282{
283 char buf[32];
284 int ret;
285 int tmp;
286 char *end;
287
288 ret = read_file(path, buf, sizeof(buf));
289 if (ret < 0)
290 return -1;
291
292 tmp = strtol(buf, &end, 0);
293 if (end == buf ||
294 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
295 goto err;
296
297 *val = tmp;
298 return 0;
299
300err:
301 return -1;
302}
303
Dima Zavin1a5ca612011-09-26 14:14:19 -0700304static int get_battery_capacity(struct charger *charger)
305{
306 int ret;
307 int batt_cap = -1;
308
309 if (!charger->battery)
310 return -1;
311
312 ret = read_file_int(charger->battery->cap_path, &batt_cap);
313 if (ret < 0 || batt_cap > 100) {
314 batt_cap = -1;
315 }
316
317 return batt_cap;
318}
319
Dima Zavinf48b2362011-08-30 10:46:09 -0700320static struct power_supply *find_supply(struct charger *charger,
321 const char *name)
322{
323 struct listnode *node;
324 struct power_supply *supply;
325
326 list_for_each(node, &charger->supplies) {
327 supply = node_to_item(node, struct power_supply, list);
328 if (!strncmp(name, supply->name, sizeof(supply->name)))
329 return supply;
330 }
331 return NULL;
332}
333
334static struct power_supply *add_supply(struct charger *charger,
335 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700336 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700337{
338 struct power_supply *supply;
339
340 supply = calloc(1, sizeof(struct power_supply));
341 if (!supply)
342 return NULL;
343
344 strlcpy(supply->name, name, sizeof(supply->name));
345 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700346 snprintf(supply->cap_path, sizeof(supply->cap_path),
347 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700348 supply->online = online;
349 list_add_tail(&charger->supplies, &supply->list);
350 charger->num_supplies++;
351 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
352 return supply;
353}
354
355static void remove_supply(struct charger *charger, struct power_supply *supply)
356{
357 if (!supply)
358 return;
359 list_remove(&supply->list);
360 charger->num_supplies--;
361 free(supply);
362}
363
choongryeol.lee92557132012-11-15 17:03:03 -0800364#ifdef CHARGER_ENABLE_SUSPEND
365static int request_suspend(bool enable)
366{
367 if (enable)
368 return autosuspend_enable();
369 else
370 return autosuspend_disable();
371}
372#else
373static int request_suspend(bool enable)
374{
375 return 0;
376}
377#endif
378
Dima Zavinf48b2362011-08-30 10:46:09 -0700379static void parse_uevent(const char *msg, struct uevent *uevent)
380{
381 uevent->action = "";
382 uevent->path = "";
383 uevent->subsystem = "";
384 uevent->ps_name = "";
385 uevent->ps_online = "";
386 uevent->ps_type = "";
387
388 /* currently ignoring SEQNUM */
389 while (*msg) {
390#ifdef DEBUG_UEVENTS
391 LOGV("uevent str: %s\n", msg);
392#endif
393 if (!strncmp(msg, "ACTION=", 7)) {
394 msg += 7;
395 uevent->action = msg;
396 } else if (!strncmp(msg, "DEVPATH=", 8)) {
397 msg += 8;
398 uevent->path = msg;
399 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
400 msg += 10;
401 uevent->subsystem = msg;
402 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
403 msg += 18;
404 uevent->ps_name = msg;
405 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
406 msg += 20;
407 uevent->ps_online = msg;
408 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
409 msg += 18;
410 uevent->ps_type = msg;
411 }
412
413 /* advance to after the next \0 */
414 while (*msg++)
415 ;
416 }
417
418 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
419 uevent->action, uevent->path, uevent->subsystem,
420 uevent->ps_name, uevent->ps_type, uevent->ps_online);
421}
422
423static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
424{
425 int online;
426 char ps_type[32];
427 struct power_supply *supply = NULL;
428 int i;
429 bool was_online = false;
430 bool battery = false;
431
432 if (uevent->ps_type[0] == '\0') {
433 char *path;
434 int ret;
435
436 if (uevent->path[0] == '\0')
437 return;
438 ret = asprintf(&path, "/sys/%s/type", uevent->path);
439 if (ret <= 0)
440 return;
441 ret = read_file(path, ps_type, sizeof(ps_type));
442 free(path);
443 if (ret < 0)
444 return;
445 } else {
446 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
447 }
448
449 if (!strncmp(ps_type, "Battery", 7))
450 battery = true;
451
452 online = atoi(uevent->ps_online);
453 supply = find_supply(charger, uevent->ps_name);
454 if (supply) {
455 was_online = supply->online;
456 supply->online = online;
457 }
458
459 if (!strcmp(uevent->action, "add")) {
460 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700461 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
462 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700463 if (!supply) {
464 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
465 uevent->ps_type, online);
466 return;
467 }
468 /* only pick up the first battery for now */
469 if (battery && !charger->battery)
470 charger->battery = supply;
471 } else {
472 LOGE("supply '%s' already exists..\n", uevent->ps_name);
473 }
474 } else if (!strcmp(uevent->action, "remove")) {
475 if (supply) {
476 if (charger->battery == supply)
477 charger->battery = NULL;
478 remove_supply(charger, supply);
479 supply = NULL;
480 }
481 } else if (!strcmp(uevent->action, "change")) {
482 if (!supply) {
483 LOGE("power supply '%s' not found ('%s' %d)\n",
484 uevent->ps_name, ps_type, online);
485 return;
486 }
487 } else {
488 return;
489 }
490
491 /* allow battery to be managed in the supply list but make it not
492 * contribute to online power supplies. */
493 if (!battery) {
494 if (was_online && !online)
495 charger->num_supplies_online--;
496 else if (supply && !was_online && online)
497 charger->num_supplies_online++;
498 }
499
500 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
501 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
502 uevent->action, charger->num_supplies_online, charger->num_supplies);
503}
504
505static void process_uevent(struct charger *charger, struct uevent *uevent)
506{
507 if (!strcmp(uevent->subsystem, "power_supply"))
508 process_ps_uevent(charger, uevent);
509}
510
511#define UEVENT_MSG_LEN 1024
512static int handle_uevent_fd(struct charger *charger, int fd)
513{
514 char msg[UEVENT_MSG_LEN+2];
515 int n;
516
517 if (fd < 0)
518 return -1;
519
520 while (true) {
521 struct uevent uevent;
522
523 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
524 if (n <= 0)
525 break;
526 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
527 continue;
528
529 msg[n] = '\0';
530 msg[n+1] = '\0';
531
532 parse_uevent(msg, &uevent);
533 process_uevent(charger, &uevent);
534 }
535
536 return 0;
537}
538
539static int uevent_callback(int fd, short revents, void *data)
540{
541 struct charger *charger = data;
542
543 if (!(revents & POLLIN))
544 return -1;
545 return handle_uevent_fd(charger, fd);
546}
547
548/* force the kernel to regenerate the change events for the existing
549 * devices, if valid */
550static void do_coldboot(struct charger *charger, DIR *d, const char *event,
551 bool follow_links, int max_depth)
552{
553 struct dirent *de;
554 int dfd, fd;
555
556 dfd = dirfd(d);
557
558 fd = openat(dfd, "uevent", O_WRONLY);
559 if (fd >= 0) {
560 write(fd, event, strlen(event));
561 close(fd);
562 handle_uevent_fd(charger, charger->uevent_fd);
563 }
564
565 while ((de = readdir(d)) && max_depth > 0) {
566 DIR *d2;
567
568 LOGV("looking at '%s'\n", de->d_name);
569
570 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
571 de->d_name[0] == '.') {
572 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
573 de->d_name, de->d_type, max_depth, follow_links);
574 continue;
575 }
576 LOGV("can descend into '%s'\n", de->d_name);
577
578 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
579 if (fd < 0) {
580 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
581 errno, strerror(errno));
582 continue;
583 }
584
585 d2 = fdopendir(fd);
586 if (d2 == 0)
587 close(fd);
588 else {
589 LOGV("opened '%s'\n", de->d_name);
590 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
591 closedir(d2);
592 }
593 }
594}
595
596static void coldboot(struct charger *charger, const char *path,
597 const char *event)
598{
599 char str[256];
600
601 LOGV("doing coldboot '%s' in '%s'\n", event, path);
602 DIR *d = opendir(path);
603 if (d) {
604 snprintf(str, sizeof(str), "%s\n", event);
605 do_coldboot(charger, d, str, true, 1);
606 closedir(d);
607 }
608}
609
610static int draw_text(const char *str, int x, int y)
611{
612 int str_len_px = gr_measure(str);
613
614 if (x < 0)
615 x = (gr_fb_width() - str_len_px) / 2;
616 if (y < 0)
617 y = (gr_fb_height() - char_height) / 2;
Doug Zongker12c45fb2013-03-07 13:35:23 -0800618 gr_text(x, y, str, 0);
Dima Zavinf48b2362011-08-30 10:46:09 -0700619
620 return y + char_height;
621}
622
623static void android_green(void)
624{
625 gr_color(0xa4, 0xc6, 0x39, 255);
626}
627
Dima Zavin0052abd2011-09-22 15:49:04 -0700628/* returns the last y-offset of where the surface ends */
629static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700630{
Dima Zavin0052abd2011-09-22 15:49:04 -0700631 int w;
632 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700633 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700634 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700635
Dima Zavin0052abd2011-09-22 15:49:04 -0700636 w = gr_get_width(surface);
637 h = gr_get_height(surface);
638 x = (gr_fb_width() - w) / 2 ;
639 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700640
Dima Zavin0052abd2011-09-22 15:49:04 -0700641 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
642 gr_blit(surface, 0, 0, w, h, x, y);
643 return y + h;
644}
Dima Zavinf48b2362011-08-30 10:46:09 -0700645
Dima Zavin0052abd2011-09-22 15:49:04 -0700646static void draw_unknown(struct charger *charger)
647{
648 int y;
649 if (charger->surf_unknown) {
650 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700651 } else {
652 android_green();
653 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700654 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700655 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700656}
Dima Zavinf48b2362011-08-30 10:46:09 -0700657
Dima Zavin0052abd2011-09-22 15:49:04 -0700658static void draw_battery(struct charger *charger)
659{
660 struct animation *batt_anim = charger->batt_anim;
661 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
662
663 if (batt_anim->num_frames != 0) {
664 draw_surface_centered(charger, frame->surface);
665 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
666 batt_anim->cur_frame, frame->name, frame->min_capacity,
667 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700668 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700669}
Dima Zavinf48b2362011-08-30 10:46:09 -0700670
Dima Zavin0052abd2011-09-22 15:49:04 -0700671static void redraw_screen(struct charger *charger)
672{
673 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700674
Dima Zavin0052abd2011-09-22 15:49:04 -0700675 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700676
Dima Zavin0052abd2011-09-22 15:49:04 -0700677 /* try to display *something* */
678 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
679 draw_unknown(charger);
680 else
681 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700682 gr_flip();
683}
684
Dima Zavin0052abd2011-09-22 15:49:04 -0700685static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700686{
Dima Zavin0052abd2011-09-22 15:49:04 -0700687 anim->run = true;
688}
689
690static void reset_animation(struct animation *anim)
691{
692 anim->cur_cycle = 0;
693 anim->cur_frame = 0;
694 anim->run = false;
695}
696
697static void update_screen_state(struct charger *charger, int64_t now)
698{
699 struct animation *batt_anim = charger->batt_anim;
700 int cur_frame;
701 int disp_time;
702
703 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700704 return;
705
Dima Zavin0052abd2011-09-22 15:49:04 -0700706 /* animation is over, blank screen and leave */
707 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
708 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700709 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700710 gr_fb_blank(true);
711 LOGV("[%lld] animation done\n", now);
Devin Kimfd8e6502012-12-07 09:25:06 -0800712 if (charger->num_supplies_online > 0)
713 request_suspend(true);
Dima Zavin0052abd2011-09-22 15:49:04 -0700714 return;
715 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700716
Dima Zavin0052abd2011-09-22 15:49:04 -0700717 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
718
719 /* animation starting, set up the animation */
720 if (batt_anim->cur_frame == 0) {
721 int batt_cap;
722 int ret;
723
724 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700725 batt_cap = get_battery_capacity(charger);
726 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700727 int i;
728
729 /* find first frame given current capacity */
730 for (i = 1; i < batt_anim->num_frames; i++) {
731 if (batt_cap < batt_anim->frames[i].min_capacity)
732 break;
733 }
734 batt_anim->cur_frame = i - 1;
735
736 /* show the first frame for twice as long */
737 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
738 }
739
740 batt_anim->capacity = batt_cap;
741 }
742
743 /* unblank the screen on first cycle */
744 if (batt_anim->cur_cycle == 0)
745 gr_fb_blank(false);
746
747 /* draw the new frame (@ cur_frame) */
748 redraw_screen(charger);
749
750 /* if we don't have anim frames, we only have one image, so just bump
751 * the cycle counter and exit
752 */
753 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
754 LOGV("[%lld] animation missing or unknown battery status\n", now);
755 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
756 batt_anim->cur_cycle++;
757 return;
758 }
759
760 /* schedule next screen transition */
761 charger->next_screen_transition = now + disp_time;
762
763 /* advance frame cntr to the next valid frame
764 * if necessary, advance cycle cntr, and reset frame cntr
765 */
766 batt_anim->cur_frame++;
Dima Zavin9ec3f3e2011-10-31 16:53:41 -0700767
768 /* if the frame is used for level-only, that is only show it when it's
769 * the current level, skip it during the animation.
770 */
771 while (batt_anim->cur_frame < batt_anim->num_frames &&
772 batt_anim->frames[batt_anim->cur_frame].level_only)
773 batt_anim->cur_frame++;
774 if (batt_anim->cur_frame >= batt_anim->num_frames) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700775 batt_anim->cur_cycle++;
776 batt_anim->cur_frame = 0;
777
778 /* don't reset the cycle counter, since we use that as a signal
779 * in a test above to check if animation is over
780 */
781 }
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) {
843 LOGI("[%lld] rebooting\n", now);
844 android_reboot(ANDROID_RB_RESTART, 0, 0);
845 } else {
846 /* if the key is pressed but timeout hasn't expired,
847 * make sure we wake up at the right-ish time to check
848 */
849 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
850 }
851 } else {
852 /* if the power key got released, force screen state cycle */
choongryeol.lee92557132012-11-15 17:03:03 -0800853 if (key->pending) {
854 request_suspend(false);
Dima Zavin0052abd2011-09-22 15:49:04 -0700855 kick_animation(charger->batt_anim);
choongryeol.lee92557132012-11-15 17:03:03 -0800856 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700857 }
858 }
859
860 key->pending = false;
861}
862
863static void handle_input_state(struct charger *charger, int64_t now)
864{
865 process_key(charger, KEY_POWER, now);
866
867 if (charger->next_key_check != -1 && now > charger->next_key_check)
868 charger->next_key_check = -1;
869}
870
871static void handle_power_supply_state(struct charger *charger, int64_t now)
872{
873 if (charger->num_supplies_online == 0) {
choongryeol.lee92557132012-11-15 17:03:03 -0800874 request_suspend(false);
Dima Zavinf48b2362011-08-30 10:46:09 -0700875 if (charger->next_pwr_check == -1) {
876 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
877 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
878 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
879 } else if (now >= charger->next_pwr_check) {
880 LOGI("[%lld] shutting down\n", now);
881 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
882 } else {
883 /* otherwise we already have a shutdown timer scheduled */
884 }
885 } else {
886 /* online supply present, reset shutdown timer if set */
887 if (charger->next_pwr_check != -1) {
888 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700889 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700890 }
891 charger->next_pwr_check = -1;
892 }
893}
894
895static void wait_next_event(struct charger *charger, int64_t now)
896{
897 int64_t next_event = INT64_MAX;
898 int64_t timeout;
899 struct input_event ev;
900 int ret;
901
902 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
903 charger->next_screen_transition, charger->next_key_check,
904 charger->next_pwr_check);
905
Dima Zavinf48b2362011-08-30 10:46:09 -0700906 if (charger->next_screen_transition != -1)
907 next_event = charger->next_screen_transition;
908 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
909 next_event = charger->next_key_check;
910 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
911 next_event = charger->next_pwr_check;
912
913 if (next_event != -1 && next_event != INT64_MAX)
914 timeout = max(0, next_event - now);
915 else
916 timeout = -1;
917 LOGV("[%lld] blocking (%lld)\n", now, timeout);
918 ret = ev_wait((int)timeout);
919 if (!ret)
920 ev_dispatch();
921}
922
923static int input_callback(int fd, short revents, void *data)
924{
925 struct charger *charger = data;
926 struct input_event ev;
927 int ret;
928
929 ret = ev_get_input(fd, revents, &ev);
930 if (ret)
931 return -1;
Dima Zavin2471a6a2011-10-12 16:16:05 -0700932 update_input_state(charger, &ev);
Dima Zavinf48b2362011-08-30 10:46:09 -0700933 return 0;
934}
935
936static void event_loop(struct charger *charger)
937{
938 int ret;
939
940 while (true) {
941 int64_t now = curr_time_ms();
942
943 LOGV("[%lld] event_loop()\n", now);
944 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700945 handle_power_supply_state(charger, now);
946
Dima Zavin0052abd2011-09-22 15:49:04 -0700947 /* do screen update last in case any of the above want to start
948 * screen transitions (animations, etc)
949 */
950 update_screen_state(charger, now);
951
Dima Zavinf48b2362011-08-30 10:46:09 -0700952 wait_next_event(charger, now);
953 }
954}
955
956int main(int argc, char **argv)
957{
958 int ret;
959 struct charger *charger = &charger_state;
960 int64_t now = curr_time_ms() - 1;
961 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700962 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700963
964 list_init(&charger->supplies);
965
966 klog_init();
967 klog_set_level(CHARGER_KLOG_LEVEL);
968
Dima Zavin823ebc42011-09-29 17:20:07 -0700969 dump_last_kmsg();
970
971 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
972
Dima Zavinf48b2362011-08-30 10:46:09 -0700973 gr_init();
974 gr_font_size(&char_width, &char_height);
975
976 ev_init(input_callback, charger);
977
978 fd = uevent_open_socket(64*1024, true);
979 if (fd >= 0) {
980 fcntl(fd, F_SETFL, O_NONBLOCK);
981 ev_add_fd(fd, uevent_callback, charger);
982 }
983 charger->uevent_fd = fd;
984 coldboot(charger, "/sys/class/power_supply", "add");
985
Dima Zavin0052abd2011-09-22 15:49:04 -0700986 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700987 if (ret < 0) {
988 LOGE("Cannot load image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700989 charger->surf_unknown = NULL;
990 }
991
992 for (i = 0; i < charger->batt_anim->num_frames; i++) {
993 struct frame *frame = &charger->batt_anim->frames[i];
994
995 ret = res_create_surface(frame->name, &frame->surface);
996 if (ret < 0) {
997 LOGE("Cannot load image %s\n", frame->name);
998 /* TODO: free the already allocated surfaces... */
999 charger->batt_anim->num_frames = 0;
1000 charger->batt_anim->num_cycles = 1;
1001 break;
1002 }
Dima Zavinf48b2362011-08-30 10:46:09 -07001003 }
1004
Dima Zavin2471a6a2011-10-12 16:16:05 -07001005 ev_sync_key_state(set_key_callback, charger);
1006
Dima Zavin209c7b02012-10-12 15:13:52 -07001007#ifndef CHARGER_DISABLE_INIT_BLANK
Dima Zavinf48b2362011-08-30 10:46:09 -07001008 gr_fb_blank(true);
Dima Zavin209c7b02012-10-12 15:13:52 -07001009#endif
Dima Zavinf48b2362011-08-30 10:46:09 -07001010
1011 charger->next_screen_transition = now - 1;
1012 charger->next_key_check = -1;
1013 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -07001014 reset_animation(charger->batt_anim);
1015 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -07001016
1017 event_loop(charger);
1018
1019 return 0;
1020}