• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Allowed to be mocked for testing.
57     virtual int CreateDisplaySurface(const std::string& name, GRSurface** surface);
58     virtual int CreateMultiDisplaySurface(const std::string& name, int* frames, int* fps,
59                                           GRSurface*** surface);
60 
61   private:
62     void InitDefaultAnimationFrames();
63     void UpdateScreenState(int64_t now);
64     int SetKeyCallback(int code, int value);
65     void UpdateInputState(input_event* ev);
66     void SetNextKeyCheck(key_state* key, int64_t timeout);
67     void ProcessKey(int code, int64_t now);
68     void HandleInputState(int64_t now);
69     void HandlePowerSupplyState(int64_t now);
70     int InputCallback(int fd, unsigned int epevents);
71     void InitAnimation();
72 
73     bool have_battery_state_ = false;
74     bool screen_blanked_ = false;
75     int64_t next_screen_transition_ = 0;
76     int64_t next_key_check_ = 0;
77     int64_t next_pwr_check_ = 0;
78     int64_t wait_batt_level_timestamp_ = 0;
79 
80     key_state keys_[KEY_MAX + 1] = {};
81 
82     animation batt_anim_;
83     GRSurface* surf_unknown_ = nullptr;
84     int boot_min_cap_ = 0;
85 
86     HealthInfo_1_0 health_info_ = {};
87     std::unique_ptr<HealthdDraw> healthd_draw_;
88     std::vector<animation::frame> owned_frames_;
89 };
90 }  // namespace android
91 
92 int healthd_charger_main(int argc, char** argv);
93