Merge "Update HDMI-CEC HAL interface definition" into klp-modular-dev
diff --git a/include/hardware/bt_pan.h b/include/hardware/bt_pan.h
index c8b36b4..83e7949 100644
--- a/include/hardware/bt_pan.h
+++ b/include/hardware/bt_pan.h
@@ -40,8 +40,8 @@
 */
 typedef void (*btpan_connection_state_callback)(btpan_connection_state_t state, bt_status_t error,
                                                 const bt_bdaddr_t *bd_addr, int local_role, int remote_role);
-typedef void (*btpan_control_state_callback)(btpan_control_state_t state, bt_status_t error,
-                                            int local_role, const char* ifname);
+typedef void (*btpan_control_state_callback)(btpan_control_state_t state, int local_role,
+                                            bt_status_t error, const char* ifname);
 
 typedef struct {
     size_t size;
diff --git a/modules/sensors/multihal.cpp b/modules/sensors/multihal.cpp
index 36345f9..f7177a3 100644
--- a/modules/sensors/multihal.cpp
+++ b/modules/sensors/multihal.cpp
@@ -49,7 +49,7 @@
 bool waiting_for_data = false;
 
 /*
- * Vector of sub modules, whose indexes are referred to ni this file as module_index.
+ * 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;
 
@@ -91,17 +91,40 @@
     return global_handle;
 }
 
+// Returns the local handle, or -1 if it does not exist.
 static int get_local_handle(int global_handle) {
+    if (global_to_full.count(global_handle) == 0) {
+        ALOGW("Unknown global_handle %d", global_handle);
+        return -1;
+    }
     return global_to_full[global_handle].localHandle;
 }
 
+// Returns the sub_hw_modules index of the module that contains the sensor associates with this
+// global_handle, or -1 if that global_handle does not exist.
 static int get_module_index(int global_handle) {
+    if (global_to_full.count(global_handle) == 0) {
+        ALOGW("Unknown global_handle %d", global_handle);
+        return -1;
+    }
     FullHandle f = global_to_full[global_handle];
     ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d",
             global_handle, f.moduleIndex, f.localHandle);
     return f.moduleIndex;
 }
 
+// Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
+static int get_global_handle(FullHandle* full_handle) {
+    int global_handle = -1;
+    if (full_to_global.count(*full_handle)) {
+        global_handle = full_to_global[*full_handle];
+    } else {
+        ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d",
+            full_handle->moduleIndex, full_handle->localHandle);
+    }
+    return global_handle;
+}
+
 static const int SENSOR_EVENT_QUEUE_CAPACITY = 20;
 
 struct TaskContext {
@@ -198,35 +221,54 @@
     this->threads.push_back(writerThread);
 }
 
-sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int handle) {
-    int sub_index = get_module_index(handle);
+// Returns the device pointer, or NULL if the global handle is invalid.
+sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
+    int sub_index = get_module_index(global_handle);
+    if (sub_index < 0 || sub_index >= this->sub_hw_devices.size()) {
+        return NULL;
+    }
     return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
 }
 
-sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int handle) {
-    int sub_index = get_module_index(handle);
+// Returns the device pointer, or NULL if the global handle is invalid.
+sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
+    int sub_index = get_module_index(global_handle);
+    if (sub_index < 0 || sub_index >= this->sub_hw_devices.size()) {
+        return NULL;
+    }
     return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
 }
 
+// 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);
-    return v0->common.version;
+    if (v0) {
+        return v0->common.version;
+    } else {
+        return -1;
+    }
 }
 
 int sensors_poll_context_t::activate(int handle, int enabled) {
     int retval = -EINVAL;
     ALOGV("activate");
+    int local_handle = get_local_handle(handle);
     sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
-    if (v0)
-        retval = v0->activate(v0, get_local_handle(handle), enabled);
+    if (local_handle >= 0 && v0) {
+        retval = v0->activate(v0, local_handle, enabled);
+    }
     ALOGV("retval %d", retval);
     return retval;
 }
 
 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
+    int retval = -EINVAL;
     ALOGV("setDelay");
+    int local_handle = get_local_handle(handle);
     sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
-    int retval = v0->setDelay(v0, get_local_handle(handle), ns);
+    if (local_handle >= 0 && v0) {
+        retval = v0->setDelay(v0, local_handle, ns);
+    }
     ALOGV("retval %d", retval);
     return retval;
 }
@@ -239,13 +281,17 @@
     // with a local handle that needs to be converted to a global handle.
     FullHandle full_handle;
     full_handle.moduleIndex = sub_index;
+
     // If it's a metadata event, rewrite the inner payload, not the sensor field.
+    // If the event's sensor field is unregistered for any reason, rewrite the sensor field
+    // with a -1, instead of writing an incorrect but plausible sensor number, because
+    // get_global_handle() returns -1 for unknown FullHandles.
     if (dest->type == SENSOR_TYPE_META_DATA) {
         full_handle.localHandle = dest->meta_data.sensor;
-        dest->meta_data.sensor = full_to_global[full_handle];
+        dest->meta_data.sensor = get_global_handle(&full_handle);
     } else {
         full_handle.localHandle = dest->sensor;
-        dest->sensor = full_to_global[full_handle];
+        dest->sensor = get_global_handle(&full_handle);
     }
 }
 
@@ -288,9 +334,10 @@
     ALOGV("batch");
     int retval = -EINVAL;
     int version = this->get_device_version_by_handle(handle);
-    if (version >= SENSORS_DEVICE_API_VERSION_1_0) {
-        sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
-        retval = v1->batch(v1, get_local_handle(handle), flags, period_ns, timeout);
+    int local_handle = get_local_handle(handle);
+    sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
+    if (version >= SENSORS_DEVICE_API_VERSION_1_0 && local_handle >= 0 && v1) {
+        retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
     }
     ALOGV("retval %d", retval);
     return retval;
@@ -300,9 +347,10 @@
     ALOGV("flush");
     int retval = -EINVAL;
     int version = this->get_device_version_by_handle(handle);
-    if (version >= SENSORS_DEVICE_API_VERSION_1_0) {
-        sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
-        retval = v1->flush(v1, get_local_handle(handle));
+    int local_handle = get_local_handle(handle);
+    sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
+    if (version >= SENSORS_DEVICE_API_VERSION_1_0 && local_handle >= 0 && v1) {
+        retval = v1->flush(v1, local_handle);
     }
     ALOGV("retval %d", retval);
     return retval;
@@ -380,7 +428,7 @@
         ALOGW("No multihal config file found at %s", CONFIG_FILENAME);
         return;
     }
-    ALOGI("Multihal config file found at %s", CONFIG_FILENAME);
+    ALOGV("Multihal config file found at %s", CONFIG_FILENAME);
     char *line = NULL;
     size_t len = 0;
     int line_count = 0;
@@ -393,7 +441,7 @@
         ALOGV("config file line #%d: '%s'", ++line_count, line);
         char *real_path = realpath(line, NULL);
         if (starts_with(real_path, LEGAL_SUBHAL_PATH_PREFIX)) {
-            ALOGI("accepting valid path '%s'", real_path);
+            ALOGV("accepting valid path '%s'", real_path);
             char* compact_line = new char[strlen(real_path) + 1];
             strcpy(compact_line, real_path);
             so_paths->push_back(compact_line);
@@ -431,7 +479,7 @@
         if (lib_handle == NULL) {
             ALOGW("dlerror(): %s", dlerror());
         } else {
-            ALOGI("hal lib was loaded: %s", path);
+            ALOGI("Loaded library from %s", path);
             ALOGV("Opening symbol \"%s\"", sym);
             // clear old errors
             dlerror();
@@ -442,7 +490,7 @@
             } else if (module == NULL) {
                 ALOGW("module == NULL");
             } else {
-                ALOGI("OK, dlsym()'ed \"%s\"", sym);
+                ALOGV("Loaded symbols from \"%s\"", sym);
                 sub_hw_modules->push_back(module);
             }
         }
@@ -506,7 +554,7 @@
             int global_handle = assign_global_handle(module_index, local_handle);
 
             mutable_sensor_list[mutable_sensor_index].handle = global_handle;
-            ALOGI("module_index %d, local_handle %d, global_handle %d",
+            ALOGV("module_index %d, local_handle %d, global_handle %d",
                     module_index, local_handle, global_handle);
 
             mutable_sensor_index++;
@@ -553,7 +601,7 @@
 
 static int open_sensors(const struct hw_module_t* hw_module, const char* name,
         struct hw_device_t** hw_device_out) {
-    ALOGI("open_sensors begin...");
+    ALOGV("open_sensors begin...");
 
     lazy_init_modules();
 
@@ -584,6 +632,6 @@
 
     // Prepare the output param and return
     *hw_device_out = &dev->proxy_device.common;
-    ALOGI("...open_sensors end");
+    ALOGV("...open_sensors end");
     return 0;
 }