[IOT-1388] fixed duplicate start server in EDR adapter.
authorjihwan.seo <jihwan.seo@samsung.com>
Wed, 5 Oct 2016 11:00:18 +0000 (20:00 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Tue, 18 Oct 2016 06:00:26 +0000 (06:00 +0000)
when both mode(server and client) in App is set.
startDiscoveryServer and startListenningServer is calling.
and then both accept thread
and receive thread is running redundantly

Change-Id: I70395ace6115d52d28d2c034ea24c70aafeb671e
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12799
Reviewed-by: Abhishek Sharma <ce.abhishek@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13065

resource/csdk/connectivity/src/bt_edr_adapter/android/caedrserver.c

index ff7c8e4..e3a0a2a 100644 (file)
@@ -46,6 +46,13 @@ static ca_thread_pool_t g_threadPoolHandle = NULL;
 static JavaVM *g_jvm;
 
 /**
+ * when Both Mode(server and client) in App is set,
+ * startDiscoveryServer and startListenningServer is calling.
+ * and then both accept thread and receive thread is running redundantly.
+ */
+static bool g_isStartServer = false;
+
+/**
  * Maximum CoAP over TCP header length
  * to know the total data length.
  */
@@ -92,6 +99,11 @@ static oc_mutex g_mutexStateList = NULL;
 static oc_mutex g_mutexObjectList = NULL;
 
 /**
+ * Mutex to synchronize start server state.
+ */
+static oc_mutex g_mutexStartServerState = NULL;
+
+/**
  * Thread context information for unicast, multicast and secured unicast server.
  */
 typedef struct
@@ -113,7 +125,7 @@ static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
 
 static void CAReceiveHandler(void *data)
 {
-    OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler..");
+    OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler");
     // Input validation
     VERIFY_NON_NULL_VOID(data, TAG, "Invalid thread context");
 
@@ -252,6 +264,15 @@ CAResult_t CAEDRServerStart()
         return CA_STATUS_NOT_INITIALIZED;
     }
 
+    oc_mutex_lock(g_mutexStartServerState);
+    if (g_isStartServer)
+    {
+        OIC_LOG(DEBUG, TAG, "server already started");
+        oc_mutex_unlock(g_mutexStartServerState);
+        return CA_STATUS_OK;
+    }
+    oc_mutex_unlock(g_mutexStartServerState);
+
     CAResult_t res = CAEDRServerStartAcceptThread();
     if (CA_STATUS_OK == res)
     {
@@ -262,6 +283,9 @@ CAResult_t CAEDRServerStart()
             CAEDRServerStop();
             return CA_STATUS_FAILED;
         }
+        oc_mutex_lock(g_mutexStartServerState);
+        g_isStartServer = true;
+        oc_mutex_unlock(g_mutexStartServerState);
     }
 
     return res;
@@ -298,6 +322,9 @@ CAResult_t CAEDRServerStop()
     }
 
     CAEDRNatvieCloseServerTask(env);
+    oc_mutex_lock(g_mutexStartServerState);
+    g_isStartServer = false;
+    oc_mutex_unlock(g_mutexStartServerState);
 
     if (isAttached)
     {
@@ -343,6 +370,12 @@ static void CAEDRServerDestroyMutex()
         oc_mutex_free(g_mutexObjectList);
         g_mutexObjectList = NULL;
     }
+
+    if (g_mutexStartServerState)
+    {
+        oc_mutex_free(g_mutexStartServerState);
+        g_mutexStartServerState = NULL;
+    }
 }
 
 static CAResult_t CAEDRServerCreateMutex()
@@ -390,6 +423,15 @@ static CAResult_t CAEDRServerCreateMutex()
         return CA_STATUS_FAILED;
     }
 
+    g_mutexStartServerState = oc_mutex_new();
+    if (!g_mutexStartServerState)
+    {
+        OIC_LOG(ERROR, TAG, "Failed to created mutex!");
+
+        CAEDRServerDestroyMutex();
+        return CA_STATUS_FAILED;
+    }
+
     return CA_STATUS_OK;
 }