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