• 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 <android/hardware/thermal/2.0/IThermal.h>
44 
45 #include "utils/config_parser.h"
46 #include "utils/thermal_files.h"
47 #include "utils/thermal_watcher.h"
48 
49 namespace android {
50 namespace hardware {
51 namespace thermal {
52 namespace V2_0 {
53 namespace implementation {
54 
55 using ::android::hardware::hidl_vec;
56 using ::android::hardware::thermal::V1_0::CpuUsage;
57 using ::android::hardware::thermal::V2_0::CoolingType;
58 using ::android::hardware::thermal::V2_0::IThermal;
59 using CoolingDevice_1_0 = ::android::hardware::thermal::V1_0::CoolingDevice;
60 using CoolingDevice_2_0 = ::android::hardware::thermal::V2_0::CoolingDevice;
61 using Temperature_1_0 = ::android::hardware::thermal::V1_0::Temperature;
62 using Temperature_2_0 = ::android::hardware::thermal::V2_0::Temperature;
63 using TemperatureType_1_0 = ::android::hardware::thermal::V1_0::TemperatureType;
64 using TemperatureType_2_0 = ::android::hardware::thermal::V2_0::TemperatureType;
65 using ::android::hardware::thermal::V2_0::TemperatureThreshold;
66 using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
67 
68 using NotificationCallback = std::function<void(const std::vector<Temperature_2_0> &temps)>;
69 using NotificationTime = std::chrono::time_point<std::chrono::steady_clock>;
70 
71 struct SensorStatus {
72     ThrottlingSeverity severity;
73     ThrottlingSeverity prev_hot_severity;
74     ThrottlingSeverity prev_cold_severity;
75 };
76 
77 class ThermalHelper {
78   public:
79     ThermalHelper(const NotificationCallback &cb);
80     ~ThermalHelper() = default;
81 
82     bool fillTemperatures(hidl_vec<Temperature_1_0> *temperatures) const;
83     bool fillCurrentTemperatures(bool filterType, TemperatureType_2_0 type,
84                                  hidl_vec<Temperature_2_0> *temperatures) const;
85     bool fillTemperatureThresholds(bool filterType, TemperatureType_2_0 type,
86                                    hidl_vec<TemperatureThreshold> *thresholds) const;
87     bool fillCurrentCoolingDevices(bool filterType, CoolingType type,
88                                    hidl_vec<CoolingDevice_2_0> *coolingdevices) const;
89     bool fillCpuUsages(hidl_vec<CpuUsage> *cpu_usages) const;
90 
91     // Dissallow copy and assign.
92     ThermalHelper(const ThermalHelper &) = delete;
93     void operator=(const ThermalHelper &) = delete;
94 
isInitializedOk()95     bool isInitializedOk() const { return is_initialized_; }
96 
97     // Read the temperature of a single sensor.
98     bool readTemperature(std::string_view sensor_name, Temperature_1_0 *out) const;
99     bool readTemperature(
100             std::string_view sensor_name, Temperature_2_0 *out,
101             std::pair<ThrottlingSeverity, ThrottlingSeverity> *throtting_status = nullptr) const;
102     bool readTemperatureThreshold(std::string_view sensor_name, TemperatureThreshold *out) const;
103     // Read the value of a single cooling device.
104     bool readCoolingDevice(std::string_view cooling_device, CoolingDevice_2_0 *out) const;
105     // Get SensorInfo Map
GetSensorInfoMap()106     const std::map<std::string, SensorInfo> &GetSensorInfoMap() const { return sensor_info_map_; }
107 
108   private:
109     bool initializeSensorMap(const std::map<std::string, std::string> &path_map);
110     bool initializeCoolingDevices(const std::map<std::string, std::string> &path_map);
111     bool initializeTrip(const std::map<std::string, std::string> &path_map);
112 
113     // For thermal_watcher_'s polling thread
114     bool thermalWatcherCallbackFunc(const std::set<std::string> &uevent_sensors);
115     // Return hot and cold severity status as std::pair
116     std::pair<ThrottlingSeverity, ThrottlingSeverity> getSeverityFromThresholds(
117         const ThrottlingArray &hot_thresholds, const ThrottlingArray &cold_thresholds,
118         const ThrottlingArray &hot_hysteresis, const ThrottlingArray &cold_hysteresis,
119         ThrottlingSeverity prev_hot_severity, ThrottlingSeverity prev_cold_severity,
120         float value) const;
121 
122     sp<ThermalWatcher> thermal_watcher_;
123     ThermalFiles thermal_sensors_;
124     ThermalFiles cooling_devices_;
125     bool is_initialized_;
126     const NotificationCallback cb_;
127     const std::map<std::string, CoolingType> cooling_device_info_map_;
128     const std::map<std::string, SensorInfo> sensor_info_map_;
129 
130     mutable std::shared_mutex sensor_status_map_mutex_;
131     std::map<std::string, SensorStatus> sensor_status_map_;
132 };
133 
134 }  // namespace implementation
135 }  // namespace V2_0
136 }  // namespace thermal
137 }  // namespace hardware
138 }  // namespace android
139 
140 #endif  // THERMAL_THERMAL_HELPER_H__
141