1 /*
2 * Copyright (C) 2018 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 #ifndef android_hardware_audio_Hidl_Utils_H_
18 #define android_hardware_audio_Hidl_Utils_H_
19
20 // clang-format off
21 #include PATH(android/hardware/audio/common/COMMON_TYPES_FILE_VERSION/types.h)
22 // clang-format on
23
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include <system/audio.h>
29
30 namespace android {
31 namespace hardware {
32 namespace audio {
33 namespace common {
34 namespace COMMON_TYPES_CPP_VERSION {
35 namespace implementation {
36
37 using ::android::hardware::hidl_vec;
38 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
39
40 struct HidlUtils {
41 static status_t audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
42 AudioConfig* config);
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
50 static status_t audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
51 AudioGainConfig* config);
52 static status_t audioGainConfigToHal(const AudioGainConfig& config,
53 struct audio_gain_config* halConfig);
54 static status_t audioGainFromHal(const struct audio_gain& halGain, bool isInput,
55 AudioGain* gain);
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);
59 static status_t audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
60 AudioOffloadInfo* offload);
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,
69 hidl_vec<AudioPortConfig>* configs);
70 static status_t audioPortConfigsToHal(const hidl_vec<AudioPortConfig>& configs,
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
82
83 #if MAJOR_VERSION >= 7
84 static constexpr char sAudioTagSeparator = AUDIO_ATTRIBUTES_TAGS_SEPARATOR;
85
86 static status_t audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
87 AudioChannelMask* channelMask);
88 static status_t audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
89 hidl_vec<AudioChannelMask>* channelMasks);
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);
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);
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);
108 static status_t audioFormatsFromHal(const std::vector<std::string>& halFormats,
109 hidl_vec<AudioFormat>* formats);
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);
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);
125 static status_t audioTagsFromHal(const std::vector<std::string>& strTags,
126 hidl_vec<AudioTag>* tags);
127 static status_t audioTagsToHal(const hidl_vec<AudioTag>& tags, char* halTags);
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);
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);
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);
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);
158
159 #endif // MAJOR_VERSION >= 7
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);
174 };
175
176 #if MAJOR_VERSION <= 6
177 #if MAJOR_VERSION >= 4
audioContentTypeFromHal(const audio_content_type_t halContentType,AudioContentType * contentType)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
audioContentTypeToHal(const AudioContentType & contentType,audio_content_type_t * halContentType)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
audioSourceFromHal(audio_source_t halSource,AudioSource * source)191 inline status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
192 *source = AudioSource(halSource);
193 return NO_ERROR;
194 }
195
audioSourceToHal(const AudioSource & source,audio_source_t * halSource)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>
deviceAddressToHalImpl(const DA & device,audio_devices_t * halDeviceType,char * halDeviceAddress)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());
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());
226 }
227 return NO_ERROR;
228 }
229
230 template <typename DA>
deviceAddressFromHalImpl(audio_devices_t halDeviceType,const char * halDeviceAddress,DA * device)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 }
265 // Fall back to bus address for other device types, e.g. for microphones.
266 device->busAddress = halDeviceAddress;
267 return NO_ERROR;
268 }
269 #endif // MAJOR_VERSION <= 6
270
271 } // namespace implementation
272 } // namespace COMMON_TYPES_CPP_VERSION
273 } // namespace common
274 } // namespace audio
275 } // namespace hardware
276 } // namespace android
277
278 #endif // android_hardware_audio_Hidl_Utils_H_
279