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 <cutils/native_handle.h>
23 #include <cutils/properties.h>
24 #include <hwbinder/IPCThreadState.h>
25 #include <media/AudioContainers.h>
26 #include <mediautils/TimeCheck.h>
27 #include <utils/Log.h>
28
29 #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h)
30 #include <HidlUtils.h>
31 #include <common/all-versions/VersionUtils.h>
32 #include <util/CoreUtils.h>
33
34 #include "DeviceHalHidl.h"
35 #include "EffectHalHidl.h"
36 #include "ParameterUtils.h"
37 #include "StreamHalHidl.h"
38
39 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
40 #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h>
41 #include <aidl/android/hardware/audio/sounddose/BpSoundDoseFactory.h>
42 #include <android/binder_manager.h>
43
44 constexpr std::string_view kSoundDoseInterfaceModule = "/default";
45
46 using aidl::android::hardware::audio::core::sounddose::ISoundDose;
47 using aidl::android::hardware::audio::sounddose::ISoundDoseFactory;
48 #endif
49
50 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
51 using ::android::hardware::audio::common::utils::EnumBitfield;
52 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils;
53 using ::android::hardware::hidl_string;
54 using ::android::hardware::hidl_vec;
55
56 namespace android {
57
58 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
59 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
60
61 class DeviceHalHidl::SoundDoseWrapper {
62 public:
63 SoundDoseWrapper() = default;
64 ~SoundDoseWrapper() = default;
65
66 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
67 std::shared_ptr<ISoundDoseFactory> mSoundDoseFactory;
68 std::shared_ptr<ISoundDose> mSoundDose;
69 #endif
70 };
71
DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice> & device)72 DeviceHalHidl::DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice>& device)
73 : CoreConversionHelperHidl("DeviceHalHidl"),
74 mDevice(device),
75 mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) {
76 }
77
DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice> & device)78 DeviceHalHidl::DeviceHalHidl(
79 const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& device)
80 : CoreConversionHelperHidl("DeviceHalHidl"),
81 #if MAJOR_VERSION <= 6 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0)
82 mDevice(device),
83 #endif
84 mPrimaryDevice(device),
85 mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) {
86 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
87 auto getDeviceRet = mPrimaryDevice->getDevice();
88 if (getDeviceRet.isOk()) {
89 mDevice = getDeviceRet;
90 } else {
91 ALOGE("Call to IPrimaryDevice.getDevice has failed: %s",
92 getDeviceRet.description().c_str());
93 }
94 #endif
95 }
96
~DeviceHalHidl()97 DeviceHalHidl::~DeviceHalHidl() {
98 if (mDevice != 0) {
99 #if MAJOR_VERSION <= 5
100 mDevice.clear();
101 hardware::IPCThreadState::self()->flushCommands();
102 #elif MAJOR_VERSION >= 6
103 mDevice->close();
104 #endif
105 }
106 }
107
getAudioPorts(std::vector<media::audio::common::AudioPort> * ports __unused)108 status_t DeviceHalHidl::getAudioPorts(
109 std::vector<media::audio::common::AudioPort> *ports __unused) {
110 return INVALID_OPERATION;
111 }
112
getAudioRoutes(std::vector<media::AudioRoute> * routes __unused)113 status_t DeviceHalHidl::getAudioRoutes(std::vector<media::AudioRoute> *routes __unused) {
114 return INVALID_OPERATION;
115 }
116
getSupportedModes(std::vector<media::audio::common::AudioMode> * modes __unused)117 status_t DeviceHalHidl::getSupportedModes(
118 std::vector<media::audio::common::AudioMode> *modes __unused) {
119 return INVALID_OPERATION;
120 }
121
getSupportedDevices(uint32_t *)122 status_t DeviceHalHidl::getSupportedDevices(uint32_t*) {
123 // Obsolete.
124 return INVALID_OPERATION;
125 }
126
initCheck()127 status_t DeviceHalHidl::initCheck() {
128 TIME_CHECK();
129 if (mDevice == 0) return NO_INIT;
130 return processReturn("initCheck", mDevice->initCheck());
131 }
132
setVoiceVolume(float volume)133 status_t DeviceHalHidl::setVoiceVolume(float volume) {
134 TIME_CHECK();
135 if (mDevice == 0) return NO_INIT;
136 if (mPrimaryDevice == 0) return INVALID_OPERATION;
137 return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume));
138 }
139
setMasterVolume(float volume)140 status_t DeviceHalHidl::setMasterVolume(float volume) {
141 TIME_CHECK();
142 if (mDevice == 0) return NO_INIT;
143 return processReturn("setMasterVolume", mDevice->setMasterVolume(volume));
144 }
145
getMasterVolume(float * volume)146 status_t DeviceHalHidl::getMasterVolume(float *volume) {
147 TIME_CHECK();
148 if (mDevice == 0) return NO_INIT;
149 Result retval;
150 Return<void> ret = mDevice->getMasterVolume(
151 [&](Result r, float v) {
152 retval = r;
153 if (retval == Result::OK) {
154 *volume = v;
155 }
156 });
157 return processReturn("getMasterVolume", ret, retval);
158 }
159
setMode(audio_mode_t mode)160 status_t DeviceHalHidl::setMode(audio_mode_t mode) {
161 TIME_CHECK();
162 if (mDevice == 0) return NO_INIT;
163 if (mPrimaryDevice == 0) return INVALID_OPERATION;
164 return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode)));
165 }
166
setMicMute(bool state)167 status_t DeviceHalHidl::setMicMute(bool state) {
168 TIME_CHECK();
169 if (mDevice == 0) return NO_INIT;
170 return processReturn("setMicMute", mDevice->setMicMute(state));
171 }
172
getMicMute(bool * state)173 status_t DeviceHalHidl::getMicMute(bool *state) {
174 TIME_CHECK();
175 if (mDevice == 0) return NO_INIT;
176 Result retval;
177 Return<void> ret = mDevice->getMicMute(
178 [&](Result r, bool mute) {
179 retval = r;
180 if (retval == Result::OK) {
181 *state = mute;
182 }
183 });
184 return processReturn("getMicMute", ret, retval);
185 }
186
setMasterMute(bool state)187 status_t DeviceHalHidl::setMasterMute(bool state) {
188 TIME_CHECK();
189 if (mDevice == 0) return NO_INIT;
190 return processReturn("setMasterMute", mDevice->setMasterMute(state));
191 }
192
getMasterMute(bool * state)193 status_t DeviceHalHidl::getMasterMute(bool *state) {
194 TIME_CHECK();
195 if (mDevice == 0) return NO_INIT;
196 Result retval;
197 Return<void> ret = mDevice->getMasterMute(
198 [&](Result r, bool mute) {
199 retval = r;
200 if (retval == Result::OK) {
201 *state = mute;
202 }
203 });
204 return processReturn("getMasterMute", ret, retval);
205 }
206
setParameters(const String8 & kvPairs)207 status_t DeviceHalHidl::setParameters(const String8& kvPairs) {
208 TIME_CHECK();
209 if (mDevice == 0) return NO_INIT;
210 hidl_vec<ParameterValue> hidlParams;
211 status_t status = parametersFromHal(kvPairs, &hidlParams);
212 if (status != OK) return status;
213 // TODO: change the API so that context and kvPairs are separated
214 return processReturn("setParameters",
215 utils::setParameters(mDevice, {} /* context */, hidlParams));
216 }
217
getParameters(const String8 & keys,String8 * values)218 status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) {
219 TIME_CHECK();
220 values->clear();
221 if (mDevice == 0) return NO_INIT;
222 hidl_vec<hidl_string> hidlKeys;
223 status_t status = keysFromHal(keys, &hidlKeys);
224 if (status != OK) return status;
225 Result retval;
226 Return<void> ret = utils::getParameters(mDevice,
227 {} /* context */,
228 hidlKeys,
229 [&](Result r, const hidl_vec<ParameterValue>& parameters) {
230 retval = r;
231 if (retval == Result::OK) {
232 parametersToHal(parameters, values);
233 }
234 });
235 return processReturn("getParameters", ret, retval);
236 }
237
getInputBufferSize(const struct audio_config * config,size_t * size)238 status_t DeviceHalHidl::getInputBufferSize(
239 const struct audio_config *config, size_t *size) {
240 TIME_CHECK();
241 if (mDevice == 0) return NO_INIT;
242 AudioConfig hidlConfig;
243 HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig);
244 Result retval;
245 Return<void> ret = mDevice->getInputBufferSize(
246 hidlConfig,
247 [&](Result r, uint64_t bufferSize) {
248 retval = r;
249 if (retval == Result::OK) {
250 *size = static_cast<size_t>(bufferSize);
251 }
252 });
253 return processReturn("getInputBufferSize", ret, retval);
254 }
255
openOutputStream(audio_io_handle_t handle,audio_devices_t deviceType,audio_output_flags_t flags,struct audio_config * config,const char * address,sp<StreamOutHalInterface> * outStream)256 status_t DeviceHalHidl::openOutputStream(
257 audio_io_handle_t handle,
258 audio_devices_t deviceType,
259 audio_output_flags_t flags,
260 struct audio_config *config,
261 const char *address,
262 sp<StreamOutHalInterface> *outStream) {
263 TIME_CHECK();
264 if (mDevice == 0) return NO_INIT;
265 DeviceAddress hidlDevice;
266 if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice);
267 status != OK) {
268 return status;
269 }
270 AudioConfig hidlConfig;
271 if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig);
272 status != OK) {
273 return status;
274 }
275
276 #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1)
277 //TODO: b/193496180 use spatializer flag at audio HAL when available
278 if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) {
279 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER);
280 flags = (audio_output_flags_t)
281 (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
282 }
283 #endif
284
285 CoreUtils::AudioOutputFlags hidlFlags;
286 if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) {
287 return status;
288 }
289 Result retval = Result::NOT_INITIALIZED;
290 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
291 Return<void> ret = mDevice->openOutputStream_7_1(
292 #else
293 Return<void> ret = mDevice->openOutputStream(
294 #endif
295 handle, hidlDevice, hidlConfig, hidlFlags,
296 #if MAJOR_VERSION >= 4
297 {} /* metadata */,
298 #endif
299 [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result,
300 const AudioConfig& suggestedConfig) {
301 retval = r;
302 if (retval == Result::OK) {
303 *outStream = new StreamOutHalHidl(result);
304 }
305 HidlUtils::audioConfigToHal(suggestedConfig, config);
306 });
307 return processReturn("openOutputStream", ret, retval);
308 }
309
openInputStream(audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,audio_input_flags_t flags,const char * address,audio_source_t source,audio_devices_t outputDevice,const char * outputDeviceAddress,sp<StreamInHalInterface> * inStream)310 status_t DeviceHalHidl::openInputStream(
311 audio_io_handle_t handle,
312 audio_devices_t devices,
313 struct audio_config *config,
314 audio_input_flags_t flags,
315 const char *address,
316 audio_source_t source,
317 audio_devices_t outputDevice,
318 const char *outputDeviceAddress,
319 sp<StreamInHalInterface> *inStream) {
320 TIME_CHECK();
321 if (mDevice == 0) return NO_INIT;
322 DeviceAddress hidlDevice;
323 if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice);
324 status != OK) {
325 return status;
326 }
327 AudioConfig hidlConfig;
328 if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig);
329 status != OK) {
330 return status;
331 }
332 CoreUtils::AudioInputFlags hidlFlags;
333 #if MAJOR_VERSION <= 5
334 // Some flags were specific to framework and must not leak to the HAL.
335 flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT);
336 #endif
337 if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) {
338 return status;
339 }
340 Result retval = Result::NOT_INITIALIZED;
341 #if MAJOR_VERSION == 2
342 auto sinkMetadata = AudioSource(source);
343 #elif MAJOR_VERSION >= 4
344 // TODO: correctly propagate the tracks sources and volume
345 // for now, only send the main source at 1dbfs
346 AudioSource hidlSource;
347 if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) {
348 return status;
349 }
350 SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}};
351 #endif
352 #if MAJOR_VERSION < 5
353 (void)outputDevice;
354 (void)outputDeviceAddress;
355 #else
356 #if MAJOR_VERSION >= 7
357 (void)HidlUtils::audioChannelMaskFromHal(
358 AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask);
359 #endif
360 if (outputDevice != AUDIO_DEVICE_NONE) {
361 DeviceAddress hidlOutputDevice;
362 if (status_t status = CoreUtils::deviceAddressFromHal(
363 outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) {
364 return status;
365 }
366 sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice));
367 }
368 #endif
369 Return<void> ret = mDevice->openInputStream(
370 handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata,
371 [&](Result r,
372 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& result,
373 const AudioConfig& suggestedConfig) {
374 retval = r;
375 if (retval == Result::OK) {
376 *inStream = new StreamInHalHidl(result);
377 }
378 HidlUtils::audioConfigToHal(suggestedConfig, config);
379 });
380 return processReturn("openInputStream", ret, retval);
381 }
382
supportsAudioPatches(bool * supportsPatches)383 status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) {
384 TIME_CHECK();
385 if (mDevice == 0) return NO_INIT;
386 return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches);
387 }
388
createAudioPatch(unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * patch)389 status_t DeviceHalHidl::createAudioPatch(
390 unsigned int num_sources,
391 const struct audio_port_config *sources,
392 unsigned int num_sinks,
393 const struct audio_port_config *sinks,
394 audio_patch_handle_t *patch) {
395 TIME_CHECK();
396 if (mDevice == 0) return NO_INIT;
397 if (patch == nullptr) return BAD_VALUE;
398
399 #if MAJOR_VERSION < 6
400 if (*patch != AUDIO_PATCH_HANDLE_NONE) {
401 status_t status = releaseAudioPatch(*patch);
402 ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d",
403 __func__, status, *patch);
404 *patch = AUDIO_PATCH_HANDLE_NONE;
405 }
406 #endif
407
408 hidl_vec<AudioPortConfig> hidlSources, hidlSinks;
409 HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources);
410 HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks);
411 Result retval = Result::OK;
412 Return<void> ret;
413 std::string methodName = "createAudioPatch";
414 if (*patch == AUDIO_PATCH_HANDLE_NONE) { // always true for MAJOR_VERSION < 6
415 ret = mDevice->createAudioPatch(
416 hidlSources, hidlSinks,
417 [&](Result r, AudioPatchHandle hidlPatch) {
418 retval = r;
419 if (retval == Result::OK) {
420 *patch = static_cast<audio_patch_handle_t>(hidlPatch);
421 }
422 });
423 } else {
424 #if MAJOR_VERSION >= 6
425 ret = mDevice->updateAudioPatch(
426 *patch,
427 hidlSources, hidlSinks,
428 [&](Result r, AudioPatchHandle hidlPatch) {
429 retval = r;
430 if (retval == Result::OK) {
431 *patch = static_cast<audio_patch_handle_t>(hidlPatch);
432 }
433 });
434 methodName = "updateAudioPatch";
435 #endif
436 }
437 return processReturn(methodName.c_str(), ret, retval);
438 }
439
releaseAudioPatch(audio_patch_handle_t patch)440 status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) {
441 TIME_CHECK();
442 if (mDevice == 0) return NO_INIT;
443 return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch));
444 }
445
446 template <typename HalPort>
getAudioPortImpl(HalPort * port)447 status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) {
448 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort;
449 if (mDevice == 0) return NO_INIT;
450 AudioPort hidlPort;
451 HidlUtils::audioPortFromHal(*port, &hidlPort);
452 Result retval;
453 Return<void> ret = mDevice->getAudioPort(
454 hidlPort,
455 [&](Result r, const AudioPort& p) {
456 retval = r;
457 if (retval == Result::OK) {
458 HidlUtils::audioPortToHal(p, port);
459 }
460 });
461 return processReturn("getAudioPort", ret, retval);
462 }
463
getAudioPort(struct audio_port * port)464 status_t DeviceHalHidl::getAudioPort(struct audio_port *port) {
465 TIME_CHECK();
466 return getAudioPortImpl(port);
467 }
468
getAudioPort(struct audio_port_v7 * port)469 status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) {
470 TIME_CHECK();
471 #if MAJOR_VERSION >= 7
472 return getAudioPortImpl(port);
473 #else
474 struct audio_port audioPort = {};
475 status_t result = NO_ERROR;
476 if (!audio_populate_audio_port(port, &audioPort)) {
477 ALOGE("Failed to populate legacy audio port from audio_port_v7");
478 result = BAD_VALUE;
479 }
480 status_t status = getAudioPort(&audioPort);
481 if (status == NO_ERROR) {
482 audio_populate_audio_port_v7(&audioPort, port);
483 } else {
484 result = status;
485 }
486 return result;
487 #endif
488 }
489
setAudioPortConfig(const struct audio_port_config * config)490 status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) {
491 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortConfig;
492 TIME_CHECK();
493 if (mDevice == 0) return NO_INIT;
494 AudioPortConfig hidlConfig;
495 HidlUtils::audioPortConfigFromHal(*config, &hidlConfig);
496 return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig));
497 }
498
499 #if MAJOR_VERSION == 2
getMicrophones(std::vector<audio_microphone_characteristic_t> * microphonesInfo __unused)500 status_t DeviceHalHidl::getMicrophones(
501 std::vector<audio_microphone_characteristic_t> *microphonesInfo __unused) {
502 if (mDevice == 0) return NO_INIT;
503 return INVALID_OPERATION;
504 }
505 #elif MAJOR_VERSION >= 4
getMicrophones(std::vector<audio_microphone_characteristic_t> * microphonesInfo)506 status_t DeviceHalHidl::getMicrophones(
507 std::vector<audio_microphone_characteristic_t> *microphonesInfo) {
508 TIME_CHECK();
509 if (mDevice == 0) return NO_INIT;
510 Result retval;
511 Return<void> ret = mDevice->getMicrophones(
512 [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) {
513 retval = r;
514 for (size_t k = 0; k < micArrayHal.size(); k++) {
515 audio_microphone_characteristic_t dst;
516 //convert
517 (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst);
518 microphonesInfo->push_back(dst);
519 }
520 });
521 return processReturn("getMicrophones", ret, retval);
522 }
523 #endif
524
525 #if MAJOR_VERSION >= 6
addDeviceEffect(const struct audio_port_config * device,sp<EffectHalInterface> effect)526 status_t DeviceHalHidl::addDeviceEffect(
527 const struct audio_port_config *device, sp<EffectHalInterface> effect) {
528 TIME_CHECK();
529 if (mDevice == 0) return NO_INIT;
530 auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect);
531 return processReturn("addDeviceEffect", mDevice->addDeviceEffect(
532 static_cast<AudioPortHandle>(device->id), hidlEffect->effectId()));
533 }
534 #else
addDeviceEffect(const struct audio_port_config * device __unused,sp<EffectHalInterface> effect __unused)535 status_t DeviceHalHidl::addDeviceEffect(
536 const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) {
537 return INVALID_OPERATION;
538 }
539 #endif
540
541 #if MAJOR_VERSION >= 6
removeDeviceEffect(const struct audio_port_config * device,sp<EffectHalInterface> effect)542 status_t DeviceHalHidl::removeDeviceEffect(
543 const struct audio_port_config *device, sp<EffectHalInterface> effect) {
544 TIME_CHECK();
545 if (mDevice == 0) return NO_INIT;
546 auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect);
547 return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect(
548 static_cast<AudioPortHandle>(device->id), hidlEffect->effectId()));
549 }
550 #else
removeDeviceEffect(const struct audio_port_config * device __unused,sp<EffectHalInterface> effect __unused)551 status_t DeviceHalHidl::removeDeviceEffect(
552 const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) {
553 return INVALID_OPERATION;
554 }
555 #endif
556
prepareToDisconnectExternalDevice(const struct audio_port_v7 * port)557 status_t DeviceHalHidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) {
558 // For HIDL HAL, there is not API to call notify the HAL to prepare for device connected
559 // state changed. Call `setConnectedState` directly.
560 const status_t status = setConnectedState(port, false /*connected*/);
561 if (status == NO_ERROR) {
562 // Cache the port id so that it won't disconnect twice.
563 mDeviceDisconnectionNotified.insert(port->id);
564 }
565 return status;
566 }
567
setConnectedState(const struct audio_port_v7 * port,bool connected)568 status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) {
569 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort;
570 TIME_CHECK();
571 if (mDevice == 0) return NO_INIT;
572 if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) {
573 // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` and
574 // then call `setConnectedState`. However, in HIDL HAL, there is no API for
575 // `prepareToDisconnectExternalDevice`. In that case, HIDL HAL will call `setConnectedState`
576 // when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous
577 // call is successful. Also remove the cache here to avoid a large cache after a long run.
578 return NO_ERROR;
579 }
580 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
581 if (supportsSetConnectedState7_1) {
582 AudioPort hidlPort;
583 if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) {
584 return result;
585 }
586 Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected);
587 if (!ret.isOk() || ret != Result::NOT_SUPPORTED) {
588 return processReturn("setConnectedState_7_1", ret);
589 } else if (ret == Result::OK) {
590 return NO_ERROR;
591 }
592 supportsSetConnectedState7_1 = false;
593 }
594 #endif
595 DeviceAddress hidlAddress;
596 if (status_t result = CoreUtils::deviceAddressFromHal(
597 port->ext.device.type, port->ext.device.address, &hidlAddress);
598 result != NO_ERROR) {
599 return result;
600 }
601 return processReturn("setConnectedState", mDevice->setConnectedState(hidlAddress, connected));
602 }
603
getHwAvSync()604 error::Result<audio_hw_sync_t> DeviceHalHidl::getHwAvSync() {
605 TIME_CHECK();
606 if (mDevice == 0) return NO_INIT;
607 audio_hw_sync_t value;
608 Result result;
609 Return<void> ret = mDevice->getHwAvSync([&value, &result](Result r, audio_hw_sync_t v) {
610 value = v;
611 result = r;
612 });
613 RETURN_IF_ERROR(processReturn("getHwAvSync", ret, result));
614 return value;
615 }
616
dump(int fd,const Vector<String16> & args)617 status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) {
618 TIME_CHECK();
619 if (mDevice == 0) return NO_INIT;
620 native_handle_t* hidlHandle = native_handle_create(1, 0);
621 hidlHandle->data[0] = fd;
622 hidl_vec<hidl_string> hidlArgs;
623 argsFromHal(args, &hidlArgs);
624 Return<void> ret = mDevice->debug(hidlHandle, hidlArgs);
625 native_handle_delete(hidlHandle);
626
627 // TODO(b/111997867, b/177271958) Workaround - remove when fixed.
628 // A Binder transmitted fd may not close immediately due to a race condition b/111997867
629 // when the remote binder thread removes the last refcount to the fd blocks in the
630 // kernel for binder activity. We send a Binder ping() command to unblock the thread
631 // and complete the fd close / release.
632 //
633 // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(),
634 // EffectsFactoryHalHidl::dumpEffects().
635
636 (void)mDevice->ping(); // synchronous Binder call
637
638 return processReturn("dump", ret);
639 }
640
641 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
getSoundDoseInterface(const std::string & module,::ndk::SpAIBinder * soundDoseBinder)642 status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module,
643 ::ndk::SpAIBinder* soundDoseBinder) {
644 if (mSoundDoseWrapper->mSoundDose != nullptr) {
645 *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder();
646 return OK;
647 }
648
649 if (mSoundDoseWrapper->mSoundDoseFactory == nullptr) {
650 std::string interface =
651 std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data();
652 AIBinder* binder = AServiceManager_checkService(interface.c_str());
653 if (binder == nullptr) {
654 ALOGW("%s service %s doesn't exist", __func__, interface.c_str());
655 return NO_INIT;
656 }
657 mSoundDoseWrapper->mSoundDoseFactory =
658 ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder));
659 }
660
661 auto result = mSoundDoseWrapper->mSoundDoseFactory->getSoundDose(
662 module, &mSoundDoseWrapper->mSoundDose);
663 if (!result.isOk()) {
664 ALOGW("%s could not get sound dose interface: %s", __func__, result.getMessage());
665 return BAD_VALUE;
666 }
667
668 if (mSoundDoseWrapper->mSoundDose == nullptr) {
669 ALOGW("%s standalone sound dose interface is not implemented", __func__);
670 *soundDoseBinder = nullptr;
671 return OK;
672 }
673
674 *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder();
675 ALOGI("%s using standalone sound dose interface", __func__);
676 return OK;
677 }
678 #else
getSoundDoseInterface(const std::string & module,::ndk::SpAIBinder * soundDoseBinder)679 status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module,
680 ::ndk::SpAIBinder* soundDoseBinder) {
681 (void)module; // avoid unused param
682 (void)soundDoseBinder; // avoid unused param
683 return INVALID_OPERATION;
684 }
685 #endif
686
supportsBluetoothVariableLatency(bool * supports)687 status_t DeviceHalHidl::supportsBluetoothVariableLatency(bool* supports) {
688 if (supports == nullptr) {
689 return BAD_VALUE;
690 }
691 *supports = false;
692
693 String8 reply;
694 status_t status = getParameters(
695 String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), &reply);
696 if (status != NO_ERROR) {
697 return status;
698 }
699 AudioParameter replyParams(reply);
700 String8 trueOrFalse;
701 status = replyParams.get(
702 String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), trueOrFalse);
703 if (status != NO_ERROR) {
704 return status;
705 }
706 *supports = trueOrFalse == AudioParameter::valueTrue;
707 return NO_ERROR;
708 }
709 } // namespace android
710