Merge tag 'android-14.0.0_r50' into leaf-3.2

Android 14.0.0 Release 50 (AP2A.240605.024)

* tag 'android-14.0.0_r50':
  LINT: remove space from lint declaration
  Add debug logs to enable dynamic sensor
  Fix incorrect error log in sensors hidraw daemon
  Use LLNDK guard instead of builtin_available
  Set aconfig container to "system"
  Support 192kHz remote submix paths for E-AC-3(-JOC) IEC 61937
  Call disconnect callback when a dynamic sensor disconnects
  Add flag for disconnect handling
  Refactor HidRawSensor::enable

Change-Id: I2c2dbf72de80a182e54cb113848826f247014b68
diff --git a/hardware.c b/hardware.c
index 7907331..94b5d5d 100644
--- a/hardware.c
+++ b/hardware.c
@@ -179,20 +179,20 @@
                             const char *subname)
 {
 #ifdef __ANDROID_APEX__
-    // When used in APEX, it should look only into the same APEX because
+    // When used in VAPEX, it should look only into the same APEX because
     // libhardware modules don't provide ABI stability.
-    if (__builtin_available(android AAPEXSUPPORT_API, *)) {
-        AApexInfo *apex_info;
-        if (AApexInfo_create(&apex_info) == AAPEXINFO_OK) {
-            snprintf(path, path_len, "/apex/%s/%s/%s.%s.so",
-                     AApexInfo_getName(apex_info), HAL_LIBRARY_SUBDIR, name, subname);
-            AApexInfo_destroy(apex_info);
-            if (access(path, R_OK) == 0)
-                return 0;
-        }
-    } else {
-        ALOGE("hw_module_exists: libapexsupport is not supported in %d.", __ANDROID_API__);
+#if __ANDROID_VENDOR_API__ >= 202404
+    AApexInfo *apex_info;
+    if (AApexInfo_create(&apex_info) == AAPEXINFO_OK) {
+        snprintf(path, path_len, "/apex/%s/%s/%s.%s.so",
+                 AApexInfo_getName(apex_info), HAL_LIBRARY_SUBDIR, name, subname);
+        AApexInfo_destroy(apex_info);
+        if (access(path, R_OK) == 0)
+            return 0;
     }
+#else  // __ANDROID_VENDOR_API__
+    ALOGE("hw_module_exists: libapexsupport is not supported in %d.", __ANDROID_VENDOR_API__);
+#endif // __ANDROID_VENDOR_API__
 #else // __ANDROID_APEX__
     snprintf(path, path_len, "%s/%s.%s.so",
              HAL_LIBRARY_PATH3, name, subname);
diff --git a/include_all/hardware/sensors.h b/include_all/hardware/sensors.h
index 7b844b8..5f490b5 100644
--- a/include_all/hardware/sensors.h
+++ b/include_all/hardware/sensors.h
@@ -481,7 +481,7 @@
 
     uint32_t reserved1[3];
 } sensors_event_t;
-// LINT.ThenChange (frameworks/native/include/android/sensor.h)
+// LINT.ThenChange(frameworks/native/include/android/sensor.h)
 
 
 /* see SENSOR_TYPE_META_DATA */
diff --git a/modules/audio_remote_submix/audio_hw.cpp b/modules/audio_remote_submix/audio_hw.cpp
index 5019ae5..d6c8c7b 100644
--- a/modules/audio_remote_submix/audio_hw.cpp
+++ b/modules/audio_remote_submix/audio_hw.cpp
@@ -199,9 +199,9 @@
 // Determine whether the specified sample rate is supported by the submix module.
 static bool sample_rate_supported(const uint32_t sample_rate)
 {
-    // Set of sample rates supported by Format_from_SR_C() frameworks/av/media/libnbaio/NAIO.cpp.
     static const unsigned int supported_sample_rates[] = {
         8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
+        192000 /* for IEC 61937 encapsulated E-AC-3(-JOC) */
     };
     bool return_value;
     SUBMIX_VALUE_IN_SET(sample_rate, supported_sample_rates, &return_value);
@@ -1240,6 +1240,10 @@
     *stream_out = NULL;
 
     // Make sure it's possible to open the device given the current audio config.
+    if (!audio_is_linear_pcm(config->format)) {
+        ALOGD("adev_open_output_stream(): not supported for audio format %#x", config->format);
+        return -EINVAL;
+    }
     submix_sanitize_config(config, false);
 
     int route_idx = -1;
@@ -1455,6 +1459,11 @@
 
     *stream_in = NULL;
 
+    if (!audio_is_linear_pcm(config->format)) {
+        ALOGD("adev_open_input_stream(): not supported for audio format %#x", config->format);
+        return -EINVAL;
+    }
+
     // Do we already have a route for this address
     int route_idx = -1;
 
diff --git a/modules/sensors/dynamic_sensor/Android.bp b/modules/sensors/dynamic_sensor/Android.bp
index b6e0020..1ede955 100644
--- a/modules/sensors/dynamic_sensor/Android.bp
+++ b/modules/sensors/dynamic_sensor/Android.bp
@@ -194,6 +194,7 @@
 aconfig_declarations {
     name: "dynamic_sensors_flags",
     package: "com.android.libhardware.dynamic.sensors.flags",
+    container: "system",
     srcs: ["dynamic_sensors.aconfig"],
 }
 
@@ -202,4 +203,4 @@
     aconfig_declarations: "dynamic_sensors_flags",
     host_supported: true,
     vendor: true,
-}
\ No newline at end of file
+}
diff --git a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp
index 101b983..8df91c2 100644
--- a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp
+++ b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.cpp
@@ -20,6 +20,7 @@
 #include <convertV2_1.h>
 #include <hardware/sensors-base.h>
 #include <log/log.h>
