blob: 025d53066f7eb5cbd1b82851b9aa7f47f1f7b0eb [file] [log] [blame]
Todd Poynorfea5b4d2013-09-09 12:09:08 -07001/*
2 * Copyright (C) 2011-2013 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#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <linux/input.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/epoll.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/un.h>
29#include <time.h>
30#include <unistd.h>
31
32#include <sys/socket.h>
33#include <linux/netlink.h>
34
35#include <batteryservice/BatteryService.h>
36#include <cutils/android_reboot.h>
37#include <cutils/klog.h>
38#include <cutils/misc.h>
39
40#ifdef CHARGER_ENABLE_SUSPEND
41#include <suspend/autosuspend.h>
42#endif
43
44#include "minui/minui.h"
45
46#include "healthd.h"
47
48#ifndef max
49#define max(a,b) ((a) > (b) ? (a) : (b))
50#endif
51
52#ifndef min
53#define min(a,b) ((a) < (b) ? (a) : (b))
54#endif
55
56#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
57
58#define MSEC_PER_SEC (1000LL)
59#define NSEC_PER_MSEC (1000000LL)
60
61#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
62#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
63#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
64
65#define BATTERY_FULL_THRESH 95
66
67#define LAST_KMSG_PATH "/proc/last_kmsg"
Todd Poynorcd7c1042013-11-22 17:52:59 -080068#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops"
Todd Poynorfea5b4d2013-09-09 12:09:08 -070069#define LAST_KMSG_MAX_SZ (32 * 1024)
70
71#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
72#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
73#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
74
75struct key_state {
76 bool pending;
77 bool down;
78 int64_t timestamp;
79};
80
81struct frame {
82 const char *name;
83 int disp_time;
84 int min_capacity;
85 bool level_only;
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;
102};
103
104struct charger {
105 bool have_battery_state;
106 bool charger_connected;
107 int capacity;
108 int64_t next_screen_transition;
109 int64_t next_key_check;
110 int64_t next_pwr_check;
111
112 struct key_state keys[KEY_MAX + 1];
113
114 struct animation *batt_anim;
115 gr_surface surf_unknown;
116};
117
118static struct frame batt_anim_frames[] = {
119 {
120 .name = "charger/battery_0",
121 .disp_time = 750,
122 .min_capacity = 0,
123 .level_only = false,
124 .surface = NULL,
125 },
126 {
127 .name = "charger/battery_1",
128 .disp_time = 750,
129 .min_capacity = 20,
130 .level_only = false,
131 .surface = NULL,
132 },
133 {
134 .name = "charger/battery_2",
135 .disp_time = 750,
136 .min_capacity = 40,
137 .level_only = false,
138 .surface = NULL,
139 },
140 {
141 .name = "charger/battery_3",
142 .disp_time = 750,
143 .min_capacity = 60,
144 .level_only = false,
145 .surface = NULL,
146 },
147 {
148 .name = "charger/battery_4",
149 .disp_time = 750,
150 .min_capacity = 80,
151 .level_only = true,
152 .surface = NULL,
153 },
154 {
155 .name = "charger/battery_5",
156 .disp_time = 750,
157 .min_capacity = BATTERY_FULL_THRESH,
158 .level_only = false,
159 .surface = NULL,
160 },
161};
162
163static struct animation battery_animation = {
164 .run = false,
165 .frames = batt_anim_frames,
166 .cur_frame = 0,
167 .num_frames = ARRAY_SIZE(batt_anim_frames),
168 .cur_cycle = 0,
169 .num_cycles = 3,
170 .capacity = 0,
171};
172
173static struct charger charger_state;
174
175static int char_width;
176static int char_height;
177
178/* current time in milliseconds */
179static int64_t curr_time_ms(void)
180{
181 struct timespec tm;
182 clock_gettime(CLOCK_MONOTONIC, &tm);
183 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
184}
185
186static void clear_screen(void)
187{
188 gr_color(0, 0, 0, 255);
189 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
190};
191
192#define MAX_KLOG_WRITE_BUF_SZ 256
193
194static void dump_last_kmsg(void)
195{
196 char *buf;
197 char *ptr;
198 unsigned sz = 0;
199 int len;
200
201 LOGI("\n");
202 LOGI("*************** LAST KMSG ***************\n");
203 LOGI("\n");
Todd Poynorcd7c1042013-11-22 17:52:59 -0800204 buf = (char *)load_file(LAST_KMSG_PSTORE_PATH, &sz);
205
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700206 if (!buf || !sz) {
Todd Poynorcd7c1042013-11-22 17:52:59 -0800207 buf = (char *)load_file(LAST_KMSG_PATH, &sz);
208 if (!buf || !sz) {
209 LOGI("last_kmsg not found. Cold reset?\n");
210 goto out;
211 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700212 }
213
214 len = min(sz, LAST_KMSG_MAX_SZ);
215 ptr = buf + (sz - len);
216
217 while (len > 0) {
218 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
219 char yoink;
220 char *nl;
221
222 nl = (char *)memrchr(ptr, '\n', cnt - 1);
223 if (nl)
224 cnt = nl - ptr + 1;
225
226 yoink = ptr[cnt];
227 ptr[cnt] = '\0';
228 klog_write(6, "<6>%s", ptr);
229 ptr[cnt] = yoink;
230
231 len -= cnt;
232 ptr += cnt;
233 }
234
235 free(buf);
236
237out:
238 LOGI("\n");
239 LOGI("************* END LAST KMSG *************\n");
240 LOGI("\n");
241}
242
243static int get_battery_capacity()
244{
245 return charger_state.capacity;
246}
247
248#ifdef CHARGER_ENABLE_SUSPEND
249static int request_suspend(bool enable)
250{
251 if (enable)
252 return autosuspend_enable();
253 else
254 return autosuspend_disable();
255}
256#else
257static int request_suspend(bool enable)
258{
259 return 0;
260}
261#endif
262
263static int draw_text(const char *str, int x, int y)
264{
265 int str_len_px = gr_measure(str);
266
267 if (x < 0)
268 x = (gr_fb_width() - str_len_px) / 2;
269 if (y < 0)
270 y = (gr_fb_height() - char_height) / 2;
271 gr_text(x, y, str, 0);
272
273 return y + char_height;
274}
275
276static void android_green(void)
277{
278 gr_color(0xa4, 0xc6, 0x39, 255);
279}
280
281/* returns the last y-offset of where the surface ends */
282static int draw_surface_centered(struct charger *charger, gr_surface surface)
283{
284 int w;
285 int h;
286 int x;
287 int y;
288
289 w = gr_get_width(surface);
290 h = gr_get_height(surface);
291 x = (gr_fb_width() - w) / 2 ;
292 y = (gr_fb_height() - h) / 2 ;
293
294 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
295 gr_blit(surface, 0, 0, w, h, x, y);
296 return y + h;
297}
298
299static void draw_unknown(struct charger *charger)
300{
301 int y;
302 if (charger->surf_unknown) {
303 draw_surface_centered(charger, charger->surf_unknown);
304 } else {
305 android_green();
306 y = draw_text("Charging!", -1, -1);
307 draw_text("?\?/100", -1, y + 25);
308 }
309}
310
311static void draw_battery(struct charger *charger)
312{
313 struct animation *batt_anim = charger->batt_anim;
314 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
315
316 if (batt_anim->num_frames != 0) {
317 draw_surface_centered(charger, frame->surface);
318 LOGV("drawing frame #%d name=%s min_cap=%d time=%d\n",
319 batt_anim->cur_frame, frame->name, frame->min_capacity,
320 frame->disp_time);
321 }
322}
323
324static void redraw_screen(struct charger *charger)
325{
326 struct animation *batt_anim = charger->batt_anim;
327
328 clear_screen();
329
330 /* try to display *something* */
331 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
332 draw_unknown(charger);
333 else
334 draw_battery(charger);
335 gr_flip();
336}
337
338static void kick_animation(struct animation *anim)
339{
340 anim->run = true;
341}
342
343static void reset_animation(struct animation *anim)
344{
345 anim->cur_cycle = 0;
346 anim->cur_frame = 0;
347 anim->run = false;
348}
349
350static void update_screen_state(struct charger *charger, int64_t now)
351{
352 struct animation *batt_anim = charger->batt_anim;
353 int cur_frame;
354 int disp_time;
355
356 if (!batt_anim->run || now < charger->next_screen_transition)
357 return;
358
359 /* animation is over, blank screen and leave */
360 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
361 reset_animation(batt_anim);
362 charger->next_screen_transition = -1;
363 gr_fb_blank(true);
364 LOGV("[%lld] animation done\n", now);
365 if (!charger->charger_connected)
366 request_suspend(true);
367 return;
368 }
369
370 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
371
372 /* animation starting, set up the animation */
373 if (batt_anim->cur_frame == 0) {
374 int batt_cap;
375 int ret;
376
377 LOGV("[%lld] animation starting\n", now);
378 batt_cap = get_battery_capacity();
379 if (batt_cap >= 0 && batt_anim->num_frames != 0) {
380 int i;
381
382 /* find first frame given current capacity */
383 for (i = 1; i < batt_anim->num_frames; i++) {
384 if (batt_cap < batt_anim->frames[i].min_capacity)
385 break;
386 }
387 batt_anim->cur_frame = i - 1;
388
389 /* show the first frame for twice as long */
390 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
391 }
392
393 batt_anim->capacity = batt_cap;
394 }
395
396 /* unblank the screen on first cycle */
397 if (batt_anim->cur_cycle == 0)
398 gr_fb_blank(false);
399
400 /* draw the new frame (@ cur_frame) */
401 redraw_screen(charger);
402
403 /* if we don't have anim frames, we only have one image, so just bump
404 * the cycle counter and exit
405 */
406 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
407 LOGV("[%lld] animation missing or unknown battery status\n", now);
408 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
409 batt_anim->cur_cycle++;
410 return;
411 }
412
413 /* schedule next screen transition */
414 charger->next_screen_transition = now + disp_time;
415
416 /* advance frame cntr to the next valid frame
417 * if necessary, advance cycle cntr, and reset frame cntr
418 */
419 batt_anim->cur_frame++;
420
421 /* if the frame is used for level-only, that is only show it when it's
422 * the current level, skip it during the animation.
423 */
424 while (batt_anim->cur_frame < batt_anim->num_frames &&
425 batt_anim->frames[batt_anim->cur_frame].level_only)
426 batt_anim->cur_frame++;
427 if (batt_anim->cur_frame >= batt_anim->num_frames) {
428 batt_anim->cur_cycle++;
429 batt_anim->cur_frame = 0;
430
431 /* don't reset the cycle counter, since we use that as a signal
432 * in a test above to check if animation is over
433 */
434 }
435}
436
437static int set_key_callback(int code, int value, void *data)
438{
439 struct charger *charger = (struct charger *)data;
440 int64_t now = curr_time_ms();
441 int down = !!value;
442
443 if (code > KEY_MAX)
444 return -1;
445
446 /* ignore events that don't modify our state */
447 if (charger->keys[code].down == down)
448 return 0;
449
450 /* only record the down even timestamp, as the amount
451 * of time the key spent not being pressed is not useful */
452 if (down)
453 charger->keys[code].timestamp = now;
454 charger->keys[code].down = down;
455 charger->keys[code].pending = true;
456 if (down) {
457 LOGV("[%lld] key[%d] down\n", now, code);
458 } else {
459 int64_t duration = now - charger->keys[code].timestamp;
460 int64_t secs = duration / 1000;
461 int64_t msecs = duration - secs * 1000;
462 LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
463 code, secs, msecs);
464 }
465
466 return 0;
467}
468
469static void update_input_state(struct charger *charger,
470 struct input_event *ev)
471{
472 if (ev->type != EV_KEY)
473 return;
474 set_key_callback(ev->code, ev->value, charger);
475}
476
477static void set_next_key_check(struct charger *charger,
478 struct key_state *key,
479 int64_t timeout)
480{
481 int64_t then = key->timestamp + timeout;
482
483 if (charger->next_key_check == -1 || then < charger->next_key_check)
484 charger->next_key_check = then;
485}
486
487static void process_key(struct charger *charger, int code, int64_t now)
488{
489 struct key_state *key = &charger->keys[code];
490 int64_t next_key_check;
491
492 if (code == KEY_POWER) {
493 if (key->down) {
494 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
495 if (now >= reboot_timeout) {
496 LOGI("[%lld] rebooting\n", now);
497 android_reboot(ANDROID_RB_RESTART, 0, 0);
498 } else {
499 /* if the key is pressed but timeout hasn't expired,
500 * make sure we wake up at the right-ish time to check
501 */
502 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
503 }
504 } else {
505 /* if the power key got released, force screen state cycle */
506 if (key->pending) {
507 request_suspend(false);
508 kick_animation(charger->batt_anim);
509 }
510 }
511 }
512
513 key->pending = false;
514}
515
516static void handle_input_state(struct charger *charger, int64_t now)
517{
518 process_key(charger, KEY_POWER, now);
519
520 if (charger->next_key_check != -1 && now > charger->next_key_check)
521 charger->next_key_check = -1;
522}
523
524static void handle_power_supply_state(struct charger *charger, int64_t now)
525{
526 if (!charger->have_battery_state)
527 return;
528
529 if (!charger->charger_connected) {
530 request_suspend(false);
531 if (charger->next_pwr_check == -1) {
532 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
533 LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
534 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
535 } else if (now >= charger->next_pwr_check) {
536 LOGI("[%lld] shutting down\n", now);
537 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
538 } else {
539 /* otherwise we already have a shutdown timer scheduled */
540 }
541 } else {
542 /* online supply present, reset shutdown timer if set */
543 if (charger->next_pwr_check != -1) {
544 LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
545 kick_animation(charger->batt_anim);
546 }
547 charger->next_pwr_check = -1;
548 }
549}
550
551void healthd_mode_charger_heartbeat()
552{
553 struct charger *charger = &charger_state;
554 int64_t now = curr_time_ms();
555 int ret;
556
557 handle_input_state(charger, now);
558 handle_power_supply_state(charger, now);
559
560 /* do screen update last in case any of the above want to start
561 * screen transitions (animations, etc)
562 */
563 update_screen_state(charger, now);
564}
565
566void healthd_mode_charger_battery_update(
567 struct android::BatteryProperties *props)
568{
569 struct charger *charger = &charger_state;
570
571 charger->charger_connected =
572 props->chargerAcOnline || props->chargerUsbOnline ||
573 props->chargerWirelessOnline;
574 charger->capacity = props->batteryLevel;
575
576 if (!charger->have_battery_state) {
577 charger->have_battery_state = true;
578 charger->next_screen_transition = curr_time_ms() - 1;
579 reset_animation(charger->batt_anim);
580 kick_animation(charger->batt_anim);
581 }
582}
583
584int healthd_mode_charger_preparetowait(void)
585{
586 struct charger *charger = &charger_state;
587 int64_t now = curr_time_ms();
588 int64_t next_event = INT64_MAX;
589 int64_t timeout;
590 struct input_event ev;
591 int ret;
592
593 LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
594 charger->next_screen_transition, charger->next_key_check,
595 charger->next_pwr_check);
596
597 if (charger->next_screen_transition != -1)
598 next_event = charger->next_screen_transition;
599 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
600 next_event = charger->next_key_check;
601 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
602 next_event = charger->next_pwr_check;
603
604 if (next_event != -1 && next_event != INT64_MAX)
605 timeout = max(0, next_event - now);
606 else
607 timeout = -1;
608
609 return (int)timeout;
610}
611
612static int input_callback(int fd, unsigned int epevents, void *data)
613{
614 struct charger *charger = (struct charger *)data;
615 struct input_event ev;
616 int ret;
617
618 ret = ev_get_input(fd, epevents, &ev);
619 if (ret)
620 return -1;
621 update_input_state(charger, &ev);
622 return 0;
623}
624
625static void charger_event_handler(uint32_t epevents)
626{
627 int ret;
628
629 ret = ev_wait(-1);
630 if (!ret)
631 ev_dispatch();
632}
633
634void healthd_mode_charger_init(struct healthd_config *config)
635{
636 int ret;
637 struct charger *charger = &charger_state;
638 int i;
639 int epollfd;
640
641 dump_last_kmsg();
642
643 LOGI("--------------- STARTING CHARGER MODE ---------------\n");
644
645 gr_init();
646 gr_font_size(&char_width, &char_height);
647
648 ret = ev_init(input_callback, charger);
649 if (!ret) {
650 epollfd = ev_get_epollfd();
651 healthd_register_event(epollfd, charger_event_handler);
652 }
653
654 ret = res_create_surface("charger/battery_fail", &charger->surf_unknown);
655 if (ret < 0) {
656 LOGE("Cannot load image\n");
657 charger->surf_unknown = NULL;
658 }
659
660 charger->batt_anim = &battery_animation;
661
662 for (i = 0; i < charger->batt_anim->num_frames; i++) {
663 struct frame *frame = &charger->batt_anim->frames[i];
664
665 ret = res_create_surface(frame->name, &frame->surface);
666 if (ret < 0) {
667 LOGE("Cannot load image %s\n", frame->name);
668 /* TODO: free the already allocated surfaces... */
669 charger->batt_anim->num_frames = 0;
670 charger->batt_anim->num_cycles = 1;
671 break;
672 }
673 }
674
675 ev_sync_key_state(set_key_callback, charger);
676
677#ifndef CHARGER_DISABLE_INIT_BLANK
678 gr_fb_blank(true);
679#endif
680
681 charger->next_screen_transition = -1;
682 charger->next_key_check = -1;
683 charger->next_pwr_check = -1;
684}