Merge "Use LLNDK guard instead of builtin_available" into main am: bce3918b3d am: 6959a887b5

Original change: https://android-review.googlesource.com/c/platform/hardware/libhardware/+/2943126

Change-Id: Ic090d1330ec342979dd729f848146c3fa8985376
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
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..5baa006 100644
--- a/modules/sensors/dynamic_sensor/HidRawSensor.cpp
+++ b/modules/sensors/dynamic_sensor/HidRawSensor.cpp
@@ -995,11 +995,22 @@
         return NO_ERROR;
     }
 
+    bool setLeAudioTransportOk = setLeAudioTransport(device, enable);
+    bool setPowerOk = setPower(device, enable);
+    bool setReportingOk = setReportingState(device, enable);
+    if (setPowerOk && setReportingOk && setLeAudioTransportOk) {
+        mEnabled = enable;
+        return NO_ERROR;
+    } else {
+        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 +1041,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 +1064,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 +1088,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/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