1*4d7e907cSAndroid Build Coastguard Worker /*
2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker *
4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker *
8*4d7e907cSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker *
10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker */
16*4d7e907cSAndroid Build Coastguard Worker
17*4d7e907cSAndroid Build Coastguard Worker #include "include/thermalutils/ThermalHidlWrapper.h"
18*4d7e907cSAndroid Build Coastguard Worker
19*4d7e907cSAndroid Build Coastguard Worker #include <hidl/HidlTransportSupport.h>
20*4d7e907cSAndroid Build Coastguard Worker
21*4d7e907cSAndroid Build Coastguard Worker #include <cmath>
22*4d7e907cSAndroid Build Coastguard Worker
23*4d7e907cSAndroid Build Coastguard Worker namespace aidl {
24*4d7e907cSAndroid Build Coastguard Worker namespace android {
25*4d7e907cSAndroid Build Coastguard Worker namespace hardware {
26*4d7e907cSAndroid Build Coastguard Worker namespace thermal {
27*4d7e907cSAndroid Build Coastguard Worker
28*4d7e907cSAndroid Build Coastguard Worker using ::android::hardware::Void;
29*4d7e907cSAndroid Build Coastguard Worker
30*4d7e907cSAndroid Build Coastguard Worker namespace {
31*4d7e907cSAndroid Build Coastguard Worker
32*4d7e907cSAndroid Build Coastguard Worker template <typename T, typename U>
setFailureAndCallback(T _hidl_cb,hidl_vec<U> data,std::string_view debug_msg)33*4d7e907cSAndroid Build Coastguard Worker Return<void> setFailureAndCallback(T _hidl_cb, hidl_vec<U> data, std::string_view debug_msg) {
34*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
35*4d7e907cSAndroid Build Coastguard Worker status.code = ThermalStatusCode::FAILURE;
36*4d7e907cSAndroid Build Coastguard Worker status.debugMessage = debug_msg.data();
37*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status, data);
38*4d7e907cSAndroid Build Coastguard Worker return Void();
39*4d7e907cSAndroid Build Coastguard Worker }
40*4d7e907cSAndroid Build Coastguard Worker
41*4d7e907cSAndroid Build Coastguard Worker template <typename T>
setFailureAndCallback(T _hidl_cb,std::string_view debug_msg)42*4d7e907cSAndroid Build Coastguard Worker Return<void> setFailureAndCallback(T _hidl_cb, std::string_view debug_msg) {
43*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
44*4d7e907cSAndroid Build Coastguard Worker status.code = ThermalStatusCode::FAILURE;
45*4d7e907cSAndroid Build Coastguard Worker status.debugMessage = debug_msg.data();
46*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status);
47*4d7e907cSAndroid Build Coastguard Worker return Void();
48*4d7e907cSAndroid Build Coastguard Worker }
49*4d7e907cSAndroid Build Coastguard Worker
50*4d7e907cSAndroid Build Coastguard Worker template <typename T, typename U>
setInitFailureAndCallback(T _hidl_cb,hidl_vec<U> data)51*4d7e907cSAndroid Build Coastguard Worker Return<void> setInitFailureAndCallback(T _hidl_cb, hidl_vec<U> data) {
52*4d7e907cSAndroid Build Coastguard Worker return setFailureAndCallback(
53*4d7e907cSAndroid Build Coastguard Worker _hidl_cb, data, "Thermal AIDL HAL client used by HIDL wrapper was not initialized");
54*4d7e907cSAndroid Build Coastguard Worker }
55*4d7e907cSAndroid Build Coastguard Worker
56*4d7e907cSAndroid Build Coastguard Worker template <typename T>
setInitFailureAndCallback(T _hidl_cb)57*4d7e907cSAndroid Build Coastguard Worker Return<void> setInitFailureAndCallback(T _hidl_cb) {
58*4d7e907cSAndroid Build Coastguard Worker return setFailureAndCallback(
59*4d7e907cSAndroid Build Coastguard Worker _hidl_cb, "Thermal AIDL HAL client used by HIDL wrapper was not initialized");
60*4d7e907cSAndroid Build Coastguard Worker }
61*4d7e907cSAndroid Build Coastguard Worker
62*4d7e907cSAndroid Build Coastguard Worker template <typename T, typename U>
setUnsupportedFailureAndCallback(T _hidl_cb,hidl_vec<U> data)63*4d7e907cSAndroid Build Coastguard Worker Return<void> setUnsupportedFailureAndCallback(T _hidl_cb, hidl_vec<U> data) {
64*4d7e907cSAndroid Build Coastguard Worker return setFailureAndCallback(_hidl_cb, data, "Operation unsupported by Thermal HIDL wrapper");
65*4d7e907cSAndroid Build Coastguard Worker }
66*4d7e907cSAndroid Build Coastguard Worker
convertAidlTemperatureType(const TemperatureType & type)67*4d7e907cSAndroid Build Coastguard Worker TemperatureType_2_0 convertAidlTemperatureType(const TemperatureType& type) {
68*4d7e907cSAndroid Build Coastguard Worker if (type < TemperatureType::CPU || type > TemperatureType::NPU) {
69*4d7e907cSAndroid Build Coastguard Worker return TemperatureType_2_0::UNKNOWN;
70*4d7e907cSAndroid Build Coastguard Worker }
71*4d7e907cSAndroid Build Coastguard Worker return static_cast<TemperatureType_2_0>(type);
72*4d7e907cSAndroid Build Coastguard Worker }
73*4d7e907cSAndroid Build Coastguard Worker
convertAidlCoolingType(const CoolingType & type)74*4d7e907cSAndroid Build Coastguard Worker CoolingType_2_0 convertAidlCoolingType(const CoolingType& type) {
75*4d7e907cSAndroid Build Coastguard Worker if (type < CoolingType::FAN || type > CoolingType::COMPONENT) {
76*4d7e907cSAndroid Build Coastguard Worker return CoolingType_2_0::COMPONENT;
77*4d7e907cSAndroid Build Coastguard Worker }
78*4d7e907cSAndroid Build Coastguard Worker return static_cast<CoolingType_2_0>(type);
79*4d7e907cSAndroid Build Coastguard Worker }
80*4d7e907cSAndroid Build Coastguard Worker
convertAidlTemperature(const Temperature & temperature)81*4d7e907cSAndroid Build Coastguard Worker Temperature_2_0 convertAidlTemperature(const Temperature& temperature) {
82*4d7e907cSAndroid Build Coastguard Worker Temperature_2_0 t = Temperature_2_0{
83*4d7e907cSAndroid Build Coastguard Worker convertAidlTemperatureType(temperature.type), temperature.name, temperature.value,
84*4d7e907cSAndroid Build Coastguard Worker static_cast<ThrottlingSeverity_2_0>(temperature.throttlingStatus)};
85*4d7e907cSAndroid Build Coastguard Worker return t;
86*4d7e907cSAndroid Build Coastguard Worker }
87*4d7e907cSAndroid Build Coastguard Worker
convertAidlCoolingDevice(const CoolingDevice & cooling_device)88*4d7e907cSAndroid Build Coastguard Worker CoolingDevice_2_0 convertAidlCoolingDevice(const CoolingDevice& cooling_device) {
89*4d7e907cSAndroid Build Coastguard Worker CoolingDevice_2_0 t =
90*4d7e907cSAndroid Build Coastguard Worker CoolingDevice_2_0{convertAidlCoolingType(cooling_device.type), cooling_device.name,
91*4d7e907cSAndroid Build Coastguard Worker static_cast<uint64_t>(cooling_device.value)};
92*4d7e907cSAndroid Build Coastguard Worker return t;
93*4d7e907cSAndroid Build Coastguard Worker }
convertAidlTemperatureThreshold(const TemperatureThreshold & threshold)94*4d7e907cSAndroid Build Coastguard Worker TemperatureThreshold_2_0 convertAidlTemperatureThreshold(const TemperatureThreshold& threshold) {
95*4d7e907cSAndroid Build Coastguard Worker TemperatureThreshold_2_0 t =
96*4d7e907cSAndroid Build Coastguard Worker TemperatureThreshold_2_0{convertAidlTemperatureType(threshold.type), threshold.name,
97*4d7e907cSAndroid Build Coastguard Worker threshold.hotThrottlingThresholds.data(),
98*4d7e907cSAndroid Build Coastguard Worker threshold.coldThrottlingThresholds.data(), NAN};
99*4d7e907cSAndroid Build Coastguard Worker return t;
100*4d7e907cSAndroid Build Coastguard Worker }
101*4d7e907cSAndroid Build Coastguard Worker
102*4d7e907cSAndroid Build Coastguard Worker } // namespace
103*4d7e907cSAndroid Build Coastguard Worker
104*4d7e907cSAndroid Build Coastguard Worker // Methods from ::android::hardware::thermal::V1_0::IThermal follow.
getTemperatures(getTemperatures_cb _hidl_cb)105*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::getTemperatures(getTemperatures_cb _hidl_cb) {
106*4d7e907cSAndroid Build Coastguard Worker hidl_vec<Temperature_1_0> ret_1_0;
107*4d7e907cSAndroid Build Coastguard Worker setUnsupportedFailureAndCallback(_hidl_cb, ret_1_0);
108*4d7e907cSAndroid Build Coastguard Worker return Void();
109*4d7e907cSAndroid Build Coastguard Worker }
110*4d7e907cSAndroid Build Coastguard Worker
getCpuUsages(std::function<void (const ThermalStatus &,const hidl_vec<CpuUsage> &)> _hidl_cb)111*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::getCpuUsages(
112*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&, const hidl_vec<CpuUsage>&)> _hidl_cb) {
113*4d7e907cSAndroid Build Coastguard Worker hidl_vec<CpuUsage> ret_1_0;
114*4d7e907cSAndroid Build Coastguard Worker setUnsupportedFailureAndCallback(_hidl_cb, ret_1_0);
115*4d7e907cSAndroid Build Coastguard Worker return Void();
116*4d7e907cSAndroid Build Coastguard Worker }
117*4d7e907cSAndroid Build Coastguard Worker
getCoolingDevices(std::function<void (const ThermalStatus &,const hidl_vec<CoolingDevice_1_0> &)> _hidl_cb)118*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::getCoolingDevices(
119*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&, const hidl_vec<CoolingDevice_1_0>&)> _hidl_cb) {
120*4d7e907cSAndroid Build Coastguard Worker hidl_vec<CoolingDevice_1_0> ret_1_0;
121*4d7e907cSAndroid Build Coastguard Worker setUnsupportedFailureAndCallback(_hidl_cb, ret_1_0);
122*4d7e907cSAndroid Build Coastguard Worker return Void();
123*4d7e907cSAndroid Build Coastguard Worker }
124*4d7e907cSAndroid Build Coastguard Worker
125*4d7e907cSAndroid Build Coastguard Worker // Methods from ::android::hardware::thermal::V2_0::IThermal follow.
getCurrentTemperatures(bool filterType,TemperatureType_2_0 type,std::function<void (const ThermalStatus &,const hidl_vec<Temperature_2_0> &)> _hidl_cb)126*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::getCurrentTemperatures(
127*4d7e907cSAndroid Build Coastguard Worker bool filterType, TemperatureType_2_0 type,
128*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&, const hidl_vec<Temperature_2_0>&)> _hidl_cb) {
129*4d7e907cSAndroid Build Coastguard Worker hidl_vec<Temperature_2_0> ret_2_0;
130*4d7e907cSAndroid Build Coastguard Worker if (!thermal_service_) {
131*4d7e907cSAndroid Build Coastguard Worker setInitFailureAndCallback(_hidl_cb, ret_2_0);
132*4d7e907cSAndroid Build Coastguard Worker }
133*4d7e907cSAndroid Build Coastguard Worker
134*4d7e907cSAndroid Build Coastguard Worker std::vector<Temperature> ret_aidl;
135*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
136*4d7e907cSAndroid Build Coastguard Worker ::ndk::ScopedAStatus a_status;
137*4d7e907cSAndroid Build Coastguard Worker if (filterType) {
138*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->getTemperaturesWithType(static_cast<TemperatureType>(type),
139*4d7e907cSAndroid Build Coastguard Worker &ret_aidl);
140*4d7e907cSAndroid Build Coastguard Worker } else {
141*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->getTemperatures(&ret_aidl);
142*4d7e907cSAndroid Build Coastguard Worker }
143*4d7e907cSAndroid Build Coastguard Worker if (a_status.isOk()) {
144*4d7e907cSAndroid Build Coastguard Worker std::vector<Temperature_2_0> ret;
145*4d7e907cSAndroid Build Coastguard Worker for (const auto& temperature : ret_aidl) {
146*4d7e907cSAndroid Build Coastguard Worker ret.push_back(convertAidlTemperature(temperature));
147*4d7e907cSAndroid Build Coastguard Worker }
148*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status, hidl_vec<Temperature_2_0>(ret));
149*4d7e907cSAndroid Build Coastguard Worker } else {
150*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, ret_2_0, a_status.getMessage());
151*4d7e907cSAndroid Build Coastguard Worker }
152*4d7e907cSAndroid Build Coastguard Worker return Void();
153*4d7e907cSAndroid Build Coastguard Worker }
154*4d7e907cSAndroid Build Coastguard Worker
getTemperatureThresholds(bool filterType,TemperatureType_2_0 type,std::function<void (const ThermalStatus &,const hidl_vec<TemperatureThreshold_2_0> &)> _hidl_cb)155*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::getTemperatureThresholds(
156*4d7e907cSAndroid Build Coastguard Worker bool filterType, TemperatureType_2_0 type,
157*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&, const hidl_vec<TemperatureThreshold_2_0>&)>
158*4d7e907cSAndroid Build Coastguard Worker _hidl_cb) {
159*4d7e907cSAndroid Build Coastguard Worker hidl_vec<TemperatureThreshold_2_0> ret_2_0;
160*4d7e907cSAndroid Build Coastguard Worker if (!thermal_service_) {
161*4d7e907cSAndroid Build Coastguard Worker setInitFailureAndCallback(_hidl_cb, ret_2_0);
162*4d7e907cSAndroid Build Coastguard Worker }
163*4d7e907cSAndroid Build Coastguard Worker
164*4d7e907cSAndroid Build Coastguard Worker std::vector<TemperatureThreshold> ret_aidl;
165*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
166*4d7e907cSAndroid Build Coastguard Worker ::ndk::ScopedAStatus a_status;
167*4d7e907cSAndroid Build Coastguard Worker if (filterType) {
168*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->getTemperatureThresholdsWithType(
169*4d7e907cSAndroid Build Coastguard Worker static_cast<TemperatureType>(type), &ret_aidl);
170*4d7e907cSAndroid Build Coastguard Worker } else {
171*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->getTemperatureThresholds(&ret_aidl);
172*4d7e907cSAndroid Build Coastguard Worker }
173*4d7e907cSAndroid Build Coastguard Worker if (a_status.isOk()) {
174*4d7e907cSAndroid Build Coastguard Worker std::vector<TemperatureThreshold_2_0> ret;
175*4d7e907cSAndroid Build Coastguard Worker for (const auto& threshold : ret_aidl) {
176*4d7e907cSAndroid Build Coastguard Worker ret.push_back(convertAidlTemperatureThreshold(threshold));
177*4d7e907cSAndroid Build Coastguard Worker }
178*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status, hidl_vec<TemperatureThreshold_2_0>(ret));
179*4d7e907cSAndroid Build Coastguard Worker } else {
180*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, ret_2_0, a_status.getMessage());
181*4d7e907cSAndroid Build Coastguard Worker }
182*4d7e907cSAndroid Build Coastguard Worker return Void();
183*4d7e907cSAndroid Build Coastguard Worker }
184*4d7e907cSAndroid Build Coastguard Worker
registerThermalChangedCallback(const sp<IThermalChangedCallback_2_0> & callback,bool filterType,TemperatureType_2_0 type,std::function<void (const ThermalStatus &)> _hidl_cb)185*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::registerThermalChangedCallback(
186*4d7e907cSAndroid Build Coastguard Worker const sp<IThermalChangedCallback_2_0>& callback, bool filterType, TemperatureType_2_0 type,
187*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&)> _hidl_cb) {
188*4d7e907cSAndroid Build Coastguard Worker if (!thermal_service_) {
189*4d7e907cSAndroid Build Coastguard Worker setInitFailureAndCallback(_hidl_cb);
190*4d7e907cSAndroid Build Coastguard Worker }
191*4d7e907cSAndroid Build Coastguard Worker if (callback == nullptr) {
192*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, "Invalid nullptr callback");
193*4d7e907cSAndroid Build Coastguard Worker return Void();
194*4d7e907cSAndroid Build Coastguard Worker }
195*4d7e907cSAndroid Build Coastguard Worker std::lock_guard<std::mutex> _lock(callback_wrappers_mutex_);
196*4d7e907cSAndroid Build Coastguard Worker for (const auto& callback_wrapper : callback_wrappers_) {
197*4d7e907cSAndroid Build Coastguard Worker if (::android::hardware::interfacesEqual(callback_wrapper->callback_2_0_.get(),
198*4d7e907cSAndroid Build Coastguard Worker callback.get())) {
199*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, "The callback was already registered through wrapper");
200*4d7e907cSAndroid Build Coastguard Worker return Void();
201*4d7e907cSAndroid Build Coastguard Worker }
202*4d7e907cSAndroid Build Coastguard Worker }
203*4d7e907cSAndroid Build Coastguard Worker std::shared_ptr<IThermalChangedCallbackWrapper> callback_wrapper =
204*4d7e907cSAndroid Build Coastguard Worker ndk::SharedRefBase::make<IThermalChangedCallbackWrapper>(callback);
205*4d7e907cSAndroid Build Coastguard Worker ::ndk::ScopedAStatus a_status;
206*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
207*4d7e907cSAndroid Build Coastguard Worker if (filterType) {
208*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->registerThermalChangedCallbackWithType(
209*4d7e907cSAndroid Build Coastguard Worker callback_wrapper, static_cast<TemperatureType>(type));
210*4d7e907cSAndroid Build Coastguard Worker } else {
211*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->registerThermalChangedCallback(callback_wrapper);
212*4d7e907cSAndroid Build Coastguard Worker }
213*4d7e907cSAndroid Build Coastguard Worker if (a_status.isOk()) {
214*4d7e907cSAndroid Build Coastguard Worker callback_wrappers_.push_back(callback_wrapper);
215*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status);
216*4d7e907cSAndroid Build Coastguard Worker } else {
217*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, a_status.getMessage());
218*4d7e907cSAndroid Build Coastguard Worker }
219*4d7e907cSAndroid Build Coastguard Worker return Void();
220*4d7e907cSAndroid Build Coastguard Worker }
221*4d7e907cSAndroid Build Coastguard Worker
unregisterThermalChangedCallback(const sp<IThermalChangedCallback_2_0> & callback,std::function<void (const ThermalStatus &)> _hidl_cb)222*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::unregisterThermalChangedCallback(
223*4d7e907cSAndroid Build Coastguard Worker const sp<IThermalChangedCallback_2_0>& callback,
224*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&)> _hidl_cb) {
225*4d7e907cSAndroid Build Coastguard Worker if (!thermal_service_) {
226*4d7e907cSAndroid Build Coastguard Worker setInitFailureAndCallback(_hidl_cb);
227*4d7e907cSAndroid Build Coastguard Worker }
228*4d7e907cSAndroid Build Coastguard Worker if (callback == nullptr) {
229*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, "Invalid nullptr callback");
230*4d7e907cSAndroid Build Coastguard Worker return Void();
231*4d7e907cSAndroid Build Coastguard Worker }
232*4d7e907cSAndroid Build Coastguard Worker std::lock_guard<std::mutex> _lock(callback_wrappers_mutex_);
233*4d7e907cSAndroid Build Coastguard Worker for (auto it = callback_wrappers_.begin(); it != callback_wrappers_.end(); it++) {
234*4d7e907cSAndroid Build Coastguard Worker auto callback_wrapper = *it;
235*4d7e907cSAndroid Build Coastguard Worker if (::android::hardware::interfacesEqual(callback_wrapper->callback_2_0_.get(),
236*4d7e907cSAndroid Build Coastguard Worker callback.get())) {
237*4d7e907cSAndroid Build Coastguard Worker ::ndk::ScopedAStatus a_status;
238*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
239*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->unregisterThermalChangedCallback(callback_wrapper);
240*4d7e907cSAndroid Build Coastguard Worker if (a_status.isOk()) {
241*4d7e907cSAndroid Build Coastguard Worker callback_wrappers_.erase(it);
242*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status);
243*4d7e907cSAndroid Build Coastguard Worker } else {
244*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, a_status.getMessage());
245*4d7e907cSAndroid Build Coastguard Worker }
246*4d7e907cSAndroid Build Coastguard Worker return Void();
247*4d7e907cSAndroid Build Coastguard Worker }
248*4d7e907cSAndroid Build Coastguard Worker }
249*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, "The callback was not registered through wrapper before");
250*4d7e907cSAndroid Build Coastguard Worker return Void();
251*4d7e907cSAndroid Build Coastguard Worker }
252*4d7e907cSAndroid Build Coastguard Worker
getCurrentCoolingDevices(bool filterType,CoolingType_2_0 type,std::function<void (const ThermalStatus &,const hidl_vec<CoolingDevice_2_0> &)> _hidl_cb)253*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::getCurrentCoolingDevices(
254*4d7e907cSAndroid Build Coastguard Worker bool filterType, CoolingType_2_0 type,
255*4d7e907cSAndroid Build Coastguard Worker std::function<void(const ThermalStatus&, const hidl_vec<CoolingDevice_2_0>&)> _hidl_cb) {
256*4d7e907cSAndroid Build Coastguard Worker hidl_vec<CoolingDevice_2_0> ret_2_0;
257*4d7e907cSAndroid Build Coastguard Worker if (!thermal_service_) {
258*4d7e907cSAndroid Build Coastguard Worker setInitFailureAndCallback(_hidl_cb, ret_2_0);
259*4d7e907cSAndroid Build Coastguard Worker }
260*4d7e907cSAndroid Build Coastguard Worker
261*4d7e907cSAndroid Build Coastguard Worker std::vector<CoolingDevice> ret_aidl;
262*4d7e907cSAndroid Build Coastguard Worker ThermalStatus status;
263*4d7e907cSAndroid Build Coastguard Worker ::ndk::ScopedAStatus a_status;
264*4d7e907cSAndroid Build Coastguard Worker if (filterType) {
265*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->getCoolingDevicesWithType(static_cast<CoolingType>(type),
266*4d7e907cSAndroid Build Coastguard Worker &ret_aidl);
267*4d7e907cSAndroid Build Coastguard Worker } else {
268*4d7e907cSAndroid Build Coastguard Worker a_status = thermal_service_->getCoolingDevices(&ret_aidl);
269*4d7e907cSAndroid Build Coastguard Worker }
270*4d7e907cSAndroid Build Coastguard Worker if (a_status.isOk()) {
271*4d7e907cSAndroid Build Coastguard Worker std::vector<CoolingDevice_2_0> ret;
272*4d7e907cSAndroid Build Coastguard Worker for (const auto& cooling_device : ret_aidl) {
273*4d7e907cSAndroid Build Coastguard Worker ret.push_back(convertAidlCoolingDevice(cooling_device));
274*4d7e907cSAndroid Build Coastguard Worker }
275*4d7e907cSAndroid Build Coastguard Worker _hidl_cb(status, hidl_vec<CoolingDevice_2_0>(ret));
276*4d7e907cSAndroid Build Coastguard Worker } else {
277*4d7e907cSAndroid Build Coastguard Worker setFailureAndCallback(_hidl_cb, ret_2_0, a_status.getMessage());
278*4d7e907cSAndroid Build Coastguard Worker }
279*4d7e907cSAndroid Build Coastguard Worker return Void();
280*4d7e907cSAndroid Build Coastguard Worker }
281*4d7e907cSAndroid Build Coastguard Worker
282*4d7e907cSAndroid Build Coastguard Worker // Methods from ::android::hidl::base::V1_0::IBase follow.
debug(const hidl_handle & handle,const hidl_vec<hidl_string> & args)283*4d7e907cSAndroid Build Coastguard Worker Return<void> ThermalHidlWrapper::debug(const hidl_handle& handle,
284*4d7e907cSAndroid Build Coastguard Worker const hidl_vec<hidl_string>& args) {
285*4d7e907cSAndroid Build Coastguard Worker if (handle != nullptr && handle->numFds >= 1) {
286*4d7e907cSAndroid Build Coastguard Worker int fd = handle->data[0];
287*4d7e907cSAndroid Build Coastguard Worker char** arr = new char*[args.size()];
288*4d7e907cSAndroid Build Coastguard Worker for (size_t i = 0; i < args.size(); i++) {
289*4d7e907cSAndroid Build Coastguard Worker arr[i] = strdup(args[i].c_str());
290*4d7e907cSAndroid Build Coastguard Worker }
291*4d7e907cSAndroid Build Coastguard Worker thermal_service_->dump(fd, (const char**)arr, args.size());
292*4d7e907cSAndroid Build Coastguard Worker }
293*4d7e907cSAndroid Build Coastguard Worker return Void();
294*4d7e907cSAndroid Build Coastguard Worker }
295*4d7e907cSAndroid Build Coastguard Worker
notifyThrottling(const Temperature & temperature)296*4d7e907cSAndroid Build Coastguard Worker ::ndk::ScopedAStatus ThermalHidlWrapper::IThermalChangedCallbackWrapper::notifyThrottling(
297*4d7e907cSAndroid Build Coastguard Worker const Temperature& temperature) {
298*4d7e907cSAndroid Build Coastguard Worker callback_2_0_->notifyThrottling(convertAidlTemperature(temperature));
299*4d7e907cSAndroid Build Coastguard Worker return ::ndk::ScopedAStatus::ok();
300*4d7e907cSAndroid Build Coastguard Worker }
301*4d7e907cSAndroid Build Coastguard Worker
302*4d7e907cSAndroid Build Coastguard Worker } // namespace thermal
303*4d7e907cSAndroid Build Coastguard Worker } // namespace hardware
304*4d7e907cSAndroid Build Coastguard Worker } // namespace android
305*4d7e907cSAndroid Build Coastguard Worker } // namespace aidl
306