From 15a2b5d9adeb1a00204726853ad8fd168f264a78 Mon Sep 17 00:00:00 2001 From: Kush Date: Thu, 6 Feb 2020 13:22:04 +0530 Subject: [PATCH] Adding fix for bi directional request https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/664 (cherry-picked from e7010477b877fc74ba3773c2661ec289da6de8ae) Change-Id: Ia7c0bb9dc15c0fde7bbd34262fc39c57356ddf66 Signed-off-by: Kush Signed-off-by: DoHyun Pyun --- .../connectivity/src/bt_le_adapter/caleadapter.c | 62 ++++++++++++++++------ 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 4d9bf62..ce09f97 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -154,6 +154,11 @@ static oc_mutex g_bleClientReceiveDataMutex = NULL; static oc_mutex g_bleServerReceiveDataMutex = NULL; /** + * Mutex to synchronize the updates of current device role. + */ +static oc_mutex g_bleDeviceRoleMutex= NULL; + +/** * Callback to be called when network packet received from either * GattServer or GattClient. */ @@ -2050,6 +2055,16 @@ static CAResult_t CAInitLEAdapterMutex() } } + if (NULL == g_bleDeviceRoleMutex) + { + g_bleDeviceRoleMutex = oc_mutex_new(); + if (NULL == g_bleDeviceRoleMutex) + { + OIC_LOG(ERROR, CALEADAPTER_TAG, "oc_mutex_new failed"); + return CA_STATUS_FAILED; + } + } + return CA_STATUS_OK; } @@ -2072,6 +2087,9 @@ static void CATerminateLEAdapterMutex() oc_mutex_free(g_senderInfoMutex); g_senderInfoMutex = NULL; + + oc_mutex_free(g_bleDeviceRoleMutex); + g_bleDeviceRoleMutex = NULL; } /** @@ -2396,7 +2414,8 @@ static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint, const void *data, si ssize_t ret = 0; if (ADAPTER_SERVER == g_adapterType || - (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType)) + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType) || + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA_SERVER_INIT== dataType)) { result = CALEAdapterServerSendData(endpoint, data, dataLen); if (CA_STATUS_OK != result) @@ -2413,7 +2432,8 @@ static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint, const void *data, si } else if (ADAPTER_CLIENT == g_adapterType || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) || - (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType)) + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType) || + (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA_CLIENT_INIT== dataType)) { result = CALEAdapterClientSendData(endpoint, data, dataLen); if (CA_STATUS_OK != result) @@ -2771,7 +2791,9 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, CAResult_t result = CA_STATUS_FAILED; - OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "g_adapterType: %d", g_adapterType); + oc_mutex_lock(g_bleDeviceRoleMutex); + + OIC_LOG_V(INFO, CALEADAPTER_TAG, "g_adapterType: %d g_currentRole %d", g_adapterType, g_currentDeviceRole); if (ADAPTER_EMPTY == g_adapterType) { OIC_LOG(ERROR, CALEADAPTER_TAG, "g_adapterType is Empty"); @@ -2784,12 +2806,21 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, OIC_LOG(DEBUG, CALEADAPTER_TAG, "Response Message from client" ); dataType = CA_RESPONSE_DATA_CLIENT_INIT; } + oc_mutex_unlock(g_bleDeviceRoleMutex); oc_mutex_lock(g_bleIsServerMutex); if (ADAPTER_SERVER == g_adapterType || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType) || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA_SERVER_INIT== dataType)) { + oc_mutex_lock(g_bleDeviceRoleMutex); + if(g_currentDeviceRole == ADAPTER_EMPTY) + { + OIC_LOG(INFO, CALEADAPTER_TAG, "Setting current device role as server" ); + g_currentDeviceRole = ADAPTER_SERVER; + } + oc_mutex_unlock(g_bleDeviceRoleMutex); + #ifdef __WITH_DTLS__ if (endpoint && endpoint->flags & CA_SECURE) { @@ -2817,12 +2848,6 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, result = CALEAdapterServerSendData(endpoint, data, dataLen); #endif - if(g_currentDeviceRole == ADAPTER_EMPTY) - { - OIC_LOG(INFO, CALEADAPTER_TAG, "Setting current device role as server" ); - g_currentDeviceRole = ADAPTER_SERVER; - } - if (CA_STATUS_OK != result) { oc_mutex_unlock(g_bleIsServerMutex); @@ -2840,6 +2865,15 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType) || (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA_CLIENT_INIT== dataType)) { + + oc_mutex_lock(g_bleDeviceRoleMutex); + if(g_currentDeviceRole == ADAPTER_EMPTY) + { + OIC_LOG(INFO, CALEADAPTER_TAG, "Setting current device role as client" ); + g_currentDeviceRole = ADAPTER_CLIENT; + } + oc_mutex_unlock(g_bleDeviceRoleMutex); + #ifdef __WITH_DTLS__ if (endpoint && endpoint->flags & CA_SECURE) { @@ -2867,12 +2901,6 @@ static int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, result = CALEAdapterClientSendData(endpoint, data, dataLen); #endif - if(g_currentDeviceRole == ADAPTER_EMPTY) - { - OIC_LOG(INFO, CALEADAPTER_TAG, "Setting current device role as client" ); - g_currentDeviceRole = ADAPTER_CLIENT; - } - if (CA_STATUS_OK != result) { oc_mutex_unlock(g_bleIsServerMutex); @@ -3091,6 +3119,10 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha if(!isConnected) { + oc_mutex_lock(g_bleDeviceRoleMutex); + g_currentDeviceRole = ADAPTER_EMPTY; + oc_mutex_unlock(g_bleDeviceRoleMutex); + #ifndef SINGLE_THREAD if(g_bleClientSenderInfo) { -- 2.7.4