• 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 #include "ThermalStateListener.h"
18 
19 #include <android-base/logging.h>
20 #include <android/binder_manager.h>
21 
22 namespace aidl {
23 namespace google {
24 namespace hardware {
25 namespace power {
26 namespace impl {
27 namespace pixel {
28 
connectThermalHal()29 bool ThermalStateListener::connectThermalHal() {
30     const std::string thermalServiceName = std::string(IThermal::descriptor) + "/default";
31 
32     if (!AServiceManager_isDeclared(thermalServiceName.c_str())) {
33         LOG(ERROR) << "Thermal HAL service not declared";
34         return false;
35     }
36 
37     ndk::SpAIBinder thermalHalBinder =
38             ndk::SpAIBinder(AServiceManager_waitForService(thermalServiceName.c_str()));
39 
40     if (thermalHalBinder.get() == nullptr) {
41         LOG(ERROR) << "Cannot get Thermal Hal binder!";
42         return false;
43     }
44 
45     mThermalAIDL = IThermal::fromBinder(thermalHalBinder);
46     if (mThermalAIDL == nullptr) {
47         LOG(ERROR) << "Cannot get Thermal Hal AIDL!";
48         return false;
49     }
50 
51     LOG(INFO) << "Connected to Thermalhal.";
52     return true;
53 }
54 
thermalCallback(const Temperature & temp)55 void ThermalStateListener::thermalCallback(const Temperature &temp) {
56     if (temp.type == TemperatureType::SKIN) {
57         LOG(INFO) << "New skin throttling state: "
58                   << ::android::internal::ToString(temp.throttlingStatus);
59         mThermalThrotSev = temp.throttlingStatus;
60     }
61 }
62 
registerCallback()63 bool ThermalStateListener::registerCallback() {
64     if (mThermalAIDL == nullptr) {
65         LOG(ERROR) << "Thermal Hal AIDL not connected!";
66         return false;
67     }
68 
69     if (mThermalCallback == nullptr) {
70         mThermalCallback = ndk::SharedRefBase::make<ThermalCallback>(
71                 [this](const Temperature &temperature) { thermalCallback(temperature); });
72     }
73 
74     auto ret = mThermalAIDL->registerThermalChangedCallback(mThermalCallback);
75     if (!ret.isOk()) {
76         LOG(ERROR) << "Failed to register the Powerhal's thermal callback: " << ret.getMessage();
77         return false;
78     }
79     // TODO(guibing): handling the death connection
80     LOG(INFO) << "Registered the thermal callback.";
81     return true;
82 }
83 
init()84 bool ThermalStateListener::init() {
85     if (connectThermalHal() && registerCallback()) {
86         return true;
87     }
88     LOG(ERROR) << "Failed to initialize the thermal state listener!";
89     return false;
90 }
91 
getThermalThrotSev()92 ThrottlingSeverity ThermalStateListener::getThermalThrotSev() {
93     return mThermalThrotSev.load();
94 }
95 
96 }  // namespace pixel
97 }  // namespace impl
98 }  // namespace power
99 }  // namespace hardware
100 }  // namespace google
101 }  // namespace aidl
102