Merge "Disable UWB before starting VtsHalUwbTargetTest" into 24D1-dev
diff --git a/audio/aidl/default/bluetooth/ModuleBluetooth.cpp b/audio/aidl/default/bluetooth/ModuleBluetooth.cpp
index ac375a0..8c381cd 100644
--- a/audio/aidl/default/bluetooth/ModuleBluetooth.cpp
+++ b/audio/aidl/default/bluetooth/ModuleBluetooth.cpp
@@ -224,31 +224,19 @@
     const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
     const auto& description = devicePort.device.type;
     // This method must return an error when the device can not be connected.
-    if (description.connection == AudioDeviceDescription::CONNECTION_BT_A2DP) {
-        bool isA2dpEnabled = false;
-        if (!!mBluetoothA2dp) {
-            RETURN_STATUS_IF_ERROR((*mBluetoothA2dp).isEnabled(&isA2dpEnabled));
-        }
-        LOG(DEBUG) << __func__ << ": isA2dpEnabled: " << isA2dpEnabled;
-        if (!isA2dpEnabled) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
-    } else if (description.connection == AudioDeviceDescription::CONNECTION_BT_LE) {
-        bool isLeEnabled = false;
-        if (!!mBluetoothLe) {
-            RETURN_STATUS_IF_ERROR((*mBluetoothLe).isEnabled(&isLeEnabled));
-        }
-        LOG(DEBUG) << __func__ << ": isLeEnabled: " << isLeEnabled;
-        if (!isLeEnabled) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
-    } else if (description.connection == AudioDeviceDescription::CONNECTION_WIRELESS &&
-               description.type == AudioDeviceType::OUT_HEARING_AID) {
-        // Hearing aids can use a number of profiles, no single switch exists.
-    } else {
+    // Since A2DP/LE status events are sent asynchronously, it is more reliable
+    // to attempt connecting to the BT stack rather than judge by the A2DP/LE status.
+    if (description.connection != AudioDeviceDescription::CONNECTION_BT_A2DP &&
+        description.connection != AudioDeviceDescription::CONNECTION_BT_LE &&
+        !(description.connection == AudioDeviceDescription::CONNECTION_WIRELESS &&
+          description.type == AudioDeviceType::OUT_HEARING_AID)) {
         LOG(ERROR) << __func__ << ": unsupported device type: " << audioPort->toString();
         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
     }
     CachedProxy proxy;
     RETURN_STATUS_IF_ERROR(createProxy(*audioPort, nextPortId, proxy));
-    // Since the device is already connected and configured by the BT stack, provide
-    // the current configuration instead of all possible profiles.
+    // If the device is actually connected, it is configured by the BT stack.
+    // Provide the current configuration instead of all possible profiles.
     const auto& pcmConfig = proxy.pcmConfig;
     audioPort->profiles.clear();
     audioPort->profiles.push_back(
diff --git a/audio/aidl/default/include/core-impl/StreamRemoteSubmix.h b/audio/aidl/default/include/core-impl/StreamRemoteSubmix.h
index b2cdc28..0d50c96 100644
--- a/audio/aidl/default/include/core-impl/StreamRemoteSubmix.h
+++ b/audio/aidl/default/include/core-impl/StreamRemoteSubmix.h
@@ -55,8 +55,8 @@
     r_submix::AudioConfig mStreamConfig;
     std::shared_ptr<r_submix::SubmixRoute> mCurrentRoute = nullptr;
 
-    // limit for number of read error log entries to avoid spamming the logs
-    static constexpr int kMaxReadErrorLogs = 5;
+    // Limit for the number of error log entries to avoid spamming the logs.
+    static constexpr int kMaxErrorLogs = 5;
     // The duration of kMaxReadFailureAttempts * READ_ATTEMPT_SLEEP_MS must be strictly inferior
     // to the duration of a record buffer at the current record sample rate (of the device, not of
     // the recording itself). Here we have: 3 * 5ms = 15ms < 1024 frames * 1000 / 48000 = 21.333ms
@@ -68,6 +68,7 @@
     long mFramesSinceStart = 0;
     int mReadErrorCount = 0;
     int mReadFailureCount = 0;
+    int mWriteShutdownCount = 0;
 };
 
 class StreamInRemoteSubmix final : public StreamIn, public StreamSwitcher {
diff --git a/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp b/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
index 2376ed9..ca3f91a 100644
--- a/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
+++ b/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
@@ -134,11 +134,16 @@
     *latencyMs = getDelayInUsForFrameCount(getStreamPipeSizeInFrames()) / 1000;
     LOG(VERBOSE) << __func__ << ": Latency " << *latencyMs << "ms";
     mCurrentRoute->exitStandby(mIsInput);
-    RETURN_STATUS_IF_ERROR(mIsInput ? inRead(buffer, frameCount, actualFrameCount)
-                                    : outWrite(buffer, frameCount, actualFrameCount));
+    ::android::status_t status = mIsInput ? inRead(buffer, frameCount, actualFrameCount)
+                                          : outWrite(buffer, frameCount, actualFrameCount);
+    if ((status != ::android::OK && mIsInput) ||
+        ((status != ::android::OK && status != ::android::DEAD_OBJECT) && !mIsInput)) {
+        return status;
+    }
     mFramesSinceStart += *actualFrameCount;
-    if (!mIsInput) return ::android::OK;
-    // Only input streams need to block, for output this is implemented by MonoPipe.
+    if (!mIsInput && status != ::android::DEAD_OBJECT) return ::android::OK;
+    // Input streams always need to block, output streams need to block when there is no sink.
+    // When the sink exists, more sophisticated blocking algorithm is implemented by MonoPipe.
     const long bufferDurationUs =
             (*actualFrameCount) * MICROS_PER_SECOND / mContext.getSampleRate();
     const auto totalDurationUs = (::android::uptimeNanos() - mStartTimeNs) / NANOS_PER_MICROSECOND;
@@ -188,14 +193,17 @@
     if (sink != nullptr) {
         if (sink->isShutdown()) {
             sink.clear();
-            LOG(DEBUG) << __func__ << ": pipe shutdown, ignoring the write";
+            if (++mWriteShutdownCount < kMaxErrorLogs) {
+                LOG(DEBUG) << __func__ << ": pipe shutdown, ignoring the write. (limited logging)";
+            }
             *actualFrameCount = frameCount;
-            return ::android::OK;
+            return ::android::DEAD_OBJECT;  // Induce wait in `transfer`.
         }
     } else {
         LOG(FATAL) << __func__ << ": without a pipe!";
         return ::android::UNKNOWN_ERROR;
     }
+    mWriteShutdownCount = 0;
 
     LOG(VERBOSE) << __func__ << ": " << mDeviceAddress.toString() << ", " << frameCount
                  << " frames";
@@ -262,7 +270,7 @@
     // about to read from audio source
     sp<MonoPipeReader> source = mCurrentRoute->getSource();
     if (source == nullptr) {
-        if (++mReadErrorCount < kMaxReadErrorLogs) {
+        if (++mReadErrorCount < kMaxErrorLogs) {
             LOG(ERROR) << __func__
                        << ": no audio pipe yet we're trying to read! (not all errors will be "
                           "logged)";
diff --git a/power/aidl/vts/VtsHalPowerTargetTest.cpp b/power/aidl/vts/VtsHalPowerTargetTest.cpp
index 53fcef1..272674f 100644
--- a/power/aidl/vts/VtsHalPowerTargetTest.cpp
+++ b/power/aidl/vts/VtsHalPowerTargetTest.cpp
@@ -122,10 +122,16 @@
         power = IPower::fromBinder(ndk::SpAIBinder(binder));
         auto status = power->getInterfaceVersion(&mServiceVersion);
         ASSERT_TRUE(status.isOk());
+        if (mServiceVersion >= 2) {
+            status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &mSession);
+            mSessionSupport = status.isOk();
+        }
     }
 
     std::shared_ptr<IPower> power;
     int32_t mServiceVersion;
+    std::shared_ptr<IPowerHintSession> mSession;
+    bool mSessionSupport = false;
 };
 
 class HintSessionAidl : public PowerAidl {
@@ -135,12 +141,11 @@
         if (mServiceVersion < 2) {
             GTEST_SKIP() << "DEVICE not launching with Power V2 and beyond.";
         }
-
-        auto status = power->createHintSession(getpid(), getuid(), kSelfTids, 16666666L, &mSession);
-        ASSERT_TRUE(status.isOk());
+        if (!mSessionSupport) {
+            GTEST_SKIP() << "DEVICE not support Hint Session.";
+        }
         ASSERT_NE(nullptr, mSession);
     }
-    std::shared_ptr<IPowerHintSession> mSession;
 };
 
 class FMQAidl : public PowerAidl {
@@ -154,7 +159,10 @@
         auto status =
                 power->createHintSessionWithConfig(getpid(), getuid(), kSelfTids, 16666666L,
                                                    SessionTag::OTHER, &mSessionConfig, &mSession);
-        ASSERT_TRUE(status.isOk());
+        mSessionSupport = status.isOk();
+        if (!mSessionSupport) {
+            GTEST_SKIP() << "DEVICE not support Hint Session.";
+        }
         ASSERT_NE(nullptr, mSession);
 
         status = power->getSessionChannel(getpid(), getuid(), &mChannelConfig);
@@ -175,8 +183,12 @@
         ASSERT_NE(mEventFlag, nullptr);
     }
     virtual void TearDown() {
-        mSession->close();
-        ASSERT_TRUE(power->closeSessionChannel(getpid(), getuid()).isOk());
+        if (mSession) {
+            mSession->close();
+            if (mChannel->isValid()) {
+                ASSERT_TRUE(power->closeSessionChannel(getpid(), getuid()).isOk());
+            }
+        }
     }
 
   protected:
@@ -239,6 +251,9 @@
 }
 
 TEST_P(PowerAidl, getHintSessionPreferredRate) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     if (mServiceVersion < 2) {
         GTEST_SKIP() << "DEVICE not launching with Power V2 and beyond.";
     }
