• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
82     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
83 
84     // Helper function for calling callbacks
85     void sendThermalChangedCallback(const Temperature &t);
86 
87   private:
88     class Looper {
89       public:
90         struct Event {
91             std::function<void()> handler;
92         };
93 
Looper()94         Looper() {
95             thread_ = std::thread([&] { loop(); });
96         }
97         ~Looper();
98 
99         void addEvent(const Event &e);
100 
101       private:
102         std::condition_variable cv_;
103         std::queue<Event> events_;
104         std::mutex mutex_;
105         std::thread thread_;
106         bool aborted_;
107 
108         void loop();
109     };
110 
111     std::shared_ptr<ThermalHelper> thermal_helper_;
112     std::mutex thermal_callback_mutex_;
113     std::vector<CallbackSetting> callbacks_;
114     std::mutex cdev_callback_mutex_;
115     std::vector<CoolingDeviceCallbackSetting> cdev_callbacks_;
116 
117     Looper looper_;
118 
119     ndk::ScopedAStatus getFilteredTemperatures(bool filterType, TemperatureType type,
120                                                std::vector<Temperature> *_aidl_return);
121     ndk::ScopedAStatus getFilteredCoolingDevices(bool filterType, CoolingType type,
122                                                  std::vector<CoolingDevice> *_aidl_return);
123     ndk::ScopedAStatus getFilteredTemperatureThresholds(
124             bool filterType, TemperatureType type, std::vector<TemperatureThreshold> *_aidl_return);
125     ndk::ScopedAStatus registerThermalChangedCallback(
126             const std::shared_ptr<IThermalChangedCallback> &callback, bool filterType,
127             TemperatureType type);
128 
129     void dumpVirtualSensorInfo(std::ostringstream *dump_buf);
130     void dumpVtEstimatorInfo(std::ostringstream *dump_buf);
131     void dumpThrottlingInfo(std::ostringstream *dump_buf);
132     void dumpThrottlingRequestStatus(std::ostringstream *dump_buf);
133     void dumpPowerRailInfo(std::ostringstream *dump_buf);
134     void dumpStatsRecord(std::ostringstream *dump_buf, const StatsRecord &stats_record,
135                          std::string_view line_prefix);
136     void dumpThermalStats(std::ostringstream *dump_buf);
137     void dumpThermalData(int fd, const char **args, uint32_t numArgs);
138 };
139 
140 }  // namespace implementation
141 }  // namespace thermal
142 }  // namespace hardware
143 }  // namespace android
144 }  // namespace aidl
145