Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 1 | /* |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 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 | |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 17 | #ifndef android_hardware_audio_Hidl_Utils_H_ |
| 18 | #define android_hardware_audio_Hidl_Utils_H_ |
| 19 | |
Mikhail Naganov | 8140f56 | 2022-01-15 01:15:12 +0000 | [diff] [blame] | 20 | // clang-format off |
| 21 | #include PATH(android/hardware/audio/common/COMMON_TYPES_FILE_VERSION/types.h) |
| 22 | // clang-format on |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 23 | |
| 24 | #include <memory> |
Mikhail Naganov | a9ac889 | 2021-01-15 19:05:04 +0000 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 27 | |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 28 | #include <system/audio.h> |
| 29 | |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 30 | namespace android { |
Kevin Rocard | 6891d7e | 2017-12-14 18:39:39 -0800 | [diff] [blame] | 31 | namespace hardware { |
| 32 | namespace audio { |
| 33 | namespace common { |
Mikhail Naganov | 8140f56 | 2022-01-15 01:15:12 +0000 | [diff] [blame] | 34 | namespace COMMON_TYPES_CPP_VERSION { |
Mikhail Naganov | 543bf9c | 2018-12-11 16:36:53 -0800 | [diff] [blame] | 35 | namespace implementation { |
| 36 | |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 37 | using ::android::hardware::hidl_vec; |
Mikhail Naganov | 8140f56 | 2022-01-15 01:15:12 +0000 | [diff] [blame] | 38 | using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 39 | |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 40 | struct HidlUtils { |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 41 | static status_t audioConfigFromHal(const audio_config_t& halConfig, bool isInput, |
| 42 | AudioConfig* config); |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 43 | static status_t audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig); |
| 44 | #if MAJOR_VERSION >= 4 |
| 45 | static status_t audioContentTypeFromHal(const audio_content_type_t halContentType, |
| 46 | AudioContentType* contentType); |
| 47 | static status_t audioContentTypeToHal(const AudioContentType& contentType, |
| 48 | audio_content_type_t* halContentType); |
| 49 | #endif |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 50 | static status_t audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput, |
| 51 | AudioGainConfig* config); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 52 | static status_t audioGainConfigToHal(const AudioGainConfig& config, |
| 53 | struct audio_gain_config* halConfig); |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 54 | static status_t audioGainFromHal(const struct audio_gain& halGain, bool isInput, |
| 55 | AudioGain* gain); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 56 | static status_t audioGainToHal(const AudioGain& gain, struct audio_gain* halGain); |
| 57 | static status_t audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage); |
| 58 | static status_t audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage); |
Andy Hung | 7aab101 | 2020-02-14 15:39:55 -0800 | [diff] [blame] | 59 | static status_t audioOffloadInfoFromHal(const audio_offload_info_t& halOffload, |
| 60 | AudioOffloadInfo* offload); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 61 | static status_t audioOffloadInfoToHal(const AudioOffloadInfo& offload, |
| 62 | audio_offload_info_t* halOffload); |
| 63 | static status_t audioPortConfigFromHal(const struct audio_port_config& halConfig, |
| 64 | AudioPortConfig* config); |
| 65 | static status_t audioPortConfigToHal(const AudioPortConfig& config, |
| 66 | struct audio_port_config* halConfig); |
| 67 | static status_t audioPortConfigsFromHal(unsigned int numHalConfigs, |
| 68 | const struct audio_port_config* halConfigs, |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 69 | hidl_vec<AudioPortConfig>* configs); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 70 | static status_t audioPortConfigsToHal(const hidl_vec<AudioPortConfig>& configs, |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 71 | std::unique_ptr<audio_port_config[]>* halConfigs); |
| 72 | static status_t audioPortFromHal(const struct audio_port& halPort, AudioPort* port); |
| 73 | static status_t audioPortToHal(const AudioPort& port, struct audio_port* halPort); |
| 74 | static status_t audioSourceFromHal(audio_source_t halSource, AudioSource* source); |
| 75 | static status_t audioSourceToHal(const AudioSource& source, audio_source_t* halSource); |
| 76 | #if MAJOR_VERSION >= 5 |
| 77 | static status_t deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType, |
| 78 | char* halDeviceAddress); |
| 79 | static status_t deviceAddressFromHal(audio_devices_t halDeviceType, |
| 80 | const char* halDeviceAddress, DeviceAddress* device); |
| 81 | #endif |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 82 | |
Mikhail Naganov | bae2336 | 2020-12-14 23:18:30 +0000 | [diff] [blame] | 83 | #if MAJOR_VERSION >= 7 |
Eric Laurent | daedb0d | 2020-11-20 18:42:21 +0100 | [diff] [blame] | 84 | static constexpr char sAudioTagSeparator = ';'; |
| 85 | |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 86 | static status_t audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput, |
| 87 | AudioChannelMask* channelMask); |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 88 | static status_t audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks, |
| 89 | hidl_vec<AudioChannelMask>* channelMasks); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 90 | static status_t audioChannelMaskToHal(const AudioChannelMask& channelMask, |
| 91 | audio_channel_mask_t* halChannelMask); |
| 92 | static status_t audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput, |
| 93 | AudioConfigBase* configBase); |
| 94 | static status_t audioConfigBaseToHal(const AudioConfigBase& configBase, |
| 95 | audio_config_base_t* halConfigBase); |
Mikhail Naganov | ff61198 | 2021-01-27 02:16:53 +0000 | [diff] [blame] | 96 | static status_t audioConfigBaseOptionalFromHal(const audio_config_base_t& halConfigBase, |
| 97 | bool isInput, bool formatSpecified, |
| 98 | bool sampleRateSpecified, |
| 99 | bool channelMaskSpecified, |
| 100 | AudioConfigBaseOptional* configBase); |
| 101 | static status_t audioConfigBaseOptionalToHal(const AudioConfigBaseOptional& configBase, |
| 102 | audio_config_base_t* halConfigBase, |
| 103 | bool* formatSpecified, bool* sampleRateSpecified, |
| 104 | bool* channelMaskSpecified); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 105 | static status_t audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device); |
| 106 | static status_t audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice); |
| 107 | static status_t audioFormatFromHal(audio_format_t halFormat, AudioFormat* format); |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 108 | static status_t audioFormatsFromHal(const std::vector<std::string>& halFormats, |
| 109 | hidl_vec<AudioFormat>* formats); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 110 | static status_t audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat); |
| 111 | static status_t audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask, |
| 112 | hidl_vec<AudioGainMode>* gainModeMask); |
| 113 | static status_t audioGainModeMaskToHal(const hidl_vec<AudioGainMode>& gainModeMask, |
| 114 | audio_gain_mode_t* halGainModeMask); |
| 115 | static status_t audioPortFromHal(const struct audio_port_v7& halPort, AudioPort* port); |
| 116 | static status_t audioPortToHal(const AudioPort& port, struct audio_port_v7* halPort); |
| 117 | static status_t audioProfileFromHal(const struct audio_profile& halProfile, bool isInput, |
| 118 | AudioProfile* profile); |
| 119 | static status_t audioProfileToHal(const AudioProfile& profile, |
| 120 | struct audio_profile* halProfile); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 121 | static status_t audioStreamTypeFromHal(audio_stream_type_t halStreamType, |
| 122 | AudioStreamType* streamType); |
| 123 | static status_t audioStreamTypeToHal(const AudioStreamType& streamType, |
| 124 | audio_stream_type_t* halStreamType); |
Mikhail Naganov | a9ac889 | 2021-01-15 19:05:04 +0000 | [diff] [blame] | 125 | static status_t audioTagsFromHal(const std::vector<std::string>& strTags, |
| 126 | hidl_vec<AudioTag>* tags); |
Mikhail Naganov | 3f1457b | 2020-12-17 15:01:54 -0800 | [diff] [blame] | 127 | static status_t audioTagsToHal(const hidl_vec<AudioTag>& tags, char* halTags); |
Mikhail Naganov | a9ac889 | 2021-01-15 19:05:04 +0000 | [diff] [blame] | 128 | static hidl_vec<AudioTag> filterOutNonVendorTags(const hidl_vec<AudioTag>& tags); |
| 129 | static std::vector<std::string> filterOutNonVendorTags(const std::vector<std::string>& tags); |
| 130 | static std::vector<std::string> splitAudioTags(const char* halTags); |
jiabin | 574a86f | 2021-03-05 06:42:05 +0000 | [diff] [blame] | 131 | static status_t audioTransportsFromHal(const struct audio_port_v7& halPort, bool isInput, |
| 132 | hidl_vec<AudioTransport>* transports); |
| 133 | static status_t audioTransportsToHal(const hidl_vec<AudioTransport>& transports, |
| 134 | struct audio_port_v7* halTransport); |
Mikhail Naganov | 1b444a5 | 2020-10-29 13:08:05 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | static status_t audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask, |
| 138 | AudioChannelMask* channelMask); |
| 139 | static status_t audioInputChannelMaskFromHal(audio_channel_mask_t halChannelMask, |
| 140 | AudioChannelMask* channelMask); |
| 141 | static status_t audioOutputChannelMaskFromHal(audio_channel_mask_t halChannelMask, |
| 142 | AudioChannelMask* channelMask); |
| 143 | static status_t audioPortExtendedInfoFromHal( |
| 144 | audio_port_role_t role, audio_port_type_t type, |
| 145 | const struct audio_port_config_device_ext& device, |
| 146 | const struct audio_port_config_mix_ext& mix, |
| 147 | const struct audio_port_config_session_ext& session, AudioPortExtendedInfo* ext, |
| 148 | bool* isInput); |
| 149 | static status_t audioPortExtendedInfoToHal(const AudioPortExtendedInfo& ext, |
| 150 | audio_port_role_t* role, audio_port_type_t* type, |
| 151 | struct audio_port_config_device_ext* device, |
| 152 | struct audio_port_config_mix_ext* mix, |
| 153 | struct audio_port_config_session_ext* session); |
jiabin | 574a86f | 2021-03-05 06:42:05 +0000 | [diff] [blame] | 154 | static status_t encapsulationTypeFromHal(audio_encapsulation_type_t halEncapsulationType, |
| 155 | AudioEncapsulationType* encapsulationType); |
| 156 | static status_t encapsulationTypeToHal(const AudioEncapsulationType& encapsulationType, |
| 157 | audio_encapsulation_type_t* halEncapsulationType); |
Eric Laurent | daedb0d | 2020-11-20 18:42:21 +0100 | [diff] [blame] | 158 | |
Mikhail Naganov | bae2336 | 2020-12-14 23:18:30 +0000 | [diff] [blame] | 159 | #endif // MAJOR_VERSION >= 7 |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 160 | |
| 161 | // V4 and below have DeviceAddress defined in the 'core' interface. |
| 162 | // To avoid duplicating code, the implementations of deviceAddressTo/FromHal |
| 163 | // are defined as templates. These templates can be only used directly by V4 |
| 164 | // and below. |
| 165 | #if MAJOR_VERSION >= 5 |
| 166 | private: |
| 167 | #endif |
| 168 | template <typename DA> |
| 169 | static status_t deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType, |
| 170 | char* halDeviceAddress); |
| 171 | template <typename DA> |
| 172 | static status_t deviceAddressFromHalImpl(audio_devices_t halDeviceType, |
| 173 | const char* halDeviceAddress, DA* device); |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 176 | #if MAJOR_VERSION <= 6 |
| 177 | #if MAJOR_VERSION >= 4 |
| 178 | inline status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType, |
| 179 | AudioContentType* contentType) { |
| 180 | *contentType = AudioContentType(halContentType); |
| 181 | return NO_ERROR; |
| 182 | } |
| 183 | |
| 184 | inline status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType, |
| 185 | audio_content_type_t* halContentType) { |
| 186 | *halContentType = static_cast<audio_content_type_t>(contentType); |
| 187 | return NO_ERROR; |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | inline status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) { |
| 192 | *source = AudioSource(halSource); |
| 193 | return NO_ERROR; |
| 194 | } |
| 195 | |
| 196 | inline status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) { |
| 197 | *halSource = static_cast<audio_source_t>(source); |
| 198 | return NO_ERROR; |
| 199 | } |
| 200 | |
| 201 | template <typename DA> |
| 202 | status_t HidlUtils::deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType, |
| 203 | char* halDeviceAddress) { |
| 204 | *halDeviceType = static_cast<audio_devices_t>(device.device); |
| 205 | memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN); |
| 206 | if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) { |
| 207 | snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 208 | device.address.mac[0], device.address.mac[1], device.address.mac[2], |
| 209 | device.address.mac[3], device.address.mac[4], device.address.mac[5]); |
| 210 | } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) { |
| 211 | snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d", |
| 212 | device.address.ipv4[0], device.address.ipv4[1], device.address.ipv4[2], |
| 213 | device.address.ipv4[3]); |
| 214 | } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) { |
| 215 | snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d", |
| 216 | device.address.alsa.card, device.address.alsa.device); |
| 217 | } else if (*halDeviceType == AUDIO_DEVICE_OUT_BUS || *halDeviceType == AUDIO_DEVICE_IN_BUS) { |
| 218 | snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s", device.busAddress.c_str()); |
| 219 | } else if (*halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX || |
| 220 | *halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 221 | snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s", |
| 222 | device.rSubmixAddress.c_str()); |
Mikhail Naganov | 32fe782 | 2021-02-12 15:44:21 -0800 | [diff] [blame] | 223 | } else { |
| 224 | // Fall back to bus address for other device types, e.g. for microphones. |
| 225 | snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s", device.busAddress.c_str()); |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 226 | } |
| 227 | return NO_ERROR; |
| 228 | } |
| 229 | |
| 230 | template <typename DA> |
| 231 | status_t HidlUtils::deviceAddressFromHalImpl(audio_devices_t halDeviceType, |
| 232 | const char* halDeviceAddress, DA* device) { |
| 233 | if (device == nullptr) { |
| 234 | return BAD_VALUE; |
| 235 | } |
| 236 | device->device = AudioDevice(halDeviceType); |
| 237 | if (halDeviceAddress == nullptr || |
| 238 | strnlen(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) { |
| 239 | return NO_ERROR; |
| 240 | } |
| 241 | |
| 242 | if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) { |
| 243 | int status = |
| 244 | sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &device->address.mac[0], |
| 245 | &device->address.mac[1], &device->address.mac[2], &device->address.mac[3], |
| 246 | &device->address.mac[4], &device->address.mac[5]); |
| 247 | return status == 6 ? OK : BAD_VALUE; |
| 248 | } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) { |
| 249 | int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &device->address.ipv4[0], |
| 250 | &device->address.ipv4[1], &device->address.ipv4[2], |
| 251 | &device->address.ipv4[3]); |
| 252 | return status == 4 ? OK : BAD_VALUE; |
| 253 | } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) { |
| 254 | int status = sscanf(halDeviceAddress, "card=%d;device=%d", &device->address.alsa.card, |
| 255 | &device->address.alsa.device); |
| 256 | return status == 2 ? OK : BAD_VALUE; |
| 257 | } else if (halDeviceType == AUDIO_DEVICE_OUT_BUS || halDeviceType == AUDIO_DEVICE_IN_BUS) { |
| 258 | device->busAddress = halDeviceAddress; |
| 259 | return OK; |
| 260 | } else if (halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX || |
| 261 | halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 262 | device->rSubmixAddress = halDeviceAddress; |
| 263 | return OK; |
| 264 | } |
Mikhail Naganov | 32fe782 | 2021-02-12 15:44:21 -0800 | [diff] [blame] | 265 | // Fall back to bus address for other device types, e.g. for microphones. |
Mikhail Naganov | b52e93f | 2020-12-10 16:10:08 -0800 | [diff] [blame] | 266 | device->busAddress = halDeviceAddress; |
| 267 | return NO_ERROR; |
| 268 | } |
| 269 | #endif // MAJOR_VERSION <= 6 |
| 270 | |
Mikhail Naganov | 543bf9c | 2018-12-11 16:36:53 -0800 | [diff] [blame] | 271 | } // namespace implementation |
Mikhail Naganov | 8140f56 | 2022-01-15 01:15:12 +0000 | [diff] [blame] | 272 | } // namespace COMMON_TYPES_CPP_VERSION |
Kevin Rocard | 6891d7e | 2017-12-14 18:39:39 -0800 | [diff] [blame] | 273 | } // namespace common |
| 274 | } // namespace audio |
| 275 | } // namespace hardware |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 276 | } // namespace android |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 277 | |
| 278 | #endif // android_hardware_audio_Hidl_Utils_H_ |