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 <optional>
23 #include <vector>
24 
25 #include <aidl/android/hardware/health/BatteryStatus.h>
26 #include <health/HealthLoop.h>
27 #include <healthd/healthd.h>
28 
29 #include "animation.h"
30 
31 class GRSurface;
32 class HealthdDraw;
33 
34 namespace android {
35 struct key_state {
36     bool pending;
37     bool down;
38     int64_t timestamp;
39 };
40 
41 // Health info that interests charger
42 struct ChargerHealthInfo {
43     int32_t battery_level;
44     aidl::android::hardware::health::BatteryStatus battery_status;
45 };
46 
47 enum DirectRenderManager {
48     DRM_INNER,
49     DRM_OUTER,
50 };
51 
52 enum SrceenSwitch {
53     SCREEN_SWITCH_DEFAULT,
54     SCREEN_SWITCH_DISABLE,
55     SCREEN_SWITCH_ENABLE,
56 };
57 
58 // Configuration interface for charger. This includes:
59 // - HalHealthLoop APIs that interests charger.
60 // - configuration values that used to be provided by sysprops
61 class ChargerConfigurationInterface {
62   public:
63     virtual ~ChargerConfigurationInterface() = default;
64     // HalHealthLoop related APIs
65     virtual std::optional<bool> ChargerShouldKeepScreenOn() = 0;
66     virtual bool ChargerIsOnline() = 0;
67     virtual void ChargerInitConfig(healthd_config* config) = 0;
68     using BoundFunction =
69             std::function<void(android::hardware::health::HealthLoop*, uint32_t /* epevents */)>;
70     virtual int ChargerRegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) = 0;
71 
72     // Other configuration values
73     virtual bool ChargerEnableSuspend() = 0;
74 };
75 
76 // charger UI
77 class Charger {
78   public:
79     explicit Charger(ChargerConfigurationInterface* configuration);
80     virtual ~Charger();
81 
82     // Hooks for ChargerConfigurationInterface
83     void OnHeartbeat();
84     int OnPrepareToWait();
85     // |cookie| is passed to ChargerConfigurationInterface::ChargerInitConfig
86     void OnInit(struct healthd_config* config);
87     void OnHealthInfoChanged(const ChargerHealthInfo& health_info);
88 
89   protected:
90     // Allowed to be mocked for testing.
91     virtual int CreateDisplaySurface(const std::string& name, GRSurface** surface);
92     virtual int CreateMultiDisplaySurface(const std::string& name, int* frames, int* fps,
93                                           GRSurface*** surface);
94 
95   private:
96     void InitDefaultAnimationFrames();
97     void UpdateScreenState(int64_t now);
98     int SetKeyCallback(int code, int value);
99     int SetSwCallback(int code, int value);
100     void UpdateInputState(input_event* ev);
101     void SetNextKeyCheck(key_state* key, int64_t timeout);
102     void ProcessKey(int code, int64_t now);
103     void ProcessHallSensor(int code);
104     void HandleInputState(int64_t now);
105     void HandlePowerSupplyState(int64_t now);
106     int InputCallback(int fd, unsigned int epevents);
107     void InitHealthdDraw();
108     void InitAnimation();
109     int RequestEnableSuspend();
110     int RequestDisableSuspend();
111     void BlankSecScreen();
112 
113     bool have_battery_state_ = false;
114     bool screen_blanked_ = false;
115     bool init_screen_ = false;
116     int64_t next_screen_transition_ = 0;
117     int64_t next_key_check_ = 0;
118     int64_t next_pwr_check_ = 0;
119     int64_t wait_batt_level_timestamp_ = 0;
120 
121     DirectRenderManager drm_;
122     SrceenSwitch screen_switch_;
123 
124     key_state keys_[KEY_MAX + 1] = {};
125 
126     animation batt_anim_;
127     GRSurface* surf_unknown_ = nullptr;
128     int boot_min_cap_ = 0;
129 
130     ChargerHealthInfo health_info_ = {};
131     std::unique_ptr<HealthdDraw> healthd_draw_;
132     std::vector<animation::frame> owned_frames_;
133 
134     ChargerConfigurationInterface* configuration_;
135 };
136 }  // namespace android
137