blob: a0bd1bc27e5deeb09768e810611fe69e15095f71 [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>
23
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080024#include <system/audio.h>
25
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080026namespace android {
Kevin Rocard6891d7e2017-12-14 18:39:39 -080027namespace hardware {
28namespace audio {
29namespace common {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080030namespace CPP_VERSION {
Mikhail Naganov543bf9c2018-12-11 16:36:53 -080031namespace implementation {
32
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080033using ::android::hardware::hidl_vec;
Mikhail Naganov543bf9c2018-12-11 16:36:53 -080034using namespace ::android::hardware::audio::common::CPP_VERSION;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080035
Mikhail Naganov1b444a52020-10-29 13:08:05 -070036struct HidlUtils {
Mikhail Naganov1b444a52020-10-29 13:08:05 -070037 static status_t audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
38 AudioConfig* config);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080039 static status_t audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig);
40#if MAJOR_VERSION >= 4
41 static status_t audioContentTypeFromHal(const audio_content_type_t halContentType,
42 AudioContentType* contentType);
43 static status_t audioContentTypeToHal(const AudioContentType& contentType,
44 audio_content_type_t* halContentType);
45#endif
Mikhail Naganov1b444a52020-10-29 13:08:05 -070046 static status_t audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
47 AudioGainConfig* config);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070048 static status_t audioGainConfigToHal(const AudioGainConfig& config,
49 struct audio_gain_config* halConfig);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080050 static status_t audioGainFromHal(const struct audio_gain& halGain, bool isInput,
51 AudioGain* gain);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070052 static status_t audioGainToHal(const AudioGain& gain, struct audio_gain* halGain);
53 static status_t audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage);
54 static status_t audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage);
Andy Hung7aab1012020-02-14 15:39:55 -080055 static status_t audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
56 AudioOffloadInfo* offload);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070057 static status_t audioOffloadInfoToHal(const AudioOffloadInfo& offload,
58 audio_offload_info_t* halOffload);
59 static status_t audioPortConfigFromHal(const struct audio_port_config& halConfig,
60 AudioPortConfig* config);
61 static status_t audioPortConfigToHal(const AudioPortConfig& config,
62 struct audio_port_config* halConfig);
63 static status_t audioPortConfigsFromHal(unsigned int numHalConfigs,
64 const struct audio_port_config* halConfigs,
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080065 hidl_vec<AudioPortConfig>* configs);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070066 static status_t audioPortConfigsToHal(const hidl_vec<AudioPortConfig>& configs,
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080067 std::unique_ptr<audio_port_config[]>* halConfigs);
68 static status_t audioPortFromHal(const struct audio_port& halPort, AudioPort* port);
69 static status_t audioPortToHal(const AudioPort& port, struct audio_port* halPort);
70 static status_t audioSourceFromHal(audio_source_t halSource, AudioSource* source);
71 static status_t audioSourceToHal(const AudioSource& source, audio_source_t* halSource);
72#if MAJOR_VERSION >= 5
73 static status_t deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
74 char* halDeviceAddress);
75 static status_t deviceAddressFromHal(audio_devices_t halDeviceType,
76 const char* halDeviceAddress, DeviceAddress* device);
77#endif
Mikhail Naganov1b444a52020-10-29 13:08:05 -070078
Mikhail Naganovbae23362020-12-14 23:18:30 +000079#if MAJOR_VERSION >= 7
Mikhail Naganov1b444a52020-10-29 13:08:05 -070080 static status_t audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
81 AudioChannelMask* channelMask);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080082 static status_t audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
83 hidl_vec<AudioChannelMask>* channelMasks);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070084 static status_t audioChannelMaskToHal(const AudioChannelMask& channelMask,
85 audio_channel_mask_t* halChannelMask);
86 static status_t audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput,
87 AudioConfigBase* configBase);
88 static status_t audioConfigBaseToHal(const AudioConfigBase& configBase,
89 audio_config_base_t* halConfigBase);
90 static status_t audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device);
91 static status_t audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice);
92 static status_t audioFormatFromHal(audio_format_t halFormat, AudioFormat* format);
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080093 static status_t audioFormatsFromHal(const std::vector<std::string>& halFormats,
94 hidl_vec<AudioFormat>* formats);
Mikhail Naganov1b444a52020-10-29 13:08:05 -070095 static status_t audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat);
96 static status_t audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask,
97 hidl_vec<AudioGainMode>* gainModeMask);
98 static status_t audioGainModeMaskToHal(const hidl_vec<AudioGainMode>& gainModeMask,
99 audio_gain_mode_t* halGainModeMask);
100 static status_t audioPortFromHal(const struct audio_port_v7& halPort, AudioPort* port);
101 static status_t audioPortToHal(const AudioPort& port, struct audio_port_v7* halPort);
102 static status_t audioProfileFromHal(const struct audio_profile& halProfile, bool isInput,
103 AudioProfile* profile);
104 static status_t audioProfileToHal(const AudioProfile& profile,
105 struct audio_profile* halProfile);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700106 static status_t audioStreamTypeFromHal(audio_stream_type_t halStreamType,
107 AudioStreamType* streamType);
108 static status_t audioStreamTypeToHal(const AudioStreamType& streamType,
109 audio_stream_type_t* halStreamType);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700110
111 private:
112 static status_t audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask,
113 AudioChannelMask* channelMask);
114 static status_t audioInputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
115 AudioChannelMask* channelMask);
116 static status_t audioOutputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
117 AudioChannelMask* channelMask);
118 static status_t audioPortExtendedInfoFromHal(
119 audio_port_role_t role, audio_port_type_t type,
120 const struct audio_port_config_device_ext& device,
121 const struct audio_port_config_mix_ext& mix,
122 const struct audio_port_config_session_ext& session, AudioPortExtendedInfo* ext,
123 bool* isInput);
124 static status_t audioPortExtendedInfoToHal(const AudioPortExtendedInfo& ext,
125 audio_port_role_t* role, audio_port_type_t* type,
126 struct audio_port_config_device_ext* device,
127 struct audio_port_config_mix_ext* mix,
128 struct audio_port_config_session_ext* session);
Mikhail Naganovbae23362020-12-14 23:18:30 +0000129#endif // MAJOR_VERSION >= 7
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800130
131 // V4 and below have DeviceAddress defined in the 'core' interface.
132 // To avoid duplicating code, the implementations of deviceAddressTo/FromHal
133 // are defined as templates. These templates can be only used directly by V4
134 // and below.
135#if MAJOR_VERSION >= 5
136 private:
137#endif
138 template <typename DA>
139 static status_t deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType,
140 char* halDeviceAddress);
141 template <typename DA>
142 static status_t deviceAddressFromHalImpl(audio_devices_t halDeviceType,
143 const char* halDeviceAddress, DA* device);
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800144};
145
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800146#if MAJOR_VERSION <= 6
147#if MAJOR_VERSION >= 4
148inline status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
149 AudioContentType* contentType) {
150 *contentType = AudioContentType(halContentType);
151 return NO_ERROR;
152}
153
154inline status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
155 audio_content_type_t* halContentType) {
156 *halContentType = static_cast<audio_content_type_t>(contentType);
157 return NO_ERROR;
158}
159#endif
160
161inline status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
162 *source = AudioSource(halSource);
163 return NO_ERROR;
164}
165
166inline status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) {
167 *halSource = static_cast<audio_source_t>(source);
168 return NO_ERROR;
169}
170
171template <typename DA>
172status_t HidlUtils::deviceAddressToHalImpl(const DA& device, audio_devices_t* halDeviceType,
173 char* halDeviceAddress) {
174 *halDeviceType = static_cast<audio_devices_t>(device.device);
175 memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
176 if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) {
177 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%02X:%02X:%02X:%02X:%02X:%02X",
178 device.address.mac[0], device.address.mac[1], device.address.mac[2],
179 device.address.mac[3], device.address.mac[4], device.address.mac[5]);
180 } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) {
181 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d",
182 device.address.ipv4[0], device.address.ipv4[1], device.address.ipv4[2],
183 device.address.ipv4[3]);
184 } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) {
185 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d",
186 device.address.alsa.card, device.address.alsa.device);
187 } else if (*halDeviceType == AUDIO_DEVICE_OUT_BUS || *halDeviceType == AUDIO_DEVICE_IN_BUS) {
188 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s", device.busAddress.c_str());
189 } else if (*halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
190 *halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
191 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s",
192 device.rSubmixAddress.c_str());
193 }
194 return NO_ERROR;
195}
196
197template <typename DA>
198status_t HidlUtils::deviceAddressFromHalImpl(audio_devices_t halDeviceType,
199 const char* halDeviceAddress, DA* device) {
200 if (device == nullptr) {
201 return BAD_VALUE;
202 }
203 device->device = AudioDevice(halDeviceType);
204 if (halDeviceAddress == nullptr ||
205 strnlen(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
206 return NO_ERROR;
207 }
208
209 if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
210 int status =
211 sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &device->address.mac[0],
212 &device->address.mac[1], &device->address.mac[2], &device->address.mac[3],
213 &device->address.mac[4], &device->address.mac[5]);
214 return status == 6 ? OK : BAD_VALUE;
215 } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
216 int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &device->address.ipv4[0],
217 &device->address.ipv4[1], &device->address.ipv4[2],
218 &device->address.ipv4[3]);
219 return status == 4 ? OK : BAD_VALUE;
220 } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
221 int status = sscanf(halDeviceAddress, "card=%d;device=%d", &device->address.alsa.card,
222 &device->address.alsa.device);
223 return status == 2 ? OK : BAD_VALUE;
224 } else if (halDeviceType == AUDIO_DEVICE_OUT_BUS || halDeviceType == AUDIO_DEVICE_IN_BUS) {
225 device->busAddress = halDeviceAddress;
226 return OK;
227 } else if (halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
228 halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
229 device->rSubmixAddress = halDeviceAddress;
230 return OK;
231 }
232 device->busAddress = halDeviceAddress;
233 return NO_ERROR;
234}
235#endif // MAJOR_VERSION <= 6
236
Mikhail Naganov543bf9c2018-12-11 16:36:53 -0800237} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800238} // namespace CPP_VERSION
Kevin Rocard6891d7e2017-12-14 18:39:39 -0800239} // namespace common
240} // namespace audio
241} // namespace hardware
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800242} // namespace android
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800243
244#endif // android_hardware_audio_Hidl_Utils_H_