-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththermal.cpp
More file actions
291 lines (234 loc) · 9.25 KB
/
thermal.cpp
File metadata and controls
291 lines (234 loc) · 9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Changes from Qualcomm Innovation Center are provided under the following license:
Copyright (c) 2023, 2025 Qualcomm Innovation Center, Inc. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause-Clear */
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <cerrno>
#include <mutex>
#include <string>
#include <android-base/file.h>
#include <android-base/logging.h>
#include "thermal.h"
namespace aidl::android::hardware::thermal{
using ndk::ScopedAStatus;
namespace {
bool interfacesEqual(const std::shared_ptr<::ndk::ICInterface>& left,
const std::shared_ptr<::ndk::ICInterface>& right) {
if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) {
return left == right;
}
return left->asBinder() == right->asBinder();
}
}// namespace
static const Temperature dummy_temp_1_0 = {
.type = TemperatureType::SKIN,
.name = "test sensor",
.value = 30,
.throttlingStatus = ThrottlingSeverity::NONE,
};
Thermal::Thermal():
utils(std::bind(&Thermal::sendThrottlingChangeCB, this,
std::placeholders::_1))
{ }
ScopedAStatus Thermal::getCoolingDevices(std::vector<CoolingDevice>* out_data) {
std::vector<CoolingDevice> cdev;
if (!utils.isCdevInitialized())
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
"ThermalHAL not initialized properly.");
else {
if (utils.readCdevStates(cdev) <= 0)
LOG(VERBOSE) << __func__ << "Failed to read thermal cooling devices.";
}
if (out_data != nullptr)
*out_data = std::move(cdev);
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::getCoolingDevicesWithType(CoolingType in_type,
std::vector<CoolingDevice>* out_data) {
std::vector<CoolingDevice> cdev;
if (!utils.isCdevInitialized())
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
"ThermalHAL not initialized properly.");
else {
if (utils.readCdevStates(in_type, cdev) <= 0)
LOG(VERBOSE) << __func__ << "Failed to read thermal cooling devices.";
}
if (out_data != nullptr)
*out_data = std::move(cdev);
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::getTemperatures(std::vector<Temperature>* out_temp) {
LOG(VERBOSE) << __func__;
std::vector<Temperature> temperatures;
if (!utils.isSensorInitialized()) {
std::vector<Temperature> _temp = {dummy_temp_1_0};
LOG(VERBOSE) << __func__ << " Returning Dummy Value";
if (out_temp != nullptr)
*out_temp = std::move(_temp);
return ScopedAStatus::ok();
}
if (utils.readTemperatures(temperatures) <= 0)
LOG(VERBOSE) << __func__ << "Sensor Temperature read failure.";
if (out_temp != nullptr)
*out_temp = std::move(temperatures);
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::getTemperaturesWithType(TemperatureType in_type,
std::vector<Temperature>* out_temp) {
std::vector<Temperature> temperatures;
if (!utils.isSensorInitialized(in_type))
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
"ThermalHAL given sensor type not initialized.");
else {
if (utils.readTemperatures(in_type, temperatures) <= 0)
LOG(VERBOSE) << __func__ << "Sensor Temperature read failure.";
}
if (out_temp != nullptr)
*out_temp = std::move(temperatures);
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::getTemperatureThresholds(std::vector<TemperatureThreshold>* out_temp_thresh) {
std::vector<TemperatureThreshold> thresh;
if (!utils.isSensorInitialized())
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
"ThermalHAL for sensor not initialized.");
if (utils.readTemperatureThreshold(thresh) <= 0)
LOG(VERBOSE) << __func__ << "Sensor Threshold read failure or type not supported.";
if (out_temp_thresh != nullptr)
*out_temp_thresh = std::move(thresh);
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::getTemperatureThresholdsWithType(
TemperatureType in_type,
std::vector<TemperatureThreshold>* out_temp_thresh) {
std::vector<TemperatureThreshold> thresh;
if (!utils.isSensorInitialized(in_type))
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
"ThermalHAL given sensor type not initialized.");
else{
if (utils.readTemperatureThreshold(in_type, thresh) <= 0)
LOG(VERBOSE) << __func__ << "Sensor Threshold read failure or type not supported.";
}
if (out_temp_thresh != nullptr)
*out_temp_thresh = std::move(thresh);
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::registerThermalChangedCallback(
const std::shared_ptr<IThermalChangedCallback>& in_callback) {
if (in_callback == nullptr) {
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"Invalid nullptr callback");
}
{
std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
TemperatureType in_type = TemperatureType::UNKNOWN;
for (CallbackSetting _cb: cb) {
if (interfacesEqual(_cb.callback, in_callback))
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"Callback already registered");
}
cb.emplace_back(in_callback, in_type);
LOG(DEBUG) << "A callback has been registered to ThermalHAL ";
}
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::registerThermalChangedCallbackWithType(
const std::shared_ptr<IThermalChangedCallback>& in_callback, TemperatureType in_type) {
LOG(VERBOSE) << __func__ << " IThermalChangedCallback: " << in_callback
<< ", TemperatureType: " << static_cast<int32_t>(in_type);
if (in_callback == nullptr) {
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"Invalid nullptr callback");
}
if (in_type == TemperatureType::BCL_VOLTAGE ||
in_type == TemperatureType::BCL_CURRENT)
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"BCL current and voltage notification not supported");
{
std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
for (CallbackSetting _cb: cb) {
if (interfacesEqual(_cb.callback, in_callback))
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"Callback already registered");
}
cb.emplace_back(in_callback, in_type);
LOG(DEBUG) << "A callback has been registered to ThermalHAL Type: " << android::hardware::thermal::toString(in_type);
}
return ScopedAStatus::ok();
}
ScopedAStatus Thermal::unregisterThermalChangedCallback(
const std::shared_ptr<IThermalChangedCallback>& in_callback) {
if (in_callback == nullptr) {
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"Invalid nullptr callback");
}
std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
std::vector<CallbackSetting>::iterator it;
bool removed = false;
for (it = cb.begin(); it != cb.end(); it++) {
if (interfacesEqual(it->callback, in_callback)) {
cb.erase(it);
LOG(DEBUG) << "callback unregistered. isFilter: ";
removed = true;
break;
}
}
if (!removed) {
return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
"Callback wasn't registered");
}
LOG(DEBUG) << "A callback has been registered to ThermalHAL" ;
return ScopedAStatus::ok();
}
void Thermal::sendThrottlingChangeCB(const Temperature &t)
{
std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
std::vector<CallbackSetting>::iterator it;
LOG(DEBUG) << "Throttle Severity change: " << " Type: " << (int)t.type
<< " Name: " << t.name << " Value: " << t.value <<
" ThrottlingStatus: " << (int)t.throttlingStatus;
it = cb.begin();
while (it != cb.end()) {
if (it->type == t.type || it->type == TemperatureType::UNKNOWN) {
::ndk::ScopedAStatus ret = it->callback->notifyThrottling(t);
if (!ret.isOk()) {
LOG(ERROR) << "Notify callback execution error. Removing"<<ret.getMessage();
it = cb.erase(it);
continue;
}
}
it++;
}
}
} // namespace aidl::android::hardware::thermal