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, const 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);
137 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
140 OIC_LOG(DEBUG, TAG, "OUT");
143 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
145 OIC_LOG(DEBUG, TAG, "IN");
147 // Call the callback.
148 if (g_networkChangeCallback != NULL)
150 g_networkChangeCallback(info, status);
153 OIC_LOG(DEBUG, TAG, "OUT");
156 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
157 const void *data, uint32_t dataLen,
160 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
162 // Call the callback.
163 if (g_errorHandleCallback != NULL)
165 g_errorHandleCallback(endpoint, data, dataLen, result);
169 void CAInitializeAdapters(ca_thread_pool_t handle)
171 OIC_LOG(DEBUG, TAG, "initialize adapters..");
173 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
175 // Initialize adapters and register callback.
177 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
178 CAAdapterErrorHandleCallback, handle);
179 #endif /* IP_ADAPTER */
182 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
183 CAAdapterErrorHandleCallback, handle);
184 #endif /* EDR_ADAPTER */
187 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
188 CAAdapterErrorHandleCallback, handle);
189 #endif /* LE_ADAPTER */
192 CAInitializeRA(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
194 #endif /* RA_ADAPTER */
199 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
201 OIC_LOG(DEBUG, TAG, "IN");
203 g_networkPacketReceivedCallback = callback;
205 OIC_LOG(DEBUG, TAG, "OUT");
208 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
210 OIC_LOG(DEBUG, TAG, "IN");
212 g_networkChangeCallback = callback;
214 OIC_LOG(DEBUG, TAG, "OUT");
217 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
219 OIC_LOG(DEBUG, TAG, "Set error handle callback");
220 g_errorHandleCallback = errorCallback;
223 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
225 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
227 int index = CAGetAdapterIndex(transportType);
231 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
232 return CA_STATUS_FAILED;
235 if (g_adapterHandler[index].startAdapter != NULL)
237 g_adapterHandler[index].startAdapter();
243 void CAStopAdapter(CATransportAdapter_t transportType)
245 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
247 int index = CAGetAdapterIndex(transportType);
251 OIC_LOG(ERROR, TAG, "unknown transport type!");
255 if (g_adapterHandler[index].stopAdapter != NULL)
257 g_adapterHandler[index].stopAdapter();
261 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
263 if (info == NULL || size == NULL)
265 return CA_STATUS_INVALID_PARAM;
268 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
269 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
271 CAResult_t res = CA_STATUS_FAILED;
272 uint32_t resSize = 0;
273 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
275 if (g_adapterHandler[index].GetnetInfo != NULL)
277 // #1. get information for each adapter
278 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
282 if (res == CA_STATUS_OK)
284 resSize += tempSize[index];
289 "%d adapter network info size is %" PRIu32 " res:%d",
296 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
300 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
304 return CA_STATUS_FAILED;
307 // #3. add data into result
309 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
310 CA_MEMORY_ALLOC_CHECK(resInfo);
316 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
319 if (tempSize[index] == 0)
326 sizeof(*resInfo) * tempSize[index]);
328 resInfo += tempSize[index];
331 OICFree(tempInfo[index]);
332 tempInfo[index] = NULL;
335 OIC_LOG(DEBUG, TAG, "each network info save success!");
338 // memory error label.
341 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
344 OICFree(tempInfo[index]);
345 tempInfo[index] = NULL;
348 return CA_MEMORY_ALLOC_FAILED;
351 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
353 OIC_LOG(DEBUG, TAG, "IN");
355 if (endpoint == NULL)
357 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
358 return CA_STATUS_INVALID_PARAM;
361 CATransportAdapter_t type = endpoint->adapter;
363 int index = CAGetAdapterIndex(type);
367 OIC_LOG(ERROR, TAG, "unknown transport type!");
368 return CA_STATUS_INVALID_PARAM;
371 uint32_t sentDataLen = 0;
373 if (g_adapterHandler[index].sendData != NULL)
375 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
378 if (sentDataLen != length)
380 OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter");
382 //in case of single thread, no error handler. Report error immediately
383 return CA_SEND_FAILED;
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 u_arraylist_t *list = CAGetSelectedNetworkList();
399 OIC_LOG(DEBUG, TAG, "No selected network");
400 return CA_SEND_FAILED;
404 for (i = 0; i < u_arraylist_length(list); i++)
406 void* ptrType = u_arraylist_get(list, i);
413 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
415 int index = CAGetAdapterIndex(connType);
419 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
423 uint32_t sentDataLen = 0;
425 if (g_adapterHandler[index].sendDataToAll != NULL)
427 void *payload = (void *) OICMalloc(length);
430 OIC_LOG(ERROR, TAG, "Out of memory!");
431 return CA_MEMORY_ALLOC_FAILED;
433 memcpy(payload, data, length);
434 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
438 if (sentDataLen != length)
440 OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter");
442 //in case of single thread, no error handler. Report error immediately
443 return CA_SEND_FAILED;
448 OIC_LOG(DEBUG, TAG, "OUT");
453 CAResult_t CAStartListeningServerAdapters()
455 OIC_LOG(DEBUG, TAG, "IN");
457 u_arraylist_t *list = CAGetSelectedNetworkList();
460 OIC_LOG(ERROR, TAG, "No selected network");
461 return CA_STATUS_FAILED;
465 for (i = 0; i < u_arraylist_length(list); i++)
467 void* ptrType = u_arraylist_get(list, i);
474 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
476 int index = CAGetAdapterIndex(connType);
479 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
483 if (g_adapterHandler[index].startListenServer != NULL)
485 g_adapterHandler[index].startListenServer();
489 OIC_LOG(DEBUG, TAG, "OUT");
493 CAResult_t CAStartDiscoveryServerAdapters()
495 OIC_LOG(DEBUG, TAG, "IN");
497 u_arraylist_t *list = CAGetSelectedNetworkList();
501 OIC_LOG(ERROR, TAG, "No selected network");
502 return CA_STATUS_FAILED;
506 for (i = 0; i < u_arraylist_length(list); i++)
508 void* ptrType = u_arraylist_get(list, i);
515 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
517 int index = CAGetAdapterIndex(connType);
521 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
525 if (g_adapterHandler[index].startDiscoveryServer != NULL)
527 g_adapterHandler[index].startDiscoveryServer();
531 OIC_LOG(DEBUG, TAG, "OUT");
535 void CATerminateAdapters()
537 OIC_LOG(DEBUG, TAG, "IN");
540 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
542 if (g_adapterHandler[index].terminate != NULL)
544 g_adapterHandler[index].terminate();
548 OIC_LOG(DEBUG, TAG, "OUT");
552 CAResult_t CAReadData()
554 OIC_LOG(DEBUG, TAG, "IN");
555 u_arraylist_t *list = CAGetSelectedNetworkList();
559 return CA_STATUS_FAILED;
563 for (i = 0; i < u_arraylist_length(list); i++)
565 void *ptrType = u_arraylist_get(list, i);
568 OIC_LOG(ERROR, TAG, "get list fail");
569 return CA_STATUS_FAILED;
572 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
574 int index = CAGetAdapterIndex(connType);
578 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
582 if (g_adapterHandler[index].readData != NULL)
584 g_adapterHandler[index].readData();
588 OIC_LOG(DEBUG, TAG, "OUT");