From fb2bf007fc00431430437f0ab123885e5d030131 Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Fri, 2 Dec 2016 17:11:07 +0900 Subject: [PATCH] add exception check login in android BLE Change-Id: If324fc9399f48ce64d759d016da124963e136ea9 Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/15057 Tested-by: jenkins-iotivity Reviewed-by: Jaehong Jo Reviewed-by: Dan Mihai Reviewed-by: Larry Sachs Reviewed-by: Ashok Babu Channa --- resource/csdk/connectivity/inc/caadapterutils.h | 7 + .../connectivity/src/adapter_util/caadapterutils.c | 13 ++ .../src/bt_le_adapter/android/caleclient.c | 166 +++++++++++++----- .../src/bt_le_adapter/android/calenwmonitor.c | 3 + .../src/bt_le_adapter/android/caleserver.c | 194 +++++++++++++-------- .../src/bt_le_adapter/android/caleutils.c | 75 +++++--- 6 files changed, 322 insertions(+), 136 deletions(-) diff --git a/resource/csdk/connectivity/inc/caadapterutils.h b/resource/csdk/connectivity/inc/caadapterutils.h index ffbaa56..cb9692f 100644 --- a/resource/csdk/connectivity/inc/caadapterutils.h +++ b/resource/csdk/connectivity/inc/caadapterutils.h @@ -277,6 +277,13 @@ jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className, const char* methodFormat); /** + * check JNI exception occurrence + * @param[in] env JNI interface pointer. + * @return true(occurrence) or false(no occurrence). + */ +bool CACheckJNIException(JNIEnv *env); + +/** * To Delete other Global References * Called during CATerminate to remove global references */ diff --git a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c index f4ee9c5..c7065cf 100644 --- a/resource/csdk/connectivity/src/adapter_util/caadapterutils.c +++ b/resource/csdk/connectivity/src/adapter_util/caadapterutils.c @@ -296,6 +296,7 @@ jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className, if (!jni_cid) { OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className); + CACheckJNIException(env); return NULL; } @@ -303,6 +304,7 @@ jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className, if (!jni_midID) { OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid); return NULL; } @@ -311,6 +313,17 @@ jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className, return jni_midID; } +bool CACheckJNIException(JNIEnv *env) +{ + if ((*env)->ExceptionCheck(env)) + { + (*env)->ExceptionDescribe(env); + (*env)->ExceptionClear(env); + return true; + } + return false; +} + void CADeleteGlobalReferences(JNIEnv *env) { if (g_Context) diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c index be78e6d..54a67ee 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -351,7 +351,7 @@ CAResult_t CALECreateJniInterfaceObject() if (!jApplicationContext) { OIC_LOG(ERROR, TAG, "Could not get application context"); - return CA_STATUS_FAILED; + goto error_exit; } jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface"); @@ -380,7 +380,7 @@ CAResult_t CALECreateJniInterfaceObject() return CA_STATUS_OK; error_exit: - + CACheckJNIException(env); if (isAttached) { (*g_jvm)->DetachCurrentThread(g_jvm); @@ -637,12 +637,10 @@ CAResult_t CALEClientDestroyJniInterface() (*env)->CallStaticVoidMethod(env, jni_LeInterface, jni_InterfaceDestroyMethod); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "destroyLeInterface has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); - goto error_exit; + goto detach_thread; } OIC_LOG(DEBUG, TAG, "Destroy instance for CaLeClientInterface"); @@ -655,7 +653,9 @@ CAResult_t CALEClientDestroyJniInterface() return CA_STATUS_OK; error_exit: + CACheckJNIException(env); +detach_thread: if (isAttached) { (*g_jvm)->DetachCurrentThread(g_jvm); @@ -925,6 +925,7 @@ CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); goto error_exit; } @@ -941,8 +942,11 @@ CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* g_sendBuffer = NULL; } jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen); + CACheckJNIException(env); (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data); + CACheckJNIException(env); g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr); + CACheckJNIException(env); // Target device to send message is just one. g_targetCnt = 1; @@ -1108,6 +1112,7 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) if (!address) { OIC_LOG(ERROR, TAG, "address is not available"); + CACheckJNIException(env); return CA_STATUS_FAILED; } oc_mutex_lock(g_deviceStateListMutex); @@ -1243,6 +1248,7 @@ jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt) if (!jni_obj_device) { OIC_LOG(ERROR, TAG, "jni_obj_device is null"); + CACheckJNIException(env); return NULL; } @@ -1250,6 +1256,7 @@ jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt) if (!jni_address) { OIC_LOG(ERROR, TAG, "jni_address is null"); + CACheckJNIException(env); return NULL; } @@ -1279,11 +1286,9 @@ CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt) OIC_LOG(DEBUG, TAG, "request to close GATT"); (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "closeGATT has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } @@ -1368,6 +1373,7 @@ CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -1378,6 +1384,7 @@ CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1389,6 +1396,7 @@ CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback) if (!jni_mid_startLeScan) { OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1399,6 +1407,7 @@ CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1410,6 +1419,7 @@ CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback) if (!jni_obj_startLeScan) { OIC_LOG(INFO, TAG, "startLeScan has failed"); + CACheckJNIException(env); } else { @@ -1437,6 +1447,7 @@ CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobj if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -1447,6 +1458,7 @@ CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobj if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1458,6 +1470,7 @@ CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobj if (!jni_mid_startLeScan) { OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1468,6 +1481,7 @@ CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobj if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1479,6 +1493,7 @@ CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobj if (!jni_obj_startLeScan) { OIC_LOG(INFO, TAG, "startLeScan has failed"); + CACheckJNIException(env); } else { @@ -1500,7 +1515,7 @@ jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid) if (!jni_cid_uuid) { OIC_LOG(ERROR, TAG, "jni_cid_uuid is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", @@ -1509,19 +1524,24 @@ jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid) if (!jni_mid_fromString) { OIC_LOG(ERROR, TAG, "jni_mid_fromString is null"); - return NULL; + goto error_exit; } jstring jni_uuid = (*env)->NewStringUTF(env, uuid); + CACheckJNIException(env); jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid); if (!jni_obj_uuid) { OIC_LOG(ERROR, TAG, "jni_obj_uuid is null"); - return NULL; + goto error_exit; } return jni_obj_uuid; + +error_exit: + CACheckJNIException(env); + return NULL; } CAResult_t CALEClientStopScan() @@ -1585,6 +1605,7 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -1595,6 +1616,7 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1606,6 +1628,7 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback) if (!jni_mid_stopLeScan) { OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1616,6 +1639,7 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return CA_STATUS_FAILED; } @@ -1623,13 +1647,11 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback) OIC_LOG(INFO, TAG, "CALL API - stopLeScan"); // call start le scan method (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "stopLeScan has failed"); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); (*env)->DeleteLocalRef(env, jni_obj_BTAdapter); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } @@ -1650,6 +1672,7 @@ CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, jint state if (!address) { OIC_LOG(ERROR, TAG, "address is not available"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -1696,6 +1719,7 @@ jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state if (!address) { OIC_LOG(ERROR, TAG, "address is not available"); + CACheckJNIException(env); oc_mutex_unlock(g_deviceStateListMutex); return JNI_FALSE; } @@ -1748,6 +1772,7 @@ CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolea if (!address) { OIC_LOG(ERROR, TAG, "address is not available"); + CACheckJNIException(env); oc_mutex_unlock(g_threadSendMutex); return CA_STATUS_FAILED; } @@ -1876,6 +1901,7 @@ jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean aut if (!jni_obj_connectGatt) { OIC_LOG(ERROR, TAG, "connectGatt was failed..it will be removed"); + CACheckJNIException(env); CALEClientRemoveDeviceInScanDeviceList(env, jni_address); CALEClientUpdateSendCnt(env); return NULL; @@ -1898,7 +1924,7 @@ CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } // get remote bt adapter method @@ -1908,7 +1934,7 @@ CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } // gat bt adapter object @@ -1917,7 +1943,7 @@ CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } // get closeProfileProxy method @@ -1928,14 +1954,14 @@ CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt) if (!jni_mid_closeProfileProxy) { OIC_LOG(ERROR, TAG, "jni_mid_closeProfileProxy is null"); - return CA_STATUS_FAILED; + goto error_exit; } jclass jni_cid_BTProfile = (*env)->FindClass(env, CLASSPATH_BT_PROFILE); if (!jni_cid_BTProfile) { OIC_LOG(ERROR, TAG, "jni_cid_BTProfile is null"); - return CA_STATUS_FAILED; + goto error_exit; } // GATT - Constant value : 7 (0x00000007) @@ -1944,23 +1970,26 @@ CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt) if (!id_gatt) { OIC_LOG(ERROR, TAG, "id_gatt is null"); - return CA_STATUS_FAILED; + goto error_exit; } jint jni_gatt = (*env)->GetStaticIntField(env, jni_cid_BTProfile, id_gatt); + CACheckJNIException(env); OIC_LOG(DEBUG, TAG, "CALL API - close the connection of the profile proxy to the Service"); (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_closeProfileProxy, jni_gatt, gatt); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "closeProfileProxy has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "OUT - CALEClientCloseProfileProxy"); return CA_STATUS_OK; + +error_exit: + CACheckJNIException(env); + return CA_STATUS_FAILED; } @@ -1983,11 +2012,9 @@ CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt) // call disconnect gatt method OIC_LOG(INFO, TAG, "CALL API - disconnect"); (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "disconnect has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } @@ -2044,6 +2071,7 @@ CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remote_address) if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -2069,6 +2097,7 @@ CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remote_address) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); (*env)->ReleaseStringUTFChars(env, remote_address, address); return CA_STATUS_FAILED; } @@ -2123,6 +2152,7 @@ CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt) if (!ret) { OIC_LOG(ERROR, TAG, "discoverServices has not been started"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -2179,6 +2209,7 @@ CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv* env, jobject gatt) const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); if (!address) { + CACheckJNIException(env); CALEClientSendFinish(env, gatt); return CA_STATUS_FAILED; } @@ -2255,6 +2286,7 @@ CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt) VERIFY_NON_NULL(gatt, TAG, "gatt is null"); jobject gattParam = (*env)->NewGlobalRef(env, gatt); + CACheckJNIException(env); if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEWriteCharacteristicThread, (void*)gattParam)) { @@ -2303,6 +2335,7 @@ CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt, } else { + CACheckJNIException(env); OIC_LOG(ERROR, TAG, "writeCharacteristic has failed"); return CA_STATUS_FAILED; } @@ -2325,6 +2358,7 @@ CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt) if (!jni_uuid) { OIC_LOG(ERROR, TAG, "jni_uuid is null"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -2357,6 +2391,7 @@ CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt) else { OIC_LOG(ERROR, TAG, "readCharacteristic has failed"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -2398,6 +2433,7 @@ CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoot else { OIC_LOG(ERROR, TAG, "setCharacteristicNotification has failed"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -2442,6 +2478,7 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha if (!jni_obj_gattService) { OIC_LOG(ERROR, TAG, "jni_obj_gattService is null"); + CACheckJNIException(env); return NULL; } @@ -2462,6 +2499,7 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha if (!uuid) { OIC_LOG(ERROR, TAG, "uuid is null"); + CACheckJNIException(env); return NULL; } @@ -2473,10 +2511,16 @@ jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring cha return NULL; } - OIC_LOG(DEBUG, TAG, "request to get Characteristic"); + OIC_LOG(DEBUG, TAG, "CALL API getCharacteristic"); jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService, jni_mid_getCharacteristic, jni_obj_tx_uuid); + if (!jni_obj_GattCharacteristic) + { + OIC_LOG(ERROR, TAG, "getCharacteristic has failed"); + CACheckJNIException(env); + return NULL; + } (*env)->ReleaseStringUTFChars(env, characterUUID, uuid); return jni_obj_GattCharacteristic; @@ -2499,7 +2543,7 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j if (!jni_uuid) { OIC_LOG(ERROR, TAG, "jni_uuid is null"); - return NULL; + goto error_exit; } jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid); @@ -2514,7 +2558,7 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j if (!jni_cid_BTGattCharacteristic) { OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null"); - return NULL; + goto error_exit; } OIC_LOG(DEBUG, TAG, "set value in Characteristic"); @@ -2523,7 +2567,7 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j if (!jni_mid_setValue) { OIC_LOG(ERROR, TAG, "jni_mid_setValue is null"); - return NULL; + goto error_exit; } jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue, @@ -2535,7 +2579,7 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j else { OIC_LOG(ERROR, TAG, "the locally stored value hasn't been set"); - return NULL; + goto error_exit; } // set Write Type @@ -2544,7 +2588,7 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j if (!jni_mid_setWriteType) { OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic, @@ -2552,15 +2596,24 @@ jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, j if (!jni_fid_no_response) { OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available"); - return NULL; + goto error_exit; } jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic, jni_fid_no_response); + CACheckJNIException(env); (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val); + if (CACheckJNIException(env)) + { + OIC_LOG(ERROR, TAG, "setWriteType has failed"); + } return jni_obj_GattCharacteristic; + +error_exit: + CACheckJNIException(env); + return NULL; } jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic) @@ -2585,6 +2638,7 @@ jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteris jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic, jni_mid_getValue); + CACheckJNIException(env); return jni_obj_data_array; } @@ -2617,6 +2671,7 @@ CAResult_t CALEClientCreateUUIDList() if (!jni_cid_uuid_list) { OIC_LOG(ERROR, TAG, "jni_cid_uuid_list is null"); + CACheckJNIException(env); goto error_exit; } @@ -2625,6 +2680,7 @@ CAResult_t CALEClientCreateUUIDList() if (!jni_obj_uuid_list) { OIC_LOG(ERROR, TAG, "jni_obj_uuid_list is null"); + CACheckJNIException(env); goto error_exit; } @@ -2638,6 +2694,7 @@ CAResult_t CALEClientCreateUUIDList() (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid); g_uuidList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list); + CACheckJNIException(env); if (isAttached) { @@ -2685,7 +2742,6 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, if (!jni_obj_cc_uuid) { OIC_LOG(ERROR, TAG, "jni_obj_cc_uuid is null"); - return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "request to get descriptor"); @@ -2694,7 +2750,7 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, if (!jni_obj_descriptor) { OIC_LOG(INFO, TAG, "jni_obj_descriptor is null"); - return CA_NOT_SUPPORTED; + goto error_exit; } OIC_LOG(DEBUG, TAG, "set value in descriptor"); @@ -2703,14 +2759,14 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, if (!jni_cid_descriptor) { OIC_LOG(ERROR, TAG, "jni_cid_descriptor is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_descriptor, "setValue", "([B)Z"); if (!jni_mid_setValue) { OIC_LOG(ERROR, TAG, "jni_mid_setValue is null"); - return CA_STATUS_FAILED; + goto error_exit; } jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor, @@ -2718,7 +2774,7 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, if (!jni_fid_NotiValue) { OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null"); - return CA_STATUS_FAILED; + goto error_exit; } OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE"); @@ -2733,7 +2789,7 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, else { OIC_LOG(ERROR, TAG, "setValue has failed"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_writeDescriptor = CAGetJNIMethodID(env, "android/bluetooth/BluetoothGatt", @@ -2756,10 +2812,14 @@ CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt, else { OIC_LOG(ERROR, TAG, "writeDescriptor has failed"); - return CA_STATUS_FAILED; + goto error_exit; } return CA_STATUS_OK; + +error_exit: + CACheckJNIException(env); + return CA_STATUS_FAILED; } void CALEClientCreateScanDeviceList(JNIEnv *env) @@ -2804,6 +2864,7 @@ CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device) if (!remoteAddress) { OIC_LOG(ERROR, TAG, "remoteAddress is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_remoteAddress); oc_mutex_unlock(g_deviceListMutex); return CA_STATUS_FAILED; @@ -2812,6 +2873,7 @@ CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device) if (!CALEClientIsDeviceInScanDeviceList(env, remoteAddress)) { jobject gdevice = (*env)->NewGlobalRef(env, device); + CACheckJNIException(env); u_arraylist_add(g_deviceList, gdevice); oc_cond_signal(g_deviceDescCond); OIC_LOG_V(DEBUG, TAG, "Added a new BT Device in deviceList [%s]", remoteAddress); @@ -2856,6 +2918,7 @@ bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_setAddress); return true; } @@ -2948,6 +3011,7 @@ CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); oc_mutex_unlock(g_deviceListMutex); return CA_STATUS_FAILED; } @@ -2956,6 +3020,7 @@ CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address) if (!remoteAddress) { OIC_LOG(ERROR, TAG, "remoteAddress is null"); + CACheckJNIException(env); (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress); oc_mutex_unlock(g_deviceListMutex); return CA_STATUS_FAILED; @@ -3019,6 +3084,7 @@ CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt) if (!remoteAddress) { OIC_LOG(ERROR, TAG, "remoteAddress is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_remoteAddress); oc_mutex_unlock(g_gattObjectMutex); return CA_STATUS_FAILED; @@ -3065,6 +3131,7 @@ bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_setAddress); return true; } @@ -3114,6 +3181,7 @@ jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_setAddress); oc_mutex_unlock(g_gattObjectMutex); return NULL; @@ -3205,6 +3273,7 @@ CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); oc_mutex_unlock(g_gattObjectMutex); return CA_STATUS_FAILED; } @@ -3222,6 +3291,7 @@ CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt) if (!remoteAddress) { OIC_LOG(ERROR, TAG, "remoteAddress is null"); + CACheckJNIException(env); (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress); oc_mutex_unlock(g_gattObjectMutex); return CA_STATUS_FAILED; @@ -3290,6 +3360,7 @@ CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr) if (!setAddress) { OIC_LOG(ERROR, TAG, "setAddress is null"); + CACheckJNIException(env); oc_mutex_unlock(g_gattObjectMutex); return CA_STATUS_FAILED; } @@ -3347,6 +3418,7 @@ jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) if (!targetAddress) { OIC_LOG(ERROR, TAG, "targetAddress is not available"); + CACheckJNIException(env); return NULL; } @@ -3380,6 +3452,7 @@ jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) jobject jni_obj_device = (*env)->CallObjectMethod(env, jarrayObj, jni_mid_getDevice); if (!jni_obj_device) { + CACheckJNIException(env); oc_mutex_unlock(g_gattObjectMutex); OIC_LOG(ERROR, TAG, "jni_obj_device is null"); (*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress); @@ -3399,6 +3472,7 @@ jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) const char* btAddress = (*env)->GetStringUTFChars(env, jni_btAddress, NULL); if (!btAddress) { + CACheckJNIException(env); oc_mutex_unlock(g_gattObjectMutex); OIC_LOG(ERROR, TAG, "btAddress is not available"); (*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress); @@ -3458,6 +3532,7 @@ CAResult_t CALEClientUpdateDeviceStateWithBtDevice(JNIEnv *env, if (!address) { OIC_LOG(ERROR, TAG, "targetAddress is not available"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_Address); return CA_STATUS_FAILED; } @@ -4198,6 +4273,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); g_leScanCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); } JNIEXPORT void JNICALL @@ -4210,6 +4286,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, j VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); g_leGattCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); } JNIEXPORT void JNICALL @@ -4258,6 +4335,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(J if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); goto error_exit; } OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - address [%s]", address); @@ -4390,6 +4468,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIE const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); if (!address) { + CACheckJNIException(env); CALEClientSendFinish(env, gatt); return; } @@ -4495,6 +4574,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); if (!address) { + CACheckJNIException(env); goto error_exit; } @@ -4522,6 +4602,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( if (g_clientErrorCallback) { jint length = (*env)->GetArrayLength(env, data); + CACheckJNIException(env); g_clientErrorCallback(address, data, length, CA_SEND_FAILED); CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, address, 0, length, false, "writeChar failure"); @@ -4582,6 +4663,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback( jboolean isCopy; jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy); + CACheckJNIException(env); OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %p", jni_byte_responseData); @@ -4608,6 +4690,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback( if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); OICFree(receivedData); return; } @@ -4652,6 +4735,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); if (!address) { + CACheckJNIException(env); goto error_exit; } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c b/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c index 694e651..f9a0a63 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c @@ -275,6 +275,7 @@ static CAResult_t CALEStateConnectedCallback(JNIEnv *env, jstring jni_address, if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -415,6 +416,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); return; } @@ -456,6 +458,7 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCa if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); return; } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c index 2a7cdb5..5ea9fd0 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c @@ -186,6 +186,12 @@ static jint CALEServerGetConnectionState(JNIEnv *env, jobject device) jint jni_state = (jint)(*env)->CallIntMethod(env, g_bluetoothManager, jni_mid_getConnectionState, device, GATT_PROFILE); + if (CACheckJNIException(env)) + { + OIC_LOG(ERROR, TAG, "getConnectionState has failed"); + return -1; + } + OIC_LOG_V(INFO, TAG, "connection state is %d", jni_state); return jni_state; } @@ -232,6 +238,7 @@ jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData) if (!jni_obj_bluetoothGattService) { OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattService is null"); + CACheckJNIException(env); return NULL; } @@ -260,6 +267,7 @@ jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData) if (!jni_obj_bluetoothGattCharacteristic) { OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattCharacteristic is null"); + CACheckJNIException(env); return NULL; } @@ -278,6 +286,7 @@ jobject CALEServerSetResponseData(JNIEnv *env, jbyteArray responseData) if (JNI_FALSE == jni_boolean_setValue) { OIC_LOG(ERROR, TAG, "Fail to set response data"); + CACheckJNIException(env); } OIC_LOG(DEBUG, TAG, "OUT - CALEServerSetResponseData"); @@ -329,6 +338,7 @@ CAResult_t CALEServerSendResponseData(JNIEnv *env, jobject device, jobject respo if (JNI_FALSE == jni_boolean_notifyCharacteristicChanged) { OIC_LOG(ERROR, TAG, "Fail to notify characteristic"); + CACheckJNIException(env); return CA_SEND_FAILED; } @@ -391,6 +401,7 @@ CAResult_t CALEServerSendResponse(JNIEnv *env, jobject device, jint requestId, j if (JNI_FALSE == jni_boolean_sendResponse) { OIC_LOG(ERROR, TAG, "Fail to send response for gatt characteristic write request"); + CACheckJNIException(env); return CA_SEND_FAILED; } @@ -454,7 +465,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_cid_AdvertiseSettings) { OIC_LOG(ERROR, TAG, "jni_cid_AdvertiseSettings is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_AdvertiseSettings = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings, @@ -462,7 +473,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_AdvertiseSettings) { OIC_LOG(ERROR, TAG, "jni_mid_AdvertiseSettings is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_AdvertiseSettings = (*env)->NewObject(env, jni_cid_AdvertiseSettings, @@ -470,7 +481,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_AdvertiseSettings) { OIC_LOG(ERROR, TAG, "jni_AdvertiseSettings is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_setAdvertiseMode = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings, @@ -480,7 +491,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_setAdvertiseMode) { OIC_LOG(ERROR, TAG, "jni_mid_setAdvertiseMode is null"); - return CA_STATUS_FAILED; + goto error_exit; } // 0: Low power, 1: Balanced @@ -489,7 +500,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_setAdvertiseMode) { OIC_LOG(ERROR, TAG, "jni_obj_setAdvertiseMode is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_setConnectable = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings, @@ -499,7 +510,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_setConnectable) { OIC_LOG(ERROR, TAG, "jni_mid_setConnectable is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_setConnectable = (*env)->CallObjectMethod(env, jni_AdvertiseSettings, @@ -507,7 +518,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_setConnectable) { OIC_LOG(ERROR, TAG, "jni_obj_setConnectable is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_setTimeout = (*env)->GetMethodID(env, jni_cid_AdvertiseSettings, "setTimeout", @@ -516,7 +527,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_setTimeout) { OIC_LOG(ERROR, TAG, "jni_mid_setTimeout is null"); - return CA_STATUS_FAILED; + goto error_exit; } //A value of 0 will disable the time limit @@ -525,7 +536,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_setTimeout) { OIC_LOG(ERROR, TAG, "jni_obj_setTimeout is null"); - return CA_STATUS_FAILED; + goto error_exit; } jclass jni_cid_AdvertiseDataBuilder = (*env)->FindClass(env, @@ -534,7 +545,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_cid_AdvertiseDataBuilder) { OIC_LOG(ERROR, TAG, "jni_cid_AdvertiseDataBuilder is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_AdvertiseDataBuilder = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder, @@ -542,7 +553,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_AdvertiseDataBuilder) { OIC_LOG(ERROR, TAG, "jni_mid_AdvertiseDataBuilder is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_AdvertiseDataBuilder = (*env)->NewObject(env, jni_cid_AdvertiseDataBuilder, @@ -550,7 +561,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_AdvertiseDataBuilder) { OIC_LOG(ERROR, TAG, "jni_AdvertiseDataBuilder is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_AdvertiseDataBuilderForScanRsp = (*env)->NewObject(env, @@ -559,7 +570,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_AdvertiseDataBuilderForScanRsp) { OIC_LOG(ERROR, TAG, "jni_AdvertiseDataBuilderForScanRsp is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID); @@ -583,7 +594,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_addServiceUuid) { OIC_LOG(ERROR, TAG, "jni_mid_addServiceUuid is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_addServiceUuid = (*env)->CallObjectMethod(env, jni_AdvertiseDataBuilder, @@ -592,7 +603,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_addServiceUuid) { OIC_LOG(ERROR, TAG, "jni_obj_addServiceUuid is null"); - return CA_STATUS_FAILED; + goto error_exit; } // Device name has to be included in advertise packet after Android API 23 @@ -605,7 +616,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_setIncludeDeviceName) { OIC_LOG(ERROR, TAG, "jni_mid_setIncludeDeviceName is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_setIncludeDeviceName = (*env)->CallObjectMethod(env, @@ -615,7 +626,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_setIncludeDeviceName) { OIC_LOG(ERROR, TAG, "jni_obj_setIncludeDeviceName is null"); - return CA_STATUS_FAILED; + goto error_exit; } jclass jni_cid_BTAdapter = (*env)->FindClass(env, "android/bluetooth/BluetoothAdapter"); @@ -632,7 +643,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, @@ -640,7 +651,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_getBluetoothLeAdvertiser = (*env)->GetMethodID(env, jni_cid_BTAdapter, @@ -650,7 +661,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_getBluetoothLeAdvertiser) { OIC_LOG(ERROR, TAG, "jni_mid_getBluetoothLeAdvertiser is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod( @@ -658,7 +669,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_getBluetoothLeAdvertiser) { OIC_LOG(ERROR, TAG, "jni_obj_getBluetoothLeAdvertiser is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_build_LeAdvertiseSettings = (*env)->GetMethodID(env, @@ -669,7 +680,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_build_LeAdvertiseSettings) { OIC_LOG(ERROR, TAG, "jni_mid_build_LeAdvertiseSettings is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_build_LeAdvertiseSettings = (*env)->CallObjectMethod( @@ -677,7 +688,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_build_LeAdvertiseSettings) { OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseSettings is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_build_LeAdvertiseData = (*env)->GetMethodID(env, jni_cid_AdvertiseDataBuilder, @@ -687,7 +698,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_build_LeAdvertiseData) { OIC_LOG(ERROR, TAG, "jni_mid_build_LeAdvertiseData is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_build_LeAdvertiseData = (*env)->CallObjectMethod(env, jni_AdvertiseDataBuilder, @@ -695,7 +706,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_build_LeAdvertiseData) { OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseData is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_build_LeAdvertiseDataForScanRsp = (*env)->CallObjectMethod(env, @@ -704,7 +715,7 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_build_LeAdvertiseDataForScanRsp) { OIC_LOG(ERROR, TAG, "jni_obj_build_LeAdvertiseDataForScanRsp is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_startAdvertising = CAGetJNIMethodID(env, "android/bluetooth/le/" @@ -724,19 +735,19 @@ CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback) (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_startAdvertising, jni_obj_build_LeAdvertiseSettings, jni_obj_build_LeAdvertiseData, jni_obj_build_LeAdvertiseDataForScanRsp, advertiseCallback); - - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "StartAdvertising has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "Advertising started!!"); - OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartAdvertise"); return CA_STATUS_OK; + +error_exit: + CACheckJNIException(env); + return CA_STATUS_FAILED; } CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) @@ -755,7 +766,7 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, @@ -765,7 +776,7 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_getBTLeAdvertiser = (*env)->GetMethodID(env, jni_cid_BTAdapter, @@ -775,7 +786,7 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_getBTLeAdvertiser) { OIC_LOG(ERROR, TAG, "jni_mid_getBTLeAdvertiser is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_stopAdvertising = CAGetJNIMethodID(env, "android/bluetooth/le/" @@ -786,7 +797,7 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_mid_stopAdvertising) { OIC_LOG(ERROR, TAG, "jni_mid_stopAdvertising is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, @@ -794,7 +805,7 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_getBluetoothLeAdvertiser = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, @@ -802,21 +813,23 @@ CAResult_t CALEServerStopAdvertise(JNIEnv *env, jobject advertiseCallback) if (!jni_obj_getBluetoothLeAdvertiser) { OIC_LOG(ERROR, TAG, "jni_obj_getBluetoothLeAdvertiser is null"); - return CA_STATUS_FAILED; + goto error_exit; } (*env)->CallVoidMethod(env, jni_obj_getBluetoothLeAdvertiser, jni_mid_stopAdvertising, advertiseCallback); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "getBluetoothLeAdvertiser has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "Advertising stopped!!"); return CA_STATUS_OK; + +error_exit: + CACheckJNIException(env); + return CA_STATUS_FAILED; } CAResult_t CALEServerStartGattServer(JNIEnv *env, jobject gattServerCallback) @@ -879,14 +892,14 @@ jobject CALEServerOpenGattServer(JNIEnv *env) if (!CALEIsEnableBTAdapter(env)) { OIC_LOG(ERROR, TAG, "BT adapter is not enabled"); - return NULL; + goto error_exit; } jclass jni_cid_context = (*env)->FindClass(env, "android/content/Context"); if (!jni_cid_context) { OIC_LOG(ERROR, TAG, "jni_cid_context is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_bluetoothService = (*env)->GetStaticFieldID(env, jni_cid_context, @@ -895,7 +908,7 @@ jobject CALEServerOpenGattServer(JNIEnv *env) if (!jni_fid_bluetoothService) { OIC_LOG(ERROR, TAG, "jni_fid_bluetoothService is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_getSystemService = (*env)->GetMethodID(env, jni_cid_context, @@ -905,7 +918,7 @@ jobject CALEServerOpenGattServer(JNIEnv *env) if (!jni_mid_getSystemService) { OIC_LOG(ERROR, TAG, "jni_mid_getSystemService is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_openGattServer = CAGetJNIMethodID(env, "android/bluetooth/" @@ -927,7 +940,7 @@ jobject CALEServerOpenGattServer(JNIEnv *env) if (!jni_obj_bluetoothService) { OIC_LOG(ERROR, TAG, "jni_obj_bluetoothService is null"); - return NULL; + goto error_exit; } jobject jni_obj_bluetoothManager = (*env)->CallObjectMethod(env, g_context, @@ -936,7 +949,7 @@ jobject CALEServerOpenGattServer(JNIEnv *env) if (!jni_obj_bluetoothManager) { OIC_LOG(ERROR, TAG, "jni_obj_bluetoothManager is null"); - return NULL; + goto error_exit; } if (g_bluetoothManager) @@ -952,11 +965,15 @@ jobject CALEServerOpenGattServer(JNIEnv *env) if (!jni_obj_bluetoothGattServer) { OIC_LOG(ERROR, TAG, "jni_obj_bluetoothGattServer is null"); - return NULL; + goto error_exit; } OIC_LOG(DEBUG, TAG, "OUT - CALEServerOpenGattServer"); return jni_obj_bluetoothGattServer; + +error_exit: + CACheckJNIException(env); + return NULL; } jobject CALEServerCreateGattService(JNIEnv *env) @@ -975,7 +992,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_cid_bluetoothGattService) { OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattService is null"); - return NULL; + goto error_exit; } jclass jni_cid_bluetoothGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/" @@ -983,7 +1000,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_cid_bluetoothGattCharacteristic) { OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattCharacteristic is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_serviceType = (*env)->GetStaticFieldID(env, jni_cid_bluetoothGattService, @@ -991,7 +1008,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_fid_serviceType) { OIC_LOG(ERROR, TAG, "jni_fid_serviceType is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_readProperties = (*env)->GetStaticFieldID(env, @@ -1000,7 +1017,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_fid_readProperties) { OIC_LOG(ERROR, TAG, "jni_fid_readProperties is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_writeProperties = (*env)->GetStaticFieldID(env, @@ -1009,7 +1026,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_fid_writeProperties) { OIC_LOG(ERROR, TAG, "jni_fid_writeProperties is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env, @@ -1018,7 +1035,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_fid_readPermissions) { OIC_LOG(ERROR, TAG, "jni_fid_readPermissions is null"); - return NULL; + goto error_exit; } jfieldID jni_fid_writePermissions = (*env)->GetStaticFieldID( @@ -1026,7 +1043,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_fid_writePermissions) { OIC_LOG(ERROR, TAG, "jni_fid_writePermissions is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_bluetoothGattService = (*env)->GetMethodID(env, jni_cid_bluetoothGattService, @@ -1034,7 +1051,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_mid_bluetoothGattService) { OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattService is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_addCharacteristic = (*env)->GetMethodID(env, jni_cid_bluetoothGattService, @@ -1044,7 +1061,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_mid_addCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_addCharacteristic is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_bluetoothGattCharacteristic = (*env)->GetMethodID( @@ -1052,7 +1069,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_mid_bluetoothGattCharacteristic) { OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattCharacteristic is null"); - return NULL; + goto error_exit; } jobject jni_obj_serviceUUID = CALEGetUuidFromString(env, OIC_GATT_SERVICE_UUID); @@ -1070,7 +1087,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_bluetoothGattService) { OIC_LOG(ERROR, TAG, "jni_bluetoothGattService is null"); - return NULL; + goto error_exit; } jobject jni_obj_readUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID); @@ -1083,14 +1100,17 @@ jobject CALEServerCreateGattService(JNIEnv *env) jint jni_int_readProperties = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattCharacteristic, jni_fid_readProperties); + CACheckJNIException(env); jint jni_int_readPermissions = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattCharacteristic, jni_fid_readPermissions); + CACheckJNIException(env); jint jni_int_writePermissions = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattCharacteristic, jni_fid_writePermissions); + CACheckJNIException(env); jobject jni_readCharacteristic = (*env)->NewObject(env, jni_cid_bluetoothGattCharacteristic, jni_mid_bluetoothGattCharacteristic, @@ -1100,7 +1120,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_readCharacteristic) { OIC_LOG(ERROR, TAG, "jni_readCharacteristic is null"); - return NULL; + goto error_exit; } jboolean jni_boolean_addReadCharacteristic = (*env)->CallBooleanMethod( @@ -1108,7 +1128,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_boolean_addReadCharacteristic) { OIC_LOG(ERROR, TAG, "jni_boolean_addReadCharacteristic is null"); - return NULL; + goto error_exit; } jobject jni_obj_writeUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID); @@ -1121,6 +1141,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) jint jni_int_writeProperties = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattCharacteristic, jni_fid_writeProperties); + CACheckJNIException(env); jobject jni_writeCharacteristic = (*env)->NewObject(env, jni_cid_bluetoothGattCharacteristic, jni_mid_bluetoothGattCharacteristic, @@ -1129,7 +1150,7 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (!jni_writeCharacteristic) { OIC_LOG(ERROR, TAG, "jni_writeCharacteristic is null"); - return NULL; + goto error_exit; } jboolean jni_boolean_addWriteCharacteristic = (*env)->CallBooleanMethod( @@ -1137,11 +1158,15 @@ jobject CALEServerCreateGattService(JNIEnv *env) if (JNI_FALSE == jni_boolean_addWriteCharacteristic) { OIC_LOG(ERROR, TAG, "Fail to add jni_boolean_addReadCharacteristic"); - return NULL; + goto error_exit; } OIC_LOG(DEBUG, TAG, "OUT - CALEServerCreateGattService"); return jni_bluetoothGattService; + +error_exit: + CACheckJNIException(env); + return NULL; } CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) @@ -1155,7 +1180,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (!jni_cid_bluetoothGattDescriptor) { OIC_LOG(ERROR, TAG, "jni_cid_bluetoothGattDescriptor is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_bluetoothGattDescriptor = (*env)->GetMethodID(env, @@ -1165,7 +1190,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (!jni_mid_bluetoothGattDescriptor) { OIC_LOG(ERROR, TAG, "jni_mid_bluetoothGattDescriptor is null"); - return CA_STATUS_FAILED; + goto error_exit; } jfieldID jni_fid_readPermissions = (*env)->GetStaticFieldID(env, @@ -1174,7 +1199,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (!jni_fid_readPermissions) { OIC_LOG(ERROR, TAG, "jni_fid_readPermissions is null"); - return CA_STATUS_FAILED; + goto error_exit; } jobject jni_obj_readUuid = CALEGetUuidFromString(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID); @@ -1186,6 +1211,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) jint jni_int_readPermissions = (*env)->GetStaticIntField(env, jni_cid_bluetoothGattDescriptor, jni_fid_readPermissions); + CACheckJNIException(env); OIC_LOG(DEBUG, TAG, "initialize new Descriptor"); @@ -1195,7 +1221,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (!jni_readDescriptor) { OIC_LOG(ERROR, TAG, "jni_readDescriptor is null"); - return CA_STATUS_FAILED; + goto error_exit; } jclass jni_cid_GattCharacteristic = (*env)->FindClass(env, "android/bluetooth/" @@ -1203,7 +1229,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (!jni_cid_GattCharacteristic) { OIC_LOG(ERROR, TAG, "jni_cid_GattCharacteristic is null"); - return CA_STATUS_FAILED; + goto error_exit; } jmethodID jni_mid_addDescriptor = (*env)->GetMethodID(env, jni_cid_GattCharacteristic, @@ -1213,7 +1239,7 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (!jni_mid_addDescriptor) { OIC_LOG(ERROR, TAG, "jni_mid_addDescriptor is null"); - return CA_STATUS_FAILED; + goto error_exit; } jboolean jni_boolean_addDescriptor = (*env)->CallBooleanMethod(env, characteristic, @@ -1223,13 +1249,17 @@ CAResult_t CALEServerAddDescriptor(JNIEnv *env, jobject characteristic) if (JNI_FALSE == jni_boolean_addDescriptor) { OIC_LOG(ERROR, TAG, "addDescriptor has failed"); - return CA_STATUS_FAILED; + goto error_exit; } else { OIC_LOG(DEBUG, TAG, "addDescriptor success"); } return CA_STATUS_OK; + +error_exit: + CACheckJNIException(env); + return CA_STATUS_FAILED; } CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer, @@ -1263,6 +1293,7 @@ CAResult_t CALEServerAddGattService(JNIEnv *env, jobject bluetoothGattServer, if (JNI_FALSE == jni_boolean_addService) { OIC_LOG(ERROR, TAG, "Fail to add GATT service"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -1297,6 +1328,7 @@ CAResult_t CALEServerConnect(JNIEnv *env, jobject bluetoothDevice) if (JNI_FALSE == jni_boolean_connect) { OIC_LOG(ERROR, TAG, "Fail to connect"); + CACheckJNIException(env); return CA_STATUS_FAILED; } @@ -1365,11 +1397,9 @@ CAResult_t CALEServerDisconnect(JNIEnv *env, jobject bluetoothDevice) (*env)->CallVoidMethod(env, g_bluetoothGattServer, jni_mid_cancelConnection, bluetoothDevice); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "cancelConnection has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } @@ -1398,11 +1428,9 @@ CAResult_t CALEServerGattClose(JNIEnv *env, jobject bluetoothGattServer) OIC_LOG(DEBUG, TAG, "request to close GATT"); (*env)->CallVoidMethod(env, bluetoothGattServer, jni_mid_closeGatt); - if ((*env)->ExceptionCheck(env)) + if (CACheckJNIException(env)) { OIC_LOG(ERROR, TAG, "closeGATT has failed"); - (*env)->ExceptionDescribe(env); - (*env)->ExceptionClear(env); return CA_STATUS_FAILED; } @@ -1836,6 +1864,7 @@ CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char* address, co if (jarrayObj) { g_obj_bluetoothDevice = (*env)->NewGlobalRef(env, jarrayObj); + CACheckJNIException(env); } (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress); break; @@ -1846,8 +1875,11 @@ CAResult_t CALEServerSendUnicastMessageImpl(JNIEnv *env, const char* address, co if (g_obj_bluetoothDevice) { jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen); + CACheckJNIException(env); (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data); + CACheckJNIException(env); g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr); + CACheckJNIException(env); CAResult_t res = CALEServerSend(env, g_obj_bluetoothDevice, g_sendBuffer); if (CA_STATUS_OK != res) @@ -1910,8 +1942,11 @@ CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, g_sendBuffer = NULL; } jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen); + CACheckJNIException(env); (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data); + CACheckJNIException(env); g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr); + CACheckJNIException(env); uint32_t length = u_arraylist_length(g_connectedDeviceList); for (uint32_t index = 0; index < length; index++) @@ -1925,7 +1960,9 @@ CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, // send data for all device jbyteArray jni_bytearr_data = (*env)->NewByteArray(env, dataLen); + CACheckJNIException(env); (*env)->SetByteArrayRegion(env, jni_bytearr_data, 0, dataLen, (jbyte*) data); + CACheckJNIException(env); jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj); if (!jni_address) @@ -1950,6 +1987,7 @@ CAResult_t CALEServerSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data, if (jarrayObj) { g_obj_bluetoothDevice = (*env)->NewGlobalRef(env, jarrayObj); + CACheckJNIException(env); } CAResult_t res = CALEServerSend(env, g_obj_bluetoothDevice, jni_bytearr_data); @@ -2202,6 +2240,7 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeRegisterGattServerCallback(JNIEnv * VERIFY_NON_NULL_VOID(callback, TAG, "callback"); g_bluetoothGattServerCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); } JNIEXPORT void JNICALL @@ -2215,6 +2254,7 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeRegisterBluetoothLeAdvertiseCallbac VERIFY_NON_NULL_VOID(callback, TAG, "callback"); g_leAdvertiseCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); } JNIEXPORT void JNICALL @@ -2244,6 +2284,7 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerConnectionStateChangeCall if (!remoteAddress) { OIC_LOG(ERROR, TAG, "remoteAddress is null"); + CACheckJNIException(env); return; } @@ -2320,9 +2361,11 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteReques // get Byte Array and covert to uint8_t* jint length = (*env)->GetArrayLength(env, data); + CACheckJNIException(env); jboolean isCopy; jbyte *jni_byte_requestData = (jbyte *) (*env)->GetByteArrayElements(env, data, &isCopy); + CACheckJNIException(env); uint8_t* requestData = NULL; requestData = OICMalloc(length); @@ -2347,6 +2390,7 @@ Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerCharacteristicWriteReques if (!address) { OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); OICFree(requestData); return; } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c index d863f9e..9e7c14d 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c @@ -39,7 +39,7 @@ jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid) if (!jni_cid_UUID) { OIC_LOG(ERROR, TAG, "jni_cid_UUID is not available"); - return NULL; + goto error_exit; } jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_UUID, "fromString", @@ -48,14 +48,14 @@ jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid) if (!jni_mid_fromString) { OIC_LOG(ERROR, TAG, "jni_mid_fromString is not available"); - return NULL; + goto error_exit; } jstring str_uuid = (*env)->NewStringUTF(env, uuid); if (!str_uuid) { OIC_LOG(ERROR, TAG, "str_uuid is not available"); - return NULL; + goto error_exit; } jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_UUID, jni_mid_fromString, @@ -63,10 +63,14 @@ jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid) if (!jni_obj_uuid) { OIC_LOG(ERROR, TAG, "Fail to get jni uuid object"); - return NULL; + goto error_exit; } return jni_obj_uuid; + +error_exit: + CACheckJNIException(env); + return NULL; } jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid) @@ -78,7 +82,7 @@ jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid) if (!jni_cid_ParcelUuid) { OIC_LOG(ERROR, TAG, "jni_cid_ParcelUuid is not available"); - return NULL; + goto error_exit; } jmethodID jni_mid_ParcelUuid = (*env)->GetMethodID(env, jni_cid_ParcelUuid, "", @@ -86,17 +90,20 @@ jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid) if (!jni_mid_ParcelUuid) { OIC_LOG(ERROR, TAG, "jni_mid_ParcelUuid is not available"); - return NULL; + goto error_exit; } jobject jni_ParcelUuid = (*env)->NewObject(env, jni_cid_ParcelUuid, jni_mid_ParcelUuid, uuid); if (!jni_ParcelUuid) { OIC_LOG(ERROR, TAG, "Fail to get jni ParcelUuid"); - return NULL; + goto error_exit; } return jni_ParcelUuid; +error_exit: + CACheckJNIException(env); + return NULL; } bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice) @@ -114,7 +121,7 @@ bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice) } jint jni_bondState = (jint)(*env)->CallIntMethod(env, bluetoothDevice, jni_mid_getBondState); - + CACheckJNIException(env); OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bondState); if (BOND_BONDED == jni_bondState) @@ -139,19 +146,19 @@ jobjectArray CALEGetBondedDevices(JNIEnv *env) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "getBondedDevices: jni_cid_BTAdapter is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM); - + CACheckJNIException(env); jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter); if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "getBondedDevices: bluetooth adapter is null"); - return NULL; + goto error_exit; } // Get a list of currently paired devices @@ -161,7 +168,7 @@ jobjectArray CALEGetBondedDevices(JNIEnv *env) if (!jni_mid_getBondedDevices) { OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_getBondedDevicesr is null"); - return NULL; + goto error_exit; } jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, @@ -169,7 +176,7 @@ jobjectArray CALEGetBondedDevices(JNIEnv *env) if (!jni_obj_setPairedDevices) { OIC_LOG(ERROR, TAG, "getBondedDevices: jni_obj_setPairedDevices is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_toArray = CAGetJNIMethodID(env, "java/util/Set", "toArray", @@ -185,10 +192,14 @@ jobjectArray CALEGetBondedDevices(JNIEnv *env) if (!jni_arrayPairedDevices) { OIC_LOG(ERROR, TAG, "getBondedDevices: jni_arrayPairedDevices is null"); - return NULL; + goto error_exit; } return jni_arrayPairedDevices; + +error_exit: + CACheckJNIException(env); + return NULL; } jint CALEGetBTStateOnInfo(JNIEnv *env) @@ -199,6 +210,7 @@ jint CALEGetBTStateOnInfo(JNIEnv *env) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "getBTStateOnInfo: jni_cid_BTAdapter is null"); + CACheckJNIException(env); return -1; } @@ -206,10 +218,12 @@ jint CALEGetBTStateOnInfo(JNIEnv *env) if (!jni_fid_stateon) { OIC_LOG(ERROR, TAG, "get_field_state is not available"); + CACheckJNIException(env); return -1; } jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon); + CACheckJNIException(env); OIC_LOG_V(DEBUG, TAG, "bluetooth.STATE_ON state integer value : %d", jni_int_val); return jni_int_val; @@ -236,6 +250,7 @@ jint CALEGetBuildVersion(JNIEnv *env) if (!jni_cls_version) { OIC_LOG(ERROR, TAG, "jni_cls_version is null"); + CACheckJNIException(env); return -1; } @@ -243,10 +258,12 @@ jint CALEGetBuildVersion(JNIEnv *env) if (!jni_fid_sdk) { OIC_LOG(ERROR, TAG, "jni_fid_sdk is null"); + CACheckJNIException(env); return -1; } jint jni_int_sdk = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_sdk); + CACheckJNIException(env); OIC_LOG_V(DEBUG, TAG, "sdk version is %d", jni_int_sdk); return jni_int_sdk; @@ -262,6 +279,7 @@ jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName) if (!jni_cls_version) { OIC_LOG(ERROR, TAG, "jni_cls_version is null"); + CACheckJNIException(env); return -1; } @@ -269,10 +287,12 @@ jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName) if (!jni_fid_version) { OIC_LOG(ERROR, TAG, "jni_fid_version is null"); + CACheckJNIException(env); return -1; } jint jni_int_version = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_version); + CACheckJNIException(env); OIC_LOG_V(DEBUG, TAG, "version [%s] is %d",versionName, jni_int_version); return jni_int_version; @@ -286,6 +306,7 @@ jboolean CALEIsEnableBTAdapter(JNIEnv *env) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null"); + CACheckJNIException(env); return JNI_FALSE; } @@ -295,6 +316,7 @@ jboolean CALEIsEnableBTAdapter(JNIEnv *env) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return JNI_FALSE; } @@ -304,6 +326,7 @@ jboolean CALEIsEnableBTAdapter(JNIEnv *env) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); return JNI_FALSE; } @@ -313,12 +336,14 @@ jboolean CALEIsEnableBTAdapter(JNIEnv *env) if (!jni_mid_isEnable) { OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null"); + CACheckJNIException(env); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); (*env)->DeleteLocalRef(env, jni_obj_BTAdapter); return JNI_FALSE; } jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable); + CACheckJNIException(env); OIC_LOG_V(DEBUG, TAG, "adapter state is %d", jni_isEnable); (*env)->DeleteLocalRef(env, jni_cid_BTAdapter); @@ -345,6 +370,7 @@ jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice) if (!jni_address) { OIC_LOG(ERROR, TAG, "jni_address is null"); + CACheckJNIException(env); return NULL; } @@ -361,6 +387,7 @@ jint CALEGetConstantsValue(JNIEnv *env, const char* classType, const char* name) if (!jni_cid) { OIC_LOG(ERROR, TAG, "jni_cid is null"); + CACheckJNIException(env); return -1; } @@ -369,10 +396,13 @@ jint CALEGetConstantsValue(JNIEnv *env, const char* classType, const char* name) if (!jni_fieldID) { OIC_LOG(ERROR, TAG, "jni_fieldID is null"); + CACheckJNIException(env); return -1; } - return (*env)->GetStaticIntField(env, jni_cid, jni_fieldID); + jint jni_id = (*env)->GetStaticIntField(env, jni_cid, jni_fieldID); + CACheckJNIException(env); + return jni_id; } jobject CALEGetRemoteDevice(JNIEnv *env, jstring address) @@ -386,7 +416,7 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address) if (!jni_cid_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null"); - return NULL; + goto error_exit; } // get remote bt adapter method @@ -396,7 +426,7 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address) if (!jni_mid_getDefaultAdapter) { OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null"); - return NULL; + goto error_exit; } // gat bt adapter object @@ -405,7 +435,7 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address) if (!jni_obj_BTAdapter) { OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null"); - return NULL; + goto error_exit; } jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env, jni_cid_BTAdapter, @@ -414,7 +444,7 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address) if (!jni_mid_getRemoteDevice) { OIC_LOG(ERROR, TAG, "jni_mid_getRemoteDevice is null"); - return NULL; + goto error_exit; } jobject jni_obj_device = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, @@ -423,11 +453,15 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address) if (!jni_obj_device) { OIC_LOG(ERROR, TAG, "jni_obj_device is null"); - return NULL; + goto error_exit; } OIC_LOG(DEBUG, TAG, "OUT - CALEGetRemoteDevice"); return jni_obj_device; + +error_exit: + CACheckJNIException(env); + return NULL; } jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt) @@ -449,6 +483,7 @@ jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt) if (!jni_obj_device) { OIC_LOG(ERROR, TAG, "jni_obj_device is null"); + CACheckJNIException(env); return NULL; } -- 2.7.4