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