blob: 9fe85d40d70b25005eeeb628c67eccf1058c539e [file] [log] [blame]
Todd Poynorfea5b4d2013-09-09 12:09:08 -07001/*
Luke Song1d540dd2017-07-13 15:10:35 -07002 * Copyright (C) 2011-2017 The Android Open Source Project
Todd Poynorfea5b4d2013-09-09 12:09:08 -07003 *
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
Yifan Hongb5d70332021-10-20 17:15:32 -070017#include <charger/healthd_mode_charger.h>
Yifan Hong7dcf7b02019-10-08 17:27:11 -070018
Todd Poynorfea5b4d2013-09-09 12:09:08 -070019#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
Colin Crosse1d52472014-05-15 17:49:06 -070022#include <inttypes.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070023#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
Yifan Hong7dcf7b02019-10-08 17:27:11 -070033#include <optional>
Tao Bao92c26012017-01-18 22:54:54 -080034
Damien Bargiacchi565ba022016-08-11 15:29:50 -070035#include <android-base/file.h>
Yifan Hongabda7152020-08-05 16:15:15 -070036#include <android-base/logging.h>
Mark Salyzyn26f1dd72017-06-27 09:19:09 -070037#include <android-base/macros.h>
Yifan Hongabda7152020-08-05 16:15:15 -070038#include <android-base/strings.h>
Damien Bargiacchi565ba022016-08-11 15:29:50 -070039
Todd Poynorfea5b4d2013-09-09 12:09:08 -070040#include <linux/netlink.h>
Luke Song1d540dd2017-07-13 15:10:35 -070041#include <sys/socket.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070042
Yifan Hongdc9c08b2019-03-25 18:09:35 -070043#include <cutils/android_get_control_file.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070044#include <cutils/klog.h>
45#include <cutils/misc.h>
Riley Andrews6bd45882014-06-23 15:20:51 -070046#include <cutils/properties.h>
Luke Song1d540dd2017-07-13 15:10:35 -070047#include <cutils/uevent.h>
Todd Poynorc8183bb2017-01-31 15:51:50 -080048#include <sys/reboot.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070049
Todd Poynorfea5b4d2013-09-09 12:09:08 -070050#include <suspend/autosuspend.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070051
Damien Bargiacchi565ba022016-08-11 15:29:50 -070052#include "AnimationParser.h"
Luke Song1d540dd2017-07-13 15:10:35 -070053#include "healthd_draw.h"
Todd Poynorfea5b4d2013-09-09 12:09:08 -070054
Yifan Hongb5d70332021-10-20 17:15:32 -070055#include <aidl/android/hardware/health/BatteryStatus.h>
56#include <health/HealthLoop.h>
Yabin Cuie98e1772016-02-17 12:21:34 -080057#include <healthd/healthd.h>
Todd Poynorfea5b4d2013-09-09 12:09:08 -070058
Yifan Honge3ffd1b2021-10-20 22:18:16 -070059#if !defined(__ANDROID_VNDK__)
60#include "charger.sysprop.h"
61#endif
62
Yifan Hongabda7152020-08-05 16:15:15 -070063using std::string_literals::operator""s;
Damien Bargiacchi565ba022016-08-11 15:29:50 -070064using namespace android;
Yifan Hongb5d70332021-10-20 17:15:32 -070065using aidl::android::hardware::health::BatteryStatus;
Yifan Hong7dcf7b02019-10-08 17:27:11 -070066using android::hardware::health::HealthLoop;
Damien Bargiacchi565ba022016-08-11 15:29:50 -070067
Tao Bao5747e222018-09-11 10:46:35 -070068// main healthd loop
69extern int healthd_main(void);
70
Yifan Hong7dcf7b02019-10-08 17:27:11 -070071// minui globals
Luke Song1d540dd2017-07-13 15:10:35 -070072char* locale;
Colin Cross1c38f5d2014-02-13 13:34:37 -080073
Todd Poynorfea5b4d2013-09-09 12:09:08 -070074#ifndef max
Luke Song1d540dd2017-07-13 15:10:35 -070075#define max(a, b) ((a) > (b) ? (a) : (b))
Todd Poynorfea5b4d2013-09-09 12:09:08 -070076#endif
77
78#ifndef min
Luke Song1d540dd2017-07-13 15:10:35 -070079#define min(a, b) ((a) < (b) ? (a) : (b))
Todd Poynorfea5b4d2013-09-09 12:09:08 -070080#endif
81
Luke Song1d540dd2017-07-13 15:10:35 -070082#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Todd Poynorfea5b4d2013-09-09 12:09:08 -070083
Luke Song1d540dd2017-07-13 15:10:35 -070084#define MSEC_PER_SEC (1000LL)
85#define NSEC_PER_MSEC (1000000LL)
Todd Poynorfea5b4d2013-09-09 12:09:08 -070086
Luke Song1d540dd2017-07-13 15:10:35 -070087#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
88#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
Todd Poynorfea5b4d2013-09-09 12:09:08 -070089#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
kentsouba61ea42018-07-17 17:49:34 +080090#define UNPLUGGED_DISPLAY_TIME (3 * MSEC_PER_SEC)
Ken Tsou6c7ece72019-02-15 10:50:58 +080091#define MAX_BATT_LEVEL_WAIT_TIME (3 * MSEC_PER_SEC)
John Zhao5cac1002019-03-31 17:22:47 +080092#define UNPLUGGED_SHUTDOWN_TIME_PROP "ro.product.charger.unplugged_shutdown_time"
Todd Poynorfea5b4d2013-09-09 12:09:08 -070093
Luke Song1d540dd2017-07-13 15:10:35 -070094#define LAST_KMSG_MAX_SZ (32 * 1024)
Todd Poynorfea5b4d2013-09-09 12:09:08 -070095
Luke Song1d540dd2017-07-13 15:10:35 -070096#define LOGE(x...) KLOG_ERROR("charger", x);
97#define LOGW(x...) KLOG_WARNING("charger", x);
98#define LOGV(x...) KLOG_DEBUG("charger", x);
Todd Poynorfea5b4d2013-09-09 12:09:08 -070099
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700100namespace android {
101
Yifan Hongac748362021-10-26 17:20:25 -0700102#if defined(__ANDROID_VNDK__)
103static constexpr const char* vendor_animation_desc_path =
104 "/vendor/etc/res/values/charger/animation.txt";
105static constexpr const char* vendor_animation_root = "/vendor/etc/res/images/";
106static constexpr const char* vendor_default_animation_root = "/vendor/etc/res/images/default/";
107#else
108
Yifan Hongabda7152020-08-05 16:15:15 -0700109// Legacy animation resources are loaded from this directory.
110static constexpr const char* legacy_animation_root = "/res/images/";
111
112// Built-in animation resources are loaded from this directory.
113static constexpr const char* system_animation_root = "/system/etc/res/images/";
114
115// Resources in /product/etc/res overrides resources in /res and /system/etc/res.
Yifan Hong082d2952019-01-28 12:55:54 -0800116// If the device is using the Generic System Image (GSI), resources may exist in
117// both paths.
118static constexpr const char* product_animation_desc_path =
119 "/product/etc/res/values/charger/animation.txt";
120static constexpr const char* product_animation_root = "/product/etc/res/images/";
121static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt";
Yifan Hongac748362021-10-26 17:20:25 -0700122#endif
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700123
Luke Song1d540dd2017-07-13 15:10:35 -0700124static const animation BASE_ANIMATION = {
125 .text_clock =
126 {
127 .pos_x = 0,
128 .pos_y = 0,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700129
Luke Song1d540dd2017-07-13 15:10:35 -0700130 .color_r = 255,
131 .color_g = 255,
132 .color_b = 255,
133 .color_a = 255,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700134
Luke Song1d540dd2017-07-13 15:10:35 -0700135 .font = nullptr,
136 },
137 .text_percent =
138 {
139 .pos_x = 0,
140 .pos_y = 0,
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700141
Luke Song1d540dd2017-07-13 15:10:35 -0700142 .color_r = 255,
143 .color_g = 255,
144 .color_b = 255,
145 .color_a = 255,
146 },
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700147
148 .run = false,
149
150 .frames = nullptr,
151 .cur_frame = 0,
152 .num_frames = 0,
153 .first_frame_repeats = 2,
154
155 .cur_cycle = 0,
156 .num_cycles = 3,
157
158 .cur_level = 0,
159 .cur_status = BATTERY_STATUS_UNKNOWN,
160};
161
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700162void Charger::InitDefaultAnimationFrames() {
163 owned_frames_ = {
164 {
165 .disp_time = 750,
166 .min_level = 0,
167 .max_level = 19,
168 .surface = NULL,
169 },
170 {
171 .disp_time = 750,
172 .min_level = 0,
173 .max_level = 39,
174 .surface = NULL,
175 },
176 {
177 .disp_time = 750,
178 .min_level = 0,
179 .max_level = 59,
180 .surface = NULL,
181 },
182 {
183 .disp_time = 750,
184 .min_level = 0,
185 .max_level = 79,
186 .surface = NULL,
187 },
188 {
189 .disp_time = 750,
190 .min_level = 80,
191 .max_level = 95,
192 .surface = NULL,
193 },
194 {
195 .disp_time = 750,
196 .min_level = 0,
197 .max_level = 100,
198 .surface = NULL,
199 },
200 };
201}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700202
Yifan Hongb5d70332021-10-20 17:15:32 -0700203Charger::Charger(ChargerConfigurationInterface* configuration)
204 : batt_anim_(BASE_ANIMATION), configuration_(configuration) {}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700205
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700206Charger::~Charger() {}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700207
208/* current time in milliseconds */
Luke Song1d540dd2017-07-13 15:10:35 -0700209static int64_t curr_time_ms() {
210 timespec tm;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700211 clock_gettime(CLOCK_MONOTONIC, &tm);
212 return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
213}
214
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700215#define MAX_KLOG_WRITE_BUF_SZ 256
216
Luke Song1d540dd2017-07-13 15:10:35 -0700217static void dump_last_kmsg(void) {
Yifan Hongdc9c08b2019-03-25 18:09:35 -0700218 std::string buf;
Luke Song1d540dd2017-07-13 15:10:35 -0700219 char* ptr;
Yifan Hongdc9c08b2019-03-25 18:09:35 -0700220 size_t len;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700221
Todd Poynorebeb0c02014-09-23 14:54:24 -0700222 LOGW("*************** LAST KMSG ***************\n");
Mark Salyzyn26f1dd72017-06-27 09:19:09 -0700223 const char* kmsg[] = {
224 // clang-format off
225 "/sys/fs/pstore/console-ramoops-0",
226 "/sys/fs/pstore/console-ramoops",
227 "/proc/last_kmsg",
228 // clang-format on
229 };
Yifan Hongdc9c08b2019-03-25 18:09:35 -0700230 for (size_t i = 0; i < arraysize(kmsg) && buf.empty(); ++i) {
231 auto fd = android_get_control_file(kmsg[i]);
232 if (fd >= 0) {
233 android::base::ReadFdToString(fd, &buf);
234 } else {
235 android::base::ReadFileToString(kmsg[i], &buf);
236 }
Mark Salyzyn26f1dd72017-06-27 09:19:09 -0700237 }
Todd Poynorcd7c1042013-11-22 17:52:59 -0800238
Yifan Hongdc9c08b2019-03-25 18:09:35 -0700239 if (buf.empty()) {
Mark Salyzyn26f1dd72017-06-27 09:19:09 -0700240 LOGW("last_kmsg not found. Cold reset?\n");
241 goto out;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700242 }
243
Yifan Hongdc9c08b2019-03-25 18:09:35 -0700244 len = min(buf.size(), LAST_KMSG_MAX_SZ);
245 ptr = &buf[buf.size() - len];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700246
247 while (len > 0) {
Yifan Hongdc9c08b2019-03-25 18:09:35 -0700248 size_t cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700249 char yoink;
Luke Song1d540dd2017-07-13 15:10:35 -0700250 char* nl;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700251
Luke Song1d540dd2017-07-13 15:10:35 -0700252 nl = (char*)memrchr(ptr, '\n', cnt - 1);
253 if (nl) cnt = nl - ptr + 1;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700254
255 yoink = ptr[cnt];
256 ptr[cnt] = '\0';
Todd Poynorebeb0c02014-09-23 14:54:24 -0700257 klog_write(6, "<4>%s", ptr);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700258 ptr[cnt] = yoink;
259
260 len -= cnt;
261 ptr += cnt;
262 }
263
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700264out:
Todd Poynorebeb0c02014-09-23 14:54:24 -0700265 LOGW("************* END LAST KMSG *************\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700266}
267
Yifan Hong8e551342021-10-26 22:51:29 -0700268int Charger::RequestEnableSuspend() {
269 if (!configuration_->ChargerEnableSuspend()) {
Yifan Hong97eecdc2019-07-03 11:07:37 -0700270 return 0;
271 }
Yifan Hong8e551342021-10-26 22:51:29 -0700272 return autosuspend_enable();
273}
Yifan Hong97eecdc2019-07-03 11:07:37 -0700274
Yifan Hong8e551342021-10-26 22:51:29 -0700275int Charger::RequestDisableSuspend() {
276 if (!configuration_->ChargerEnableSuspend()) {
277 return 0;
278 }
279 return autosuspend_disable();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700280}
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700281
Luke Song1d540dd2017-07-13 15:10:35 -0700282static void kick_animation(animation* anim) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700283 anim->run = true;
284}
285
Luke Song1d540dd2017-07-13 15:10:35 -0700286static void reset_animation(animation* anim) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700287 anim->cur_cycle = 0;
288 anim->cur_frame = 0;
289 anim->run = false;
290}
291
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700292void Charger::UpdateScreenState(int64_t now) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700293 int disp_time;
294
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700295 if (!batt_anim_.run || now < next_screen_transition_) return;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700296
Ken Tsou6c7ece72019-02-15 10:50:58 +0800297 // If battery level is not ready, keep checking in the defined time
Yifan Hongb5d70332021-10-20 17:15:32 -0700298 if (health_info_.battery_level == 0 && health_info_.battery_status == BatteryStatus::UNKNOWN) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700299 if (wait_batt_level_timestamp_ == 0) {
Ken Tsou6c7ece72019-02-15 10:50:58 +0800300 // Set max delay time and skip drawing screen
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700301 wait_batt_level_timestamp_ = now + MAX_BATT_LEVEL_WAIT_TIME;
Ken Tsou6c7ece72019-02-15 10:50:58 +0800302 LOGV("[%" PRId64 "] wait for battery capacity ready\n", now);
303 return;
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700304 } else if (now <= wait_batt_level_timestamp_) {
Ken Tsou6c7ece72019-02-15 10:50:58 +0800305 // Do nothing, keep waiting
306 return;
307 }
308 // If timeout and battery level is still not ready, draw unknown battery
309 }
310
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700311 if (healthd_draw_ == nullptr) {
Yifan Hongb5d70332021-10-20 17:15:32 -0700312 std::optional<bool> out_screen_on = configuration_->ChargerShouldKeepScreenOn();
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700313 if (out_screen_on.has_value()) {
314 if (!*out_screen_on) {
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700315 LOGV("[%" PRId64 "] leave screen off\n", now);
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700316 batt_anim_.run = false;
317 next_screen_transition_ = -1;
Yifan Hongb5d70332021-10-20 17:15:32 -0700318 if (configuration_->ChargerIsOnline()) {
Yifan Hong8e551342021-10-26 22:51:29 -0700319 RequestEnableSuspend();
Yifan Hongb5d70332021-10-20 17:15:32 -0700320 }
Ruchi Kandoibdf11c72014-09-25 19:44:42 -0700321 return;
322 }
Todd Poynora7300272014-06-30 13:15:05 -0700323 }
324
Xiaohui Niua40d8722021-08-31 16:22:25 +0800325 healthd_draw_ = HealthdDraw::Create(&batt_anim_);
326 if (healthd_draw_ == nullptr) return;
Todd Poynora7300272014-06-30 13:15:05 -0700327
Yifan Honge3ffd1b2021-10-20 22:18:16 -0700328#if !defined(__ANDROID_VNDK__)
Yifan Hong97eecdc2019-07-03 11:07:37 -0700329 if (android::sysprop::ChargerProperties::disable_init_blank().value_or(false)) {
Jack Wuc1b17112021-09-29 22:27:56 +0800330 healthd_draw_->blank_screen(true, static_cast<int>(drm_));
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700331 screen_blanked_ = true;
Yifan Hong97eecdc2019-07-03 11:07:37 -0700332 }
Yifan Honge3ffd1b2021-10-20 22:18:16 -0700333#endif
Todd Poynora7300272014-06-30 13:15:05 -0700334 }
335
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700336 /* animation is over, blank screen and leave */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700337 if (batt_anim_.num_cycles > 0 && batt_anim_.cur_cycle == batt_anim_.num_cycles) {
338 reset_animation(&batt_anim_);
339 next_screen_transition_ = -1;
Jack Wuc1b17112021-09-29 22:27:56 +0800340 healthd_draw_->blank_screen(true, static_cast<int>(drm_));
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700341 screen_blanked_ = true;
Colin Crosse1d52472014-05-15 17:49:06 -0700342 LOGV("[%" PRId64 "] animation done\n", now);
Yifan Hongb5d70332021-10-20 17:15:32 -0700343 if (configuration_->ChargerIsOnline()) {
Yifan Hong8e551342021-10-26 22:51:29 -0700344 RequestEnableSuspend();
Yifan Hongb5d70332021-10-20 17:15:32 -0700345 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700346 return;
347 }
348
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700349 disp_time = batt_anim_.frames[batt_anim_.cur_frame].disp_time;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700350
Jack Wu3d637a92021-09-27 17:40:40 +0800351 /* turn off all screen */
352 if (screen_switch_ == SCREEN_SWITCH_ENABLE) {
353 healthd_draw_->blank_screen(true, 0 /* drm */);
354 healthd_draw_->blank_screen(true, 1 /* drm */);
Jack Wu56540a02021-10-22 17:10:37 +0800355 healthd_draw_->rotate_screen(static_cast<int>(drm_));
Jack Wu3d637a92021-09-27 17:40:40 +0800356 screen_blanked_ = true;
357 screen_switch_ = SCREEN_SWITCH_DISABLE;
358 }
359
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700360 if (screen_blanked_) {
Jack Wuc1b17112021-09-29 22:27:56 +0800361 healthd_draw_->blank_screen(false, static_cast<int>(drm_));
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700362 screen_blanked_ = false;
kentsouba61ea42018-07-17 17:49:34 +0800363 }
Thierry Strudelac2aa7d2018-05-23 15:48:46 -0700364
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700365 /* animation starting, set up the animation */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700366 if (batt_anim_.cur_frame == 0) {
Colin Crosse1d52472014-05-15 17:49:06 -0700367 LOGV("[%" PRId64 "] animation starting\n", now);
Yifan Hongb5d70332021-10-20 17:15:32 -0700368 batt_anim_.cur_level = health_info_.battery_level;
369 batt_anim_.cur_status = (int)health_info_.battery_status;
370 if (health_info_.battery_level >= 0 && batt_anim_.num_frames != 0) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700371 /* find first frame given current battery level */
372 for (int i = 0; i < batt_anim_.num_frames; i++) {
373 if (batt_anim_.cur_level >= batt_anim_.frames[i].min_level &&
374 batt_anim_.cur_level <= batt_anim_.frames[i].max_level) {
375 batt_anim_.cur_frame = i;
376 break;
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700377 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700378 }
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700379
Yifan Hongb5d70332021-10-20 17:15:32 -0700380 if (configuration_->ChargerIsOnline()) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700381 // repeat the first frame first_frame_repeats times
382 disp_time = batt_anim_.frames[batt_anim_.cur_frame].disp_time *
383 batt_anim_.first_frame_repeats;
384 } else {
385 disp_time = UNPLUGGED_DISPLAY_TIME / batt_anim_.num_cycles;
386 }
387
388 LOGV("cur_frame=%d disp_time=%d\n", batt_anim_.cur_frame, disp_time);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700389 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700390 }
391
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700392 /* draw the new frame (@ cur_frame) */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700393 healthd_draw_->redraw_screen(&batt_anim_, surf_unknown_);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700394
395 /* if we don't have anim frames, we only have one image, so just bump
396 * the cycle counter and exit
397 */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700398 if (batt_anim_.num_frames == 0 || batt_anim_.cur_level < 0) {
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700399 LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now);
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700400 next_screen_transition_ = now + BATTERY_UNKNOWN_TIME;
401 batt_anim_.cur_cycle++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700402 return;
403 }
404
405 /* schedule next screen transition */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700406 next_screen_transition_ = curr_time_ms() + disp_time;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700407
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700408 /* advance frame cntr to the next valid frame only if we are charging
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700409 * if necessary, advance cycle cntr, and reset frame cntr
410 */
Yifan Hongb5d70332021-10-20 17:15:32 -0700411 if (configuration_->ChargerIsOnline()) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700412 batt_anim_.cur_frame++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700413
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700414 while (batt_anim_.cur_frame < batt_anim_.num_frames &&
415 (batt_anim_.cur_level < batt_anim_.frames[batt_anim_.cur_frame].min_level ||
416 batt_anim_.cur_level > batt_anim_.frames[batt_anim_.cur_frame].max_level)) {
417 batt_anim_.cur_frame++;
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700418 }
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700419 if (batt_anim_.cur_frame >= batt_anim_.num_frames) {
420 batt_anim_.cur_cycle++;
421 batt_anim_.cur_frame = 0;
Ruchi Kandoi9015eaa2014-06-23 11:13:15 -0700422
423 /* don't reset the cycle counter, since we use that as a signal
424 * in a test above to check if animation is over
425 */
426 }
427 } else {
428 /* Stop animating if we're not charging.
429 * If we stop it immediately instead of going through this loop, then
430 * the animation would stop somewhere in the middle.
431 */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700432 batt_anim_.cur_frame = 0;
433 batt_anim_.cur_cycle++;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700434 }
435}
436
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700437int Charger::SetKeyCallback(int code, int value) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700438 int64_t now = curr_time_ms();
439 int down = !!value;
440
Luke Song1d540dd2017-07-13 15:10:35 -0700441 if (code > KEY_MAX) return -1;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700442
443 /* ignore events that don't modify our state */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700444 if (keys_[code].down == down) return 0;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700445
446 /* only record the down even timestamp, as the amount
447 * of time the key spent not being pressed is not useful */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700448 if (down) keys_[code].timestamp = now;
449 keys_[code].down = down;
450 keys_[code].pending = true;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700451 if (down) {
Colin Crosse1d52472014-05-15 17:49:06 -0700452 LOGV("[%" PRId64 "] key[%d] down\n", now, code);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700453 } else {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700454 int64_t duration = now - keys_[code].timestamp;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700455 int64_t secs = duration / 1000;
456 int64_t msecs = duration - secs * 1000;
Luke Song1d540dd2017-07-13 15:10:35 -0700457 LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n", now, code,
458 secs, msecs);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700459 }
460
461 return 0;
462}
463
Jack Wu3d637a92021-09-27 17:40:40 +0800464int Charger::SetSwCallback(int code, int value) {
465 if (code > SW_MAX) return -1;
466 if (code == SW_LID) {
467 if ((screen_switch_ == SCREEN_SWITCH_DEFAULT) || ((value != 0) && (drm_ == DRM_INNER)) ||
468 ((value == 0) && (drm_ == DRM_OUTER))) {
469 screen_switch_ = SCREEN_SWITCH_ENABLE;
470 drm_ = (value != 0) ? DRM_OUTER : DRM_INNER;
471 keys_[code].pending = true;
472 }
473 }
474
475 return 0;
476}
477
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700478void Charger::UpdateInputState(input_event* ev) {
Jack Wu3d637a92021-09-27 17:40:40 +0800479 if (ev->type == EV_SW && ev->code == SW_LID) {
480 SetSwCallback(ev->code, ev->value);
481 return;
482 }
483
Luke Song1d540dd2017-07-13 15:10:35 -0700484 if (ev->type != EV_KEY) return;
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700485 SetKeyCallback(ev->code, ev->value);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700486}
487
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700488void Charger::SetNextKeyCheck(key_state* key, int64_t timeout) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700489 int64_t then = key->timestamp + timeout;
490
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700491 if (next_key_check_ == -1 || then < next_key_check_) next_key_check_ = then;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700492}
493
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700494void Charger::ProcessKey(int code, int64_t now) {
495 key_state* key = &keys_[code];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700496
497 if (code == KEY_POWER) {
498 if (key->down) {
499 int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
500 if (now >= reboot_timeout) {
Riley Andrews6bd45882014-06-23 15:20:51 -0700501 /* We do not currently support booting from charger mode on
502 all devices. Check the property and continue booting or reboot
503 accordingly. */
504 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700505 LOGW("[%" PRId64 "] booting from charger mode\n", now);
Riley Andrews6bd45882014-06-23 15:20:51 -0700506 property_set("sys.boot_from_charger_mode", "1");
507 } else {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700508 if (batt_anim_.cur_level >= boot_min_cap_) {
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700509 LOGW("[%" PRId64 "] rebooting\n", now);
Todd Poynorc8183bb2017-01-31 15:51:50 -0800510 reboot(RB_AUTOBOOT);
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700511 } else {
Luke Song1d540dd2017-07-13 15:10:35 -0700512 LOGV("[%" PRId64
513 "] ignore power-button press, battery level "
514 "less than minimum\n",
515 now);
Ruchi Kandoia84b1f62014-10-21 18:24:11 -0700516 }
Riley Andrews6bd45882014-06-23 15:20:51 -0700517 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700518 } else {
519 /* if the key is pressed but timeout hasn't expired,
520 * make sure we wake up at the right-ish time to check
521 */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700522 SetNextKeyCheck(key, POWER_ON_KEY_TIME);
Ruchi Kandoi9a11aaa2014-10-22 14:16:35 -0700523
Luke Song1d540dd2017-07-13 15:10:35 -0700524 /* Turn on the display and kick animation on power-key press
525 * rather than on key release
526 */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700527 kick_animation(&batt_anim_);
Yifan Hong8e551342021-10-26 22:51:29 -0700528 RequestDisableSuspend();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700529 }
530 } else {
531 /* if the power key got released, force screen state cycle */
532 if (key->pending) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700533 kick_animation(&batt_anim_);
Yifan Hong8e551342021-10-26 22:51:29 -0700534 RequestDisableSuspend();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700535 }
536 }
537 }
538
539 key->pending = false;
540}
541
Jack Wu3d637a92021-09-27 17:40:40 +0800542void Charger::ProcessHallSensor(int code) {
543 key_state* key = &keys_[code];
544
545 if (code == SW_LID) {
546 if (key->pending) {
547 reset_animation(&batt_anim_);
548 kick_animation(&batt_anim_);
549 RequestDisableSuspend();
550 }
551 }
552
553 key->pending = false;
554}
555
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700556void Charger::HandleInputState(int64_t now) {
557 ProcessKey(KEY_POWER, now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700558
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700559 if (next_key_check_ != -1 && now > next_key_check_) next_key_check_ = -1;
Jack Wu3d637a92021-09-27 17:40:40 +0800560
561 ProcessHallSensor(SW_LID);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700562}
563
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700564void Charger::HandlePowerSupplyState(int64_t now) {
John Zhao5cac1002019-03-31 17:22:47 +0800565 int timer_shutdown = UNPLUGGED_SHUTDOWN_TIME;
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700566 if (!have_battery_state_) return;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700567
Yifan Hongb5d70332021-10-20 17:15:32 -0700568 if (!configuration_->ChargerIsOnline()) {
Yifan Hong8e551342021-10-26 22:51:29 -0700569 RequestDisableSuspend();
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700570 if (next_pwr_check_ == -1) {
kentsouba61ea42018-07-17 17:49:34 +0800571 /* Last cycle would have stopped at the extreme top of battery-icon
572 * Need to show the correct level corresponding to capacity.
573 *
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700574 * Reset next_screen_transition_ to update screen immediately.
kentsouba61ea42018-07-17 17:49:34 +0800575 * Reset & kick animation to show complete animation cycles
576 * when charger disconnected.
577 */
John Zhao5cac1002019-03-31 17:22:47 +0800578 timer_shutdown =
579 property_get_int32(UNPLUGGED_SHUTDOWN_TIME_PROP, UNPLUGGED_SHUTDOWN_TIME);
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700580 next_screen_transition_ = now - 1;
581 reset_animation(&batt_anim_);
582 kick_animation(&batt_anim_);
583 next_pwr_check_ = now + timer_shutdown;
Todd Poynorebeb0c02014-09-23 14:54:24 -0700584 LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700585 now, (int64_t)timer_shutdown, next_pwr_check_);
586 } else if (now >= next_pwr_check_) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700587 LOGW("[%" PRId64 "] shutting down\n", now);
Todd Poynorc8183bb2017-01-31 15:51:50 -0800588 reboot(RB_POWER_OFF);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700589 } else {
590 /* otherwise we already have a shutdown timer scheduled */
591 }
592 } else {
593 /* online supply present, reset shutdown timer if set */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700594 if (next_pwr_check_ != -1) {
595 /* Reset next_screen_transition_ to update screen immediately.
kentsouba61ea42018-07-17 17:49:34 +0800596 * Reset & kick animation to show complete animation cycles
597 * when charger connected again.
598 */
Yifan Hong8e551342021-10-26 22:51:29 -0700599 RequestDisableSuspend();
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700600 next_screen_transition_ = now - 1;
601 reset_animation(&batt_anim_);
602 kick_animation(&batt_anim_);
kentsouba61ea42018-07-17 17:49:34 +0800603 LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700604 }
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700605 next_pwr_check_ = -1;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700606 }
607}
608
Yifan Hongb5d70332021-10-20 17:15:32 -0700609void Charger::OnHeartbeat() {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700610 // charger* charger = &charger_state;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700611 int64_t now = curr_time_ms();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700612
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700613 HandleInputState(now);
614 HandlePowerSupplyState(now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700615
616 /* do screen update last in case any of the above want to start
617 * screen transitions (animations, etc)
618 */
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700619 UpdateScreenState(now);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700620}
621
Yifan Hongb5d70332021-10-20 17:15:32 -0700622void Charger::OnHealthInfoChanged(const ChargerHealthInfo& health_info) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700623 if (!have_battery_state_) {
624 have_battery_state_ = true;
625 next_screen_transition_ = curr_time_ms() - 1;
Yifan Hong8e551342021-10-26 22:51:29 -0700626 RequestDisableSuspend();
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700627 reset_animation(&batt_anim_);
628 kick_animation(&batt_anim_);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700629 }
Yifan Hongb5d70332021-10-20 17:15:32 -0700630 health_info_ = health_info;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700631}
632
Yifan Hongb5d70332021-10-20 17:15:32 -0700633int Charger::OnPrepareToWait(void) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700634 int64_t now = curr_time_ms();
635 int64_t next_event = INT64_MAX;
636 int64_t timeout;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700637
Luke Song1d540dd2017-07-13 15:10:35 -0700638 LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n",
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700639 now, next_screen_transition_, next_key_check_, next_pwr_check_);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700640
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700641 if (next_screen_transition_ != -1) next_event = next_screen_transition_;
642 if (next_key_check_ != -1 && next_key_check_ < next_event) next_event = next_key_check_;
643 if (next_pwr_check_ != -1 && next_pwr_check_ < next_event) next_event = next_pwr_check_;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700644
645 if (next_event != -1 && next_event != INT64_MAX)
646 timeout = max(0, next_event - now);
647 else
648 timeout = -1;
649
Luke Song1d540dd2017-07-13 15:10:35 -0700650 return (int)timeout;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700651}
652
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700653int Charger::InputCallback(int fd, unsigned int epevents) {
Luke Song1d540dd2017-07-13 15:10:35 -0700654 input_event ev;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700655 int ret;
656
657 ret = ev_get_input(fd, epevents, &ev);
Luke Song1d540dd2017-07-13 15:10:35 -0700658 if (ret) return -1;
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700659 UpdateInputState(&ev);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700660 return 0;
661}
662
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700663static void charger_event_handler(HealthLoop* /*charger_loop*/, uint32_t /*epevents*/) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700664 int ret;
665
666 ret = ev_wait(-1);
Luke Song1d540dd2017-07-13 15:10:35 -0700667 if (!ret) ev_dispatch();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700668}
669
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700670void Charger::InitAnimation() {
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700671 bool parse_success;
672
673 std::string content;
Yifan Hongac748362021-10-26 17:20:25 -0700674
675#if defined(__ANDROID_VNDK__)
676 if (base::ReadFileToString(vendor_animation_desc_path, &content)) {
677 parse_success = parse_animation_desc(content, &batt_anim_);
678 batt_anim_.set_resource_root(vendor_animation_root);
679 } else {
680 LOGW("Could not open animation description at %s\n", vendor_animation_desc_path);
681 parse_success = false;
682 }
683#else
Yifan Hong082d2952019-01-28 12:55:54 -0800684 if (base::ReadFileToString(product_animation_desc_path, &content)) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700685 parse_success = parse_animation_desc(content, &batt_anim_);
686 batt_anim_.set_resource_root(product_animation_root);
Yifan Hong082d2952019-01-28 12:55:54 -0800687 } else if (base::ReadFileToString(animation_desc_path, &content)) {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700688 parse_success = parse_animation_desc(content, &batt_anim_);
Yifan Hongabda7152020-08-05 16:15:15 -0700689 // Fallback resources always exist in system_animation_root. On legacy devices with an old
690 // ramdisk image, resources may be overridden under root. For example,
691 // /res/images/charger/battery_fail.png may not be the same as
692 // system/core/healthd/images/battery_fail.png in the source tree, but is a device-specific
693 // image. Hence, load from /res, and fall back to /system/etc/res.
694 batt_anim_.set_resource_root(legacy_animation_root, system_animation_root);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700695 } else {
696 LOGW("Could not open animation description at %s\n", animation_desc_path);
697 parse_success = false;
698 }
Yifan Hongac748362021-10-26 17:20:25 -0700699#endif
700
701#if defined(__ANDROID_VNDK__)
702 auto default_animation_root = vendor_default_animation_root;
703#else
704 auto default_animation_root = system_animation_root;
705#endif
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700706
707 if (!parse_success) {
Yifan Hongac748362021-10-26 17:20:25 -0700708 LOGW("Could not parse animation description. "
709 "Using default animation with resources at %s\n",
710 default_animation_root);
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700711 batt_anim_ = BASE_ANIMATION;
Yifan Hongac748362021-10-26 17:20:25 -0700712 batt_anim_.animation_file.assign(default_animation_root + "charger/battery_scale.png"s);
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700713 InitDefaultAnimationFrames();
714 batt_anim_.frames = owned_frames_.data();
715 batt_anim_.num_frames = owned_frames_.size();
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700716 }
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700717 if (batt_anim_.fail_file.empty()) {
Yifan Hongac748362021-10-26 17:20:25 -0700718 batt_anim_.fail_file.assign(default_animation_root + "charger/battery_fail.png"s);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700719 }
720
721 LOGV("Animation Description:\n");
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700722 LOGV(" animation: %d %d '%s' (%d)\n", batt_anim_.num_cycles, batt_anim_.first_frame_repeats,
723 batt_anim_.animation_file.c_str(), batt_anim_.num_frames);
724 LOGV(" fail_file: '%s'\n", batt_anim_.fail_file.c_str());
725 LOGV(" clock: %d %d %d %d %d %d '%s'\n", batt_anim_.text_clock.pos_x,
726 batt_anim_.text_clock.pos_y, batt_anim_.text_clock.color_r, batt_anim_.text_clock.color_g,
727 batt_anim_.text_clock.color_b, batt_anim_.text_clock.color_a,
728 batt_anim_.text_clock.font_file.c_str());
729 LOGV(" percent: %d %d %d %d %d %d '%s'\n", batt_anim_.text_percent.pos_x,
730 batt_anim_.text_percent.pos_y, batt_anim_.text_percent.color_r,
731 batt_anim_.text_percent.color_g, batt_anim_.text_percent.color_b,
732 batt_anim_.text_percent.color_a, batt_anim_.text_percent.font_file.c_str());
733 for (int i = 0; i < batt_anim_.num_frames; i++) {
734 LOGV(" frame %.2d: %d %d %d\n", i, batt_anim_.frames[i].disp_time,
735 batt_anim_.frames[i].min_level, batt_anim_.frames[i].max_level);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700736 }
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700737}
738
Yifan Hongb5d70332021-10-20 17:15:32 -0700739void Charger::OnInit(struct healthd_config* config) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700740 int ret;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700741 int i;
742 int epollfd;
743
744 dump_last_kmsg();
745
Todd Poynorebeb0c02014-09-23 14:54:24 -0700746 LOGW("--------------- STARTING CHARGER MODE ---------------\n");
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700747
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700748 ret = ev_init(
749 std::bind(&Charger::InputCallback, this, std::placeholders::_1, std::placeholders::_2));
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700750 if (!ret) {
751 epollfd = ev_get_epollfd();
Yifan Hongb5d70332021-10-20 17:15:32 -0700752 configuration_->ChargerRegisterEvent(epollfd, &charger_event_handler, EVENT_WAKEUP_FD);
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700753 }
754
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700755 InitAnimation();
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700756
Greg Kaiser8763fd22020-08-12 07:03:50 -0700757 ret = CreateDisplaySurface(batt_anim_.fail_file, &surf_unknown_);
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700758 if (ret < 0) {
Yifan Hongac748362021-10-26 17:20:25 -0700759#if !defined(__ANDROID_VNDK__)
Thierry Strudelac2aa7d2018-05-23 15:48:46 -0700760 LOGE("Cannot load custom battery_fail image. Reverting to built in: %d\n", ret);
Yifan Hongabda7152020-08-05 16:15:15 -0700761 ret = CreateDisplaySurface((system_animation_root + "charger/battery_fail.png"s).c_str(),
762 &surf_unknown_);
Yifan Hongac748362021-10-26 17:20:25 -0700763#endif
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700764 if (ret < 0) {
765 LOGE("Cannot load built in battery_fail image\n");
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700766 surf_unknown_ = NULL;
Damien Bargiacchi565ba022016-08-11 15:29:50 -0700767 }
768 }
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700769
Elliott Hughes9e85ea12015-04-15 10:25:55 -0700770 GRSurface** scale_frames;
Doug Zongkeree6ef152014-03-11 08:42:09 -0700771 int scale_count;
Tao Bao0db80232015-12-16 10:57:10 -0800772 int scale_fps; // Not in use (charger/battery_scale doesn't have FPS text
773 // chunk). We are using hard-coded frame.disp_time instead.
Greg Kaiser8763fd22020-08-12 07:03:50 -0700774 ret = CreateMultiDisplaySurface(batt_anim_.animation_file, &scale_count, &scale_fps,
Yifan Hongabda7152020-08-05 16:15:15 -0700775 &scale_frames);
Doug Zongkeree6ef152014-03-11 08:42:09 -0700776 if (ret < 0) {
777 LOGE("Cannot load battery_scale image\n");
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700778 batt_anim_.num_frames = 0;
779 batt_anim_.num_cycles = 1;
780 } else if (scale_count != batt_anim_.num_frames) {
Luke Song1d540dd2017-07-13 15:10:35 -0700781 LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n", scale_count,
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700782 batt_anim_.num_frames);
783 batt_anim_.num_frames = 0;
784 batt_anim_.num_cycles = 1;
Doug Zongkeree6ef152014-03-11 08:42:09 -0700785 } else {
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700786 for (i = 0; i < batt_anim_.num_frames; i++) {
787 batt_anim_.frames[i].surface = scale_frames[i];
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700788 }
789 }
Jack Wuc1b17112021-09-29 22:27:56 +0800790 drm_ = DRM_INNER;
Jack Wu3d637a92021-09-27 17:40:40 +0800791 screen_switch_ = SCREEN_SWITCH_DEFAULT;
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700792 ev_sync_key_state(std::bind(&Charger::SetKeyCallback, this, std::placeholders::_1,
793 std::placeholders::_2));
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700794
Jack Wu3d637a92021-09-27 17:40:40 +0800795 (void)ev_sync_sw_state(
796 std::bind(&Charger::SetSwCallback, this, std::placeholders::_1, std::placeholders::_2));
797
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700798 next_screen_transition_ = -1;
799 next_key_check_ = -1;
800 next_pwr_check_ = -1;
801 wait_batt_level_timestamp_ = 0;
Yifan Hong10c2b402017-11-08 10:57:52 -0800802
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700803 // Retrieve healthd_config from the existing health HAL.
Yifan Hongb5d70332021-10-20 17:15:32 -0700804 configuration_->ChargerInitConfig(config);
Yifan Hong10c2b402017-11-08 10:57:52 -0800805
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700806 boot_min_cap_ = config->boot_min_cap;
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700807}
Tao Bao5747e222018-09-11 10:46:35 -0700808
Yifan Hongabda7152020-08-05 16:15:15 -0700809int Charger::CreateDisplaySurface(const std::string& name, GRSurface** surface) {
810 return res_create_display_surface(name.c_str(), surface);
811}
812
813int Charger::CreateMultiDisplaySurface(const std::string& name, int* frames, int* fps,
814 GRSurface*** surface) {
815 return res_create_multi_display_surface(name.c_str(), frames, fps, surface);
816}
817
818void set_resource_root_for(const std::string& root, const std::string& backup_root,
819 std::string* value) {
820 if (value->empty()) {
821 return;
822 }
823
824 std::string new_value = root + *value + ".png";
825 // If |backup_root| is provided, additionally check whether the file under |root| is
826 // accessible or not. If not accessible, fallback to file under |backup_root|.
827 if (!backup_root.empty() && access(new_value.data(), F_OK) == -1) {
828 new_value = backup_root + *value + ".png";
829 }
830
831 *value = new_value;
832}
833
834void animation::set_resource_root(const std::string& root, const std::string& backup_root) {
835 CHECK(android::base::StartsWith(root, "/") && android::base::EndsWith(root, "/"))
836 << "animation root " << root << " must start and end with /";
837 CHECK(backup_root.empty() || (android::base::StartsWith(backup_root, "/") &&
838 android::base::EndsWith(backup_root, "/")))
839 << "animation backup root " << backup_root << " must start and end with /";
840 set_resource_root_for(root, backup_root, &animation_file);
841 set_resource_root_for(root, backup_root, &fail_file);
842 set_resource_root_for(root, backup_root, &text_clock.font_file);
843 set_resource_root_for(root, backup_root, &text_percent.font_file);
844}
845
Yifan Hong7dcf7b02019-10-08 17:27:11 -0700846} // namespace android