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;} }
54 static CAConnectivityHandler_t *g_adapterHandler = NULL;
56 static uint32_t g_numberOfAdapters = 0;
58 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
60 static CANetworkChangeCallback g_networkChangeCallback = 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 CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
129 // Call the callback.
130 if (g_networkChangeCallback != NULL)
132 g_networkChangeCallback(info, status);
136 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
137 const void *data, uint32_t dataLen,
140 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
142 // Call the callback.
143 if (g_errorHandleCallback != NULL)
145 g_errorHandleCallback(endpoint, data, dataLen, result);
149 void CAInitializeAdapters(ca_thread_pool_t handle)
151 OIC_LOG(DEBUG, TAG, "initialize adapters..");
153 // Initialize adapters and register callback.
155 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
156 CAAdapterErrorHandleCallback, handle);
157 #endif /* IP_ADAPTER */
160 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
161 CAAdapterErrorHandleCallback, handle);
162 #endif /* EDR_ADAPTER */
165 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
166 CAAdapterErrorHandleCallback, handle);
167 #endif /* LE_ADAPTER */
170 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
172 #endif /* RA_ADAPTER */
175 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
176 CAAdapterErrorHandleCallback, handle);
177 #endif /* TCP_ADAPTER */
180 CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
181 CAAdapterErrorHandleCallback, handle);
182 #endif /* NFC_ADAPTER */
185 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
187 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
189 g_networkPacketReceivedCallback = callback;
192 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
194 OIC_LOG(DEBUG, TAG, "Set network handle callback");
196 g_networkChangeCallback = callback;
199 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
201 OIC_LOG(DEBUG, TAG, "Set error handle callback");
202 g_errorHandleCallback = errorCallback;
205 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
207 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
209 int index = CAGetAdapterIndex(transportType);
212 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
213 return CA_STATUS_FAILED;
216 CAResult_t res = CA_STATUS_FAILED;
217 if (g_adapterHandler[index].startAdapter != NULL)
219 res = g_adapterHandler[index].startAdapter();
225 void CAStopAdapter(CATransportAdapter_t transportType)
227 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
229 int index = CAGetAdapterIndex(transportType);
232 OIC_LOG(ERROR, TAG, "unknown transport type!");
236 if (g_adapterHandler[index].stopAdapter != NULL)
238 g_adapterHandler[index].stopAdapter();
242 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
244 if (info == NULL || size == NULL)
246 return CA_STATUS_INVALID_PARAM;
249 CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
252 OIC_LOG(ERROR, TAG, "Out of memory!");
253 return CA_MEMORY_ALLOC_FAILED;
255 uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
258 OIC_LOG(ERROR, TAG, "Out of memory!");
260 return CA_MEMORY_ALLOC_FAILED;
263 CAResult_t res = CA_STATUS_FAILED;
265 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
267 if (g_adapterHandler[index].GetnetInfo != NULL)
269 // #1. get information for each adapter
270 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
274 if (res == CA_STATUS_OK)
276 resSize += tempSize[index];
281 "%zu adapter network info size is %" PRIu32 " res:%d",
288 OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
292 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
298 return CA_STATUS_FAILED;
301 // #3. add data into result
303 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
304 CA_MEMORY_ALLOC_CHECK(resInfo);
310 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
313 if (tempSize[index] == 0)
320 sizeof(*resInfo) * tempSize[index]);
322 resInfo += tempSize[index];
325 OICFree(tempInfo[index]);
326 tempInfo[index] = NULL;
331 OIC_LOG(DEBUG, TAG, "each network info save success!");
334 // memory error label.
337 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
339 OICFree(tempInfo[index]);
340 tempInfo[index] = NULL;
345 return CA_MEMORY_ALLOC_FAILED;
348 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
350 if (endpoint == NULL)
352 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
353 return CA_STATUS_INVALID_PARAM;
357 u_arraylist_t *list = CAGetSelectedNetworkList();
360 OIC_LOG(ERROR, TAG, "No selected network");
361 return CA_SEND_FAILED;
363 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
365 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
367 void* ptrType = u_arraylist_get(list, i);
374 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
375 if (0 == (connType & requestedAdapter))
380 int index = CAGetAdapterIndex(connType);
384 OIC_LOG(ERROR, TAG, "unknown transport type!");
385 return CA_STATUS_INVALID_PARAM;
388 int32_t sentDataLen = 0;
390 if (NULL != g_adapterHandler[index].sendData)
392 OIC_LOG(DEBUG, TAG, "unicast message to adapter");
393 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
396 if (sentDataLen != (int32_t)length)
398 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
400 //in case of single thread, no error handler. Report error immediately
401 return CA_SEND_FAILED;
410 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
412 u_arraylist_t *list = CAGetSelectedNetworkList();
415 OIC_LOG(DEBUG, TAG, "No selected network");
416 return CA_SEND_FAILED;
419 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
420 size_t selectedLength = u_arraylist_length(list);
421 for (size_t i = 0; i < selectedLength; i++)
423 void* ptrType = u_arraylist_get(list, i);
430 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
431 if (0 == (connType & requestedAdapter))
436 int index = CAGetAdapterIndex(connType);
440 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
444 uint32_t sentDataLen = 0;
446 if (NULL != g_adapterHandler[index].sendDataToAll)
448 void *payload = (void *) OICMalloc(length);
451 OIC_LOG(ERROR, TAG, "Out of memory!");
452 return CA_MEMORY_ALLOC_FAILED;
454 memcpy(payload, data, length);
455 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
459 if (sentDataLen != length)
461 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
463 //in case of single thread, no error handler. Report error immediately
464 return CA_SEND_FAILED;
472 CAResult_t CAStartListeningServerAdapters()
474 CAResult_t result = CA_STATUS_FAILED;
476 u_arraylist_t *list = CAGetSelectedNetworkList();
479 OIC_LOG(ERROR, TAG, "No selected network");
483 size_t length = u_arraylist_length(list);
484 for (size_t i = 0; i < length; i++)
486 void* ptrType = u_arraylist_get(list, i);
493 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
495 int index = CAGetAdapterIndex(connType);
498 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
502 if (g_adapterHandler[index].startListenServer != NULL)
504 const CAResult_t tmp =
505 g_adapterHandler[index].startListenServer();
507 // Successful listen if at least one adapter started.
508 if (CA_STATUS_OK == tmp)
518 CAResult_t CAStopListeningServerAdapters()
520 u_arraylist_t *list = CAGetSelectedNetworkList();
523 OIC_LOG(ERROR, TAG, "No selected network");
524 return CA_STATUS_FAILED;
527 size_t length = u_arraylist_length(list);
528 for (size_t i = 0; i < length; i++)
530 void* ptrType = u_arraylist_get(list, i);
536 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
538 int index = CAGetAdapterIndex(connType);
541 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
545 if (g_adapterHandler[index].stopListenServer != NULL)
547 g_adapterHandler[index].stopListenServer();
554 CAResult_t CAStartDiscoveryServerAdapters()
556 CAResult_t result = CA_STATUS_FAILED;
558 u_arraylist_t *list = CAGetSelectedNetworkList();
562 OIC_LOG(ERROR, TAG, "No selected network");
566 size_t length = u_arraylist_length(list);
567 for (size_t i = 0; i < length; i++)
569 void* ptrType = u_arraylist_get(list, i);
576 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
578 int index = CAGetAdapterIndex(connType);
581 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
585 if (g_adapterHandler[index].startDiscoveryServer != NULL)
587 const CAResult_t tmp =
588 g_adapterHandler[index].startDiscoveryServer();
590 // Successful discovery if at least one adapter started.
591 if (CA_STATUS_OK == tmp)
601 void CATerminateAdapters()
603 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
605 if (g_adapterHandler[index].terminate != NULL)
607 g_adapterHandler[index].terminate();
611 OICFree(g_adapterHandler);
612 g_adapterHandler = NULL;
613 g_numberOfAdapters = 0;
617 CAResult_t CAReadData()
619 u_arraylist_t *list = CAGetSelectedNetworkList();
623 return CA_STATUS_FAILED;
627 for (i = 0; i < u_arraylist_length(list); i++)
629 void *ptrType = u_arraylist_get(list, i);
632 OIC_LOG(ERROR, TAG, "get list fail");
633 return CA_STATUS_FAILED;
636 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
638 int index = CAGetAdapterIndex(connType);
641 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
645 if (g_adapterHandler[index].readData != NULL)
647 g_adapterHandler[index].readData();