blob: 9320e8a552f991e0c226434795004d1294c918e4 [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
52#define MSEC_PER_SEC (1000LL)
53#define NSEC_PER_MSEC (1000000LL)
54
55#define SCREEN_ON_TIME (5 * MSEC_PER_SEC)
Dima Zavin92312a52011-09-16 13:15:47 -070056#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Dima Zavinf48b2362011-08-30 10:46:09 -070057#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
58
59#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
60#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
61#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
62
63struct key_state {
64 bool pending;
65 bool down;
66 int64_t timestamp;
67};
68
69struct power_supply {
70 struct listnode list;
71 char name[256];
72 char type[32];
73 bool online;
74 bool valid;
75};
76
77struct charger {
78 int64_t next_screen_transition;
79 int64_t next_key_check;
80 int64_t next_pwr_check;
81 bool screen_on;
82
83 struct key_state keys[KEY_MAX + 1];
84 gr_surface surf_charging;
85 int uevent_fd;
86
87 struct listnode supplies;
88 int num_supplies;
89 int num_supplies_online;
90
91 struct power_supply *battery;
92};
93
94struct uevent {
95 const char *action;
96 const char *path;
97 const char *subsystem;
98 const char *ps_name;
99 const char *ps_type;
100 const char *ps_online;
101};
102
103static int char_width;
104static int char_height;
105
106struct charger charger_state;
107
108/* current time in milliseconds */
109static int64_t curr_time_ms(void)
110{
111 struct timespec tm;
112 clock_gettime(CLOCK_MONOTONIC, &tm);
113 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
114}
115
116static void clear_screen(void)
117{
118 gr_color(0, 0, 0, 255);
119 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
120};
121
122static int read_file(const char *path, char *buf, size_t sz)
123{
124 int fd;
125 size_t cnt;
126
127 fd = open(path, O_RDONLY, 0);
128 if (fd < 0)
129 goto err;
130
131 cnt = read(fd, buf, sz - 1);
132 if (cnt <= 0)
133 goto err;
134 buf[cnt] = '\0';
135 if (buf[cnt - 1] == '\n') {
136 cnt--;
137 buf[cnt] = '\0';
138 }
139
140 close(fd);
141 return cnt;
142
143err:
144 if (fd >= 0)
145 close(fd);
146 return -1;
147}
148
149static int read_file_int(const char *path, int *val)
150{
151 char buf[32];
152 int ret;
153 int tmp;
154 char *end;
155
156 ret = read_file(path, buf, sizeof(buf));
157 if (ret < 0)
158 return -1;
159
160 tmp = strtol(buf, &end, 0);
161 if (end == buf ||
162 ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
163 goto err;
164
165 *val = tmp;
166 return 0;
167
168err:
169 return -1;
170}
171
172static struct power_supply *find_supply(struct charger *charger,
173 const char *name)
174{
175 struct listnode *node;
176 struct power_supply *supply;
177
178 list_for_each(node, &charger->supplies) {
179 supply = node_to_item(node, struct power_supply, list);
180 if (!strncmp(name, supply->name, sizeof(supply->name)))
181 return supply;
182 }
183 return NULL;
184}
185
186static struct power_supply *add_supply(struct charger *charger,
187 const char *name, const char *type,
188 bool online)
189{
190 struct power_supply *supply;
191
192 supply = calloc(1, sizeof(struct power_supply));
193 if (!supply)
194 return NULL;
195
196 strlcpy(supply->name, name, sizeof(supply->name));
197 strlcpy(supply->type, type, sizeof(supply->type));
198 supply->online = online;
199 list_add_tail(&charger->supplies, &supply->list);
200 charger->num_supplies++;
201 LOGV("... added %s %s %d\n", supply->name, supply->type, online);
202 return supply;
203}
204
205static void remove_supply(struct charger *charger, struct power_supply *supply)
206{
207 if (!supply)
208 return;
209 list_remove(&supply->list);
210 charger->num_supplies--;
211 free(supply);
212}
213
214static void parse_uevent(const char *msg, struct uevent *uevent)
215{
216 uevent->action = "";
217 uevent->path = "";
218 uevent->subsystem = "";
219 uevent->ps_name = "";
220 uevent->ps_online = "";
221 uevent->ps_type = "";
222
223 /* currently ignoring SEQNUM */
224 while (*msg) {
225#ifdef DEBUG_UEVENTS
226 LOGV("uevent str: %s\n", msg);
227#endif
228 if (!strncmp(msg, "ACTION=", 7)) {
229 msg += 7;
230 uevent->action = msg;
231 } else if (!strncmp(msg, "DEVPATH=", 8)) {
232 msg += 8;
233 uevent->path = msg;
234 } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
235 msg += 10;
236 uevent->subsystem = msg;
237 } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
238 msg += 18;
239 uevent->ps_name = msg;
240 } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
241 msg += 20;
242 uevent->ps_online = msg;
243 } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
244 msg += 18;
245 uevent->ps_type = msg;
246 }
247
248 /* advance to after the next \0 */
249 while (*msg++)
250 ;
251 }
252
253 LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
254 uevent->action, uevent->path, uevent->subsystem,
255 uevent->ps_name, uevent->ps_type, uevent->ps_online);
256}
257
258static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
259{
260 int online;
261 char ps_type[32];
262 struct power_supply *supply = NULL;
263 int i;
264 bool was_online = false;
265 bool battery = false;
266
267 if (uevent->ps_type[0] == '\0') {
268 char *path;
269 int ret;
270
271 if (uevent->path[0] == '\0')
272 return;
273 ret = asprintf(&path, "/sys/%s/type", uevent->path);
274 if (ret <= 0)
275 return;
276 ret = read_file(path, ps_type, sizeof(ps_type));
277 free(path);
278 if (ret < 0)
279 return;
280 } else {
281 strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
282 }
283
284 if (!strncmp(ps_type, "Battery", 7))
285 battery = true;
286
287 online = atoi(uevent->ps_online);
288 supply = find_supply(charger, uevent->ps_name);
289 if (supply) {
290 was_online = supply->online;
291 supply->online = online;
292 }
293
294 if (!strcmp(uevent->action, "add")) {
295 if (!supply) {
296 supply = add_supply(charger, uevent->ps_name, ps_type, online);
297 if (!supply) {
298 LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
299 uevent->ps_type, online);
300 return;
301 }
302 /* only pick up the first battery for now */
303 if (battery && !charger->battery)
304 charger->battery = supply;
305 } else {
306 LOGE("supply '%s' already exists..\n", uevent->ps_name);
307 }
308 } else if (!strcmp(uevent->action, "remove")) {
309 if (supply) {
310 if (charger->battery == supply)
311 charger->battery = NULL;
312 remove_supply(charger, supply);
313 supply = NULL;
314 }
315 } else if (!strcmp(uevent->action, "change")) {
316 if (!supply) {
317 LOGE("power supply '%s' not found ('%s' %d)\n",
318 uevent->ps_name, ps_type, online);
319 return;
320 }
321 } else {
322 return;
323 }
324
325 /* allow battery to be managed in the supply list but make it not
326 * contribute to online power supplies. */
327 if (!battery) {
328 if (was_online && !online)
329 charger->num_supplies_online--;
330 else if (supply && !was_online && online)
331 charger->num_supplies_online++;
332 }
333
334 LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
335 uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
336 uevent->action, charger->num_supplies_online, charger->num_supplies);
337}
338
339static void process_uevent(struct charger *charger, struct uevent *uevent)
340{
341 if (!strcmp(uevent->subsystem, "power_supply"))
342 process_ps_uevent(charger, uevent);
343}
344
345#define UEVENT_MSG_LEN 1024
346static int handle_uevent_fd(struct charger *charger, int fd)
347{
348 char msg[UEVENT_MSG_LEN+2];
349 int n;
350
351 if (fd < 0)
352 return -1;
353
354 while (true) {
355 struct uevent uevent;
356
357 n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
358 if (n <= 0)
359 break;
360 if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
361 continue;
362
363 msg[n] = '\0';
364 msg[n+1] = '\0';
365
366 parse_uevent(msg, &uevent);
367 process_uevent(charger, &uevent);
368 }
369
370 return 0;
371}
372
373static int uevent_callback(int fd, short revents, void *data)
374{
375 struct charger *charger = data;
376
377 if (!(revents & POLLIN))
378 return -1;
379 return handle_uevent_fd(charger, fd);
380}
381
382/* force the kernel to regenerate the change events for the existing
383 * devices, if valid */
384static void do_coldboot(struct charger *charger, DIR *d, const char *event,
385 bool follow_links, int max_depth)
386{
387 struct dirent *de;
388 int dfd, fd;
389
390 dfd = dirfd(d);
391
392 fd = openat(dfd, "uevent", O_WRONLY);
393 if (fd >= 0) {
394 write(fd, event, strlen(event));
395 close(fd);
396 handle_uevent_fd(charger, charger->uevent_fd);
397 }
398
399 while ((de = readdir(d)) && max_depth > 0) {
400 DIR *d2;
401
402 LOGV("looking at '%s'\n", de->d_name);
403
404 if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
405 de->d_name[0] == '.') {
406 LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
407 de->d_name, de->d_type, max_depth, follow_links);
408 continue;
409 }
410 LOGV("can descend into '%s'\n", de->d_name);
411
412 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
413 if (fd < 0) {
414 LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
415 errno, strerror(errno));
416 continue;
417 }
418
419 d2 = fdopendir(fd);
420 if (d2 == 0)
421 close(fd);
422 else {
423 LOGV("opened '%s'\n", de->d_name);
424 do_coldboot(charger, d2, event, follow_links, max_depth - 1);
425 closedir(d2);
426 }
427 }
428}
429
430static void coldboot(struct charger *charger, const char *path,
431 const char *event)
432{
433 char str[256];
434
435 LOGV("doing coldboot '%s' in '%s'\n", event, path);
436 DIR *d = opendir(path);
437 if (d) {
438 snprintf(str, sizeof(str), "%s\n", event);
439 do_coldboot(charger, d, str, true, 1);
440 closedir(d);
441 }
442}
443
444static int draw_text(const char *str, int x, int y)
445{
446 int str_len_px = gr_measure(str);
447
448 if (x < 0)
449 x = (gr_fb_width() - str_len_px) / 2;
450 if (y < 0)
451 y = (gr_fb_height() - char_height) / 2;
452 gr_text(x, y, str);
453
454 return y + char_height;
455}
456
457static void android_green(void)
458{
459 gr_color(0xa4, 0xc6, 0x39, 255);
460}
461
462static void redraw_screen(struct charger *charger)
463{
464 int surf_height;
465 int surf_width;
466 int x;
467 int y = 0;
468 int batt_cap;
469 int ret;
470 char cap_string[128];
471 char cap_path[256];
472
473 clear_screen();
474
475 if (charger->surf_charging) {
476 surf_width = gr_get_width(charger->surf_charging);
477 surf_height = gr_get_height(charger->surf_charging);
478 x = (gr_fb_width() - surf_width) / 2 ;
479 y = (gr_fb_height() - surf_height) / 2 ;
480
481 gr_blit(charger->surf_charging, 0, 0,
482 surf_width, surf_height,
483 x, y);
484 y += surf_height;
485 } else {
486 android_green();
487 y = draw_text("Charging!", -1, -1);
488 }
489
490 cap_string[0] = '\0';
491 if (charger->battery) {
492 ret = snprintf(cap_path, sizeof(cap_path),
493 "/sys/class/power_supply/%s/capacity",
494 charger->battery->name);
495 if (ret <= 0)
496 goto done;
497 ret = read_file_int(cap_path, &batt_cap);
498 if (ret >= 0)
499 snprintf(cap_string, sizeof(cap_string), "%d/100", batt_cap);
500 }
501
502 if (cap_string[0] == '\0')
503 snprintf(cap_string, sizeof(cap_string), "?\?/100");
504
505 y += 25;
506 android_green();
507 draw_text(cap_string, -1, y);
508
509done:
510 gr_flip();
511}
512
513static void update_screen_state(struct charger *charger, int64_t now,
514 bool force)
515{
516 if (!force && ((now < charger->next_screen_transition) ||
517 (charger->next_screen_transition == -1)))
518 return;
519
520 if (!charger->screen_on)
521 charger->next_screen_transition = now + SCREEN_ON_TIME;
522 else
523 charger->next_screen_transition = -1;
524 charger->screen_on = !charger->screen_on;
525
526 gr_fb_blank(!charger->screen_on);
527 if (charger->screen_on)
528 redraw_screen(charger);
529 LOGV("[%lld] screen %s\n", now, charger->screen_on ? "on" : "off");
530}
531
532static void update_input_state(struct charger *charger,
533 struct input_event *ev,
534 int64_t now)
535{
536 int down = !!ev->value;
537
538 if (ev->type != EV_KEY || ev->code > KEY_MAX)
539 return;
540
541 /* only record the down even timestamp, as the amount
542 * of time the key spent not being pressed is not useful */
543 if (down)
544 charger->keys[ev->code].timestamp = now;
545 charger->keys[ev->code].down = down;
546 charger->keys[ev->code].pending = true;
547 if (down) {
548 LOGV("[%lld] key[%d] down\n", now, ev->code);
549 } else {
550 int64_t duration = now - charger->keys[ev->code].timestamp;
551 int64_t secs = duration / 1000;
552 int64_t msecs = duration - secs * 1000;
553 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
554 ev->code, secs, msecs);
555 }
556}
557
558static void set_next_key_check(struct charger *charger,
559 struct key_state *key,
560 int64_t timeout)
561{
562 int64_t then = key->timestamp + timeout;
563
564 if (charger->next_key_check == -1 || then < charger->next_key_check)
565 charger->next_key_check = then;
566}
567
568static void process_key(struct charger *charger, int code, int64_t now)
569{
570 struct key_state *key = &charger->keys[code];
571 int64_t next_key_check;
572
573 if (code == KEY_POWER) {
574 if (key->down) {
575 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
576 if (now >= reboot_timeout) {
577 LOGI("[%lld] rebooting\n", now);
578 android_reboot(ANDROID_RB_RESTART, 0, 0);
579 } else {
580 /* if the key is pressed but timeout hasn't expired,
581 * make sure we wake up at the right-ish time to check
582 */
583 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
584 }
585 } else {
586 /* if the power key got released, force screen state cycle */
587 if (key->pending)
588 update_screen_state(charger, now, true);
589 }
590 }
591
592 key->pending = false;
593}
594
595static void handle_input_state(struct charger *charger, int64_t now)
596{
597 process_key(charger, KEY_POWER, now);
598
599 if (charger->next_key_check != -1 && now > charger->next_key_check)
600 charger->next_key_check = -1;
601}
602
603static void handle_power_supply_state(struct charger *charger, int64_t now)
604{
605 if (charger->num_supplies_online == 0) {
606 if (charger->next_pwr_check == -1) {
607 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
608 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
609 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
610 } else if (now >= charger->next_pwr_check) {
611 LOGI("[%lld] shutting down\n", now);
612 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
613 } else {
614 /* otherwise we already have a shutdown timer scheduled */
615 }
616 } else {
617 /* online supply present, reset shutdown timer if set */
618 if (charger->next_pwr_check != -1) {
619 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
620 update_screen_state(charger, now, true);
621 }
622 charger->next_pwr_check = -1;
623 }
624}
625
626static void wait_next_event(struct charger *charger, int64_t now)
627{
628 int64_t next_event = INT64_MAX;
629 int64_t timeout;
630 struct input_event ev;
631 int ret;
632
633 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
634 charger->next_screen_transition, charger->next_key_check,
635 charger->next_pwr_check);
636
637 /* TODO: right now it's just screen on/off and keys, but later I'm sure
638 * there will be animations */
639 if (charger->next_screen_transition != -1)
640 next_event = charger->next_screen_transition;
641 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
642 next_event = charger->next_key_check;
643 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
644 next_event = charger->next_pwr_check;
645
646 if (next_event != -1 && next_event != INT64_MAX)
647 timeout = max(0, next_event - now);
648 else
649 timeout = -1;
650 LOGV("[%lld] blocking (%lld)\n", now, timeout);
651 ret = ev_wait((int)timeout);
652 if (!ret)
653 ev_dispatch();
654}
655
656static int input_callback(int fd, short revents, void *data)
657{
658 struct charger *charger = data;
659 struct input_event ev;
660 int ret;
661
662 ret = ev_get_input(fd, revents, &ev);
663 if (ret)
664 return -1;
665 update_input_state(charger, &ev, curr_time_ms());
666 return 0;
667}
668
669static void event_loop(struct charger *charger)
670{
671 int ret;
672
673 while (true) {
674 int64_t now = curr_time_ms();
675
676 LOGV("[%lld] event_loop()\n", now);
677 handle_input_state(charger, now);
678 update_screen_state(charger, now, false);
679 handle_power_supply_state(charger, now);
680
681 wait_next_event(charger, now);
682 }
683}
684
685int main(int argc, char **argv)
686{
687 int ret;
688 struct charger *charger = &charger_state;
689 int64_t now = curr_time_ms() - 1;
690 int fd;
691
692 list_init(&charger->supplies);
693
694 klog_init();
695 klog_set_level(CHARGER_KLOG_LEVEL);
696
697 gr_init();
698 gr_font_size(&char_width, &char_height);
699
700 ev_init(input_callback, charger);
701
702 fd = uevent_open_socket(64*1024, true);
703 if (fd >= 0) {
704 fcntl(fd, F_SETFL, O_NONBLOCK);
705 ev_add_fd(fd, uevent_callback, charger);
706 }
707 charger->uevent_fd = fd;
708 coldboot(charger, "/sys/class/power_supply", "add");
709
710 ret = res_create_surface("charging", &charger->surf_charging);
711 if (ret < 0) {
712 LOGE("Cannot load image\n");
713 charger->surf_charging = NULL;
714 }
715
716 gr_fb_blank(true);
717
718 charger->next_screen_transition = now - 1;
719 charger->next_key_check = -1;
720 charger->next_pwr_check = -1;
721 charger->screen_on = false;
722
723 event_loop(charger);
724
725 return 0;
726}