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