1 /*
2 * Copyright (C) 2019 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 #define LOG_TAG "GCH_HidlThermalUtils"
18 //#define LOG_NDEBUG 0
19 #include "hidl_thermal_utils.h"
20
21 #include <log/log.h>
22
23 namespace android {
24 namespace hardware {
25 namespace hidl_thermal_utils {
26
Create(google_camera_hal::NotifyThrottlingFunc notify_throttling)27 std::unique_ptr<HidlThermalChangedCallback> HidlThermalChangedCallback::Create(
28 google_camera_hal::NotifyThrottlingFunc notify_throttling) {
29 auto thermal_changed_callback = std::unique_ptr<HidlThermalChangedCallback>(
30 new HidlThermalChangedCallback(notify_throttling));
31 if (thermal_changed_callback == nullptr) {
32 ALOGE("%s: Failed to create a thermal changed callback", __FUNCTION__);
33 return nullptr;
34 }
35
36 return thermal_changed_callback;
37 }
38
HidlThermalChangedCallback(google_camera_hal::NotifyThrottlingFunc notify_throttling)39 HidlThermalChangedCallback::HidlThermalChangedCallback(
40 google_camera_hal::NotifyThrottlingFunc notify_throttling)
41 : kNotifyThrottling(notify_throttling) {
42 }
43
ConvertToHidlTemperatureType(const google_camera_hal::TemperatureType & hal_temperature_type,TemperatureType * hidl_temperature_type)44 status_t ConvertToHidlTemperatureType(
45 const google_camera_hal::TemperatureType& hal_temperature_type,
46 TemperatureType* hidl_temperature_type) {
47 if (hidl_temperature_type == nullptr) {
48 ALOGE("%s: hidl_temperature_type is nullptr", __FUNCTION__);
49 return BAD_VALUE;
50 }
51
52 switch (hal_temperature_type) {
53 case google_camera_hal::TemperatureType::kUnknown:
54 *hidl_temperature_type = TemperatureType::UNKNOWN;
55 break;
56 case google_camera_hal::TemperatureType::kCpu:
57 *hidl_temperature_type = TemperatureType::CPU;
58 break;
59 case google_camera_hal::TemperatureType::kGpu:
60 *hidl_temperature_type = TemperatureType::GPU;
61 break;
62 case google_camera_hal::TemperatureType::kBattery:
63 *hidl_temperature_type = TemperatureType::BATTERY;
64 break;
65 case google_camera_hal::TemperatureType::kSkin:
66 *hidl_temperature_type = TemperatureType::SKIN;
67 break;
68 case google_camera_hal::TemperatureType::kUsbPort:
69 *hidl_temperature_type = TemperatureType::USB_PORT;
70 break;
71 case google_camera_hal::TemperatureType::kPowerAmplifier:
72 *hidl_temperature_type = TemperatureType::POWER_AMPLIFIER;
73 break;
74 case google_camera_hal::TemperatureType::kBclVoltage:
75 *hidl_temperature_type = TemperatureType::BCL_VOLTAGE;
76 break;
77 case google_camera_hal::TemperatureType::kBclCurrent:
78 *hidl_temperature_type = TemperatureType::BCL_CURRENT;
79 break;
80 case google_camera_hal::TemperatureType::kBclPercentage:
81 *hidl_temperature_type = TemperatureType::BCL_PERCENTAGE;
82 break;
83 case google_camera_hal::TemperatureType::kNpu:
84 *hidl_temperature_type = TemperatureType::NPU;
85 break;
86 default:
87 ALOGE("%s: Unknown temperature type: %d", __FUNCTION__,
88 hal_temperature_type);
89 return BAD_VALUE;
90 }
91
92 return OK;
93 }
94
ConvertToHalTemperatureType(const TemperatureType & hidl_temperature_type,google_camera_hal::TemperatureType * hal_temperature_type)95 status_t HidlThermalChangedCallback::ConvertToHalTemperatureType(
96 const TemperatureType& hidl_temperature_type,
97 google_camera_hal::TemperatureType* hal_temperature_type) {
98 if (hal_temperature_type == nullptr) {
99 ALOGE("%s: hal_temperature_type is nullptr", __FUNCTION__);
100 return BAD_VALUE;
101 }
102
103 switch (hidl_temperature_type) {
104 case TemperatureType::UNKNOWN:
105 *hal_temperature_type = google_camera_hal::TemperatureType::kUnknown;
106 break;
107 case TemperatureType::CPU:
108 *hal_temperature_type = google_camera_hal::TemperatureType::kCpu;
109 break;
110 case TemperatureType::GPU:
111 *hal_temperature_type = google_camera_hal::TemperatureType::kGpu;
112 break;
113 case TemperatureType::BATTERY:
114 *hal_temperature_type = google_camera_hal::TemperatureType::kBattery;
115 break;
116 case TemperatureType::SKIN:
117 *hal_temperature_type = google_camera_hal::TemperatureType::kSkin;
118 break;
119 case TemperatureType::USB_PORT:
120 *hal_temperature_type = google_camera_hal::TemperatureType::kUsbPort;
121 break;
122 case TemperatureType::POWER_AMPLIFIER:
123 *hal_temperature_type =
124 google_camera_hal::TemperatureType::kPowerAmplifier;
125 break;
126 case TemperatureType::BCL_VOLTAGE:
127 *hal_temperature_type = google_camera_hal::TemperatureType::kBclVoltage;
128 break;
129 case TemperatureType::BCL_CURRENT:
130 *hal_temperature_type = google_camera_hal::TemperatureType::kBclCurrent;
131 break;
132 case TemperatureType::BCL_PERCENTAGE:
133 *hal_temperature_type = google_camera_hal::TemperatureType::kBclPercentage;
134 break;
135 case TemperatureType::NPU:
136 *hal_temperature_type = google_camera_hal::TemperatureType::kNpu;
137 break;
138 default:
139 ALOGE("%s: Unknown temperature type: %d", __FUNCTION__,
140 hidl_temperature_type);
141 return BAD_VALUE;
142 }
143
144 return OK;
145 }
146
ConvertToHalThrottlingSeverity(const ThrottlingSeverity & hidl_throttling_severity,google_camera_hal::ThrottlingSeverity * hal_throttling_severity)147 status_t HidlThermalChangedCallback::ConvertToHalThrottlingSeverity(
148 const ThrottlingSeverity& hidl_throttling_severity,
149 google_camera_hal::ThrottlingSeverity* hal_throttling_severity) {
150 if (hal_throttling_severity == nullptr) {
151 ALOGE("%s: hal_throttling_severity is nullptr", __FUNCTION__);
152 return BAD_VALUE;
153 }
154
155 switch (hidl_throttling_severity) {
156 case ThrottlingSeverity::NONE:
157 *hal_throttling_severity = google_camera_hal::ThrottlingSeverity::kNone;
158 break;
159 case ThrottlingSeverity::LIGHT:
160 *hal_throttling_severity = google_camera_hal::ThrottlingSeverity::kLight;
161 break;
162 case ThrottlingSeverity::MODERATE:
163 *hal_throttling_severity =
164 google_camera_hal::ThrottlingSeverity::kModerate;
165 break;
166 case ThrottlingSeverity::SEVERE:
167 *hal_throttling_severity = google_camera_hal::ThrottlingSeverity::kSevere;
168 break;
169 case ThrottlingSeverity::CRITICAL:
170 *hal_throttling_severity =
171 google_camera_hal::ThrottlingSeverity::kCritical;
172 break;
173 case ThrottlingSeverity::EMERGENCY:
174 *hal_throttling_severity =
175 google_camera_hal::ThrottlingSeverity::kEmergency;
176 break;
177 case ThrottlingSeverity::SHUTDOWN:
178 *hal_throttling_severity =
179 google_camera_hal::ThrottlingSeverity::kShutdown;
180 break;
181 default:
182 ALOGE("%s: Unknown temperature severity: %d", __FUNCTION__,
183 hidl_throttling_severity);
184 return BAD_VALUE;
185 }
186
187 return OK;
188 }
189
ConvertToHalTemperature(const Temperature & hidl_temperature,google_camera_hal::Temperature * hal_temperature)190 status_t HidlThermalChangedCallback::ConvertToHalTemperature(
191 const Temperature& hidl_temperature,
192 google_camera_hal::Temperature* hal_temperature) {
193 if (hal_temperature == nullptr) {
194 ALOGE("%s: hal_temperature is nullptr", __FUNCTION__);
195 return BAD_VALUE;
196 }
197
198 status_t res = ConvertToHalTemperatureType(hidl_temperature.type,
199 &hal_temperature->type);
200 if (res != OK) {
201 ALOGE("%s: Converting to hal temperature type failed: %s(%d)", __FUNCTION__,
202 strerror(-res), res);
203 return res;
204 }
205
206 hal_temperature->name = hidl_temperature.name;
207 hal_temperature->value = hidl_temperature.value;
208
209 res = ConvertToHalThrottlingSeverity(hidl_temperature.throttlingStatus,
210 &hal_temperature->throttling_status);
211 if (res != OK) {
212 ALOGE("%s: Converting to hal throttling severity type failed: %s(%d)",
213 __FUNCTION__, strerror(-res), res);
214 return res;
215 }
216
217 return OK;
218 }
219
notifyThrottling(const Temperature & temperature)220 Return<void> HidlThermalChangedCallback::notifyThrottling(
221 const Temperature& temperature) {
222 google_camera_hal::Temperature hal_temperature;
223 status_t res = ConvertToHalTemperature(temperature, &hal_temperature);
224 if (res != OK) {
225 ALOGE("%s: Converting to hal temperature failed: %s(%d)", __FUNCTION__,
226 strerror(-res), res);
227 return Void();
228 }
229
230 kNotifyThrottling(hal_temperature);
231 return Void();
232 }
233
234 } // namespace hidl_thermal_utils
235 } // namespace hardware
236 } // namespace android
237