summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkush.agrawal@samsung.com <kush.agrawal@samsung.com>2019-11-21 20:00:39 +0530
committerDoHyun Pyun <dh79.pyun@samsung.com>2019-11-28 16:26:28 +0900
commit3618a9b6ea86a9c767b7e82832342f7fd5d5723a (patch)
tree622f9906994752759f6ce163521d11157f104608
parent00f7883e3b28725e7177bfe69b4163e2e9e6bf51 (diff)
downloadiotivity-3618a9b6ea86a9c767b7e82832342f7fd5d5723a.tar.gz
iotivity-3618a9b6ea86a9c767b7e82832342f7fd5d5723a.tar.bz2
iotivity-3618a9b6ea86a9c767b7e82832342f7fd5d5723a.zip
Removed duplicated code blocks (#615)
https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/615 (cherry-picked from 1dd8501207e6c6865cf7bd0d5483003c4bad1ff7) Change-Id: I2aae5778988d75b113bf4535241781a5f1678c84 Signed-off-by: Kush <kush.agrawal@samsung.com> Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
-rwxr-xr-x[-rw-r--r--]resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c273
-rwxr-xr-x[-rw-r--r--]resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c205
-rwxr-xr-x[-rw-r--r--]resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c18
-rwxr-xr-x[-rw-r--r--]resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h2
4 files changed, 113 insertions, 385 deletions
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 9bf93301f..6207788de 100644..100755
--- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c
+++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c
@@ -212,19 +212,10 @@ static void CALEScanThread(void* object)
{
(void)object;
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return;
}
oc_mutex_lock(g_threadScanIntervalMutex);
@@ -350,31 +341,13 @@ CAResult_t CALECreateJniInterfaceObject()
{
OIC_LOG(DEBUG, TAG, "CALECreateJniInterfaceObject");
- if (!g_context)
- {
- OIC_LOG(ERROR, TAG, "g_context is null");
- return CA_STATUS_FAILED;
- }
-
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_context, TAG, "g_context is null", CA_STATUS_FAILED);
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context",
@@ -488,25 +461,12 @@ CAResult_t CALEClientInitialize()
CALEClientAddUuid(OIC_GATT_CUSTOM_UUID2, CA_LE_TYPE_CUSTOM_UUID);
CALEClientAddUuid(OIC_GATT_CUSTOM_UUID3, CA_LE_TYPE_CUSTOM_UUID);
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
g_jniIntSdk = CALEGetBuildVersion(env);
@@ -585,25 +545,12 @@ void CALEClientTerminate()
{
OIC_LOG(DEBUG, TAG, "CALEClientTerminate");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return;
- }
+ VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null");
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return ;
}
// stop scan
@@ -698,25 +645,12 @@ jobject CALEClientHiddenConnectGatt(jobject btDevice, const char* address, jbool
{
OIC_LOG(INFO, TAG, "IN - CALEClientHiddenConnectGatt");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return NULL;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", NULL);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return NULL;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return NULL;
}
jstring jni_address = (*env)->NewStringUTF(env, address);
@@ -766,25 +700,12 @@ CAResult_t CALEClientDestroyJniInterface()
{
OIC_LOG(DEBUG, TAG, "CALEClientDestroyJniInterface");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
jclass jni_LeInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
@@ -871,25 +792,12 @@ CAResult_t CALEClientSendMulticastMessage(const uint8_t* data,
OIC_LOG_V(DEBUG, TAG, "CALEClientSendMulticastMessage(%p)", data);
VERIFY_NON_NULL(data, TAG, "data is null");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CALEClientSendMulticastMessageImpl(env, data, dataLen);
@@ -1044,24 +952,12 @@ CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t*
data);
VERIFY_NON_NULL(address, TAG, "address is null");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
oc_mutex_lock(g_threadSendMutex);
@@ -1490,9 +1386,11 @@ CAResult_t CALEClientStartScan()
return CA_STATUS_FAILED;
}
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
+
+ JNIEnv* env = NULL;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
return CA_STATUS_FAILED;
}
@@ -1500,19 +1398,6 @@ CAResult_t CALEClientStartScan()
{
g_setFullScanFlag = true;
}
- bool isAttached = false;
- JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
- }
OIC_LOG(DEBUG, TAG, "CALEClientStartScan");
@@ -2481,24 +2366,12 @@ error_exit:
CAResult_t CALEClientStopScan()
{
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CA_STATUS_FAILED;
@@ -3227,20 +3100,12 @@ CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
static void CALEWriteCharacteristicThread(void* object)
{
VERIFY_NON_NULL_VOID(object, TAG, "object is null");
+ VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null");
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return ;
}
jobject gatt = (jobject)object;
@@ -3745,25 +3610,12 @@ jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteris
CAResult_t CALEClientCreateUUIDList()
{
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
// create new object array
@@ -4881,7 +4733,7 @@ CAResult_t CALEClientInitGattMutexVaraibles()
void CALEClientTerminateGattMutexVariables()
{
-
+
oc_mutex_free(g_bleServerBDAddressMutex);
g_bleServerBDAddressMutex = NULL;
@@ -4970,25 +4822,12 @@ void CAStopLEGattClient()
{
OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return;
- }
+ VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null");
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return ;
}
CAResult_t ret = CALEClientDisconnectAll(env);
@@ -5085,9 +4924,9 @@ CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t
void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
{
-
+
g_CABLEClientDataReceivedCallback = callback;
-
+
}
void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
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 3cbb821a0..98bd82e72 100644..100755
--- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c
+++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c
@@ -96,32 +96,13 @@ CAResult_t CALEServerCreateJniInterfaceObject()
{
OIC_LOG(DEBUG, TAG, "CALEServerCreateJniInterfaceObject");
- if (!g_context)
- {
- OIC_LOG(ERROR, TAG, "g_context is null");
- return CA_STATUS_FAILED;
- }
-
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_context, TAG, "g_context is null",CA_STATUS_FAILED);
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeServerInterface");
@@ -424,26 +405,12 @@ CAResult_t CALEServerStartAdvertise()
return CA_STATUS_OK;
}
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
// start advertise
@@ -765,26 +732,12 @@ error_exit:
CAResult_t CALEServerStopAdvertise()
{
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CALEServerStopAdvertiseImpl(env, g_leAdvertiseCallback);
@@ -1550,26 +1503,12 @@ CAResult_t CALEServerInitialize()
CALeServerJniInit();
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CALECheckPlatformVersion(env, 21);
@@ -1627,26 +1566,12 @@ void CALEServerTerminate()
{
OIC_LOG(DEBUG, TAG, "IN - CALEServerTerminate");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return;
- }
+ VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null");
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return ;
}
if (g_sendBuffer)
@@ -1683,26 +1608,12 @@ CAResult_t CALEServerSendUnicastMessage(const char* address, const uint8_t* data
VERIFY_NON_NULL(address, TAG, "address is null");
VERIFY_NON_NULL(data, TAG, "data is null");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CALEServerSendUnicastMessageImpl(env, address, data, dataLen);
@@ -1724,26 +1635,12 @@ CAResult_t CALEServerSendMulticastMessage(const uint8_t* data, uint32_t dataLen)
OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage(%p)", data);
VERIFY_NON_NULL(data, TAG, "data is null");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CALEServerSendMulticastMessageImpl(env, data, dataLen);
@@ -1776,26 +1673,12 @@ CAResult_t CALEServerStartMulticastServer()
return CA_STATUS_FAILED;
}
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
g_isStartServer = true;
@@ -2685,26 +2568,12 @@ CAResult_t CAStopLEGattServer()
{
OIC_LOG(DEBUG, TAG, "CAStopLEGattServer");
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
+ VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED);
- bool isAttached = false;
JNIEnv* env = NULL;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
+ bool isAttached = false;
+ if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){
+ return CA_STATUS_FAILED;
}
CAResult_t ret = CALEServerGattClose(env, g_bluetoothGattServer);
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 e41acfabe..0d8b35925 100644..100755
--- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c
+++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c
@@ -581,3 +581,21 @@ jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt)
return jni_address;
}
+
+bool CALEAttachCurrentThread(JNIEnv **env, JavaVM *g_jvm, bool *attach_status)
+{
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
+ {
+ res = (*g_jvm)->AttachCurrentThread(g_jvm, env, NULL);
+
+ if (JNI_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
+ return false;
+ }
+ *attach_status = true;
+ }
+
+ return true;
+} \ No newline at end of file
diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h
index ee523031a..b4764c355 100644..100755
--- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h
+++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h
@@ -202,6 +202,8 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address);
*/
jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt);
+bool CALEAttachCurrentThread(JNIEnv **env, JavaVM *g_jvm, bool *attach_status);
+
#ifdef __cplusplus
} /* extern "C" */
#endif