@@ -250,6 +265,9 @@
 }
 
 TEST_P(PowerAidl, createHintSessionWithConfig) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     if (mServiceVersion < 5) {
         GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
     }
@@ -271,6 +289,9 @@
 }
 
 TEST_P(HintSessionAidl, createAndCloseHintSession) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     ASSERT_TRUE(mSession->pause().isOk());
     ASSERT_TRUE(mSession->resume().isOk());
     // Test normal destroy operation
@@ -279,6 +300,9 @@
 }
 
 TEST_P(HintSessionAidl, createHintSessionFailed) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     std::shared_ptr<IPowerHintSession> session;
     auto status = power->createHintSession(getpid(), getuid(), kEmptyTids, 16666666L, &session);
 
@@ -288,11 +312,17 @@
 }
 
 TEST_P(HintSessionAidl, updateAndReportDurations) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     ASSERT_TRUE(mSession->updateTargetWorkDuration(16666667LL).isOk());
     ASSERT_TRUE(mSession->reportActualWorkDuration(kDurations).isOk());
 }
 
 TEST_P(HintSessionAidl, sendSessionHint) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     if (mServiceVersion < 4) {
         GTEST_SKIP() << "DEVICE not launching with Power V4 and beyond.";
     }
@@ -306,6 +336,9 @@
 }
 
 TEST_P(HintSessionAidl, setThreads) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     if (mServiceVersion < 4) {
         GTEST_SKIP() << "DEVICE not launching with Power V4 and beyond.";
     }
@@ -318,6 +351,9 @@
 }
 
 TEST_P(HintSessionAidl, setSessionMode) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     if (mServiceVersion < 5) {
         GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
     }
@@ -333,6 +369,9 @@
 }
 
 TEST_P(HintSessionAidl, getSessionConfig) {
+    if (!mSessionSupport) {
+        GTEST_SKIP() << "DEVICE not support Hint Session.";
+    }
     if (mServiceVersion < 5) {
         GTEST_SKIP() << "DEVICE not launching with Power V5 and beyond.";
     }