diff --git a/core/java/android/hardware/location/ContextHubService.java b/core/java/android/hardware/location/ContextHubService.java index b65e24e3b81e0..3e6cb63d9f633 100644 --- a/core/java/android/hardware/location/ContextHubService.java +++ b/core/java/android/hardware/location/ContextHubService.java @@ -38,8 +38,8 @@ public class ContextHubService extends IContextHubService.Stub { 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 42dc983792d20..80ae550710be7 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 set_os_app_as_destination(hub_message_t *msg, int hubHandle) { } } +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 jobjectArray nativeInitialize(JNIEnv *env, jobject instance) 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 @@ static jint nativeSendMessage(JNIEnv *env, jobject instance, jintArray header_, 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");