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 CALocalConnectivity_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
211 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
213 // #1. get information each adapter
215 CAResult_t res = CA_STATUS_FAILED;
216 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
218 if (g_adapterHandler[index].GetnetInfo != NULL)
220 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
222 OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
223 tempSize[index], res);
227 uint32_t resSize = 0;
228 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
231 if (tempInfo[index] == NULL || tempSize[index] <= 0)
237 resSize += tempSize[index];
240 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
244 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
248 return CA_STATUS_FAILED;
251 // #3. add data into result
254 CALocalConnectivity_t *resInfo = (CALocalConnectivity_t *) OICCalloc(
255 resSize, sizeof(CALocalConnectivity_t));
256 CA_MEMORY_ALLOC_CHECK(resInfo);
259 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
262 if (tempInfo[index] == NULL || tempSize[index] <= 0)
267 memcpy(resInfo + pos, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
269 pos += tempSize[index];
272 OICFree(tempInfo[index]);
273 tempInfo[index] = NULL;
287 OIC_LOG(DEBUG, TAG, "each network info save success!");
290 // memory error label.
293 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
296 OICFree(tempInfo[index]);
297 tempInfo[index] = NULL;
300 return CA_MEMORY_ALLOC_FAILED;
303 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
305 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
307 CAResult_t res = CA_STATUS_FAILED;
309 if (endpoint == NULL)
311 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
312 return CA_STATUS_INVALID_PARAM;
315 CATransportType_t type = endpoint->transportType;
317 int index = CAGetAdapterIndex(type);
321 OIC_LOG(ERROR, TAG, "unknown transport type!");
322 return CA_STATUS_INVALID_PARAM;
325 uint32_t sentDataLen = 0;
327 if (g_adapterHandler[index].sendData != NULL)
329 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
332 if (sentDataLen != -1)
339 CAResult_t CASendMulticastData(const void *data, uint32_t length)
341 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
343 CAResult_t res = CA_STATUS_FAILED;
344 u_arraylist_t *list = CAGetSelectedNetworkList();
348 OIC_LOG(DEBUG, TAG, "No selected network");
349 return CA_STATUS_FAILED;
353 for (i = 0; i < u_arraylist_length(list); i++)
355 void* ptrType = u_arraylist_get(list, i);
362 CATransportType_t connType = *(CATransportType_t *) ptrType;
364 int index = CAGetAdapterIndex(connType);
368 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
372 uint32_t sentDataLen = 0;
374 if (g_adapterHandler[index].sendDataToAll != NULL)
376 void *payload = (void *) OICMalloc(length);
379 OIC_LOG(ERROR, TAG, "Out of memory!");
380 return CA_MEMORY_ALLOC_FAILED;
382 memcpy(payload, data, length);
383 sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
387 if (sentDataLen == length)
393 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
400 CAResult_t CAStartListeningServerAdapters()
402 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
404 u_arraylist_t *list = CAGetSelectedNetworkList();
407 OIC_LOG(ERROR, TAG, "No selected network");
408 return CA_STATUS_FAILED;
412 for (i = 0; i < u_arraylist_length(list); i++)
414 void* ptrType = u_arraylist_get(list, i);
421 CATransportType_t connType = *(CATransportType_t *) ptrType;
423 int index = CAGetAdapterIndex(connType);
426 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
430 if (g_adapterHandler[index].startListenServer != NULL)
432 g_adapterHandler[index].startListenServer();
439 CAResult_t CAStartDiscoveryServerAdapters()
441 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
443 u_arraylist_t *list = CAGetSelectedNetworkList();
447 OIC_LOG(ERROR, TAG, "No selected network");
448 return CA_STATUS_FAILED;
452 for (i = 0; i < u_arraylist_length(list); i++)
454 void* ptrType = u_arraylist_get(list, i);
461 CATransportType_t connType = *(CATransportType_t *) ptrType;
463 int index = CAGetAdapterIndex(connType);
467 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
471 if (g_adapterHandler[index].startDiscoveryServer != NULL)
473 g_adapterHandler[index].startDiscoveryServer();
480 void CATerminateAdapters()
482 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
485 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
487 if (g_adapterHandler[index].terminate != NULL)
489 g_adapterHandler[index].terminate();