To support autoconnect flag of connectGatt(..) for multi-connection.
authorjihwan.seo <jihwan.seo@samsung.com>
Wed, 2 Mar 2016 07:31:14 +0000 (16:31 +0900)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 2 Mar 2016 20:39:26 +0000 (20:39 +0000)
suitable autoconnect flag will be suppored as per devices
for backgound connection and first connection.

Change-Id: Ib8b3bb7ab6cedd8553d2b3ed51ea15dbc8caff68
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5289
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h
resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c
resource/csdk/connectivity/util/src/camanager/android/caleconnectionmanager.c

index 8939cdc..e3e6a2d 100644 (file)
@@ -96,8 +96,6 @@ static ca_mutex g_scanMutex = NULL;
 
 static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
 
-static jboolean g_autoConnectFlag = JNI_FALSE;
-
 /**
  * check if retry logic for connection routine has to be stopped or not.
  * in case of error value including this method, connection routine has to be stopped.
@@ -382,7 +380,6 @@ void CALEClientTerminate()
     g_threadCond = NULL;
     g_threadWriteCharacteristicCond = NULL;
     g_isSignalSetFlag = false;
-    CALEClientSetAutoConnectFlag(JNI_FALSE);
 
     if (isAttached)
     {
@@ -1027,7 +1024,8 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
         }
 
         // connection request
-        jobject newGatt = CALEClientConnect(env, device, CALEClientGetAutoConnectFlag());
+        jobject newGatt = CALEClientConnect(env, device,
+                                            CALEClientGetAutoConnectFlag(env, jni_address));
         if (NULL == newGatt)
         {
             OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
@@ -1549,16 +1547,72 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
     return CA_STATUS_OK;
 }
 
-void CALEClientSetAutoConnectFlag(jboolean flag)
+CAResult_t CALEClientSetAutoConnectFlag(JNIEnv *env, jstring jni_address, jboolean flag)
 {
-    OIC_LOG_V(INFO, TAG, "auto connect flag is set %d", flag);
-    g_autoConnectFlag = flag;
+    OIC_LOG(DEBUG, TAG, "IN - CALEClientSetAutoConnectFlag");
+    VERIFY_NON_NULL(env, TAG, "env");
+    VERIFY_NON_NULL(jni_address, TAG, "jni_address");
+
+    ca_mutex_lock(g_deviceStateListMutex);
+
+    char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
+    if (!address)
+    {
+        OIC_LOG(ERROR, TAG, "address is not available");
+        return CA_STATUS_FAILED;
+    }
+
+    if (CALEClientIsDeviceInList(address))
+    {
+        CALEState_t* curState = CALEClientGetStateInfo(address);
+        if(!curState)
+        {
+            OIC_LOG(ERROR, TAG, "curState is null");
+            (*env)->ReleaseStringUTFChars(env, jni_address, address);
+            ca_mutex_unlock(g_deviceStateListMutex);
+            return CA_STATUS_FAILED;
+        }
+        OIC_LOG_V(INFO, TAG, "auto connect flag is set %d", flag);
+
+        curState->autoConnectFlag = flag;
+    }
+
+    (*env)->ReleaseStringUTFChars(env, jni_address, address);
+    ca_mutex_unlock(g_deviceStateListMutex);
+    OIC_LOG(DEBUG, TAG, "OUT - CALEClientSetAutoConnectFlag");
+    return CA_STATUS_OK;
 }
 
-jboolean CALEClientGetAutoConnectFlag()
+jboolean CALEClientGetAutoConnectFlag(JNIEnv *env, jstring jni_address)
 {
-    OIC_LOG_V(INFO, TAG, "auto connect flag is %d", g_autoConnectFlag);
-    return g_autoConnectFlag;
+    OIC_LOG(DEBUG, TAG, "IN - CALEClientGetAutoConnectFlag");
+    VERIFY_NON_NULL_RET(env, TAG, "env", false);
+    VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false);
+
+    ca_mutex_lock(g_deviceStateListMutex);
+
+    char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
+    if (!address)
+    {
+        OIC_LOG(ERROR, TAG, "address is not available");
+        return JNI_FALSE;
+    }
+
+    CALEState_t* curState = CALEClientGetStateInfo(address);
+    if(!curState)
+    {
+        OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false");
+        (*env)->ReleaseStringUTFChars(env, jni_address, address);
+        ca_mutex_unlock(g_deviceStateListMutex);
+        return JNI_FALSE;
+    }
+    OIC_LOG_V(INFO, TAG, "auto connect flag is %d", curState->autoConnectFlag);
+
+    (*env)->ReleaseStringUTFChars(env, jni_address, address);
+    ca_mutex_unlock(g_deviceStateListMutex);
+
+    OIC_LOG(DEBUG, TAG, "OUT - CALEClientGetAutoConnectFlag");
+    return curState->autoConnectFlag;
 }
 
 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect)
@@ -3300,6 +3354,7 @@ CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
         {
             state->notificationState = curState->notificationState;
         }
+        state->autoConnectFlag = curState->autoConnectFlag;
 
         // delete previous state for update new state
         CAResult_t res = CALEClientRemoveDeviceState(state->address);
@@ -3384,6 +3439,39 @@ CAResult_t CALEClientRemoveAllDeviceState()
     return CA_STATUS_OK;
 }
 
+CAResult_t CALEClientResetDeviceStateForAll()
+{
+    OIC_LOG(DEBUG, TAG, "CALEClientResetDeviceStateForAll");
+
+    ca_mutex_lock(g_deviceStateListMutex);
+    if (!g_deviceStateList)
+    {
+        OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
+        ca_mutex_unlock(g_deviceStateListMutex);
+        return CA_STATUS_FAILED;
+    }
+
+    size_t length = u_arraylist_length(g_deviceStateList);
+    for (size_t index = 0; index < length; index++)
+    {
+        CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
+        if (!state)
+        {
+            OIC_LOG(ERROR, TAG, "jarrayObj is null");
+            continue;
+        }
+
+        // autoConnectFlag value will be not changed,
+        // since it has reset only termination case.
+        state->connectedState = STATE_DISCONNECTED;
+        state->notificationState = STATE_CHARACTER_UNSET;
+        state->sendState = STATE_SEND_NONE;
+    }
+    ca_mutex_unlock(g_deviceStateListMutex);
+
+    return CA_STATUS_OK;
+}
+
 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
 {
     OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
@@ -4089,7 +4177,8 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(J
                 goto error_exit;
             }
 
-            jobject newGatt = CALEClientConnect(env, btObject, CALEClientGetAutoConnectFlag());
+            jobject newGatt = CALEClientConnect(env, btObject,
+                                                CALEClientGetAutoConnectFlag(env, leAddress));
             if (!newGatt)
             {
                 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
index 5d28318..ad2f8da 100644 (file)
@@ -54,6 +54,7 @@ typedef struct le_state_info
     jint connectedState;
     uint16_t notificationState;
     uint16_t sendState;
+    jboolean autoConnectFlag;
 } CALEState_t;
 
 /**
@@ -275,15 +276,19 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
 
 /**
  * set auto connect flag for connectGatt API.
- * @param[in]   flag        auto connect flag.
+ * @param[in]   env                   JNI interface pointer.
+ * @param[in]   jni_address           remote address.
+ * @param[in]   flag                  auto connect flag.
  */
