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