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