android: update EDR socket info when re-connection
authorJaewook Jung <jw0213.jung@samsung.com>
Thu, 28 Apr 2016 07:53:22 +0000 (16:53 +0900)
committerJon A. Cruz <jon@joncruz.org>
Fri, 29 Apr 2016 07:14:32 +0000 (07:14 +0000)
If a EDR client requests connection to an Android EDR server which had been connected before,
the server has to update the socket info of the client on the list.
Otherwise the server tries to read data of a wrong socket object.

Change-Id: I4978455c26f0b6ade9fbf5cd5756a60d038c039e
Signed-off-by: Jaewook Jung <jw0213.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7959
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jon@joncruz.org>
resource/csdk/connectivity/src/bt_edr_adapter/android/caedrutils.c

index a70ddc0..885ceef 100644 (file)
@@ -556,49 +556,52 @@ void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
 
     const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
 
-    if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
+    if (CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
     {
-        CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) OICCalloc(1, sizeof(*socketInfo));
-        if (!socketInfo)
-        {
-            OIC_LOG(ERROR, TAG, "Out of memory");
-            return;
-        }
+        OIC_LOG(DEBUG, TAG, "the address exists in deviceObjectList. remove it to add new");
+        CAEDRNativeRemoveDeviceSocketBaseAddr(env, jni_remoteAddress);
+    }
 
-        jmethodID jni_mid_getInputStream = CAGetJNIMethodID(env,
-                                                            "android/bluetooth/BluetoothSocket",
-                                                            "getInputStream",
-                                                            "()Ljava/io/InputStream;");
-        if (!jni_mid_getInputStream)
-        {
-            OIC_LOG(ERROR, TAG, "jni_mid_getInputStream is null");
-            return;
-        }
+    (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
+    (*env)->DeleteLocalRef(env, jni_remoteAddress);
 
-        jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, deviceSocket,
-                                                               jni_mid_getInputStream);
-        if (!jni_obj_inputStream)
-        {
-            OIC_LOG(ERROR, TAG, "jni_obj_inputStream is null");
-            return;
-        }
+    CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) OICCalloc(1, sizeof(*socketInfo));
+    if (!socketInfo)
+    {
+        OIC_LOG(ERROR, TAG, "Out of memory");
+        return;
+    }
+
+    jmethodID jni_mid_getInputStream = CAGetJNIMethodID(env, "android/bluetooth/BluetoothSocket",
+                                                             "getInputStream",
+                                                             "()Ljava/io/InputStream;");
+    if (!jni_mid_getInputStream)
+    {
+        OIC_LOG(ERROR, TAG, "jni_mid_getInputStream is null");
+        return;
+    }
 
-        socketInfo->deviceSocket = (*env)->NewGlobalRef(env, deviceSocket);
-        socketInfo->inputStream = (*env)->NewGlobalRef(env, jni_obj_inputStream);
-        (*env)->DeleteLocalRef(env, jni_obj_inputStream);
+    jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, deviceSocket,
+                                                           jni_mid_getInputStream);
+    if (!jni_obj_inputStream)
+    {
+        OIC_LOG(ERROR, TAG, "jni_obj_inputStream is null");
+        return;
+    }
 
-        bool result = u_arraylist_add(g_deviceObjectList, (void *) socketInfo);
-        if (!result)
-        {
-            OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
-            OICFree(socketInfo);
-            return;
-        }
+    socketInfo->deviceSocket = (*env)->NewGlobalRef(env, deviceSocket);
+    socketInfo->inputStream = (*env)->NewGlobalRef(env, jni_obj_inputStream);
+    (*env)->DeleteLocalRef(env, jni_obj_inputStream);
 
-        OIC_LOG(DEBUG, TAG, "add new device socket object to list");
+    bool result = u_arraylist_add(g_deviceObjectList, (void *) socketInfo);
+    if (!result)
+    {
+        OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
+        OICFree(socketInfo);
+        return;
     }
-    (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
-    (*env)->DeleteLocalRef(env, jni_remoteAddress);
+
+    OIC_LOG(DEBUG, TAG, "add new device socket object to list");
 }
 
 bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
@@ -611,7 +614,7 @@ bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
         return false;
     }
 
-    jint length = u_arraylist_length(g_deviceStateList);
+    jint length = u_arraylist_length(g_deviceObjectList);
     for (jint index = 0; index < length; index++)
     {
         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
@@ -678,7 +681,7 @@ void CAEDRNativeSocketCloseToAll(JNIEnv *env)
         return;
     }
 
-    jint length = u_arraylist_length(g_deviceStateList);
+    jint length = u_arraylist_length(g_deviceObjectList);
     for (jint index = 0; index < length; index++)
     {
         jobject jni_obj_socket = (jobject) u_arraylist_get(g_deviceObjectList, index);
@@ -710,7 +713,7 @@ void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env)
         return;
     }
 
-    jint length = u_arraylist_length(g_deviceStateList);
+    jint length = u_arraylist_length(g_deviceObjectList);
     for (jint index = 0; index < length; index++)
     {
 
@@ -750,7 +753,7 @@ void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
         return;
     }
 
-    jint length = u_arraylist_length(g_deviceStateList);
+    jint length = u_arraylist_length(g_deviceObjectList);
     for (jint index = 0; index < length; index++)
     {
         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
@@ -818,7 +821,7 @@ void CAEDRNativeRemoveDeviceSocketBaseAddr(JNIEnv *env, jstring address)
         return;
     }
 
-    jint length = u_arraylist_length(g_deviceStateList);
+    jint length = u_arraylist_length(g_deviceObjectList);
     for (jint index = 0; index < length; index++)
     {
         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,
@@ -903,7 +906,7 @@ jobject CAEDRNativeGetDeviceSocketBaseAddr(JNIEnv *env, const char* remoteAddres
         return NULL;
     }
 
-    jint length = u_arraylist_length(g_deviceStateList);
+    jint length = u_arraylist_length(g_deviceObjectList);
     for (jint index = 0; index < length; index++)
     {
         CAEDRSocketInfo_t *socketInfo = (CAEDRSocketInfo_t *) u_arraylist_get(g_deviceObjectList,