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, handle);
151 #endif /* IP_ADAPTER */
154 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
155 #endif /* EDR_ADAPTER */
158 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
159 #endif /* LE_ADAPTER */
163 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
165 OIC_LOG(DEBUG, TAG, "Set packet received callback");
167 g_networkPacketReceivedCallback = callback;
170 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
172 OIC_LOG(DEBUG, TAG, "Set network change callback");
174 g_networkChangeCallback = callback;
177 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
179 OIC_LOG(DEBUG, TAG, "Set error handle callback");
180 g_errorHandleCallback = errorCallback;
183 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
185 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
187 int index = CAGetAdapterIndex(transportType);
191 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
192 return CA_STATUS_FAILED;
195 if (g_adapterHandler[index].startAdapter != NULL)
197 g_adapterHandler[index].startAdapter();
203 void CAStopAdapter(CATransportAdapter_t transportType)
205 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
207 int index = CAGetAdapterIndex(transportType);
211 OIC_LOG(ERROR, TAG, "unknown transport type!");
215 if (g_adapterHandler[index].stopAdapter != NULL)
217 g_adapterHandler[index].stopAdapter();
221 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
223 if (info == NULL || size == NULL)
225 return CA_STATUS_INVALID_PARAM;
228 CAEndpoint_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
229 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
231 CAResult_t res = CA_STATUS_FAILED;
232 uint32_t resSize = 0;
233 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
235 if (g_adapterHandler[index].GetnetInfo != NULL)
237 // #1. get information for each adapter
238 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
242 if (res == CA_STATUS_OK)
244 resSize += tempSize[index];
249 "%d adapter network info size is %" PRIu32 " res:%d",
256 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
260 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
264 return CA_STATUS_FAILED;
267 // #3. add data into result
269 CAEndpoint_t *resInfo = (CAEndpoint_t *) OICCalloc(resSize, sizeof (*resInfo));
270 CA_MEMORY_ALLOC_CHECK(resInfo);
276 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
279 if (tempSize[index] == 0)
286 sizeof(*resInfo) * tempSize[index]);
288 resInfo += tempSize[index];
291 OICFree(tempInfo[index]);
292 tempInfo[index] = NULL;
295 OIC_LOG(DEBUG, TAG, "each network info save success!");
298 // memory error label.
301 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
304 OICFree(tempInfo[index]);
305 tempInfo[index] = NULL;
308 return CA_MEMORY_ALLOC_FAILED;
311 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
313 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
315 CAResult_t res = CA_STATUS_FAILED;
317 if (endpoint == NULL)
319 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
320 return CA_STATUS_INVALID_PARAM;
323 CATransportAdapter_t type = endpoint->adapter;
325 int index = CAGetAdapterIndex(type);
329 OIC_LOG(ERROR, TAG, "unknown transport type!");
330 return CA_STATUS_INVALID_PARAM;
333 uint32_t sentDataLen = 0;
335 if (g_adapterHandler[index].sendData != NULL)
337 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
340 if (sentDataLen != -1)
347 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
349 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
351 CAResult_t res = CA_SEND_FAILED;
352 u_arraylist_t *list = CAGetSelectedNetworkList();
356 OIC_LOG(DEBUG, TAG, "No selected network");
357 return CA_SEND_FAILED;
361 for (i = 0; i < u_arraylist_length(list); i++)
363 void* ptrType = u_arraylist_get(list, i);
370 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
372 int index = CAGetAdapterIndex(connType);
376 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
380 uint32_t sentDataLen = 0;
382 if (g_adapterHandler[index].sendDataToAll != NULL)
384 void *payload = (void *) OICMalloc(length);
387 OIC_LOG(ERROR, TAG, "Out of memory!");
388 return CA_MEMORY_ALLOC_FAILED;
390 memcpy(payload, data, length);
391 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length);
395 if (sentDataLen == length)
401 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
408 CAResult_t CAStartListeningServerAdapters()
410 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
412 u_arraylist_t *list = CAGetSelectedNetworkList();
415 OIC_LOG(ERROR, TAG, "No selected network");
416 return CA_STATUS_FAILED;
420 for (i = 0; i < u_arraylist_length(list); i++)
422 void* ptrType = u_arraylist_get(list, i);
429 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
431 int index = CAGetAdapterIndex(connType);
434 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
438 if (g_adapterHandler[index].startListenServer != NULL)
440 g_adapterHandler[index].startListenServer();
447 CAResult_t CAStartDiscoveryServerAdapters()
449 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
451 u_arraylist_t *list = CAGetSelectedNetworkList();
455 OIC_LOG(ERROR, TAG, "No selected network");
456 return CA_STATUS_FAILED;
460 for (i = 0; i < u_arraylist_length(list); i++)
462 void* ptrType = u_arraylist_get(list, i);
469 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
471 int index = CAGetAdapterIndex(connType);
475 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
479 if (g_adapterHandler[index].startDiscoveryServer != NULL)
481 g_adapterHandler[index].startDiscoveryServer();
488 void CATerminateAdapters()
490 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
493 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
495 if (g_adapterHandler[index].terminate != NULL)
497 g_adapterHandler[index].terminate();