1 /* 2 * Copyright (C) 2022 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 #ifndef MITIGATION_THERMAL_MANAGER_H_ 20 #define MITIGATION_THERMAL_MANAGER_H_ 21 22 #include <aidl/android/hardware/thermal/BnThermalChangedCallback.h> 23 #include <aidl/android/hardware/thermal/IThermal.h> 24 #include <android-base/chrono_utils.h> 25 #include <android-base/file.h> 26 #include <android-base/logging.h> 27 #include <android-base/parseint.h> 28 #include <android-base/properties.h> 29 #include <android-base/strings.h> 30 #include <android/binder_auto_utils.h> 31 #include <unistd.h> 32 #include <utils/Mutex.h> 33 34 #include <fstream> 35 #include <iostream> 36 #include <regex> 37 38 #include "MitigationConfig.h" 39 40 namespace android { 41 namespace hardware { 42 namespace google { 43 namespace pixel { 44 45 using ::aidl::android::hardware::thermal::BnThermalChangedCallback; 46 using ::aidl::android::hardware::thermal::IThermal; 47 using ::aidl::android::hardware::thermal::Temperature; 48 using ::aidl::android::hardware::thermal::TemperatureType; 49 using ::aidl::android::hardware::thermal::ThrottlingSeverity; 50 using android::hardware::google::pixel::MitigationConfig; 51 52 class MitigationThermalManager { 53 public: 54 static MitigationThermalManager &getInstance(); 55 56 // delete copy and move constructors and assign operators 57 MitigationThermalManager(MitigationThermalManager const &) = delete; 58 MitigationThermalManager(MitigationThermalManager &&) = delete; 59 MitigationThermalManager &operator=(MitigationThermalManager const &) = delete; 60 MitigationThermalManager &operator=(MitigationThermalManager &&) = delete; 61 62 private: 63 // ThermalCallback implements the HIDL thermal changed callback 64 // interface, IThermalChangedCallback. 65 void thermalCb(const Temperature &temperature); 66 android::base::boot_clock::time_point lastCapturedTime; 67 68 class ThermalCallback : public BnThermalChangedCallback { 69 public: ThermalCallback(std::function<void (const Temperature &)> notify_function)70 ThermalCallback(std::function<void(const Temperature &)> notify_function) 71 : notifyFunction(notify_function) {} 72 73 // Callback function. thermal service will call this. notifyThrottling(const Temperature & temperature)74 ndk::ScopedAStatus notifyThrottling(const Temperature &temperature) override { 75 if ((temperature.type == TemperatureType::BCL_VOLTAGE) || 76 (temperature.type == TemperatureType::BCL_CURRENT)) { 77 notifyFunction(temperature); 78 } 79 return ndk::ScopedAStatus::ok(); 80 } 81 82 private: 83 std::function<void(const Temperature &)> notifyFunction; 84 }; 85 onThermalAidlBinderDied(void *)86 static void onThermalAidlBinderDied(void *) { 87 LOG(ERROR) << "Thermal AIDL service died, trying to reconnect"; 88 MitigationThermalManager::getInstance().connectThermalHal(); 89 } 90 91 public: 92 MitigationThermalManager(); 93 ~MitigationThermalManager(); 94 bool connectThermalHal(); 95 bool isMitigationTemperature(const Temperature &temperature); 96 bool registerCallback(); 97 void remove(); 98 void updateConfig(const struct MitigationConfig::Config &cfg); 99 100 101 private: 102 std::mutex thermal_callback_mutex_; 103 std::mutex thermal_hal_mutex_; 104 // Thermal hal interface. 105 std::shared_ptr<IThermal> thermal; 106 // Thermal hal callback object. 107 std::shared_ptr<ThermalCallback> callback; 108 // Receiver when AIDL thermal hal restart. 109 ndk::ScopedAIBinder_DeathRecipient aidlDeathRecipient; 110 std::vector<std::string> kSystemPath; 111 std::vector<std::string> kFilteredZones; 112 std::vector<std::string> kSystemName; 113 std::string kLogFilePath; 114 std::string kTimestampFormat; 115 }; 116 117 } // namespace pixel 118 } // namespace google 119 } // namespace hardware 120 } // namespace android 121 #endif 122