blob: 91774c6bc4d9b8b5f1ed8bc203c4dd51521fd12d [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
Tao Bao92c26012017-01-18 22:54:54 -080033#include <functional>
34
Damien Bargiacchi565ba022016-08-11 15:29:50 -070035#include <android-base/file.h>
36#include <android-base/stringprintf.h>
37
Todd Poynorfea5b4d2013-09-09 12:09:08 -070038#include <sys/socket.h>
39#include <linux/netlink.h>
40
41#include <batteryservice/BatteryService.h>
42#include <cutils/android_reboot.h>
43#include <cutils/klog.h>
44#include <cutils/misc.h>
Riley Andrews6bd45882014-06-23 15:20:51 -070045#include <cutils/uevent.h>
46#include <cutils/properties.h>
Tao Bao92c26012017-01-18 22:54:54 -080047#include <minui/minui.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070048
49#ifdef CHARGER_ENABLE_SUSPEND
50#include <suspend/autosuspend.h>
51#endif
52
Damien Bargiacchi565ba022016-08-11 15:29:50 -070053#include "animation.h"
54#include "AnimationParser.h"
Todd Poynorfea5b4d2013-09-09 12:09:08 -070055
Yabin Cuie98e1772016-02-17 12:21:34 -080056#include <healthd/healthd.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070057
Damien Bargiacchi565ba022016-08-11 15:29:50 -070058using namespace android;
59
Colin Cross1c38f5d2014-02-13 13:34:37 -080060char *locale;
61
Todd Poynorfea5b4d2013-09-09 12:09:08 -070062#ifndef max
63#define max(a,b) ((a) > (b) ? (a) : (b))
64#endif
65
66#ifndef min
67#define min(a,b) ((a) < (b) ? (a) : (b))
68#endif
69
Chih-Hung Hsiehcdb2ca52016-06-10 10:39:35 -070070#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
Todd Poynorfea5b4d2013-09-09 12:09:08 -070071
72#define MSEC_PER_SEC (1000LL)
73#define NSEC_PER_MSEC (1000000LL)
74
75#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
76#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
77#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
78
Todd Poynorfea5b4d2013-09-09 12:09:08 -070079#define LAST_KMSG_PATH "/proc/last_kmsg"
Todd Poynorcd7c1042013-11-22 17:52:59 -080080#define LAST_KMSG_PSTORE_PATH "/sys/fs/pstore/console-ramoops"
Todd Poynorfea5b4d2013-09-09 12:09:08 -070081#define LAST_KMSG_MAX_SZ (32 * 1024)
82
83#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
Todd Poynorebeb0c02014-09-23 14:54:24 -070084#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
Todd Poynorfea5b4d2013-09-09 12:09:08 -070085#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
86
Damien Bargiacchi565ba022016-08-11 15:29:50 -070087static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt";
88
Todd Poynorfea5b4d2013-09-09 12:09:08 -070089struct key_state {
90 bool pending;
91 bool down;
92 int64_t timestamp;
93};
94
Todd Poynorfea5b4d2013-09-09 12:09:08 -070095struct charger {
96 bool have_battery_state;
97 bool charger_connected;
Todd Poynorfea5b4d2013-09-09 12:09:08 -070098 int64_t next_screen_transition;
99 int64_t next_key_check;
100 int64_t next_pwr_check;
101
102 struct key_state keys[KEY_MAX + 1];
103
104 struct animation *batt_anim;
Elliott Hughes9e85ea12015-04-15 10:25:55 -0700105 GRSurface* surf_unknown;
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700106 int boot_min_cap;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700107};
108
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700109static const struct animation BASE_ANIMATION = {
110 .text_clock = {
111 .pos_x = 0,
112 .pos_y = 0,
113
114 .color_r = 255,
115 .color_g = 255,
116 .color_b = 255,
117 .color_a = 255,
118
119 .font = nullptr,
120 },
121 .text_percent = {
122 .pos_x = 0,
123 .pos_y = 0,
124
125 .color_r = 255,
126 .color_g = 255,
127 .color_b = 255,
128 .color_a = 255,
129 },
130
131 .run = false,
132
133 .frames = nullptr,
134 .cur_frame = 0,
135 .num_frames = 0,
136 .first_frame_repeats = 2,
137
138 .cur_cycle = 0,
139 .num_cycles = 3,
140
141 .cur_level = 0,
142 .cur_status = BATTERY_STATUS_UNKNOWN,
143};
144
145
146static struct animation::frame default_animation_frames[] = {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700147 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700148 .disp_time = 750,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700149 .min_level = 0,
150 .max_level = 19,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700151 .surface = NULL,
152 },
153 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700154 .disp_time = 750,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700155 .min_level = 0,
156 .max_level = 39,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700157 .surface = NULL,
158 },
159 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700160 .disp_time = 750,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700161 .min_level = 0,
162 .max_level = 59,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700163 .surface = NULL,
164 },
165 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700166 .disp_time = 750,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700167 .min_level = 0,
168 .max_level = 79,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700169 .surface = NULL,
170 },
171 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700172 .disp_time = 750,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700173 .min_level = 80,
174 .max_level = 95,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700175 .surface = NULL,
176 },
177 {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700178 .disp_time = 750,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700179 .min_level = 0,
180 .max_level = 100,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700181 .surface = NULL,
182 },
183};
184
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700185static struct animation battery_animation = BASE_ANIMATION;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700186
187static struct charger charger_state;
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700188static struct healthd_config *healthd_config;
189static struct android::BatteryProperties *batt_prop;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700190static int char_width;
191static int char_height;
Todd Poynora7300272014-06-30 13:15:05 -0700192static bool minui_inited;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700193
194/* current time in milliseconds */
195static int64_t curr_time_ms(void)
196{
197 struct timespec tm;
198 clock_gettime(CLOCK_MONOTONIC, &tm);
199 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
200}
201
202static void clear_screen(void)
203{
204 gr_color(0, 0, 0, 255);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700205 gr_clear();
206}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700207
208#define MAX_KLOG_WRITE_BUF_SZ 256
209
210static void dump_last_kmsg(void)
211{
212 char *buf;
213 char *ptr;
214 unsigned sz = 0;
215 int len;
216
Todd Poynorebeb0c02014-09-23 14:54:24 -0700217 LOGW("\n");
218 LOGW("*************** LAST KMSG ***************\n");
219 LOGW("\n");
Todd Poynorcd7c1042013-11-22 17:52:59 -0800220 buf = (char *)load_file(LAST_KMSG_PSTORE_PATH, &sz);
221
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700222 if (!buf || !sz) {
Todd Poynorcd7c1042013-11-22 17:52:59 -0800223 buf = (char *)load_file(LAST_KMSG_PATH, &sz);
224 if (!buf || !sz) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700225 LOGW("last_kmsg not found. Cold reset?\n");
Todd Poynorcd7c1042013-11-22 17:52:59 -0800226 goto out;
227 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700228 }
229
230 len = min(sz, LAST_KMSG_MAX_SZ);
231 ptr = buf + (sz - len);
232
233 while (len > 0) {
234 int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
235 char yoink;
236 char *nl;
237
238 nl = (char *)memrchr(ptr, '\n', cnt - 1);
239 if (nl)
240 cnt = nl - ptr + 1;
241
242 yoink = ptr[cnt];
243 ptr[cnt] = '\0';
Todd Poynorebeb0c02014-09-23 14:54:24 -0700244 klog_write(6, "<4>%s", ptr);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700245 ptr[cnt] = yoink;
246
247 len -= cnt;
248 ptr += cnt;
249 }
250
251 free(buf);
252
253out:
Todd Poynorebeb0c02014-09-23 14:54:24 -0700254 LOGW("\n");
255 LOGW("************* END LAST KMSG *************\n");
256 LOGW("\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700257}
258
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700259#ifdef CHARGER_ENABLE_SUSPEND
260static int request_suspend(bool enable)
261{
262 if (enable)
263 return autosuspend_enable();
264 else
265 return autosuspend_disable();
266}
267#else
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700268static int request_suspend(bool /*enable*/)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700269{
270 return 0;
271}
272#endif
273
274static int draw_text(const char *str, int x, int y)
275{
Damien Bargiacchi0ee524d2016-08-18 17:33:32 -0700276 int str_len_px = gr_measure(gr_sys_font(), str);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700277
278 if (x < 0)
279 x = (gr_fb_width() - str_len_px) / 2;
280 if (y < 0)
281 y = (gr_fb_height() - char_height) / 2;
Damien Bargiacchi0ee524d2016-08-18 17:33:32 -0700282 gr_text(gr_sys_font(), x, y, str, 0);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700283
284 return y + char_height;
285}
286
287static void android_green(void)
288{
289 gr_color(0xa4, 0xc6, 0x39, 255);
290}
291
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700292// Negative x or y coordinates position the text away from the opposite edge that positive ones do.
293void determine_xy(const animation::text_field& field, const int length, int* x, int* y)
294{
295 *x = field.pos_x;
296 *y = field.pos_y;
297
298 int str_len_px = length * field.font->char_width;
299 if (field.pos_x == CENTER_VAL) {
300 *x = (gr_fb_width() - str_len_px) / 2;
301 } else if (field.pos_x >= 0) {
302 *x = field.pos_x;
303 } else { // position from max edge
304 *x = gr_fb_width() + field.pos_x - str_len_px;
305 }
306
307 if (field.pos_y == CENTER_VAL) {
308 *y = (gr_fb_height() - field.font->char_height) / 2;
309 } else if (field.pos_y >= 0) {
310 *y = field.pos_y;
311 } else { // position from max edge
312 *y = gr_fb_height() + field.pos_y - field.font->char_height;
313 }
314}
315
316static void draw_clock(const animation& anim)
317{
318 static constexpr char CLOCK_FORMAT[] = "%H:%M";
319 static constexpr int CLOCK_LENGTH = 6;
320
321 const animation::text_field& field = anim.text_clock;
322
323 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) return;
324
325 time_t rawtime;
326 time(&rawtime);
327 struct tm* time_info = localtime(&rawtime);
328
329 char clock_str[CLOCK_LENGTH];
330 size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
331 if (length != CLOCK_LENGTH - 1) {
332 LOGE("Could not format time\n");
333 return;
334 }
335
336 int x, y;
337 determine_xy(field, length, &x, &y);
338
339 LOGV("drawing clock %s %d %d\n", clock_str, x, y);
340 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
341 gr_text(field.font, x, y, clock_str, false);
342}
343
344static void draw_percent(const animation& anim)
345{
Damien Bargiacchi3f0250c2016-10-24 16:45:29 -0700346 int cur_level = anim.cur_level;
347 if (anim.cur_status == BATTERY_STATUS_FULL) {
348 cur_level = 100;
349 }
350
351 if (cur_level <= 0) return;
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700352
353 const animation::text_field& field = anim.text_percent;
354 if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
355 return;
356 }
357
Damien Bargiacchi3f0250c2016-10-24 16:45:29 -0700358 std::string str = base::StringPrintf("%d%%", cur_level);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700359
360 int x, y;
361 determine_xy(field, str.size(), &x, &y);
362
363 LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
364 gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
365 gr_text(field.font, x, y, str.c_str(), false);
366}
367
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700368/* returns the last y-offset of where the surface ends */
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700369static int draw_surface_centered(GRSurface* surface)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700370{
371 int w;
372 int h;
373 int x;
374 int y;
375
376 w = gr_get_width(surface);
377 h = gr_get_height(surface);
378 x = (gr_fb_width() - w) / 2 ;
379 y = (gr_fb_height() - h) / 2 ;
380
381 LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
382 gr_blit(surface, 0, 0, w, h, x, y);
383 return y + h;
384}
385
386static void draw_unknown(struct charger *charger)
387{
388 int y;
389 if (charger->surf_unknown) {
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700390 draw_surface_centered(charger->surf_unknown);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700391 } else {
392 android_green();
393 y = draw_text("Charging!", -1, -1);
394 draw_text("?\?/100", -1, y + 25);
395 }
396}
397
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700398static void draw_battery(const struct charger* charger)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700399{
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700400 const struct animation& anim = *charger->batt_anim;
401 const struct animation::frame& frame = anim.frames[anim.cur_frame];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700402
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700403 if (anim.num_frames != 0) {
404 draw_surface_centered(frame.surface);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700405 LOGV("drawing frame #%d min_cap=%d time=%d\n",
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700406 anim.cur_frame, frame.min_level,
407 frame.disp_time);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700408 }
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700409 draw_clock(anim);
410 draw_percent(anim);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700411}
412
413static void redraw_screen(struct charger *charger)
414{
415 struct animation *batt_anim = charger->batt_anim;
416
417 clear_screen();
418
419 /* try to display *something* */
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700420 if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700421 draw_unknown(charger);
422 else
423 draw_battery(charger);
424 gr_flip();
425}
426
427static void kick_animation(struct animation *anim)
428{
429 anim->run = true;
430}
431
432static void reset_animation(struct animation *anim)
433{
434 anim->cur_cycle = 0;
435 anim->cur_frame = 0;
436 anim->run = false;
437}
438
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700439static void init_status_display(struct animation* anim)
440{
441 int res;
442
443 if (!anim->text_clock.font_file.empty()) {
444 if ((res =
445 gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
446 LOGE("Could not load time font (%d)\n", res);
447 }
448 }
449
450 if (!anim->text_percent.font_file.empty()) {
451 if ((res =
452 gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
453 LOGE("Could not load percent font (%d)\n", res);
454 }
455 }
456}
457
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700458static void update_screen_state(struct charger *charger, int64_t now)
459{
460 struct animation *batt_anim = charger->batt_anim;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700461 int disp_time;
462
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700463 if (!batt_anim->run || now < charger->next_screen_transition) return;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700464
Todd Poynora7300272014-06-30 13:15:05 -0700465 if (!minui_inited) {
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700466 if (healthd_config && healthd_config->screen_on) {
467 if (!healthd_config->screen_on(batt_prop)) {
468 LOGV("[%" PRId64 "] leave screen off\n", now);
469 batt_anim->run = false;
470 charger->next_screen_transition = -1;
471 if (charger->charger_connected)
472 request_suspend(true);
473 return;
474 }
Todd Poynora7300272014-06-30 13:15:05 -0700475 }
476
477 gr_init();
Damien Bargiacchi0ee524d2016-08-18 17:33:32 -0700478 gr_font_size(gr_sys_font(), &char_width, &char_height);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700479 init_status_display(batt_anim);
Todd Poynora7300272014-06-30 13:15:05 -0700480
481#ifndef CHARGER_DISABLE_INIT_BLANK
482 gr_fb_blank(true);
483#endif
484 minui_inited = true;
485 }
486
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700487 /* animation is over, blank screen and leave */
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700488 if (batt_anim->num_cycles > 0 && batt_anim->cur_cycle == batt_anim->num_cycles) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700489 reset_animation(batt_anim);
490 charger->next_screen_transition = -1;
491 gr_fb_blank(true);
Colin Crosse1d52472014-05-15 17:49:06 -0700492 LOGV("[%" PRId64 "] animation done\n", now);
Todd Poynor342a2262014-08-18 11:23:11 -0700493 if (charger->charger_connected)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700494 request_suspend(true);
495 return;
496 }
497
498 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
499
500 /* animation starting, set up the animation */
501 if (batt_anim->cur_frame == 0) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700502
Colin Crosse1d52472014-05-15 17:49:06 -0700503 LOGV("[%" PRId64 "] animation starting\n", now);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700504 if (batt_prop) {
505 batt_anim->cur_level = batt_prop->batteryLevel;
506 batt_anim->cur_status = batt_prop->batteryStatus;
507 if (batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
508 /* find first frame given current battery level */
509 for (int i = 0; i < batt_anim->num_frames; i++) {
510 if (batt_anim->cur_level >= batt_anim->frames[i].min_level &&
511 batt_anim->cur_level <= batt_anim->frames[i].max_level) {
512 batt_anim->cur_frame = i;
513 break;
514 }
515 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700516
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700517 // repeat the first frame first_frame_repeats times
518 disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time *
519 batt_anim->first_frame_repeats;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700520 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700521 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700522 }
523
524 /* unblank the screen on first cycle */
525 if (batt_anim->cur_cycle == 0)
526 gr_fb_blank(false);
527
528 /* draw the new frame (@ cur_frame) */
529 redraw_screen(charger);
530
531 /* if we don't have anim frames, we only have one image, so just bump
532 * the cycle counter and exit
533 */
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700534 if (batt_anim->num_frames == 0 || batt_anim->cur_level < 0) {
535 LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700536 charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
537 batt_anim->cur_cycle++;
538 return;
539 }
540
541 /* schedule next screen transition */
542 charger->next_screen_transition = now + disp_time;
543
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700544 /* advance frame cntr to the next valid frame only if we are charging
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700545 * if necessary, advance cycle cntr, and reset frame cntr
546 */
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700547 if (charger->charger_connected) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700548 batt_anim->cur_frame++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700549
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700550 while (batt_anim->cur_frame < batt_anim->num_frames &&
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700551 (batt_anim->cur_level < batt_anim->frames[batt_anim->cur_frame].min_level ||
552 batt_anim->cur_level > batt_anim->frames[batt_anim->cur_frame].max_level)) {
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700553 batt_anim->cur_frame++;
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700554 }
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700555 if (batt_anim->cur_frame >= batt_anim->num_frames) {
556 batt_anim->cur_cycle++;
557 batt_anim->cur_frame = 0;
558
559 /* don't reset the cycle counter, since we use that as a signal
560 * in a test above to check if animation is over
561 */
562 }
563 } else {
564 /* Stop animating if we're not charging.
565 * If we stop it immediately instead of going through this loop, then
566 * the animation would stop somewhere in the middle.
567 */
568 batt_anim->cur_frame = 0;
569 batt_anim->cur_cycle++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700570 }
571}
572
Tao Bao92c26012017-01-18 22:54:54 -0800573static int set_key_callback(struct charger *charger, int code, int value)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700574{
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700575 int64_t now = curr_time_ms();
576 int down = !!value;
577
578 if (code > KEY_MAX)
579 return -1;
580
581 /* ignore events that don't modify our state */
582 if (charger->keys[code].down == down)
583 return 0;
584
585 /* only record the down even timestamp, as the amount
586 * of time the key spent not being pressed is not useful */
587 if (down)
588 charger->keys[code].timestamp = now;
589 charger->keys[code].down = down;
590 charger->keys[code].pending = true;
591 if (down) {
Colin Crosse1d52472014-05-15 17:49:06 -0700592 LOGV("[%" PRId64 "] key[%d] down\n", now, code);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700593 } else {
594 int64_t duration = now - charger->keys[code].timestamp;
595 int64_t secs = duration / 1000;
596 int64_t msecs = duration - secs * 1000;
Colin Crosse1d52472014-05-15 17:49:06 -0700597 LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n",
598 now, code, secs, msecs);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700599 }
600
601 return 0;
602}
603
604static void update_input_state(struct charger *charger,
605 struct input_event *ev)
606{
607 if (ev->type != EV_KEY)
608 return;
Tao Bao92c26012017-01-18 22:54:54 -0800609 set_key_callback(charger, ev->code, ev->value);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700610}
611
612static void set_next_key_check(struct charger *charger,
613 struct key_state *key,
614 int64_t timeout)
615{
616 int64_t then = key->timestamp + timeout;
617
618 if (charger->next_key_check == -1 || then < charger->next_key_check)
619 charger->next_key_check = then;
620}
621
622static void process_key(struct charger *charger, int code, int64_t now)
623{
624 struct key_state *key = &charger->keys[code];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700625
626 if (code == KEY_POWER) {
627 if (key->down) {
628 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
629 if (now >= reboot_timeout) {
Riley Andrews6bd45882014-06-23 15:20:51 -0700630 /* We do not currently support booting from charger mode on
631 all devices. Check the property and continue booting or reboot
632 accordingly. */
633 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700634 LOGW("[%" PRId64 "] booting from charger mode\n", now);
Riley Andrews6bd45882014-06-23 15:20:51 -0700635 property_set("sys.boot_from_charger_mode", "1");
636 } else {
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700637 if (charger->batt_anim->cur_level >= charger->boot_min_cap) {
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700638 LOGW("[%" PRId64 "] rebooting\n", now);
639 android_reboot(ANDROID_RB_RESTART, 0, 0);
640 } else {
Chih-Hung Hsiehc49ceca2015-02-24 10:50:41 -0800641 LOGV("[%" PRId64 "] ignore power-button press, battery level "
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700642 "less than minimum\n", now);
643 }
Riley Andrews6bd45882014-06-23 15:20:51 -0700644 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700645 } else {
646 /* if the key is pressed but timeout hasn't expired,
647 * make sure we wake up at the right-ish time to check
648 */
649 set_next_key_check(charger, key, POWER_ON_KEY_TIME);
Ruchi Kandoi9a11aaa2014-10-22 14:16:35 -0700650
651 /* Turn on the display and kick animation on power-key press
652 * rather than on key release
653 */
654 kick_animation(charger->batt_anim);
655 request_suspend(false);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700656 }
657 } else {
658 /* if the power key got released, force screen state cycle */
659 if (key->pending) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700660 kick_animation(charger->batt_anim);
661 }
662 }
663 }
664
665 key->pending = false;
666}
667
668static void handle_input_state(struct charger *charger, int64_t now)
669{
670 process_key(charger, KEY_POWER, now);
671
672 if (charger->next_key_check != -1 && now > charger->next_key_check)
673 charger->next_key_check = -1;
674}
675
676static void handle_power_supply_state(struct charger *charger, int64_t now)
677{
678 if (!charger->have_battery_state)
679 return;
680
681 if (!charger->charger_connected) {
Ruchi Kandoi9a11aaa2014-10-22 14:16:35 -0700682
683 /* Last cycle would have stopped at the extreme top of battery-icon
684 * Need to show the correct level corresponding to capacity.
685 */
686 kick_animation(charger->batt_anim);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700687 request_suspend(false);
688 if (charger->next_pwr_check == -1) {
689 charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
Todd Poynorebeb0c02014-09-23 14:54:24 -0700690 LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
Colin Crosse1d52472014-05-15 17:49:06 -0700691 now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700692 } else if (now >= charger->next_pwr_check) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700693 LOGW("[%" PRId64 "] shutting down\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700694 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
695 } else {
696 /* otherwise we already have a shutdown timer scheduled */
697 }
698 } else {
699 /* online supply present, reset shutdown timer if set */
700 if (charger->next_pwr_check != -1) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700701 LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700702 kick_animation(charger->batt_anim);
703 }
704 charger->next_pwr_check = -1;
705 }
706}
707
708void healthd_mode_charger_heartbeat()
709{
710 struct charger *charger = &charger_state;
711 int64_t now = curr_time_ms();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700712
713 handle_input_state(charger, now);
714 handle_power_supply_state(charger, now);
715
716 /* do screen update last in case any of the above want to start
717 * screen transitions (animations, etc)
718 */
719 update_screen_state(charger, now);
720}
721
722void healthd_mode_charger_battery_update(
723 struct android::BatteryProperties *props)
724{
725 struct charger *charger = &charger_state;
726
727 charger->charger_connected =
728 props->chargerAcOnline || props->chargerUsbOnline ||
729 props->chargerWirelessOnline;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700730
731 if (!charger->have_battery_state) {
732 charger->have_battery_state = true;
733 charger->next_screen_transition = curr_time_ms() - 1;
734 reset_animation(charger->batt_anim);
735 kick_animation(charger->batt_anim);
736 }
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700737 batt_prop = props;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700738}
739
740int healthd_mode_charger_preparetowait(void)
741{
742 struct charger *charger = &charger_state;
743 int64_t now = curr_time_ms();
744 int64_t next_event = INT64_MAX;
745 int64_t timeout;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700746
Colin Crosse1d52472014-05-15 17:49:06 -0700747 LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now,
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700748 charger->next_screen_transition, charger->next_key_check,
749 charger->next_pwr_check);
750
751 if (charger->next_screen_transition != -1)
752 next_event = charger->next_screen_transition;
753 if (charger->next_key_check != -1 && charger->next_key_check < next_event)
754 next_event = charger->next_key_check;
755 if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
756 next_event = charger->next_pwr_check;
757
758 if (next_event != -1 && next_event != INT64_MAX)
759 timeout = max(0, next_event - now);
760 else
761 timeout = -1;
762
763 return (int)timeout;
764}
765
Tao Bao92c26012017-01-18 22:54:54 -0800766static int input_callback(struct charger *charger, int fd, unsigned int epevents)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700767{
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700768 struct input_event ev;
769 int ret;
770
771 ret = ev_get_input(fd, epevents, &ev);
772 if (ret)
773 return -1;
774 update_input_state(charger, &ev);
775 return 0;
776}
777
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700778static void charger_event_handler(uint32_t /*epevents*/)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700779{
780 int ret;
781
782 ret = ev_wait(-1);
783 if (!ret)
784 ev_dispatch();
785}
786
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700787animation* init_animation()
788{
789 bool parse_success;
790
791 std::string content;
792 if (base::ReadFileToString(animation_desc_path, &content)) {
793 parse_success = parse_animation_desc(content, &battery_animation);
794 } else {
795 LOGW("Could not open animation description at %s\n", animation_desc_path);
796 parse_success = false;
797 }
798
799 if (!parse_success) {
800 LOGW("Could not parse animation description. Using default animation.\n");
801 battery_animation = BASE_ANIMATION;
802 battery_animation.animation_file.assign("charger/battery_scale");
803 battery_animation.frames = default_animation_frames;
804 battery_animation.num_frames = ARRAY_SIZE(default_animation_frames);
805 }
806 if (battery_animation.fail_file.empty()) {
807 battery_animation.fail_file.assign("charger/battery_fail");
808 }
809
810 LOGV("Animation Description:\n");
811 LOGV(" animation: %d %d '%s' (%d)\n",
812 battery_animation.num_cycles, battery_animation.first_frame_repeats,
813 battery_animation.animation_file.c_str(), battery_animation.num_frames);
814 LOGV(" fail_file: '%s'\n", battery_animation.fail_file.c_str());
815 LOGV(" clock: %d %d %d %d %d %d '%s'\n",
816 battery_animation.text_clock.pos_x, battery_animation.text_clock.pos_y,
817 battery_animation.text_clock.color_r, battery_animation.text_clock.color_g,
818 battery_animation.text_clock.color_b, battery_animation.text_clock.color_a,
819 battery_animation.text_clock.font_file.c_str());
820 LOGV(" percent: %d %d %d %d %d %d '%s'\n",
821 battery_animation.text_percent.pos_x, battery_animation.text_percent.pos_y,
822 battery_animation.text_percent.color_r, battery_animation.text_percent.color_g,
823 battery_animation.text_percent.color_b, battery_animation.text_percent.color_a,
824 battery_animation.text_percent.font_file.c_str());
825 for (int i = 0; i < battery_animation.num_frames; i++) {
826 LOGV(" frame %.2d: %d %d %d\n", i, battery_animation.frames[i].disp_time,
827 battery_animation.frames[i].min_level, battery_animation.frames[i].max_level);
828 }
829
830 return &battery_animation;
831}
832
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700833void healthd_mode_charger_init(struct healthd_config* config)
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700834{
835 int ret;
836 struct charger *charger = &charger_state;
837 int i;
838 int epollfd;
839
840 dump_last_kmsg();
841
Todd Poynorebeb0c02014-09-23 14:54:24 -0700842 LOGW("--------------- STARTING CHARGER MODE ---------------\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700843
Tao Bao92c26012017-01-18 22:54:54 -0800844 ret = ev_init(std::bind(&input_callback, charger, std::placeholders::_1,
845 std::placeholders::_2));
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700846 if (!ret) {
847 epollfd = ev_get_epollfd();
Tim Murraye89ea5e2016-10-18 16:35:15 -0700848 healthd_register_event(epollfd, charger_event_handler, EVENT_WAKEUP_FD);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700849 }
850
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700851 struct animation* anim = init_animation();
852 charger->batt_anim = anim;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700853
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700854 ret = res_create_display_surface(anim->fail_file.c_str(), &charger->surf_unknown);
855 if (ret < 0) {
856 LOGE("Cannot load custom battery_fail image. Reverting to built in.\n");
857 ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
858 if (ret < 0) {
859 LOGE("Cannot load built in battery_fail image\n");
860 charger->surf_unknown = NULL;
861 }
862 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700863
Elliott Hughes9e85ea12015-04-15 10:25:55 -0700864 GRSurface** scale_frames;
Doug Zongkeree6ef152014-03-11 08:42:09 -0700865 int scale_count;
Tao Bao0db80232015-12-16 10:57:10 -0800866 int scale_fps; // Not in use (charger/battery_scale doesn't have FPS text
867 // chunk). We are using hard-coded frame.disp_time instead.
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700868 ret = res_create_multi_display_surface(anim->animation_file.c_str(),
869 &scale_count, &scale_fps, &scale_frames);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700870 if (ret < 0) {
871 LOGE("Cannot load battery_scale image\n");
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700872 anim->num_frames = 0;
873 anim->num_cycles = 1;
874 } else if (scale_count != anim->num_frames) {
Doug Zongkeree6ef152014-03-11 08:42:09 -0700875 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700876 scale_count, anim->num_frames);
877 anim->num_frames = 0;
878 anim->num_cycles = 1;
Doug Zongkeree6ef152014-03-11 08:42:09 -0700879 } else {
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700880 for (i = 0; i < anim->num_frames; i++) {
881 anim->frames[i].surface = scale_frames[i];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700882 }
883 }
Tao Bao92c26012017-01-18 22:54:54 -0800884 ev_sync_key_state(std::bind(&set_key_callback, charger, std::placeholders::_1,
885 std::placeholders::_2));
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700886
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700887 charger->next_screen_transition = -1;
888 charger->next_key_check = -1;
889 charger->next_pwr_check = -1;
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700890 healthd_config = config;
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700891 charger->boot_min_cap = config->boot_min_cap;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700892}