added direct gatt connect in Android
authorjihwan.seo <jihwan.seo@samsung.com>
Wed, 18 May 2016 11:45:33 +0000 (20:45 +0900)
committerJon A. Cruz <jon@joncruz.org>
Fri, 27 May 2016 02:35:09 +0000 (02:35 +0000)
it can prevent to connect duplicatly while CASendRequest is working.

Change-Id: Ib8690c8e3ce7da43cab958473da33fa748e9ec56
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8217
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jon@joncruz.org>
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h
resource/csdk/connectivity/util/src/camanager/android/caleautoconnector.c
resource/csdk/connectivity/util/src/camanager/android/caleautoconnector.h

index abb9f1b..7f0ffd2 100644 (file)
@@ -1582,6 +1582,46 @@ jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state
     return ret;
 }
 
+CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect)
+{
+    OIC_LOG(DEBUG, TAG, "CALEClientDirectConnect");
+    VERIFY_NON_NULL(env, TAG, "env is null");
+    VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
+
+    ca_mutex_lock(g_threadSendMutex);
+
+    jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
+    if (!jni_address)
+    {
+        OIC_LOG(ERROR, TAG, "jni_address is not available");
+        ca_mutex_unlock(g_threadSendMutex);
+        return CA_STATUS_FAILED;
+    }
+
+    const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
+    if (!address)
+    {
+        OIC_LOG(ERROR, TAG, "address is not available");
+        ca_mutex_unlock(g_threadSendMutex);
+        return CA_STATUS_FAILED;
+    }
+
+    CAResult_t res = CA_STATUS_OK;
+    if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE,
+                              STATE_DISCONNECTED))
+    {
+        jobject newGatt = CALEClientConnect(env, bluetoothDevice, autoconnect);
+        if (NULL == newGatt)
+        {
+            OIC_LOG(INFO, TAG, "newGatt is not available");
+            res = CA_STATUS_FAILED;
+        }
+    }
+    ca_mutex_unlock(g_threadSendMutex);
+
+    return res;
+}
+
 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect)
 {
     OIC_LOG(DEBUG, TAG, "CALEClientConnect");
index df49c44..ee5ffc8 100644 (file)
@@ -594,6 +594,15 @@ CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
  */
 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
 
+/**
+ * connect to GATT Server hosted by this device directly.
+ * @param[in]   env                   JNI interface pointer.
+ * @param[in]   bluetoothDevice       bluetooth device object.
+ * @param[in]   autoconnect           connect as soon as the device becomes avaiable(true).
+ * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
+ */
+CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index 6416674..03cd4e7 100644 (file)
@@ -137,11 +137,13 @@ CAResult_t CAManagerStartAutoConnection(JNIEnv *env, jstring remote_le_address)
         return CA_STATUS_FAILED;
     }
 
+    CAResult_t res = CA_STATUS_OK;
     for (size_t retry_cnt = 0 ; retry_cnt < MAX_RETRY_COUNT ; retry_cnt++)
     {
         // there is retry logic 5 times when connectGatt call has failed
         // because BT adapter might be not ready yet.
-        if (NULL == CAManagerConnectGatt(env, remote_le_address))
+        res = CAManagerConnectGatt(env, remote_le_address);
+        if (CA_STATUS_OK != res)
         {
             OIC_LOG_V(INFO, TAG, "retry will be started at least %d times after delay 1sec",
                       MAX_RETRY_COUNT - retry_cnt - 1);
@@ -161,13 +163,13 @@ CAResult_t CAManagerStartAutoConnection(JNIEnv *env, jstring remote_le_address)
     }
     ca_mutex_unlock(g_connectRetryMutex);
     OIC_LOG(DEBUG, TAG, "OUT - CAManagerStartAutoConnection");
-    return CA_STATUS_OK;
+    return res;
 }
 
-jobject CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
+CAResult_t CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
 {
-    VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
-    VERIFY_NON_NULL_RET(remote_le_address, TAG, "remote_le_address", NULL);
+    VERIFY_NON_NULL(env, TAG, "env");
+    VERIFY_NON_NULL(remote_le_address, TAG, "remote_le_address");
 
     OIC_LOG(DEBUG, TAG, "IN - CAManagerConnectGatt");
 
@@ -175,7 +177,7 @@ jobject CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
     if (!jni_bluetooth)
     {
         OIC_LOG(ERROR, TAG, "jni_bluetooth is null");
-        return NULL;
+        return CA_STATUS_FAILED;
     }
 
     if (!CAManagerIsDeviceBonded(env, jni_bluetooth))
@@ -184,19 +186,19 @@ jobject CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
     }
 
     // request to connection with AutoConnection Flag
-    OIC_LOG(INFO, TAG, "request gatt connection by CM auto connector");
-    jobject newGatt = (jobject)CALEClientConnect(env, jni_bluetooth, JNI_TRUE);
-    if (NULL == newGatt)
+    OIC_LOG(INFO, TAG, "request to gatt connection for auto connection");
+    CAResult_t res = CALEClientDirectConnect(env, jni_bluetooth, JNI_TRUE);
+    if (CA_STATUS_OK != res)
     {
         OIC_LOG(INFO, TAG, "re-connection will be started");
-        return NULL;
+        return res;
     }
 
     // set flag auto connection is requested.
     CAManagerSetAutoConnectingFlag(env, remote_le_address, true);
 
     OIC_LOG(DEBUG, TAG, "OUT - CAManagerConnectGatt");
-    return newGatt;
+    return CA_STATUS_OK;
 }
 
 CAResult_t CAManagerProcessRecovery(JNIEnv *env, uint16_t adapter_state)
index 4875a88..b7d88fe 100644 (file)
@@ -43,9 +43,9 @@ CAResult_t CAManagerStartAutoConnection(JNIEnv *env, jstring remote_le_address);
  * request connect gatt on client in adapter
  * @param[in]   env                   JNI interface pointer.
  * @param[in]   remote_le_address     remote address.
- * @return  gatt profile object.
+ * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
  */
-jobject CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address);
+CAResult_t CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address);
 
 /**
  * initialize LE AutoConnection.