#include "cainterface.h"
#include "caipinterface.h"
#include <coap/utlist.h>
+#include "octhread.h"
#ifdef RA_ADAPTER
#include "caraadapter.h"
static struct CANetworkCallback_t * g_networkChangeCallbackList = NULL;
+/**
+ * Mutex to synchronize network change list.
+ */
+static oc_mutex g_mutexNetCallbackList = NULL;
+
/**
* network callback structure is handling
* for adapter state changed and connection state changed event.
return CA_STATUS_INVALID_PARAM;
}
+ oc_mutex_lock(g_mutexNetCallbackList);
CANetworkCallback_t* callback = NULL;
LL_FOREACH(g_networkChangeCallbackList, callback)
{
if (callback && adapterCB == callback->adapter && connCB == callback->conn)
{
OIC_LOG(DEBUG, TAG, "this callback is already added");
+ oc_mutex_unlock(g_mutexNetCallbackList);
return CA_STATUS_OK;
}
}
if (NULL == callback)
{
OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
+ oc_mutex_unlock(g_mutexNetCallbackList);
return CA_MEMORY_ALLOC_FAILED;
}
callback->adapter = adapterCB;
callback->conn = connCB;
LL_APPEND(g_networkChangeCallbackList, callback);
+ oc_mutex_unlock(g_mutexNetCallbackList);
OIC_LOG_V(INFO, TAG, "Added NetworkStateChanged Callback [%p]", callback);
return CA_STATUS_OK;
{
OIC_LOG(DEBUG, TAG, "Remove NetworkStateChanged Callback");
+ oc_mutex_lock(g_mutexNetCallbackList);
CANetworkCallback_t* callback = NULL;
LL_FOREACH(g_networkChangeCallbackList, callback)
{
{
OIC_LOG(DEBUG, TAG, "remove specific callback");
LL_DELETE(g_networkChangeCallbackList, callback);
+ oc_mutex_unlock(g_mutexNetCallbackList);
OICFree(callback);
return CA_STATUS_OK;
}
}
+ oc_mutex_unlock(g_mutexNetCallbackList);
+
return CA_STATUS_OK;
}
static void CAAdapterChangedCallback(CATransportAdapter_t adapter, CANetworkStatus_t status)
{
+
+ oc_mutex_lock(g_mutexNetCallbackList);
// Call the callback.
CANetworkCallback_t* callback = NULL;
LL_FOREACH(g_networkChangeCallbackList, callback)
OIC_LOG_V(INFO, TAG, "OUT application adapter changed callback [%p]", callback);
}
}
+ oc_mutex_unlock(g_mutexNetCallbackList);
OIC_LOG_V(DEBUG, TAG, "[%d] adapter status is changed to [%d]", adapter, status);
}
#if defined(TCP_ADAPTER) || defined(EDR_ADAPTER) || defined(LE_ADAPTER)
static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
{
+ oc_mutex_lock(g_mutexNetCallbackList);
// Call the callback.
CANetworkCallback_t* callback = NULL;
LL_FOREACH(g_networkChangeCallbackList, callback)
callback->conn(info, isConnected);
}
}
+ oc_mutex_unlock(g_mutexNetCallbackList);
OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
}
#endif
}
}
+static void CADestroyMutex()
+{
+ if (g_mutexNetCallbackList)
+ {
+ oc_mutex_free(g_mutexNetCallbackList);
+ g_mutexNetCallbackList = NULL;
+ }
+}
+
+static CAResult_t CACreateMutex()
+{
+ if (!g_mutexNetCallbackList)
+ {
+ g_mutexNetCallbackList = oc_mutex_new();
+ if (!g_mutexNetCallbackList)
+ {
+ return CA_STATUS_FAILED;
+ }
+ }
+
+ return CA_STATUS_OK;
+}
+
void CAInitializeAdapters(ca_thread_pool_t handle, CATransportAdapter_t transportType)
{
OIC_LOG_V(DEBUG, TAG, "initialize adapters %d", transportType);
+ if (CA_STATUS_OK != CACreateMutex())
+ {
+ OIC_LOG(ERROR, TAG, "Failed to create mutex!");
+ }
+
// Initialize ssl adapter.
#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
if (CA_STATUS_OK != CAinitSslAdapter())
void CATerminateAdapters()
{
+ CADestroyMutex();
+
for (uint32_t index = 0; index < g_numberOfAdapters; index++)
{
if (g_adapterHandler[index].terminate != NULL)