From 959390a43d884a71a5ddec563de2ecf0007bd36d Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Wed, 5 Oct 2016 20:00:18 +0900 Subject: [PATCH] [IOT-1388] fixed duplicate start server in EDR adapter. 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/12799 Reviewed-by: Abhishek Sharma Tested-by: jenkins-iotivity Reviewed-by: Ashok Babu Channa Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/13065 --- .../src/bt_edr_adapter/android/caedrserver.c | 44 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrserver.c b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrserver.c index ff7c8e4..e3a0a2a 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrserver.c +++ b/resource/csdk/connectivity/src/bt_edr_adapter/android/caedrserver.c @@ -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; } -- 2.7.4