• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2025 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 #include <aidl/android/hardware/thermal/BnThermalChangedCallback.h>
19 #include <aidl/android/hardware/thermal/IThermal.h>
20 
21 namespace aidl {
22 namespace google {
23 namespace hardware {
24 namespace power {
25 namespace impl {
26 namespace pixel {
27 
28 using ::aidl::android::hardware::thermal::BnThermalChangedCallback;
29 using ::aidl::android::hardware::thermal::IThermal;
30 using ::aidl::android::hardware::thermal::Temperature;
31 using ::aidl::android::hardware::thermal::TemperatureType;
32 using ::aidl::android::hardware::thermal::ThrottlingSeverity;
33 
34 /**
35  * Listen to the device thermal throttling status, so it could be used to
36  * aggregate the performance metrics with the thermal states.
37  */
38 class ThermalStateListener {
39   public:
40     ~ThermalStateListener() = default;
41     ThermalStateListener(ThermalStateListener const &) = delete;
42     ThermalStateListener(ThermalStateListener &&) = delete;
43     ThermalStateListener &operator=(ThermalStateListener const &) = delete;
44     ThermalStateListener &operator=(ThermalStateListener &&) = delete;
45 
46     bool init();
47     ThrottlingSeverity getThermalThrotSev();
48 
49     // Singleton
getInstance()50     static ThermalStateListener *getInstance() {
51         static ThermalStateListener instance{};
52         return &instance;
53     }
54 
55   private:
56     ThermalStateListener() = default;
57     bool connectThermalHal();
58     bool registerCallback();
59     void thermalCallback(const Temperature &temp);
60 
61     class ThermalCallback : public BnThermalChangedCallback {
62       public:
ThermalCallback(std::function<void (const Temperature &)> notify_function)63         ThermalCallback(std::function<void(const Temperature &)> notify_function)
64             : mNotifyFunction(notify_function) {}
65 
notifyThrottling(const Temperature & temperature)66         ndk::ScopedAStatus notifyThrottling(const Temperature &temperature) override {
67             mNotifyFunction(temperature);
68             return ndk::ScopedAStatus::ok();
69         }
70 
71       private:
72         std::function<void(const Temperature &)> mNotifyFunction;
73     };
74 
75     std::shared_ptr<IThermal> mThermalAIDL;
76     std::shared_ptr<ThermalCallback> mThermalCallback;
77     std::atomic<ThrottlingSeverity> mThermalThrotSev{ThrottlingSeverity::NONE};
78 };
79 
80 }  // namespace pixel
81 }  // namespace impl
82 }  // namespace power
83 }  // namespace hardware
84 }  // namespace google
85 }  // namespace aidl
86