+#include <com_android_libhardware_dynamic_sensors_flags.h>
 
 #include <chrono>
 #include <thread>
@@ -32,6 +33,8 @@
 template<class T> using Return = ::android::hardware::Return<T>;
 using ::android::hardware::Void;
 
+namespace dynamic_sensors_flags = com::android::libhardware::dynamic::sensors::flags;
+
 namespace android {
 namespace SensorHalExt {
 
@@ -165,6 +168,8 @@
     std::vector<Event> events;
     Event hal_event;
     bool wakeup;
+    bool disconnectDynamicSensorFlag =
+        dynamic_sensors_flags::dynamic_sensors_hal_disconnect_dynamic_sensor();
 
     if (e.type == SENSOR_TYPE_DYNAMIC_SENSOR_META) {
         const dynamic_sensor_meta_event_t* sensor_meta;
@@ -172,21 +177,25 @@
         sensor_meta = static_cast<const dynamic_sensor_meta_event_t*>(
                 &(e.dynamic_sensor_meta));
         if (sensor_meta->connected != 0) {
-            // The sensor framework must be notified of the connected sensor
-            // through the callback before handling the sensor added event. If
-            // it isn't, it will assert when looking up the sensor handle when
-            // processing the sensor added event.
-            //
-            // TODO (b/201529167): Fix dynamic sensors addition / removal when
-            //                     converting to AIDL.
-            // The sensor framework runs in a separate process from the sensor
-            // HAL, and it processes events in a dedicated thread, so it's
-            // possible the event handling can be done before the callback is
-            // run. Thus, a delay is added after sending notification of the
-            // connected sensor.
             onSensorConnected(sensor_meta->handle, sensor_meta->sensor);
+        } else if (disconnectDynamicSensorFlag) {
+            onSensorDisconnected(sensor_meta->handle);
+        }
+        // The sensor framework must be notified of the connected sensor
+        // through the callback before handling the sensor added event. If
+        // it isn't, it will assert when looking up the sensor handle when
+        // processing the sensor added event.
+        //
+        // TODO (b/201529167): Fix dynamic sensors addition / removal when
+        //                     converting to AIDL.
+        // The sensor framework runs in a separate process from the sensor
+        // HAL, and it processes events in a dedicated thread, so it's
+        // possible the event handling can be done before the callback is
+        // run. Thus, a delay is added after sending notification of the
+        // connected sensor.
+        if (disconnectDynamicSensorFlag || sensor_meta->connected != 0) {
             std::this_thread::sleep_for(std::chrono::milliseconds(1000));
-       }
+        }
     }
 
     convertFromSensorEvent(e, &hal_event);
@@ -226,6 +235,14 @@
     mHalProxyCallback->onDynamicSensorsConnected_2_1(sensor_list);
 }
 
