Merge "Wait for HAL service death signal before trying to connect it again" into main
diff --git a/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp b/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
index cef0ea6..f8ead16 100644
--- a/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
+++ b/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
@@ -283,6 +283,15 @@
         }
         return ::android::OK;
     }
+    // get and hold the sink because 'MonoPipeReader' does not hold a strong pointer to it.
+    sp<MonoPipe> sink = mCurrentRoute->getSink();
+    if (sink == nullptr) {
+        if (++mReadErrorCount < kMaxErrorLogs) {
+            LOG(ERROR) << __func__
+                       << ": the sink has been released! (not all errors will be logged)";
+        }
+        return ::android::OK;
+    }
     mReadErrorCount = 0;
 
     LOG(VERBOSE) << __func__ << ": " << mDeviceAddress.toString() << ", " << frameCount
diff --git a/audio/aidl/vts/EffectHelper.h b/audio/aidl/vts/EffectHelper.h
index 5c75e48..e334ee9 100644
--- a/audio/aidl/vts/EffectHelper.h
+++ b/audio/aidl/vts/EffectHelper.h
@@ -88,6 +88,7 @@
 static constexpr int kNPointFFT = 16384;
 static constexpr int kSamplingFrequency = 44100;
 static constexpr int kDefaultChannelLayout = AudioChannelLayout::LAYOUT_STEREO;
+static constexpr float kLn10Div20 = -0.11512925f;  // -ln(10)/20
 
 class EffectHelper {
   public:
@@ -595,6 +596,8 @@
         }
     }
 
+    constexpr float dBToAmplitude(float dB) { return std::exp(dB * kLn10Div20); }
+
     static int getHalVersion(const std::shared_ptr<IEffect>& effect) {
         int version = 0;
         return (effect && effect->getInterfaceVersion(&version).isOk()) ? version : 0;
diff --git a/audio/aidl/vts/TestUtils.h b/audio/aidl/vts/TestUtils.h
index 3a6c137..9ebdc6e 100644
--- a/audio/aidl/vts/TestUtils.h
+++ b/audio/aidl/vts/TestUtils.h
@@ -124,3 +124,30 @@
     EXPECT_PRED_FORMAT2(                                                                           \
             ::android::hardware::audio::common::testing::detail::assertResultOrUnknownTransaction, \
             expected, ret)
+
+namespace android::hardware::audio::common::testing::detail {
+
+template <typename>
+struct mf_traits {};
+template <class T, class U>
+struct mf_traits<U T::*> {
+    using member_type = U;
+};
+
+}  // namespace android::hardware::audio::common::testing::detail
+
+namespace aidl::android::media::audio::common {
+
+template <typename P>
+std::enable_if_t<std::is_function_v<typename ::android::hardware::audio::common::testing::detail::
+                                            mf_traits<decltype(&P::toString)>::member_type>,
+                 std::ostream&>
+operator<<(std::ostream& os, const P& p) {
+    return os << p.toString();
+}
+template <typename E>
+std::enable_if_t<std::is_enum_v<E>, std::ostream&> operator<<(std::ostream& os, const E& e) {
+    return os << toString(e);
+}
+
+}  // namespace aidl::android::media::audio::common
diff --git a/audio/aidl/vts/VtsHalAudioCoreConfigTargetTest.cpp b/audio/aidl/vts/VtsHalAudioCoreConfigTargetTest.cpp
index eaec88b..7b15e5e 100644
--- a/audio/aidl/vts/VtsHalAudioCoreConfigTargetTest.cpp
+++ b/audio/aidl/vts/VtsHalAudioCoreConfigTargetTest.cpp
@@ -341,7 +341,7 @@
         auto values = criterionV2.values;
         auto valueIt = find_if(values.begin(), values.end(),
                                [&](const auto& typedValue) { return typedValue == value; });
-        EXPECT_NE(valueIt, values.end());
+        EXPECT_NE(valueIt, values.end()) << "Not found: \"" << value << "\"";
     }
 
     /**
diff --git a/audio/aidl/vts/VtsHalDynamicsProcessingTest.cpp b/audio/aidl/vts/VtsHalDynamicsProcessingTest.cpp
index 70790c4..0d4c74e 100644
--- a/audio/aidl/vts/VtsHalDynamicsProcessingTest.cpp
+++ b/audio/aidl/vts/VtsHalDynamicsProcessingTest.cpp
@@ -788,9 +788,10 @@
     void SetUp() override {
         SetUpDynamicsProcessingEffect();
         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
-        ASSERT_NO_FATAL_FAILURE(generateSineWave(1000 /*Input Frequency*/, mInput, 1.0,
-                                                 kSamplingFrequency, mChannelLayout));
+        ASSERT_NO_FATAL_FAILURE(
+                generateSineWave(kInputFrequency, mInput, 1.0, kSamplingFrequency, mChannelLayout));
         mInputDb = calculateDb(mInput);
