Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 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 <stdio.h> |
| 18 | |
| 19 | #define LOG_TAG "DeviceHalHidl" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <android/hardware/audio/2.0/IPrimaryDevice.h> |
| 23 | #include <cutils/native_handle.h> |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include "DeviceHalHidl.h" |
| 27 | #include "HidlUtils.h" |
| 28 | #include "StreamHalHidl.h" |
| 29 | |
| 30 | using ::android::hardware::audio::common::V2_0::AudioConfig; |
| 31 | using ::android::hardware::audio::common::V2_0::AudioDevice; |
| 32 | using ::android::hardware::audio::common::V2_0::AudioInputFlag; |
| 33 | using ::android::hardware::audio::common::V2_0::AudioOutputFlag; |
| 34 | using ::android::hardware::audio::common::V2_0::AudioPatchHandle; |
| 35 | using ::android::hardware::audio::common::V2_0::AudioPort; |
| 36 | using ::android::hardware::audio::common::V2_0::AudioPortConfig; |
| 37 | using ::android::hardware::audio::common::V2_0::AudioMode; |
| 38 | using ::android::hardware::audio::common::V2_0::AudioSource; |
| 39 | using ::android::hardware::audio::V2_0::DeviceAddress; |
| 40 | using ::android::hardware::audio::V2_0::IPrimaryDevice; |
| 41 | using ::android::hardware::audio::V2_0::ParameterValue; |
| 42 | using ::android::hardware::audio::V2_0::Result; |
| 43 | using ::android::hardware::hidl_string; |
| 44 | using ::android::hardware::hidl_vec; |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | namespace { |
| 49 | |
| 50 | status_t deviceAddressFromHal( |
| 51 | audio_devices_t device, const char* halAddress, DeviceAddress* address) { |
| 52 | address->device = AudioDevice(device); |
Eric Laurent | 2c37fd3 | 2017-01-27 14:35:02 -0800 | [diff] [blame^] | 53 | |
| 54 | if (address == nullptr || strnlen(halAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) { |
| 55 | return OK; |
| 56 | } |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 57 | const bool isInput = (device & AUDIO_DEVICE_BIT_IN) != 0; |
| 58 | if (isInput) device &= ~AUDIO_DEVICE_BIT_IN; |
| 59 | if ((!isInput && (device & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) |
| 60 | || (isInput && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) { |
| 61 | int status = sscanf(halAddress, |
| 62 | "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", |
| 63 | &address->address.mac[0], &address->address.mac[1], &address->address.mac[2], |
| 64 | &address->address.mac[3], &address->address.mac[4], &address->address.mac[5]); |
| 65 | return status == 6 ? OK : BAD_VALUE; |
| 66 | } else if ((!isInput && (device & AUDIO_DEVICE_OUT_IP) != 0) |
| 67 | || (isInput && (device & AUDIO_DEVICE_IN_IP) != 0)) { |
| 68 | int status = sscanf(halAddress, |
| 69 | "%hhu.%hhu.%hhu.%hhu", |
| 70 | &address->address.ipv4[0], &address->address.ipv4[1], |
| 71 | &address->address.ipv4[2], &address->address.ipv4[3]); |
| 72 | return status == 4 ? OK : BAD_VALUE; |
| 73 | } else if ((!isInput && (device & AUDIO_DEVICE_OUT_ALL_USB)) != 0 |
| 74 | || (isInput && (device & AUDIO_DEVICE_IN_ALL_USB)) != 0) { |
| 75 | int status = sscanf(halAddress, |
| 76 | "card=%d;device=%d", |
| 77 | &address->address.alsa.card, &address->address.alsa.device); |
| 78 | return status == 2 ? OK : BAD_VALUE; |
| 79 | } else if ((!isInput && (device & AUDIO_DEVICE_OUT_BUS) != 0) |
| 80 | || (isInput && (device & AUDIO_DEVICE_IN_BUS) != 0)) { |
| 81 | if (halAddress != NULL) { |
| 82 | address->busAddress = halAddress; |
| 83 | return OK; |
| 84 | } |
| 85 | return BAD_VALUE; |
| 86 | } else if ((!isInput && (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 |
| 87 | || (isInput && (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) { |
| 88 | if (halAddress != NULL) { |
| 89 | address->rSubmixAddress = halAddress; |
| 90 | return OK; |
| 91 | } |
| 92 | return BAD_VALUE; |
| 93 | } |
| 94 | return OK; |
| 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | |
| 99 | DeviceHalHidl::DeviceHalHidl(const sp<IDevice>& device) |
| 100 | : ConversionHelperHidl("Device"), mDevice(device) { |
| 101 | } |
| 102 | |
| 103 | DeviceHalHidl::~DeviceHalHidl() { |
| 104 | } |
| 105 | |
| 106 | status_t DeviceHalHidl::getSupportedDevices(uint32_t*) { |
| 107 | // Obsolete. |
| 108 | return INVALID_OPERATION; |
| 109 | } |
| 110 | |
| 111 | status_t DeviceHalHidl::initCheck() { |
| 112 | if (mDevice == 0) return NO_INIT; |
| 113 | return processReturn("initCheck", mDevice->initCheck()); |
| 114 | } |
| 115 | |
| 116 | status_t DeviceHalHidl::setVoiceVolume(float volume) { |
| 117 | if (mDevice == 0) return NO_INIT; |
| 118 | sp<IPrimaryDevice> primaryDev = IPrimaryDevice::castFrom(mDevice); |
| 119 | if (primaryDev == 0) return INVALID_OPERATION; |
| 120 | return processReturn("setVoiceVolume", primaryDev->setVoiceVolume(volume)); |
| 121 | } |
| 122 | |
| 123 | status_t DeviceHalHidl::setMasterVolume(float volume) { |
| 124 | if (mDevice == 0) return NO_INIT; |
| 125 | sp<IPrimaryDevice> primaryDev = IPrimaryDevice::castFrom(mDevice); |
| 126 | if (primaryDev == 0) return INVALID_OPERATION; |
| 127 | return processReturn("setMasterVolume", primaryDev->setMasterVolume(volume)); |
| 128 | } |
| 129 | |
| 130 | status_t DeviceHalHidl::getMasterVolume(float *volume) { |
| 131 | if (mDevice == 0) return NO_INIT; |
| 132 | sp<IPrimaryDevice> primaryDev = IPrimaryDevice::castFrom(mDevice); |
| 133 | if (primaryDev == 0) return INVALID_OPERATION; |
| 134 | Result retval; |
| 135 | Return<void> ret = primaryDev->getMasterVolume( |
| 136 | [&](Result r, float v) { |
| 137 | retval = r; |
| 138 | if (retval == Result::OK) { |
| 139 | *volume = v; |
| 140 | } |
| 141 | }); |
| 142 | return processReturn("getMasterVolume", ret, retval); |
| 143 | } |
| 144 | |
| 145 | status_t DeviceHalHidl::setMode(audio_mode_t mode) { |
| 146 | if (mDevice == 0) return NO_INIT; |
| 147 | sp<IPrimaryDevice> primaryDev = IPrimaryDevice::castFrom(mDevice); |
| 148 | if (primaryDev == 0) return INVALID_OPERATION; |
| 149 | return processReturn("setMode", primaryDev->setMode(AudioMode(mode))); |
| 150 | } |
| 151 | |
| 152 | status_t DeviceHalHidl::setMicMute(bool state) { |
| 153 | if (mDevice == 0) return NO_INIT; |
| 154 | return processReturn("setMicMute", mDevice->setMicMute(state)); |
| 155 | } |
| 156 | |
| 157 | status_t DeviceHalHidl::getMicMute(bool *state) { |
| 158 | if (mDevice == 0) return NO_INIT; |
| 159 | Result retval; |
| 160 | Return<void> ret = mDevice->getMicMute( |
| 161 | [&](Result r, bool mute) { |
| 162 | retval = r; |
| 163 | if (retval == Result::OK) { |
| 164 | *state = mute; |
| 165 | } |
| 166 | }); |
| 167 | return processReturn("getMicMute", ret, retval); |
| 168 | } |
| 169 | |
| 170 | status_t DeviceHalHidl::setMasterMute(bool state) { |
| 171 | if (mDevice == 0) return NO_INIT; |
| 172 | return processReturn("setMasterMute", mDevice->setMasterMute(state)); |
| 173 | } |
| 174 | |
| 175 | status_t DeviceHalHidl::getMasterMute(bool *state) { |
| 176 | if (mDevice == 0) return NO_INIT; |
| 177 | Result retval; |
| 178 | Return<void> ret = mDevice->getMasterMute( |
| 179 | [&](Result r, bool mute) { |
| 180 | retval = r; |
| 181 | if (retval == Result::OK) { |
| 182 | *state = mute; |
| 183 | } |
| 184 | }); |
| 185 | return processReturn("getMasterMute", ret, retval); |
| 186 | } |
| 187 | |
| 188 | status_t DeviceHalHidl::setParameters(const String8& kvPairs) { |
| 189 | if (mDevice == 0) return NO_INIT; |
| 190 | hidl_vec<ParameterValue> hidlParams; |
| 191 | status_t status = parametersFromHal(kvPairs, &hidlParams); |
| 192 | if (status != OK) return status; |
| 193 | return processReturn("setParameters", mDevice->setParameters(hidlParams)); |
| 194 | } |
| 195 | |
| 196 | status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) { |
| 197 | values->clear(); |
| 198 | if (mDevice == 0) return NO_INIT; |
| 199 | hidl_vec<hidl_string> hidlKeys; |
| 200 | status_t status = keysFromHal(keys, &hidlKeys); |
| 201 | if (status != OK) return status; |
| 202 | Result retval; |
| 203 | Return<void> ret = mDevice->getParameters( |
| 204 | hidlKeys, |
| 205 | [&](Result r, const hidl_vec<ParameterValue>& parameters) { |
| 206 | retval = r; |
| 207 | if (retval == Result::OK) { |
| 208 | parametersToHal(parameters, values); |
| 209 | } |
| 210 | }); |
| 211 | return processReturn("getParameters", ret, retval); |
| 212 | } |
| 213 | |
| 214 | status_t DeviceHalHidl::getInputBufferSize( |
| 215 | const struct audio_config *config, size_t *size) { |
| 216 | if (mDevice == 0) return NO_INIT; |
| 217 | AudioConfig hidlConfig; |
| 218 | HidlUtils::audioConfigFromHal(*config, &hidlConfig); |
| 219 | Result retval; |
| 220 | Return<void> ret = mDevice->getInputBufferSize( |
| 221 | hidlConfig, |
| 222 | [&](Result r, uint64_t bufferSize) { |
| 223 | retval = r; |
| 224 | if (retval == Result::OK) { |
| 225 | *size = static_cast<size_t>(bufferSize); |
| 226 | } |
| 227 | }); |
| 228 | return processReturn("getInputBufferSize", ret, retval); |
| 229 | } |
| 230 | |
| 231 | status_t DeviceHalHidl::openOutputStream( |
| 232 | audio_io_handle_t handle, |
| 233 | audio_devices_t devices, |
| 234 | audio_output_flags_t flags, |
| 235 | struct audio_config *config, |
| 236 | const char *address, |
| 237 | sp<StreamOutHalInterface> *outStream) { |
| 238 | if (mDevice == 0) return NO_INIT; |
| 239 | DeviceAddress hidlDevice; |
| 240 | status_t status = deviceAddressFromHal(devices, address, &hidlDevice); |
| 241 | if (status != OK) return status; |
| 242 | AudioConfig hidlConfig; |
| 243 | HidlUtils::audioConfigFromHal(*config, &hidlConfig); |
| 244 | Result retval = Result::NOT_INITIALIZED; |
| 245 | Return<void> ret = mDevice->openOutputStream( |
| 246 | handle, |
| 247 | hidlDevice, |
| 248 | hidlConfig, |
| 249 | AudioOutputFlag(flags), |
| 250 | [&](Result r, const sp<IStreamOut>& result, const AudioConfig& suggestedConfig) { |
| 251 | retval = r; |
| 252 | if (retval == Result::OK) { |
| 253 | *outStream = new StreamOutHalHidl(result); |
| 254 | } |
| 255 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 256 | }); |
| 257 | return processReturn("openOutputStream", ret, retval); |
| 258 | } |
| 259 | |
| 260 | status_t DeviceHalHidl::openInputStream( |
| 261 | audio_io_handle_t handle, |
| 262 | audio_devices_t devices, |
| 263 | struct audio_config *config, |
| 264 | audio_input_flags_t flags, |
| 265 | const char *address, |
| 266 | audio_source_t source, |
| 267 | sp<StreamInHalInterface> *inStream) { |
| 268 | if (mDevice == 0) return NO_INIT; |
| 269 | DeviceAddress hidlDevice; |
| 270 | status_t status = deviceAddressFromHal(devices, address, &hidlDevice); |
| 271 | if (status != OK) return status; |
| 272 | AudioConfig hidlConfig; |
| 273 | HidlUtils::audioConfigFromHal(*config, &hidlConfig); |
| 274 | Result retval = Result::NOT_INITIALIZED; |
| 275 | Return<void> ret = mDevice->openInputStream( |
| 276 | handle, |
| 277 | hidlDevice, |
| 278 | hidlConfig, |
| 279 | AudioInputFlag(flags), |
| 280 | AudioSource(source), |
| 281 | [&](Result r, const sp<IStreamIn>& result, const AudioConfig& suggestedConfig) { |
| 282 | retval = r; |
| 283 | if (retval == Result::OK) { |
| 284 | *inStream = new StreamInHalHidl(result); |
| 285 | } |
| 286 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 287 | }); |
| 288 | return processReturn("openInputStream", ret, retval); |
| 289 | } |
| 290 | |
| 291 | status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) { |
| 292 | if (mDevice == 0) return NO_INIT; |
| 293 | return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches); |
| 294 | } |
| 295 | |
| 296 | status_t DeviceHalHidl::createAudioPatch( |
| 297 | unsigned int num_sources, |
| 298 | const struct audio_port_config *sources, |
| 299 | unsigned int num_sinks, |
| 300 | const struct audio_port_config *sinks, |
| 301 | audio_patch_handle_t *patch) { |
| 302 | if (mDevice == 0) return NO_INIT; |
| 303 | hidl_vec<AudioPortConfig> hidlSources, hidlSinks; |
| 304 | HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources); |
| 305 | HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks); |
| 306 | Result retval; |
| 307 | Return<void> ret = mDevice->createAudioPatch( |
| 308 | hidlSources, hidlSinks, |
| 309 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 310 | retval = r; |
| 311 | if (retval == Result::OK) { |
| 312 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 313 | } |
| 314 | }); |
| 315 | return processReturn("createAudioPatch", ret, retval); |
| 316 | } |
| 317 | |
| 318 | status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) { |
| 319 | if (mDevice == 0) return NO_INIT; |
| 320 | return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch)); |
| 321 | } |
| 322 | |
| 323 | status_t DeviceHalHidl::getAudioPort(struct audio_port *port) { |
| 324 | if (mDevice == 0) return NO_INIT; |
| 325 | AudioPort hidlPort; |
| 326 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 327 | Result retval; |
| 328 | Return<void> ret = mDevice->getAudioPort( |
| 329 | hidlPort, |
| 330 | [&](Result r, const AudioPort& p) { |
| 331 | retval = r; |
| 332 | if (retval == Result::OK) { |
| 333 | HidlUtils::audioPortToHal(p, port); |
| 334 | } |
| 335 | }); |
| 336 | return processReturn("getAudioPort", ret, retval); |
| 337 | } |
| 338 | |
| 339 | status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) { |
| 340 | if (mDevice == 0) return NO_INIT; |
| 341 | AudioPortConfig hidlConfig; |
| 342 | HidlUtils::audioPortConfigFromHal(*config, &hidlConfig); |
| 343 | return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig)); |
| 344 | } |
| 345 | |
| 346 | status_t DeviceHalHidl::dump(int fd) { |
| 347 | if (mDevice == 0) return NO_INIT; |
| 348 | native_handle_t* hidlHandle = native_handle_create(1, 0); |
| 349 | hidlHandle->data[0] = fd; |
| 350 | Return<void> ret = mDevice->debugDump(hidlHandle); |
| 351 | native_handle_delete(hidlHandle); |
| 352 | return processReturn("dump", ret); |
| 353 | } |
| 354 | |
| 355 | } // namespace android |