Merge "Add support of sensor direct report and operation parameter"
diff --git a/modules/sensors/Android.mk b/modules/sensors/Android.mk
index ca277e6..e5e0c9c 100644
--- a/modules/sensors/Android.mk
+++ b/modules/sensors/Android.mk
@@ -17,6 +17,7 @@
 LOCAL_PATH := $(call my-dir)
 
 ifeq ($(USE_SENSOR_MULTI_HAL),true)
+ifneq ($(PRODUCT_FULL_TREBLE),true)
 
 include $(CLEAR_VARS)
 
@@ -41,6 +42,10 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
+else
+$(warning Treble enabled device have built-in sensor multihal support. \
+          USE_SENSOR_MULTI_HAL should not be set.)
+endif # PRODUCT_FULL_TREBLE
 endif # USE_SENSOR_MULTI_HAL
 
 include $(call all-makefiles-under, $(LOCAL_PATH))
diff --git a/modules/sensors/multihal.cpp b/modules/sensors/multihal.cpp
index 887a4ba..4d6013f 100644
--- a/modules/sensors/multihal.cpp
+++ b/modules/sensors/multihal.cpp
@@ -196,7 +196,12 @@
     int poll(sensors_event_t* data, int count);
     int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
     int flush(int handle);
-    int inject_sensor_data(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
+    int inject_sensor_data(const sensors_event_t *data);
+    int register_direct_channel(const struct sensors_direct_mem_t* mem,
+                                int channel_handle);
+    int config_direct_report(int sensor_handle,
+                             int channel_handle,
+                             const struct sensors_direct_cfg_t *config);
     int close();
 
     std::vector<hw_device_t*> sub_hw_devices;
@@ -206,6 +211,7 @@
 
     sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
     sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
+    sensors_poll_device_1_t* get_primary_v1_device();
     int get_device_version_by_handle(int global_handle);
 
     void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
@@ -245,6 +251,14 @@
     return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
 }
 
+// Returns the device pointer, or NULL if primary hal does not exist
+sensors_poll_device_1_t* sensors_poll_context_t::get_primary_v1_device() {
+    if (sub_hw_devices.size() < 1) {
+        return nullptr;
+    }
+    return (sensors_poll_device_1_t*) this->sub_hw_devices[0];
+}
+
 // Returns the device version, or -1 if the handle is invalid.
 int sensors_poll_context_t::get_device_version_by_handle(int handle) {
     sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
@@ -270,6 +284,11 @@
     return version != -1 && (version >= level);
 }
 
