• 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         callbacks_.erase(
245             std::remove_if(callbacks_.begin(), callbacks_.end(),
246                            [&](const CallbackSetting &c) {
247                                if (!c.is_filter_type || t.type == c.type) {
248                                    Return<void> ret = c.callback->notifyThrottling(t);
249                                    return !ret.isOk();
250                                }
251                                LOG(ERROR)
252                                    << "a Thermal callback is dead, removed from callback list.";
253                                return false;
254                            }),
255             callbacks_.end());
256     }
257 }
258 
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)259 Return<void> Thermal::debug(const hidl_handle &handle, const hidl_vec<hidl_string> &) {
260     if (handle != nullptr && handle->numFds >= 1) {
261         int fd = handle->data[0];
262         std::ostringstream dump_buf;
263 
264         if (!thermal_helper_.isInitializedOk()) {
265             dump_buf << "ThermalHAL not initialized properly." << std::endl;
266         } else {
267             {
268                 hidl_vec<Temperature_1_0> temperatures;
269                 dump_buf << "getTemperatures:" << std::endl;
270                 if (!thermal_helper_.fillTemperatures(&temperatures)) {
271                     dump_buf << "Failed to read thermal sensors." << std::endl;
272                 }
273 
274                 for (const auto &t : temperatures) {
275                     dump_buf << " Type: " << android::hardware::thermal::V1_0::toString(t.type)
276                              << " Name: " << t.name << " CurrentValue: " << t.currentValue
277                              << " ThrottlingThreshold: " << t.throttlingThreshold
278                              << " ShutdownThreshold: " << t.shutdownThreshold
279                              << " VrThrottlingThreshold: " << t.vrThrottlingThreshold << std::endl;
280                 }
281             }
282             {
283                 hidl_vec<CpuUsage> cpu_usages;
284                 dump_buf << "getCpuUsages:" << std::endl;
285                 if (!thermal_helper_.fillCpuUsages(&cpu_usages)) {
286                     dump_buf << "Failed to get CPU usages." << std::endl;
287                 }
288 
289                 for (const auto &usage : cpu_usages) {
290                     dump_buf << " Name: " << usage.name << " Active: " << usage.active
291                              << " Total: " << usage.total << " IsOnline: " << usage.isOnline
292                              << std::endl;
293                 }
294             }
295             {
296                 dump_buf << "getCurrentTemperatures:" << std::endl;
297                 hidl_vec<Temperature_2_0> temperatures;
298                 if (!thermal_helper_.fillCurrentTemperatures(false, TemperatureType_2_0::SKIN,
299                                                              &temperatures)) {
300                     dump_buf << "Failed to getCurrentTemperatures." << std::endl;
301                 }
302 
303                 for (const auto &t : temperatures) {
304                     dump_buf << " Type: " << android::hardware::thermal::V2_0::toString(t.type)
305                              << " Name: " << t.name << " CurrentValue: " << t.value
306                              << " ThrottlingStatus: "
307                              << android::hardware::thermal::V2_0::toString(t.throttlingStatus)
308                              << std::endl;
309                 }
310             }
311             {
312                 dump_buf << "getTemperatureThresholds:" << std::endl;
313                 hidl_vec<TemperatureThreshold> temperatures;
314                 if (!thermal_helper_.fillTemperatureThresholds(false, TemperatureType_2_0::SKIN,
315                                                                &temperatures)) {
316                     dump_buf << "Failed to getTemperatureThresholds." << std::endl;
317                 }
318 
319                 for (const auto &t : temperatures) {
320                     dump_buf << " Type: " << android::hardware::thermal::V2_0::toString(t.type)
321                              << " Name: " << t.name;
322                     dump_buf << " hotThrottlingThreshold: [";
323                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
324                         dump_buf << t.hotThrottlingThresholds[i] << " ";
325                     }
326                     dump_buf << "] coldThrottlingThreshold: [";
327                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
328                         dump_buf << t.coldThrottlingThresholds[i] << " ";
329                     }
330                     dump_buf << "] vrThrottlingThreshold: " << t.vrThrottlingThreshold;
331                     dump_buf << std::endl;
332                 }
333             }
334             {
335                 dump_buf << "getCurrentCoolingDevices:" << std::endl;
336                 hidl_vec<CoolingDevice_2_0> cooling_devices;
337                 if (!thermal_helper_.fillCurrentCoolingDevices(false, CoolingType::CPU,
338                                                                &cooling_devices)) {
339                     dump_buf << "Failed to getCurrentCoolingDevices." << std::endl;
340                 }
341 
342                 for (const auto &c : cooling_devices) {
343                     dump_buf << " Type: " << android::hardware::thermal::V2_0::toString(c.type)
344                              << " Name: " << c.name << " CurrentValue: " << c.value << std::endl;
345                 }
346             }
347             {
348                 dump_buf << "Callbacks: Total " << callbacks_.size() << std::endl;
349                 for (const auto &c : callbacks_) {
350                     dump_buf << " IsFilter: " << c.is_filter_type
351                              << " Type: " << android::hardware::thermal::V2_0::toString(c.type)
352                              << std::endl;
353                 }
354             }
355             {
356                 dump_buf << "getHysteresis:" << std::endl;
357                 const auto &map = thermal_helper_.GetSensorInfoMap();
358                 for (const auto &name_info_pair : map) {
359                     dump_buf << " Name: " << name_info_pair.first;
360                     dump_buf << " hotHysteresis: [";
361                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
362                         dump_buf << name_info_pair.second.hot_hysteresis[i] << " ";
363                     }
364                     dump_buf << "] coldHysteresis: [";
365                     for (size_t i = 0; i < kThrottlingSeverityCount; ++i) {
366                         dump_buf << name_info_pair.second.cold_hysteresis[i] << " ";
367                     }
368                     dump_buf << "]" << std::endl;
369                 }
370             }
371             {
372                 dump_buf << "Monitor:" << std::endl;
373                 const auto &map = thermal_helper_.GetSensorInfoMap();
374                 for (const auto &name_info_pair : map) {
375                     dump_buf << " Name: " << name_info_pair.first;
376                     dump_buf << " Monitor: " << std::boolalpha << name_info_pair.second.is_monitor
377                              << std::noboolalpha << std::endl;
378                 }
379             }
380         }
381         std::string buf = dump_buf.str();
382         if (!android::base::WriteStringToFd(buf, fd)) {
383             PLOG(ERROR) << "Failed to dump state to fd";
384         }
385         fsync(fd);
386     }
387     return Void();
388 }
389 
390 }  // namespace implementation
391 }  // namespace V2_0
392 }  // namespace thermal
393 }  // namespace hardware
394 }  // namespace android
395