blob: 6800ad277d5bcc695215dc99f4635fc4dc76994f [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>
Riley Andrews6bd45882014-06-23 15:20:51 -070040#include <cutils/uevent.h>
41#include <cutils/properties.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070042
43#ifdef CHARGER_ENABLE_SUSPEND
44#include <suspend/autosuspend.h>
45#endif
46
47#include "minui/minui.h"
48
49#include "healthd.h"
50
Colin Cross1c38f5d2014-02-13 13:34:37 -080051char *locale;
52
Todd Poynorfea5b4d2013-09-09 12:09:08 -070053#ifndef max
54#define max(a,b) ((a) > (b) ? (a) : (b))
55#endif
56
57#ifndef min
58#define min(a,b) ((a) < (b) ? (a) : (b))
59#endif
60
61#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
62
63#define MSEC_PER_SEC (1000LL)
64#define NSEC_PER_MSEC (1000000LL)
65
66#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
67#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
68#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
69
70#define BATTERY_FULL_THRESH 95
71
72#define LAST_KMSG_PATH "/proc/last_kmsg"
Todd Poynorcd7c1042013-11-22 17:52:59 -080073#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops"
Todd Poynorfea5b4d2013-09-09 12:09:08 -070074#define LAST_KMSG_MAX_SZ (32 * 1024)
75
76#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
Todd Poynorebeb0c02014-09-23 14:54:24 -070077#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
Todd Poynorfea5b4d2013-09-09 12:09:08 -070078#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
79
80struct key_state {
81 bool pending;
82 bool down;
83 int64_t timestamp;
84};
85
86struct frame {
Todd Poynorfea5b4d2013-09-09 12:09:08 -070087 int disp_time;
88 int min_capacity;
89 bool level_only;
90
Elliott Hughes9e85ea12015-04-15 10:25:55 -070091 GRSurface* surface;
Todd Poynorfea5b4d2013-09-09 12:09:08 -070092};
93
94struct animation {
95 bool run;
96
97 struct frame *frames;
98 int cur_frame;
99 int num_frames;
100
101 int cur_cycle;
102 int num_cycles;
103
104 /* current capacity being animated */
105 int capacity;
106};
107
108struct charger {
109 bool have_battery_state;
110 bool charger_connected;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700111 int64_t next_screen_transition;
112 int64_t next_key_check;
113 int64_t next_pwr_check;
114
115 struct key_state keys[KEY_MAX + 1];
116
117 struct animation *batt_anim;
Elliott Hughes9e85ea12015-04-15 10:25:55 -0700118 GRSurface* surf_unknown;
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700119 int boot_min_cap;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700120};
121
122static struct frame batt_anim_frames[] = {
123 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700124 .disp_time = 750,
125 .min_capacity = 0,
126 .level_only = false,
127 .surface = NULL,
128 },
129 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700130 .disp_time = 750,
131 .min_capacity = 20,
132 .level_only = false,
133 .surface = NULL,
134 },
135 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700136 .disp_time = 750,
137 .min_capacity = 40,
138 .level_only = false,
139 .surface = NULL,
140 },
141 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700142 .disp_time = 750,
143 .min_capacity = 60,
144 .level_only = false,
145 .surface = NULL,
146 },
147 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700148 .disp_time = 750,
149 .min_capacity = 80,
150 .level_only = true,
151 .surface = NULL,
152 },
153 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700154 .disp_time = 750,
155 .min_capacity = BATTERY_FULL_THRESH,
156 .level_only = false,
157 .surface = NULL,
158 },
159};
160
161static struct animation battery_animation = {
162 .run = false,
163 .frames = batt_anim_frames,
164 .cur_frame = 0,
165 .num_frames = ARRAY_SIZE(batt_anim_frames),
166 .cur_cycle = 0,
167 .num_cycles = 3,
168 .capacity = 0,
169};
170
171static struct charger charger_state;
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700172static struct healthd_config *healthd_config;
173static struct android::BatteryProperties *batt_prop;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700174static int char_width;
175static int char_height;
Todd Poynora7300272014-06-30 13:15:05 -0700176static bool minui_inited;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700177
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);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700189 gr_clear();
190}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700191
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
Todd Poynorebeb0c02014-09-23 14:54:24 -0700201 LOGW("\n");
202 LOGW("*************** LAST KMSG ***************\n");
203 LOGW("\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) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700209 LOGW("last_kmsg not found. Cold reset?\n");
Todd Poynorcd7c1042013-11-22 17:52:59 -0800210 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';
Todd Poynorebeb0c02014-09-23 14:54:24 -0700228 klog_write(6, "<4>%s", ptr);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700229 ptr[cnt] = yoink;
230
231 len -= cnt;
232 ptr += cnt;
233 }
234
235 free(buf);
236
237out:
Todd Poynorebeb0c02014-09-23 14:54:24 -0700238 LOGW("\n");
239 LOGW("************* END LAST KMSG *************\n");
240 LOGW("\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700241}
242
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700243#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
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700252static int request_suspend(bool /*enable*/)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700253{
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 */
Elliott Hughes9e85ea12015-04-15 10:25:55 -0700277static int draw_surface_centered(struct charger* /*charger*/, GRSurface* surface)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700278{
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;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700348 int disp_time;
349
350 if (!batt_anim->run || now < charger->next_screen_transition)
351 return;
352
Todd Poynora7300272014-06-30 13:15:05 -0700353 if (!minui_inited) {
Todd Poynora7300272014-06-30 13:15:05 -0700354
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700355 if (healthd_config && healthd_config->screen_on) {
356 if (!healthd_config->screen_on(batt_prop)) {
357 LOGV("[%" PRId64 "] leave screen off\n", now);
358 batt_anim->run = false;
359 charger->next_screen_transition = -1;
360 if (charger->charger_connected)
361 request_suspend(true);
362 return;
363 }
Todd Poynora7300272014-06-30 13:15:05 -0700364 }
365
366 gr_init();
367 gr_font_size(&char_width, &char_height);
368
369#ifndef CHARGER_DISABLE_INIT_BLANK
370 gr_fb_blank(true);
371#endif
372 minui_inited = true;
373 }
374
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700375 /* animation is over, blank screen and leave */
376 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
377 reset_animation(batt_anim);
378 charger->next_screen_transition = -1;
379 gr_fb_blank(true);
Colin Crosse1d52472014-05-15 17:49:06 -0700380 LOGV("[%" PRId64 "] animation done\n", now);
Todd Poynor342a2262014-08-18 11:23:11 -0700381 if (charger->charger_connected)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700382 request_suspend(true);
383 return;
384 }
385
386 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
387
388 /* animation starting, set up the animation */
389 if (batt_anim->cur_frame == 0) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700390
Colin Crosse1d52472014-05-15 17:49:06 -0700391 LOGV("[%" PRId64 "] animation starting\n", now);
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700392 if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700393 int i;
394
395 /* find first frame given current capacity */
396 for (i = 1; i < batt_anim->num_frames; i++) {
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700397 if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700398 break;
399 }
400 batt_anim->cur_frame = i - 1;
401
402 /* show the first frame for twice as long */
403 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
404 }
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700405 if (batt_prop)
406 batt_anim->capacity = batt_prop->batteryLevel;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700407 }
408
409 /* unblank the screen on first cycle */
410 if (batt_anim->cur_cycle == 0)
411 gr_fb_blank(false);
412
413 /* draw the new frame (@ cur_frame) */
414 redraw_screen(charger);
415
416 /* if we don't have anim frames, we only have one image, so just bump
417 * the cycle counter and exit
418 */
419 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
Colin Crosse1d52472014-05-15 17:49:06 -0700420 LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700421 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
422 batt_anim->cur_cycle++;
423 return;
424 }
425
426 /* schedule next screen transition */
427 charger->next_screen_transition = now + disp_time;
428
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700429 /* advance frame cntr to the next valid frame only if we are charging
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700430 * if necessary, advance cycle cntr, and reset frame cntr
431 */
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700432 if (charger->charger_connected) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700433 batt_anim->cur_frame++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700434
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700435 /* if the frame is used for level-only, that is only show it when it's
436 * the current level, skip it during the animation.
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700437 */
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700438 while (batt_anim->cur_frame < batt_anim->num_frames &&
439 batt_anim->frames[batt_anim->cur_frame].level_only)
440 batt_anim->cur_frame++;
441 if (batt_anim->cur_frame >= batt_anim->num_frames) {
442 batt_anim->cur_cycle++;
443 batt_anim->cur_frame = 0;
444
445 /* don't reset the cycle counter, since we use that as a signal
446 * in a test above to check if animation is over
447 */
448 }
449 } else {
450 /* Stop animating if we're not charging.
451 * If we stop it immediately instead of going through this loop, then
452 * the animation would stop somewhere in the middle.
453 */
454 batt_anim->cur_frame = 0;
455 batt_anim->cur_cycle++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700456 }
457}
458
459static int set_key_callback(int code, int value, void *data)
460{
461 struct charger *charger = (struct charger *)data;
462 int64_t now = curr_time_ms();
463 int down = !!value;
464
465 if (code > KEY_MAX)
466 return -1;
467
468 /* ignore events that don't modify our state */
469 if (charger->keys[code].down == down)
470 return 0;
471
472 /* only record the down even timestamp, as the amount
473 * of time the key spent not being pressed is not useful */
474 if (down)
475 charger->keys[code].timestamp = now;
476 charger->keys[code].down = down;
477 charger->keys[code].pending = true;
478 if (down) {
Colin Crosse1d52472014-05-15 17:49:06 -0700479 LOGV("[%" PRId64 "] key[%d] down\n", now, code);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700480 } else {
481 int64_t duration = now - charger->keys[code].timestamp;
482 int64_t secs = duration / 1000;
483 int64_t msecs = duration - secs * 1000;
Colin Crosse1d52472014-05-15 17:49:06 -0700484 LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n",
485 now, code, secs, msecs);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700486 }
487
488 return 0;
489}
490
491static void update_input_state(struct charger *charger,
492 struct input_event *ev)
493{
494 if (ev->type != EV_KEY)
495 return;
496 set_key_callback(ev->code, ev->value, charger);
497}
498
499static void set_next_key_check(struct charger *charger,
500 struct key_state *key,
501 int64_t timeout)
502{
503 int64_t then = key->timestamp + timeout;
504
505 if (charger->next_key_check == -1 || then < charger->next_key_check)
506 charger->next_key_check = then;
507}
508
509static void process_key(struct charger *charger, int code, int64_t now)
510{
511 struct key_state *key = &charger->keys[code];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700512
513 if (code == KEY_POWER) {
514 if (key->down) {
515 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
516 if (now >= reboot_timeout) {
Riley Andrews6bd45882014-06-23 15:20:51 -0700517 /* We do not currently support booting from charger mode on
518 all devices. Check the property and continue booting or reboot
519 accordingly. */
520 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700521 LOGW("[%" PRId64 "] booting from charger mode\n", now);
Riley Andrews6bd45882014-06-23 15:20:51 -0700522 property_set("sys.boot_from_charger_mode", "1");
523 } else {
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700524 if (charger->batt_anim->capacity >= charger->boot_min_cap) {
525 LOGW("[%" PRId64 "] rebooting\n", now);
526 android_reboot(ANDROID_RB_RESTART, 0, 0);
527 } else {
Chih-Hung Hsiehc49ceca2015-02-24 10:50:41 -0800528 LOGV("[%" PRId64 "] ignore power-button press, battery level "
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700529 "less than minimum\n", now);
530 }
Riley Andrews6bd45882014-06-23 15:20:51 -0700531 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700532 } else {
533 /* if the key is pressed but timeout hasn't expired,
534 * make sure we wake up at the right-ish time to check
535 */
536 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
Ruchi Kandoi9a11aaa2014-10-22 14:16:35 -0700537
538 /* Turn on the display and kick animation on power-key press
539 * rather than on key release
540 */
541 kick_animation(charger->batt_anim);
542 request_suspend(false);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700543 }
544 } else {
545 /* if the power key got released, force screen state cycle */
546 if (key->pending) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700547 kick_animation(charger->batt_anim);
548 }
549 }
550 }
551
552 key->pending = false;
553}
554
555static void handle_input_state(struct charger *charger, int64_t now)
556{
557 process_key(charger, KEY_POWER, now);
558
559 if (charger->next_key_check != -1 && now > charger->next_key_check)
560 charger->next_key_check = -1;
561}
562
563static void handle_power_supply_state(struct charger *charger, int64_t now)
564{
565 if (!charger->have_battery_state)
566 return;
567
568 if (!charger->charger_connected) {
Ruchi Kandoi9a11aaa2014-10-22 14:16:35 -0700569
570 /* Last cycle would have stopped at the extreme top of battery-icon
571 * Need to show the correct level corresponding to capacity.
572 */
573 kick_animation(charger->batt_anim);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700574 request_suspend(false);
575 if (charger->next_pwr_check == -1) {
576 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
Todd Poynorebeb0c02014-09-23 14:54:24 -0700577 LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
Colin Crosse1d52472014-05-15 17:49:06 -0700578 now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700579 } else if (now >= charger->next_pwr_check) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700580 LOGW("[%" PRId64 "] shutting down\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700581 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
582 } else {
583 /* otherwise we already have a shutdown timer scheduled */
584 }
585 } else {
586 /* online supply present, reset shutdown timer if set */
587 if (charger->next_pwr_check != -1) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700588 LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700589 kick_animation(charger->batt_anim);
590 }
591 charger->next_pwr_check = -1;
592 }
593}
594
595void healthd_mode_charger_heartbeat()
596{
597 struct charger *charger = &charger_state;
598 int64_t now = curr_time_ms();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700599
600 handle_input_state(charger, now);
601 handle_power_supply_state(charger, now);
602
603 /* do screen update last in case any of the above want to start
604 * screen transitions (animations, etc)
605 */
606 update_screen_state(charger, now);
607}
608
609void healthd_mode_charger_battery_update(
610 struct android::BatteryProperties *props)
611{
612 struct charger *charger = &charger_state;
613
614 charger->charger_connected =
615 props->chargerAcOnline || props->chargerUsbOnline ||
616 props->chargerWirelessOnline;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700617
618 if (!charger->have_battery_state) {
619 charger->have_battery_state = true;
620 charger->next_screen_transition = curr_time_ms() - 1;
621 reset_animation(charger->batt_anim);
622 kick_animation(charger->batt_anim);
623 }
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700624 batt_prop = props;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700625}
626
627int healthd_mode_charger_preparetowait(void)
628{
629 struct charger *charger = &charger_state;
630 int64_t now = curr_time_ms();
631 int64_t next_event = INT64_MAX;
632 int64_t timeout;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700633
Colin Crosse1d52472014-05-15 17:49:06 -0700634 LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700635 charger->next_screen_transition, charger->next_key_check,
636 charger->next_pwr_check);
637
638 if (charger->next_screen_transition != -1)
639 next_event = charger->next_screen_transition;
640 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
641 next_event = charger->next_key_check;
642 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
643 next_event = charger->next_pwr_check;
644
645 if (next_event != -1 && next_event != INT64_MAX)
646 timeout = max(0, next_event - now);
647 else
648 timeout = -1;
649
650 return (int)timeout;
651}
652
653static int input_callback(int fd, unsigned int epevents, void *data)
654{
655 struct charger *charger = (struct charger *)data;
656 struct input_event ev;
657 int ret;
658
659 ret = ev_get_input(fd, epevents, &ev);
660 if (ret)
661 return -1;
662 update_input_state(charger, &ev);
663 return 0;
664}
665
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700666static void charger_event_handler(uint32_t /*epevents*/)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700667{
668 int ret;
669
670 ret = ev_wait(-1);
671 if (!ret)
672 ev_dispatch();
673}
674
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700675void healthd_mode_charger_init(struct healthd_config* config)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700676{
677 int ret;
678 struct charger *charger = &charger_state;
679 int i;
680 int epollfd;
681
682 dump_last_kmsg();
683
Todd Poynorebeb0c02014-09-23 14:54:24 -0700684 LOGW("--------------- STARTING CHARGER MODE ---------------\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700685
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700686 ret = ev_init(input_callback, charger);
687 if (!ret) {
688 epollfd = ev_get_epollfd();
689 healthd_register_event(epollfd, charger_event_handler);
690 }
691
Doug Zongker2f10b6b2014-03-18 00:15:27 +0000692 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700693 if (ret < 0) {
Doug Zongkeree6ef152014-03-11 08:42:09 -0700694 LOGE("Cannot load battery_fail image\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700695 charger->surf_unknown = NULL;
696 }
697
698 charger->batt_anim = &battery_animation;
699
Elliott Hughes9e85ea12015-04-15 10:25:55 -0700700 GRSurface** scale_frames;
Doug Zongkeree6ef152014-03-11 08:42:09 -0700701 int scale_count;
Doug Zongker2f10b6b2014-03-18 00:15:27 +0000702 ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700703 if (ret < 0) {
704 LOGE("Cannot load battery_scale image\n");
705 charger->batt_anim->num_frames = 0;
706 charger->batt_anim->num_cycles = 1;
707 } else if (scale_count != charger->batt_anim->num_frames) {
708 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
709 scale_count, charger->batt_anim->num_frames);
710 charger->batt_anim->num_frames = 0;
711 charger->batt_anim->num_cycles = 1;
712 } else {
713 for (i = 0; i < charger->batt_anim->num_frames; i++) {
714 charger->batt_anim->frames[i].surface = scale_frames[i];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700715 }
716 }
717
718 ev_sync_key_state(set_key_callback, charger);
719
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700720 charger->next_screen_transition = -1;
721 charger->next_key_check = -1;
722 charger->next_pwr_check = -1;
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700723 healthd_config = config;
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700724 charger->boot_min_cap = config->boot_min_cap;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700725}