#include <string.h>
#include <inttypes.h>
+#include "logger.h"
+#include "oic_malloc.h"
+#include "caadapterutils.h"
+#include "canetworkconfigurator.h"
#include "cainterfacecontroller.h"
-#include "caipadapter.h"
#include "caedradapter.h"
#include "caleadapter.h"
-#include "canetworkconfigurator.h"
+#include "canfcadapter.h"
#include "caremotehandler.h"
-#include "oic_malloc.h"
-#include "logger.h"
#include "cathreadpool.h"
+#include "caipadapter.h"
+#include "cainterface.h"
+
+#ifdef RA_ADAPTER
+#include "caraadapter.h"
+#endif
-#define TAG PCF("CA")
+#ifdef TCP_ADAPTER
+#include "catcpadapter.h"
+#endif
+
+#define TAG "OIC_CA_INF_CTR"
#define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
{OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
-#define CA_TRANSPORT_TYPE_NUM 3
+static CAConnectivityHandler_t *g_adapterHandler = NULL;
-static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM] = {};
+static uint32_t g_numberOfAdapters = 0;
static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
-static CANetworkChangeCallback g_networkChangeCallback = NULL;
+static CAAdapterChangeCallback g_adapterChangeCallback = NULL;
+
+static CAConnectionChangeCallback g_connChangeCallback = NULL;
static CAErrorHandleCallback g_errorHandleCallback = NULL;
static int CAGetAdapterIndex(CATransportAdapter_t cType)
{
- switch (cType)
+ for (uint32_t index=0 ; index < g_numberOfAdapters ; index++)
{
- case CA_ADAPTER_IP:
- return 0;
- case CA_ADAPTER_GATT_BTLE:
- return 1;
- case CA_ADAPTER_RFCOMM_BTEDR:
- return 2;
- default:
- break;
+ if (cType == g_adapterHandler[index].cType )
+ {
+ return index;
+ }
}
-
- OIC_LOG(DEBUG, TAG, "CA_TRANSPORT_TYPE_NUM is not 3");
-
return -1;
}
-static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
+static void CARegisterCallback(CAConnectivityHandler_t handler)
{
- OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
-
if(handler.startAdapter == NULL ||
handler.startListenServer == NULL ||
+ handler.stopListenServer == NULL ||
handler.startDiscoveryServer == NULL ||
handler.sendData == NULL ||
handler.sendDataToAll == NULL ||
OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
return;
}
-
- int index = CAGetAdapterIndex(cType);
-
- if (index == -1)
+ uint32_t numberofAdapters = g_numberOfAdapters + 1;
+ CAConnectivityHandler_t *adapterHandler = OICRealloc(g_adapterHandler,
+ (numberofAdapters) * sizeof(*adapterHandler));
+ if (NULL == adapterHandler)
{
- OIC_LOG(ERROR, TAG, "unknown connectivity type!");
+ OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
return;
}
+ g_adapterHandler = adapterHandler;
+ g_numberOfAdapters = numberofAdapters;
+ g_adapterHandler[g_numberOfAdapters-1] = handler;
- g_adapterHandler[index] = handler;
-
- OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
+ OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", handler.cType);
}
-static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
+#ifdef RA_ADAPTER
+CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
{
- OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
+ return CASetRAInfo(caraInfo);
+}
+#endif
- // Call the callback.
+static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
+ const void *data, uint32_t dataLen)
+{
if (g_networkPacketReceivedCallback != NULL)
{
- g_networkPacketReceivedCallback(endpoint, data, dataLen);
+ g_networkPacketReceivedCallback(sep, data, dataLen);
}
else
{
- OICFree(data);
-
OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
}
}
-static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
+static void CAAdapterChangedCallback(CATransportAdapter_t adapter, CANetworkStatus_t status)
{
- OIC_LOG(DEBUG, TAG, "Network Changed callback");
+ // Call the callback.
+ if (g_adapterChangeCallback != NULL)
+ {
+ g_adapterChangeCallback(adapter, status);
+ }
+ OIC_LOG_V(DEBUG, TAG, "[%d]adapter status is changed to [%d]", adapter, status);
+}
+static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
+{
// Call the callback.
- if (g_networkChangeCallback != NULL)
+ if (g_connChangeCallback != NULL)
{
- g_networkChangeCallback(info, status);
+ g_connChangeCallback(info, isConnected);
}
+ OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
}
static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
{
OIC_LOG(DEBUG, TAG, "initialize adapters..");
- memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
-
// Initialize adapters and register callback.
#ifdef IP_ADAPTER
- CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- handle);
+ CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
+ CAAdapterErrorHandleCallback, handle);
#endif /* IP_ADAPTER */
#ifdef EDR_ADAPTER
- CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- CAAdapterErrorHandleCallback, handle);
+ CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
+ CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
#endif /* EDR_ADAPTER */
#ifdef LE_ADAPTER
- CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- CAAdapterErrorHandleCallback, handle);
+ CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
+ CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
#endif /* LE_ADAPTER */
+#ifdef RA_ADAPTER
+ CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
+ handle);
+#endif /* RA_ADAPTER */
+
+#ifdef TCP_ADAPTER
+ CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
+ CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
+#endif /* TCP_ADAPTER */
+
+#ifdef NFC_ADAPTER
+ CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
+ CAAdapterErrorHandleCallback, handle);
+#endif /* NFC_ADAPTER */
}
void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
{
- OIC_LOG(DEBUG, TAG, "Set packet received callback");
+ OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
g_networkPacketReceivedCallback = callback;
}
-void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
+void CASetNetworkMonitorCallbacks(CAAdapterChangeCallback adapterCB,
+ CAConnectionChangeCallback connCB)
{
- OIC_LOG(DEBUG, TAG, "Set network change callback");
+ OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
- g_networkChangeCallback = callback;
+ g_adapterChangeCallback = adapterCB;
+ g_connChangeCallback = connCB;
}
void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
int index = CAGetAdapterIndex(transportType);
-
- if (index == -1)
+ if (0 > index)
{
OIC_LOG(ERROR, TAG, "unknown connectivity type!");
return CA_STATUS_FAILED;
}
+ CAResult_t res = CA_STATUS_FAILED;
if (g_adapterHandler[index].startAdapter != NULL)
{
- g_adapterHandler[index].startAdapter();
+ res = g_adapterHandler[index].startAdapter();
}
- return CA_STATUS_OK;
+ return res;
}
void CAStopAdapter(CATransportAdapter_t transportType)
OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
int index = CAGetAdapterIndex(transportType);
-
- if (index == -1)
+ if (0 > index)
{
OIC_LOG(ERROR, TAG, "unknown transport type!");
return;
return CA_STATUS_INVALID_PARAM;
}
- CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
- uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
+ CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
+ if (!tempInfo)
+ {
+ OIC_LOG(ERROR, TAG, "Out of memory!");
+ return CA_MEMORY_ALLOC_FAILED;
+ }
+ uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
+ if (!tempSize)
+ {
+ OIC_LOG(ERROR, TAG, "Out of memory!");
+ OICFree(tempInfo);
+ return CA_MEMORY_ALLOC_FAILED;
+ }
CAResult_t res = CA_STATUS_FAILED;
- uint32_t resSize = 0;
- for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+ size_t resSize = 0;
+ for (uint32_t index = 0; index < g_numberOfAdapters; index++)
{
if (g_adapterHandler[index].GetnetInfo != NULL)
{
OIC_LOG_V(DEBUG,
TAG,
- "%d adapter network info size is %" PRIu32 " res:%d",
+ "%zu adapter network info size is %" PRIu32 " res:%d",
index,
tempSize[index],
res);
}
}
- OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
+ OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
if (resSize == 0)
{
+ OICFree(tempInfo);
+ OICFree(tempSize);
if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
{
return res;
*info = resInfo;
*size = resSize;
- for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+ for (uint32_t index = 0; index < g_numberOfAdapters; index++)
{
// check information
if (tempSize[index] == 0)
OICFree(tempInfo[index]);
tempInfo[index] = NULL;
}
+ OICFree(tempInfo);
+ OICFree(tempSize);
OIC_LOG(DEBUG, TAG, "each network info save success!");
return CA_STATUS_OK;
// memory error label.
memory_error_exit:
- for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+ for (uint32_t index = 0; index < g_numberOfAdapters; index++)
{
-
OICFree(tempInfo[index]);
tempInfo[index] = NULL;
}
+ OICFree(tempInfo);
+ OICFree(tempSize);
return CA_MEMORY_ALLOC_FAILED;
}
CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
{
- OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
-
- CAResult_t res = CA_STATUS_FAILED;
-
if (endpoint == NULL)
{
OIC_LOG(DEBUG, TAG, "Invalid endpoint");
return CA_STATUS_INVALID_PARAM;
}
- CATransportAdapter_t type = endpoint->adapter;
-
- int index = CAGetAdapterIndex(type);
- if (index == -1)
+ u_arraylist_t *list = CAGetSelectedNetworkList();
+ if (!list)
{
- OIC_LOG(ERROR, TAG, "unknown transport type!");
- return CA_STATUS_INVALID_PARAM;
+ OIC_LOG(ERROR, TAG, "No selected network");
+ return CA_SEND_FAILED;
}
+ CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
- uint32_t sentDataLen = 0;
-
- if (g_adapterHandler[index].sendData != NULL)
+ for (uint32_t i = 0; i < u_arraylist_length(list); i++)
{
- sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
- }
+ void* ptrType = u_arraylist_get(list, i);
+
+ if (NULL == ptrType)
+ {
+ continue;
+ }
+
+ CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
+ if (0 == (connType & requestedAdapter))
+ {
+ continue;
+ }
+
+ int index = CAGetAdapterIndex(connType);
+
+ if (-1 == index)
+ {
+ OIC_LOG(ERROR, TAG, "unknown transport type!");
+ return CA_STATUS_INVALID_PARAM;
+ }
+
+ int32_t sentDataLen = 0;
+
+ if (NULL != g_adapterHandler[index].sendData)
+ {
+ OIC_LOG(DEBUG, TAG, "unicast message to adapter");
+ sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
+ }
+
+ if (sentDataLen != (int32_t)length)
+ {
+ OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
+#ifdef SINGLE_THREAD
+ //in case of single thread, no error handler. Report error immediately
+ return CA_SEND_FAILED;
+#endif
+ }
- if (sentDataLen != -1)
- {
- res = CA_STATUS_OK;
}
- return res;
+
+ return CA_STATUS_OK;
}
CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
{
- OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
-
- CAResult_t res = CA_SEND_FAILED;
u_arraylist_t *list = CAGetSelectedNetworkList();
-
if (!list)
{
OIC_LOG(DEBUG, TAG, "No selected network");
return CA_SEND_FAILED;
}
- int i = 0;
- for (i = 0; i < u_arraylist_length(list); i++)
+ CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
+ size_t selectedLength = u_arraylist_length(list);
+ for (size_t i = 0; i < selectedLength; i++)
{
void* ptrType = u_arraylist_get(list, i);
- if(ptrType == NULL)
+ if(NULL == ptrType)
{
continue;
}
CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
+ if (0 == (connType & requestedAdapter))
+ {
+ continue;
+ }
int index = CAGetAdapterIndex(connType);
-
- if (index == -1)
+ if (0 > index)
+ if (-1 == index)
{
OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
continue;
uint32_t sentDataLen = 0;
- if (g_adapterHandler[index].sendDataToAll != NULL)
+ if (NULL != g_adapterHandler[index].sendDataToAll)
{
void *payload = (void *) OICMalloc(length);
if (!payload)
OICFree(payload);
}
- if (sentDataLen == length)
+ if (sentDataLen != length)
{
- res = CA_STATUS_OK;
- }
- else
- {
- OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
+ OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
+#ifdef SINGLE_THREAD
+ //in case of single thread, no error handler. Report error immediately
+ return CA_SEND_FAILED;
+#endif
}
}
- return res;
+ return CA_STATUS_OK;
}
CAResult_t CAStartListeningServerAdapters()
{
- OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
+ CAResult_t result = CA_STATUS_FAILED;
u_arraylist_t *list = CAGetSelectedNetworkList();
if (!list)
{
OIC_LOG(ERROR, TAG, "No selected network");
- return CA_STATUS_FAILED;
+ return result;
}
- int i = 0;
- for (i = 0; i < u_arraylist_length(list); i++)
+ size_t length = u_arraylist_length(list);
+ for (size_t i = 0; i < length; i++)
{
void* ptrType = u_arraylist_get(list, i);
CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
int index = CAGetAdapterIndex(connType);
- if (index == -1)
+ if (0 > index)
{
OIC_LOG(ERROR, TAG, "unknown connectivity type!");
continue;
if (g_adapterHandler[index].startListenServer != NULL)
{
- g_adapterHandler[index].startListenServer();
+ const CAResult_t tmp =
+ g_adapterHandler[index].startListenServer();
+
+ // Successful listen if at least one adapter started.
+ if (CA_STATUS_OK == tmp)
+ {
+ result = tmp;
+ }
+ }
+ }
+
+ return result;
+}
+
+CAResult_t CAStopListeningServerAdapters()
+{
+ u_arraylist_t *list = CAGetSelectedNetworkList();
+ if (!list)
+ {
+ OIC_LOG(ERROR, TAG, "No selected network");
+ return CA_STATUS_FAILED;
+ }
+
+ size_t length = u_arraylist_length(list);
+ for (size_t i = 0; i < length; i++)
+ {
+ void* ptrType = u_arraylist_get(list, i);
+ if(ptrType == NULL)
+ {
+ continue;
+ }
+
+ CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
+
+ int index = CAGetAdapterIndex(connType);
+ if (0 > index)
+ {
+ OIC_LOG(ERROR, TAG, "unknown connectivity type!");
+ continue;
+ }
+
+ if (g_adapterHandler[index].stopListenServer != NULL)
+ {
+ g_adapterHandler[index].stopListenServer();
}
}
CAResult_t CAStartDiscoveryServerAdapters()
{
- OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
+ CAResult_t result = CA_STATUS_FAILED;
u_arraylist_t *list = CAGetSelectedNetworkList();
if (!list)
{
OIC_LOG(ERROR, TAG, "No selected network");
- return CA_STATUS_FAILED;
+ return result;
}
- int i = 0;
- for (i = 0; i < u_arraylist_length(list); i++)
+ size_t length = u_arraylist_length(list);
+ for (size_t i = 0; i < length; i++)
{
void* ptrType = u_arraylist_get(list, i);
CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
int index = CAGetAdapterIndex(connType);
-
- if (index == -1)
+ if (0 > index)
{
OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
continue;
if (g_adapterHandler[index].startDiscoveryServer != NULL)
{
- g_adapterHandler[index].startDiscoveryServer();
+ const CAResult_t tmp =
+ g_adapterHandler[index].startDiscoveryServer();
+
+ // Successful discovery if at least one adapter started.
+ if (CA_STATUS_OK == tmp)
+ {
+ result = tmp;
+ }
}
}
- return CA_STATUS_OK;
+ return result;
}
void CATerminateAdapters()
{
- OIC_LOG(DEBUG, TAG, "terminate all adapters..");
-
- uint32_t index;
- for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
+ for (uint32_t index = 0; index < g_numberOfAdapters; index++)
{
if (g_adapterHandler[index].terminate != NULL)
{
g_adapterHandler[index].terminate();
}
}
+
+ OICFree(g_adapterHandler);
+ g_adapterHandler = NULL;
+ g_numberOfAdapters = 0;
}
+
+#ifdef SINGLE_THREAD
+CAResult_t CAReadData()
+{
+ u_arraylist_t *list = CAGetSelectedNetworkList();
+
+ if (!list)
+ {
+ return CA_STATUS_FAILED;
+ }
+
+ uint8_t i = 0;
+ for (i = 0; i < u_arraylist_length(list); i++)
+ {
+ void *ptrType = u_arraylist_get(list, i);
+ if (NULL == ptrType)
+ {
+ OIC_LOG(ERROR, TAG, "get list fail");
+ return CA_STATUS_FAILED;
+ }
+
+ CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
+
+ int index = CAGetAdapterIndex(connType);
+ if (0 > index)
+ {
+ OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
+ continue;
+ }
+
+ if (g_adapterHandler[index].readData != NULL)
+ {
+ g_adapterHandler[index].readData();
+ }
+ }
+
+ return CA_STATUS_OK;
+}
+#endif
+