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 CAAdapterStateChangedCB g_adapterChangeCallback = NULL;
60 static CAConnectionStateChangedCB 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 if (CA_INTERFACE_UP == status)
134 g_adapterChangeCallback(adapter, true);
136 else if (CA_INTERFACE_DOWN == status)
138 g_adapterChangeCallback(adapter, false);
141 OIC_LOG_V(DEBUG, TAG, "[%d]adapter status is changed to [%d]", adapter, status);
144 #if defined(TCP_ADAPTER) || defined(EDR_ADAPTER) || defined(LE_ADAPTER)
145 static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
147 // Call the callback.
148 if (g_connChangeCallback != NULL)
150 g_connChangeCallback(info, isConnected);
152 OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
156 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
157 const void *data, uint32_t dataLen,
160 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
162 // Call the callback.
163 if (g_errorHandleCallback != NULL)
165 g_errorHandleCallback(endpoint, data, dataLen, result);
169 void CAInitializeAdapters(ca_thread_pool_t handle)
171 OIC_LOG(DEBUG, TAG, "initialize adapters..");
173 // Initialize adapters and register callback.
175 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
176 CAAdapterErrorHandleCallback, handle);
177 #endif /* IP_ADAPTER */
180 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
181 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
182 #endif /* EDR_ADAPTER */
185 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
186 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
187 #endif /* LE_ADAPTER */
190 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
192 #endif /* RA_ADAPTER */
195 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
196 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
197 #endif /* TCP_ADAPTER */
200 CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
201 CAAdapterErrorHandleCallback, handle);
202 #endif /* NFC_ADAPTER */
205 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
207 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
209 g_networkPacketReceivedCallback = callback;
212 void CASetNetworkMonitorCallbacks(CAAdapterStateChangedCB adapterCB,
213 CAConnectionStateChangedCB connCB)
215 OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
217 g_adapterChangeCallback = adapterCB;
218 g_connChangeCallback = connCB;
221 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
223 OIC_LOG(DEBUG, TAG, "Set error handle callback");
224 g_errorHandleCallback = errorCallback;
227 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
229 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
231 int index = CAGetAdapterIndex(transportType);
234 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
235 return CA_STATUS_FAILED;
238 CAResult_t res = CA_STATUS_FAILED;
239 if (g_adapterHandler[index].startAdapter != NULL)
241 res = g_adapterHandler[index].startAdapter();
247 void CAStopAdapter(CATransportAdapter_t transportType)
249 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
251 int index = CAGetAdapterIndex(transportType);
254 OIC_LOG(ERROR, TAG, "unknown transport type!");
258 if (g_adapterHandler[index].stopAdapter != NULL)
260 g_adapterHandler[index].stopAdapter();
264 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
266 if (info == NULL || size == NULL)
268 return CA_STATUS_INVALID_PARAM;
271 CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
274 OIC_LOG(ERROR, TAG, "Out of memory!");
275 return CA_MEMORY_ALLOC_FAILED;
277 uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
280 OIC_LOG(ERROR, TAG, "Out of memory!");
282 return CA_MEMORY_ALLOC_FAILED;
285 CAResult_t res = CA_STATUS_FAILED;
287 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
289 if (g_adapterHandler[index].GetnetInfo != NULL)
291 // #1. get information for each adapter
292 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
296 if (res == CA_STATUS_OK)
298 resSize += tempSize[index];
303 "%" PRIu32 " adapter network info size is %" PRIu32 " res:%d",
310 OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
316 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
322 return CA_STATUS_FAILED;
326 // #3. add data into result
328 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
329 CA_MEMORY_ALLOC_CHECK(resInfo);
335 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
338 if (tempSize[index] == 0)
345 sizeof(*resInfo) * tempSize[index]);
347 resInfo += tempSize[index];
350 OICFree(tempInfo[index]);
351 tempInfo[index] = NULL;
356 OIC_LOG(DEBUG, TAG, "each network info save success!");
359 // memory error label.
362 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
364 OICFree(tempInfo[index]);
365 tempInfo[index] = NULL;
370 return CA_MEMORY_ALLOC_FAILED;
373 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
374 CADataType_t dataType)
376 if (endpoint == NULL)
378 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
379 return CA_STATUS_INVALID_PARAM;
383 u_arraylist_t *list = CAGetSelectedNetworkList();
386 OIC_LOG(ERROR, TAG, "No selected network");
387 return CA_SEND_FAILED;
389 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
391 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
393 void* ptrType = u_arraylist_get(list, i);
400 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
401 if (0 == (connType & requestedAdapter))
406 int index = CAGetAdapterIndex(connType);
410 OIC_LOG(ERROR, TAG, "unknown transport type!");
411 return CA_STATUS_INVALID_PARAM;
414 int32_t sentDataLen = 0;
416 if (NULL != g_adapterHandler[index].sendData)
418 OIC_LOG(DEBUG, TAG, "unicast message to adapter");
419 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length, dataType);
422 if (sentDataLen != (int32_t)length)
424 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
426 //in case of single thread, no error handler. Report error immediately
427 return CA_SEND_FAILED;
436 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
437 CADataType_t dataType)
439 u_arraylist_t *list = CAGetSelectedNetworkList();
442 OIC_LOG(DEBUG, TAG, "No selected network");
443 return CA_SEND_FAILED;
446 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
447 size_t selectedLength = u_arraylist_length(list);
448 for (size_t i = 0; i < selectedLength; i++)
450 void* ptrType = u_arraylist_get(list, i);
457 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
458 if (0 == (connType & requestedAdapter))
463 int index = CAGetAdapterIndex(connType);
466 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
470 uint32_t sentDataLen = 0;
472 if (NULL != g_adapterHandler[index].sendDataToAll)
474 void *payload = (void *) OICMalloc(length);
477 OIC_LOG(ERROR, TAG, "Out of memory!");
478 return CA_MEMORY_ALLOC_FAILED;
480 memcpy(payload, data, length);
481 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length, dataType);
485 if (sentDataLen != length)
487 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
489 //in case of single thread, no error handler. Report error immediately
490 return CA_SEND_FAILED;
498 CAResult_t CAStartListeningServerAdapters()
500 CAResult_t result = CA_STATUS_FAILED;
502 u_arraylist_t *list = CAGetSelectedNetworkList();
505 OIC_LOG(ERROR, TAG, "No selected network");
509 size_t length = u_arraylist_length(list);
510 for (size_t i = 0; i < length; i++)
512 void* ptrType = u_arraylist_get(list, i);
519 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
521 int index = CAGetAdapterIndex(connType);
524 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
528 if (g_adapterHandler[index].startListenServer != NULL)
530 const CAResult_t tmp =
531 g_adapterHandler[index].startListenServer();
533 // Successful listen if at least one adapter started.
534 if (CA_STATUS_OK == tmp)
544 CAResult_t CAStopListeningServerAdapters()
546 u_arraylist_t *list = CAGetSelectedNetworkList();
549 OIC_LOG(ERROR, TAG, "No selected network");
550 return CA_STATUS_FAILED;
553 size_t length = u_arraylist_length(list);
554 for (size_t i = 0; i < length; i++)
556 void* ptrType = u_arraylist_get(list, i);
562 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
564 int index = CAGetAdapterIndex(connType);
567 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
571 if (g_adapterHandler[index].stopListenServer != NULL)
573 g_adapterHandler[index].stopListenServer();
580 CAResult_t CAStartDiscoveryServerAdapters()
582 CAResult_t result = CA_STATUS_FAILED;
584 u_arraylist_t *list = CAGetSelectedNetworkList();
588 OIC_LOG(ERROR, TAG, "No selected network");
592 size_t length = u_arraylist_length(list);
593 for (size_t i = 0; i < length; i++)
595 void* ptrType = u_arraylist_get(list, i);
602 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
604 int index = CAGetAdapterIndex(connType);
607 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
611 if (g_adapterHandler[index].startDiscoveryServer != NULL)
613 const CAResult_t tmp =
614 g_adapterHandler[index].startDiscoveryServer();
616 // Successful discovery if at least one adapter started.
617 if (CA_STATUS_OK == tmp)
627 void CATerminateAdapters()
629 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
631 if (g_adapterHandler[index].terminate != NULL)
633 g_adapterHandler[index].terminate();
637 OICFree(g_adapterHandler);
638 g_adapterHandler = NULL;
639 g_numberOfAdapters = 0;
643 CAResult_t CAReadData()
645 u_arraylist_t *list = CAGetSelectedNetworkList();
649 return CA_STATUS_FAILED;
653 for (i = 0; i < u_arraylist_length(list); i++)
655 void *ptrType = u_arraylist_get(list, i);
658 OIC_LOG(ERROR, TAG, "get list fail");
659 return CA_STATUS_FAILED;
662 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
664 int index = CAGetAdapterIndex(connType);
667 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
671 if (g_adapterHandler[index].readData != NULL)
673 g_adapterHandler[index].readData();