From a5c0437327e30d22b450922310f40935353ff2da Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Fri, 26 Jan 2024 16:19:45 -0800 Subject: [PATCH] audio: Add retries for BT proxy port registration When ModuleBluetooth::createProxy is invoked on BT device connection, the BT stack may not be fully ready yet, and port registration can fail. This is an intermittent state and registration should succeed after retrying. Bug: 320838889 Test: atest pts-bot:A2DP/SRC/REL/BV-02-I Change-Id: I0c7cf7c1c6a8ee03ef55b004f89139e3b56ee9cd --- audio/aidl/default/bluetooth/ModuleBluetooth.cpp | 8 +++++++- audio/aidl/default/include/core-impl/ModuleBluetooth.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/audio/aidl/default/bluetooth/ModuleBluetooth.cpp b/audio/aidl/default/bluetooth/ModuleBluetooth.cpp index 9084b3044c..ac375a069e 100644 --- a/audio/aidl/default/bluetooth/ModuleBluetooth.cpp +++ b/audio/aidl/default/bluetooth/ModuleBluetooth.cpp @@ -299,7 +299,13 @@ ndk::ScopedAStatus ModuleBluetooth::createProxy(const AudioPort& audioPort, int3 : std::shared_ptr( std::make_shared()); const auto& devicePort = audioPort.ext.get(); - if (const auto device = devicePort.device.type; !proxy.ptr->registerPort(device)) { + const auto device = devicePort.device.type; + bool registrationSuccess = false; + for (int i = 0; i < kCreateProxyRetries && !registrationSuccess; ++i) { + registrationSuccess = proxy.ptr->registerPort(device); + usleep(kCreateProxyRetrySleepMs * 1000); + } + if (!registrationSuccess) { LOG(ERROR) << __func__ << ": failed to register BT port for " << device.toString(); return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); } diff --git a/audio/aidl/default/include/core-impl/ModuleBluetooth.h b/audio/aidl/default/include/core-impl/ModuleBluetooth.h index 9451411f48..b95b52699b 100644 --- a/audio/aidl/default/include/core-impl/ModuleBluetooth.h +++ b/audio/aidl/default/include/core-impl/ModuleBluetooth.h @@ -85,6 +85,8 @@ class ModuleBluetooth final : public Module { ndk::ScopedAStatus findOrCreateProxy( const ::aidl::android::media::audio::common::AudioPort& audioPort, CachedProxy& proxy); + static constexpr int kCreateProxyRetries = 5; + static constexpr int kCreateProxyRetrySleepMs = 250; ChildInterface mBluetoothA2dp; ChildInterface mBluetoothLe; std::map mProxies;