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 CAErrorHandleCallback g_errorHandleCallback = NULL;
51 static int CAGetAdapterIndex(CATransportType_t cType)
65 OIC_LOG(DEBUG, TAG, "CA_TRANSPORT_TYPE_NUM is not 4");
70 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportType_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(CARemoteEndpoint_t *endpoint, void *data,
104 OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
106 // Call the callback.
107 if (g_networkPacketReceivedCallback != NULL)
109 g_networkPacketReceivedCallback(endpoint, data, dataLen);
118 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
122 static void CANetworkChangedCallback(CALocalConnectivity_t *info,
123 CANetworkStatus_t status)
125 OIC_LOG(DEBUG, TAG, "Network Changed callback");
127 // Call the callback.
128 if (g_networkChangeCallback != NULL)
130 g_networkChangeCallback(info, status);
134 static void CAAdapterErrorHandleCallback(const CARemoteEndpoint_t *endpoint, const void *data,
135 uint32_t dataLen, CAResult_t result)
137 OIC_LOG(DEBUG, TAG, "received error from adapter in interfacecontroller");
139 // Call the callback.
140 if (g_errorHandleCallback != NULL)
142 g_errorHandleCallback(endpoint, data, dataLen, result);
146 void CAInitializeAdapters(ca_thread_pool_t handle)
148 OIC_LOG(DEBUG, TAG, "initialize adapters..");
150 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
152 // Initialize adapters and register callback.
154 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
156 #endif /* IP_ADAPTER */
159 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
161 #endif /* EDR_ADAPTER */
164 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
166 #endif /* LE_ADAPTER */
170 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
172 OIC_LOG(DEBUG, TAG, "Set packet received callback");
174 g_networkPacketReceivedCallback = callback;
177 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
179 OIC_LOG(DEBUG, TAG, "Set network change callback");
181 g_networkChangeCallback = callback;
184 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
186 OIC_LOG(DEBUG, TAG, "Set error handle callback");
187 g_errorHandleCallback = errorCallback;
190 CAResult_t CAStartAdapter(CATransportType_t transportType)
192 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
194 int index = CAGetAdapterIndex(transportType);
198 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
199 return CA_STATUS_FAILED;
202 if (g_adapterHandler[index].startAdapter != NULL)
204 g_adapterHandler[index].startAdapter();
210 void CAStopAdapter(CATransportType_t transportType)
212 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
214 int index = CAGetAdapterIndex(transportType);
218 OIC_LOG(ERROR, TAG, "unknown transport type!");
222 if (g_adapterHandler[index].stopAdapter != NULL)
224 g_adapterHandler[index].stopAdapter();
228 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
230 if (info == NULL || size == NULL)
232 return CA_STATUS_INVALID_PARAM;
235 CALocalConnectivity_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
236 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
238 CAResult_t res = CA_STATUS_FAILED;
239 uint32_t resSize = 0;
240 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
242 if (g_adapterHandler[index].GetnetInfo != NULL)
244 // #1. get information for each adapter
245 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
249 if (res == CA_STATUS_OK)
251 resSize += tempSize[index];
256 "%d adapter network info size is %" PRIu32 " res:%d",
263 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
267 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
271 return CA_STATUS_FAILED;
274 // #3. add data into result
277 CALocalConnectivity_t *resInfo = (CALocalConnectivity_t *)
278 OICCalloc(resSize, sizeof(CALocalConnectivity_t));
279 CA_MEMORY_ALLOC_CHECK(resInfo);
285 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
288 if (tempSize[index] == 0)
295 sizeof(*resInfo) * tempSize[index]);
297 resInfo += tempSize[index];
300 OICFree(tempInfo[index]);
301 tempInfo[index] = NULL;
304 OIC_LOG(DEBUG, TAG, "each network info save success!");
307 // memory error label.
310 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
313 OICFree(tempInfo[index]);
314 tempInfo[index] = NULL;
317 return CA_MEMORY_ALLOC_FAILED;
320 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
322 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
324 CAResult_t res = CA_STATUS_FAILED;
326 if (endpoint == NULL)
328 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
329 return CA_STATUS_INVALID_PARAM;
332 CATransportType_t type = endpoint->transportType;
334 int index = CAGetAdapterIndex(type);
338 OIC_LOG(ERROR, TAG, "unknown transport type!");
339 return CA_STATUS_INVALID_PARAM;
342 uint32_t sentDataLen = 0;
344 if (g_adapterHandler[index].sendData != NULL)
346 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
349 if (sentDataLen != -1)
356 CAResult_t CASendMulticastData(const void *data, uint32_t length)
358 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
360 CAResult_t res = CA_SEND_FAILED;
361 u_arraylist_t *list = CAGetSelectedNetworkList();
365 OIC_LOG(DEBUG, TAG, "No selected network");
366 return CA_SEND_FAILED;
370 for (i = 0; i < u_arraylist_length(list); i++)
372 void* ptrType = u_arraylist_get(list, i);
379 CATransportType_t connType = *(CATransportType_t *) ptrType;
381 int index = CAGetAdapterIndex(connType);
385 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
389 uint32_t sentDataLen = 0;
391 if (g_adapterHandler[index].sendDataToAll != NULL)
393 void *payload = (void *) OICMalloc(length);
396 OIC_LOG(ERROR, TAG, "Out of memory!");
397 return CA_MEMORY_ALLOC_FAILED;
399 memcpy(payload, data, length);
400 sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
404 if (sentDataLen == length)
410 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
417 CAResult_t CAStartListeningServerAdapters()
419 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
421 u_arraylist_t *list = CAGetSelectedNetworkList();
424 OIC_LOG(ERROR, TAG, "No selected network");
425 return CA_STATUS_FAILED;
429 for (i = 0; i < u_arraylist_length(list); i++)
431 void* ptrType = u_arraylist_get(list, i);
438 CATransportType_t connType = *(CATransportType_t *) ptrType;
440 int index = CAGetAdapterIndex(connType);
443 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
447 if (g_adapterHandler[index].startListenServer != NULL)
449 g_adapterHandler[index].startListenServer();
456 CAResult_t CAStartDiscoveryServerAdapters()
458 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
460 u_arraylist_t *list = CAGetSelectedNetworkList();
464 OIC_LOG(ERROR, TAG, "No selected network");
465 return CA_STATUS_FAILED;
469 for (i = 0; i < u_arraylist_length(list); i++)
471 void* ptrType = u_arraylist_get(list, i);
478 CATransportType_t connType = *(CATransportType_t *) ptrType;
480 int index = CAGetAdapterIndex(connType);
484 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
488 if (g_adapterHandler[index].startDiscoveryServer != NULL)
490 g_adapterHandler[index].startDiscoveryServer();
497 void CATerminateAdapters()
499 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
502 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
504 if (g_adapterHandler[index].terminate != NULL)
506 g_adapterHandler[index].terminate();