1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
27 #include "oic_malloc.h"
28 #include "caadapterutils.h"
29 #include "canetworkconfigurator.h"
30 #include "cainterfacecontroller.h"
31 #include "caedradapter.h"
32 #include "caleadapter.h"
33 #include "canfcadapter.h"
34 #include "caremotehandler.h"
35 #include "cathreadpool.h"
36 #include "caipadapter.h"
37 #include "cainterface.h"
40 #include "caraadapter.h"
44 #include "catcpadapter.h"
47 #define TAG "OIC_CA_INF_CTR"
49 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
50 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
52 static CAConnectivityHandler_t *g_adapterHandler = NULL;
54 static uint32_t g_numberOfAdapters = 0;
56 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
58 static CAAdapterChangeCallback g_adapterChangeCallback = NULL;
60 static CAConnectionChangeCallback g_connChangeCallback = NULL;
62 static CAErrorHandleCallback g_errorHandleCallback = NULL;
64 static int CAGetAdapterIndex(CATransportAdapter_t cType)
66 for (uint32_t index=0 ; index < g_numberOfAdapters ; index++)
68 if (cType == g_adapterHandler[index].cType )
76 static void CARegisterCallback(CAConnectivityHandler_t handler)
78 if(handler.startAdapter == NULL ||
79 handler.startListenServer == NULL ||
80 handler.stopListenServer == NULL ||
81 handler.startDiscoveryServer == NULL ||
82 handler.sendData == NULL ||
83 handler.sendDataToAll == NULL ||
84 handler.GetnetInfo == NULL ||
85 handler.readData == NULL ||
86 handler.stopAdapter == NULL ||
87 handler.terminate == NULL)
89 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
92 uint32_t numberofAdapters = g_numberOfAdapters + 1;
93 CAConnectivityHandler_t *adapterHandler = OICRealloc(g_adapterHandler,
94 (numberofAdapters) * sizeof(*adapterHandler));
95 if (NULL == adapterHandler)
97 OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
100 g_adapterHandler = adapterHandler;
101 g_numberOfAdapters = numberofAdapters;
102 g_adapterHandler[g_numberOfAdapters-1] = handler;
104 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", handler.cType);
108 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
110 return CASetRAInfo(caraInfo);
114 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
115 const void *data, uint32_t dataLen)
117 if (g_networkPacketReceivedCallback != NULL)
119 g_networkPacketReceivedCallback(sep, data, dataLen);
123 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
127 static void CAAdapterChangedCallback(CATransportAdapter_t adapter, CANetworkStatus_t status)
129 // Call the callback.
130 if (g_adapterChangeCallback != NULL)
132 g_adapterChangeCallback(adapter, status);
134 OIC_LOG_V(DEBUG, TAG, "[%d]adapter status is changed to [%d]", adapter, status);
137 static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
139 // Call the callback.
140 if (g_connChangeCallback != NULL)
142 g_connChangeCallback(info, isConnected);
144 OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
147 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
148 const void *data, uint32_t dataLen,
151 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
153 // Call the callback.
154 if (g_errorHandleCallback != NULL)
156 g_errorHandleCallback(endpoint, data, dataLen, result);
160 void CAInitializeAdapters(ca_thread_pool_t handle)
162 OIC_LOG(DEBUG, TAG, "initialize adapters..");
164 // Initialize adapters and register callback.
166 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
167 CAAdapterErrorHandleCallback, handle);
168 #endif /* IP_ADAPTER */
171 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
172 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
173 #endif /* EDR_ADAPTER */
176 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
177 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
178 #endif /* LE_ADAPTER */
181 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
183 #endif /* RA_ADAPTER */
186 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
187 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
188 #endif /* TCP_ADAPTER */
191 CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
192 CAAdapterErrorHandleCallback, handle);
193 #endif /* NFC_ADAPTER */
196 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
198 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
200 g_networkPacketReceivedCallback = callback;
203 void CASetNetworkMonitorCallbacks(CAAdapterChangeCallback adapterCB,
204 CAConnectionChangeCallback connCB)
206 OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
208 g_adapterChangeCallback = adapterCB;
209 g_connChangeCallback = connCB;
212 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
214 OIC_LOG(DEBUG, TAG, "Set error handle callback");
215 g_errorHandleCallback = errorCallback;
218 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
220 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
222 int index = CAGetAdapterIndex(transportType);
225 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
226 return CA_STATUS_FAILED;
229 CAResult_t res = CA_STATUS_FAILED;
230 if (g_adapterHandler[index].startAdapter != NULL)
232 res = g_adapterHandler[index].startAdapter();
238 void CAStopAdapter(CATransportAdapter_t transportType)
240 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
242 int index = CAGetAdapterIndex(transportType);
245 OIC_LOG(ERROR, TAG, "unknown transport type!");
249 if (g_adapterHandler[index].stopAdapter != NULL)
251 g_adapterHandler[index].stopAdapter();
255 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
257 if (info == NULL || size == NULL)
259 return CA_STATUS_INVALID_PARAM;
262 CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
265 OIC_LOG(ERROR, TAG, "Out of memory!");
266 return CA_MEMORY_ALLOC_FAILED;
268 uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
271 OIC_LOG(ERROR, TAG, "Out of memory!");
273 return CA_MEMORY_ALLOC_FAILED;
276 CAResult_t res = CA_STATUS_FAILED;
278 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
280 if (g_adapterHandler[index].GetnetInfo != NULL)
282 // #1. get information for each adapter
283 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
287 if (res == CA_STATUS_OK)
289 resSize += tempSize[index];
294 "%zu adapter network info size is %" PRIu32 " res:%d",
301 OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
307 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
311 return CA_STATUS_FAILED;
314 // #3. add data into result
316 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
317 CA_MEMORY_ALLOC_CHECK(resInfo);
323 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
326 if (tempSize[index] == 0)
333 sizeof(*resInfo) * tempSize[index]);
335 resInfo += tempSize[index];
338 OICFree(tempInfo[index]);
339 tempInfo[index] = NULL;
344 OIC_LOG(DEBUG, TAG, "each network info save success!");
347 // memory error label.
350 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
352 OICFree(tempInfo[index]);
353 tempInfo[index] = NULL;
358 return CA_MEMORY_ALLOC_FAILED;
361 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
363 if (endpoint == NULL)
365 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
366 return CA_STATUS_INVALID_PARAM;
370 u_arraylist_t *list = CAGetSelectedNetworkList();
373 OIC_LOG(ERROR, TAG, "No selected network");
374 return CA_SEND_FAILED;
376 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
378 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
380 void* ptrType = u_arraylist_get(list, i);
387 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
388 if (0 == (connType & requestedAdapter))
393 int index = CAGetAdapterIndex(connType);
397 OIC_LOG(ERROR, TAG, "unknown transport type!");
398 return CA_STATUS_INVALID_PARAM;
401 int32_t sentDataLen = 0;
403 if (NULL != g_adapterHandler[index].sendData)
405 OIC_LOG(DEBUG, TAG, "unicast message to adapter");
406 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
409 if (sentDataLen != (int32_t)length)
411 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
413 //in case of single thread, no error handler. Report error immediately
414 return CA_SEND_FAILED;
423 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
425 u_arraylist_t *list = CAGetSelectedNetworkList();
428 OIC_LOG(DEBUG, TAG, "No selected network");
429 return CA_SEND_FAILED;
432 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
433 size_t selectedLength = u_arraylist_length(list);
434 for (size_t i = 0; i < selectedLength; i++)
436 void* ptrType = u_arraylist_get(list, i);
443 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
444 if (0 == (connType & requestedAdapter))
449 int index = CAGetAdapterIndex(connType);
453 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
457 uint32_t sentDataLen = 0;
459 if (NULL != g_adapterHandler[index].sendDataToAll)
461 void *payload = (void *) OICMalloc(length);
464 OIC_LOG(ERROR, TAG, "Out of memory!");
465 return CA_MEMORY_ALLOC_FAILED;
467 memcpy(payload, data, length);
468 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
472 if (sentDataLen != length)
474 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
476 //in case of single thread, no error handler. Report error immediately
477 return CA_SEND_FAILED;
485 CAResult_t CAStartListeningServerAdapters()
487 CAResult_t result = CA_STATUS_FAILED;
489 u_arraylist_t *list = CAGetSelectedNetworkList();
492 OIC_LOG(ERROR, TAG, "No selected network");
496 size_t length = u_arraylist_length(list);
497 for (size_t i = 0; i < length; i++)
499 void* ptrType = u_arraylist_get(list, i);
506 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
508 int index = CAGetAdapterIndex(connType);
511 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
515 if (g_adapterHandler[index].startListenServer != NULL)
517 const CAResult_t tmp =
518 g_adapterHandler[index].startListenServer();
520 // Successful listen if at least one adapter started.
521 if (CA_STATUS_OK == tmp)
531 CAResult_t CAStopListeningServerAdapters()
533 u_arraylist_t *list = CAGetSelectedNetworkList();
536 OIC_LOG(ERROR, TAG, "No selected network");
537 return CA_STATUS_FAILED;
540 size_t length = u_arraylist_length(list);
541 for (size_t i = 0; i < length; i++)
543 void* ptrType = u_arraylist_get(list, i);
549 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
551 int index = CAGetAdapterIndex(connType);
554 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
558 if (g_adapterHandler[index].stopListenServer != NULL)
560 g_adapterHandler[index].stopListenServer();
567 CAResult_t CAStartDiscoveryServerAdapters()
569 CAResult_t result = CA_STATUS_FAILED;
571 u_arraylist_t *list = CAGetSelectedNetworkList();
575 OIC_LOG(ERROR, TAG, "No selected network");
579 size_t length = u_arraylist_length(list);
580 for (size_t i = 0; i < length; i++)
582 void* ptrType = u_arraylist_get(list, i);
589 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
591 int index = CAGetAdapterIndex(connType);
594 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
598 if (g_adapterHandler[index].startDiscoveryServer != NULL)
600 const CAResult_t tmp =
601 g_adapterHandler[index].startDiscoveryServer();
603 // Successful discovery if at least one adapter started.
604 if (CA_STATUS_OK == tmp)
614 void CATerminateAdapters()
616 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
618 if (g_adapterHandler[index].terminate != NULL)
620 g_adapterHandler[index].terminate();
624 OICFree(g_adapterHandler);
625 g_adapterHandler = NULL;
626 g_numberOfAdapters = 0;
630 CAResult_t CAReadData()
632 u_arraylist_t *list = CAGetSelectedNetworkList();
636 return CA_STATUS_FAILED;
640 for (i = 0; i < u_arraylist_length(list); i++)
642 void *ptrType = u_arraylist_get(list, i);
645 OIC_LOG(ERROR, TAG, "get list fail");
646 return CA_STATUS_FAILED;
649 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
651 int index = CAGetAdapterIndex(connType);
654 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
658 if (g_adapterHandler[index].readData != NULL)
660 g_adapterHandler[index].readData();