+void DynamicSensorsSubHal::onSensorDisconnected(int handle) {
+    hidl_vec<int32_t> handleList;
+    handleList.resize(1);
+    handleList[0] = handle;
+
+    mHalProxyCallback->onDynamicSensorsDisconnected(handleList);
+}
+
 } // namespace SensorHalExt
 } // namespace android
 
diff --git a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h
index f59b00a..0d90dc0 100644
--- a/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h
+++ b/modules/sensors/dynamic_sensor/DynamicSensorsSubHal.h
@@ -77,6 +77,7 @@
                                                       kDynamicHandleBase;
 
     void onSensorConnected(int handle, const sensor_t* sensor_info);
+    void onSensorDisconnected(int handle);
 
     std::unique_ptr<DynamicSensorManager> mDynamicSensorManager;
     sp<IHalProxyCallback> mHalProxyCallback;
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.cpp b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
index 939b58d..b61185d 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.cpp
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
@@ -988,18 +988,33 @@
     SP(HidDevice) device = PROMOTE(mDevice);
 
     if (device == nullptr) {
+        LOG_E << "enable: no device" << LOG_ENDL;
         return NO_INIT;
     }
 
     if (enable == mEnabled) {
+        LOG_D << "enable: already in desired state" << LOG_ENDL;
         return NO_ERROR;
     }
 
