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 ******************************************************************/
26 #include "cainterfacecontroller.h"
27 #include "caipadapter.h"
28 #include "caedradapter.h"
29 #include "caleadapter.h"
30 #include "canetworkconfigurator.h"
31 #include "caremotehandler.h"
32 #include "oic_malloc.h"
34 #include "cathreadpool.h"
38 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
39 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
41 #define CA_TRANSPORT_TYPE_NUM 3
43 static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM] = {};
45 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
47 static CANetworkChangeCallback g_networkChangeCallback = NULL;
49 static CAErrorHandleCallback g_errorHandleCallback = NULL;
51 static int CAGetAdapterIndex(CATransportAdapter_t cType)
57 case CA_ADAPTER_GATT_BTLE:
59 case CA_ADAPTER_RFCOMM_BTEDR:
65 OIC_LOG(DEBUG, TAG, "CA_TRANSPORT_TYPE_NUM is not 3");
70 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
72 OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
74 if(handler.startAdapter == NULL ||
75 handler.startListenServer == NULL ||
76 handler.startDiscoveryServer == NULL ||
77 handler.sendData == NULL ||
78 handler.sendDataToAll == NULL ||
79 handler.GetnetInfo == NULL ||
80 handler.readData == NULL ||
81 handler.stopAdapter == NULL ||
82 handler.terminate == NULL)
84 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
88 int index = CAGetAdapterIndex(cType);
92 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
96 g_adapterHandler[index] = handler;
98 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
101 static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
103 OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
105 // Call the callback.
106 if (g_networkPacketReceivedCallback != NULL)
108 g_networkPacketReceivedCallback(endpoint, data, dataLen);
114 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
118 static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
120 OIC_LOG(DEBUG, TAG, "Network Changed callback");
122 // Call the callback.
123 if (g_networkChangeCallback != NULL)
125 g_networkChangeCallback(info, status);
129 static void CAAdapterErrorHandleCallback(const CAEndpoint_t *endpoint,
130 const void *data, uint32_t dataLen,
133 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
135 // Call the callback.
136 if (g_errorHandleCallback != NULL)
138 g_errorHandleCallback(endpoint, data, dataLen, result);
142 void CAInitializeAdapters(ca_thread_pool_t handle)
144 OIC_LOG(DEBUG, TAG, "initialize adapters..");
146 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
148 // Initialize adapters and register callback.
150 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
151 CAAdapterErrorHandleCallback, handle);
152 #endif /* IP_ADAPTER */
155 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
156 CAAdapterErrorHandleCallback, handle);
157 #endif /* EDR_ADAPTER */
160 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
161 CAAdapterErrorHandleCallback, handle);
162 #endif /* LE_ADAPTER */
166 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
168 OIC_LOG(DEBUG, TAG, "Set packet received callback");
170 g_networkPacketReceivedCallback = callback;
173 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
175 OIC_LOG(DEBUG, TAG, "Set network change callback");
177 g_networkChangeCallback = callback;
180 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
182 OIC_LOG(DEBUG, TAG, "Set error handle callback");
183 g_errorHandleCallback = errorCallback;
186 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
188 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
190 int index = CAGetAdapterIndex(transportType);
194 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
195 return CA_STATUS_FAILED;
198 if (g_adapterHandler[index].startAdapter != NULL)
200 g_adapterHandler[index].startAdapter();
206 void CAStopAdapter(CATransportAdapter_t transportType)
208 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
210 int index = CAGetAdapterIndex(transportType);
214 OIC_LOG(ERROR, TAG, "unknown transport type!");
218 if (g_adapterHandler[index].stopAdapter != NULL)
220 g_adapterHandler[index].stopAdapter();
224 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
226 if (info == NULL || size == NULL)
228 return CA_STATUS_INVALID_PARAM;
231 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
232 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
234 CAResult_t res = CA_STATUS_FAILED;
235 uint32_t resSize = 0;
236 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
238 if (g_adapterHandler[index].GetnetInfo != NULL)
240 // #1. get information for each adapter
241 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
245 if (res == CA_STATUS_OK)
247 resSize += tempSize[index];
252 "%d adapter network info size is %" PRIu32 " res:%d",
259 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
263 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
267 return CA_STATUS_FAILED;
270 // #3. add data into result
272 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
273 CA_MEMORY_ALLOC_CHECK(resInfo);
279 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
282 if (tempSize[index] == 0)
289 sizeof(*resInfo) * tempSize[index]);
291 resInfo += tempSize[index];
294 OICFree(tempInfo[index]);
295 tempInfo[index] = NULL;
298 OIC_LOG(DEBUG, TAG, "each network info save success!");
301 // memory error label.
304 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
307 OICFree(tempInfo[index]);
308 tempInfo[index] = NULL;
311 return CA_MEMORY_ALLOC_FAILED;
314 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
316 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
318 CAResult_t res = CA_STATUS_FAILED;
320 if (endpoint == NULL)
322 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
323 return CA_STATUS_INVALID_PARAM;
326 CATransportAdapter_t type = endpoint->adapter;
328 int index = CAGetAdapterIndex(type);
332 OIC_LOG(ERROR, TAG, "unknown transport type!");
333 return CA_STATUS_INVALID_PARAM;
336 uint32_t sentDataLen = 0;
338 if (g_adapterHandler[index].sendData != NULL)
340 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
343 if (sentDataLen != -1)
350 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
352 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
354 CAResult_t res = CA_SEND_FAILED;
355 u_arraylist_t *list = CAGetSelectedNetworkList();
359 OIC_LOG(DEBUG, TAG, "No selected network");
360 return CA_SEND_FAILED;
364 for (i = 0; i < u_arraylist_length(list); i++)
366 void* ptrType = u_arraylist_get(list, i);
373 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
375 int index = CAGetAdapterIndex(connType);
379 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
383 uint32_t sentDataLen = 0;
385 if (g_adapterHandler[index].sendDataToAll != NULL)
387 void *payload = (void *) OICMalloc(length);
390 OIC_LOG(ERROR, TAG, "Out of memory!");
391 return CA_MEMORY_ALLOC_FAILED;
393 memcpy(payload, data, length);
394 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
398 if (sentDataLen == length)
404 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
411 CAResult_t CAStartListeningServerAdapters()
413 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
415 u_arraylist_t *list = CAGetSelectedNetworkList();
418 OIC_LOG(ERROR, TAG, "No selected network");
419 return CA_STATUS_FAILED;
423 for (i = 0; i < u_arraylist_length(list); i++)
425 void* ptrType = u_arraylist_get(list, i);
432 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
434 int index = CAGetAdapterIndex(connType);
437 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
441 if (g_adapterHandler[index].startListenServer != NULL)
443 g_adapterHandler[index].startListenServer();
450 CAResult_t CAStartDiscoveryServerAdapters()
452 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
454 u_arraylist_t *list = CAGetSelectedNetworkList();
458 OIC_LOG(ERROR, TAG, "No selected network");
459 return CA_STATUS_FAILED;
463 for (i = 0; i < u_arraylist_length(list); i++)
465 void* ptrType = u_arraylist_get(list, i);
472 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
474 int index = CAGetAdapterIndex(connType);
478 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
482 if (g_adapterHandler[index].startDiscoveryServer != NULL)
484 g_adapterHandler[index].startDiscoveryServer();
491 void CATerminateAdapters()
493 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
496 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
498 if (g_adapterHandler[index].terminate != NULL)
500 g_adapterHandler[index].terminate();