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 <string.h>
18
19 #define LOG_TAG "HalHidl"
20 #include <media/AudioContainers.h>
21 #include <media/AudioParameter.h>
22 #include <utils/Log.h>
23
24 #include "ConversionHelperHidl.h"
25
26 namespace android {
27 namespace CPP_VERSION {
28
29 using namespace ::android::hardware::audio::common::CPP_VERSION;
30 using namespace ::android::hardware::audio::CPP_VERSION;
31
32 // static
keysFromHal(const String8 & keys,hidl_vec<hidl_string> * hidlKeys)33 status_t ConversionHelperHidl::keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys) {
34 AudioParameter halKeys(keys);
35 if (halKeys.size() == 0) return BAD_VALUE;
36 hidlKeys->resize(halKeys.size());
37 //FIXME: keyStreamSupportedChannels and keyStreamSupportedSamplingRates come with a
38 // "keyFormat=<value>" pair. We need to transform it into a single key string so that it is
39 // carried over to the legacy HAL via HIDL.
40 String8 value;
41 bool keepFormatValue = halKeys.size() == 2 &&
42 (halKeys.get(String8(AudioParameter::keyStreamSupportedChannels), value) == NO_ERROR ||
43 halKeys.get(String8(AudioParameter::keyStreamSupportedSamplingRates), value) == NO_ERROR);
44
45 for (size_t i = 0; i < halKeys.size(); ++i) {
46 String8 key;
47 status_t status = halKeys.getAt(i, key);
48 if (status != OK) return status;
49 if (keepFormatValue && key == AudioParameter::keyFormat) {
50 AudioParameter formatParam;
51 halKeys.getAt(i, key, value);
52 formatParam.add(key, value);
53 key = formatParam.toString();
54 }
55 (*hidlKeys)[i] = key.string();
56 }
57 return OK;
58 }
59
60 // static
parametersFromHal(const String8 & kvPairs,hidl_vec<ParameterValue> * hidlParams)61 status_t ConversionHelperHidl::parametersFromHal(
62 const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) {
63 AudioParameter params(kvPairs);
64 if (params.size() == 0) return BAD_VALUE;
65 hidlParams->resize(params.size());
66 for (size_t i = 0; i < params.size(); ++i) {
67 String8 key, value;
68 status_t status = params.getAt(i, key, value);
69 if (status != OK) return status;
70 (*hidlParams)[i].key = key.string();
71 (*hidlParams)[i].value = value.string();
72 }
73 return OK;
74 }
75
76 // static
parametersToHal(const hidl_vec<ParameterValue> & parameters,String8 * values)77 void ConversionHelperHidl::parametersToHal(
78 const hidl_vec<ParameterValue>& parameters, String8 *values) {
79 AudioParameter params;
80 for (size_t i = 0; i < parameters.size(); ++i) {
81 params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
82 }
83 values->setTo(params.toString());
84 }
85
ConversionHelperHidl(const char * className)86 ConversionHelperHidl::ConversionHelperHidl(const char* className)
87 : mClassName(className) {
88 }
89
90 // static
analyzeResult(const Result & result)91 status_t ConversionHelperHidl::analyzeResult(const Result& result) {
92 switch (result) {
93 case Result::OK: return OK;
94 case Result::INVALID_ARGUMENTS: return BAD_VALUE;
95 case Result::INVALID_STATE: return NOT_ENOUGH_DATA;
96 case Result::NOT_INITIALIZED: return NO_INIT;
97 case Result::NOT_SUPPORTED: return INVALID_OPERATION;
98 default: return NO_INIT;
99 }
100 }
101
emitError(const char * funcName,const char * description)102 void ConversionHelperHidl::emitError(const char* funcName, const char* description) {
103 ALOGE("%s %p %s: %s (from rpc)", mClassName, this, funcName, description);
104 }
105
106 #if MAJOR_VERSION >= 4
107 // TODO: Use the same implementation in the hal when it moves to a util library.
deviceAddressToHal(const DeviceAddress & address)108 static std::string deviceAddressToHal(const DeviceAddress& address) {
109 // HAL assumes that the address is NUL-terminated.
110 char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
111 memset(halAddress, 0, sizeof(halAddress));
112 audio_devices_t halDevice = static_cast<audio_devices_t>(address.device);
113 if (getAudioDeviceOutAllA2dpSet().count(halDevice) > 0 ||
114 halDevice == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
115 snprintf(halAddress, sizeof(halAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
116 address.address.mac[0], address.address.mac[1], address.address.mac[2],
117 address.address.mac[3], address.address.mac[4], address.address.mac[5]);
118 } else if (halDevice == AUDIO_DEVICE_OUT_IP || halDevice == AUDIO_DEVICE_IN_IP) {
119 snprintf(halAddress, sizeof(halAddress), "%d.%d.%d.%d", address.address.ipv4[0],
120 address.address.ipv4[1], address.address.ipv4[2], address.address.ipv4[3]);
121 } else if (getAudioDeviceOutAllUsbSet().count(halDevice) > 0 ||
122 getAudioDeviceInAllUsbSet().count(halDevice) > 0) {
123 snprintf(halAddress, sizeof(halAddress), "card=%d;device=%d", address.address.alsa.card,
124 address.address.alsa.device);
125 } else if (halDevice == AUDIO_DEVICE_OUT_BUS || halDevice == AUDIO_DEVICE_IN_BUS) {
126 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
127 } else if (halDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
128 halDevice == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
129 snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str());
130 } else {
131 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
132 }
133 return halAddress;
134 }
135
136 //local conversion helpers
137
channelMappingToHal(AudioMicrophoneChannelMapping mapping)138 static audio_microphone_channel_mapping_t channelMappingToHal(AudioMicrophoneChannelMapping mapping) {
139 switch (mapping) {
140 case AudioMicrophoneChannelMapping::UNUSED:
141 return AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
142 case AudioMicrophoneChannelMapping::DIRECT:
143 return AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT;
144 case AudioMicrophoneChannelMapping::PROCESSED:
145 return AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED;
146 default:
147 LOG_ALWAYS_FATAL("Unknown channelMappingToHal conversion %d", mapping);
148 }
149 }
150
locationToHal(AudioMicrophoneLocation location)151 static audio_microphone_location_t locationToHal(AudioMicrophoneLocation location) {
152 switch (location) {
153 case AudioMicrophoneLocation::UNKNOWN:
154 return AUDIO_MICROPHONE_LOCATION_UNKNOWN;
155 case AudioMicrophoneLocation::MAINBODY:
156 return AUDIO_MICROPHONE_LOCATION_MAINBODY;
157 case AudioMicrophoneLocation::MAINBODY_MOVABLE:
158 return AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE;
159 case AudioMicrophoneLocation::PERIPHERAL:
160 return AUDIO_MICROPHONE_LOCATION_PERIPHERAL;
161 default:
162 LOG_ALWAYS_FATAL("Unknown locationToHal conversion %d", location);
163 }
164 }
directionalityToHal(AudioMicrophoneDirectionality dir)165 static audio_microphone_directionality_t directionalityToHal(AudioMicrophoneDirectionality dir) {
166 switch (dir) {
167 case AudioMicrophoneDirectionality::UNKNOWN:
168 return AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN;
169 case AudioMicrophoneDirectionality::OMNI:
170 return AUDIO_MICROPHONE_DIRECTIONALITY_OMNI;
171 case AudioMicrophoneDirectionality::BI_DIRECTIONAL:
172 return AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL;
173 case AudioMicrophoneDirectionality::CARDIOID:
174 return AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID;
175 case AudioMicrophoneDirectionality::HYPER_CARDIOID:
176 return AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID;
177 case AudioMicrophoneDirectionality::SUPER_CARDIOID:
178 return AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID;
179 default:
180 LOG_ALWAYS_FATAL("Unknown directionalityToHal conversion %d", dir);
181 }
182 }
183
microphoneInfoToHal(const MicrophoneInfo & src,audio_microphone_characteristic_t * pDst)184 void microphoneInfoToHal(const MicrophoneInfo& src,
185 audio_microphone_characteristic_t *pDst) {
186 if (pDst != NULL) {
187 snprintf(pDst->device_id, sizeof(pDst->device_id),
188 "%s", src.deviceId.c_str());
189 pDst->device = static_cast<audio_devices_t>(src.deviceAddress.device);
190 snprintf(pDst->address, sizeof(pDst->address),
191 "%s", deviceAddressToHal(src.deviceAddress).c_str());
192 if (src.channelMapping.size() > AUDIO_CHANNEL_COUNT_MAX) {
193 ALOGW("microphoneInfoToStruct found %zu channelMapping elements. Max expected is %d",
194 src.channelMapping.size(), AUDIO_CHANNEL_COUNT_MAX);
195 }
196 size_t ch;
197 for (ch = 0; ch < src.channelMapping.size() && ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
198 pDst->channel_mapping[ch] = channelMappingToHal(src.channelMapping[ch]);
199 }
200 for (; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
201 pDst->channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
202 }
203 pDst->location = locationToHal(src.location);
204 pDst->group = (audio_microphone_group_t)src.group;
205 pDst->index_in_the_group = (unsigned int)src.indexInTheGroup;
206 pDst->sensitivity = src.sensitivity;
207 pDst->max_spl = src.maxSpl;
208 pDst->min_spl = src.minSpl;
209 pDst->directionality = directionalityToHal(src.directionality);
210 pDst->num_frequency_responses = (unsigned int)src.frequencyResponse.size();
211 if (pDst->num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
212 ALOGW("microphoneInfoToStruct found %d frequency responses. Max expected is %d",
213 pDst->num_frequency_responses, AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES);
214 pDst->num_frequency_responses = AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES;
215 }
216 for (size_t k = 0; k < pDst->num_frequency_responses; k++) {
217 pDst->frequency_responses[0][k] = src.frequencyResponse[k].frequency;
218 pDst->frequency_responses[1][k] = src.frequencyResponse[k].level;
219 }
220 pDst->geometric_location.x = src.position.x;
221 pDst->geometric_location.y = src.position.y;
222 pDst->geometric_location.z = src.position.z;
223 pDst->orientation.x = src.orientation.x;
224 pDst->orientation.y = src.orientation.y;
225 pDst->orientation.z = src.orientation.z;
226 }
227 }
228 #endif
229
230 } // namespace CPP_VERSION
231 } // namespace android
232