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 4
43 static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM];
45 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
47 static CANetworkChangeCallback g_networkChangeCallback = NULL;
49 static int CAGetAdapterIndex(CATransportType_t cType)
63 OIC_LOG(DEBUG, TAG, "CA_TRANSPORT_TYPE_NUM is not 4");
68 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportType_t cType)
70 OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
72 if(handler.startAdapter == NULL ||
73 handler.startListenServer == NULL ||
74 handler.startDiscoveryServer == NULL ||
75 handler.sendData == NULL ||
76 handler.sendDataToAll == NULL ||
77 handler.GetnetInfo == NULL ||
78 handler.readData == NULL ||
79 handler.stopAdapter == NULL ||
80 handler.terminate == NULL)
82 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
86 int index = CAGetAdapterIndex(cType);
90 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
94 memcpy(&g_adapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
96 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
99 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
102 OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
104 // Call the callback.
105 if (g_networkPacketReceivedCallback != NULL)
107 g_networkPacketReceivedCallback(endpoint, data, dataLen);
116 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
120 static void CANetworkChangedCallback(CALocalConnectivity_t *info,
121 CANetworkStatus_t status)
123 OIC_LOG(DEBUG, TAG, "Network Changed callback");
125 // Call the callback.
126 if (g_networkChangeCallback != NULL)
128 g_networkChangeCallback(info, status);
132 void CAInitializeAdapters(ca_thread_pool_t handle)
134 OIC_LOG(DEBUG, TAG, "initialize adapters..");
136 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
138 // Initialize adapters and register callback.
140 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
142 #endif /* IP_ADAPTER */
145 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
147 #endif /* EDR_ADAPTER */
150 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
152 #endif /* LE_ADAPTER */
156 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
158 OIC_LOG(DEBUG, TAG, "Set packet received callback");
160 g_networkPacketReceivedCallback = callback;
163 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
165 OIC_LOG(DEBUG, TAG, "Set network change callback");
167 g_networkChangeCallback = callback;
170 CAResult_t CAStartAdapter(CATransportType_t transportType)
172 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
174 int index = CAGetAdapterIndex(transportType);
178 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
179 return CA_STATUS_FAILED;
182 if (g_adapterHandler[index].startAdapter != NULL)
184 g_adapterHandler[index].startAdapter();
190 void CAStopAdapter(CATransportType_t transportType)
192 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
194 int index = CAGetAdapterIndex(transportType);
198 OIC_LOG(ERROR, TAG, "unknown transport type!");
202 if (g_adapterHandler[index].stopAdapter != NULL)
204 g_adapterHandler[index].stopAdapter();
208 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
210 if (info == NULL || size == NULL)
212 return CA_STATUS_INVALID_PARAM;
215 CALocalConnectivity_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
216 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
218 CAResult_t res = CA_STATUS_FAILED;
219 uint32_t resSize = 0;
220 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
222 if (g_adapterHandler[index].GetnetInfo != NULL)
224 // #1. get information for each adapter
225 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
229 if (res == CA_STATUS_OK)
231 resSize += tempSize[index];
236 "%d adapter network info size is %" PRIu32 " res:%d",
243 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
247 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
251 return CA_STATUS_FAILED;
254 // #3. add data into result
257 CALocalConnectivity_t * resInfo = OICCalloc(resSize, sizeof(*resInfo));
258 CA_MEMORY_ALLOC_CHECK(resInfo);
264 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
267 if (tempSize[index] == 0)
274 sizeof(*resInfo) * tempSize[index]);
276 resInfo += tempSize[index];
279 OICFree(tempInfo[index]);
280 tempInfo[index] = NULL;
283 OIC_LOG(DEBUG, TAG, "each network info save success!");
286 // memory error label.
289 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
292 OICFree(tempInfo[index]);
293 tempInfo[index] = NULL;
296 return CA_MEMORY_ALLOC_FAILED;
299 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
301 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
303 CAResult_t res = CA_STATUS_FAILED;
305 if (endpoint == NULL)
307 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
308 return CA_STATUS_INVALID_PARAM;
311 CATransportType_t type = endpoint->transportType;
313 int index = CAGetAdapterIndex(type);
317 OIC_LOG(ERROR, TAG, "unknown transport type!");
318 return CA_STATUS_INVALID_PARAM;
321 uint32_t sentDataLen = 0;
323 if (g_adapterHandler[index].sendData != NULL)
325 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
328 if (sentDataLen != -1)
335 CAResult_t CASendMulticastData(const void *data, uint32_t length)
337 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
339 CAResult_t res = CA_STATUS_FAILED;
340 u_arraylist_t *list = CAGetSelectedNetworkList();
344 OIC_LOG(DEBUG, TAG, "No selected network");
345 return CA_STATUS_FAILED;
349 for (i = 0; i < u_arraylist_length(list); i++)
351 void* ptrType = u_arraylist_get(list, i);
358 CATransportType_t connType = *(CATransportType_t *) ptrType;
360 int index = CAGetAdapterIndex(connType);
364 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
368 uint32_t sentDataLen = 0;
370 if (g_adapterHandler[index].sendDataToAll != NULL)
372 void *payload = (void *) OICMalloc(length);
375 OIC_LOG(ERROR, TAG, "Out of memory!");
376 return CA_MEMORY_ALLOC_FAILED;
378 memcpy(payload, data, length);
379 sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
383 if (sentDataLen == length)
389 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
396 CAResult_t CAStartListeningServerAdapters()
398 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
400 u_arraylist_t *list = CAGetSelectedNetworkList();
403 OIC_LOG(ERROR, TAG, "No selected network");
404 return CA_STATUS_FAILED;
408 for (i = 0; i < u_arraylist_length(list); i++)
410 void* ptrType = u_arraylist_get(list, i);
417 CATransportType_t connType = *(CATransportType_t *) ptrType;
419 int index = CAGetAdapterIndex(connType);
422 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
426 if (g_adapterHandler[index].startListenServer != NULL)
428 g_adapterHandler[index].startListenServer();
435 CAResult_t CAStartDiscoveryServerAdapters()
437 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
439 u_arraylist_t *list = CAGetSelectedNetworkList();
443 OIC_LOG(ERROR, TAG, "No selected network");
444 return CA_STATUS_FAILED;
448 for (i = 0; i < u_arraylist_length(list); i++)
450 void* ptrType = u_arraylist_get(list, i);
457 CATransportType_t connType = *(CATransportType_t *) ptrType;
459 int index = CAGetAdapterIndex(connType);
463 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
467 if (g_adapterHandler[index].startDiscoveryServer != NULL)
469 g_adapterHandler[index].startDiscoveryServer();
476 void CATerminateAdapters()
478 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
481 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
483 if (g_adapterHandler[index].terminate != NULL)
485 g_adapterHandler[index].terminate();