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 static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
146 // Call the callback.
147 if (g_connChangeCallback != NULL)
149 g_connChangeCallback(info, isConnected);
151 OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
154 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
155 const void *data, uint32_t dataLen,
158 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
160 // Call the callback.
161 if (g_errorHandleCallback != NULL)
163 g_errorHandleCallback(endpoint, data, dataLen, result);
167 void CAInitializeAdapters(ca_thread_pool_t handle)
169 OIC_LOG(DEBUG, TAG, "initialize adapters..");
171 // Initialize adapters and register callback.
173 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
174 CAAdapterErrorHandleCallback, handle);
175 #endif /* IP_ADAPTER */
178 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
179 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
180 #endif /* EDR_ADAPTER */
183 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
184 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
185 #endif /* LE_ADAPTER */
188 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
190 #endif /* RA_ADAPTER */
193 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
194 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
195 #endif /* TCP_ADAPTER */
198 CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
199 CAAdapterErrorHandleCallback, handle);
200 #endif /* NFC_ADAPTER */
203 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
205 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
207 g_networkPacketReceivedCallback = callback;
210 void CASetNetworkMonitorCallbacks(CAAdapterStateChangedCB adapterCB,
211 CAConnectionStateChangedCB connCB)
213 OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
215 g_adapterChangeCallback = adapterCB;
216 g_connChangeCallback = connCB;
219 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
221 OIC_LOG(DEBUG, TAG, "Set error handle callback");
222 g_errorHandleCallback = errorCallback;
225 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
227 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
229 int index = CAGetAdapterIndex(transportType);
232 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
233 return CA_STATUS_FAILED;
236 CAResult_t res = CA_STATUS_FAILED;
237 if (g_adapterHandler[index].startAdapter != NULL)
239 res = g_adapterHandler[index].startAdapter();
245 void CAStopAdapter(CATransportAdapter_t transportType)
247 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
249 int index = CAGetAdapterIndex(transportType);
252 OIC_LOG(ERROR, TAG, "unknown transport type!");
256 if (g_adapterHandler[index].stopAdapter != NULL)
258 g_adapterHandler[index].stopAdapter();
262 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
264 if (info == NULL || size == NULL)
266 return CA_STATUS_INVALID_PARAM;
269 CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
272 OIC_LOG(ERROR, TAG, "Out of memory!");
273 return CA_MEMORY_ALLOC_FAILED;
275 uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
278 OIC_LOG(ERROR, TAG, "Out of memory!");
280 return CA_MEMORY_ALLOC_FAILED;
283 CAResult_t res = CA_STATUS_FAILED;
285 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
287 if (g_adapterHandler[index].GetnetInfo != NULL)
289 // #1. get information for each adapter
290 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
294 if (res == CA_STATUS_OK)
296 resSize += tempSize[index];
301 "%zu adapter network info size is %" PRIu32 " res:%d",
308 OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
314 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
320 return CA_STATUS_FAILED;
324 // #3. add data into result
326 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
327 CA_MEMORY_ALLOC_CHECK(resInfo);
333 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
336 if (tempSize[index] == 0)
343 sizeof(*resInfo) * tempSize[index]);
345 resInfo += tempSize[index];
348 OICFree(tempInfo[index]);
349 tempInfo[index] = NULL;
354 OIC_LOG(DEBUG, TAG, "each network info save success!");
357 // memory error label.
360 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
362 OICFree(tempInfo[index]);
363 tempInfo[index] = NULL;
368 return CA_MEMORY_ALLOC_FAILED;
371 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
372 CADataType_t dataType)
374 if (endpoint == NULL)
376 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
377 return CA_STATUS_INVALID_PARAM;
381 u_arraylist_t *list = CAGetSelectedNetworkList();
384 OIC_LOG(ERROR, TAG, "No selected network");
385 return CA_SEND_FAILED;
387 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
389 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
391 void* ptrType = u_arraylist_get(list, i);
398 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
399 if (0 == (connType & requestedAdapter))
404 int index = CAGetAdapterIndex(connType);
408 OIC_LOG(ERROR, TAG, "unknown transport type!");
409 return CA_STATUS_INVALID_PARAM;
412 int32_t sentDataLen = 0;
414 if (NULL != g_adapterHandler[index].sendData)
416 OIC_LOG(DEBUG, TAG, "unicast message to adapter");
417 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length, dataType);
420 if (sentDataLen != (int32_t)length)
422 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
424 //in case of single thread, no error handler. Report error immediately
425 return CA_SEND_FAILED;
434 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
435 CADataType_t dataType)
437 u_arraylist_t *list = CAGetSelectedNetworkList();
440 OIC_LOG(DEBUG, TAG, "No selected network");
441 return CA_SEND_FAILED;
444 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
445 size_t selectedLength = u_arraylist_length(list);
446 for (size_t i = 0; i < selectedLength; i++)
448 void* ptrType = u_arraylist_get(list, i);
455 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
456 if (0 == (connType & requestedAdapter))
461 int index = CAGetAdapterIndex(connType);
464 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
468 uint32_t sentDataLen = 0;
470 if (NULL != g_adapterHandler[index].sendDataToAll)
472 void *payload = (void *) OICMalloc(length);
475 OIC_LOG(ERROR, TAG, "Out of memory!");
476 return CA_MEMORY_ALLOC_FAILED;
478 memcpy(payload, data, length);
479 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length, dataType);
483 if (sentDataLen != length)
485 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
487 //in case of single thread, no error handler. Report error immediately
488 return CA_SEND_FAILED;
496 CAResult_t CAStartListeningServerAdapters()
498 CAResult_t result = CA_STATUS_FAILED;
500 u_arraylist_t *list = CAGetSelectedNetworkList();
503 OIC_LOG(ERROR, TAG, "No selected network");
507 size_t length = u_arraylist_length(list);
508 for (size_t i = 0; i < length; i++)
510 void* ptrType = u_arraylist_get(list, i);
517 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
519 int index = CAGetAdapterIndex(connType);
522 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
526 if (g_adapterHandler[index].startListenServer != NULL)
528 const CAResult_t tmp =
529 g_adapterHandler[index].startListenServer();
531 // Successful listen if at least one adapter started.
532 if (CA_STATUS_OK == tmp)
542 CAResult_t CAStopListeningServerAdapters()
544 u_arraylist_t *list = CAGetSelectedNetworkList();
547 OIC_LOG(ERROR, TAG, "No selected network");
548 return CA_STATUS_FAILED;
551 size_t length = u_arraylist_length(list);
552 for (size_t i = 0; i < length; i++)
554 void* ptrType = u_arraylist_get(list, i);
560 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
562 int index = CAGetAdapterIndex(connType);
565 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
569 if (g_adapterHandler[index].stopListenServer != NULL)
571 g_adapterHandler[index].stopListenServer();
578 CAResult_t CAStartDiscoveryServerAdapters()
580 CAResult_t result = CA_STATUS_FAILED;
582 u_arraylist_t *list = CAGetSelectedNetworkList();
586 OIC_LOG(ERROR, TAG, "No selected network");
590 size_t length = u_arraylist_length(list);
591 for (size_t i = 0; i < length; i++)
593 void* ptrType = u_arraylist_get(list, i);
600 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
602 int index = CAGetAdapterIndex(connType);
605 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
609 if (g_adapterHandler[index].startDiscoveryServer != NULL)
611 const CAResult_t tmp =
612 g_adapterHandler[index].startDiscoveryServer();
614 // Successful discovery if at least one adapter started.
615 if (CA_STATUS_OK == tmp)
625 void CATerminateAdapters()
627 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
629 if (g_adapterHandler[index].terminate != NULL)
631 g_adapterHandler[index].terminate();
635 OICFree(g_adapterHandler);
636 g_adapterHandler = NULL;
637 g_numberOfAdapters = 0;
641 CAResult_t CAReadData()
643 u_arraylist_t *list = CAGetSelectedNetworkList();
647 return CA_STATUS_FAILED;
651 for (i = 0; i < u_arraylist_length(list); i++)
653 void *ptrType = u_arraylist_get(list, i);
656 OIC_LOG(ERROR, TAG, "get list fail");
657 return CA_STATUS_FAILED;
660 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
662 int index = CAGetAdapterIndex(connType);
665 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
669 if (g_adapterHandler[index].readData != NULL)
671 g_adapterHandler[index].readData();