-void CALEClientSetAutoConnectFlag(jboolean flag);
+CAResult_t CALEClientSetAutoConnectFlag(JNIEnv *env, jstring jni_address, jboolean flag);
 
 /**
  * get auto connect flag.
+ * @param[in]   env                   JNI interface pointer.
+ * @param[in]   jni_address           remote address.
  * @return  current auto connect flag;
  */
-jboolean CALEClientGetAutoConnectFlag();
+jboolean CALEClientGetAutoConnectFlag(JNIEnv *env, jstring jni_address);
 
 /**
  * connect to gatt server hosted.
@@ -532,6 +537,13 @@ bool CALEClientIsDeviceInList(const char *remoteAddress);
 CAResult_t CALEClientRemoveAllDeviceState();
 
 /**
+ * Reset values of device state for all of devices.
+ * this method has to be invoked when BT adapter is disabled.
+ * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
+ */
+CAResult_t CALEClientResetDeviceStateForAll();
+
+/**
  * remove the device state for a remote device.
  * @param[in]   remoteAddress         remote address.
  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
index 1fa93b8..488b269 100644 (file)
@@ -304,10 +304,10 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, j
             OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
         }
 
-        res = CALEClientRemoveAllDeviceState();
+        res = CALEClientResetDeviceStateForAll();
         if (CA_STATUS_OK != res)
         {
-            OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
+            OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
         }
 
         // remove obj for server
@@ -361,11 +361,4 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *en
     {
         OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
     }
-
-    const char* address = (*env)->GetStringUTFChars(env, jaddr, NULL);
-    if (!address)
-    {
-        OIC_LOG(ERROR, TAG, "address is null");
-        return;
-    }
 }
index b479219..30b379a 100644 (file)
@@ -585,6 +585,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caManagerLeServicesDiscoveredCallback(J
 
                 // next connection will be requested as JNI_TRUE flag
                 // after first connection
+                CALEClientSetAutoConnectFlag(env, jni_str_entry, JNI_TRUE);
             }
             else
             {