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"
37 #define TAG "CA_INTRFC_CNTRLR"
39 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
40 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
42 #define CA_TRANSPORT_TYPE_NUM 3
44 static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM] = {};
46 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
48 static CANetworkChangeCallback g_networkChangeCallback = NULL;
50 static CAErrorHandleCallback g_errorHandleCallback = NULL;
52 static int CAGetAdapterIndex(CATransportAdapter_t cType)
58 case CA_ADAPTER_GATT_BTLE:
60 case CA_ADAPTER_RFCOMM_BTEDR:
66 OIC_LOG(DEBUG, TAG, "CA_TRANSPORT_TYPE_NUM is not 3");
71 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
73 OIC_LOG(DEBUG, TAG, "IN");
75 if(handler.startAdapter == NULL ||
76 handler.startListenServer == NULL ||
77 handler.startDiscoveryServer == NULL ||
78 handler.sendData == NULL ||
79 handler.sendDataToAll == NULL ||
80 handler.GetnetInfo == NULL ||
81 handler.readData == NULL ||
82 handler.stopAdapter == NULL ||
83 handler.terminate == NULL)
85 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
89 int index = CAGetAdapterIndex(cType);
93 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
97 g_adapterHandler[index] = handler;
99 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
100 OIC_LOG(DEBUG, TAG, "OUT");
103 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
105 OIC_LOG(DEBUG, TAG, "IN");
107 // Call the callback.
108 if (g_networkPacketReceivedCallback != NULL)
110 g_networkPacketReceivedCallback(endpoint, data, dataLen);
116 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
119 OIC_LOG(DEBUG, TAG, "OUT");
122 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
124 OIC_LOG(DEBUG, TAG, "IN");
126 // Call the callback.
127 if (g_networkChangeCallback != NULL)
129 g_networkChangeCallback(info, status);
132 OIC_LOG(DEBUG, TAG, "OUT");
135 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
136 const void *data, uint32_t dataLen,
139 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
141 // Call the callback.
142 if (g_errorHandleCallback != NULL)
144 g_errorHandleCallback(endpoint, data, dataLen, result);
148 void CAInitializeAdapters(ca_thread_pool_t handle)
150 OIC_LOG(DEBUG, TAG, "initialize adapters..");
152 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
154 // Initialize adapters and register callback.
156 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
157 CAAdapterErrorHandleCallback, handle);
158 #endif /* IP_ADAPTER */
161 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
162 CAAdapterErrorHandleCallback, handle);
163 #endif /* EDR_ADAPTER */
166 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
167 CAAdapterErrorHandleCallback, handle);
168 #endif /* LE_ADAPTER */
172 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
174 OIC_LOG(DEBUG, TAG, "IN");
176 g_networkPacketReceivedCallback = callback;
178 OIC_LOG(DEBUG, TAG, "OUT");
181 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
183 OIC_LOG(DEBUG, TAG, "IN");
185 g_networkChangeCallback = callback;
187 OIC_LOG(DEBUG, TAG, "OUT");
190 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
192 OIC_LOG(DEBUG, TAG, "Set error handle callback");
193 g_errorHandleCallback = errorCallback;
196 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
198 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
200 int index = CAGetAdapterIndex(transportType);
204 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
205 return CA_STATUS_FAILED;
208 if (g_adapterHandler[index].startAdapter != NULL)
210 g_adapterHandler[index].startAdapter();
216 void CAStopAdapter(CATransportAdapter_t transportType)
218 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
220 int index = CAGetAdapterIndex(transportType);
224 OIC_LOG(ERROR, TAG, "unknown transport type!");
228 if (g_adapterHandler[index].stopAdapter != NULL)
230 g_adapterHandler[index].stopAdapter();
234 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
236 if (info == NULL || size == NULL)
238 return CA_STATUS_INVALID_PARAM;
241 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
242 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
244 CAResult_t res = CA_STATUS_FAILED;
245 uint32_t resSize = 0;
246 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
248 if (g_adapterHandler[index].GetnetInfo != NULL)
250 // #1. get information for each adapter
251 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
255 if (res == CA_STATUS_OK)
257 resSize += tempSize[index];
262 "%d adapter network info size is %" PRIu32 " res:%d",
269 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
273 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
277 return CA_STATUS_FAILED;
280 // #3. add data into result
282 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
283 CA_MEMORY_ALLOC_CHECK(resInfo);
289 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
292 if (tempSize[index] == 0)
299 sizeof(*resInfo) * tempSize[index]);
301 resInfo += tempSize[index];
304 OICFree(tempInfo[index]);
305 tempInfo[index] = NULL;
308 OIC_LOG(DEBUG, TAG, "each network info save success!");
311 // memory error label.
314 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
317 OICFree(tempInfo[index]);
318 tempInfo[index] = NULL;
321 return CA_MEMORY_ALLOC_FAILED;
324 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
326 OIC_LOG(DEBUG, TAG, "IN");
328 CAResult_t res = CA_STATUS_FAILED;
330 if (endpoint == NULL)
332 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
333 return CA_STATUS_INVALID_PARAM;
336 CATransportAdapter_t type = endpoint->adapter;
338 int index = CAGetAdapterIndex(type);
342 OIC_LOG(ERROR, TAG, "unknown transport type!");
343 return CA_STATUS_INVALID_PARAM;
346 uint32_t sentDataLen = 0;
348 if (g_adapterHandler[index].sendData != NULL)
350 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
353 if (sentDataLen != -1)
358 OIC_LOG(DEBUG, TAG, "OUT");
362 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
364 OIC_LOG(DEBUG, TAG, "IN");
366 CAResult_t res = CA_SEND_FAILED;
367 u_arraylist_t *list = CAGetSelectedNetworkList();
371 OIC_LOG(DEBUG, TAG, "No selected network");
372 return CA_SEND_FAILED;
376 for (i = 0; i < u_arraylist_length(list); i++)
378 void* ptrType = u_arraylist_get(list, i);
385 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
387 int index = CAGetAdapterIndex(connType);
391 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
395 uint32_t sentDataLen = 0;
397 if (g_adapterHandler[index].sendDataToAll != NULL)
399 void *payload = (void *) OICMalloc(length);
402 OIC_LOG(ERROR, TAG, "Out of memory!");
403 return CA_MEMORY_ALLOC_FAILED;
405 memcpy(payload, data, length);
406 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
410 if (sentDataLen == length)
416 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
420 OIC_LOG(DEBUG, TAG, "OUT");
425 CAResult_t CAStartListeningServerAdapters()
427 OIC_LOG(DEBUG, TAG, "IN");
429 u_arraylist_t *list = CAGetSelectedNetworkList();
432 OIC_LOG(ERROR, TAG, "No selected network");
433 return CA_STATUS_FAILED;
437 for (i = 0; i < u_arraylist_length(list); i++)
439 void* ptrType = u_arraylist_get(list, i);
446 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
448 int index = CAGetAdapterIndex(connType);
451 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
455 if (g_adapterHandler[index].startListenServer != NULL)
457 g_adapterHandler[index].startListenServer();
461 OIC_LOG(DEBUG, TAG, "OUT");
465 CAResult_t CAStartDiscoveryServerAdapters()
467 OIC_LOG(DEBUG, TAG, "IN");
469 u_arraylist_t *list = CAGetSelectedNetworkList();
473 OIC_LOG(ERROR, TAG, "No selected network");
474 return CA_STATUS_FAILED;
478 for (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);
493 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
497 if (g_adapterHandler[index].startDiscoveryServer != NULL)
499 g_adapterHandler[index].startDiscoveryServer();
503 OIC_LOG(DEBUG, TAG, "OUT");
507 void CATerminateAdapters()
509 OIC_LOG(DEBUG, TAG, "IN");
512 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
514 if (g_adapterHandler[index].terminate != NULL)
516 g_adapterHandler[index].terminate();
520 OIC_LOG(DEBUG, TAG, "OUT");
524 CAResult_t CAReadData()
526 OIC_LOG(DEBUG, TAG, "IN");
527 u_arraylist_t *list = CAGetSelectedNetworkList();
531 return CA_STATUS_FAILED;
535 for (i = 0; i < u_arraylist_length(list); i++)
537 void *ptrType = u_arraylist_get(list, i);
540 OIC_LOG(ERROR, TAG, "get list fail");
541 return CA_STATUS_FAILED;
544 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
546 int index = CAGetAdapterIndex(connType);
550 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
554 if (g_adapterHandler[index].readData != NULL)
556 g_adapterHandler[index].readData();
560 OIC_LOG(DEBUG, TAG, "OUT");