blob: 9ed5944a282daa16aa45180d48691eb1d8c2e6b7 [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
91 gr_surface surface;
92};
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;
118 gr_surface surf_unknown;
119};
120
121static struct frame batt_anim_frames[] = {
122 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700123 .disp_time = 750,
124 .min_capacity = 0,
125 .level_only = false,
126 .surface = NULL,
127 },
128 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700129 .disp_time = 750,
130 .min_capacity = 20,
131 .level_only = false,
132 .surface = NULL,
133 },
134 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700135 .disp_time = 750,
136 .min_capacity = 40,
137 .level_only = false,
138 .surface = NULL,
139 },
140 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700141 .disp_time = 750,
142 .min_capacity = 60,
143 .level_only = false,
144 .surface = NULL,
145 },
146 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700147 .disp_time = 750,
148 .min_capacity = 80,
149 .level_only = true,
150 .surface = NULL,
151 },
152 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700153 .disp_time = 750,
154 .min_capacity = BATTERY_FULL_THRESH,
155 .level_only = false,
156 .surface = NULL,
157 },
158};
159
160static struct animation battery_animation = {
161 .run = false,
162 .frames = batt_anim_frames,
163 .cur_frame = 0,
164 .num_frames = ARRAY_SIZE(batt_anim_frames),
165 .cur_cycle = 0,
166 .num_cycles = 3,
167 .capacity = 0,
168};
169
170static struct charger charger_state;
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700171static struct healthd_config *healthd_config;
172static struct android::BatteryProperties *batt_prop;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700173static int char_width;
174static int char_height;
Todd Poynora7300272014-06-30 13:15:05 -0700175static bool minui_inited;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700176
177/* current time in milliseconds */
178static int64_t curr_time_ms(void)
179{
180 struct timespec tm;
181 clock_gettime(CLOCK_MONOTONIC, &tm);
182 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
183}
184
185static void clear_screen(void)
186{
187 gr_color(0, 0, 0, 255);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700188 gr_clear();
189}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700190
191#define MAX_KLOG_WRITE_BUF_SZ 256
192
193static void dump_last_kmsg(void)
194{
195 char *buf;
196 char *ptr;
197 unsigned sz = 0;
198 int len;
199
Todd Poynorebeb0c02014-09-23 14:54:24 -0700200 LOGW("\n");
201 LOGW("*************** LAST KMSG ***************\n");
202 LOGW("\n");
Todd Poynorcd7c1042013-11-22 17:52:59 -0800203 buf = (char *)load_file(LAST_KMSG_PSTORE_PATH, &sz);
204
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700205 if (!buf || !sz) {
Todd Poynorcd7c1042013-11-22 17:52:59 -0800206 buf = (char *)load_file(LAST_KMSG_PATH, &sz);
207 if (!buf || !sz) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700208 LOGW("last_kmsg not found. Cold reset?\n");
Todd Poynorcd7c1042013-11-22 17:52:59 -0800209 goto out;
210 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700211 }
212
213 len = min(sz, LAST_KMSG_MAX_SZ);
214 ptr = buf + (sz - len);
215
216 while (len > 0) {
217 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
218 char yoink;
219 char *nl;
220
221 nl = (char *)memrchr(ptr, '\n', cnt - 1);
222 if (nl)
223 cnt = nl - ptr + 1;
224
225 yoink = ptr[cnt];
226 ptr[cnt] = '\0';
Todd Poynorebeb0c02014-09-23 14:54:24 -0700227 klog_write(6, "<4>%s", ptr);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700228 ptr[cnt] = yoink;
229
230 len -= cnt;
231 ptr += cnt;
232 }
233
234 free(buf);
235
236out:
Todd Poynorebeb0c02014-09-23 14:54:24 -0700237 LOGW("\n");
238 LOGW("************* END LAST KMSG *************\n");
239 LOGW("\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700240}
241
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700242#ifdef CHARGER_ENABLE_SUSPEND
243static int request_suspend(bool enable)
244{
245 if (enable)
246 return autosuspend_enable();
247 else
248 return autosuspend_disable();
249}
250#else
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700251static int request_suspend(bool /*enable*/)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700252{
253 return 0;
254}
255#endif
256
257static int draw_text(const char *str, int x, int y)
258{
259 int str_len_px = gr_measure(str);
260
261 if (x < 0)
262 x = (gr_fb_width() - str_len_px) / 2;
263 if (y < 0)
264 y = (gr_fb_height() - char_height) / 2;
265 gr_text(x, y, str, 0);
266
267 return y + char_height;
268}
269
270static void android_green(void)
271{
272 gr_color(0xa4, 0xc6, 0x39, 255);
273}
274
275/* returns the last y-offset of where the surface ends */
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700276static int draw_surface_centered(struct charger* /*charger*/, gr_surface surface)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700277{
278 int w;
279 int h;
280 int x;
281 int y;
282
283 w = gr_get_width(surface);
284 h = gr_get_height(surface);
285 x = (gr_fb_width() - w) / 2 ;
286 y = (gr_fb_height() - h) / 2 ;
287
288 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
289 gr_blit(surface, 0, 0, w, h, x, y);
290 return y + h;
291}
292
293static void draw_unknown(struct charger *charger)
294{
295 int y;
296 if (charger->surf_unknown) {
297 draw_surface_centered(charger, charger->surf_unknown);
298 } else {
299 android_green();
300 y = draw_text("Charging!", -1, -1);
301 draw_text("?\?/100", -1, y + 25);
302 }
303}
304
305static void draw_battery(struct charger *charger)
306{
307 struct animation *batt_anim = charger->batt_anim;
308 struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
309
310 if (batt_anim->num_frames != 0) {
311 draw_surface_centered(charger, frame->surface);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700312 LOGV("drawing frame #%d min_cap=%d time=%d\n",
313 batt_anim->cur_frame, frame->min_capacity,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700314 frame->disp_time);
315 }
316}
317
318static void redraw_screen(struct charger *charger)
319{
320 struct animation *batt_anim = charger->batt_anim;
321
322 clear_screen();
323
324 /* try to display *something* */
325 if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
326 draw_unknown(charger);
327 else
328 draw_battery(charger);
329 gr_flip();
330}
331
332static void kick_animation(struct animation *anim)
333{
334 anim->run = true;
335}
336
337static void reset_animation(struct animation *anim)
338{
339 anim->cur_cycle = 0;
340 anim->cur_frame = 0;
341 anim->run = false;
342}
343
344static void update_screen_state(struct charger *charger, int64_t now)
345{
346 struct animation *batt_anim = charger->batt_anim;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700347 int disp_time;
348
349 if (!batt_anim->run || now < charger->next_screen_transition)
350 return;
351
Todd Poynora7300272014-06-30 13:15:05 -0700352 if (!minui_inited) {
Todd Poynora7300272014-06-30 13:15:05 -0700353
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700354 if (healthd_config && healthd_config->screen_on) {
355 if (!healthd_config->screen_on(batt_prop)) {
356 LOGV("[%" PRId64 "] leave screen off\n", now);
357 batt_anim->run = false;
358 charger->next_screen_transition = -1;
359 if (charger->charger_connected)
360 request_suspend(true);
361 return;
362 }
Todd Poynora7300272014-06-30 13:15:05 -0700363 }
364
365 gr_init();
366 gr_font_size(&char_width, &char_height);
367
368#ifndef CHARGER_DISABLE_INIT_BLANK
369 gr_fb_blank(true);
370#endif
371 minui_inited = true;
372 }
373
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700374 /* animation is over, blank screen and leave */
375 if (batt_anim->cur_cycle == batt_anim->num_cycles) {
376 reset_animation(batt_anim);
377 charger->next_screen_transition = -1;
378 gr_fb_blank(true);
Colin Crosse1d52472014-05-15 17:49:06 -0700379 LOGV("[%" PRId64 "] animation done\n", now);
Todd Poynor342a2262014-08-18 11:23:11 -0700380 if (charger->charger_connected)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700381 request_suspend(true);
382 return;
383 }
384
385 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
386
387 /* animation starting, set up the animation */
388 if (batt_anim->cur_frame == 0) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700389
Colin Crosse1d52472014-05-15 17:49:06 -0700390 LOGV("[%" PRId64 "] animation starting\n", now);
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700391 if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700392 int i;
393
394 /* find first frame given current capacity */
395 for (i = 1; i < batt_anim->num_frames; i++) {
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700396 if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700397 break;
398 }
399 batt_anim->cur_frame = i - 1;
400
401 /* show the first frame for twice as long */
402 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
403 }
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700404 if (batt_prop)
405 batt_anim->capacity = batt_prop->batteryLevel;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700406 }
407
408 /* unblank the screen on first cycle */
409 if (batt_anim->cur_cycle == 0)
410 gr_fb_blank(false);
411
412 /* draw the new frame (@ cur_frame) */
413 redraw_screen(charger);
414
415 /* if we don't have anim frames, we only have one image, so just bump
416 * the cycle counter and exit
417 */
418 if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
Colin Crosse1d52472014-05-15 17:49:06 -0700419 LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700420 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
421 batt_anim->cur_cycle++;
422 return;
423 }
424
425 /* schedule next screen transition */
426 charger->next_screen_transition = now + disp_time;
427
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700428 /* advance frame cntr to the next valid frame only if we are charging
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700429 * if necessary, advance cycle cntr, and reset frame cntr
430 */
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700431 if (charger->charger_connected) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700432 batt_anim->cur_frame++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700433
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700434 /* if the frame is used for level-only, that is only show it when it's
435 * the current level, skip it during the animation.
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700436 */
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700437 while (batt_anim->cur_frame < batt_anim->num_frames &&
438 batt_anim->frames[batt_anim->cur_frame].level_only)
439 batt_anim->cur_frame++;
440 if (batt_anim->cur_frame >= batt_anim->num_frames) {
441 batt_anim->cur_cycle++;
442 batt_anim->cur_frame = 0;
443
444 /* don't reset the cycle counter, since we use that as a signal
445 * in a test above to check if animation is over
446 */
447 }
448 } else {
449 /* Stop animating if we're not charging.
450 * If we stop it immediately instead of going through this loop, then
451 * the animation would stop somewhere in the middle.
452 */
453 batt_anim->cur_frame = 0;
454 batt_anim->cur_cycle++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700455 }
456}
457
458static int set_key_callback(int code, int value, void *data)
459{
460 struct charger *charger = (struct charger *)data;
461 int64_t now = curr_time_ms();
462 int down = !!value;
463
464 if (code > KEY_MAX)
465 return -1;
466
467 /* ignore events that don't modify our state */
468 if (charger->keys[code].down == down)
469 return 0;
470
471 /* only record the down even timestamp, as the amount
472 * of time the key spent not being pressed is not useful */
473 if (down)
474 charger->keys[code].timestamp = now;
475 charger->keys[code].down = down;
476 charger->keys[code].pending = true;
477 if (down) {
Colin Crosse1d52472014-05-15 17:49:06 -0700478 LOGV("[%" PRId64 "] key[%d] down\n", now, code);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700479 } else {
480 int64_t duration = now - charger->keys[code].timestamp;
481 int64_t secs = duration / 1000;
482 int64_t msecs = duration - secs * 1000;
Colin Crosse1d52472014-05-15 17:49:06 -0700483 LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n",
484 now, code, secs, msecs);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700485 }
486
487 return 0;
488}
489
490static void update_input_state(struct charger *charger,
491 struct input_event *ev)
492{
493 if (ev->type != EV_KEY)
494 return;
495 set_key_callback(ev->code, ev->value, charger);
496}
497
498static void set_next_key_check(struct charger *charger,
499 struct key_state *key,
500 int64_t timeout)
501{
502 int64_t then = key->timestamp + timeout;
503
504 if (charger->next_key_check == -1 || then < charger->next_key_check)
505 charger->next_key_check = then;
506}
507
508static void process_key(struct charger *charger, int code, int64_t now)
509{
510 struct key_state *key = &charger->keys[code];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700511
512 if (code == KEY_POWER) {
513 if (key->down) {
514 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
515 if (now >= reboot_timeout) {
Riley Andrews6bd45882014-06-23 15:20:51 -0700516 /* We do not currently support booting from charger mode on
517 all devices. Check the property and continue booting or reboot
518 accordingly. */
519 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700520 LOGW("[%" PRId64 "] booting from charger mode\n", now);
Riley Andrews6bd45882014-06-23 15:20:51 -0700521 property_set("sys.boot_from_charger_mode", "1");
522 } else {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700523 LOGW("[%" PRId64 "] rebooting\n", now);
Riley Andrews6bd45882014-06-23 15:20:51 -0700524 android_reboot(ANDROID_RB_RESTART, 0, 0);
525 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700526 } else {
527 /* if the key is pressed but timeout hasn't expired,
528 * make sure we wake up at the right-ish time to check
529 */
530 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
531 }
532 } else {
533 /* if the power key got released, force screen state cycle */
534 if (key->pending) {
535 request_suspend(false);
536 kick_animation(charger->batt_anim);
537 }
538 }
539 }
540
541 key->pending = false;
542}
543
544static void handle_input_state(struct charger *charger, int64_t now)
545{
546 process_key(charger, KEY_POWER, now);
547
548 if (charger->next_key_check != -1 && now > charger->next_key_check)
549 charger->next_key_check = -1;
550}
551
552static void handle_power_supply_state(struct charger *charger, int64_t now)
553{
554 if (!charger->have_battery_state)
555 return;
556
557 if (!charger->charger_connected) {
558 request_suspend(false);
559 if (charger->next_pwr_check == -1) {
560 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
Todd Poynorebeb0c02014-09-23 14:54:24 -0700561 LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
Colin Crosse1d52472014-05-15 17:49:06 -0700562 now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700563 } else if (now >= charger->next_pwr_check) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700564 LOGW("[%" PRId64 "] shutting down\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700565 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
566 } else {
567 /* otherwise we already have a shutdown timer scheduled */
568 }
569 } else {
570 /* online supply present, reset shutdown timer if set */
571 if (charger->next_pwr_check != -1) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700572 LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700573 kick_animation(charger->batt_anim);
574 }
575 charger->next_pwr_check = -1;
576 }
577}
578
579void healthd_mode_charger_heartbeat()
580{
581 struct charger *charger = &charger_state;
582 int64_t now = curr_time_ms();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700583
584 handle_input_state(charger, now);
585 handle_power_supply_state(charger, now);
586
587 /* do screen update last in case any of the above want to start
588 * screen transitions (animations, etc)
589 */
590 update_screen_state(charger, now);
591}
592
593void healthd_mode_charger_battery_update(
594 struct android::BatteryProperties *props)
595{
596 struct charger *charger = &charger_state;
597
598 charger->charger_connected =
599 props->chargerAcOnline || props->chargerUsbOnline ||
600 props->chargerWirelessOnline;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700601
602 if (!charger->have_battery_state) {
603 charger->have_battery_state = true;
604 charger->next_screen_transition = curr_time_ms() - 1;
605 reset_animation(charger->batt_anim);
606 kick_animation(charger->batt_anim);
607 }
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700608 batt_prop = props;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700609}
610
611int healthd_mode_charger_preparetowait(void)
612{
613 struct charger *charger = &charger_state;
614 int64_t now = curr_time_ms();
615 int64_t next_event = INT64_MAX;
616 int64_t timeout;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700617
Colin Crosse1d52472014-05-15 17:49:06 -0700618 LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700619 charger->next_screen_transition, charger->next_key_check,
620 charger->next_pwr_check);
621
622 if (charger->next_screen_transition != -1)
623 next_event = charger->next_screen_transition;
624 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
625 next_event = charger->next_key_check;
626 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
627 next_event = charger->next_pwr_check;
628
629 if (next_event != -1 && next_event != INT64_MAX)
630 timeout = max(0, next_event - now);
631 else
632 timeout = -1;
633
634 return (int)timeout;
635}
636
637static int input_callback(int fd, unsigned int epevents, void *data)
638{
639 struct charger *charger = (struct charger *)data;
640 struct input_event ev;
641 int ret;
642
643 ret = ev_get_input(fd, epevents, &ev);
644 if (ret)
645 return -1;
646 update_input_state(charger, &ev);
647 return 0;
648}
649
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700650static void charger_event_handler(uint32_t /*epevents*/)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700651{
652 int ret;
653
654 ret = ev_wait(-1);
655 if (!ret)
656 ev_dispatch();
657}
658
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700659void healthd_mode_charger_init(struct healthd_config* config)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700660{
661 int ret;
662 struct charger *charger = &charger_state;
663 int i;
664 int epollfd;
665
666 dump_last_kmsg();
667
Todd Poynorebeb0c02014-09-23 14:54:24 -0700668 LOGW("--------------- STARTING CHARGER MODE ---------------\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700669
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700670 ret = ev_init(input_callback, charger);
671 if (!ret) {
672 epollfd = ev_get_epollfd();
673 healthd_register_event(epollfd, charger_event_handler);
674 }
675
Doug Zongker2f10b6b2014-03-18 00:15:27 +0000676 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700677 if (ret < 0) {
Doug Zongkeree6ef152014-03-11 08:42:09 -0700678 LOGE("Cannot load battery_fail image\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700679 charger->surf_unknown = NULL;
680 }
681
682 charger->batt_anim = &battery_animation;
683
Doug Zongkeree6ef152014-03-11 08:42:09 -0700684 gr_surface* scale_frames;
685 int scale_count;
Doug Zongker2f10b6b2014-03-18 00:15:27 +0000686 ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_frames);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700687 if (ret < 0) {
688 LOGE("Cannot load battery_scale image\n");
689 charger->batt_anim->num_frames = 0;
690 charger->batt_anim->num_cycles = 1;
691 } else if (scale_count != charger->batt_anim->num_frames) {
692 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
693 scale_count, charger->batt_anim->num_frames);
694 charger->batt_anim->num_frames = 0;
695 charger->batt_anim->num_cycles = 1;
696 } else {
697 for (i = 0; i < charger->batt_anim->num_frames; i++) {
698 charger->batt_anim->frames[i].surface = scale_frames[i];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700699 }
700 }
701
702 ev_sync_key_state(set_key_callback, charger);
703
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700704 charger->next_screen_transition = -1;
705 charger->next_key_check = -1;
706 charger->next_pwr_check = -1;
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700707 healthd_config = config;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700708}