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 "caipinterface.h"
39 #include <coap/utlist.h>
43 #include "caraadapter.h"
47 #include "catcpadapter.h"
50 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
51 #include "ca_adapter_net_ssl.h"
54 #define TAG "OIC_CA_INF_CTR"
56 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
57 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
59 static CAConnectivityHandler_t *g_adapterHandler = NULL;
61 static uint32_t g_numberOfAdapters = 0;
63 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
65 static CAErrorHandleCallback g_errorHandleCallback = NULL;
67 static struct CANetworkCallback_t * g_networkChangeCallbackList = NULL;
70 * Mutex to synchronize network change list.
72 static oc_mutex g_mutexNetCallbackList = NULL;
75 * network callback structure is handling
76 * for adapter state changed and connection state changed event.
78 typedef struct CANetworkCallback_t {
80 /** Linked list; for multiple callback list.*/
81 struct CANetworkCallback_t * next;
83 /** Adapter state changed event callback. */
84 CAAdapterStateChangedCB adapter;
86 /** Connection state changed event callback. */
87 CAConnectionStateChangedCB conn;
89 } CANetworkCallback_t;
91 static int CAGetAdapterIndex(CATransportAdapter_t cType)
93 for (uint32_t index=0 ; index < g_numberOfAdapters ; index++)
95 if (cType == g_adapterHandler[index].cType )
100 OIC_LOG_V(ERROR, TAG, "adapter info [%d]", g_numberOfAdapters);
104 static void CARegisterCallback(CAConnectivityHandler_t handler)
106 if (handler.startAdapter == NULL ||
107 handler.startListenServer == NULL ||
108 handler.stopListenServer == NULL ||
109 handler.startDiscoveryServer == NULL ||
110 handler.sendData == NULL ||
111 handler.sendDataToAll == NULL ||
112 handler.GetnetInfo == NULL ||
113 handler.readData == NULL ||
114 handler.stopAdapter == NULL ||
115 handler.terminate == NULL)
117 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
120 uint32_t numberofAdapters = g_numberOfAdapters + 1;
121 CAConnectivityHandler_t *adapterHandler = OICRealloc(g_adapterHandler,
122 (numberofAdapters) * sizeof(*adapterHandler));
123 if (NULL == adapterHandler)
125 OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
128 g_adapterHandler = adapterHandler;
129 g_numberOfAdapters = numberofAdapters;
130 g_adapterHandler[g_numberOfAdapters-1] = handler;
132 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", handler.cType);
136 * Add a network callback from caller to the network callback list
138 * @param adapterCB adapter state changed callback
139 * @param connCB connection state changed callback
144 static CAResult_t AddNetworkStateChangedCallback(CAAdapterStateChangedCB adapterCB,
145 CAConnectionStateChangedCB connCB)
147 OIC_LOG(DEBUG, TAG, "Add NetworkStateChanged Callback");
149 if (!adapterCB || !connCB)
151 OIC_LOG(ERROR, TAG, "parameter is null");
152 return CA_STATUS_INVALID_PARAM;
155 oc_mutex_lock(g_mutexNetCallbackList);
156 CANetworkCallback_t* callback = NULL;
157 LL_FOREACH(g_networkChangeCallbackList, callback)
159 if (callback && adapterCB == callback->adapter && connCB == callback->conn)
161 OIC_LOG(DEBUG, TAG, "this callback is already added");
162 oc_mutex_unlock(g_mutexNetCallbackList);
167 callback = (CANetworkCallback_t *) OICCalloc(1, sizeof(CANetworkCallback_t));
168 if (NULL == callback)
170 OIC_LOG(ERROR, TAG, "Memory allocation failed during registration");
171 oc_mutex_unlock(g_mutexNetCallbackList);
172 return CA_MEMORY_ALLOC_FAILED;
175 callback->adapter = adapterCB;
176 callback->conn = connCB;
177 LL_APPEND(g_networkChangeCallbackList, callback);
178 oc_mutex_unlock(g_mutexNetCallbackList);
179 OIC_LOG_V(INFO, TAG, "Added NetworkStateChanged Callback [%p]", callback);
185 * Remove a network callback from the network callback list
187 * @param adapterCB adapter state changed callback
188 * @param connCB connection state changed callback
193 static CAResult_t RemoveNetworkStateChangedCallback(CAAdapterStateChangedCB adapterCB,
194 CAConnectionStateChangedCB connCB)
196 OIC_LOG(DEBUG, TAG, "Remove NetworkStateChanged Callback");
198 oc_mutex_lock(g_mutexNetCallbackList);
199 CANetworkCallback_t* callback = NULL;
200 LL_FOREACH(g_networkChangeCallbackList, callback)
202 if (callback && adapterCB == callback->adapter && connCB == callback->conn)
204 OIC_LOG(DEBUG, TAG, "remove specific callback");
205 LL_DELETE(g_networkChangeCallbackList, callback);
206 oc_mutex_unlock(g_mutexNetCallbackList);
211 oc_mutex_unlock(g_mutexNetCallbackList);
217 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
219 return CASetRAInfo(caraInfo);
223 static CAResult_t CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
224 const void *data, uint32_t dataLen)
226 if (g_networkPacketReceivedCallback != NULL)
228 return g_networkPacketReceivedCallback(sep, data, dataLen);
232 OIC_LOG(INFO, TAG, "network packet received callback is NULL!");
237 static void CAAdapterChangedCallback(CATransportAdapter_t adapter, CANetworkStatus_t status)
240 oc_mutex_lock(g_mutexNetCallbackList);
241 // Call the callback.
242 CANetworkCallback_t* callback = NULL;
243 LL_FOREACH(g_networkChangeCallbackList, callback)
245 if (callback && callback->adapter)
247 OIC_LOG_V(INFO, TAG, "IN application adapter changed callback [%p]", callback);
248 if (CA_INTERFACE_UP == status)
250 callback->adapter(adapter, true);
252 else if (CA_INTERFACE_DOWN == status)
254 callback->adapter(adapter, false);
256 OIC_LOG_V(INFO, TAG, "OUT application adapter changed callback [%p]", callback);
259 oc_mutex_unlock(g_mutexNetCallbackList);
260 OIC_LOG_V(DEBUG, TAG, "[%d] adapter status is changed to [%d]", adapter, status);
263 #if defined(TCP_ADAPTER) || defined(EDR_ADAPTER) || defined(LE_ADAPTER)
264 static void CAConnectionChangedCallback(const CAEndpoint_t *info, bool isConnected)
266 oc_mutex_lock(g_mutexNetCallbackList);
267 // Call the callback.
268 CANetworkCallback_t* callback = NULL;
269 LL_FOREACH(g_networkChangeCallbackList, callback)
271 if (callback && callback->conn)
273 callback->conn(info, isConnected);
276 oc_mutex_unlock(g_mutexNetCallbackList);
277 OIC_LOG_V(DEBUG, TAG, "[%s] connection status is changed to [%d]", info->addr, isConnected);
281 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
282 const void *data, uint32_t dataLen,
285 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
287 // Call the callback.
288 if (g_errorHandleCallback != NULL)
290 g_errorHandleCallback(endpoint, data, dataLen, result);
294 static void CADestroyMutex()
296 if (g_mutexNetCallbackList)
298 oc_mutex_free(g_mutexNetCallbackList);
299 g_mutexNetCallbackList = NULL;
303 static CAResult_t CACreateMutex()
305 if (!g_mutexNetCallbackList)
307 g_mutexNetCallbackList = oc_mutex_new();
308 if (!g_mutexNetCallbackList)
310 return CA_STATUS_FAILED;
317 void CAInitializeAdapters(ca_thread_pool_t handle, CATransportAdapter_t transportType)
319 OIC_LOG_V(DEBUG, TAG, "initialize adapters %d", transportType);
321 if (CA_STATUS_OK != CACreateMutex())
323 OIC_LOG(ERROR, TAG, "Failed to create mutex!");
326 // Initialize ssl adapter.
327 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
328 if (CA_STATUS_OK != CAinitSslAdapter())
330 OIC_LOG(ERROR, TAG, "Failed to init SSL adapter");
334 // Initialize adapters and register callback.
336 if ((transportType & CA_ADAPTER_IP) || (CA_DEFAULT_ADAPTER == transportType)
337 || (transportType == CA_ALL_ADAPTERS))
339 CAInitializeIP(CARegisterCallback, (CANetworkPacketReceivedCallback)CAReceivedPacketCallback, CAAdapterChangedCallback,
340 (CAErrorHandleCallback)CAAdapterErrorHandleCallback, handle);
342 #endif /* IP_ADAPTER */
345 if ((transportType & CA_ADAPTER_RFCOMM_BTEDR) || (CA_DEFAULT_ADAPTER == transportType)
346 || (transportType == CA_ALL_ADAPTERS))
348 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
349 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
351 #endif /* EDR_ADAPTER */
354 if ((transportType & CA_ADAPTER_GATT_BTLE) || (CA_DEFAULT_ADAPTER == transportType)
355 || (transportType == CA_ALL_ADAPTERS))
357 CAInitializeLE(CARegisterCallback, (CANetworkPacketReceivedCallback)CAReceivedPacketCallback, CAAdapterChangedCallback,
358 CAConnectionChangedCallback, (CAErrorHandleCallback)CAAdapterErrorHandleCallback, handle);
360 #endif /* LE_ADAPTER */
363 if ((transportType & CA_ADAPTER_REMOTE_ACCESS) || (CA_DEFAULT_ADAPTER == transportType)
364 || (transportType == CA_ALL_ADAPTERS))
366 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
369 #endif /* RA_ADAPTER */
372 if ((transportType & CA_ADAPTER_TCP) || (CA_DEFAULT_ADAPTER == transportType)
373 || (transportType == CA_ALL_ADAPTERS))
375 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
376 CAConnectionChangedCallback, CAAdapterErrorHandleCallback, handle);
378 #endif /* TCP_ADAPTER */
381 if ((transportType & CA_ADAPTER_NFC) || (CA_DEFAULT_ADAPTER == transportType)
382 || (transportType == CA_ALL_ADAPTERS))
384 CAInitializeNFC(CARegisterCallback, CAReceivedPacketCallback, CAAdapterChangedCallback,
385 CAAdapterErrorHandleCallback, handle);
387 #endif /* NFC_ADAPTER */
390 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
392 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
394 g_networkPacketReceivedCallback = callback;
397 void CASetNetworkMonitorCallbacks(CAAdapterStateChangedCB adapterCB,
398 CAConnectionStateChangedCB connCB)
400 OIC_LOG(DEBUG, TAG, "Set network monitoring callback");
401 CAResult_t res = AddNetworkStateChangedCallback(adapterCB, connCB);
402 if (CA_STATUS_OK != res)
404 OIC_LOG(ERROR, TAG, "AddNetworkStateChangedCallback has failed");
408 CAResult_t CAUnsetNetworkMonitorCallbacks(CAAdapterStateChangedCB adapterCB,
409 CAConnectionStateChangedCB connCB)
411 OIC_LOG(DEBUG, TAG, "Unset network monitoring callback");
412 CAResult_t res = RemoveNetworkStateChangedCallback(adapterCB, connCB);
413 if (CA_STATUS_OK != res)
415 OIC_LOG(ERROR, TAG, "RemoveNetworkStateChangedCallback has failed");
420 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
422 OIC_LOG(DEBUG, TAG, "Set error handle callback");
423 g_errorHandleCallback = errorCallback;
426 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
428 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
430 int index = CAGetAdapterIndex(transportType);
433 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
434 return CA_STATUS_FAILED;
437 CAResult_t res = CA_STATUS_FAILED;
438 if (g_adapterHandler[index].startAdapter != NULL)
440 res = g_adapterHandler[index].startAdapter();
446 void CAStopAdapter(CATransportAdapter_t transportType)
448 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
450 int index = CAGetAdapterIndex(transportType);
453 OIC_LOG(ERROR, TAG, "unknown transport type!");
457 if (g_adapterHandler[index].stopAdapter != NULL)
459 g_adapterHandler[index].stopAdapter();
463 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
465 if (info == NULL || size == NULL)
467 return CA_STATUS_INVALID_PARAM;
470 CAEndpoint_t **tempInfo = (CAEndpoint_t**) OICCalloc(g_numberOfAdapters, sizeof(*tempInfo));
473 OIC_LOG(ERROR, TAG, "Out of memory!");
474 return CA_MEMORY_ALLOC_FAILED;
476 uint32_t *tempSize =(uint32_t*) OICCalloc(g_numberOfAdapters, sizeof(*tempSize));
479 OIC_LOG(ERROR, TAG, "Out of memory!");
481 return CA_MEMORY_ALLOC_FAILED;
484 CAResult_t res = CA_STATUS_FAILED;
485 uint32_t resSize = 0;
486 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
488 if (g_adapterHandler[index].GetnetInfo != NULL)
490 // #1. get information for each adapter
491 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
495 if (res == CA_STATUS_OK)
497 resSize += tempSize[index];
503 "%" PRIu32 " adapter network info size is %" PRIu32 " res:%d",
511 OIC_LOG_V(DEBUG, TAG, "network info total size is %" PRIu32, resSize);
517 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
523 return CA_STATUS_FAILED;
527 // #3. add data into result
529 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
530 CA_MEMORY_ALLOC_CHECK(resInfo);
536 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
539 if (tempSize[index] == 0)
546 sizeof(*resInfo) * tempSize[index]);
548 resInfo += tempSize[index];
551 OICFree(tempInfo[index]);
552 tempInfo[index] = NULL;
557 OIC_LOG(DEBUG, TAG, "each network info save success!");
560 // memory error label.
563 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
565 OICFree(tempInfo[index]);
566 tempInfo[index] = NULL;
571 return CA_MEMORY_ALLOC_FAILED;
574 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
575 CADataType_t dataType)
577 if (endpoint == NULL)
579 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
580 return CA_STATUS_INVALID_PARAM;
584 u_arraylist_t *list = CAGetSelectedNetworkList();
587 OIC_LOG(ERROR, TAG, "No selected network");
588 return CA_SEND_FAILED;
590 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
592 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
594 void* ptrType = u_arraylist_get(list, i);
601 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
602 if (0 == (connType & requestedAdapter))
607 int index = CAGetAdapterIndex(connType);
611 OIC_LOG_V(ERROR, TAG, "unknown transport type[%d]", connType);
612 return CA_STATUS_INVALID_PARAM;
615 int32_t sentDataLen = 0;
617 if (NULL != g_adapterHandler[index].sendData)
619 OIC_LOG(DEBUG, TAG, "unicast message to adapter");
620 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length, dataType);
623 if (sentDataLen != (int32_t)length)
625 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
627 //in case of single thread, no error handler. Report error immediately
628 return CA_SEND_FAILED;
637 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length,
638 CADataType_t dataType)
640 u_arraylist_t *list = CAGetSelectedNetworkList();
643 OIC_LOG(DEBUG, TAG, "No selected network");
644 return CA_SEND_FAILED;
647 CATransportAdapter_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
648 size_t selectedLength = u_arraylist_length(list);
649 for (size_t i = 0; i < selectedLength; i++)
651 void* ptrType = u_arraylist_get(list, i);
658 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
659 if (0 == (connType & requestedAdapter))
664 int index = CAGetAdapterIndex(connType);
667 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
671 uint32_t sentDataLen = 0;
673 if (NULL != g_adapterHandler[index].sendDataToAll)
675 void *payload = (void *) OICMalloc(length);
678 OIC_LOG(ERROR, TAG, "Out of memory!");
679 return CA_MEMORY_ALLOC_FAILED;
681 memcpy(payload, data, length);
682 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length, dataType);
686 if (sentDataLen != length)
688 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
690 //in case of single thread, no error handler. Report error immediately
691 return CA_SEND_FAILED;
699 CAResult_t CAStartListeningServerAdapters()
701 CAResult_t result = CA_STATUS_FAILED;
703 u_arraylist_t *list = CAGetSelectedNetworkList();
706 OIC_LOG(ERROR, TAG, "No selected network");
710 size_t length = u_arraylist_length(list);
711 for (size_t i = 0; i < length; i++)
713 void* ptrType = u_arraylist_get(list, i);
720 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
722 int index = CAGetAdapterIndex(connType);
725 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
729 if (g_adapterHandler[index].startListenServer != NULL)
731 const CAResult_t tmp =
732 g_adapterHandler[index].startListenServer();
734 // Successful listen if at least one adapter started.
735 if (CA_STATUS_OK == tmp)
745 CAResult_t CAStopListeningServerAdapters()
747 u_arraylist_t *list = CAGetSelectedNetworkList();
750 OIC_LOG(ERROR, TAG, "No selected network");
751 return CA_STATUS_FAILED;
754 size_t length = u_arraylist_length(list);
755 for (size_t i = 0; i < length; i++)
757 void* ptrType = u_arraylist_get(list, i);
763 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
765 int index = CAGetAdapterIndex(connType);
768 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
772 if (g_adapterHandler[index].stopListenServer != NULL)
774 g_adapterHandler[index].stopListenServer();
781 CAResult_t CAStartDiscoveryServerAdapters()
783 CAResult_t result = CA_STATUS_FAILED;
785 u_arraylist_t *list = CAGetSelectedNetworkList();
789 OIC_LOG(ERROR, TAG, "No selected network");
793 size_t length = u_arraylist_length(list);
794 for (size_t i = 0; i < length; i++)
796 void* ptrType = u_arraylist_get(list, i);
803 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
805 int index = CAGetAdapterIndex(connType);
808 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
812 if (g_adapterHandler[index].startDiscoveryServer != NULL)
814 const CAResult_t tmp =
815 g_adapterHandler[index].startDiscoveryServer();
817 // Successful discovery if at least one adapter started.
818 if (CA_STATUS_OK == tmp)
828 void CATerminateAdapters()
832 for (uint32_t index = 0; index < g_numberOfAdapters; index++)
834 if (g_adapterHandler[index].terminate != NULL)
836 g_adapterHandler[index].terminate();
840 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
841 CAdeinitSslAdapter();
844 OICFree(g_adapterHandler);
845 g_adapterHandler = NULL;
846 g_numberOfAdapters = 0;
850 CAResult_t CAReadData()
852 u_arraylist_t *list = CAGetSelectedNetworkList();
856 return CA_STATUS_FAILED;
860 for (i = 0; i < u_arraylist_length(list); i++)
862 void *ptrType = u_arraylist_get(list, i);
865 OIC_LOG(ERROR, TAG, "get list fail");
866 return CA_STATUS_FAILED;
869 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
871 int index = CAGetAdapterIndex(connType);
874 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
878 if (g_adapterHandler[index].readData != NULL)
880 g_adapterHandler[index].readData();
889 CAResult_t CASetMulticastTTL(size_t ttl)
891 return CAIPSetMulticastTTL(ttl);
894 CAResult_t CAGetMulticastTTL(size_t *ttl)
896 return CAIPGetMulticastTTL(ttl);
901 CAResult_t CADisconnectSession(const CAEndpoint_t *endpoint)
903 return CATCPDisconnectSession(endpoint);
908 void CAStartGattServer()
910 CALEStartGattServer();
913 void CAStopGattServer()
915 CALEStopGattServer();