+        ASSERT_NEAR(mInputDb, kSineFullScaleDb, kToleranceDb);
     }
 
     void TearDown() override { TearDownDynamicsProcessingEffect(); }
@@ -823,6 +824,9 @@
     static constexpr float kDefaultRatio = 4;
     static constexpr float kDefaultThreshold = -10;
     static constexpr float kDefaultPostGain = 0;
+    static constexpr float kInputFrequency = 1000;
+    // Full scale sine wave with 1000 Hz frequency is -3 dB
+    static constexpr float kSineFullScaleDb = -3;
     std::vector<DynamicsProcessing::LimiterConfig> mLimiterConfigList;
     std::vector<float> mInput;
     float mInputDb;
@@ -887,9 +891,13 @@
     std::vector<float> output(mInput.size());
     for (float postGainDb : postGainDbValues) {
         cleanUpLimiterConfig();
+        ASSERT_NO_FATAL_FAILURE(generateSineWave(kInputFrequency, mInput, dBToAmplitude(postGainDb),
+                                                 kSamplingFrequency, mChannelLayout));
+        mInputDb = calculateDb(mInput);
+        EXPECT_NEAR(mInputDb, kSineFullScaleDb - postGainDb, kToleranceDb);
         for (int i = 0; i < mChannelCount; i++) {
             fillLimiterConfig(mLimiterConfigList, i, true, kDefaultLinkerGroup, kDefaultAttackTime,
-                              kDefaultReleaseTime, kDefaultRatio, -1, postGainDb);
+                              kDefaultReleaseTime, 1, kDefaultThreshold, postGainDb);
         }
         ASSERT_NO_FATAL_FAILURE(setLimiterParamsAndProcess(mInput, output));
         if (!isAllParamsValid()) {
@@ -1255,6 +1263,7 @@
         ASSERT_NO_FATAL_FAILURE(generateSineWave(mTestFrequencies, mInput, 1.0, kSamplingFrequency,
                                                  mChannelLayout));
         mInputDb = calculateDb(mInput);
+        ASSERT_NEAR(mInputDb, kFullScaleDb, kToleranceDb);
     }
 
     void TearDown() override { TearDownDynamicsProcessingEffect(); }
@@ -1267,12 +1276,7 @@
         addEngineConfig(mEngineConfigPreset);
         addMbcChannelConfig(mChannelConfig);
         addMbcBandConfigs(mCfgs);
