• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011-2017 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 "healthd_mode_charger.h"
18 
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.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 <optional>
34 
35 #include <android-base/file.h>
36 #include <android-base/logging.h>
37 #include <android-base/macros.h>
38 #include <android-base/strings.h>
39 
40 #include <linux/netlink.h>
41 #include <sys/socket.h>
42 
43 #include <cutils/android_get_control_file.h>
44 #include <cutils/klog.h>
45 #include <cutils/misc.h>
46 #include <cutils/properties.h>
47 #include <cutils/uevent.h>
48 #include <sys/reboot.h>
49 
50 #include <suspend/autosuspend.h>
51 
52 #include "AnimationParser.h"
53 #include "charger.sysprop.h"
54 #include "charger_utils.h"
55 #include "healthd_draw.h"
56 
57 #include <android/hardware/health/2.0/IHealthInfoCallback.h>
58 #include <health/utils.h>
59 #include <health2impl/HalHealthLoop.h>
60 #include <health2impl/Health.h>
61 #include <healthd/healthd.h>
62 
63 using std::string_literals::operator""s;
64 using namespace android;
65 using android::hardware::Return;
66 using android::hardware::health::GetHealthServiceOrDefault;
67 using android::hardware::health::HealthLoop;
68 using android::hardware::health::V1_0::BatteryStatus;
69 using android::hardware::health::V2_0::Result;
70 using android::hardware::health::V2_1::IHealth;
71 using IHealth_2_0 = android::hardware::health::V2_0::IHealth;
72 using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
73 using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
74 
75 // main healthd loop
76 extern int healthd_main(void);
77 
78 // minui globals
79 char* locale;
80 
81 #ifndef max
82 #define max(a, b) ((a) > (b) ? (a) : (b))
83 #endif
84 
85 #ifndef min
86 #define min(a, b) ((a) < (b) ? (a) : (b))
87 #endif
88 
89 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
90 
91 #define MSEC_PER_SEC (1000LL)
92 #define NSEC_PER_MSEC (1000000LL)
93 
94 #define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
95 #define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
96 #define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
97 #define UNPLUGGED_DISPLAY_TIME (3 * MSEC_PER_SEC)
98 #define MAX_BATT_LEVEL_WAIT_TIME (3 * MSEC_PER_SEC)
99 #define UNPLUGGED_SHUTDOWN_TIME_PROP "ro.product.charger.unplugged_shutdown_time"
100 
101 #define LAST_KMSG_MAX_SZ (32 * 1024)
102 
103 #define LOGE(x...) KLOG_ERROR("charger", x);
104 #define LOGW(x...) KLOG_WARNING("charger", x);
105 #define LOGV(x...) KLOG_DEBUG("charger", x);
106 
107 namespace android {
108 
109 // Legacy animation resources are loaded from this directory.
110 static constexpr const char* legacy_animation_root = "/res/images/";
111 
112 // Built-in animation resources are loaded from this directory.
113 static 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.
116 // If the device is using the Generic System Image (GSI), resources may exist in
117 // both paths.
118 static constexpr const char* product_animation_desc_path =
119         "/product/etc/res/values/charger/animation.txt";
120 static constexpr const char* product_animation_root = "/product/etc/res/images/";
121 static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt";
122 
123 static const animation BASE_ANIMATION = {
124     .text_clock =
125         {
126             .pos_x = 0,
127             .pos_y = 0,
128 
129             .color_r = 255,
130             .color_g = 255,
131             .color_b = 255,
132             .color_a = 255,
133 
134             .font = nullptr,
135         },
136     .text_percent =
137         {
138             .pos_x = 0,
139             .pos_y = 0,
140 
141             .color_r = 255,
142             .color_g = 255,
143             .color_b = 255,
144             .color_a = 255,
145         },
146 
147     .run = false,
148 
149     .frames = nullptr,
150     .cur_frame = 0,
151     .num_frames = 0,
152     .first_frame_repeats = 2,
153 
154     .cur_cycle = 0,
155     .num_cycles = 3,
156 
157     .cur_level = 0,
158     .cur_status = BATTERY_STATUS_UNKNOWN,
159 };
160 
InitDefaultAnimationFrames()161 void Charger::InitDefaultAnimationFrames() {
162     owned_frames_ = {
163             {
164                     .disp_time = 750,
165                     .min_level = 0,
166                     .max_level = 19,
167                     .surface = NULL,
168             },
169             {
170                     .disp_time = 750,
171                     .min_level = 0,
172                     .max_level = 39,
173                     .surface = NULL,
174             },
175             {
176                     .disp_time = 750,
177                     .min_level = 0,
178                     .max_level = 59,
179                     .surface = NULL,
180             },
181             {
182                     .disp_time = 750,
183                     .min_level = 0,
184                     .max_level = 79,
185                     .surface = NULL,
186             },
187             {
188                     .disp_time = 750,
189                     .min_level = 80,
190                     .max_level = 95,
191                     .surface = NULL,
192             },
193             {
194                     .disp_time = 750,
195                     .min_level = 0,
196                     .max_level = 100,
197                     .surface = NULL,
198             },
199     };
200 }
201 
Charger(const sp<IHealth> & service)202 Charger::Charger(const sp<IHealth>& service)
203     : HalHealthLoop("charger", service), batt_anim_(BASE_ANIMATION) {}
204 
~Charger()205 Charger::~Charger() {}
206 
207 /* current time in milliseconds */
curr_time_ms()208 static int64_t curr_time_ms() {
209     timespec tm;
210     clock_gettime(CLOCK_MONOTONIC, &tm);
211     return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
212 }
213 
214 #define MAX_KLOG_WRITE_BUF_SZ 256
215 
dump_last_kmsg(void)216 static void dump_last_kmsg(void) {
217     std::string buf;
218     char* ptr;
219     size_t len;
220 
221     LOGW("\n");
222     LOGW("*************** LAST KMSG ***************\n");
223     LOGW("\n");
224     const char* kmsg[] = {
225         // clang-format off
226         "/sys/fs/pstore/console-ramoops-0",
227         "/sys/fs/pstore/console-ramoops",
228         "/proc/last_kmsg",
229         // clang-format on
230     };
231     for (size_t i = 0; i < arraysize(kmsg) && buf.empty(); ++i) {
232         auto fd = android_get_control_file(kmsg[i]);
233         if (fd >= 0) {
234             android::base::ReadFdToString(fd, &buf);
235         } else {
236             android::base::ReadFileToString(kmsg[i], &buf);
237         }
238     }
239 
240     if (buf.empty()) {
241         LOGW("last_kmsg not found. Cold reset?\n");
242         goto out;
243     }
244 
245     len = min(buf.size(), LAST_KMSG_MAX_SZ);
246     ptr = &buf[buf.size() - len];
247 
248     while (len > 0) {
249         size_t cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
250         char yoink;
251         char* nl;
252 
253         nl = (char*)memrchr(ptr, '\n', cnt - 1);
254         if (nl) cnt = nl - ptr + 1;
255 
256         yoink = ptr[cnt];
257         ptr[cnt] = '\0';
258         klog_write(6, "<4>%s", ptr);
259         ptr[cnt] = yoink;
260 
261         len -= cnt;
262         ptr += cnt;
263     }
264 
265 out:
266     LOGW("\n");
267     LOGW("************* END LAST KMSG *************\n");
268     LOGW("\n");
269 }
270 
request_suspend(bool enable)271 static int request_suspend(bool enable) {
272     if (!android::sysprop::ChargerProperties::enable_suspend().value_or(false)) {
273         return 0;
274     }
275 
276     if (enable)
277         return autosuspend_enable();
278     else
279         return autosuspend_disable();
280 }
281 
kick_animation(animation * anim)282 static void kick_animation(animation* anim) {
283     anim->run = true;
284 }
285 
reset_animation(animation * anim)286 static void reset_animation(animation* anim) {
287     anim->cur_cycle = 0;
288     anim->cur_frame = 0;
289     anim->run = false;
290 }
291 
UpdateScreenState(int64_t now)292 void Charger::UpdateScreenState(int64_t now) {
293     int disp_time;
294 
295     if (!batt_anim_.run || now < next_screen_transition_) return;
296 
297     // If battery level is not ready, keep checking in the defined time
298     if (health_info_.batteryLevel == 0 && health_info_.batteryStatus == BatteryStatus::UNKNOWN) {
299         if (wait_batt_level_timestamp_ == 0) {
300             // Set max delay time and skip drawing screen
301             wait_batt_level_timestamp_ = now + MAX_BATT_LEVEL_WAIT_TIME;
302             LOGV("[%" PRId64 "] wait for battery capacity ready\n", now);
303             return;
304         } else if (now <= wait_batt_level_timestamp_) {
305             // Do nothing, keep waiting
306             return;
307         }
308         // If timeout and battery level is still not ready, draw unknown battery
309     }
310 
311     if (healthd_draw_ == nullptr) {
312         std::optional<bool> out_screen_on;
313         service()->shouldKeepScreenOn([&](Result res, bool screen_on) {
314             if (res == Result::SUCCESS) {
315                 *out_screen_on = screen_on;
316             }
317         });
318         if (out_screen_on.has_value()) {
319             if (!*out_screen_on) {
320                 LOGV("[%" PRId64 "] leave screen off\n", now);
321                 batt_anim_.run = false;
322                 next_screen_transition_ = -1;
323                 if (charger_online()) request_suspend(true);
324                 return;
325             }
326         }
327 
328         healthd_draw_.reset(new HealthdDraw(&batt_anim_));
329 
330         if (android::sysprop::ChargerProperties::disable_init_blank().value_or(false)) {
331             healthd_draw_->blank_screen(true);
332             screen_blanked_ = true;
333         }
334     }
335 
336     /* animation is over, blank screen and leave */
337     if (batt_anim_.num_cycles > 0 && batt_anim_.cur_cycle == batt_anim_.num_cycles) {
338         reset_animation(&batt_anim_);
339         next_screen_transition_ = -1;
340         healthd_draw_->blank_screen(true);
341         screen_blanked_ = true;
342         LOGV("[%" PRId64 "] animation done\n", now);
343         if (charger_online()) request_suspend(true);
344         return;
345     }
346 
347     disp_time = batt_anim_.frames[batt_anim_.cur_frame].disp_time;
348 
349     if (screen_blanked_) {
350         healthd_draw_->blank_screen(false);
351         screen_blanked_ = false;
352     }
353 
354     /* animation starting, set up the animation */
355     if (batt_anim_.cur_frame == 0) {
356         LOGV("[%" PRId64 "] animation starting\n", now);
357         batt_anim_.cur_level = health_info_.batteryLevel;
358         batt_anim_.cur_status = (int)health_info_.batteryStatus;
359         if (health_info_.batteryLevel >= 0 && batt_anim_.num_frames != 0) {
360             /* find first frame given current battery level */
361             for (int i = 0; i < batt_anim_.num_frames; i++) {
362                 if (batt_anim_.cur_level >= batt_anim_.frames[i].min_level &&
363                     batt_anim_.cur_level <= batt_anim_.frames[i].max_level) {
364                     batt_anim_.cur_frame = i;
365                     break;
366                 }
367             }
368 
369             if (charger_online()) {
370                 // repeat the first frame first_frame_repeats times
371                 disp_time = batt_anim_.frames[batt_anim_.cur_frame].disp_time *
372                             batt_anim_.first_frame_repeats;
373             } else {
374                 disp_time = UNPLUGGED_DISPLAY_TIME / batt_anim_.num_cycles;
375             }
376 
377             LOGV("cur_frame=%d disp_time=%d\n", batt_anim_.cur_frame, disp_time);
378         }
379     }
380 
381     /* draw the new frame (@ cur_frame) */
382     healthd_draw_->redraw_screen(&batt_anim_, surf_unknown_);
383 
384     /* if we don't have anim frames, we only have one image, so just bump
385      * the cycle counter and exit
386      */
387     if (batt_anim_.num_frames == 0 || batt_anim_.cur_level < 0) {
388         LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now);
389         next_screen_transition_ = now + BATTERY_UNKNOWN_TIME;
390         batt_anim_.cur_cycle++;
391         return;
392     }
393 
394     /* schedule next screen transition */
395     next_screen_transition_ = curr_time_ms() + disp_time;
396 
397     /* advance frame cntr to the next valid frame only if we are charging
398      * if necessary, advance cycle cntr, and reset frame cntr
399      */
400     if (charger_online()) {
401         batt_anim_.cur_frame++;
402 
403         while (batt_anim_.cur_frame < batt_anim_.num_frames &&
404                (batt_anim_.cur_level < batt_anim_.frames[batt_anim_.cur_frame].min_level ||
405                 batt_anim_.cur_level > batt_anim_.frames[batt_anim_.cur_frame].max_level)) {
406             batt_anim_.cur_frame++;
407         }
408         if (batt_anim_.cur_frame >= batt_anim_.num_frames) {
409             batt_anim_.cur_cycle++;
410             batt_anim_.cur_frame = 0;
411 
412             /* don't reset the cycle counter, since we use that as a signal
413              * in a test above to check if animation is over
414              */
415         }
416     } else {
417         /* Stop animating if we're not charging.
418          * If we stop it immediately instead of going through this loop, then
419          * the animation would stop somewhere in the middle.
420          */
421         batt_anim_.cur_frame = 0;
422         batt_anim_.cur_cycle++;
423     }
424 }
425 
SetKeyCallback(int code,int value)426 int Charger::SetKeyCallback(int code, int value) {
427     int64_t now = curr_time_ms();
428     int down = !!value;
429 
430     if (code > KEY_MAX) return -1;
431 
432     /* ignore events that don't modify our state */
433     if (keys_[code].down == down) return 0;
434 
435     /* only record the down even timestamp, as the amount
436      * of time the key spent not being pressed is not useful */
437     if (down) keys_[code].timestamp = now;
438     keys_[code].down = down;
439     keys_[code].pending = true;
440     if (down) {
441         LOGV("[%" PRId64 "] key[%d] down\n", now, code);
442     } else {
443         int64_t duration = now - keys_[code].timestamp;
444         int64_t secs = duration / 1000;
445         int64_t msecs = duration - secs * 1000;
446         LOGV("[%" PRId64 "] key[%d] up (was down for %" PRId64 ".%" PRId64 "sec)\n", now, code,
447              secs, msecs);
448     }
449 
450     return 0;
451 }
452 
UpdateInputState(input_event * ev)453 void Charger::UpdateInputState(input_event* ev) {
454     if (ev->type != EV_KEY) return;
455     SetKeyCallback(ev->code, ev->value);
456 }
457 
SetNextKeyCheck(key_state * key,int64_t timeout)458 void Charger::SetNextKeyCheck(key_state* key, int64_t timeout) {
459     int64_t then = key->timestamp + timeout;
460 
461     if (next_key_check_ == -1 || then < next_key_check_) next_key_check_ = then;
462 }
463 
ProcessKey(int code,int64_t now)464 void Charger::ProcessKey(int code, int64_t now) {
465     key_state* key = &keys_[code];
466 
467     if (code == KEY_POWER) {
468         if (key->down) {
469             int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
470             if (now >= reboot_timeout) {
471                 /* We do not currently support booting from charger mode on
472                    all devices. Check the property and continue booting or reboot
473                    accordingly. */
474                 if (property_get_bool("ro.enable_boot_charger_mode", false)) {
475                     LOGW("[%" PRId64 "] booting from charger mode\n", now);
476                     property_set("sys.boot_from_charger_mode", "1");
477                 } else {
478                     if (batt_anim_.cur_level >= boot_min_cap_) {
479                         LOGW("[%" PRId64 "] rebooting\n", now);
480                         reboot(RB_AUTOBOOT);
481                     } else {
482                         LOGV("[%" PRId64
483                              "] ignore power-button press, battery level "
484                              "less than minimum\n",
485                              now);
486                     }
487                 }
488             } else {
489                 /* if the key is pressed but timeout hasn't expired,
490                  * make sure we wake up at the right-ish time to check
491                  */
492                 SetNextKeyCheck(key, POWER_ON_KEY_TIME);
493 
494                 /* Turn on the display and kick animation on power-key press
495                  * rather than on key release
496                  */
497                 kick_animation(&batt_anim_);
498                 request_suspend(false);
499             }
500         } else {
501             /* if the power key got released, force screen state cycle */
502             if (key->pending) {
503                 kick_animation(&batt_anim_);
504                 request_suspend(false);
505             }
506         }
507     }
508 
509     key->pending = false;
510 }
511 
HandleInputState(int64_t now)512 void Charger::HandleInputState(int64_t now) {
513     ProcessKey(KEY_POWER, now);
514 
515     if (next_key_check_ != -1 && now > next_key_check_) next_key_check_ = -1;
516 }
517 
HandlePowerSupplyState(int64_t now)518 void Charger::HandlePowerSupplyState(int64_t now) {
519     int timer_shutdown = UNPLUGGED_SHUTDOWN_TIME;
520     if (!have_battery_state_) return;
521 
522     if (!charger_online()) {
523         request_suspend(false);
524         if (next_pwr_check_ == -1) {
525             /* Last cycle would have stopped at the extreme top of battery-icon
526              * Need to show the correct level corresponding to capacity.
527              *
528              * Reset next_screen_transition_ to update screen immediately.
529              * Reset & kick animation to show complete animation cycles
530              * when charger disconnected.
531              */
532             timer_shutdown =
533                     property_get_int32(UNPLUGGED_SHUTDOWN_TIME_PROP, UNPLUGGED_SHUTDOWN_TIME);
534             next_screen_transition_ = now - 1;
535             reset_animation(&batt_anim_);
536             kick_animation(&batt_anim_);
537             next_pwr_check_ = now + timer_shutdown;
538             LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
539                  now, (int64_t)timer_shutdown, next_pwr_check_);
540         } else if (now >= next_pwr_check_) {
541             LOGW("[%" PRId64 "] shutting down\n", now);
542             reboot(RB_POWER_OFF);
543         } else {
544             /* otherwise we already have a shutdown timer scheduled */
545         }
546     } else {
547         /* online supply present, reset shutdown timer if set */
548         if (next_pwr_check_ != -1) {
549             /* Reset next_screen_transition_ to update screen immediately.
550              * Reset & kick animation to show complete animation cycles
551              * when charger connected again.
552              */
553             request_suspend(false);
554             next_screen_transition_ = now - 1;
555             reset_animation(&batt_anim_);
556             kick_animation(&batt_anim_);
557             LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
558         }
559         next_pwr_check_ = -1;
560     }
561 }
562 
Heartbeat()563 void Charger::Heartbeat() {
564     // charger* charger = &charger_state;
565     int64_t now = curr_time_ms();
566 
567     HandleInputState(now);
568     HandlePowerSupplyState(now);
569 
570     /* do screen update last in case any of the above want to start
571      * screen transitions (animations, etc)
572      */
573     UpdateScreenState(now);
574 }
575 
OnHealthInfoChanged(const HealthInfo_2_1 & health_info)576 void Charger::OnHealthInfoChanged(const HealthInfo_2_1& health_info) {
577     set_charger_online(health_info);
578 
579     if (!have_battery_state_) {
580         have_battery_state_ = true;
581         next_screen_transition_ = curr_time_ms() - 1;
582         request_suspend(false);
583         reset_animation(&batt_anim_);
584         kick_animation(&batt_anim_);
585     }
586     health_info_ = health_info.legacy.legacy;
587 
588     AdjustWakealarmPeriods(charger_online());
589 }
590 
PrepareToWait(void)591 int Charger::PrepareToWait(void) {
592     int64_t now = curr_time_ms();
593     int64_t next_event = INT64_MAX;
594     int64_t timeout;
595 
596     LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n",
597          now, next_screen_transition_, next_key_check_, next_pwr_check_);
598 
599     if (next_screen_transition_ != -1) next_event = next_screen_transition_;
600     if (next_key_check_ != -1 && next_key_check_ < next_event) next_event = next_key_check_;
601     if (next_pwr_check_ != -1 && next_pwr_check_ < next_event) next_event = next_pwr_check_;
602 
603     if (next_event != -1 && next_event != INT64_MAX)
604         timeout = max(0, next_event - now);
605     else
606         timeout = -1;
607 
608     return (int)timeout;
609 }
610 
InputCallback(int fd,unsigned int epevents)611 int Charger::InputCallback(int fd, unsigned int epevents) {
612     input_event ev;
613     int ret;
614 
615     ret = ev_get_input(fd, epevents, &ev);
616     if (ret) return -1;
617     UpdateInputState(&ev);
618     return 0;
619 }
620 
charger_event_handler(HealthLoop *,uint32_t)621 static void charger_event_handler(HealthLoop* /*charger_loop*/, uint32_t /*epevents*/) {
622     int ret;
623 
624     ret = ev_wait(-1);
625     if (!ret) ev_dispatch();
626 }
627 
InitAnimation()628 void Charger::InitAnimation() {
629     bool parse_success;
630 
631     std::string content;
632     if (base::ReadFileToString(product_animation_desc_path, &content)) {
633         parse_success = parse_animation_desc(content, &batt_anim_);
634         batt_anim_.set_resource_root(product_animation_root);
635     } else if (base::ReadFileToString(animation_desc_path, &content)) {
636         parse_success = parse_animation_desc(content, &batt_anim_);
637         // Fallback resources always exist in system_animation_root. On legacy devices with an old
638         // ramdisk image, resources may be overridden under root. For example,
639         // /res/images/charger/battery_fail.png may not be the same as
640         // system/core/healthd/images/battery_fail.png in the source tree, but is a device-specific
641         // image. Hence, load from /res, and fall back to /system/etc/res.
642         batt_anim_.set_resource_root(legacy_animation_root, system_animation_root);
643     } else {
644         LOGW("Could not open animation description at %s\n", animation_desc_path);
645         parse_success = false;
646     }
647 
648     if (!parse_success) {
649         LOGW("Could not parse animation description. Using default animation.\n");
650         batt_anim_ = BASE_ANIMATION;
651         batt_anim_.animation_file.assign(system_animation_root + "charger/battery_scale.png"s);
652         InitDefaultAnimationFrames();
653         batt_anim_.frames = owned_frames_.data();
654         batt_anim_.num_frames = owned_frames_.size();
655     }
656     if (batt_anim_.fail_file.empty()) {
657         batt_anim_.fail_file.assign(system_animation_root + "charger/battery_fail.png"s);
658     }
659 
660     LOGV("Animation Description:\n");
661     LOGV("  animation: %d %d '%s' (%d)\n", batt_anim_.num_cycles, batt_anim_.first_frame_repeats,
662          batt_anim_.animation_file.c_str(), batt_anim_.num_frames);
663     LOGV("  fail_file: '%s'\n", batt_anim_.fail_file.c_str());
664     LOGV("  clock: %d %d %d %d %d %d '%s'\n", batt_anim_.text_clock.pos_x,
665          batt_anim_.text_clock.pos_y, batt_anim_.text_clock.color_r, batt_anim_.text_clock.color_g,
666          batt_anim_.text_clock.color_b, batt_anim_.text_clock.color_a,
667          batt_anim_.text_clock.font_file.c_str());
668     LOGV("  percent: %d %d %d %d %d %d '%s'\n", batt_anim_.text_percent.pos_x,
669          batt_anim_.text_percent.pos_y, batt_anim_.text_percent.color_r,
670          batt_anim_.text_percent.color_g, batt_anim_.text_percent.color_b,
671          batt_anim_.text_percent.color_a, batt_anim_.text_percent.font_file.c_str());
672     for (int i = 0; i < batt_anim_.num_frames; i++) {
673         LOGV("  frame %.2d: %d %d %d\n", i, batt_anim_.frames[i].disp_time,
674              batt_anim_.frames[i].min_level, batt_anim_.frames[i].max_level);
675     }
676 }
677 
Init(struct healthd_config * config)678 void Charger::Init(struct healthd_config* config) {
679     int ret;
680     int i;
681     int epollfd;
682 
683     dump_last_kmsg();
684 
685     LOGW("--------------- STARTING CHARGER MODE ---------------\n");
686 
687     ret = ev_init(
688             std::bind(&Charger::InputCallback, this, std::placeholders::_1, std::placeholders::_2));
689     if (!ret) {
690         epollfd = ev_get_epollfd();
691         RegisterEvent(epollfd, &charger_event_handler, EVENT_WAKEUP_FD);
692     }
693 
694     InitAnimation();
695 
696     ret = CreateDisplaySurface(batt_anim_.fail_file, &surf_unknown_);
697     if (ret < 0) {
698         LOGE("Cannot load custom battery_fail image. Reverting to built in: %d\n", ret);
699         ret = CreateDisplaySurface((system_animation_root + "charger/battery_fail.png"s).c_str(),
700                                    &surf_unknown_);
701         if (ret < 0) {
702             LOGE("Cannot load built in battery_fail image\n");
703             surf_unknown_ = NULL;
704         }
705     }
706 
707     GRSurface** scale_frames;
708     int scale_count;
709     int scale_fps;  // Not in use (charger/battery_scale doesn't have FPS text
710                     // chunk). We are using hard-coded frame.disp_time instead.
711     ret = CreateMultiDisplaySurface(batt_anim_.animation_file, &scale_count, &scale_fps,
712                                     &scale_frames);
713     if (ret < 0) {
714         LOGE("Cannot load battery_scale image\n");
715         batt_anim_.num_frames = 0;
716         batt_anim_.num_cycles = 1;
717     } else if (scale_count != batt_anim_.num_frames) {
718         LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n", scale_count,
719              batt_anim_.num_frames);
720         batt_anim_.num_frames = 0;
721         batt_anim_.num_cycles = 1;
722     } else {
723         for (i = 0; i < batt_anim_.num_frames; i++) {
724             batt_anim_.frames[i].surface = scale_frames[i];
725         }
726     }
727     ev_sync_key_state(std::bind(&Charger::SetKeyCallback, this, std::placeholders::_1,
728                                 std::placeholders::_2));
729 
730     next_screen_transition_ = -1;
731     next_key_check_ = -1;
732     next_pwr_check_ = -1;
733     wait_batt_level_timestamp_ = 0;
734 
735     // Retrieve healthd_config from the existing health HAL.
736     HalHealthLoop::Init(config);
737 
738     boot_min_cap_ = config->boot_min_cap;
739 }
740 
CreateDisplaySurface(const std::string & name,GRSurface ** surface)741 int Charger::CreateDisplaySurface(const std::string& name, GRSurface** surface) {
742     return res_create_display_surface(name.c_str(), surface);
743 }
744 
CreateMultiDisplaySurface(const std::string & name,int * frames,int * fps,GRSurface *** surface)745 int Charger::CreateMultiDisplaySurface(const std::string& name, int* frames, int* fps,
746                                        GRSurface*** surface) {
747     return res_create_multi_display_surface(name.c_str(), frames, fps, surface);
748 }
749 
set_resource_root_for(const std::string & root,const std::string & backup_root,std::string * value)750 void set_resource_root_for(const std::string& root, const std::string& backup_root,
751                            std::string* value) {
752     if (value->empty()) {
753         return;
754     }
755 
756     std::string new_value = root + *value + ".png";
757     // If |backup_root| is provided, additionally check whether the file under |root| is
758     // accessible or not. If not accessible, fallback to file under |backup_root|.
759     if (!backup_root.empty() && access(new_value.data(), F_OK) == -1) {
760         new_value = backup_root + *value + ".png";
761     }
762 
763     *value = new_value;
764 }
765 
set_resource_root(const std::string & root,const std::string & backup_root)766 void animation::set_resource_root(const std::string& root, const std::string& backup_root) {
767     CHECK(android::base::StartsWith(root, "/") && android::base::EndsWith(root, "/"))
768             << "animation root " << root << " must start and end with /";
769     CHECK(backup_root.empty() || (android::base::StartsWith(backup_root, "/") &&
770                                   android::base::EndsWith(backup_root, "/")))
771             << "animation backup root " << backup_root << " must start and end with /";
772     set_resource_root_for(root, backup_root, &animation_file);
773     set_resource_root_for(root, backup_root, &fail_file);
774     set_resource_root_for(root, backup_root, &text_clock.font_file);
775     set_resource_root_for(root, backup_root, &text_percent.font_file);
776 }
777 
778 }  // namespace android
779 
healthd_charger_main(int argc,char ** argv)780 int healthd_charger_main(int argc, char** argv) {
781     int ch;
782 
783     while ((ch = getopt(argc, argv, "cr")) != -1) {
784         switch (ch) {
785             case 'c':
786                 // -c is now a noop
787                 break;
788             case 'r':
789                 // -r is now a noop
790                 break;
791             case '?':
792             default:
793                 LOGE("Unrecognized charger option: %c\n", optopt);
794                 exit(1);
795         }
796     }
797 
798     Charger charger(GetHealthServiceOrDefault());
799     return charger.StartLoop();
800 }
801