diff options
| -rw-r--r-- | services/core/jni/com_android_server_location_ContextHubService.cpp | 78 |
1 files changed, 33 insertions, 45 deletions
diff --git a/services/core/jni/com_android_server_location_ContextHubService.cpp b/services/core/jni/com_android_server_location_ContextHubService.cpp index 910644196c44..90b9207c92a2 100644 --- a/services/core/jni/com_android_server_location_ContextHubService.cpp +++ b/services/core/jni/com_android_server_location_ContextHubService.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ - #undef LOG_NDEBUG #undef LOG_TAG #define LOG_NDEBUG 0 @@ -39,20 +38,19 @@ #include "core_jni_helpers.h" #include "JNIHelp.h" -using IContexthub = android::hardware::contexthub::V1_0::IContexthub; - -using Result = android::hardware::contexthub::V1_0::Result; -using ContextHubMsg = android::hardware::contexthub::V1_0::ContextHubMsg; -using IContexthubCallback = android::hardware::contexthub::V1_0::IContexthubCallback; -using AsyncEventType = android::hardware::contexthub::V1_0::AsyncEventType; -using TransactionResult = android::hardware::contexthub::V1_0::TransactionResult; -using ContextHub = android::hardware::contexthub::V1_0::ContextHub; -using HubAppInfo = android::hardware::contexthub::V1_0::HubAppInfo; +using android::hardware::contexthub::V1_0::AsyncEventType; +using android::hardware::contexthub::V1_0::ContextHub; +using android::hardware::contexthub::V1_0::ContextHubMsg; +using android::hardware::contexthub::V1_0::HubAppInfo; +using android::hardware::contexthub::V1_0::IContexthub; +using android::hardware::contexthub::V1_0::IContexthubCallback; +using android::hardware::contexthub::V1_0::Result; +using android::hardware::contexthub::V1_0::TransactionResult; -template<typename T> -using Return = android::hardware::Return<T>; +using android::hardware::Return; using std::chrono::steady_clock; + // If a transaction takes longer than this, we'll allow it to be // canceled by a new transaction. Note we do _not_ automatically // cancel a transaction after this much time. We can have a @@ -361,11 +359,12 @@ struct ContextHubServiceDb { ContextHubServiceDb db; -int getHubIdForHubHandle(int hubHandle) { - if (hubHandle < 0 || hubHandle >= db.hubInfo.numHubs) { - return -1; +bool getHubIdForHubHandle(int hubHandle, uint32_t *hubId) { + if (hubHandle < 0 || hubHandle >= db.hubInfo.numHubs || hubId == nullptr) { + return false; } else { - return db.hubInfo.hubs[hubHandle].hubId; + *hubId = db.hubInfo.hubs[hubHandle].hubId; + return true; } } @@ -380,17 +379,6 @@ int getHubHandleForAppInstance(jint id) { return db.appInstances[id].hubHandle; } -int getHubIdForAppInstance(jint id) { - int hubHandle = getHubHandleForAppInstance(id); - - if (hubHandle < 0) { - ALOGD("Cannot find hub instance for app instance %d", id); - return -1; - } - - return db.hubInfo.hubs[hubHandle].hubId; -} - jint getAppInstanceForAppId(uint64_t app_id) { auto end = db.appInstances.end(); for (auto current = db.appInstances.begin(); current != end; ++current) { @@ -1012,19 +1000,18 @@ jint nativeSendMessage(JNIEnv *env, jint retVal = -1; // Default to failure jint *header = env->GetIntArrayElements(header_, 0); - unsigned int numHeaderElements = env->GetArrayLength(header_); + size_t numHeaderElements = env->GetArrayLength(header_); jbyte *data = env->GetByteArrayElements(data_, 0); - int dataBufferLength = env->GetArrayLength(data_); + size_t dataBufferLength = env->GetArrayLength(data_); if (numHeaderElements < MSG_HEADER_SIZE) { ALOGW("Malformed header len"); return -1; } - uint32_t appInstanceHandle = header[HEADER_FIELD_APP_INSTANCE]; + jint appInstanceHandle = header[HEADER_FIELD_APP_INSTANCE]; uint32_t msgType = header[HEADER_FIELD_MSG_TYPE]; int hubHandle = -1; - int hubId; uint64_t appId; if (msgType == CONTEXT_HUB_UNLOAD_APP) { @@ -1042,7 +1029,8 @@ jint nativeSendMessage(JNIEnv *env, hubHandle = header[HEADER_FIELD_HUB_HANDLE]; } - if (hubHandle < 0) { + uint32_t hubId = -1; + if (!getHubIdForHubHandle(hubHandle, &hubId)) { ALOGD("Invalid hub Handle %d", hubHandle); return -1; } @@ -1072,18 +1060,16 @@ jint nativeSendMessage(JNIEnv *env, Result result; if (msgType == CONTEXT_HUB_UNLOAD_APP) { - hubId = getHubIdForHubHandle(hubHandle); - ALOGW("Calling UnLoad NanoApp for app %" PRIx64 " on hub %d", + ALOGW("Calling UnLoad NanoApp for app %" PRIx64 " on hub %" PRIu32, db.appInstances[appInstanceHandle].appInfo.appId, hubId); result = db.hubInfo.contextHub->unloadNanoApp( hubId, db.appInstances[appInstanceHandle].appInfo.appId, CONTEXT_HUB_UNLOAD_APP); } else { - if (header[HEADER_FIELD_APP_INSTANCE] == OS_APP_ID) { + if (appInstanceHandle == OS_APP_ID) { if (msgType == CONTEXT_HUB_LOAD_APP) { std::vector<uint8_t> dataVector(reinterpret_cast<uint8_t *>(data), reinterpret_cast<uint8_t *>(data + dataBufferLength)); - hubId = getHubIdForHubHandle(hubHandle); ALOGW("Calling Load NanoApp on hub %d", hubId); result = db.hubInfo.contextHub->loadNanoApp(hubId, dataVector, @@ -1093,25 +1079,27 @@ jint nativeSendMessage(JNIEnv *env, result = Result::BAD_PARAMS; } } else { - - appId = getAppIdForAppInstance(header[HEADER_FIELD_APP_INSTANCE]); - hubId = getHubIdForAppInstance(header[HEADER_FIELD_APP_INSTANCE]); - - if (appId != static_cast<uint64_t>(INVALID_APP_ID) && hubId >= 0) { + appId = getAppIdForAppInstance(appInstanceHandle); + if (appId == static_cast<uint64_t>(INVALID_APP_ID)) { + ALOGD("Cannot find application instance %d", appInstanceHandle); + result = Result::BAD_PARAMS; + } else if (hubHandle != getHubHandleForAppInstance(appInstanceHandle)) { + ALOGE("Given hubHandle (%d) doesn't match expected for app instance (%d)", + hubHandle, + getHubHandleForAppInstance(appInstanceHandle)); + result = Result::BAD_PARAMS; + } else { ContextHubMsg msg; msg.appName = appId; msg.msgType = msgType; msg.msg.setToExternal((unsigned char *)data, dataBufferLength); - ALOGW("Sending msg of type %" PRIu32 " len %u to app %" PRIx64 " on hub %d", + ALOGW("Sending msg of type %" PRIu32 " len %zu to app %" PRIx64 " on hub %" PRIu32, msgType, dataBufferLength, appId, hubId); result = db.hubInfo.contextHub->sendMessageToHub(hubId, msg); - } else { - ALOGD("Cannot find application instance %u", header[HEADER_FIELD_APP_INSTANCE]); - result = Result::BAD_PARAMS; } } } |