#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdint.h>
+#include <inttypes.h>
+#include "logger.h"
+#include "oic_malloc.h"
+#include "caadapterutils.h"
+#include "canetworkconfigurator.h"
#include "cainterfacecontroller.h"
-#include "caethernetadapter.h"
#include "caedradapter.h"
#include "caleadapter.h"
-#include "cawifiadapter.h"
-#include "canetworkconfigurator.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 "CA_INTRFC_CNTRLR"
#define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
{OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
-#define CA_CONNECTIVITY_TYPE_NUM 4
+#ifdef TCP_ADAPTER
+#define CA_TRANSPORT_TYPE_NUM 5
+#elif RA_ADAPTER
+#define CA_TRANSPORT_TYPE_NUM 4
+#else
+#define CA_TRANSPORT_TYPE_NUM 3
+#endif
-static CAConnectivityHandler_t g_adapterHandler[CA_CONNECTIVITY_TYPE_NUM];
+static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM] = {};
static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
static CANetworkChangeCallback g_networkChangeCallback = NULL;
-static int CAGetAdapterIndex(CAConnectivityType_t cType)
+static CAErrorHandleCallback g_errorHandleCallback = NULL;
+
+static int CAGetAdapterIndex(CATransportAdapter_t cType)
{
switch (cType)
{
- case CA_ETHERNET:
+ case CA_ADAPTER_IP:
return 0;
- case CA_WIFI:
+ case CA_ADAPTER_GATT_BTLE:
return 1;
- case CA_EDR:
+ case CA_ADAPTER_RFCOMM_BTEDR:
return 2;
- case CA_LE:
+
+#ifdef RA_ADAPTER
+ case CA_ADAPTER_REMOTE_ACCESS:
return 3;
- }
+#endif
- OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4");
+#ifdef TCP_ADAPTER
+ case CA_ADAPTER_TCP:
+ return 4;
+#endif
+ default:
+ break;
+ }
return -1;
}
-static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
+static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
{
- OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
+ OIC_LOG(DEBUG, TAG, "IN");
if(handler.startAdapter == NULL ||
handler.startListenServer == NULL ||
return;
}
- memcpy(&g_adapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
+ g_adapterHandler[index] = handler;
OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
+ OIC_LOG(DEBUG, TAG, "OUT");
}
-static void CAReceivedPacketCallback(CARemoteEndpoint_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
+
+static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
+ const void *data, uint32_t dataLen)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
- // Call the callback.
if (g_networkPacketReceivedCallback != NULL)
{
- g_networkPacketReceivedCallback(endpoint, data, dataLen);
+ g_networkPacketReceivedCallback(sep, data, dataLen);
}
else
{
- OICFree(endpoint);
- endpoint = NULL;
- OICFree(data);
- data = NULL;
-
OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
}
+
+ OIC_LOG(DEBUG, TAG, "OUT");
}
-static void CANetworkChangedCallback(CALocalConnectivity_t *info,
- CANetworkStatus_t status)
+static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
{
- OIC_LOG(DEBUG, TAG, "Network Changed callback");
+ OIC_LOG(DEBUG, TAG, "IN");
// Call the callback.
if (g_networkChangeCallback != NULL)
{
g_networkChangeCallback(info, status);
}
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
+ const void *data, uint32_t dataLen,
+ CAResult_t result)
+{
+ OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
+
+ // Call the callback.
+ if (g_errorHandleCallback != NULL)
+ {
+ g_errorHandleCallback(endpoint, data, dataLen, result);
+ }
}
void CAInitializeAdapters(ca_thread_pool_t handle)
{
OIC_LOG(DEBUG, TAG, "initialize adapters..");
- memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
+ memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
// Initialize adapters and register callback.
-#ifdef ETHERNET_ADAPTER
- CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- handle);
-#endif /* ETHERNET_ADAPTER */
-
-#ifdef WIFI_ADAPTER
- CAInitializeWIFI(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- handle);
-#endif /* WIFI_ADAPTER */
+#ifdef IP_ADAPTER
+ CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
+ CAAdapterErrorHandleCallback, handle);
+#endif /* IP_ADAPTER */
#ifdef EDR_ADAPTER
CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- handle);
+ CAAdapterErrorHandleCallback, handle);
#endif /* EDR_ADAPTER */
#ifdef LE_ADAPTER
CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
- handle);
+ CAAdapterErrorHandleCallback, handle);
#endif /* LE_ADAPTER */
+#ifdef RA_ADAPTER
+ CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
+ handle);
+#endif /* RA_ADAPTER */
+
+#ifdef TCP_ADAPTER
+ CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
+ CAAdapterErrorHandleCallback, handle);
+#endif /* TCP_ADAPTER */
}
void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
{
- OIC_LOG(DEBUG, TAG, "Set packet received callback");
+ OIC_LOG(DEBUG, TAG, "IN");
g_networkPacketReceivedCallback = callback;
+
+ OIC_LOG(DEBUG, TAG, "OUT");
}
void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
{
- OIC_LOG(DEBUG, TAG, "Set network change callback");
+ OIC_LOG(DEBUG, TAG, "IN");
g_networkChangeCallback = callback;
+
+ OIC_LOG(DEBUG, TAG, "OUT");
}
-CAResult_t CAStartAdapter(CAConnectivityType_t cType)
+void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
{
- OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
+ OIC_LOG(DEBUG, TAG, "Set error handle callback");
+ g_errorHandleCallback = errorCallback;
+}
- int index = CAGetAdapterIndex(cType);
+CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
+{
+ OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
+
+ int index = CAGetAdapterIndex(transportType);
if (index == -1)
{
return CA_STATUS_OK;
}
-void CAStopAdapter(CAConnectivityType_t cType)
+void CAStopAdapter(CATransportAdapter_t transportType)
{
- OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
+ OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
- int index = CAGetAdapterIndex(cType);
+ int index = CAGetAdapterIndex(transportType);
if (index == -1)
{
- OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
+ OIC_LOG(ERROR, TAG, "unknown transport type!");
return;
}
}
}
-CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
+CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
{
- CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
- uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
+ if (info == NULL || size == NULL)
+ {
+ return CA_STATUS_INVALID_PARAM;
+ }
- memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
- memset(tempSize, 0, sizeof(uint32_t) * CA_CONNECTIVITY_TYPE_NUM);
+ CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
+ uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
- // #1. get information each adapter
- uint8_t index = 0;
CAResult_t res = CA_STATUS_FAILED;
- for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
+ uint32_t resSize = 0;
+ for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
{
if (g_adapterHandler[index].GetnetInfo != NULL)
{
- res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
+ // #1. get information for each adapter
+ res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
+ &tempSize[index]);
- OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
- tempSize[index], res);
- }
- }
+ // #2. total size
+ if (res == CA_STATUS_OK)
+ {
+ resSize += tempSize[index];
+ }
- uint32_t resSize = 0;
- for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
- {
- // check information
- if (tempInfo[index] == NULL || tempSize[index] <= 0)
- {
- continue;
+ OIC_LOG_V(DEBUG,
+ TAG,
+ "%d adapter network info size is %" PRIu32 " res:%d",
+ index,
+ tempSize[index],
+ res);
}
-
- // #2. total size
- resSize += tempSize[index];
}
OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
- if (resSize <= 0)
+ if (resSize == 0)
{
if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
{
// #3. add data into result
// memory allocation
-
- CALocalConnectivity_t *resInfo = (CALocalConnectivity_t *) OICCalloc(
- resSize, sizeof(CALocalConnectivity_t));
+ CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
CA_MEMORY_ALLOC_CHECK(resInfo);
- uint8_t pos = 0;
- for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
+ // #4. save data
+ *info = resInfo;
+ *size = resSize;
+
+ for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
{
// check information
- if (tempInfo[index] == NULL || tempSize[index] <= 0)
+ if (tempSize[index] == 0)
{
continue;
}
- memcpy(resInfo + pos, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
+ memcpy(resInfo,
+ tempInfo[index],
+ sizeof(*resInfo) * tempSize[index]);
- pos += tempSize[index];
+ resInfo += tempSize[index];
// free adapter data
OICFree(tempInfo[index]);
tempInfo[index] = NULL;
}
- // #5. save data
- if ( info != NULL )
- {
- *info = resInfo;
- }
-
- if (size)
- {
- *size = resSize;
- }
-
OIC_LOG(DEBUG, TAG, "each network info save success!");
return CA_STATUS_OK;
// memory error label.
memory_error_exit:
- for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
+ for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
{
OICFree(tempInfo[index]);
return CA_MEMORY_ALLOC_FAILED;
}
-CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
+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;
+ OIC_LOG(DEBUG, TAG, "IN");
if (endpoint == NULL)
{
return CA_STATUS_INVALID_PARAM;
}
- CAConnectivityType_t type = endpoint->connectivityType;
+ CATransportAdapter_t type = endpoint->adapter;
int index = CAGetAdapterIndex(type);
if (index == -1)
{
- OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
+ OIC_LOG(ERROR, TAG, "unknown transport type!");
return CA_STATUS_INVALID_PARAM;
}
- uint32_t sentDataLen = 0;
+ int32_t sentDataLen = 0;
if (g_adapterHandler[index].sendData != NULL)
{
sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
}
- if (sentDataLen != -1)
+ if (sentDataLen != (int)length)
{
- res = CA_STATUS_OK;
+ 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
}
- return res;
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+ return CA_STATUS_OK;
}
-CAResult_t CASendMulticastData(const void *data, uint32_t length)
+CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
{
- OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
+ OIC_LOG(DEBUG, TAG, "IN");
- CAResult_t res = CA_STATUS_FAILED;
u_arraylist_t *list = CAGetSelectedNetworkList();
-
if (!list)
{
OIC_LOG(DEBUG, TAG, "No selected network");
- return CA_STATUS_FAILED;
+ return CA_SEND_FAILED;
}
- int i = 0;
- for (i = 0; i < u_arraylist_length(list); i++)
+ CATransportFlags_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
+
+ for (uint32_t i = 0; i < u_arraylist_length(list); i++)
{
void* ptrType = u_arraylist_get(list, i);
continue;
}
- CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
+ CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
+ if ((connType & requestedAdapter) == 0)
+ {
+ continue;
+ }
int index = CAGetAdapterIndex(connType);
return CA_MEMORY_ALLOC_FAILED;
}
memcpy(payload, data, length);
- sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
+ sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
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;
+ OIC_LOG(DEBUG, TAG, "OUT");
+
+ return CA_STATUS_OK;
}
CAResult_t CAStartListeningServerAdapters()
{
- OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
+ OIC_LOG(DEBUG, TAG, "IN");
u_arraylist_t *list = CAGetSelectedNetworkList();
if (!list)
return CA_STATUS_FAILED;
}
- int i = 0;
- for (i = 0; i < u_arraylist_length(list); i++)
+ for (uint32_t i = 0; i < u_arraylist_length(list); i++)
{
void* ptrType = u_arraylist_get(list, i);
continue;
}
- CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
+ CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
int index = CAGetAdapterIndex(connType);
if (index == -1)
}
}
+ OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
CAResult_t CAStartDiscoveryServerAdapters()
{
- OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
+ OIC_LOG(DEBUG, TAG, "IN");
u_arraylist_t *list = CAGetSelectedNetworkList();
return CA_STATUS_FAILED;
}
- int i = 0;
- for (i = 0; i < u_arraylist_length(list); i++)
+ for (uint32_t i = 0; i < u_arraylist_length(list); i++)
{
void* ptrType = u_arraylist_get(list, i);
continue;
}
- CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
+ CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
int index = CAGetAdapterIndex(connType);
}
}
+ OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
void CATerminateAdapters()
{
- OIC_LOG(DEBUG, TAG, "terminate all adapters..");
+ OIC_LOG(DEBUG, TAG, "IN");
- int index;
- for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
+ uint32_t index;
+ for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
{
- if (g_adapterHandler[index].stopAdapter != NULL)
- {
- g_adapterHandler[index].stopAdapter();
- }
if (g_adapterHandler[index].terminate != NULL)
{
g_adapterHandler[index].terminate();
}
}
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+#ifdef SINGLE_THREAD
+CAResult_t CAReadData()
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+ 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 (-1 == index)
+ {
+ OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
+ continue;
+ }
+
+ if (g_adapterHandler[index].readData != NULL)
+ {
+ g_adapterHandler[index].readData();
+ }
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT");
+ return CA_STATUS_OK;
}
+#endif