• 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 class Thermal : public BnThermal {
42   public:
43     Thermal();
44     explicit Thermal(const std::shared_ptr<ThermalHelper> &helper);
45     ~Thermal() = default;
46     ndk::ScopedAStatus getTemperatures(std::vector<Temperature> *_aidl_return) override;
47     ndk::ScopedAStatus getTemperaturesWithType(TemperatureType type,
48                                                std::vector<Temperature> *_aidl_return) override;
49 
50     ndk::ScopedAStatus getCoolingDevices(std::vector<CoolingDevice> *_aidl_return) override;
51     ndk::ScopedAStatus getCoolingDevicesWithType(CoolingType type,
52                                                  std::vector<CoolingDevice> *_aidl_return) override;
53 
54     ndk::ScopedAStatus getTemperatureThresholds(
55             std::vector<TemperatureThreshold> *_aidl_return) override;
56     ndk::ScopedAStatus getTemperatureThresholdsWithType(
57             TemperatureType type, std::vector<TemperatureThreshold> *_aidl_return) override;
58 
59     ndk::ScopedAStatus registerThermalChangedCallback(
60             const std::shared_ptr<IThermalChangedCallback> &callback) override;
61     ndk::ScopedAStatus registerThermalChangedCallbackWithType(
62             const std::shared_ptr<IThermalChangedCallback> &callback,
63             TemperatureType type) override;
64     ndk::ScopedAStatus unregisterThermalChangedCallback(
65             const std::shared_ptr<IThermalChangedCallback> &callback) override;
66     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
67 
68     // Helper function for calling callbacks
69     void sendThermalChangedCallback(const Temperature &t);
70 
71   private:
72     class Looper {
73       public:
74         struct Event {
75             std::function<void()> handler;
76         };
77 
Looper()78         Looper() {
79             thread_ = std::thread([&] { loop(); });
80         }
81         ~Looper();
82 
83         void addEvent(const Event &e);
84 
85       private:
86         std::condition_variable cv_;
87         std::queue<Event> events_;
88         std::mutex mutex_;
89         std::thread thread_;
90         bool aborted_;
91 
92         void loop();
93     };
94 
95     std::shared_ptr<ThermalHelper> thermal_helper_;
96     std::mutex thermal_callback_mutex_;
97     std::vector<CallbackSetting> callbacks_;
98     Looper looper_;
99 
100     ndk::ScopedAStatus getFilteredTemperatures(bool filterType, TemperatureType type,
101                                                std::vector<Temperature> *_aidl_return);
102     ndk::ScopedAStatus getFilteredCoolingDevices(bool filterType, CoolingType type,
103                                                  std::vector<CoolingDevice> *_aidl_return);
104     ndk::ScopedAStatus getFilteredTemperatureThresholds(
105             bool filterType, TemperatureType type, std::vector<TemperatureThreshold> *_aidl_return);
106     ndk::ScopedAStatus registerThermalChangedCallback(
107             const std::shared_ptr<IThermalChangedCallback> &callback, bool filterType,
108             TemperatureType type);
109 
110     void dumpVirtualSensorInfo(std::ostringstream *dump_buf);
111     void dumpThrottlingInfo(std::ostringstream *dump_buf);
112     void dumpThrottlingRequestStatus(std::ostringstream *dump_buf);
113     void dumpPowerRailInfo(std::ostringstream *dump_buf);
114     void dumpStatsRecord(std::ostringstream *dump_buf, const StatsRecord &stats_record,
115                          std::string_view line_prefix);
116     void dumpThermalStats(std::ostringstream *dump_buf);
117     void dumpThermalData(int fd);
118 };
119 
120 }  // namespace implementation
121 }  // namespace thermal
122 }  // namespace hardware
123 }  // namespace android
124 }  // namespace aidl
125