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