-
-        if (isAllParamsValid()) {
-            ASSERT_NO_FATAL_FAILURE(SetAndGetDynamicsProcessingParameters());
-            ASSERT_NO_FATAL_FAILURE(
-                    processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
-        }
+        ASSERT_NO_FATAL_FAILURE(setParamsAndProcess(mInput, output));
     }
 
     void fillMbcBandConfig(std::vector<DynamicsProcessing::MbcBandConfig>& cfgs, int channelIndex,
@@ -1387,9 +1391,9 @@
     std::vector<float> postGainDbValues = {-55, -30, 0, 30, 55};
     std::vector<float> output(mInput.size());
     for (float postGainDb : postGainDbValues) {
-        float amplitude = 1.0 / (pow(10, postGainDb / 20));
-        ASSERT_NO_FATAL_FAILURE(generateSineWave(mTestFrequencies, mInput, amplitude,
-                                                 kSamplingFrequency, mChannelLayout));
+        ASSERT_NO_FATAL_FAILURE(generateSineWave(mTestFrequencies, mInput,
+                                                 dBToAmplitude(postGainDb), kSamplingFrequency,
+                                                 mChannelLayout));
         mInputDb = calculateDb(mInput);
         EXPECT_NEAR(mInputDb, kFullScaleDb - postGainDb, kToleranceDb);
         cleanUpMbcConfig();
diff --git a/biometrics/fingerprint/aidl/default/main.cpp b/biometrics/fingerprint/aidl/default/main.cpp
index 8ca44d6..bf3f38e 100644
--- a/biometrics/fingerprint/aidl/default/main.cpp
+++ b/biometrics/fingerprint/aidl/default/main.cpp
@@ -39,8 +39,6 @@
         if (strcmp(argv[1], "default") == 0) {
             const std::string instance = std::string(Fingerprint::descriptor) + "/default";
             auto binder = hal->asBinder();
-            auto binder_ext = hal_vhal->asBinder();
-            CHECK(STATUS_OK == AIBinder_setExtension(binder.get(), binder_ext.get()));
             binder_status_t status =
                     AServiceManager_registerLazyService(binder.get(), instance.c_str());
             CHECK_EQ(status, STATUS_OK);
diff --git a/biometrics/fingerprint/aidl/vts/VtsHalBiometricsFingerprintTargetTest.cpp b/biometrics/fingerprint/aidl/vts/VtsHalBiometricsFingerprintTargetTest.cpp
index 1cd8c76..261ae20 100644
--- a/biometrics/fingerprint/aidl/vts/VtsHalBiometricsFingerprintTargetTest.cpp
+++ b/biometrics/fingerprint/aidl/vts/VtsHalBiometricsFingerprintTargetTest.cpp
@@ -138,6 +138,11 @@
 };
 
 class Fingerprint : public testing::TestWithParam<std::string> {
+    static void HalServiceDied(void* cookie) {
+        auto halDeathPromise = static_cast<std::promise<void>*>(cookie);
+        halDeathPromise->set_value();
+    }
+
   protected:
     void SetUp() override {
         // Prepare the callback.
@@ -157,8 +162,24 @@
             ASSERT_NE(binder, nullptr);
             mHal = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
 
+            // Create HAL service death notifier
+            auto halDeathPromise = std::make_shared<std::promise<void>>();
+            mHalDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(
+                    AIBinder_DeathRecipient_new(&HalServiceDied));
+            ASSERT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, mHalDeathRecipient.get(),
+                                                      static_cast<void*>(halDeathPromise.get())));
+
             // Create a session.
             isOk = mHal->createSession(kSensorId, kUserId, mCb, &mSession).isOk();
+
+            if (!isOk) {
+                // Failed to create session on first attempt, it is likely that the HAL service
+                //   is dying or dead. Wait for its death notification signal before next try
+                auto future = halDeathPromise->get_future();
+                auto status = future.wait_for(std::chrono::milliseconds(500));
+                EXPECT_EQ(status, std::future_status::ready);
+            }
+
             ++retries;
         } while (!isOk && retries < 2);
 
@@ -177,6 +198,7 @@
     std::shared_ptr<IFingerprint> mHal;
     std::shared_ptr<SessionCallback> mCb;
     std::shared_ptr<ISession> mSession;
+    ::ndk::ScopedAIBinder_DeathRecipient mHalDeathRecipient;
 };
 
 TEST_P(Fingerprint, GetSensorPropsWorksTest) {
diff --git a/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.cpp b/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.cpp
index 3e18de2..2474916 100644
--- a/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.cpp
+++ b/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.cpp
@@ -394,7 +394,8 @@
 void AudioSetConfigurationProviderJson::populateAseConfiguration(
     const std::string& name, LeAudioAseConfiguration& ase,
     const le_audio::AudioSetSubConfiguration* flat_subconfig,
-    const le_audio::QosConfiguration* qos_cfg) {
+    const le_audio::QosConfiguration* qos_cfg,
+    ConfigurationFlags& configurationFlags) {
   // Target latency
   switch (qos_cfg->target_latency()) {
     case le_audio::AudioSetConfigurationTargetLatency::
@@ -410,6 +411,7 @@
     case le_audio::AudioSetConfigurationTargetLatency::
         AudioSetConfigurationTargetLatency_LOW:
       ase.targetLatency = LeAudioAseConfiguration::TargetLatency::LOWER;
+      configurationFlags.bitmask |= ConfigurationFlags::LOW_LATENCY;
       break;
     default:
       ase.targetLatency = LeAudioAseConfiguration::TargetLatency::UNDEFINED;
@@ -507,7 +509,8 @@
 AudioSetConfigurationProviderJson::SetConfigurationFromFlatSubconfig(
     const std::string& name,
     const le_audio::AudioSetSubConfiguration* flat_subconfig,
-    const le_audio::QosConfiguration* qos_cfg, CodecLocation location) {
+    const le_audio::QosConfiguration* qos_cfg, CodecLocation location,
+    ConfigurationFlags& configurationFlags) {
   AseDirectionConfiguration direction_conf;
 
   LeAudioAseConfiguration ase;
@@ -515,7 +518,8 @@
   LeAudioDataPathConfiguration path;
 
   // Translate into LeAudioAseConfiguration
-  populateAseConfiguration(name, ase, flat_subconfig, qos_cfg);
+  populateAseConfiguration(name, ase, flat_subconfig, qos_cfg,
+                           configurationFlags);
 
   // Translate into LeAudioAseQosConfiguration
   populateAseQosConfiguration(qos, qos_cfg, ase,
@@ -554,10 +558,10 @@
     const le_audio::QosConfiguration* qos_cfg,
     std::vector<std::optional<AseDirectionConfiguration>>&
         directionAseConfiguration,
-    CodecLocation location) {
+    CodecLocation location, ConfigurationFlags& configurationFlags) {
   auto ase_cnt = subconfig->ase_cnt();
-  auto config =
-      SetConfigurationFromFlatSubconfig(name, subconfig, qos_cfg, location);
+  auto config = SetConfigurationFromFlatSubconfig(name, subconfig, qos_cfg,
+                                                  location, configurationFlags);
   directionAseConfiguration.push_back(config);
   // Put the same setting again.
   if (ase_cnt == 2) directionAseConfiguration.push_back(config);
@@ -643,10 +647,10 @@
     for (auto subconfig : *codec_cfg->subconfigurations()) {
       if (subconfig->direction() == kLeAudioDirectionSink) {
         processSubconfig(flat_cfg->name()->str(), subconfig, qos_sink_cfg,
-                         sinkAseConfiguration, location);
+                         sinkAseConfiguration, location, configurationFlags);
       } else {
         processSubconfig(flat_cfg->name()->str(), subconfig, qos_source_cfg,
-                         sourceAseConfiguration, location);
+                         sourceAseConfiguration, location, configurationFlags);
       }
     }
 
diff --git a/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.h b/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.h
index fac6152..8e12618 100644
--- a/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.h
+++ b/bluetooth/audio/utils/aidl_session/BluetoothLeAudioAseConfigurationSettingProvider.h
@@ -75,7 +75,8 @@
   static void populateAseConfiguration(
       const std::string& name, LeAudioAseConfiguration& ase,
       const le_audio::AudioSetSubConfiguration* flat_subconfig,
-      const le_audio::QosConfiguration* qos_cfg);
+      const le_audio::QosConfiguration* qos_cfg,
+      ConfigurationFlags& configurationFlags);
 
   static void populateAseQosConfiguration(
       LeAudioAseQosConfiguration& qos,
@@ -85,7 +86,8 @@
   static AseDirectionConfiguration SetConfigurationFromFlatSubconfig(
       const std::string& name,
       const le_audio::AudioSetSubConfiguration* flat_subconfig,
-      const le_audio::QosConfiguration* qos_cfg, CodecLocation location);
+      const le_audio::QosConfiguration* qos_cfg, CodecLocation location,
+      ConfigurationFlags& configurationFlags);
 
   static void processSubconfig(
       const std::string& name,
@@ -93,7 +95,7 @@
       const le_audio::QosConfiguration* qos_cfg,
       std::vector<std::optional<AseDirectionConfiguration>>&
           directionAseConfiguration,
-      CodecLocation location);
+      CodecLocation location, ConfigurationFlags& configurationFlags);
 
   static void PopulateAseConfigurationFromFlat(
       const le_audio::AudioSetConfiguration* flat_cfg,
diff --git a/bluetooth/ranging/aidl/default/BluetoothChannelSounding.cpp b/bluetooth/ranging/aidl/default/BluetoothChannelSounding.cpp
index e2a8693..4aba874 100644
--- a/bluetooth/ranging/aidl/default/BluetoothChannelSounding.cpp
+++ b/bluetooth/ranging/aidl/default/BluetoothChannelSounding.cpp
@@ -58,7 +58,8 @@
 
 ndk::ScopedAStatus BluetoothChannelSounding::getSupportedCsSecurityLevels(
     std::vector<CsSecurityLevel>* _aidl_return) {
-  std::vector<CsSecurityLevel> supported_security_levels = {};
+  std::vector<CsSecurityLevel> supported_security_levels = {
+      CsSecurityLevel::NOT_SUPPORTED};
   *_aidl_return = supported_security_levels;
   return ::ndk::ScopedAStatus::ok();
 }
diff --git a/security/secretkeeper/aidl/vts/Android.bp b/security/secretkeeper/aidl/vts/Android.bp
index c84afae..4353ca5 100644
--- a/security/secretkeeper/aidl/vts/Android.bp
+++ b/security/secretkeeper/aidl/vts/Android.bp
@@ -27,6 +27,7 @@
         "libciborium",
         "libcoset",
         "libdiced_open_dice",
+        "libexplicitkeydice",
         "libhex",
         "liblog_rust",
         "libsecretkeeper_client",
@@ -54,6 +55,7 @@
         "libciborium",
         "libcoset",
         "libdice_policy_builder",
+        "libexplicitkeydice",
         "liblog_rust",
         "libsecretkeeper_client",
         "libsecretkeeper_comm_nostd",
@@ -77,6 +79,7 @@
         "libclap",
         "libcoset",
         "libdice_policy_builder",
+        "libexplicitkeydice",
         "libhex",
         "liblog_rust",
         "libsecretkeeper_client",
diff --git a/security/secretkeeper/aidl/vts/dice_sample.rs b/security/secretkeeper/aidl/vts/dice_sample.rs
index d6379e5..f504445 100644
--- a/security/secretkeeper/aidl/vts/dice_sample.rs
+++ b/security/secretkeeper/aidl/vts/dice_sample.rs
@@ -34,8 +34,8 @@
     retry_bcc_main_flow, retry_dice_main_flow, Config, DiceArtifacts, DiceConfigValues, DiceError,
     DiceMode, InputValues, OwnedDiceArtifacts, HASH_SIZE, HIDDEN_SIZE,
 };
+use explicitkeydice::OwnedDiceArtifactsWithExplicitKey;
 use log::error;
-use secretkeeper_client::dice::OwnedDiceArtifactsWithExplicitKey;
 
 /// Sample UDS used to perform the root DICE flow by `make_sample_bcc_and_cdis`.
 const UDS: &[u8; CDI_SIZE] = &[
diff --git a/security/secretkeeper/aidl/vts/secretkeeper_cli.rs b/security/secretkeeper/aidl/vts/secretkeeper_cli.rs
index 9fbfb45..6a743a8 100644
--- a/security/secretkeeper/aidl/vts/secretkeeper_cli.rs
+++ b/security/secretkeeper/aidl/vts/secretkeeper_cli.rs
@@ -29,7 +29,8 @@
     WILDCARD_FULL_ARRAY,
 };
 
-use secretkeeper_client::{dice::OwnedDiceArtifactsWithExplicitKey, SkSession};
+use explicitkeydice::OwnedDiceArtifactsWithExplicitKey;
+use secretkeeper_client::SkSession;
 use secretkeeper_comm::data_types::{
     error::SecretkeeperError,
     packet::{ResponsePacket, ResponseType},
diff --git a/security/secretkeeper/aidl/vts/secretkeeper_test_client.rs b/security/secretkeeper/aidl/vts/secretkeeper_test_client.rs
index b944865..453ff8f 100644
--- a/security/secretkeeper/aidl/vts/secretkeeper_test_client.rs
+++ b/security/secretkeeper/aidl/vts/secretkeeper_test_client.rs
@@ -22,8 +22,8 @@
 use authgraph_core::key;
 use coset::{CborOrdering, CborSerializable, CoseEncrypt0, CoseKey};
 use dice_policy_builder::{TargetEntry, ConstraintSpec, ConstraintType, MissingAction, WILDCARD_FULL_ARRAY, policy_for_dice_chain};
+use explicitkeydice::OwnedDiceArtifactsWithExplicitKey;
 use rdroidtest::{ignore_if, rdroidtest};
-use secretkeeper_client::dice::OwnedDiceArtifactsWithExplicitKey;
 use secretkeeper_client::{SkSession, Error as SkClientError};
 use secretkeeper_core::cipher;
 use secretkeeper_comm::data_types::error::SecretkeeperError;