1 /*
2  * Copyright (C) 2019 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 #pragma once
18 
19 #include <linux/input.h>
20 
21 #include <memory>
22 #include <vector>
23 
24 #include <android/hardware/health/2.0/IHealthInfoCallback.h>
25 #include <android/hardware/health/2.1/IHealth.h>
26 #include <health2impl/HalHealthLoop.h>
27 
28 #include "animation.h"
29 
30 class GRSurface;
31 class HealthdDraw;
32 
33 namespace android {
34 struct key_state {
35     bool pending;
36     bool down;
37     int64_t timestamp;
38 };
39 
40 class Charger : public ::android::hardware::health::V2_1::implementation::HalHealthLoop {
41   public:
42     using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
43     using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
44 
45     Charger(const sp<android::hardware::health::V2_1::IHealth>& service);
46     ~Charger();
47 
48   protected:
49     // HealthLoop overrides.
50     void Heartbeat() override;
51     int PrepareToWait() override;
52     void Init(struct healthd_config* config) override;
53     // HalHealthLoop overrides
54     void OnHealthInfoChanged(const HealthInfo_2_1& health_info) override;
55 
56   private:
57     void InitDefaultAnimationFrames();
58     void UpdateScreenState(int64_t now);
59     int SetKeyCallback(int code, int value);
60     void UpdateInputState(input_event* ev);
61     void SetNextKeyCheck(key_state* key, int64_t timeout);
62     void ProcessKey(int code, int64_t now);
63     void HandleInputState(int64_t now);
64     void HandlePowerSupplyState(int64_t now);
65     int InputCallback(int fd, unsigned int epevents);
66     void InitAnimation();
67 
68     bool have_battery_state_ = false;
69     bool screen_blanked_ = false;
70     int64_t next_screen_transition_ = 0;
71     int64_t next_key_check_ = 0;
72     int64_t next_pwr_check_ = 0;
73     int64_t wait_batt_level_timestamp_ = 0;
74 
75     key_state keys_[KEY_MAX + 1] = {};
76 
77     animation batt_anim_;
78     GRSurface* surf_unknown_ = nullptr;
79     int boot_min_cap_ = 0;
80 
81     HealthInfo_1_0 health_info_ = {};
82     std::unique_ptr<HealthdDraw> healthd_draw_;
83     std::vector<animation::frame> owned_frames_;
84 };
85 }  // namespace android
86 
87 int healthd_charger_main(int argc, char** argv);
88