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