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.
*/
}
}
+ 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;
}
oc_mutex_free(g_senderInfoMutex);
g_senderInfoMutex = NULL;
+
+ oc_mutex_free(g_bleDeviceRoleMutex);
+ g_bleDeviceRoleMutex = NULL;
}
/**
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)
}
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)
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");
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)
{
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);
(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)
{
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);
if(!isConnected)
{
+ oc_mutex_lock(g_bleDeviceRoleMutex);
+ g_currentDeviceRole = ADAPTER_EMPTY;
+ oc_mutex_unlock(g_bleDeviceRoleMutex);
+
#ifndef SINGLE_THREAD
if(g_bleClientSenderInfo)
{