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"
38 #include <coap/utlist.h>
41 #include "caraadapter.h"
45 #include "catcpadapter.h"
48 #define TAG "OIC_CA_INF_CTR"
50 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
51 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
53 static CAConnectivityHandler_t *g_adapterHandler = NULL;
55 static uint32_t g_numberOfAdapters = 0;
57 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
59 static CAErrorHandleCallback g_errorHandleCallback = NULL;
61 static struct CANetworkCallback_t * g_networkChangeCallbackList = NULL;
64 * network callback structure is handling
65 * for adapter state changed and connection state changed event.
67 typedef struct CANetworkCallback_t {
69 /** Linked list; for multiple callback list.*/
70 struct CANetworkCallback_t * next;
72 /** Adapter state changed event callback. */
73 CAAdapterStateChangedCB adapter;
75 /** Connection state changed event callback. */
76 CAConnectionStateChangedCB conn;
78 } CANetworkCallback_t;
80 static int CAGetAdapterIndex(CATransportAdapter_t cType)
82 for (uint32_t index=0 ; index < g_numberOfAdapters ; index++)
84 if (cType == g_adapterHandler[index].cType )
92 static void CARegisterCallback(CAConnectivityHandler_t handler)
94 if (handler.startAdapter == NULL ||
95 handler.startListenServer == NULL ||
96 handler.stopListenServer == NULL ||
97 handler.startDiscoveryServer == NULL ||
98 handler.sendData == NULL ||
99 handler.sendDataToAll == NULL ||
100 handler.GetnetInfo == NULL ||
101 handler.readData == NULL ||
102 handler.stopAdapter == NULL ||
103 handler.terminate == NULL)
105 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
108 uint32_t numberofAdapters = g_numberOfAdapters + 1;
109 CAConnectivityHandler_t *adapterHandler = OICRealloc(g_adapterHandler,
110 (numberofAdapters) * sizeof(*adapterHandler));
111 if (NULL == adapterHandler)
113 OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
116 g_adapterHandler = adapterHandler;
117 g_numberOfAdapters = numberofAdapters;
118 g_adapterHandler[g_numberOfAdapters-1] = handler;
120 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", handler.cType);
124 * Add a network callback from caller to the network callback list
126 * @param adapterCB adapter state changed callback
127 * @param connCB connection state changed callback
132 static CAResult_t AddNetworkStateChangedCallback(CAAdapterStateChangedCB adapterCB,
133 CAConnectionStateChangedCB connCB)
135 OIC_LOG(DEBUG, TAG, "Add NetworkStateChanged Callback");
137 if (!adapterCB || !connCB)
139 OIC_LOG(ERROR, TAG, "parameter is null");
140 return CA_STATUS_INVALID_PARAM;
143 CANetworkCallback_t* callback = NULL;
144 LL_FOREACH(g_networkChangeCallbackList, callback)
146 if (callback && adapterCB == callback->adapter && connCB == callback->conn)
148 OIC_LOG(DEBUG, TAG, "this callback is already added");
153 callback = (CANetworkCallback_t *) OICCalloc(1, sizeof(CANetworkCallback_t));
154 if (NULL == callback)
156 OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
157 return CA_MEMORY_ALLOC_FAILED;
160 callback->adapter = adapterCB;
161 callback->conn = connCB;
162 LL_APPEND(g_networkChangeCallbackList, callback);
167 * Remove a network callback from the network callback list
169 * @param adapterCB adapter state changed callback
170 * @param connCB connection state changed callback
175 static CAResult_t RemoveNetworkStateChangedCallback(CAAdapterStateChangedCB adapterCB,
176 CAConnectionStateChangedCB connCB)
178 OIC_LOG(DEBUG, TAG, "Remove NetworkStateChanged Callback");
180 CANetworkCallback_t* callback = NULL;
181 LL_FOREACH(g_networkChangeCallbackList, callback)
183 if (callback && adapterCB == callback->adapter && connCB == callback->conn)
185 OIC_LOG(DEBUG, TAG, "remove specific callback");
186 LL_DELETE(g_networkChangeCallbackList, callback);
195 * Remove all network callback from the network callback list
197 static void RemoveAllNetworkStateChangedCallback()
199 OIC_LOG(DEBUG, TAG, "Remove All NetworkStateChanged Callback");
201 CANetworkCallback_t* callback = NULL;
202 LL_FOREACH(g_networkChangeCallbackList, callback)
204 OIC_LOG(DEBUG, TAG, "remove all callbacks");
205 LL_DELETE(g_networkChangeCallbackList, callback);
211 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
213 return CASetRAInfo(caraInfo);
217 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
218 const void *data, uint32_t dataLen)
220 if (g_networkPacketReceivedCallback != NULL)
222 g_networkPacketReceivedCallback(sep, data, dataLen);
226 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
230 static void CAAdapterChangedCallback(CATransportAdapter_t adapter, CANetworkStatus_t status)
232 // Call the callback.
233 CANetworkCallback_t* callback = NULL;
234 LL_FOREACH(g_networkChangeCallbackList, callback)
236 if (callback && callback->adapter)
238 if (CA_INTERFACE_UP == status)
240 callback->adapter(adapter, true);
242 else if (CA_INTERFACE_DOWN == status)
244 callback->adapter(adapter, false);
248 OIC_LOG_V(DEBUG, TAG, "[%d] adapter status is changed to [%d]", adapter, status);
251 #if defined(TCP_ADAPTER) || defined(EDR_ADAPTER) || defined(LE_ADAPTER)
252 static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
254 // Call the callback.
255 CANetworkCallback_t* callback = NULL;
256 LL_FOREACH(g_networkChangeCallbackList, callback)
258 if (callback && callback->conn)
260 callback->conn(info, isConnected);
263 OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
267 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
268 const void *data, uint32_t dataLen,
271 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
273 // Call the callback.
274 if (g_errorHandleCallback != NULL)
276 g_errorHandleCallback(endpoint, data, dataLen, result);
280 void CAInitializeAdapters(ca_thread_pool_t handle)
282 OIC_LOG(DEBUG, TAG, "initialize adapters..");
284 // Initialize adapters and register callback.
286 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
287 CAAdapterErrorHandleCallback, handle);
288 #endif /* IP_ADAPTER */
291 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
292 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
293 #endif /* EDR_ADAPTER */
296 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
297 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
298 #endif /* LE_ADAPTER */
301 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
303 #endif /* RA_ADAPTER */
306 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
307 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
308 #endif /* TCP_ADAPTER */
311 CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
312 CAAdapterErrorHandleCallback, handle);
313 #endif /* NFC_ADAPTER */
316 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
318 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
320 g_networkPacketReceivedCallback = callback;
323 void CASetNetworkMonitorCallbacks(CAAdapterStateChangedCB adapterCB,
324 CAConnectionStateChangedCB connCB)
326 OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
327 CAResult_t res = AddNetworkStateChangedCallback(adapterCB, connCB);
328 if (CA_STATUS_OK != res)
330 OIC_LOG(ERROR, TAG, "AddNetworkStateChangedCallback has failed");
334 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
336 OIC_LOG(DEBUG, TAG, "Set error handle callback");
337 g_errorHandleCallback = errorCallback;
340 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
342 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
344 int index = CAGetAdapterIndex(transportType);
347 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
348 return CA_STATUS_FAILED;
351 CAResult_t res = CA_STATUS_FAILED;
352 if (g_adapterHandler[index].startAdapter != NULL)
354 res = g_adapterHandler[index].startAdapter();
360 void CAStopAdapter(CATransportAdapter_t transportType)
362 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
364 int index = CAGetAdapterIndex(transportType);
367 OIC_LOG(ERROR, TAG, "unknown transport type!");
371 if (g_adapterHandler[index].stopAdapter != NULL)
373 g_adapterHandler[index].stopAdapter();
377 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
379 if (info == NULL || size == NULL)
381 return CA_STATUS_INVALID_PARAM;
384 CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
387 OIC_LOG(ERROR, TAG, "Out of memory!");
388 return CA_MEMORY_ALLOC_FAILED;
390 uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
393 OIC_LOG(ERROR, TAG, "Out of memory!");
395 return CA_MEMORY_ALLOC_FAILED;
398 CAResult_t res = CA_STATUS_FAILED;
400 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
402 if (g_adapterHandler[index].GetnetInfo != NULL)
404 // #1. get information for each adapter
405 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
409 if (res == CA_STATUS_OK)
411 resSize += tempSize[index];
416 "%" PRIu32 " adapter network info size is %" PRIu32 " res:%d",
423 OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
429 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
435 return CA_STATUS_FAILED;
439 // #3. add data into result
441 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
442 CA_MEMORY_ALLOC_CHECK(resInfo);
448 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
451 if (tempSize[index] == 0)
458 sizeof(*resInfo) * tempSize[index]);
460 resInfo += tempSize[index];
463 OICFree(tempInfo[index]);
464 tempInfo[index] = NULL;
469 OIC_LOG(DEBUG, TAG, "each network info save success!");
472 // memory error label.
475 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
477 OICFree(tempInfo[index]);
478 tempInfo[index] = NULL;
483 return CA_MEMORY_ALLOC_FAILED;
486 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
487 CADataType_t dataType)
489 if (endpoint == NULL)
491 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
492 return CA_STATUS_INVALID_PARAM;
496 u_arraylist_t *list = CAGetSelectedNetworkList();
499 OIC_LOG(ERROR, TAG, "No selected network");
500 return CA_SEND_FAILED;
502 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
504 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
506 void* ptrType = u_arraylist_get(list, i);
513 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
514 if (0 == (connType & requestedAdapter))
519 int index = CAGetAdapterIndex(connType);
523 OIC_LOG(ERROR, TAG, "unknown transport type!");
524 return CA_STATUS_INVALID_PARAM;
527 int32_t sentDataLen = 0;
529 if (NULL != g_adapterHandler[index].sendData)
531 OIC_LOG(DEBUG, TAG, "unicast message to adapter");
532 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length, dataType);
535 if (sentDataLen != (int32_t)length)
537 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
539 //in case of single thread, no error handler. Report error immediately
540 return CA_SEND_FAILED;
549 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
550 CADataType_t dataType)
552 u_arraylist_t *list = CAGetSelectedNetworkList();
555 OIC_LOG(DEBUG, TAG, "No selected network");
556 return CA_SEND_FAILED;
559 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
560 size_t selectedLength = u_arraylist_length(list);
561 for (size_t i = 0; i < selectedLength; i++)
563 void* ptrType = u_arraylist_get(list, i);
570 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
571 if (0 == (connType & requestedAdapter))
576 int index = CAGetAdapterIndex(connType);
579 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
583 uint32_t sentDataLen = 0;
585 if (NULL != g_adapterHandler[index].sendDataToAll)
587 void *payload = (void *) OICMalloc(length);
590 OIC_LOG(ERROR, TAG, "Out of memory!");
591 return CA_MEMORY_ALLOC_FAILED;
593 memcpy(payload, data, length);
594 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length, dataType);
598 if (sentDataLen != length)
600 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
602 //in case of single thread, no error handler. Report error immediately
603 return CA_SEND_FAILED;
611 CAResult_t CAStartListeningServerAdapters()
613 CAResult_t result = CA_STATUS_FAILED;
615 u_arraylist_t *list = CAGetSelectedNetworkList();
618 OIC_LOG(ERROR, TAG, "No selected network");
622 size_t length = u_arraylist_length(list);
623 for (size_t i = 0; i < length; i++)
625 void* ptrType = u_arraylist_get(list, i);
632 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
634 int index = CAGetAdapterIndex(connType);
637 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
641 if (g_adapterHandler[index].startListenServer != NULL)
643 const CAResult_t tmp =
644 g_adapterHandler[index].startListenServer();
646 // Successful listen if at least one adapter started.
647 if (CA_STATUS_OK == tmp)
657 CAResult_t CAStopListeningServerAdapters()
659 u_arraylist_t *list = CAGetSelectedNetworkList();
662 OIC_LOG(ERROR, TAG, "No selected network");
663 return CA_STATUS_FAILED;
666 size_t length = u_arraylist_length(list);
667 for (size_t i = 0; i < length; i++)
669 void* ptrType = u_arraylist_get(list, i);
675 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
677 int index = CAGetAdapterIndex(connType);
680 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
684 if (g_adapterHandler[index].stopListenServer != NULL)
686 g_adapterHandler[index].stopListenServer();
693 CAResult_t CAStartDiscoveryServerAdapters()
695 CAResult_t result = CA_STATUS_FAILED;
697 u_arraylist_t *list = CAGetSelectedNetworkList();
701 OIC_LOG(ERROR, TAG, "No selected network");
705 size_t length = u_arraylist_length(list);
706 for (size_t i = 0; i < length; i++)
708 void* ptrType = u_arraylist_get(list, i);
715 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
717 int index = CAGetAdapterIndex(connType);
720 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
724 if (g_adapterHandler[index].startDiscoveryServer != NULL)
726 const CAResult_t tmp =
727 g_adapterHandler[index].startDiscoveryServer();
729 // Successful discovery if at least one adapter started.
730 if (CA_STATUS_OK == tmp)
740 void CATerminateAdapters()
742 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
744 if (g_adapterHandler[index].terminate != NULL)
746 g_adapterHandler[index].terminate();
750 OICFree(g_adapterHandler);
751 g_adapterHandler = NULL;
752 g_numberOfAdapters = 0;
756 CAResult_t CAReadData()
758 u_arraylist_t *list = CAGetSelectedNetworkList();
762 return CA_STATUS_FAILED;
766 for (i = 0; i < u_arraylist_length(list); i++)
768 void *ptrType = u_arraylist_get(list, i);
771 OIC_LOG(ERROR, TAG, "get list fail");
772 return CA_STATUS_FAILED;
775 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
777 int index = CAGetAdapterIndex(connType);
780 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
784 if (g_adapterHandler[index].readData != NULL)
786 g_adapterHandler[index].readData();