blob: 0ade25036029d278e10e226ec3c519099bfae927 [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>
24#include <linux/netlink.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/poll.h>
30#include <sys/socket.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/un.h>
34#include <time.h>
35#include <unistd.h>
36
37#include <cutils/android_reboot.h>
38#include <cutils/klog.h>
39#include <cutils/list.h>
40#include <cutils/uevent.h>
41
42#include "minui/minui.h"
43
44#ifndef max
45#define max(a,b) ((a) > (b) ? (a) : (b))
46#endif
47
48#ifndef min
49#define min(a,b) ((a) < (b) ? (a) : (b))
50#endif
51
Dima Zavin0052abd2011-09-22 15:49:04 -070052#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
53
Dima Zavinf48b2362011-08-30 10:46:09 -070054#define MSEC_PER_SEC (1000LL)
55#define NSEC_PER_MSEC (1000000LL)
56
Dima Zavin0052abd2011-09-22 15:49:04 -070057#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070058#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070059#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
60
Dima Zavin1a5ca612011-09-26 14:14:19 -070061#define BATTERY_FULL_THRESH 95
62
Dima Zavinf48b2362011-08-30 10:46:09 -070063#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
64#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
65#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
66
67struct key_state {
68 bool pending;
69 bool down;
70 int64_t timestamp;
71};
72
73struct power_supply {
74 struct listnode list;
75 char name[256];
76 char type[32];
77 bool online;
78 bool valid;
Dima Zavin0052abd2011-09-22 15:49:04 -070079 char cap_path[PATH_MAX];
80};
81
82struct frame {
83 const char *name;
84 int disp_time;
85 int min_capacity;
86
87 gr_surface surface;
88};
89
90struct animation {
91 bool run;
92
93 struct frame *frames;
94 int cur_frame;
95 int num_frames;
96
97 int cur_cycle;
98 int num_cycles;
99
100 /* current capacity being animated */
101 int capacity;
Dima Zavinf48b2362011-08-30 10:46:09 -0700102};
103
104struct charger {
105 int64_t next_screen_transition;
106 int64_t next_key_check;
107 int64_t next_pwr_check;
Dima Zavinf48b2362011-08-30 10:46:09 -0700108
109 struct key_state keys[KEY_MAX + 1];
Dima Zavinf48b2362011-08-30 10:46:09 -0700110 int uevent_fd;
111
112 struct listnode supplies;
113 int num_supplies;
114 int num_supplies_online;
115
Dima Zavin0052abd2011-09-22 15:49:04 -0700116 struct animation *batt_anim;
117 gr_surface surf_unknown;
118
Dima Zavinf48b2362011-08-30 10:46:09 -0700119 struct power_supply *battery;
120};
121
122struct uevent {
123 const char *action;
124 const char *path;
125 const char *subsystem;
126 const char *ps_name;
127 const char *ps_type;
128 const char *ps_online;
129};
130
Dima Zavin0052abd2011-09-22 15:49:04 -0700131static struct frame batt_anim_frames[] = {
132 {
133 .name = "charger/battery_0",
134 .disp_time = 750,
135 .min_capacity = 0,
136 },
137 {
138 .name = "charger/battery_1",
139 .disp_time = 750,
140 .min_capacity = 20,
141 },
142 {
143 .name = "charger/battery_2",
144 .disp_time = 750,
145 .min_capacity = 40,
146 },
147 {
148 .name = "charger/battery_3",
149 .disp_time = 750,
150 .min_capacity = 60,
151 },
152 {
153 .name = "charger/battery_4",
154 .disp_time = 750,
155 .min_capacity = 80,
156 },
Dima Zavin1a5ca612011-09-26 14:14:19 -0700157 {
158 .name = "charger/battery_5",
159 .disp_time = 750,
160 .min_capacity = BATTERY_FULL_THRESH,
161 },
Dima Zavin0052abd2011-09-22 15:49:04 -0700162};
163
164static struct animation battery_animation = {
165 .frames = batt_anim_frames,
166 .num_frames = ARRAY_SIZE(batt_anim_frames),
167 .num_cycles = 3,
168};
169
170static struct charger charger_state = {
171 .batt_anim = &battery_animation,
172};
173
Dima Zavinf48b2362011-08-30 10:46:09 -0700174static int char_width;
175static int char_height;
176
Dima Zavinf48b2362011-08-30 10:46:09 -0700177/* current time in milliseconds */
178static int64_t curr_time_ms(void)
179{
180 struct timespec tm;
181 clock_gettime(CLOCK_MONOTONIC, &tm);
182 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
183}
184
185static void clear_screen(void)
186{
187 gr_color(0, 0, 0, 255);
188 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
189};
190
191static int read_file(const char *path, char *buf, size_t sz)
192{
193 int fd;
194 size_t cnt;
195
196 fd = open(path, O_RDONLY, 0);
197 if (fd < 0)
198 goto err;
199
200 cnt = read(fd, buf, sz - 1);
201 if (cnt <= 0)
202 goto err;
203 buf[cnt] = '\0';
204 if (buf[cnt - 1] == '\n') {
205 cnt--;
206 buf[cnt] = '\0';
207 }
208
209 close(fd);
210 return cnt;
211
212err:
213 if (fd >= 0)
214 close(fd);
215 return -1;
216}
217
218static int read_file_int(const char *path, int *val)
219{
220 char buf[32];
221 int ret;
222 int tmp;
223 char *end;
224
225 ret = read_file(path, buf, sizeof(buf));
226 if (ret < 0)
227 return -1;
228
229 tmp = strtol(buf, &end, 0);
230 if (end == buf ||
231 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
232 goto err;
233
234 *val = tmp;
235 return 0;
236
237err:
238 return -1;
239}
240
Dima Zavin1a5ca612011-09-26 14:14:19 -0700241static int get_battery_capacity(struct charger *charger)
242{
243 int ret;
244 int batt_cap = -1;
245
246 if (!charger->battery)
247 return -1;
248
249 ret = read_file_int(charger->battery->cap_path, &batt_cap);
250 if (ret < 0 || batt_cap > 100) {
251 batt_cap = -1;
252 }
253
254 return batt_cap;
255}
256
Dima Zavinf48b2362011-08-30 10:46:09 -0700257static struct power_supply *find_supply(struct charger *charger,
258 const char *name)
259{
260 struct listnode *node;
261 struct power_supply *supply;
262
263 list_for_each(node, &charger->supplies) {
264 supply = node_to_item(node, struct power_supply, list);
265 if (!strncmp(name, supply->name, sizeof(supply->name)))
266 return supply;
267 }
268 return NULL;
269}
270
271static struct power_supply *add_supply(struct charger *charger,
272 const char *name, const char *type,
Dima Zavin0052abd2011-09-22 15:49:04 -0700273 const char *path, bool online)
Dima Zavinf48b2362011-08-30 10:46:09 -0700274{
275 struct power_supply *supply;
276
277 supply = calloc(1, sizeof(struct power_supply));
278 if (!supply)
279 return NULL;
280
281 strlcpy(supply->name, name, sizeof(supply->name));
282 strlcpy(supply->type, type, sizeof(supply->type));
Dima Zavin0052abd2011-09-22 15:49:04 -0700283 snprintf(supply->cap_path, sizeof(supply->cap_path),
284 "/sys/%s/capacity", path);
Dima Zavinf48b2362011-08-30 10:46:09 -0700285 supply->online = online;
286 list_add_tail(&charger->supplies, &supply->list);
287 charger->num_supplies++;
288 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
289 return supply;
290}
291
292static void remove_supply(struct charger *charger, struct power_supply *supply)
293{
294 if (!supply)
295 return;
296 list_remove(&supply->list);
297 charger->num_supplies--;
298 free(supply);
299}
300
301static void parse_uevent(const char *msg, struct uevent *uevent)
302{
303 uevent->action = "";
304 uevent->path = "";
305 uevent->subsystem = "";
306 uevent->ps_name = "";
307 uevent->ps_online = "";
308 uevent->ps_type = "";
309
310 /* currently ignoring SEQNUM */
311 while (*msg) {
312#ifdef DEBUG_UEVENTS
313 LOGV("uevent str: %s\n", msg);
314#endif
315 if (!strncmp(msg, "ACTION=", 7)) {
316 msg += 7;
317 uevent->action = msg;
318 } else if (!strncmp(msg, "DEVPATH=", 8)) {
319 msg += 8;
320 uevent->path = msg;
321 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
322 msg += 10;
323 uevent->subsystem = msg;
324 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
325 msg += 18;
326 uevent->ps_name = msg;
327 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
328 msg += 20;
329 uevent->ps_online = msg;
330 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
331 msg += 18;
332 uevent->ps_type = msg;
333 }
334
335 /* advance to after the next \0 */
336 while (*msg++)
337 ;
338 }
339
340 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
341 uevent->action, uevent->path, uevent->subsystem,
342 uevent->ps_name, uevent->ps_type, uevent->ps_online);
343}
344
345static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
346{
347 int online;
348 char ps_type[32];
349 struct power_supply *supply = NULL;
350 int i;
351 bool was_online = false;
352 bool battery = false;
353
354 if (uevent->ps_type[0] == '\0') {
355 char *path;
356 int ret;
357
358 if (uevent->path[0] == '\0')
359 return;
360 ret = asprintf(&path, "/sys/%s/type", uevent->path);
361 if (ret <= 0)
362 return;
363 ret = read_file(path, ps_type, sizeof(ps_type));
364 free(path);
365 if (ret < 0)
366 return;
367 } else {
368 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
369 }
370
371 if (!strncmp(ps_type, "Battery", 7))
372 battery = true;
373
374 online = atoi(uevent->ps_online);
375 supply = find_supply(charger, uevent->ps_name);
376 if (supply) {
377 was_online = supply->online;
378 supply->online = online;
379 }
380
381 if (!strcmp(uevent->action, "add")) {
382 if (!supply) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700383 supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
384 online);
Dima Zavinf48b2362011-08-30 10:46:09 -0700385 if (!supply) {
386 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
387 uevent->ps_type, online);
388 return;
389 }
390 /* only pick up the first battery for now */
391 if (battery && !charger->battery)
392 charger->battery = supply;
393 } else {
394 LOGE("supply '%s' already exists..\n", uevent->ps_name);
395 }
396 } else if (!strcmp(uevent->action, "remove")) {
397 if (supply) {
398 if (charger->battery == supply)
399 charger->battery = NULL;
400 remove_supply(charger, supply);
401 supply = NULL;
402 }
403 } else if (!strcmp(uevent->action, "change")) {
404 if (!supply) {
405 LOGE("power supply '%s' not found ('%s' %d)\n",
406 uevent->ps_name, ps_type, online);
407 return;
408 }
409 } else {
410 return;
411 }
412
413 /* allow battery to be managed in the supply list but make it not
414 * contribute to online power supplies. */
415 if (!battery) {
416 if (was_online && !online)
417 charger->num_supplies_online--;
418 else if (supply && !was_online && online)
419 charger->num_supplies_online++;
420 }
421
422 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
423 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
424 uevent->action, charger->num_supplies_online, charger->num_supplies);
425}
426
427static void process_uevent(struct charger *charger, struct uevent *uevent)
428{
429 if (!strcmp(uevent->subsystem, "power_supply"))
430 process_ps_uevent(charger, uevent);
431}
432
433#define UEVENT_MSG_LEN 1024
434static int handle_uevent_fd(struct charger *charger, int fd)
435{
436 char msg[UEVENT_MSG_LEN+2];
437 int n;
438
439 if (fd < 0)
440 return -1;
441
442 while (true) {
443 struct uevent uevent;
444
445 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
446 if (n <= 0)
447 break;
448 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
449 continue;
450
451 msg[n] = '\0';
452 msg[n+1] = '\0';
453
454 parse_uevent(msg, &uevent);
455 process_uevent(charger, &uevent);
456 }
457
458 return 0;
459}
460
461static int uevent_callback(int fd, short revents, void *data)
462{
463 struct charger *charger = data;
464
465 if (!(revents & POLLIN))
466 return -1;
467 return handle_uevent_fd(charger, fd);
468}
469
470/* force the kernel to regenerate the change events for the existing
471 * devices, if valid */
472static void do_coldboot(struct charger *charger, DIR *d, const char *event,
473 bool follow_links, int max_depth)
474{
475 struct dirent *de;
476 int dfd, fd;
477
478 dfd = dirfd(d);
479
480 fd = openat(dfd, "uevent", O_WRONLY);
481 if (fd >= 0) {
482 write(fd, event, strlen(event));
483 close(fd);
484 handle_uevent_fd(charger, charger->uevent_fd);
485 }
486
487 while ((de = readdir(d)) && max_depth > 0) {
488 DIR *d2;
489
490 LOGV("looking at '%s'\n", de->d_name);
491
492 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
493 de->d_name[0] == '.') {
494 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
495 de->d_name, de->d_type, max_depth, follow_links);
496 continue;
497 }
498 LOGV("can descend into '%s'\n", de->d_name);
499
500 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
501 if (fd < 0) {
502 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
503 errno, strerror(errno));
504 continue;
505 }
506
507 d2 = fdopendir(fd);
508 if (d2 == 0)
509 close(fd);
510 else {
511 LOGV("opened '%s'\n", de->d_name);
512 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
513 closedir(d2);
514 }
515 }
516}
517
518static void coldboot(struct charger *charger, const char *path,
519 const char *event)
520{
521 char str[256];
522
523 LOGV("doing coldboot '%s' in '%s'\n", event, path);
524 DIR *d = opendir(path);
525 if (d) {
526 snprintf(str, sizeof(str), "%s\n", event);
527 do_coldboot(charger, d, str, true, 1);
528 closedir(d);
529 }
530}
531
532static int draw_text(const char *str, int x, int y)
533{
534 int str_len_px = gr_measure(str);
535
536 if (x < 0)
537 x = (gr_fb_width() - str_len_px) / 2;
538 if (y < 0)
539 y = (gr_fb_height() - char_height) / 2;
540 gr_text(x, y, str);
541
542 return y + char_height;
543}
544
545static void android_green(void)
546{
547 gr_color(0xa4, 0xc6, 0x39, 255);
548}
549
Dima Zavin0052abd2011-09-22 15:49:04 -0700550/* returns the last y-offset of where the surface ends */
551static int draw_surface_centered(struct charger *charger, gr_surface surface)
Dima Zavinf48b2362011-08-30 10:46:09 -0700552{
Dima Zavin0052abd2011-09-22 15:49:04 -0700553 int w;
554 int h;
Dima Zavinf48b2362011-08-30 10:46:09 -0700555 int x;
Dima Zavin0052abd2011-09-22 15:49:04 -0700556 int y;
Dima Zavinf48b2362011-08-30 10:46:09 -0700557
Dima Zavin0052abd2011-09-22 15:49:04 -0700558 w = gr_get_width(surface);
559 h = gr_get_height(surface);
560 x = (gr_fb_width() - w) / 2 ;
561 y = (gr_fb_height() - h) / 2 ;
Dima Zavinf48b2362011-08-30 10:46:09 -0700562
Dima Zavin0052abd2011-09-22 15:49:04 -0700563 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
564 gr_blit(surface, 0, 0, w, h, x, y);
565 return y + h;
566}
Dima Zavinf48b2362011-08-30 10:46:09 -0700567
Dima Zavin0052abd2011-09-22 15:49:04 -0700568static void draw_unknown(struct charger *charger)
569{
570 int y;
571 if (charger->surf_unknown) {
572 draw_surface_centered(charger, charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700573 } else {
574 android_green();
575 y = draw_text("Charging!", -1, -1);
Dima Zavin0052abd2011-09-22 15:49:04 -0700576 draw_text("?\?/100", -1, y + 25);
Dima Zavinf48b2362011-08-30 10:46:09 -0700577 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700578}
Dima Zavinf48b2362011-08-30 10:46:09 -0700579
Dima Zavin0052abd2011-09-22 15:49:04 -0700580static void draw_battery(struct charger *charger)
581{
582 struct animation *batt_anim = charger->batt_anim;
583 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
584
585 if (batt_anim->num_frames != 0) {
586 draw_surface_centered(charger, frame->surface);
587 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
588 batt_anim->cur_frame, frame->name, frame->min_capacity,
589 frame->disp_time);
Dima Zavinf48b2362011-08-30 10:46:09 -0700590 }
Dima Zavin0052abd2011-09-22 15:49:04 -0700591}
Dima Zavinf48b2362011-08-30 10:46:09 -0700592
Dima Zavin0052abd2011-09-22 15:49:04 -0700593static void redraw_screen(struct charger *charger)
594{
595 struct animation *batt_anim = charger->batt_anim;
Dima Zavinf48b2362011-08-30 10:46:09 -0700596
Dima Zavin0052abd2011-09-22 15:49:04 -0700597 clear_screen();
Dima Zavinf48b2362011-08-30 10:46:09 -0700598
Dima Zavin0052abd2011-09-22 15:49:04 -0700599 /* try to display *something* */
600 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
601 draw_unknown(charger);
602 else
603 draw_battery(charger);
Dima Zavinf48b2362011-08-30 10:46:09 -0700604 gr_flip();
605}
606
Dima Zavin0052abd2011-09-22 15:49:04 -0700607static void kick_animation(struct animation *anim)
Dima Zavinf48b2362011-08-30 10:46:09 -0700608{
Dima Zavin0052abd2011-09-22 15:49:04 -0700609 anim->run = true;
610}
611
612static void reset_animation(struct animation *anim)
613{
614 anim->cur_cycle = 0;
615 anim->cur_frame = 0;
616 anim->run = false;
617}
618
619static void update_screen_state(struct charger *charger, int64_t now)
620{
621 struct animation *batt_anim = charger->batt_anim;
622 int cur_frame;
623 int disp_time;
624
625 if (!batt_anim->run || now < charger->next_screen_transition)
Dima Zavinf48b2362011-08-30 10:46:09 -0700626 return;
627
Dima Zavin0052abd2011-09-22 15:49:04 -0700628 /* animation is over, blank screen and leave */
629 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
630 reset_animation(batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700631 charger->next_screen_transition = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700632 gr_fb_blank(true);
633 LOGV("[%lld] animation done\n", now);
634 return;
635 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700636
Dima Zavin0052abd2011-09-22 15:49:04 -0700637 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
638
639 /* animation starting, set up the animation */
640 if (batt_anim->cur_frame == 0) {
641 int batt_cap;
642 int ret;
643
644 LOGV("[%lld] animation starting\n", now);
Dima Zavin1a5ca612011-09-26 14:14:19 -0700645 batt_cap = get_battery_capacity(charger);
646 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
Dima Zavin0052abd2011-09-22 15:49:04 -0700647 int i;
648
649 /* find first frame given current capacity */
650 for (i = 1; i < batt_anim->num_frames; i++) {
651 if (batt_cap < batt_anim->frames[i].min_capacity)
652 break;
653 }
654 batt_anim->cur_frame = i - 1;
655
656 /* show the first frame for twice as long */
657 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
658 }
659
660 batt_anim->capacity = batt_cap;
661 }
662
663 /* unblank the screen on first cycle */
664 if (batt_anim->cur_cycle == 0)
665 gr_fb_blank(false);
666
667 /* draw the new frame (@ cur_frame) */
668 redraw_screen(charger);
669
670 /* if we don't have anim frames, we only have one image, so just bump
671 * the cycle counter and exit
672 */
673 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
674 LOGV("[%lld] animation missing or unknown battery status\n", now);
675 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
676 batt_anim->cur_cycle++;
677 return;
678 }
679
680 /* schedule next screen transition */
681 charger->next_screen_transition = now + disp_time;
682
683 /* advance frame cntr to the next valid frame
684 * if necessary, advance cycle cntr, and reset frame cntr
685 */
686 batt_anim->cur_frame++;
687 if (batt_anim->cur_frame == batt_anim->num_frames) {
688 batt_anim->cur_cycle++;
689 batt_anim->cur_frame = 0;
690
691 /* don't reset the cycle counter, since we use that as a signal
692 * in a test above to check if animation is over
693 */
694 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700695}
696
697static void update_input_state(struct charger *charger,
698 struct input_event *ev,
699 int64_t now)
700{
701 int down = !!ev->value;
702
703 if (ev->type != EV_KEY || ev->code > KEY_MAX)
704 return;
705
706 /* only record the down even timestamp, as the amount
707 * of time the key spent not being pressed is not useful */
708 if (down)
709 charger->keys[ev->code].timestamp = now;
710 charger->keys[ev->code].down = down;
711 charger->keys[ev->code].pending = true;
712 if (down) {
713 LOGV("[%lld] key[%d] down\n", now, ev->code);
714 } else {
715 int64_t duration = now - charger->keys[ev->code].timestamp;
716 int64_t secs = duration / 1000;
717 int64_t msecs = duration - secs * 1000;
718 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
719 ev->code, secs, msecs);
720 }
721}
722
723static void set_next_key_check(struct charger *charger,
724 struct key_state *key,
725 int64_t timeout)
726{
727 int64_t then = key->timestamp + timeout;
728
729 if (charger->next_key_check == -1 || then < charger->next_key_check)
730 charger->next_key_check = then;
731}
732
733static void process_key(struct charger *charger, int code, int64_t now)
734{
735 struct key_state *key = &charger->keys[code];
736 int64_t next_key_check;
737
738 if (code == KEY_POWER) {
739 if (key->down) {
740 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
741 if (now >= reboot_timeout) {
742 LOGI("[%lld] rebooting\n", now);
743 android_reboot(ANDROID_RB_RESTART, 0, 0);
744 } else {
745 /* if the key is pressed but timeout hasn't expired,
746 * make sure we wake up at the right-ish time to check
747 */
748 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
749 }
750 } else {
751 /* if the power key got released, force screen state cycle */
752 if (key->pending)
Dima Zavin0052abd2011-09-22 15:49:04 -0700753 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700754 }
755 }
756
757 key->pending = false;
758}
759
760static void handle_input_state(struct charger *charger, int64_t now)
761{
762 process_key(charger, KEY_POWER, now);
763
764 if (charger->next_key_check != -1 && now > charger->next_key_check)
765 charger->next_key_check = -1;
766}
767
768static void handle_power_supply_state(struct charger *charger, int64_t now)
769{
770 if (charger->num_supplies_online == 0) {
771 if (charger->next_pwr_check == -1) {
772 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
Dima Zavine0c70da2011-09-22 15:49:04 -0700773 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700774 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
775 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
776 } else if (now >= charger->next_pwr_check) {
777 LOGI("[%lld] shutting down\n", now);
778 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
779 } else {
780 /* otherwise we already have a shutdown timer scheduled */
781 }
782 } else {
783 /* online supply present, reset shutdown timer if set */
784 if (charger->next_pwr_check != -1) {
785 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
Dima Zavin0052abd2011-09-22 15:49:04 -0700786 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700787 }
788 charger->next_pwr_check = -1;
789 }
790}
791
792static void wait_next_event(struct charger *charger, int64_t now)
793{
794 int64_t next_event = INT64_MAX;
795 int64_t timeout;
796 struct input_event ev;
797 int ret;
798
799 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
800 charger->next_screen_transition, charger->next_key_check,
801 charger->next_pwr_check);
802
Dima Zavinf48b2362011-08-30 10:46:09 -0700803 if (charger->next_screen_transition != -1)
804 next_event = charger->next_screen_transition;
805 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
806 next_event = charger->next_key_check;
807 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
808 next_event = charger->next_pwr_check;
809
810 if (next_event != -1 && next_event != INT64_MAX)
811 timeout = max(0, next_event - now);
812 else
813 timeout = -1;
814 LOGV("[%lld] blocking (%lld)\n", now, timeout);
815 ret = ev_wait((int)timeout);
816 if (!ret)
817 ev_dispatch();
818}
819
820static int input_callback(int fd, short revents, void *data)
821{
822 struct charger *charger = data;
823 struct input_event ev;
824 int ret;
825
826 ret = ev_get_input(fd, revents, &ev);
827 if (ret)
828 return -1;
829 update_input_state(charger, &ev, curr_time_ms());
830 return 0;
831}
832
833static void event_loop(struct charger *charger)
834{
835 int ret;
836
837 while (true) {
838 int64_t now = curr_time_ms();
839
840 LOGV("[%lld] event_loop()\n", now);
841 handle_input_state(charger, now);
Dima Zavinf48b2362011-08-30 10:46:09 -0700842 handle_power_supply_state(charger, now);
843
Dima Zavin0052abd2011-09-22 15:49:04 -0700844 /* do screen update last in case any of the above want to start
845 * screen transitions (animations, etc)
846 */
847 update_screen_state(charger, now);
848
Dima Zavinf48b2362011-08-30 10:46:09 -0700849 wait_next_event(charger, now);
850 }
851}
852
853int main(int argc, char **argv)
854{
855 int ret;
856 struct charger *charger = &charger_state;
857 int64_t now = curr_time_ms() - 1;
858 int fd;
Dima Zavin0052abd2011-09-22 15:49:04 -0700859 int i;
Dima Zavinf48b2362011-08-30 10:46:09 -0700860
861 list_init(&charger->supplies);
862
863 klog_init();
864 klog_set_level(CHARGER_KLOG_LEVEL);
865
866 gr_init();
867 gr_font_size(&char_width, &char_height);
868
869 ev_init(input_callback, charger);
870
871 fd = uevent_open_socket(64*1024, true);
872 if (fd >= 0) {
873 fcntl(fd, F_SETFL, O_NONBLOCK);
874 ev_add_fd(fd, uevent_callback, charger);
875 }
876 charger->uevent_fd = fd;
877 coldboot(charger, "/sys/class/power_supply", "add");
878
Dima Zavin0052abd2011-09-22 15:49:04 -0700879 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
Dima Zavinf48b2362011-08-30 10:46:09 -0700880 if (ret < 0) {
881 LOGE("Cannot load image\n");
Dima Zavin0052abd2011-09-22 15:49:04 -0700882 charger->surf_unknown = NULL;
883 }
884
885 for (i = 0; i < charger->batt_anim->num_frames; i++) {
886 struct frame *frame = &charger->batt_anim->frames[i];
887
888 ret = res_create_surface(frame->name, &frame->surface);
889 if (ret < 0) {
890 LOGE("Cannot load image %s\n", frame->name);
891 /* TODO: free the already allocated surfaces... */
892 charger->batt_anim->num_frames = 0;
893 charger->batt_anim->num_cycles = 1;
894 break;
895 }
Dima Zavinf48b2362011-08-30 10:46:09 -0700896 }
897
898 gr_fb_blank(true);
899
900 charger->next_screen_transition = now - 1;
901 charger->next_key_check = -1;
902 charger->next_pwr_check = -1;
Dima Zavin0052abd2011-09-22 15:49:04 -0700903 reset_animation(charger->batt_anim);
904 kick_animation(charger->batt_anim);
Dima Zavinf48b2362011-08-30 10:46:09 -0700905
906 event_loop(charger);
907
908 return 0;
909}