blob: 22b7152f99cdf437bd650dd3fecf4598bd149173 [file] [log] [blame]
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -08001/*
Kevin Rocard96d2cd92018-11-14 16:22:07 -08002 * Copyright (C) 2018 The Android Open Source Project
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -08003 *
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 Rocard96d2cd92018-11-14 16:22:07 -080017#ifndef android_hardware_audio_Hidl_Utils_H_
18#define android_hardware_audio_Hidl_Utils_H_
19
20#include PATH(android/hardware/audio/common/FILE_VERSION/types.h)
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080021
22#include <memory>
Mikhail Naganova9ac8892021-01-15 19:05:04 +000023#include <string>
24#include <vector>
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080025
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080026#include <system/audio.h>
27
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080028namespace android {
Kevin Rocard6891d7e2017-12-14 18:39:39 -080029namespace hardware {
30namespace audio {
31namespace common {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080032namespace CPP_VERSION {
Mikhail Naganov543bf9c2018-12-11 16:36:53 -080033namespace implementation {
34
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080035using ::android::hardware::hidl_vec;
Mikhail Naganov543bf9c2018-12-11 16:36:53 -080036using namespace ::android::hardware::audio::common::CPP_VERSION;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080037
Mikhail Naganov1b444a52020-10-29 13:08:05 -070038struct HidlUtils {
Mikhail Naganov1b444a52020-10-29 13:08:05 -070039 static status_t audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
40 AudioConfig* config);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080041 static status_t audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig);
42#if MAJOR_VERSION >= 4
43 static status_t audioContentTypeFromHal(const audio_content_type_t halContentType,
44 AudioContentType* contentType);
45 static status_t audioContentTypeToHal(const AudioContentType& contentType,
46 audio_content_type_t* halContentType);
47#endif
Mikhail Naganov1b444a52020-10-29 13:08:05 -070048 static status_t audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
49 AudioGainConfig* config);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070050 static status_t audioGainConfigToHal(const AudioGainConfig& config,
51 struct audio_gain_config* halConfig);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080052 static status_t audioGainFromHal(const struct audio_gain& halGain, bool isInput,
53 AudioGain* gain);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070054 static status_t audioGainToHal(const AudioGain& gain, struct audio_gain* halGain);
55 static status_t audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage);
56 static status_t audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage);
Andy Hung7aab1012020-02-14 15:39:55 -080057 static status_t audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
58 AudioOffloadInfo* offload);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070059 static status_t audioOffloadInfoToHal(const AudioOffloadInfo& offload,
60 audio_offload_info_t* halOffload);
61 static status_t audioPortConfigFromHal(const struct audio_port_config& halConfig,
62 AudioPortConfig* config);
63 static status_t audioPortConfigToHal(const AudioPortConfig& config,
64 struct audio_port_config* halConfig);
65 static status_t audioPortConfigsFromHal(unsigned int numHalConfigs,
66 const struct audio_port_config* halConfigs,
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080067 hidl_vec<AudioPortConfig>* configs);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070068 static status_t audioPortConfigsToHal(const hidl_vec<AudioPortConfig>& configs,
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080069 std::unique_ptr<audio_port_config[]>* halConfigs);
70 static status_t audioPortFromHal(const struct audio_port& halPort, AudioPort* port);
71 static status_t audioPortToHal(const AudioPort& port, struct audio_port* halPort);
72 static status_t audioSourceFromHal(audio_source_t halSource, AudioSource* source);
73 static status_t audioSourceToHal(const AudioSource& source, audio_source_t* halSource);
74#if MAJOR_VERSION >= 5
75 static status_t deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
76 char* halDeviceAddress);
77 static status_t deviceAddressFromHal(audio_devices_t halDeviceType,
78 const char* halDeviceAddress, DeviceAddress* device);
79#endif
Mikhail Naganov1b444a52020-10-29 13:08:05 -070080
Mikhail Naganovbae23362020-12-14 23:18:30 +000081#if MAJOR_VERSION >= 7
Eric Laurentdaedb0d2020-11-20 18:42:21 +010082 static constexpr char sAudioTagSeparator = ';';
83
Mikhail Naganov1b444a52020-10-29 13:08:05 -070084 static status_t audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
85 AudioChannelMask* channelMask);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080086 static status_t audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
87 hidl_vec<AudioChannelMask>* channelMasks);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070088 static status_t audioChannelMaskToHal(const AudioChannelMask& channelMask,
89 audio_channel_mask_t* halChannelMask);
90 static status_t audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput,
91 AudioConfigBase* configBase);
92 static status_t audioConfigBaseToHal(const AudioConfigBase& configBase,
93 audio_config_base_t* halConfigBase);
Mikhail Naganovff611982021-01-27 02:16:53 +000094 static status_t audioConfigBaseOptionalFromHal(const audio_config_base_t& halConfigBase,
95 bool isInput, bool formatSpecified,
96 bool sampleRateSpecified,
97 bool channelMaskSpecified,
98 AudioConfigBaseOptional* configBase);
99 static status_t audioConfigBaseOptionalToHal(const AudioConfigBaseOptional& configBase,
100 audio_config_base_t* halConfigBase,
101 bool* formatSpecified, bool* sampleRateSpecified,
102 bool* channelMaskSpecified);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700103 static status_t audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device);
104 static status_t audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice);
105 static status_t audioFormatFromHal(audio_format_t halFormat, AudioFormat* format);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800106 static status_t audioFormatsFromHal(const std::vector<std::string>& halFormats,
107 hidl_vec<AudioFormat>* formats);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700108 static status_t audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat);
109 static status_t audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask,
110 hidl_vec<AudioGainMode>* gainModeMask);
111 static status_t audioGainModeMaskToHal(const hidl_vec<AudioGainMode>& gainModeMask,
112 audio_gain_mode_t* halGainModeMask);
113 static status_t audioPortFromHal(const struct audio_port_v7& halPort, AudioPort* port);
114 static status_t audioPortToHal(const AudioPort& port, struct audio_port_v7* halPort);
115 static status_t audioProfileFromHal(const struct audio_profile& halProfile, bool isInput,
116 AudioProfile* profile);
117 static status_t audioProfileToHal(const AudioProfile& profile,
118 struct audio_profile* halProfile);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700119 static status_t audioStreamTypeFromHal(audio_stream_type_t halStreamType,
120 AudioStreamType* streamType);
121 static status_t audioStreamTypeToHal(const AudioStreamType& streamType,
122 audio_stream_type_t* halStreamType);
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000123 static status_t audioTagsFromHal(const std::vector<std::string>& strTags,
124 hidl_vec<AudioTag>* tags);
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800125 static status_t audioTagsToHal(const hidl_vec<AudioTag>& tags, char* halTags);
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000126 static hidl_vec<AudioTag> filterOutNonVendorTags(const hidl_vec<AudioTag>& tags);
127 static std::vector<std::string> filterOutNonVendorTags(const std::vector<std::string>& tags);
128 static std::vector<std::string> splitAudioTags(const char* halTags);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700129
130 private:
131 static status_t audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask,
132 AudioChannelMask* channelMask);
133 static status_t audioInputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
134 AudioChannelMask* channelMask);
135 static status_t audioOutputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
136 AudioChannelMask* channelMask);
137 static status_t audioPortExtendedInfoFromHal(
138 audio_port_role_t role, audio_port_type_t type,
139 const struct audio_port_config_device_ext& device,
140 const struct audio_port_config_mix_ext& mix,
141 const struct audio_port_config_session_ext& session, AudioPortExtendedInfo* ext,
142 bool* isInput);
143 static status_t audioPortExtendedInfoToHal(const AudioPortExtendedInfo& ext,
144 audio_port_role_t* role, audio_port_type_t* type,
145 struct audio_port_config_device_ext* device,
146 struct audio_port_config_mix_ext* mix,
147 struct audio_port_config_session_ext* session);
Eric Laurentdaedb0d2020-11-20 18:42:21 +0100148
Mikhail Naganovbae23362020-12-14 23:18:30 +0000149#endif // MAJOR_VERSION >= 7
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800150
151 // V4 and below have DeviceAddress defined in the 'core' interface.
152 // To avoid duplicating code, the implementations of deviceAddressTo/FromHal
153 // are defined as templates. These templates can be only used directly by V4
154 // and below.
155#if MAJOR_VERSION >= 5
156 private:
157#endif
158 template <typename DA>
159 static status_t deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType,
160 char* halDeviceAddress);
161 template <typename DA>
162 static status_t deviceAddressFromHalImpl(audio_devices_t halDeviceType,
163 const char* halDeviceAddress, DA* device);
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800164};
165
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800166#if MAJOR_VERSION <= 6
167#if MAJOR_VERSION >= 4
168inline status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
169 AudioContentType* contentType) {
170 *contentType = AudioContentType(halContentType);
171 return NO_ERROR;
172}
173
174inline status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
175 audio_content_type_t* halContentType) {
176 *halContentType = static_cast<audio_content_type_t>(contentType);
177 return NO_ERROR;
178}
179#endif
180
181inline status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
182 *source = AudioSource(halSource);
183 return NO_ERROR;
184}
185
186inline status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) {
187 *halSource = static_cast<audio_source_t>(source);
188 return NO_ERROR;
189}
190
191template <typename DA>
192status_t HidlUtils::deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType,
193 char* halDeviceAddress) {
194 *halDeviceType = static_cast<audio_devices_t>(device.device);
195 memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
196 if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) {
197 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%02X:%02X:%02X:%02X:%02X:%02X",
198 device.address.mac[0], device.address.mac[1], device.address.mac[2],
199 device.address.mac[3], device.address.mac[4], device.address.mac[5]);
200 } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) {
201 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d",
202 device.address.ipv4[0], device.address.ipv4[1], device.address.ipv4[2],
203 device.address.ipv4[3]);
204 } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) {
205 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d",
206 device.address.alsa.card, device.address.alsa.device);
207 } else if (*halDeviceType == AUDIO_DEVICE_OUT_BUS || *halDeviceType == AUDIO_DEVICE_IN_BUS) {
208 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s", device.busAddress.c_str());
209 } else if (*halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
210 *halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
211 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s",
212 device.rSubmixAddress.c_str());
213 }
214 return NO_ERROR;
215}
216
217template <typename DA>
218status_t HidlUtils::deviceAddressFromHalImpl(audio_devices_t halDeviceType,
219 const char* halDeviceAddress, DA* device) {
220 if (device == nullptr) {
221 return BAD_VALUE;
222 }
223 device->device = AudioDevice(halDeviceType);
224 if (halDeviceAddress == nullptr ||
225 strnlen(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
226 return NO_ERROR;
227 }
228
229 if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
230 int status =
231 sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &device->address.mac[0],
232 &device->address.mac[1], &device->address.mac[2], &device->address.mac[3],
233 &device->address.mac[4], &device->address.mac[5]);
234 return status == 6 ? OK : BAD_VALUE;
235 } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
236 int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &device->address.ipv4[0],
237 &device->address.ipv4[1], &device->address.ipv4[2],
238 &device->address.ipv4[3]);
239 return status == 4 ? OK : BAD_VALUE;
240 } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
241 int status = sscanf(halDeviceAddress, "card=%d;device=%d", &device->address.alsa.card,
242 &device->address.alsa.device);
243 return status == 2 ? OK : BAD_VALUE;
244 } else if (halDeviceType == AUDIO_DEVICE_OUT_BUS || halDeviceType == AUDIO_DEVICE_IN_BUS) {
245 device->busAddress = halDeviceAddress;
246 return OK;
247 } else if (halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
248 halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
249 device->rSubmixAddress = halDeviceAddress;
250 return OK;
251 }
252 device->busAddress = halDeviceAddress;
253 return NO_ERROR;
254}
255#endif // MAJOR_VERSION <= 6
256
Mikhail Naganov543bf9c2018-12-11 16:36:53 -0800257} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800258} // namespace CPP_VERSION
Kevin Rocard6891d7e2017-12-14 18:39:39 -0800259} // namespace common
260} // namespace audio
261} // namespace hardware
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800262} // namespace android
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800263
264#endif // android_hardware_audio_Hidl_Utils_H_