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