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 "caremotehandler.h"
34 #include "cathreadpool.h"
35 #include "caipadapter.h"
36 #include "cainterface.h"
39 #include "caraadapter.h"
43 #include "catcpadapter.h"
46 #define TAG "CA_INTRFC_CNTRLR"
48 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
49 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
52 #define CA_TRANSPORT_TYPE_NUM 5
54 #define CA_TRANSPORT_TYPE_NUM 4
56 #define CA_TRANSPORT_TYPE_NUM 3
59 static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM] = {};
61 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
63 static CANetworkChangeCallback g_networkChangeCallback = NULL;
65 static CAErrorHandleCallback g_errorHandleCallback = NULL;
67 static int CAGetAdapterIndex(CATransportAdapter_t cType)
73 case CA_ADAPTER_GATT_BTLE:
75 case CA_ADAPTER_RFCOMM_BTEDR:
79 case CA_ADAPTER_REMOTE_ACCESS:
94 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
96 OIC_LOG(DEBUG, TAG, "IN");
98 if(handler.startAdapter == NULL ||
99 handler.startListenServer == NULL ||
100 handler.stopListenServer == NULL ||
101 handler.startDiscoveryServer == NULL ||
102 handler.sendData == NULL ||
103 handler.sendDataToAll == NULL ||
104 handler.GetnetInfo == NULL ||
105 handler.readData == NULL ||
106 handler.stopAdapter == NULL ||
107 handler.terminate == NULL)
109 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
113 int index = CAGetAdapterIndex(cType);
117 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
121 g_adapterHandler[index] = handler;
123 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
124 OIC_LOG(DEBUG, TAG, "OUT");
128 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
130 return CASetRAInfo(caraInfo);
134 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
135 const void *data, uint32_t dataLen)
137 OIC_LOG(DEBUG, TAG, "IN");
139 if (g_networkPacketReceivedCallback != NULL)
141 g_networkPacketReceivedCallback(sep, data, dataLen);
145 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
148 OIC_LOG(DEBUG, TAG, "OUT");
151 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
153 OIC_LOG(DEBUG, TAG, "IN");
155 // Call the callback.
156 if (g_networkChangeCallback != NULL)
158 g_networkChangeCallback(info, status);
161 OIC_LOG(DEBUG, TAG, "OUT");
164 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
165 const void *data, uint32_t dataLen,
168 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
170 // Call the callback.
171 if (g_errorHandleCallback != NULL)
173 g_errorHandleCallback(endpoint, data, dataLen, result);
177 void CAInitializeAdapters(ca_thread_pool_t handle)
179 OIC_LOG(DEBUG, TAG, "initialize adapters..");
181 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
183 // Initialize adapters and register callback.
185 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
186 CAAdapterErrorHandleCallback, handle);
187 #endif /* IP_ADAPTER */
190 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
191 CAAdapterErrorHandleCallback, handle);
192 #endif /* EDR_ADAPTER */
195 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
196 CAAdapterErrorHandleCallback, handle);
197 #endif /* LE_ADAPTER */
200 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
202 #endif /* RA_ADAPTER */
205 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
206 CAAdapterErrorHandleCallback, handle);
207 #endif /* TCP_ADAPTER */
210 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
212 OIC_LOG(DEBUG, TAG, "IN");
214 g_networkPacketReceivedCallback = callback;
216 OIC_LOG(DEBUG, TAG, "OUT");
219 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
221 OIC_LOG(DEBUG, TAG, "IN");
223 g_networkChangeCallback = callback;
225 OIC_LOG(DEBUG, TAG, "OUT");
228 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
230 OIC_LOG(DEBUG, TAG, "Set error handle callback");
231 g_errorHandleCallback = errorCallback;
234 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
236 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
238 int index = CAGetAdapterIndex(transportType);
242 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
243 return CA_STATUS_FAILED;
246 if (g_adapterHandler[index].startAdapter != NULL)
248 g_adapterHandler[index].startAdapter();
254 void CAStopAdapter(CATransportAdapter_t transportType)
256 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
258 int index = CAGetAdapterIndex(transportType);
262 OIC_LOG(ERROR, TAG, "unknown transport type!");
266 if (g_adapterHandler[index].stopAdapter != NULL)
268 g_adapterHandler[index].stopAdapter();
272 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
274 if (info == NULL || size == NULL)
276 return CA_STATUS_INVALID_PARAM;
279 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
280 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
282 CAResult_t res = CA_STATUS_FAILED;
283 uint32_t resSize = 0;
284 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
286 if (g_adapterHandler[index].GetnetInfo != NULL)
288 // #1. get information for each adapter
289 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
293 if (res == CA_STATUS_OK)
295 resSize += tempSize[index];
300 "%d adapter network info size is %" PRIu32 " res:%d",
307 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
311 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
315 return CA_STATUS_FAILED;
318 // #3. add data into result
320 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
321 CA_MEMORY_ALLOC_CHECK(resInfo);
327 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
330 if (tempSize[index] == 0)
337 sizeof(*resInfo) * tempSize[index]);
339 resInfo += tempSize[index];
342 OICFree(tempInfo[index]);
343 tempInfo[index] = NULL;
346 OIC_LOG(DEBUG, TAG, "each network info save success!");
349 // memory error label.
352 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
355 OICFree(tempInfo[index]);
356 tempInfo[index] = NULL;
359 return CA_MEMORY_ALLOC_FAILED;
362 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
364 OIC_LOG(DEBUG, TAG, "IN");
366 if (endpoint == NULL)
368 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
369 return CA_STATUS_INVALID_PARAM;
372 CATransportAdapter_t type = endpoint->adapter;
374 int index = CAGetAdapterIndex(type);
378 OIC_LOG(ERROR, TAG, "unknown transport type!");
379 return CA_STATUS_INVALID_PARAM;
382 int32_t sentDataLen = 0;
384 if (g_adapterHandler[index].sendData != NULL)
386 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
389 if (sentDataLen != (int)length)
391 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
393 //in case of single thread, no error handler. Report error immediately
394 return CA_SEND_FAILED;
398 OIC_LOG(DEBUG, TAG, "OUT");
402 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
404 OIC_LOG(DEBUG, TAG, "IN");
406 u_arraylist_t *list = CAGetSelectedNetworkList();
409 OIC_LOG(DEBUG, TAG, "No selected network");
410 return CA_SEND_FAILED;
413 CATransportFlags_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
415 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
417 void* ptrType = u_arraylist_get(list, i);
424 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
425 if ((connType & requestedAdapter) == 0)
430 int index = CAGetAdapterIndex(connType);
434 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
438 uint32_t sentDataLen = 0;
440 if (g_adapterHandler[index].sendDataToAll != NULL)
442 void *payload = (void *) OICMalloc(length);
445 OIC_LOG(ERROR, TAG, "Out of memory!");
446 return CA_MEMORY_ALLOC_FAILED;
448 memcpy(payload, data, length);
449 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
453 if (sentDataLen != length)
455 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
457 //in case of single thread, no error handler. Report error immediately
458 return CA_SEND_FAILED;
463 OIC_LOG(DEBUG, TAG, "OUT");
468 CAResult_t CAStartListeningServerAdapters()
470 OIC_LOG(DEBUG, TAG, "IN");
472 u_arraylist_t *list = CAGetSelectedNetworkList();
475 OIC_LOG(ERROR, TAG, "No selected network");
476 return CA_STATUS_FAILED;
479 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
481 void* ptrType = u_arraylist_get(list, i);
488 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
490 int index = CAGetAdapterIndex(connType);
493 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
497 if (g_adapterHandler[index].startListenServer != NULL)
499 g_adapterHandler[index].startListenServer();
503 OIC_LOG(DEBUG, TAG, "OUT");
507 CAResult_t CAStopListeningServerAdapters()
509 OIC_LOG(DEBUG, TAG, "IN");
511 u_arraylist_t *list = CAGetSelectedNetworkList();
514 OIC_LOG(ERROR, TAG, "No selected network");
515 return CA_STATUS_FAILED;
518 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
520 void* ptrType = u_arraylist_get(list, i);
526 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
528 int index = CAGetAdapterIndex(connType);
531 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
535 if (g_adapterHandler[index].stopListenServer != NULL)
537 g_adapterHandler[index].stopListenServer();
541 OIC_LOG(DEBUG, TAG, "OUT");
545 CAResult_t CAStartDiscoveryServerAdapters()
547 OIC_LOG(DEBUG, TAG, "IN");
549 u_arraylist_t *list = CAGetSelectedNetworkList();
553 OIC_LOG(ERROR, TAG, "No selected network");
554 return CA_STATUS_FAILED;
557 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
559 void* ptrType = u_arraylist_get(list, i);
566 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
568 int index = CAGetAdapterIndex(connType);
572 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
576 if (g_adapterHandler[index].startDiscoveryServer != NULL)
578 g_adapterHandler[index].startDiscoveryServer();
582 OIC_LOG(DEBUG, TAG, "OUT");
586 void CATerminateAdapters()
588 OIC_LOG(DEBUG, TAG, "IN");
591 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
593 if (g_adapterHandler[index].terminate != NULL)
595 g_adapterHandler[index].terminate();
599 OIC_LOG(DEBUG, TAG, "OUT");
603 CAResult_t CAReadData()
605 OIC_LOG(DEBUG, TAG, "IN");
606 u_arraylist_t *list = CAGetSelectedNetworkList();
610 return CA_STATUS_FAILED;
614 for (i = 0; i < u_arraylist_length(list); i++)
616 void *ptrType = u_arraylist_get(list, i);
619 OIC_LOG(ERROR, TAG, "get list fail");
620 return CA_STATUS_FAILED;
623 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
625 int index = CAGetAdapterIndex(connType);
629 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
633 if (g_adapterHandler[index].readData != NULL)
635 g_adapterHandler[index].readData();
639 OIC_LOG(DEBUG, TAG, "OUT");