• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * *    * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials provided
12  *       with the distribution.
13  *     * Neither the name of The Linux Foundation nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef THERMAL_THERMAL_HELPER_H__
31 #define THERMAL_THERMAL_HELPER_H__
32 
33 #include <array>
34 #include <chrono>
35 #include <mutex>
36 #include <shared_mutex>
37 #include <string>
38 #include <string_view>
39 #include <thread>
40 #include <unordered_map>
41 #include <vector>
42 
43 #include <aidl/android/hardware/power/IPower.h>
44 #include <aidl/google/hardware/power/extension/pixel/IPowerExt.h>
45 #include <android/hardware/thermal/2.0/IThermal.h>
46 
47 #include "utils/config_parser.h"
48 #include "utils/thermal_files.h"
49 #include "utils/thermal_watcher.h"
50 
51 namespace android {
52 namespace hardware {
53 namespace thermal {
54 namespace V2_0 {
55 namespace implementation {
56 
57 using ::aidl::android::hardware::power::IPower;
58 using ::aidl::google::hardware::power::extension::pixel::IPowerExt;
59 using ::android::hardware::hidl_vec;
60 using ::android::hardware::thermal::V1_0::CpuUsage;
61 using ::android::hardware::thermal::V2_0::CoolingType;
62 using ::android::hardware::thermal::V2_0::IThermal;
63 using CoolingDevice_1_0 = ::android::hardware::thermal::V1_0::CoolingDevice;
64 using CoolingDevice_2_0 = ::android::hardware::thermal::V2_0::CoolingDevice;
65 using Temperature_1_0 = ::android::hardware::thermal::V1_0::Temperature;
66 using Temperature_2_0 = ::android::hardware::thermal::V2_0::Temperature;
67 using TemperatureType_1_0 = ::android::hardware::thermal::V1_0::TemperatureType;
68 using TemperatureType_2_0 = ::android::hardware::thermal::V2_0::TemperatureType;
69 using ::android::hardware::thermal::V2_0::TemperatureThreshold;
70 using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
71 
72 using NotificationCallback = std::function<void(const std::vector<Temperature_2_0> &temps)>;
73 using NotificationTime = std::chrono::time_point<std::chrono::steady_clock>;
74 
75 struct SensorStatus {
76     ThrottlingSeverity severity;
77     ThrottlingSeverity prev_hot_severity;
78     ThrottlingSeverity prev_cold_severity;
79     ThrottlingSeverity prev_hint_severity;
80 };
81 
82 class PowerHalService {
83   public:
84     PowerHalService();
85     ~PowerHalService() = default;
86     bool connect();
isAidlPowerHalExist()87     bool isAidlPowerHalExist() { return power_hal_aidl_exist_; }
88     bool isModeSupported(const std::string &type, const ThrottlingSeverity &t);
isPowerHalConnected()89     bool isPowerHalConnected() { return power_hal_aidl_ != nullptr; }
isPowerHalExtConnected()90     bool isPowerHalExtConnected() { return power_hal_ext_aidl_ != nullptr; }
91     void setMode(const std::string &type, const ThrottlingSeverity &t, const bool &enable);
92 
93   private:
94     bool power_hal_aidl_exist_;
95     std::shared_ptr<IPower> power_hal_aidl_;
96     std::shared_ptr<IPowerExt> power_hal_ext_aidl_;
97     std::mutex lock_;
98 };
99 
100 class ThermalHelper {
101   public:
102     ThermalHelper(const NotificationCallback &cb);
103     ~ThermalHelper() = default;
104 
105     bool fillTemperatures(hidl_vec<Temperature_1_0> *temperatures) const;
106     bool fillCurrentTemperatures(bool filterType, TemperatureType_2_0 type,
107                                  hidl_vec<Temperature_2_0> *temperatures) const;
108     bool fillTemperatureThresholds(bool filterType, TemperatureType_2_0 type,
109                                    hidl_vec<TemperatureThreshold> *thresholds) const;
110     bool fillCurrentCoolingDevices(bool filterType, CoolingType type,
111                                    hidl_vec<CoolingDevice_2_0> *coolingdevices) const;
112     bool fillCpuUsages(hidl_vec<CpuUsage> *cpu_usages) const;
113 
114     // Dissallow copy and assign.
115     ThermalHelper(const ThermalHelper &) = delete;
116     void operator=(const ThermalHelper &) = delete;
117 
isInitializedOk()118     bool isInitializedOk() const { return is_initialized_; }
119 
120     // Read the temperature of a single sensor.
121     bool readTemperature(std::string_view sensor_name, Temperature_1_0 *out) const;
122     bool readTemperature(
123             std::string_view sensor_name, Temperature_2_0 *out,
124             std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr) const;
125     bool readTemperatureThreshold(std::string_view sensor_name, TemperatureThreshold *out) const;
126     // Read the value of a single cooling device.
127     bool readCoolingDevice(std::string_view cooling_device, CoolingDevice_2_0 *out) const;
128     // Get SensorInfo Map
GetSensorInfoMap()129     const std::map<std::string, SensorInfo> &GetSensorInfoMap() const { return sensor_info_map_; }
130 
131     void sendPowerExtHint(const Temperature_2_0 &t);
132 
isAidlPowerHalExist()133     bool isAidlPowerHalExist() { return power_hal_service_.isAidlPowerHalExist(); }
isPowerHalConnected()134     bool isPowerHalConnected() { return power_hal_service_.isPowerHalConnected(); }
isPowerHalExtConnected()135     bool isPowerHalExtConnected() { return power_hal_service_.isPowerHalExtConnected(); }
136 
137   private:
138     bool initializeSensorMap(const std::map<std::string, std::string> &path_map);
139     bool initializeCoolingDevices(const std::map<std::string, std::string> &path_map);
140     bool initializeTrip(const std::map<std::string, std::string> &path_map);
141 
142     // For thermal_watcher_'s polling thread
143     bool thermalWatcherCallbackFunc(const std::set<std::string> &uevent_sensors);
144     // Return hot and cold severity status as std::pair
145     std::pair<ThrottlingSeverity, ThrottlingSeverity> getSeverityFromThresholds(
146         const ThrottlingArray &hot_thresholds, const ThrottlingArray &cold_thresholds,
147         const ThrottlingArray &hot_hysteresis, const ThrottlingArray &cold_hysteresis,
148         ThrottlingSeverity prev_hot_severity, ThrottlingSeverity prev_cold_severity,
149         float value) const;
150 
151     bool connectToPowerHal();
152     void updateSupportedPowerHints();
153 
154     sp<ThermalWatcher> thermal_watcher_;
155     ThermalFiles thermal_sensors_;
156     ThermalFiles cooling_devices_;
157     bool is_initialized_;
158     const NotificationCallback cb_;
159     const std::map<std::string, CoolingType> cooling_device_info_map_;
160     const std::map<std::string, SensorInfo> sensor_info_map_;
161     std::map<std::string, std::map<ThrottlingSeverity, ThrottlingSeverity>>
162             supported_powerhint_map_;
163     PowerHalService power_hal_service_;
164 
165     mutable std::shared_mutex sensor_status_map_mutex_;
166     std::map<std::string, SensorStatus> sensor_status_map_;
167 };
168 
169 }  // namespace implementation
170 }  // namespace V2_0
171 }  // namespace thermal
172 }  // namespace hardware
173 }  // namespace android
174 
175 #endif  // THERMAL_THERMAL_HELPER_H__
176