• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 <cerrno>
18 #include <mutex>
19 #include <string>
20 
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <hidl/HidlTransportSupport.h>
24 
25 #include "Thermal.h"
26 #include "thermal-helper.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace thermal {
31 namespace V2_0 {
32 namespace implementation {
33 
34 namespace {
35 
36 using ::android::hardware::interfacesEqual;
37 using ::android::hardware::thermal::V1_0::ThermalStatus;
38 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
39 using ::android::hidl::base::V1_0::IBase;
40 
41 template <typename T, typename U>
setFailureAndCallback(T _hidl_cb,hidl_vec<U> data,std::string_view debug_msg)42 Return<void> setFailureAndCallback(T _hidl_cb, hidl_vec<U> data, std::string_view debug_msg) {
43     ThermalStatus status;
44     status.code = ThermalStatusCode::FAILURE;
45     status.debugMessage = debug_msg.data();
46     _hidl_cb(status, data);
47     return Void();
48 }
49 
50 template <typename T, typename U>
setInitFailureAndCallback(T _hidl_cb,hidl_vec<U> data)51 Return<void> setInitFailureAndCallback(T _hidl_cb, hidl_vec<U> data) {
52     return setFailureAndCallback(_hidl_cb, data, "Failure initializing thermal HAL");
53 }
54 
55 }  // namespace
56 
57 // On init we will spawn a thread which will continually watch for
58 // throttling.  When throttling is seen, if we have a callback registered
59 // the thread will call notifyThrottling() else it will log the dropped
60 // throttling event and do nothing.  The thread is only killed when
61 // Thermal() is killed.
Thermal()62 Thermal::Thermal()
63     : thermal_helper_(
64           std::bind(&Thermal::sendThermalChangedCallback, this, std::placeholders::_1)) {}
65 
66 // Methods from ::android::hardware::thermal::V1_0::IThermal.
getTemperatures(getTemperatures_cb _hidl_cb)67 Return<void> Thermal::getTemperatures(getTemperatures_cb _hidl_cb) {
68     ThermalStatus status;
69     status.code = ThermalStatusCode::SUCCESS;
70     hidl_vec<Temperature_1_0> temperatures;
71 
72     if (!thermal_helper_.isInitializedOk()) {
73         LOG(ERROR) << "ThermalHAL not initialized properly.";
74         return setInitFailureAndCallback(_hidl_cb, temperatures);
75     }
76 
77     if (!thermal_helper_.fillTemperatures(&temperatures)) {
78         return setFailureAndCallback(_hidl_cb, temperatures, "Failed to read thermal sensors.");
79     }
80 
81     _hidl_cb(status, temperatures);
82     return Void();
83 }
84 
getCpuUsages(getCpuUsages_cb _hidl_cb)85 Return<void> Thermal::getCpuUsages(getCpuUsages_cb _hidl_cb) {
86     ThermalStatus status;
87     status.code = ThermalStatusCode::SUCCESS;
88     hidl_vec<CpuUsage> cpu_usages;
89 
90     if (!thermal_helper_.isInitializedOk()) {
91         return setInitFailureAndCallback(_hidl_cb, cpu_usages);
92     }
93 
94     if (!thermal_helper_.fillCpuUsages(&cpu_usages)) {
95         return setFailureAndCallback(_hidl_cb, cpu_usages, "Failed to get CPU usages.");
96     }
97 
98     _hidl_cb(status, cpu_usages);
99     return Void();
100 }
101 
getCoolingDevices(getCoolingDevices_cb _hidl_cb)102 Return<void> Thermal::getCoolingDevices(getCoolingDevices_cb _hidl_cb) {
103     ThermalStatus status;
104     status.code = ThermalStatusCode::SUCCESS;
105     hidl_vec<CoolingDevice_1_0> cooling_devices;
106 
107     if (!thermal_helper_.isInitializedOk()) {
108         return setInitFailureAndCallback(_hidl_cb, cooling_devices);
109     }
110     _hidl_cb(status, cooling_devices);
111     return Void();
112 }
113 
getCurrentTemperatures(bool filterType,TemperatureType_2_0 type,getCurrentTemperatures_cb _hidl_cb)114 Return<void> Thermal::getCurrentTemperatures(bool filterType, TemperatureType_2_0 type,
115                                              getCurrentTemperatures_cb _hidl_cb) {
116     ThermalStatus status;
117     status.code = ThermalStatusCode::SUCCESS;
118     hidl_vec<Temperature_2_0> temperatures;
119 
120     if (!thermal_helper_.isInitializedOk()) {
121         LOG(ERROR) << "ThermalHAL not initialized properly.";
122         return setInitFailureAndCallback(_hidl_cb, temperatures);
123     }
124 
125     if (!thermal_helper_.fillCurrentTemperatures(filterType, type, &temperatures)) {
126         return setFailureAndCallback(_hidl_cb, temperatures, "Failed to read thermal sensors.");
127     }
128 
129     _hidl_cb(status, temperatures);
130     return Void();
131 }
132 
getTemperatureThresholds(bool filterType,TemperatureType_2_0 type,getTemperatureThresholds_cb _hidl_cb)133 Return<void> Thermal::getTemperatureThresholds(bool filterType, TemperatureType_2_0 type,
134                                                getTemperatureThresholds_cb _hidl_cb) {
135     ThermalStatus status;
136     status.code = ThermalStatusCode::SUCCESS;
137     hidl_vec<TemperatureThreshold> temperatures;
138 
139     if (!thermal_helper_.isInitializedOk()) {
140         LOG(ERROR) << "ThermalHAL not initialized properly.";
141         return setInitFailureAndCallback(_hidl_cb, temperatures);
142     }
143 
144     if (!thermal_helper_.fillTemperatureThresholds(filterType, type, &temperatures)) {
145         return setFailureAndCallback(_hidl_cb, temperatures, "Failed to read thermal sensors.");
146     }
147 
148     _hidl_cb(status, temperatures);
149     return Void();
150 }
151 
getCurrentCoolingDevices(bool filterType,CoolingType type,getCurrentCoolingDevices_cb _hidl_cb)152 Return<void> Thermal::getCurrentCoolingDevices(bool filterType, CoolingType type,
153                                                getCurrentCoolingDevices_cb _hidl_cb) {
154     ThermalStatus status;
155     status.code = ThermalStatusCode::SUCCESS;
156     hidl_vec<CoolingDevice_2_0> cooling_devices;
157 
158     if (!thermal_helper_.isInitializedOk()) {
159         LOG(ERROR) << "ThermalHAL not initialized properly.";
160         return setInitFailureAndCallback(_hidl_cb, cooling_devices);
161     }
162 
163     if (!thermal_helper_.fillCurrentCoolingDevices(filterType, type, &cooling_devices)) {
164         return setFailureAndCallback(_hidl_cb, cooling_devices, "Failed to read thermal sensors.");
165     }
166 
167     _hidl_cb(status, cooling_devices);
168     return Void();
169 }
170 
registerThermalChangedCallback(const sp<IThermalChangedCallback> & callback,bool filterType,TemperatureType_2_0 type,registerThermalChangedCallback_cb _hidl_cb)171 Return<void> Thermal::registerThermalChangedCallback(const sp<IThermalChangedCallback> &callback,
172                                                      bool filterType, TemperatureType_2_0 type,
173                                                      registerThermalChangedCallback_cb _hidl_cb) {
174     ThermalStatus status;
175     if (callback == nullptr) {
176         status.code = ThermalStatusCode::FAILURE;
177         status.debugMessage = "Invalid nullptr callback";
178         LOG(ERROR) << status.debugMessage;
179         _hidl_cb(status);
180         return Void();
181     } else {
182         status.code = ThermalStatusCode::SUCCESS;
183     }
184     std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
185     if (std::any_of(callbacks_.begin(), callbacks_.end(), [&](const CallbackSetting &c) {
186             return interfacesEqual(c.callback, callback);
187         })) {
188         status.code = ThermalStatusCode::FAILURE;
189         status.debugMessage = "Same callback registered already";
190         LOG(ERROR) << status.debugMessage;
191     } else {
192         callbacks_.emplace_back(callback, filterType, type);
193         LOG(INFO) << "a callback has been registered to ThermalHAL, isFilter: " << filterType
194                   << " Type: " << android::hardware::thermal::V2_0::toString(type);
195     }
196     _hidl_cb(status);
197     return Void();
198 }
199 
unregisterThermalChangedCallback(const sp<IThermalChangedCallback> & callback,unregisterThermalChangedCallback_cb _hidl_cb)200 Return<void> Thermal::unregisterThermalChangedCallback(
201     const sp<IThermalChangedCallback> &callback, unregisterThermalChangedCallback_cb _hidl_cb) {
202     ThermalStatus status;
203     if (callback == nullptr) {
204         status.code = ThermalStatusCode::FAILURE;
205         status.debugMessage = "Invalid nullptr callback";
206         LOG(ERROR) << status.debugMessage;
207         _hidl_cb(status);
208         return Void();
209     } else {
210         status.code = ThermalStatusCode::SUCCESS;
211     }
212     bool removed = false;
213     std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
214     callbacks_.erase(
215         std::remove_if(callbacks_.begin(), callbacks_.end(),
216                        [&](const CallbackSetting &c) {
217                            if (interfacesEqual(c.callback, callback)) {
218                                LOG(INFO)
219                                    << "a callback has been unregistered to ThermalHAL, isFilter: "
220                                    << c.is_filter_type << " Type: "
221                                    << android::hardware::thermal::V2_0::toString(c.type);
222                                removed = true;
223                                return true;
224                            }
225                            return false;
226                        }),
227         callbacks_.end());
228     if (!removed) {
229         status.code = ThermalStatusCode::FAILURE;
230         status.debugMessage = "The callback was not registered before";
231         LOG(ERROR) << status.debugMessage;
232     }
233     _hidl_cb(status);
234     return Void();
235 }
236 
sendThermalChangedCallback(const std::vector<Temperature_2_0> & temps)237 void Thermal::sendThermalChangedCallback(const std::vector<Temperature_2_0> &temps) {
238     std::lock_guard<std::mutex> _lock(thermal_callback_mutex_);
239     for (auto &t : temps) {
240         LOG(INFO) << "Sending notification: "
241                   << " Type: " << android::hardware::thermal::V2_0::toString(t.type)
242                   << " Name: " << t.name << " CurrentValue: " << t.value << " ThrottlingStatus: "
243                   << android::hardware::thermal::V2_0::toString(t.throttlingStatus);
244 
245         thermal_helper_.sendPowerExtHint(t);
246         callbacks_.erase(
247             std::remove_if(callbacks_.begin(), callbacks_.end(),
248                            [&](const CallbackSetting &c) {
249                                if (!c.is_filter_type || t.type == c.type) {
250                                    Return<void> ret = c.callback->notifyThrottling(t);
251                                    return !ret.isOk();
252                                }
253                                LOG(ERROR)
254                                    << "a Thermal callback is dead, removed from callback list.";
255                                return false;
256                            }),
257             callbacks_.end());
258     }
259 }
260 
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)261 Return<void> Thermal::debug(const hidl_handle &handle, const hidl_vec<hidl_string> &) {
262     if (handle != nullptr && handle->numFds >= 1) {
263         int fd = handle->data[0];
264         std::ostringstream dump_buf;
265 
266         if (!thermal_helper_.isInitializedOk()) {
267             dump_buf << "ThermalHAL not initialized properly." << std::endl;
268         } else {
269             {
270                 hidl_vec<Temperature_1_0> temperatures;
271                 dump_buf << "getTemperatures:" << std::endl;
272                 if (!thermal_helper_.fillTemperatures(&temperatures)) {
273                     dump_buf << "Failed to read thermal sensors." << std::endl;
274                 }
275 
276                 for (const auto &t : temperatures) {
277                     dump_buf << " Type: " << android::hardware::thermal::V1_0::toString(t.type)
278                              << " Name: " << t.name << " CurrentValue: " << t.currentValue
279                              << " ThrottlingThreshold: " << t.throttlingThreshold
280                              << " ShutdownThreshold: " << t.shutdownThreshold
281                              << " VrThrottlingThreshold: " << t.vrThrottlingThreshold << std::endl;
282                 }
283             }
284             {
285                 hidl_vec<CpuUsage> cpu_usages;
286                 dump_buf << "getCpuUsages:" << std::endl;
287                 if (!thermal_helper_.fillCpuUsages(&cpu_usages)) {
288                     dump_buf << "Failed to get CPU usages." << std::endl;
289                 }
290 
291                 for (const auto &usage : cpu_usages) {
292                     dump_buf << " Name: " << usage.name << " Active: " << usage.active
293                              << " Total: " << usage.total << " IsOnline: " << usage.isOnline
294                              << std::endl;
295                 }
296             }
297             {
298                 dump_buf << "getCurrentTemperatures:" << std::endl;
299                 hidl_vec<Temperature_2_0> temperatures;
300                 if (!thermal_helper_.fillCurrentTemperatures(false, TemperatureType_2_0::SKIN,
301                                                              &temperatures)) {
302                     dump_buf << "Failed to getCurrentTemperatures." << std::endl;
303                 }
304 
305                 for (const auto &t : temperatures) {
306                     dump_buf << " Type: " << android::hardware::thermal::V2_0::toString(t.type)
307                              << " Name: " << t.name << " CurrentValue: " << t.value
308                              << " ThrottlingStatus: "
309                              << android::hardware::thermal::V2_0::toString(t.throttlingStatus)
310                              << std::endl;
311                 }
312             }
313             {
314                 dump_buf << "getTemperatureThresholds:" << std::endl;
315                 hidl_vec<TemperatureThreshold> temperatures;
316                 if (!thermal_helper_.fillTemperatureThresholds(false, TemperatureType_2_0::SKIN,
317                                                                &temperatures)) {
318                     dump_buf << "Failed to getTemperatureThresholds." << std::endl;
319                 }
320 
321                 for (const auto &t : temperatures) {
322                     dump_buf << " Type: " << android::hardware::thermal::V2_0::toString(t.type)
323                              << " Name: " << t.name;
324                     dump_buf << " hotThrottlingThreshold: [";
325                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
326                         dump_buf << t.hotThrottlingThresholds[i] << " ";
327                     }
328                     dump_buf << "] coldThrottlingThreshold: [";
329                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
330                         dump_buf << t.coldThrottlingThresholds[i] << " ";
331                     }
332                     dump_buf << "] vrThrottlingThreshold: " << t.vrThrottlingThreshold;
333                     dump_buf << std::endl;
334                 }
335             }
336             {
337                 dump_buf << "getCurrentCoolingDevices:" << std::endl;
338                 hidl_vec<CoolingDevice_2_0> cooling_devices;
339                 if (!thermal_helper_.fillCurrentCoolingDevices(false, CoolingType::CPU,
340                                                                &cooling_devices)) {
341                     dump_buf << "Failed to getCurrentCoolingDevices." << std::endl;
342                 }
343 
344                 for (const auto &c : cooling_devices) {
345                     dump_buf << " Type: " << android::hardware::thermal::V2_0::toString(c.type)
346                              << " Name: " << c.name << " CurrentValue: " << c.value << std::endl;
347                 }
348             }
349             {
350                 dump_buf << "Callbacks: Total " << callbacks_.size() << std::endl;
351                 for (const auto &c : callbacks_) {
352                     dump_buf << " IsFilter: " << c.is_filter_type
353                              << " Type: " << android::hardware::thermal::V2_0::toString(c.type)
354                              << std::endl;
355                 }
356             }
357             {
358                 dump_buf << "getHysteresis:" << std::endl;
359                 const auto &map = thermal_helper_.GetSensorInfoMap();
360                 for (const auto &name_info_pair : map) {
361                     dump_buf << " Name: " << name_info_pair.first;
362                     dump_buf << " hotHysteresis: [";
363                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
364                         dump_buf << name_info_pair.second.hot_hysteresis[i] << " ";
365                     }
366                     dump_buf << "] coldHysteresis: [";
367                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
368                         dump_buf << name_info_pair.second.cold_hysteresis[i] << " ";
369                     }
370                     dump_buf << "]" << std::endl;
371                 }
372             }
373             {
374                 dump_buf << "Monitor:" << std::endl;
375                 const auto &map = thermal_helper_.GetSensorInfoMap();
376                 for (const auto &name_info_pair : map) {
377                     dump_buf << " Name: " << name_info_pair.first;
378                     dump_buf << " Monitor: " << std::boolalpha << name_info_pair.second.is_monitor
379                              << std::noboolalpha << std::endl;
380                 }
381             }
382             {
383                 dump_buf << "SendPowerHint:" << std::endl;
384                 const auto &map = thermal_helper_.GetSensorInfoMap();
385                 for (const auto &name_info_pair : map) {
386                     dump_buf << " Name: " << name_info_pair.first;
387                     dump_buf << " SendPowerHint: " << std::boolalpha
388                              << name_info_pair.second.send_powerhint << std::noboolalpha
389                              << std::endl;
390                 }
391             }
392             {
393                 dump_buf << "AIDL Power Hal exist: " << std::boolalpha
394                          << thermal_helper_.isAidlPowerHalExist() << std::endl;
395                 dump_buf << "AIDL Power Hal connected: " << std::boolalpha
396                          << thermal_helper_.isPowerHalConnected() << std::endl;
397                 dump_buf << "AIDL Power Hal Ext connected: " << std::boolalpha
398                          << thermal_helper_.isPowerHalExtConnected() << std::endl;
399             }
400         }
401         std::string buf = dump_buf.str();
402         if (!android::base::WriteStringToFd(buf, fd)) {
403             PLOG(ERROR) << "Failed to dump state to fd";
404         }
405         fsync(fd);
406     }
407     return Void();
408 }
409 
410 }  // namespace implementation
411 }  // namespace V2_0
412 }  // namespace thermal
413 }  // namespace hardware
414 }  // namespace android
415