From df1c2dcd5815b6fc069d13552069365c297adc75 Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Wed, 9 Mar 2016 09:58:18 +0900 Subject: [PATCH] clean up for android BLE client - release variable related to JNI - terminate Glabal variable - add some comment Change-Id: I342a7fd9a8550d0a3c2bf139254e5a147e2dd4dc Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/5591 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- android/android_api/base/jni/JniCaInterface.c | 4 + resource/csdk/connectivity/api/cautilinterface.h | 12 +++ .../src/bt_le_adapter/android/caleclient.c | 15 ++-- .../connectivity/src/bt_le_adapter/caleadapter.c | 87 +++++++++++++++++----- 4 files changed, 95 insertions(+), 23 deletions(-) diff --git a/android/android_api/base/jni/JniCaInterface.c b/android/android_api/base/jni/JniCaInterface.c index 13b7042..d641f3d 100644 --- a/android/android_api/base/jni/JniCaInterface.c +++ b/android/android_api/base/jni/JniCaInterface.c @@ -258,6 +258,8 @@ Java_org_iotivity_ca_CaInterface_caManagerSetAutoConnectionDeviceInfo(JNIEnv *en } CASetAutoConnectionDeviceInfo(address); + + (*env)->ReleaseStringUTFChars(env, jaddress, address); } JNIEXPORT void JNICALL @@ -275,6 +277,8 @@ Java_org_iotivity_ca_CaInterface_caManagerUnsetAutoConnectionDeviceInfo(JNIEnv * } CAUnsetAutoConnectionDeviceInfo(address); + + (*env)->ReleaseStringUTFChars(env, jaddress, address); } JNIEXPORT void JNICALL diff --git a/resource/csdk/connectivity/api/cautilinterface.h b/resource/csdk/connectivity/api/cautilinterface.h index 5e852d4..cdd377b 100644 --- a/resource/csdk/connectivity/api/cautilinterface.h +++ b/resource/csdk/connectivity/api/cautilinterface.h @@ -54,8 +54,20 @@ typedef void (*CAAdapterStateChangedCB)(CATransportAdapter_t adapter, bool enabl CAResult_t CARegisterNetworkMonitorHandler(CAAdapterStateChangedCB adapterStateCB, CAConnectionStateChangedCB connStateCB); +/** + * Set device to handle for auto connection. + * @param[in] address LE address to set. + * + * @return ::CA_STATUS_OK or ::CA_STATUS_FAILED. + */ CAResult_t CASetAutoConnectionDeviceInfo(const char* address); +/** + * Unset device to handle for auto connection. + * @param[in] address LE address to unset. + * + * @return ::CA_STATUS_OK or ::CA_STATUS_FAILED. + */ CAResult_t CAUnsetAutoConnectionDeviceInfo(const char* address); #ifdef __ANDROID__ 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 e3e6a2d..6bd0edc 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -1025,7 +1025,7 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) // connection request jobject newGatt = CALEClientConnect(env, device, - CALEClientGetAutoConnectFlag(env, jni_address)); + JNI_FALSE); if (NULL == newGatt) { OIC_LOG(ERROR, TAG, "CALEClientConnect has failed"); @@ -1095,7 +1095,8 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) } OIC_LOG(DEBUG, TAG, "start to connect LE"); - jobject gatt = CALEClientConnect(env, device, JNI_TRUE); + jobject gatt = CALEClientConnect(env, device, + CALEClientGetAutoConnectFlag(env, jni_address)); if (NULL == gatt) { OIC_LOG(ERROR, TAG, "CALEClientConnect has failed"); @@ -2772,6 +2773,7 @@ CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env) continue; } (*env)->DeleteGlobalRef(env, jarrayObj); + jarrayObj = NULL; } OICFree(g_deviceList); @@ -2836,6 +2838,7 @@ CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address) { OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress); (*env)->DeleteGlobalRef(env, jarrayObj); + jarrayObj = NULL; (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress); (*env)->ReleaseStringUTFChars(env, address, remoteAddress); @@ -3026,6 +3029,7 @@ CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env) continue; } (*env)->DeleteGlobalRef(env, jarrayObj); + jarrayObj = NULL; } OICFree(g_gattObjectList); @@ -3098,6 +3102,7 @@ CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt) { OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress); (*env)->DeleteGlobalRef(env, jarrayObj); + jarrayObj = NULL; (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress); (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress); @@ -3173,7 +3178,7 @@ CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr) { OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress); (*env)->DeleteGlobalRef(env, jarrayObj); - + jarrayObj = NULL; (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress); (*env)->ReleaseStringUTFChars(env, addr, remoteAddress); if (NULL == u_arraylist_remove(g_gattObjectList, index)) @@ -4177,13 +4182,13 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(J goto error_exit; } - jobject newGatt = CALEClientConnect(env, btObject, - CALEClientGetAutoConnectFlag(env, leAddress)); + jobject newGatt = CALEClientConnect(env, btObject, JNI_TRUE); if (!newGatt) { OIC_LOG(ERROR, TAG, "CALEClientConnect has failed"); goto error_exit; } + return; } else diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 16122fe..0d07691 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -29,6 +29,10 @@ #ifndef SINGLE_THREAD #include "caqueueingthread.h" #endif +#if defined(__TIZEN__) || defined(__ANDROID__) +#include "caleserver.h" +#include "caleclient.h" +#endif #include "oic_malloc.h" #include "oic_string.h" #include "caremotehandler.h" @@ -37,7 +41,7 @@ /** * Logging tag for module name. */ -#define CALEADAPTER_TAG "OIC_LE_ADAP" +#define CALEADAPTER_TAG "OIC_CA_LE_ADAP" /** * The MTU supported for BLE adapter @@ -390,6 +394,19 @@ static void CAFreeLEData(CALEData_t *bleData); */ static void CALEDataDestroyer(void *data, uint32_t size); +#ifndef SINGLE_THREAD +/** + * remove request or response data of send queue. + * + * @param[in] queueHandle queue to process the outgoing packets. + * @param[in] mutex mutex related to sender for client / server. + * @param[in] address target address to remove data in queue. + */ +static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, + ca_mutex mutex, + const char* address); +#endif + static CAResult_t CAInitLEServerQueues() { OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN"); @@ -2217,25 +2234,19 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha } } - if(g_bleServerSenderInfo) + // remove data of send queue. + if (g_bleClientSendQueueHandle) { - CABLESenderInfo_t *senderInfo = NULL; - uint32_t senderIndex = 0; - - if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleServerSenderInfo, &senderInfo, - &senderIndex)) - { - u_arraylist_remove(g_bleServerSenderInfo, senderIndex); - OICFree(senderInfo->defragData); - OICFree(senderInfo->remoteEndpoint); - OICFree(senderInfo); + CALERemoveSendQueueData(g_bleClientSendQueueHandle, + g_bleClientSendDataMutex, + address); + } - OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection"); - } - else - { - OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist"); - } + if (g_bleServerSendQueueHandle) + { + CALERemoveSendQueueData(g_bleServerSendQueueHandle, + g_bleServerSendDataMutex, + address); } #endif } @@ -2585,3 +2596,43 @@ static void CALEErrorHandler(const char *remoteAddress, OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT"); } + +#ifndef SINGLE_THREAD +static void CALERemoveSendQueueData(CAQueueingThread_t *queueHandle, ca_mutex mutex, + const char* address) +{ + OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALERemoveSendQueueData"); + + VERIFY_NON_NULL_VOID(queueHandle, CALEADAPTER_TAG, "queueHandle"); + VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address"); + + ca_mutex_lock(mutex); + while (u_queue_get_size(queueHandle->dataQueue) > 0) + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, "get data from queue"); + u_queue_message_t *message = u_queue_get_element(queueHandle->dataQueue); + if (NULL != message) + { + CALEData_t *bleData = (CALEData_t *) message->msg; + if (bleData && bleData->remoteEndpoint) + { + if (!strcmp(bleData->remoteEndpoint->addr, address)) + { + OIC_LOG(DEBUG, CALEADAPTER_TAG, "found the message of disconnected device"); + if (NULL != queueHandle->destroy) + { + queueHandle->destroy(message->msg, message->size); + } + else + { + OICFree(message->msg); + } + + OICFree(message); + } + } + } + } + ca_mutex_unlock(mutex); +} +#endif -- 2.7.4