summaryrefslogtreecommitdiff
path: root/common/hal/aidl_service/hidl_thermal_utils.cc
blob: cb2c9155d8ce992eab3effa0f890f9a985bfe6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "GCH_HidlThermalUtils"
//#define LOG_NDEBUG 0
#include "hidl_thermal_utils.h"

#include <log/log.h>

namespace android {
namespace hardware {
namespace hidl_thermal_utils {

std::unique_ptr<HidlThermalChangedCallback> HidlThermalChangedCallback::Create(
    google_camera_hal::NotifyThrottlingFunc notify_throttling) {
  auto thermal_changed_callback = std::unique_ptr<HidlThermalChangedCallback>(
      new HidlThermalChangedCallback(notify_throttling));
  if (thermal_changed_callback == nullptr) {
    ALOGE("%s: Failed to create a thermal changed callback", __FUNCTION__);
    return nullptr;
  }

  return thermal_changed_callback;
}

HidlThermalChangedCallback::HidlThermalChangedCallback(
    google_camera_hal::NotifyThrottlingFunc notify_throttling)
    : kNotifyThrottling(notify_throttling) {
}

status_t ConvertToHidlTemperatureType(
    const google_camera_hal::TemperatureType& hal_temperature_type,
    TemperatureType* hidl_temperature_type) {
  if (hidl_temperature_type == nullptr) {
    ALOGE("%s: hidl_temperature_type is nullptr", __FUNCTION__);
    return BAD_VALUE;
  }

  switch (hal_temperature_type) {
    case google_camera_hal::TemperatureType::kUnknown:
      *hidl_temperature_type = TemperatureType::UNKNOWN;
      break;
    case google_camera_hal::TemperatureType::kCpu:
      *hidl_temperature_type = TemperatureType::CPU;
      break;
    case google_camera_hal::TemperatureType::kGpu:
      *hidl_temperature_type = TemperatureType::GPU;
      break;
    case google_camera_hal::TemperatureType::kBattery:
      *hidl_temperature_type = TemperatureType::BATTERY;
      break;
    case google_camera_hal::TemperatureType::kSkin:
      *hidl_temperature_type = TemperatureType::SKIN;
      break;
    case google_camera_hal::TemperatureType::kUsbPort:
      *hidl_temperature_type = TemperatureType::USB_PORT;
      break;
    case google_camera_hal::TemperatureType::kPowerAmplifier:
      *hidl_temperature_type = TemperatureType::POWER_AMPLIFIER;
      break;
    case google_camera_hal::TemperatureType::kBclVoltage:
      *hidl_temperature_type = TemperatureType::BCL_VOLTAGE;
      break;
    case google_camera_hal::TemperatureType::kBclCurrent:
      *hidl_temperature_type = TemperatureType::BCL_CURRENT;
      break;
    case google_camera_hal::TemperatureType::kBclPercentage:
      *hidl_temperature_type = TemperatureType::BCL_PERCENTAGE;
      break;
    case google_camera_hal::TemperatureType::kNpu:
      *hidl_temperature_type = TemperatureType::NPU;
      break;
    default:
      ALOGE("%s: Unknown temperature type: %d", __FUNCTION__,
            hal_temperature_type);
      return BAD_VALUE;
  }

  return OK;
}

status_t HidlThermalChangedCallback::ConvertToHalTemperatureType(
    const TemperatureType& hidl_temperature_type,
    google_camera_hal::TemperatureType* hal_temperature_type) {
  if (hal_temperature_type == nullptr) {
    ALOGE("%s: hal_temperature_type is nullptr", __FUNCTION__);
    return BAD_VALUE;
  }

  switch (hidl_temperature_type) {
    case TemperatureType::UNKNOWN:
      *hal_temperature_type = google_camera_hal::TemperatureType::kUnknown;
      break;
    case TemperatureType::CPU:
      *hal_temperature_type = google_camera_hal::TemperatureType::kCpu;
      break;
    case TemperatureType::GPU:
      *hal_temperature_type = google_camera_hal::TemperatureType::kGpu;
      break;
    case TemperatureType::BATTERY:
      *hal_temperature_type = google_camera_hal::TemperatureType::kBattery;
      break;
    case TemperatureType::SKIN:
      *hal_temperature_type = google_camera_hal::TemperatureType::kSkin;
      break;
    case TemperatureType::USB_PORT:
      *hal_temperature_type = google_camera_hal::TemperatureType::kUsbPort;
      break;
    case TemperatureType::POWER_AMPLIFIER:
      *hal_temperature_type =
          google_camera_hal::TemperatureType::kPowerAmplifier;
      break;
    case TemperatureType::BCL_VOLTAGE:
      *hal_temperature_type = google_camera_hal::TemperatureType::kBclVoltage;
      break;
    case TemperatureType::BCL_CURRENT:
      *hal_temperature_type = google_camera_hal::TemperatureType::kBclCurrent;
      break;
    case TemperatureType::BCL_PERCENTAGE:
      *hal_temperature_type = google_camera_hal::TemperatureType::kBclPercentage;
      break;
    case TemperatureType::NPU:
      *hal_temperature_type = google_camera_hal::TemperatureType::kNpu;
      break;
    default:
      ALOGE("%s: Unknown temperature type: %d", __FUNCTION__,
            hidl_temperature_type);
      return BAD_VALUE;
  }

  return OK;
}

status_t HidlThermalChangedCallback::ConvertToHalThrottlingSeverity(
    const ThrottlingSeverity& hidl_throttling_severity,
    google_camera_hal::ThrottlingSeverity* hal_throttling_severity) {
  if (hal_throttling_severity == nullptr) {
    ALOGE("%s: hal_throttling_severity is nullptr", __FUNCTION__);
    return BAD_VALUE;
  }

  switch (hidl_throttling_severity) {
    case ThrottlingSeverity::NONE:
      *hal_throttling_severity = google_camera_hal::ThrottlingSeverity::kNone;
      break;
    case ThrottlingSeverity::LIGHT:
      *hal_throttling_severity = google_camera_hal::ThrottlingSeverity::kLight;
      break;
    case ThrottlingSeverity::MODERATE:
      *hal_throttling_severity =
          google_camera_hal::ThrottlingSeverity::kModerate;
      break;
    case ThrottlingSeverity::SEVERE:
      *hal_throttling_severity = google_camera_hal::ThrottlingSeverity::kSevere;
      break;
    case ThrottlingSeverity::CRITICAL:
      *hal_throttling_severity =
          google_camera_hal::ThrottlingSeverity::kCritical;
      break;
    case ThrottlingSeverity::EMERGENCY:
      *hal_throttling_severity =
          google_camera_hal::ThrottlingSeverity::kEmergency;
      break;
    case ThrottlingSeverity::SHUTDOWN:
      *hal_throttling_severity =
          google_camera_hal::ThrottlingSeverity::kShutdown;
      break;
    default:
      ALOGE("%s: Unknown temperature severity: %d", __FUNCTION__,
            hidl_throttling_severity);
      return BAD_VALUE;
  }

  return OK;
}

status_t HidlThermalChangedCallback::ConvertToHalTemperature(
    const Temperature& hidl_temperature,
    google_camera_hal::Temperature* hal_temperature) {
  if (hal_temperature == nullptr) {
    ALOGE("%s: hal_temperature is nullptr", __FUNCTION__);
    return BAD_VALUE;
  }

  status_t res = ConvertToHalTemperatureType(hidl_temperature.type,
                                             &hal_temperature->type);
  if (res != OK) {
    ALOGE("%s: Converting to hal temperature type failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  hal_temperature->name = hidl_temperature.name;
  hal_temperature->value = hidl_temperature.value;

  res = ConvertToHalThrottlingSeverity(hidl_temperature.throttlingStatus,
                                       &hal_temperature->throttling_status);
  if (res != OK) {
    ALOGE("%s: Converting to hal throttling severity type failed: %s(%d)",
          __FUNCTION__, strerror(-res), res);
    return res;
  }

  return OK;
}

Return<void> HidlThermalChangedCallback::notifyThrottling(
    const Temperature& temperature) {
  google_camera_hal::Temperature hal_temperature;
  status_t res = ConvertToHalTemperature(temperature, &hal_temperature);
  if (res != OK) {
    ALOGE("%s: Converting to hal temperature failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return Void();
  }

  kNotifyThrottling(hal_temperature);
  return Void();
}

}  // namespace hidl_thermal_utils
}  // namespace hardware
}  // namespace android