+    bool setLeAudioTransportOk = setLeAudioTransport(device, enable);
+    bool setPowerOk = setPower(device, enable);
+    bool setReportingOk = setReportingState(device, enable);
+    if (setPowerOk && setReportingOk && setLeAudioTransportOk) {
+        mEnabled = enable;
+        LOG_I << "enable: success" << LOG_ENDL;
+        return NO_ERROR;
+    } else {
+        LOG_E << "enable: set feature failed" << LOG_ENDL;
+        return INVALID_OPERATION;
+    }
+}
+
+bool HidRawSensor::setLeAudioTransport(const SP(HidDevice) &device, bool enable) {
     std::vector<uint8_t> buffer;
-    // TODO(b/298450041): Refactor the operations below in a separate function.
-    bool setLeAudioTransportOk = true;
+    bool success = true;
     if (mLeTransportId >= 0 && enable) {
-        setLeAudioTransportOk = false;
+        success = false;
         uint8_t id = static_cast<uint8_t>(mLeTransportId);
         if (device->getFeature(id, &buffer)
                 && (8 * buffer.size()) >=
@@ -1030,18 +1045,22 @@
 
             HidUtil::copyBits(&index, &(buffer[0]), buffer.size(), 0,
                               mLeTransportBitOffset, mLeTransportBitSize);
-            setLeAudioTransportOk = device->setFeature(id, buffer);
-            if (!setLeAudioTransportOk) {
+            success = device->setFeature(id, buffer);
+            if (!success) {
               LOG_E << "enable: setFeature VENDOR LE TRANSPORT failed" << LOG_ENDL;
             }
         } else {
             LOG_E << "enable: changing VENDOR LE TRANSPORT failed" << LOG_ENDL;
         }
     }
+    return success;
+}
 
-    bool setPowerOk = true;
+bool HidRawSensor::setPower(const SP(HidDevice) &device, bool enable) {
+    std::vector<uint8_t> buffer;
+    bool success = true;
     if (mPowerStateId >= 0) {
-        setPowerOk = false;
+        success = false;
         uint8_t id = static_cast<uint8_t>(mPowerStateId);
         if (device->getFeature(id, &buffer)
                 && (8 * buffer.size()) >=
@@ -1049,18 +1068,22 @@
             uint8_t index = enable ? mPowerStateOnIndex : mPowerStateOffIndex;
             HidUtil::copyBits(&index, &(buffer[0]), buffer.size(),
                               0, mPowerStateBitOffset, mPowerStateBitSize);
-            setPowerOk = device->setFeature(id, buffer);
-            if (!setPowerOk) {
+            success = device->setFeature(id, buffer);
+            if (!success) {
               LOG_E << "enable: setFeature POWER STATE failed" << LOG_ENDL;
             }
         } else {
             LOG_E << "enable: changing POWER STATE failed" << LOG_ENDL;
         }
     }
+    return success;
+}
 
-    bool setReportingOk = true;
+bool HidRawSensor::setReportingState(const SP(HidDevice) &device, bool enable) {
+    std::vector<uint8_t> buffer;
+    bool success = true;
     if (mReportingStateId >= 0) {
-        setReportingOk = false;
+        success = false;
         uint8_t id = static_cast<uint8_t>(mReportingStateId);
         if (device->getFeature(id, &buffer)
                 && (8 * buffer.size()) >
@@ -1069,21 +1092,15 @@
                                      mReportingStateDisableIndex;
             HidUtil::copyBits(&index, &(buffer[0]), buffer.size(),0,
                               mReportingStateBitOffset, mReportingStateBitSize);
-            setReportingOk = device->setFeature(id, buffer);
-            if (!setReportingOk) {
+            success = device->setFeature(id, buffer);
+            if (!success) {
               LOG_E << "enable: setFeature REPORTING STATE failed" << LOG_ENDL;
             }
         } else {
             LOG_E << "enable: changing REPORTING STATE failed" << LOG_ENDL;
         }
     }
-
-    if (setPowerOk && setReportingOk && setLeAudioTransportOk) {
-        mEnabled = enable;
-        return NO_ERROR;
-    } else {
-        return INVALID_OPERATION;
-    }
+    return success;
 }
 
 int HidRawSensor::batch(int64_t samplingPeriod, int64_t batchingPeriod) {
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.h b/modules/sensors/dynamic_sensor/HidRawSensor.h
index a9847c8..71d80b1 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.h
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.h
@@ -149,6 +149,10 @@
     // process HID snesor spec defined orientation(quaternion) sensor usages.
     bool processQuaternionUsage(const std::vector<HidParser::ReportPacket> &packets);
 
+    bool setLeAudioTransport(const SP(HidDevice) &device, bool enable);
+    bool setPower(const SP(HidDevice) &device, bool enable);
+    bool setReportingState(const SP(HidDevice) &device, bool enable);
+
     // get the value of a report field
     template<typename ValueType>
     bool getReportFieldValue(const std::vector<uint8_t> &message,
diff --git a/modules/sensors/dynamic_sensor/HidRawSensorDaemon.cpp b/modules/sensors/dynamic_sensor/HidRawSensorDaemon.cpp
index 4b447ac..0d561b1 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensorDaemon.cpp
+++ b/modules/sensors/dynamic_sensor/HidRawSensorDaemon.cpp
@@ -59,7 +59,7 @@
         ALOGE("failed to create HidRawSensorDevice object");
     }
 
-    ALOGE("return %zu sensors", ret.size());
+    ALOGI("return %zu sensors", ret.size());
     return ret;
 }
 
diff --git a/modules/sensors/dynamic_sensor/dynamic_sensors.aconfig b/modules/sensors/dynamic_sensor/dynamic_sensors.aconfig
index dc312c0..fe9c145 100644
--- a/modules/sensors/dynamic_sensor/dynamic_sensors.aconfig
+++ b/modules/sensors/dynamic_sensor/dynamic_sensors.aconfig
@@ -1,8 +1,16 @@
 package: "com.android.libhardware.dynamic.sensors.flags"
+container: "system"
 
 flag {
   name: "dynamic_sensors_le_audio"
   namespace: "sensors"
   description: "This flag controls the enablement of LE audio support on dynamic sensors"
   bug: "298450041"
+}
+
+flag {
+  name: "dynamic_sensors_hal_disconnect_dynamic_sensor"
+  namespace: "sensors"
+  description: "This flag controls if onDynamicSensorDisconnect callback in sensors HAL will be triggered"
+  bug: "316958439"
 }
\ No newline at end of file