• 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 #pragma once
17 
18 #include <memory>
19 #include <mutex>
20 #include <vector>
21 
22 #include <android-base/unique_fd.h>
23 #include <healthd/healthd.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace health {
28 
29 class HealthLoop {
30   public:
31     HealthLoop();
32 
33     // Client is responsible for holding this forever. Process will exit
34     // when this is destroyed.
35     virtual ~HealthLoop();
36 
37     // Initialize and start the main loop. This function does not exit unless
38     // the process is interrupted.
39     // Once the loop is started, event handlers are no longer allowed to be
40     // registered.
41     int StartLoop();
42 
43   protected:
44     // healthd_mode_ops overrides. Note that healthd_mode_ops->battery_update
45     // is missing because it is only used by BatteryMonitor.
46     // Init is called right after epollfd_ is initialized (so RegisterEvent
47     // is allowed) but before other things are initialized (so SetChargerOnline
48     // is not allowed.)
49     // The implementation of Init() should pull configuration from the
50     // underlying health HAL (via getHealthConfig()), and store it into
51     // |config|. The implementation may not initialize:
52     // - screen_on, because charger calls getScreenOn() from the HAL directly
53     // - ignorePowerSupplyNames, because it isn't used by any clients of the
54     // health HAL.
55     virtual void Init(healthd_config* config) = 0;
56     virtual void Heartbeat() = 0;
57     virtual int PrepareToWait() = 0;
58 
59     // Note that this is NOT healthd_mode_ops->battery_update(BatteryProperties*),
60     // which is called by BatteryMonitor after values are fetched. This is the
61     // implementation of healthd_battery_update(), which calls
62     // the correct IHealth::update(),
63     // which calls BatteryMonitor::update(), which calls
64     // healthd_mode_ops->battery_update(BatteryProperties*).
65     virtual void ScheduleBatteryUpdate() = 0;
66 
67     // Register an epoll event. When there is an event, |func| will be
68     // called with |this| as the first argument and |epevents| as the second.
69     // This may be called in a different thread from where StartLoop is called
70     // (for obvious reasons; StartLoop never ends).
71     // Once the loop is started, event handlers are no longer allowed to be
72     // registered.
73     using BoundFunction = std::function<void(HealthLoop*, uint32_t /* epevents */)>;
74     int RegisterEvent(int fd, BoundFunction func, EventWakeup wakeup);
75 
76     // Helper for implementing ScheduleBatteryUpdate(). An implementation of
77     // ScheduleBatteryUpdate should get charger_online from BatteryMonitor::update(),
78     // then reset wake alarm interval by calling AdjustWakealarmPeriods.
79     void AdjustWakealarmPeriods(bool charger_online);
80 
81   private:
82     struct EventHandler {
83         HealthLoop* object = nullptr;
84         int fd;
85         BoundFunction func;
86     };
87 
88     int InitInternal();
89     void MainLoop();
90     void WakeAlarmInit();
91     void WakeAlarmEvent(uint32_t);
92     void UeventInit();
93     void UeventEvent(uint32_t);
94     void WakeAlarmSetInterval(int interval);
95     void PeriodicChores();
96 
97     // These are fixed after InitInternal() is called.
98     struct healthd_config healthd_config_;
99     android::base::unique_fd wakealarm_fd_;
100     android::base::unique_fd uevent_fd_;
101 
102     android::base::unique_fd epollfd_;
103     std::vector<std::unique_ptr<EventHandler>> event_handlers_;
104     int awake_poll_interval_;  // -1 for no epoll timeout
105     int wakealarm_wake_interval_;
106 
107     // If set to true, future RegisterEvent() will be rejected. This is to ensure all
108     // events are registered before StartLoop().
109     bool reject_event_register_ = false;
110 };
111 
112 }  // namespace health
113 }  // namespace hardware
114 }  // namespace android
115