1 /*
2  * Copyright (C) 2023 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 <aidl/android/hardware/thermal/BnThermalChangedCallback.h>
18 #include <aidl/android/hardware/thermal/IThermal.h>
19 #include <android/hardware/thermal/2.0/IThermal.h>
20 #include <android/hardware/thermal/2.0/IThermalChangedCallback.h>
21 #include <android/hardware/thermal/2.0/types.h>
22 #include <hidl/Status.h>
23 
24 #include <utility>
25 
26 namespace aidl {
27 namespace android {
28 namespace hardware {
29 namespace thermal {
30 
31 using ::android::sp;
32 using ::android::hardware::hidl_array;
33 using ::android::hardware::hidl_handle;
34 using ::android::hardware::hidl_string;
35 using ::android::hardware::hidl_vec;
36 using ::android::hardware::Return;
37 
38 using IThermal_Aidl = ::aidl::android::hardware::thermal::IThermal;
39 using ::android::hardware::thermal::V1_0::CpuUsage;
40 using CoolingType_2_0 = ::android::hardware::thermal::V2_0::CoolingType;
41 using CoolingDevice_1_0 = ::android::hardware::thermal::V1_0::CoolingDevice;
42 using CoolingDevice_2_0 = ::android::hardware::thermal::V2_0::CoolingDevice;
43 using IThermal_2_0 = ::android::hardware::thermal::V2_0::IThermal;
44 using IThermalChangedCallback_2_0 = ::android::hardware::thermal::V2_0::IThermalChangedCallback;
45 using Temperature_1_0 = ::android::hardware::thermal::V1_0::Temperature;
46 using Temperature_2_0 = ::android::hardware::thermal::V2_0::Temperature;
47 using TemperatureType_2_0 = ::android::hardware::thermal::V2_0::TemperatureType;
48 
49 using ::android::hardware::thermal::V1_0::ThermalStatus;
50 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
51 
52 using TemperatureThreshold_2_0 = ::android::hardware::thermal::V2_0::TemperatureThreshold;
53 using ThrottlingSeverity_2_0 = ::android::hardware::thermal::V2_0::ThrottlingSeverity;
54 
55 // This wrapper converts all Thermal HIDL 2.0 calls to AIDL calls and converts AIDL response to
56 // HIDL 2.0 response.
57 //
58 // For Thermal HIDL 1.0 calls, it returns unsupported error.
59 class ThermalHidlWrapper : public IThermal_2_0 {
60   public:
ThermalHidlWrapper(::std::shared_ptr<IThermal_Aidl> thermal_service)61     explicit ThermalHidlWrapper(::std::shared_ptr<IThermal_Aidl> thermal_service)
62         : thermal_service_(std::move(thermal_service)) {}
63 
64     // Methods from ::android::hardware::thermal::V1_0::IThermal follow.
65     Return<void> getTemperatures(getTemperatures_cb _hidl_cb) override;
66     Return<void> getCpuUsages(getCpuUsages_cb _hidl_cb) override;
67     Return<void> getCoolingDevices(getCoolingDevices_cb _hidl_cb) override;
68 
69     // Methods from ::android::hardware::thermal::V2_0::IThermal follow.
70     Return<void> getCurrentTemperatures(bool filterType, TemperatureType_2_0 type,
71                                         getCurrentTemperatures_cb _hidl_cb) override;
72     Return<void> getTemperatureThresholds(bool filterType, TemperatureType_2_0 type,
73                                           getTemperatureThresholds_cb _hidl_cb) override;
74     Return<void> registerThermalChangedCallback(
75             const sp<IThermalChangedCallback_2_0>& callback, bool filterType,
76             TemperatureType_2_0 type, registerThermalChangedCallback_cb _hidl_cb) override;
77     Return<void> unregisterThermalChangedCallback(
78             const sp<IThermalChangedCallback_2_0>& callback,
79             unregisterThermalChangedCallback_cb _hidl_cb) override;
80     Return<void> getCurrentCoolingDevices(bool filterType, CoolingType_2_0 type,
81                                           getCurrentCoolingDevices_cb _hidl_cb) override;
82 
83     // Methods from ::android::hidl::base::V1_0::IBase follow.
84     Return<void> debug(const hidl_handle& handle, const hidl_vec<hidl_string>& args) override;
85 
86   private:
87     class IThermalChangedCallbackWrapper : public BnThermalChangedCallback {
88       public:
IThermalChangedCallbackWrapper(const sp<IThermalChangedCallback_2_0> & callback_2_0)89         explicit IThermalChangedCallbackWrapper(const sp<IThermalChangedCallback_2_0>& callback_2_0)
90             : callback_2_0_(callback_2_0) {}
91         ::ndk::ScopedAStatus notifyThrottling(const Temperature& temperature) override;
92         sp<IThermalChangedCallback_2_0> callback_2_0_;
93     };
94 
95     // Reference to thermal service.
96     ::std::shared_ptr<IThermal_Aidl> thermal_service_;
97     // Mutex lock for read/write on callback wrappers.
98     std::mutex callback_wrappers_mutex_;
99     // All thermal changed callback wrappers registered.
100     ::std::vector<std::shared_ptr<IThermalChangedCallbackWrapper>> callback_wrappers_;
101 };
102 
103 }  // namespace thermal
104 }  // namespace hardware
105 }  // namespace android
106 }  // namespace aidl
107