App upload to Context hub.

Fix constants
Use helper functions to address OS

Change-Id: I61bd1f3eff2716ead2771eda335998f2e9b73afc
diff --git a/core/java/android/hardware/location/ContextHubService.java b/core/java/android/hardware/location/ContextHubService.java
index b65e24e..3e6cb63 100644
--- a/core/java/android/hardware/location/ContextHubService.java
+++ b/core/java/android/hardware/location/ContextHubService.java
@@ -38,8 +38,8 @@
 
 
     public static final int ANY_HUB             = -1;
-    public static final int MSG_LOAD_NANO_APP   = 5;
-    public static final int MSG_UNLOAD_NANO_APP = 2;
+    public static final int MSG_LOAD_NANO_APP   = 3;
+    public static final int MSG_UNLOAD_NANO_APP = 4;
 
     private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown";
     private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN;
diff --git a/core/jni/android_hardware_location_ContextHubService.cpp b/core/jni/android_hardware_location_ContextHubService.cpp
index 42dc983..80ae550 100644
--- a/core/jni/android_hardware_location_ContextHubService.cpp
+++ b/core/jni/android_hardware_location_ContextHubService.cpp
@@ -33,7 +33,7 @@
 #include "JNIHelp.h"
 #include "core_jni_helpers.h"
 
-//static constexpr int OS_APP_ID=-1;
+static constexpr int OS_APP_ID=-1;
 
 static constexpr int MIN_APP_ID=1;
 static constexpr int MAX_APP_ID=128;
@@ -145,6 +145,14 @@
     }
 }
 
+static int get_hub_id_for_hub_handle(int hubHandle) {
+    if (hubHandle < 0 || hubHandle >= db.hubInfo.numHubs) {
+      return -1;
+    } else {
+      return db.hubInfo.hubs[hubHandle].hub_id;
+    }
+}
+
 static int get_hub_id_for_app_instance(int id) {
     if (db.appInstances.find(id) == db.appInstances.end()) {
         ALOGD("%s: Cannot find app for app instance %d", __FUNCTION__, id);
@@ -616,7 +624,6 @@
 
 static jint nativeSendMessage(JNIEnv *env, jobject instance, jintArray header_,
                               jbyteArray data_) {
-    hub_message_t msg;
     jint retVal = -1; // Default to failure
 
     jint *header = env->GetIntArrayElements(header_, 0);
@@ -624,16 +631,30 @@
     jbyte *data = env->GetByteArrayElements(data_, 0);
     int dataBufferLength = env->GetArrayLength(data_);
 
+
     if (numHeaderElements >= MSG_HEADER_SIZE) {
-        if (set_dest_app(&msg, header[HEADER_FIELD_APP_INSTANCE]) == 0) {
-          msg.message_type = header[HEADER_FIELD_MSG_TYPE];
-          msg.message_len = dataBufferLength;
-          msg.message = data;
-          retVal = db.hubInfo.contextHubModule->send_message(
-                  get_hub_id_for_app_instance(header[HEADER_FIELD_APP_INSTANCE]),
-                  &msg);
+        int setAddressSuccess;
+        int hubId;
+        hub_message_t msg;
+
+        if (header[HEADER_FIELD_APP_INSTANCE] == OS_APP_ID) {
+            setAddressSuccess = (set_os_app_as_destination(&msg, header[HEADER_FIELD_HUB_HANDLE]) == 0);
+            hubId = get_hub_id_for_hub_handle(header[HEADER_FIELD_HUB_HANDLE]);
         } else {
-          ALOGD("Could not find app instance %d", header[HEADER_FIELD_APP_INSTANCE]);
+            setAddressSuccess = (set_dest_app(&msg, header[HEADER_FIELD_APP_INSTANCE]) == 0);
+            hubId = get_hub_id_for_app_instance(header[HEADER_FIELD_APP_INSTANCE]);
+        }
+
+        if (setAddressSuccess && hubId >= 0) {
+            msg.message_type = header[HEADER_FIELD_MSG_TYPE];
+            msg.message_len = dataBufferLength;
+            msg.message = data;
+            retVal = db.hubInfo.contextHubModule->send_message(hubId, &msg);
+        } else {
+          ALOGD("Could not find app instance %d on hubHandle %d, setAddress %d",
+                header[HEADER_FIELD_APP_INSTANCE],
+                header[HEADER_FIELD_HUB_HANDLE],
+                setAddressSuccess);
         }
     } else {
         ALOGD("Malformed header len");