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.startDiscoveryServer == NULL ||
101 handler.sendData == NULL ||
102 handler.sendDataToAll == NULL ||
103 handler.GetnetInfo == NULL ||
104 handler.readData == NULL ||
105 handler.stopAdapter == NULL ||
106 handler.terminate == NULL)
108 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
112 int index = CAGetAdapterIndex(cType);
116 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
120 g_adapterHandler[index] = handler;
122 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
123 OIC_LOG(DEBUG, TAG, "OUT");
127 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
129 return CASetRAInfo(caraInfo);
133 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
134 const void *data, uint32_t dataLen)
136 OIC_LOG(DEBUG, TAG, "IN");
138 if (g_networkPacketReceivedCallback != NULL)
140 g_networkPacketReceivedCallback(sep, data, dataLen);
144 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
147 OIC_LOG(DEBUG, TAG, "OUT");
150 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
152 OIC_LOG(DEBUG, TAG, "IN");
154 // Call the callback.
155 if (g_networkChangeCallback != NULL)
157 g_networkChangeCallback(info, status);
160 OIC_LOG(DEBUG, TAG, "OUT");
163 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
164 const void *data, uint32_t dataLen,
167 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
169 // Call the callback.
170 if (g_errorHandleCallback != NULL)
172 g_errorHandleCallback(endpoint, data, dataLen, result);
176 void CAInitializeAdapters(ca_thread_pool_t handle)
178 OIC_LOG(DEBUG, TAG, "initialize adapters..");
180 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
182 // Initialize adapters and register callback.
184 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
185 CAAdapterErrorHandleCallback, handle);
186 #endif /* IP_ADAPTER */
189 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
190 CAAdapterErrorHandleCallback, handle);
191 #endif /* EDR_ADAPTER */
194 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
195 CAAdapterErrorHandleCallback, handle);
196 #endif /* LE_ADAPTER */
199 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
201 #endif /* RA_ADAPTER */
204 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
205 CAAdapterErrorHandleCallback, handle);
206 #endif /* TCP_ADAPTER */
209 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
211 OIC_LOG(DEBUG, TAG, "IN");
213 g_networkPacketReceivedCallback = callback;
215 OIC_LOG(DEBUG, TAG, "OUT");
218 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
220 OIC_LOG(DEBUG, TAG, "IN");
222 g_networkChangeCallback = callback;
224 OIC_LOG(DEBUG, TAG, "OUT");
227 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
229 OIC_LOG(DEBUG, TAG, "Set error handle callback");
230 g_errorHandleCallback = errorCallback;
233 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
235 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
237 int index = CAGetAdapterIndex(transportType);
241 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
242 return CA_STATUS_FAILED;
245 if (g_adapterHandler[index].startAdapter != NULL)
247 g_adapterHandler[index].startAdapter();
253 void CAStopAdapter(CATransportAdapter_t transportType)
255 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
257 int index = CAGetAdapterIndex(transportType);
261 OIC_LOG(ERROR, TAG, "unknown transport type!");
265 if (g_adapterHandler[index].stopAdapter != NULL)
267 g_adapterHandler[index].stopAdapter();
271 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
273 if (info == NULL || size == NULL)
275 return CA_STATUS_INVALID_PARAM;
278 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
279 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
281 CAResult_t res = CA_STATUS_FAILED;
282 uint32_t resSize = 0;
283 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
285 if (g_adapterHandler[index].GetnetInfo != NULL)
287 // #1. get information for each adapter
288 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
292 if (res == CA_STATUS_OK)
294 resSize += tempSize[index];
299 "%d adapter network info size is %" PRIu32 " res:%d",
306 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
310 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
314 return CA_STATUS_FAILED;
317 // #3. add data into result
319 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
320 CA_MEMORY_ALLOC_CHECK(resInfo);
326 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
329 if (tempSize[index] == 0)
336 sizeof(*resInfo) * tempSize[index]);
338 resInfo += tempSize[index];
341 OICFree(tempInfo[index]);
342 tempInfo[index] = NULL;
345 OIC_LOG(DEBUG, TAG, "each network info save success!");
348 // memory error label.
351 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
354 OICFree(tempInfo[index]);
355 tempInfo[index] = NULL;
358 return CA_MEMORY_ALLOC_FAILED;
361 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
363 OIC_LOG(DEBUG, TAG, "IN");
365 if (endpoint == NULL)
367 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
368 return CA_STATUS_INVALID_PARAM;
371 CATransportAdapter_t type = endpoint->adapter;
373 int index = CAGetAdapterIndex(type);
377 OIC_LOG(ERROR, TAG, "unknown transport type!");
378 return CA_STATUS_INVALID_PARAM;
381 int32_t sentDataLen = 0;
383 if (g_adapterHandler[index].sendData != NULL)
385 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
388 if (sentDataLen != (int)length)
390 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
392 //in case of single thread, no error handler. Report error immediately
393 return CA_SEND_FAILED;
397 OIC_LOG(DEBUG, TAG, "OUT");
401 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
403 OIC_LOG(DEBUG, TAG, "IN");
405 u_arraylist_t *list = CAGetSelectedNetworkList();
408 OIC_LOG(DEBUG, TAG, "No selected network");
409 return CA_SEND_FAILED;
412 CATransportFlags_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
414 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
416 void* ptrType = u_arraylist_get(list, i);
423 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
424 if ((connType & requestedAdapter) == 0)
429 int index = CAGetAdapterIndex(connType);
433 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
437 uint32_t sentDataLen = 0;
439 if (g_adapterHandler[index].sendDataToAll != NULL)
441 void *payload = (void *) OICMalloc(length);
444 OIC_LOG(ERROR, TAG, "Out of memory!");
445 return CA_MEMORY_ALLOC_FAILED;
447 memcpy(payload, data, length);
448 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
452 if (sentDataLen != length)
454 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
456 //in case of single thread, no error handler. Report error immediately
457 return CA_SEND_FAILED;
462 OIC_LOG(DEBUG, TAG, "OUT");
467 CAResult_t CAStartListeningServerAdapters()
469 OIC_LOG(DEBUG, TAG, "IN");
471 u_arraylist_t *list = CAGetSelectedNetworkList();
474 OIC_LOG(ERROR, TAG, "No selected network");
475 return CA_STATUS_FAILED;
478 for (uint32_t i = 0; i < u_arraylist_length(list); i++)
480 void* ptrType = u_arraylist_get(list, i);
487 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
489 int index = CAGetAdapterIndex(connType);
492 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
496 if (g_adapterHandler[index].startListenServer != NULL)
498 g_adapterHandler[index].startListenServer();
502 OIC_LOG(DEBUG, TAG, "OUT");
506 CAResult_t CAStartDiscoveryServerAdapters()
508 OIC_LOG(DEBUG, TAG, "IN");
510 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);
527 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
529 int index = CAGetAdapterIndex(connType);
533 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
537 if (g_adapterHandler[index].startDiscoveryServer != NULL)
539 g_adapterHandler[index].startDiscoveryServer();
543 OIC_LOG(DEBUG, TAG, "OUT");
547 void CATerminateAdapters()
549 OIC_LOG(DEBUG, TAG, "IN");
552 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
554 if (g_adapterHandler[index].terminate != NULL)
556 g_adapterHandler[index].terminate();
560 OIC_LOG(DEBUG, TAG, "OUT");
564 CAResult_t CAReadData()
566 OIC_LOG(DEBUG, TAG, "IN");
567 u_arraylist_t *list = CAGetSelectedNetworkList();
571 return CA_STATUS_FAILED;
575 for (i = 0; i < u_arraylist_length(list); i++)
577 void *ptrType = u_arraylist_get(list, i);
580 OIC_LOG(ERROR, TAG, "get list fail");
581 return CA_STATUS_FAILED;
584 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
586 int index = CAGetAdapterIndex(connType);
590 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
594 if (g_adapterHandler[index].readData != NULL)
596 g_adapterHandler[index].readData();
600 OIC_LOG(DEBUG, TAG, "OUT");