1 /*
2  * Copyright (C) 2021 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 #ifndef HARDWARE_GOOGLE_PIXEL_USB_USBOVERHEATEVENT_H
18 #define HARDWARE_GOOGLE_PIXEL_USB_USBOVERHEATEVENT_H
19 
20 #include <android-base/file.h>
21 #include <android-base/properties.h>
22 #include <android-base/unique_fd.h>
23 #include <android/hardware/thermal/2.0/IThermal.h>
24 #include <android/hardware/thermal/2.0/IThermalChangedCallback.h>
25 #include <android/hardware/thermal/2.0/types.h>
26 #include <android/hidl/manager/1.0/IServiceManager.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <sys/epoll.h>
31 #include <sys/eventfd.h>
32 #include <sys/mount.h>
33 #include <sys/stat.h>
34 #include <sys/timerfd.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <utils/Log.h>
38 
39 #include <condition_variable>
40 #include <mutex>
41 #include <string>
42 #include <thread>
43 #include <vector>
44 
45 namespace android {
46 namespace hardware {
47 namespace google {
48 namespace pixel {
49 namespace usb {
50 
51 using ::android::base::unique_fd;
52 using ::android::base::WriteStringToFile;
53 
54 using ::android::hardware::thermal::V1_0::ThermalStatus;
55 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
56 using ::android::hardware::thermal::V2_0::IThermal;
57 using ::android::hardware::thermal::V2_0::IThermalChangedCallback;
58 using ::android::hardware::thermal::V2_0::Temperature;
59 using ::android::hardware::thermal::V2_0::TemperatureType;
60 using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
61 
62 using ::android::hidl::manager::V1_0::IServiceManager;
63 using ::android::hidl::manager::V1_0::IServiceNotification;
64 
65 using ::std::lock_guard;
66 using ::std::max;
67 using ::std::move;
68 using ::std::mutex;
69 using ::std::string;
70 using ::std::thread;
71 using ::std::unique_ptr;
72 using ::std::vector;
73 
74 class ZoneInfo {
75   public:
76     // Type of the thermal sensor
77     TemperatureType type_;
78     // Name of the thermal sensor
79     string name_;
80     // Throttling severity when monitor_ is started for polling temperature
81     ThrottlingSeverity severity_;
82     ZoneInfo(const TemperatureType &type, const string &name, const ThrottlingSeverity &severity);
83 };
84 
85 class UsbOverheatEvent : public IServiceNotification, public IThermalChangedCallback {
86   private:
87     // To wake up thread to record max temperature
88     unique_fd timer_fd_;
89     // Polls on timer_fd_ & event_fd. Thread waits here when port is cold.
90     unique_fd epoll_fd_;
91     // To wake up the thread when waiting on TimerFd
92     unique_fd event_fd_;
93     // Thermal zone for monitoring Throttling event
94     ZoneInfo monitored_zone_;
95     // Info of thermal zones that are queried during polling.
96     // ATM Suez UsbPortOverheatEvent can only report one of the values though.
97     // Therefore the first one in the list will be used for
98     // getCurrentTemperature and max_overheat_temp_.
99     vector<ZoneInfo> queried_zones_;
100     //  Sampling interval for monitoring the temperature
101     int monitor_interval_sec_;
102     // Thread object that executes the ep monitoring logic
103     unique_ptr<thread> monitor_;
104     // Maximum overheat temperature recorded
105     float max_overheat_temp_;
106     // Reference to thermal service
107     ::android::sp<IThermal> thermal_service_;
108     // Thread that polls temperature to record max temp
109     static void *monitorThread(void *param);
110     // Register service notification listener
111     bool registerListener();
112     // Helper function to wakeup monitor thread
113     void wakeupMonitor();
114     // Thermal ServiceNotification listener
115     Return<void> onRegistration(const hidl_string & /*fully_qualified_name*/,
116                                 const hidl_string & /*instance_name*/,
117                                 bool /*pre_existing*/) override;
118     // Thermal service callback
119     Return<void> notifyThrottling(const Temperature &temperature) override;
120 
121   public:
122     UsbOverheatEvent(const ZoneInfo &monitored_zone, const std::vector<ZoneInfo> &queried_zones,
123                      const int &monitor_interval_sec);
124     // Start monitoring thermal zone for maximum temperature
125     bool startRecording();
126     // Stop monitoring thermal zone
127     bool stopRecording();
128     // Enquire current USB temperature
129     bool getCurrentTemperature(const string &name, float *temp);
130     // Query Max overheat temperature
131     float getMaxOverheatTemperature();
132 };
133 
134 }  // namespace usb
135 }  // namespace pixel
136 }  // namespace google
137 }  // namespace hardware
138 }  // namespace android
139 #endif
140