1 /*
2 * Copyright (C) 2016 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 #include <common/all-versions/IncludeGuard.h>
18
19 #include <stdio.h>
20
21 #include <log/log.h>
22
23 namespace android {
24 namespace hardware {
25 namespace audio {
26 namespace AUDIO_HAL_VERSION {
27 namespace implementation {
28
29 using ::android::hardware::audio::common::AUDIO_HAL_VERSION::AudioDevice;
30
deviceAddressToHal(const DeviceAddress & address)31 std::string deviceAddressToHal(const DeviceAddress& address) {
32 // HAL assumes that the address is NUL-terminated.
33 char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
34 memset(halAddress, 0, sizeof(halAddress));
35 uint32_t halDevice = static_cast<uint32_t>(address.device);
36 const bool isInput = (halDevice & AUDIO_DEVICE_BIT_IN) != 0;
37 if (isInput) halDevice &= ~AUDIO_DEVICE_BIT_IN;
38 if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) ||
39 (isInput && (halDevice & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) {
40 snprintf(halAddress, sizeof(halAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
41 address.address.mac[0], address.address.mac[1], address.address.mac[2],
42 address.address.mac[3], address.address.mac[4], address.address.mac[5]);
43 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_IP) != 0) ||
44 (isInput && (halDevice & AUDIO_DEVICE_IN_IP) != 0)) {
45 snprintf(halAddress, sizeof(halAddress), "%d.%d.%d.%d", address.address.ipv4[0],
46 address.address.ipv4[1], address.address.ipv4[2], address.address.ipv4[3]);
47 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_USB) != 0) ||
48 (isInput && (halDevice & AUDIO_DEVICE_IN_ALL_USB) != 0)) {
49 snprintf(halAddress, sizeof(halAddress), "card=%d;device=%d", address.address.alsa.card,
50 address.address.alsa.device);
51 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_BUS) != 0) ||
52 (isInput && (halDevice & AUDIO_DEVICE_IN_BUS) != 0)) {
53 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
54 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 ||
55 (isInput && (halDevice & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) {
56 snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str());
57 }
58 return halAddress;
59 }
60
61 #ifdef AUDIO_HAL_VERSION_4_0
deviceAddressFromHal(audio_devices_t device,const char * halAddress,DeviceAddress * address)62 status_t deviceAddressFromHal(audio_devices_t device, const char* halAddress,
63 DeviceAddress* address) {
64 if (address == nullptr) {
65 return BAD_VALUE;
66 }
67 address->device = AudioDevice(device);
68 if (halAddress == nullptr || strnlen(halAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
69 return OK;
70 }
71
72 const bool isInput = (device & AUDIO_DEVICE_BIT_IN) != 0;
73 if (isInput) device &= ~AUDIO_DEVICE_BIT_IN;
74 if ((!isInput && (device & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) ||
75 (isInput && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) {
76 int status =
77 sscanf(halAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &address->address.mac[0],
78 &address->address.mac[1], &address->address.mac[2], &address->address.mac[3],
79 &address->address.mac[4], &address->address.mac[5]);
80 return status == 6 ? OK : BAD_VALUE;
81 } else if ((!isInput && (device & AUDIO_DEVICE_OUT_IP) != 0) ||
82 (isInput && (device & AUDIO_DEVICE_IN_IP) != 0)) {
83 int status =
84 sscanf(halAddress, "%hhu.%hhu.%hhu.%hhu", &address->address.ipv4[0],
85 &address->address.ipv4[1], &address->address.ipv4[2], &address->address.ipv4[3]);
86 return status == 4 ? OK : BAD_VALUE;
87 } else if ((!isInput && (device & AUDIO_DEVICE_OUT_ALL_USB)) != 0 ||
88 (isInput && (device & AUDIO_DEVICE_IN_ALL_USB)) != 0) {
89 int status = sscanf(halAddress, "card=%d;device=%d", &address->address.alsa.card,
90 &address->address.alsa.device);
91 return status == 2 ? OK : BAD_VALUE;
92 } else if ((!isInput && (device & AUDIO_DEVICE_OUT_BUS) != 0) ||
93 (isInput && (device & AUDIO_DEVICE_IN_BUS) != 0)) {
94 address->busAddress = halAddress;
95 return OK;
96 } else if ((!isInput && (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 ||
97 (isInput && (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) {
98 address->rSubmixAddress = halAddress;
99 return OK;
100 }
101 address->busAddress = halAddress;
102 return OK;
103 }
104
halToChannelMapping(audio_microphone_channel_mapping_t mapping)105 AudioMicrophoneChannelMapping halToChannelMapping(audio_microphone_channel_mapping_t mapping) {
106 switch (mapping) {
107 case AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED:
108 return AudioMicrophoneChannelMapping::UNUSED;
109 case AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT:
110 return AudioMicrophoneChannelMapping::DIRECT;
111 case AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED:
112 return AudioMicrophoneChannelMapping::PROCESSED;
113 default:
114 ALOGE("Invalid channel mapping type: %d", mapping);
115 return AudioMicrophoneChannelMapping::UNUSED;
116 }
117 }
118
halToLocation(audio_microphone_location_t location)119 AudioMicrophoneLocation halToLocation(audio_microphone_location_t location) {
120 switch (location) {
121 default:
122 case AUDIO_MICROPHONE_LOCATION_UNKNOWN:
123 return AudioMicrophoneLocation::UNKNOWN;
124 case AUDIO_MICROPHONE_LOCATION_MAINBODY:
125 return AudioMicrophoneLocation::MAINBODY;
126 case AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE:
127 return AudioMicrophoneLocation::MAINBODY_MOVABLE;
128 case AUDIO_MICROPHONE_LOCATION_PERIPHERAL:
129 return AudioMicrophoneLocation::PERIPHERAL;
130 }
131 }
132
halToDirectionality(audio_microphone_directionality_t dir)133 AudioMicrophoneDirectionality halToDirectionality(audio_microphone_directionality_t dir) {
134 switch (dir) {
135 default:
136 case AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN:
137 return AudioMicrophoneDirectionality::UNKNOWN;
138 case AUDIO_MICROPHONE_DIRECTIONALITY_OMNI:
139 return AudioMicrophoneDirectionality::OMNI;
140 case AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL:
141 return AudioMicrophoneDirectionality::BI_DIRECTIONAL;
142 case AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID:
143 return AudioMicrophoneDirectionality::CARDIOID;
144 case AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID:
145 return AudioMicrophoneDirectionality::HYPER_CARDIOID;
146 case AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID:
147 return AudioMicrophoneDirectionality::SUPER_CARDIOID;
148 }
149 }
150
halToMicrophoneCharacteristics(MicrophoneInfo * pDst,const struct audio_microphone_characteristic_t & src)151 bool halToMicrophoneCharacteristics(MicrophoneInfo* pDst,
152 const struct audio_microphone_characteristic_t& src) {
153 bool status = false;
154 if (pDst != NULL) {
155 pDst->deviceId = src.device_id;
156
157 if (deviceAddressFromHal(src.device, src.address, &pDst->deviceAddress) != OK) {
158 return false;
159 }
160 pDst->channelMapping.resize(AUDIO_CHANNEL_COUNT_MAX);
161 for (size_t ch = 0; ch < pDst->channelMapping.size(); ch++) {
162 pDst->channelMapping[ch] = halToChannelMapping(src.channel_mapping[ch]);
163 }
164 pDst->location = halToLocation(src.location);
165 pDst->group = (AudioMicrophoneGroup)src.group;
166 pDst->indexInTheGroup = (uint32_t)src.index_in_the_group;
167 pDst->sensitivity = src.sensitivity;
168 pDst->maxSpl = src.max_spl;
169 pDst->minSpl = src.min_spl;
170 pDst->directionality = halToDirectionality(src.directionality);
171 pDst->frequencyResponse.resize(src.num_frequency_responses);
172 for (size_t k = 0; k < src.num_frequency_responses; k++) {
173 pDst->frequencyResponse[k].frequency = src.frequency_responses[0][k];
174 pDst->frequencyResponse[k].level = src.frequency_responses[1][k];
175 }
176 pDst->position.x = src.geometric_location.x;
177 pDst->position.y = src.geometric_location.y;
178 pDst->position.z = src.geometric_location.z;
179
180 pDst->orientation.x = src.orientation.x;
181 pDst->orientation.y = src.orientation.y;
182 pDst->orientation.z = src.orientation.z;
183
184 status = true;
185 }
186 return status;
187 }
188 #endif
189
190 } // namespace implementation
191 } // namespace AUDIO_HAL_VERSION
192 } // namespace audio
193 } // namespace hardware
194 } // namespace android
195