1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <aidl/android/hardware/thermal/BnThermal.h>
20 
21 #include <mutex>
22 #include <thread>
23 
24 #include "thermal-helper.h"
25 
26 namespace aidl {
27 namespace android {
28 namespace hardware {
29 namespace thermal {
30 namespace implementation {
31 
32 struct CallbackSetting {
CallbackSettingCallbackSetting33     CallbackSetting(std::shared_ptr<IThermalChangedCallback> callback, bool is_filter_type,
34                     TemperatureType type)
35         : callback(std::move(callback)), is_filter_type(is_filter_type), type(type) {}
36     std::shared_ptr<IThermalChangedCallback> callback;
37     bool is_filter_type;
38     TemperatureType type;
39 };
40 
41 struct CoolingDeviceCallbackSetting {
CoolingDeviceCallbackSettingCoolingDeviceCallbackSetting42     CoolingDeviceCallbackSetting(std::shared_ptr<ICoolingDeviceChangedCallback> callback,
43                                  bool is_filter_type, CoolingType type)
44         : callback(std::move(callback)), is_filter_type(is_filter_type), type(type) {}
45     std::shared_ptr<ICoolingDeviceChangedCallback> callback;
46     bool is_filter_type;
47     CoolingType type;
48 };
49 
50 class Thermal : public BnThermal {
51   public:
52     Thermal();
53     explicit Thermal(const std::shared_ptr<ThermalHelper> &helper);
54     ~Thermal() = default;
55     ndk::ScopedAStatus getTemperatures(std::vector<Temperature> *_aidl_return) override;
56     ndk::ScopedAStatus getTemperaturesWithType(TemperatureType type,
57                                                std::vector<Temperature> *_aidl_return) override;
58 
59     ndk::ScopedAStatus getTemperatureThresholds(
60             std::vector<TemperatureThreshold> *_aidl_return) override;
61     ndk::ScopedAStatus getTemperatureThresholdsWithType(
62             TemperatureType type, std::vector<TemperatureThreshold> *_aidl_return) override;
63 
64     ndk::ScopedAStatus registerThermalChangedCallback(
65             const std::shared_ptr<IThermalChangedCallback> &callback) override;
66     ndk::ScopedAStatus registerThermalChangedCallbackWithType(
67             const std::shared_ptr<IThermalChangedCallback> &callback,
68             TemperatureType type) override;
69     ndk::ScopedAStatus unregisterThermalChangedCallback(
70             const std::shared_ptr<IThermalChangedCallback> &callback) override;
71 
72     ndk::ScopedAStatus getCoolingDevices(std::vector<CoolingDevice> *_aidl_return) override;
73     ndk::ScopedAStatus getCoolingDevicesWithType(CoolingType type,
74                                                  std::vector<CoolingDevice> *_aidl_return) override;
75 
76     ndk::ScopedAStatus registerCoolingDeviceChangedCallbackWithType(
77             const std::shared_ptr<ICoolingDeviceChangedCallback> &callback,
78             CoolingType type) override;
79     ndk::ScopedAStatus unregisterCoolingDeviceChangedCallback(
80             const std::shared_ptr<ICoolingDeviceChangedCallback> &callback) override;
81     ndk::ScopedAStatus forecastSkinTemperature(int32_t forecastSeconds,
82                                                float *_aidl_return) override;
83 
84     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
85 
86     // Helper function for calling callbacks
87     void sendThermalChangedCallback(const Temperature &t);
88 
89   private:
90     class Looper {
91       public:
92         struct Event {
93             std::function<void()> handler;
94         };
95 
Looper()96         Looper() {
97             thread_ = std::thread([&] { loop(); });
98         }
99         ~Looper();
100 
101         void addEvent(const Event &e);
102 
103       private:
104         std::condition_variable cv_;
105         std::queue<Event> events_;
106         std::mutex mutex_;
107         std::thread thread_;
108         bool aborted_;
109 
110         void loop();
111     };
112 
113     std::shared_ptr<ThermalHelper> thermal_helper_;
114     std::mutex thermal_callback_mutex_;
115     std::vector<CallbackSetting> callbacks_;
116     std::mutex cdev_callback_mutex_;
117     std::vector<CoolingDeviceCallbackSetting> cdev_callbacks_;
118 
119     Looper looper_;
120 
121     ndk::ScopedAStatus getFilteredTemperatures(bool filterType, TemperatureType type,
122                                                std::vector<Temperature> *_aidl_return);
123     ndk::ScopedAStatus getFilteredCoolingDevices(bool filterType, CoolingType type,
124                                                  std::vector<CoolingDevice> *_aidl_return);
125     ndk::ScopedAStatus getFilteredTemperatureThresholds(
126             bool filterType, TemperatureType type, std::vector<TemperatureThreshold> *_aidl_return);
127     ndk::ScopedAStatus registerThermalChangedCallback(
128             const std::shared_ptr<IThermalChangedCallback> &callback, bool filterType,
129             TemperatureType type);
130 
131     void dumpVirtualSensorInfo(std::ostringstream *dump_buf);
132     void dumpVtEstimatorInfo(std::ostringstream *dump_buf);
133     void dumpThrottlingInfo(std::ostringstream *dump_buf);
134     void dumpThrottlingRequestStatus(std::ostringstream *dump_buf);
135     void dumpPowerRailInfo(std::ostringstream *dump_buf);
136     void dumpStatsRecord(std::ostringstream *dump_buf, const StatsRecord &stats_record,
137                          std::string_view line_prefix);
138     void dumpThermalStats(std::ostringstream *dump_buf);
139     void dumpThermalData(int fd, const char **args, uint32_t numArgs);
140 };
141 
142 }  // namespace implementation
143 }  // namespace thermal
144 }  // namespace hardware
145 }  // namespace android
146 }  // namespace aidl
147