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 if(handler.startAdapter == NULL ||
97 handler.startListenServer == NULL ||
98 handler.stopListenServer == NULL ||
99 handler.startDiscoveryServer == NULL ||
100 handler.sendData == NULL ||
101 handler.sendDataToAll == NULL ||
102 handler.GetnetInfo == NULL ||
103 handler.readData == NULL ||
104 handler.stopAdapter == NULL ||
105 handler.terminate == NULL)
107 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
111 int index = CAGetAdapterIndex(cType);
114 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
118 g_adapterHandler[index] = handler;
120 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
124 CAResult_t CASetAdapterRAInfo(const CARAInfo_t *caraInfo)
126 return CASetRAInfo(caraInfo);
130 static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
131 const void *data, uint32_t dataLen)
133 if (g_networkPacketReceivedCallback != NULL)
135 g_networkPacketReceivedCallback(sep, data, dataLen);
139 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
143 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
145 // Call the callback.
146 if (g_networkChangeCallback != NULL)
148 g_networkChangeCallback(info, status);
152 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
153 const void *data, uint32_t dataLen,
156 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
158 // Call the callback.
159 if (g_errorHandleCallback != NULL)
161 g_errorHandleCallback(endpoint, data, dataLen, result);
165 void CAInitializeAdapters(ca_thread_pool_t handle)
167 OIC_LOG(DEBUG, TAG, "initialize adapters..");
169 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
171 // Initialize adapters and register callback.
173 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
174 CAAdapterErrorHandleCallback, handle);
175 #endif /* IP_ADAPTER */
178 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
179 CAAdapterErrorHandleCallback, handle);
180 #endif /* EDR_ADAPTER */
183 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
184 CAAdapterErrorHandleCallback, handle);
185 #endif /* LE_ADAPTER */
188 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
190 #endif /* RA_ADAPTER */
193 CAInitializeTCP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
194 CAAdapterErrorHandleCallback, handle);
195 #endif /* TCP_ADAPTER */
198 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
200 OIC_LOG(DEBUG, TAG, "Set Receiver handle callback");
202 g_networkPacketReceivedCallback = callback;
205 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
207 OIC_LOG(DEBUG, TAG, "Set network handle callback");
209 g_networkChangeCallback = callback;
212 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
214 OIC_LOG(DEBUG, TAG, "Set error handle callback");
215 g_errorHandleCallback = errorCallback;
218 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
220 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
222 int index = CAGetAdapterIndex(transportType);
225 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
226 return CA_STATUS_FAILED;
229 CAResult_t res = CA_STATUS_FAILED;
230 if (g_adapterHandler[index].startAdapter != NULL)
232 res = g_adapterHandler[index].startAdapter();
238 void CAStopAdapter(CATransportAdapter_t transportType)
240 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
242 int index = CAGetAdapterIndex(transportType);
245 OIC_LOG(ERROR, TAG, "unknown transport type!");
249 if (g_adapterHandler[index].stopAdapter != NULL)
251 g_adapterHandler[index].stopAdapter();
255 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
257 if (info == NULL || size == NULL)
259 return CA_STATUS_INVALID_PARAM;
262 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
263 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
265 CAResult_t res = CA_STATUS_FAILED;
267 for (size_t index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
269 if (g_adapterHandler[index].GetnetInfo != NULL)
271 // #1. get information for each adapter
272 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
276 if (res == CA_STATUS_OK)
278 resSize += tempSize[index];
283 "%zu adapter network info size is %" PRIu32 " res:%d",
290 OIC_LOG_V(DEBUG, TAG, "network info total size is %zu!", resSize);
294 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 (size_t index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
313 if (tempSize[index] == 0)
320 sizeof(*resInfo) * tempSize[index]);
322 resInfo += tempSize[index];
325 OICFree(tempInfo[index]);
326 tempInfo[index] = NULL;
329 OIC_LOG(DEBUG, TAG, "each network info save success!");
332 // memory error label.
335 for (size_t index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
337 OICFree(tempInfo[index]);
338 tempInfo[index] = NULL;
341 return CA_MEMORY_ALLOC_FAILED;
344 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
346 if (endpoint == NULL)
348 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
349 return CA_STATUS_INVALID_PARAM;
352 CATransportAdapter_t type = endpoint->adapter;
354 int index = CAGetAdapterIndex(type);
357 OIC_LOG(ERROR, TAG, "unknown transport type!");
358 return CA_STATUS_INVALID_PARAM;
361 int32_t sentDataLen = 0;
363 if (g_adapterHandler[index].sendData != NULL)
365 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
368 if (sentDataLen != (int)length)
370 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
372 //in case of single thread, no error handler. Report error immediately
373 return CA_SEND_FAILED;
380 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
382 u_arraylist_t *list = CAGetSelectedNetworkList();
385 OIC_LOG(DEBUG, TAG, "No selected network");
386 return CA_SEND_FAILED;
389 CATransportFlags_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS;
390 size_t selectedLength = u_arraylist_length(list);
391 for (size_t i = 0; i < selectedLength; i++)
393 void* ptrType = u_arraylist_get(list, i);
400 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
401 if ((connType & requestedAdapter) == 0)
406 int index = CAGetAdapterIndex(connType);
409 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
413 uint32_t sentDataLen = 0;
415 if (g_adapterHandler[index].sendDataToAll != NULL)
417 void *payload = (void *) OICMalloc(length);
420 OIC_LOG(ERROR, TAG, "Out of memory!");
421 return CA_MEMORY_ALLOC_FAILED;
423 memcpy(payload, data, length);
424 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
428 if (sentDataLen != length)
430 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
432 //in case of single thread, no error handler. Report error immediately
433 return CA_SEND_FAILED;
441 CAResult_t CAStartListeningServerAdapters()
443 CAResult_t result = CA_STATUS_FAILED;
445 u_arraylist_t *list = CAGetSelectedNetworkList();
448 OIC_LOG(ERROR, TAG, "No selected network");
452 size_t length = u_arraylist_length(list);
453 for (size_t i = 0; i < length; i++)
455 void* ptrType = u_arraylist_get(list, i);
462 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
464 int index = CAGetAdapterIndex(connType);
467 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
471 if (g_adapterHandler[index].startListenServer != NULL)
473 const CAResult_t tmp =
474 g_adapterHandler[index].startListenServer();
476 // Successful listen if at least one adapter started.
477 if (CA_STATUS_OK == tmp)
487 CAResult_t CAStopListeningServerAdapters()
489 u_arraylist_t *list = CAGetSelectedNetworkList();
492 OIC_LOG(ERROR, TAG, "No selected network");
493 return CA_STATUS_FAILED;
496 size_t length = u_arraylist_length(list);
497 for (size_t i = 0; i < length; i++)
499 void* ptrType = u_arraylist_get(list, i);
505 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
507 int index = CAGetAdapterIndex(connType);
510 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
514 if (g_adapterHandler[index].stopListenServer != NULL)
516 g_adapterHandler[index].stopListenServer();
523 CAResult_t CAStartDiscoveryServerAdapters()
525 CAResult_t result = CA_STATUS_FAILED;
527 u_arraylist_t *list = CAGetSelectedNetworkList();
531 OIC_LOG(ERROR, TAG, "No selected network");
535 size_t length = u_arraylist_length(list);
536 for (size_t i = 0; i < length; i++)
538 void* ptrType = u_arraylist_get(list, i);
545 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
547 int index = CAGetAdapterIndex(connType);
550 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
554 if (g_adapterHandler[index].startDiscoveryServer != NULL)
556 const CAResult_t tmp =
557 g_adapterHandler[index].startDiscoveryServer();
559 // Successful discovery if at least one adapter started.
560 if (CA_STATUS_OK == tmp)
570 void CATerminateAdapters()
572 for (size_t index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
574 if (g_adapterHandler[index].terminate != NULL)
576 g_adapterHandler[index].terminate();
582 CAResult_t CAReadData()
584 u_arraylist_t *list = CAGetSelectedNetworkList();
588 return CA_STATUS_FAILED;
592 for (i = 0; i < u_arraylist_length(list); i++)
594 void *ptrType = u_arraylist_get(list, i);
597 OIC_LOG(ERROR, TAG, "get list fail");
598 return CA_STATUS_FAILED;
601 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
603 int index = CAGetAdapterIndex(connType);
606 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
610 if (g_adapterHandler[index].readData != NULL)
612 g_adapterHandler[index].readData();