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"
42 #define TAG "CA_INTRFC_CNTRLR"
44 #include "caraadapter.h"
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 4
54 #define CA_TRANSPORT_TYPE_NUM 3
57 static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM] = {};
59 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
61 static CANetworkChangeCallback g_networkChangeCallback = NULL;
63 static CAErrorHandleCallback g_errorHandleCallback = NULL;
65 static int CAGetAdapterIndex(CATransportAdapter_t cType)
71 case CA_ADAPTER_GATT_BTLE:
73 case CA_ADAPTER_RFCOMM_BTEDR:
77 case CA_ADAPTER_REMOTE_ACCESS:
87 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
89 OIC_LOG(DEBUG, TAG, "IN");
91 if(handler.startAdapter == NULL ||
92 handler.startListenServer == NULL ||
93 handler.startDiscoveryServer == NULL ||
94 handler.sendData == NULL ||
95 handler.sendDataToAll == NULL ||
96 handler.GetnetInfo == NULL ||
97 handler.readData == NULL ||
98 handler.stopAdapter == NULL ||
99 handler.terminate == NULL)
101 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
105 int index = CAGetAdapterIndex(cType);
109 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
113 g_adapterHandler[index] = handler;
115 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
116 OIC_LOG(DEBUG, TAG, "OUT");
120 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
122 return CASetRAInfo(caraInfo);
126 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
128 OIC_LOG(DEBUG, TAG, "IN");
130 // Call the callback.
131 if (g_networkPacketReceivedCallback != NULL)
133 g_networkPacketReceivedCallback(endpoint, data, dataLen);
139 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
142 OIC_LOG(DEBUG, TAG, "OUT");
145 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
147 OIC_LOG(DEBUG, TAG, "IN");
149 // Call the callback.
150 if (g_networkChangeCallback != NULL)
152 g_networkChangeCallback(info, status);
155 OIC_LOG(DEBUG, TAG, "OUT");
158 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
159 const void *data, uint32_t dataLen,
162 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
164 // Call the callback.
165 if (g_errorHandleCallback != NULL)
167 g_errorHandleCallback(endpoint, data, dataLen, result);
171 void CAInitializeAdapters(ca_thread_pool_t handle)
173 OIC_LOG(DEBUG, TAG, "initialize adapters..");
175 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
177 // Initialize adapters and register callback.
179 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
180 CAAdapterErrorHandleCallback, handle);
181 #endif /* IP_ADAPTER */
184 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
185 CAAdapterErrorHandleCallback, handle);
186 #endif /* EDR_ADAPTER */
189 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
190 CAAdapterErrorHandleCallback, handle);
191 #endif /* LE_ADAPTER */
194 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
196 #endif /* RA_ADAPTER */
201 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
203 OIC_LOG(DEBUG, TAG, "IN");
205 g_networkPacketReceivedCallback = callback;
207 OIC_LOG(DEBUG, TAG, "OUT");
210 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
212 OIC_LOG(DEBUG, TAG, "IN");
214 g_networkChangeCallback = callback;
216 OIC_LOG(DEBUG, TAG, "OUT");
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);
233 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
234 return CA_STATUS_FAILED;
237 if (g_adapterHandler[index].startAdapter != NULL)
239 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);
253 OIC_LOG(ERROR, TAG, "unknown transport type!");
257 if (g_adapterHandler[index].stopAdapter != NULL)
259 g_adapterHandler[index].stopAdapter();
263 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
265 if (info == NULL || size == NULL)
267 return CA_STATUS_INVALID_PARAM;
270 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
271 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
273 CAResult_t res = CA_STATUS_FAILED;
274 uint32_t resSize = 0;
275 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
277 if (g_adapterHandler[index].GetnetInfo != NULL)
279 // #1. get information for each adapter
280 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
284 if (res == CA_STATUS_OK)
286 resSize += tempSize[index];
291 "%d adapter network info size is %" PRIu32 " res:%d",
298 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
302 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
306 return CA_STATUS_FAILED;
309 // #3. add data into result
311 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
312 CA_MEMORY_ALLOC_CHECK(resInfo);
318 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
321 if (tempSize[index] == 0)
328 sizeof(*resInfo) * tempSize[index]);
330 resInfo += tempSize[index];
333 OICFree(tempInfo[index]);
334 tempInfo[index] = NULL;
337 OIC_LOG(DEBUG, TAG, "each network info save success!");
340 // memory error label.
343 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
346 OICFree(tempInfo[index]);
347 tempInfo[index] = NULL;
350 return CA_MEMORY_ALLOC_FAILED;
353 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
355 OIC_LOG(DEBUG, TAG, "IN");
357 CAResult_t res = CA_STATUS_FAILED;
359 if (endpoint == NULL)
361 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
362 return CA_STATUS_INVALID_PARAM;
365 CATransportAdapter_t type = endpoint->adapter;
367 int index = CAGetAdapterIndex(type);
371 OIC_LOG(ERROR, TAG, "unknown transport type!");
372 return CA_STATUS_INVALID_PARAM;
375 uint32_t sentDataLen = 0;
377 if (g_adapterHandler[index].sendData != NULL)
379 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
382 if (sentDataLen != -1)
387 OIC_LOG(DEBUG, TAG, "OUT");
391 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
393 OIC_LOG(DEBUG, TAG, "IN");
395 CAResult_t res = CA_SEND_FAILED;
396 u_arraylist_t *list = CAGetSelectedNetworkList();
400 OIC_LOG(DEBUG, TAG, "No selected network");
401 return CA_SEND_FAILED;
405 for (i = 0; i < u_arraylist_length(list); i++)
407 void* ptrType = u_arraylist_get(list, i);
414 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
416 int index = CAGetAdapterIndex(connType);
420 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
424 uint32_t sentDataLen = 0;
426 if (g_adapterHandler[index].sendDataToAll != NULL)
428 void *payload = (void *) OICMalloc(length);
431 OIC_LOG(ERROR, TAG, "Out of memory!");
432 return CA_MEMORY_ALLOC_FAILED;
434 memcpy(payload, data, length);
435 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
439 if (sentDataLen == length)
445 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
449 OIC_LOG(DEBUG, TAG, "OUT");
454 CAResult_t CAStartListeningServerAdapters()
456 OIC_LOG(DEBUG, TAG, "IN");
458 u_arraylist_t *list = CAGetSelectedNetworkList();
461 OIC_LOG(ERROR, TAG, "No selected network");
462 return CA_STATUS_FAILED;
466 for (i = 0; i < u_arraylist_length(list); i++)
468 void* ptrType = u_arraylist_get(list, i);
475 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
477 int index = CAGetAdapterIndex(connType);
480 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
484 if (g_adapterHandler[index].startListenServer != NULL)
486 g_adapterHandler[index].startListenServer();
490 OIC_LOG(DEBUG, TAG, "OUT");
494 CAResult_t CAStartDiscoveryServerAdapters()
496 OIC_LOG(DEBUG, TAG, "IN");
498 u_arraylist_t *list = CAGetSelectedNetworkList();
502 OIC_LOG(ERROR, TAG, "No selected network");
503 return CA_STATUS_FAILED;
507 for (i = 0; i < u_arraylist_length(list); i++)
509 void* ptrType = u_arraylist_get(list, i);
516 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
518 int index = CAGetAdapterIndex(connType);
522 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
526 if (g_adapterHandler[index].startDiscoveryServer != NULL)
528 g_adapterHandler[index].startDiscoveryServer();
532 OIC_LOG(DEBUG, TAG, "OUT");
536 void CATerminateAdapters()
538 OIC_LOG(DEBUG, TAG, "IN");
541 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
543 if (g_adapterHandler[index].terminate != NULL)
545 g_adapterHandler[index].terminate();
549 OIC_LOG(DEBUG, TAG, "OUT");
553 CAResult_t CAReadData()
555 OIC_LOG(DEBUG, TAG, "IN");
556 u_arraylist_t *list = CAGetSelectedNetworkList();
560 return CA_STATUS_FAILED;
564 for (i = 0; i < u_arraylist_length(list); i++)
566 void *ptrType = u_arraylist_get(list, i);
569 OIC_LOG(ERROR, TAG, "get list fail");
570 return CA_STATUS_FAILED;
573 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
575 int index = CAGetAdapterIndex(connType);
579 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
583 if (g_adapterHandler[index].readData != NULL)
585 g_adapterHandler[index].readData();
589 OIC_LOG(DEBUG, TAG, "OUT");