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 #ifndef ANDROID_HARDWARE_THERMAL_V2_0_CROSSHATCH_THERMAL_H
17 #define ANDROID_HARDWARE_THERMAL_V2_0_CROSSHATCH_THERMAL_H
18 
19 #include <mutex>
20 #include <thread>
21 
22 #include <android/hardware/thermal/2.0/IThermal.h>
23 #include <android/hardware/thermal/2.0/IThermalChangedCallback.h>
24 #include <hidl/Status.h>
25 
26 #include "thermal-helper.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace thermal {
31 namespace V2_0 {
32 namespace implementation {
33 
34 using ::android::sp;
35 using ::android::hardware::hidl_vec;
36 using ::android::hardware::Return;
37 using ::android::hardware::thermal::V2_0::IThermal;
38 using ::android::hardware::thermal::V2_0::IThermalChangedCallback;
39 
40 struct CallbackSetting {
CallbackSettingCallbackSetting41     CallbackSetting(sp<IThermalChangedCallback> callback, bool is_filter_type,
42                     TemperatureType_2_0 type)
43         : callback(callback), is_filter_type(is_filter_type), type(type) {}
44     sp<IThermalChangedCallback> callback;
45     bool is_filter_type;
46     TemperatureType_2_0 type;
47 };
48 
49 class Thermal : public IThermal {
50   public:
51     Thermal();
52     ~Thermal() = default;
53 
54     // Disallow copy and assign.
55     Thermal(const Thermal &) = delete;
56     void operator=(const Thermal &) = delete;
57 
58     // Methods from ::android::hardware::thermal::V1_0::IThermal.
59     Return<void> getTemperatures(getTemperatures_cb _hidl_cb) override;
60     Return<void> getCpuUsages(getCpuUsages_cb _hidl_cb) override;
61     Return<void> getCoolingDevices(getCoolingDevices_cb _hidl_cb) override;
62 
63     // Methods from ::android::hardware::thermal::V2_0::IThermal follow.
64     Return<void> getCurrentTemperatures(bool filterType, TemperatureType_2_0 type,
65                                         getCurrentTemperatures_cb _hidl_cb) override;
66     Return<void> getTemperatureThresholds(bool filterType, TemperatureType_2_0 type,
67                                           getTemperatureThresholds_cb _hidl_cb) override;
68     Return<void> registerThermalChangedCallback(const sp<IThermalChangedCallback> &callback,
69                                                 bool filterType, TemperatureType_2_0 type,
70                                                 registerThermalChangedCallback_cb _hidl_cb) override;
71     Return<void> unregisterThermalChangedCallback(
72         const sp<IThermalChangedCallback> &callback,
73         unregisterThermalChangedCallback_cb _hidl_cb) override;
74     Return<void> getCurrentCoolingDevices(bool filterType, CoolingType type,
75                                           getCurrentCoolingDevices_cb _hidl_cb) override;
76 
77     // Methods from ::android::hidl::base::V1_0::IBase follow.
78     Return<void> debug(const hidl_handle &fd, const hidl_vec<hidl_string> &args) override;
79 
80     // Helper function for calling callbacks
81     void sendThermalChangedCallback(const std::vector<Temperature_2_0> &temps);
82 
83   private:
84     ThermalHelper thermal_helper_;
85     std::mutex thermal_callback_mutex_;
86     std::vector<CallbackSetting> callbacks_;
87 };
88 
89 }  // namespace implementation
90 }  // namespace V2_0
91 }  // namespace thermal
92 }  // namespace hardware
93 }  // namespace android
94 
95 #endif  // ANDROID_HARDWARE_THERMAL_V2_0_CROSSHATCH_THERMAL_H
96