Merge changes from topic "equalizer"

* changes:
  Fix potential memory leak in multihal
  sensors: route sensor handle for multi-hal event injection
  Add support of sensor direct report and operation parameter
  Proposing ownership for modules/sensors
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/OWNERS b/modules/sensors/OWNERS
new file mode 100644
index 0000000..1af3506
--- /dev/null
+++ b/modules/sensors/OWNERS
@@ -0,0 +1,2 @@
+pengxu@google.com
+ashutoshj@google.com
diff --git a/modules/sensors/multihal.cpp b/modules/sensors/multihal.cpp
index 887a4ba..b5360cc 100644
--- a/modules/sensors/multihal.cpp
+++ b/modules/sensors/multihal.cpp
@@ -49,10 +49,11 @@
 static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER;
 bool waiting_for_data = false;
 
-/*
- * Vector of sub modules, whose indexes are referred to in this file as module_index.
- */
-static std::vector<hw_module_t *> *sub_hw_modules = NULL;
+// Vector of sub modules, whose indexes are referred to in this file as module_index.
+static std::vector<hw_module_t *> *sub_hw_modules = nullptr;
+
+// Vector of sub modules shared object handles
+static std::vector<void *> *so_handles = nullptr;
 
 /*
  * Comparable class that globally identifies a sensor, by module index and local handle.
@@ -196,7 +197,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 +212,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 +252,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 +285,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 +375,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 +428,75 @@
     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) {
+            // if specific sensor is used, we have to replace global sensor handle
+            // with local one, before passing to concrete HAL
+            sensors_event_t data_copy = *data;
+            data_copy.sensor = local_handle;
+            retval = v1->inject_sensor_data(v1, &data_copy);
+        } 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();
@@ -440,11 +510,26 @@
 
 
 static int device__close(struct hw_device_t *dev) {
+    pthread_mutex_lock(&init_modules_mutex);
     sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
     if (ctx != NULL) {
         int retval = ctx->close();
         delete ctx;
     }
+
+    if (sub_hw_modules != nullptr) {
+        delete sub_hw_modules;
+        sub_hw_modules = nullptr;
+    }
+
+    if (so_handles != nullptr) {
+        for (auto handle : *so_handles) {
+            dlclose(handle);
+        }
+        delete so_handles;
+        so_handles = nullptr;
+    }
+    pthread_mutex_unlock(&init_modules_mutex);
     return 0;
 }
 
@@ -480,7 +565,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,
@@ -499,7 +599,9 @@
  * Adds valid paths from the config file to the vector passed in.
  * The vector must not be null.
  */
-static void get_so_paths(std::vector<std::string> *so_paths) {
+static std::vector<std::string> get_so_paths() {
+    std::vector<std::string> so_paths;
+
     const std::vector<const char *> config_path_list(
             { MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH });
 
@@ -515,7 +617,7 @@
     }
     if(!stream) {
         ALOGW("No multihal config file found");
-        return;
+        return so_paths;
     }
 
     ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0,
@@ -527,8 +629,9 @@
     std::string line;
     while (std::getline(stream, line)) {
         ALOGV("config file line: '%s'", line.c_str());
-        so_paths->push_back(line);
+        so_paths.push_back(line);
     }
+    return so_paths;
 }
 
 /*
@@ -541,15 +644,15 @@
         pthread_mutex_unlock(&init_modules_mutex);
         return;
     }
-    std::vector<std::string> *so_paths = new std::vector<std::string>();
-    get_so_paths(so_paths);
+    std::vector<std::string> so_paths(get_so_paths());
 
     // dlopen the module files and cache their module symbols in sub_hw_modules
     sub_hw_modules = new std::vector<hw_module_t *>();
+    so_handles = new std::vector<void *>();
     dlerror(); // clear any old errors
     const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
-    for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
-        const char* path = it->c_str();
+    for (const auto &s : so_paths) {
+        const char* path = s.c_str();
         void* lib_handle = dlopen(path, RTLD_LAZY);
         if (lib_handle == NULL) {
             ALOGW("dlerror(): %s", dlerror());
@@ -567,8 +670,13 @@
             } else {
                 ALOGV("Loaded symbols from \"%s\"", sym);
                 sub_hw_modules->push_back(module);
+                so_handles->push_back(lib_handle);
+                lib_handle = nullptr;
             }
         }
+        if (lib_handle != nullptr) {
+            dlclose(lib_handle);
+        }
     }
     pthread_mutex_unlock(&init_modules_mutex);
 }
@@ -625,6 +733,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 +811,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;