+static bool halSupportDirectSensorReport(sensors_poll_device_1_t* v1) {
+    return v1 != nullptr && HAL_VERSION_IS_COMPLIANT(v1->common.version) &&
+            v1->register_direct_channel != nullptr && v1->config_direct_report != nullptr;
+}
+
 const char *apiNumToStr(int version) {
     switch(version) {
     case SENSORS_DEVICE_API_VERSION_1_0:
@@ -355,7 +374,7 @@
             } else {
                 empties = 0;
                 this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
-                if (data[eventsRead].sensor == -1) {
+                if (data[eventsRead].sensor == SENSORS_HANDLE_BASE - 1) {
                     // Bad handle, do not pass corrupted event upstream !
                     ALOGW("Dropping bad local handle event packet on the floor");
                 } else {
@@ -408,25 +427,71 @@
     return retval;
 }
 
-int sensors_poll_context_t::inject_sensor_data(struct sensors_poll_device_1 *dev,
-                                               const sensors_event_t *data) {
+int sensors_poll_context_t::inject_sensor_data(const sensors_event_t *data) {
     int retval = -EINVAL;
     ALOGV("inject_sensor_data");
-    // Get handle for the sensor owning the event being injected
-    int local_handle = get_local_handle(data->sensor);
-    sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
-    if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
-            local_handle >= 0 && v1) {
-        retval = v1->inject_sensor_data(dev, data);
+    if (data->sensor == -1) {
+        // operational parameter
+        sensors_poll_device_1_t* v1 = get_primary_v1_device();
+        if (v1 && v1->common.version >= SENSORS_DEVICE_API_VERSION_1_4) {
+            retval = v1->inject_sensor_data(v1, data);
+        } else {
+            ALOGE("IGNORED inject_sensor_data(operational param) call to non-API-compliant sensor");
+            return -ENOSYS;
+        }
     } else {
-        ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
-                data->type, data->sensor);
+        // Get handle for the sensor owning the event being injected
+        int local_handle = get_local_handle(data->sensor);
+        sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
+        if (halIsAPILevelCompliant(this, data->sensor, SENSORS_DEVICE_API_VERSION_1_4) &&
+                local_handle >= 0 && v1) {
+            retval = v1->inject_sensor_data(v1, data);
+        } else {
+            ALOGE("IGNORED inject_sensor_data(type=%d, handle=%d) call to non-API-compliant sensor",
+                    data->type, data->sensor);
+            retval = -ENOSYS;
+        }
     }
     ALOGV("retval %d", retval);
     return retval;
-
 }
 
+int sensors_poll_context_t::register_direct_channel(const struct sensors_direct_mem_t* mem,
+                                                   int channel_handle) {
+    int retval = -EINVAL;
+    ALOGV("register_direct_channel");
+    sensors_poll_device_1_t* v1 = get_primary_v1_device();
+    if (v1 && halSupportDirectSensorReport(v1)) {
+        retval = v1->register_direct_channel(v1, mem, channel_handle);
+    } else {
+        ALOGE("IGNORED register_direct_channel(mem=%p, handle=%d) call to non-API-compliant sensor",
+                mem, channel_handle);
+        retval = -ENOSYS;
+    }
+    ALOGV("retval %d", retval);
+    return retval;
+}
+
+int sensors_poll_context_t::config_direct_report(int sensor_handle,
+                                                int channel_handle,
+                                                const struct sensors_direct_cfg_t *config) {
+    int retval = -EINVAL;
+    ALOGV("config_direct_report");
+
+    if (config != nullptr) {
+        int local_handle = get_local_handle(sensor_handle);
+        sensors_poll_device_1_t* v1 = get_primary_v1_device();
+        if (v1 && halSupportDirectSensorReport(v1)) {
+            retval = v1->config_direct_report(v1, local_handle, channel_handle, config);
+        } else {
+            ALOGE("IGNORED config_direct_report(sensor=%d, channel=%d, rate_level=%d) call to "
+                  "non-API-compliant sensor", sensor_handle, channel_handle, config->rate_level);
+            retval = -ENOSYS;
+        }
+    }
+    ALOGV("retval %d", retval);
+    return retval;
+}
 int sensors_poll_context_t::close() {
     ALOGV("close");
     for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
@@ -480,7 +545,22 @@
 static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
         const sensors_event_t *data) {
     sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
-    return ctx->inject_sensor_data(dev, data);
+    return ctx->inject_sensor_data(data);
+}
+
+static int device__register_direct_channel(struct sensors_poll_device_1 *dev,
+                                           const struct sensors_direct_mem_t* mem,
+                                           int channel_handle) {
+    sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
+    return ctx->register_direct_channel(mem, channel_handle);
+}
+
+static int device__config_direct_report(struct sensors_poll_device_1 *dev,
+                                        int sensor_handle,
+                                        int channel_handle,
+                                        const struct sensors_direct_cfg_t *config) {
+    sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
+    return ctx->config_direct_report(sensor_handle, channel_handle, config);
 }
 
 static int open_sensors(const struct hw_module_t* module, const char* name,
@@ -625,6 +705,12 @@
             memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
                 sizeof(struct sensor_t));
 
+            // sensor direct report is only for primary module
+            if (module_index != 0) {
+                mutable_sensor_list[mutable_sensor_index].flags &=
+                    ~(SENSOR_FLAG_MASK_DIRECT_REPORT | SENSOR_FLAG_MASK_DIRECT_CHANNEL);
+            }
+
             // Overwrite the global version's handle with a global handle.
             int global_handle = assign_global_handle(module_index, local_handle);
 
@@ -697,6 +783,8 @@
     dev->proxy_device.batch = device__batch;
     dev->proxy_device.flush = device__flush;
     dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
+    dev->proxy_device.register_direct_channel = device__register_direct_channel;
+    dev->proxy_device.config_direct_report = device__config_direct_report;
 
     dev->nextReadIndex = 0;