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,
141 CAAdapterErrorHandleCallback, handle);
142 #endif /* IP_ADAPTER */
145 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
147 OIC_LOG(DEBUG, TAG, "Set packet received callback");
149 g_networkPacketReceivedCallback = callback;
152 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
154 OIC_LOG(DEBUG, TAG, "Set network change callback");
156 g_networkChangeCallback = callback;
159 CAResult_t CAStartAdapter(CATransportType_t transportType)
161 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
163 int index = CAGetAdapterIndex(transportType);
167 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
168 return CA_STATUS_FAILED;
171 if (g_adapterHandler[index].startAdapter != NULL)
173 g_adapterHandler[index].startAdapter();
179 void CAStopAdapter(CATransportType_t transportType)
181 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
183 int index = CAGetAdapterIndex(transportType);
187 OIC_LOG(ERROR, TAG, "unknown transport type!");
191 if (g_adapterHandler[index].stopAdapter != NULL)
193 g_adapterHandler[index].stopAdapter();
197 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
199 if (info == NULL || size == NULL)
201 return CA_STATUS_INVALID_PARAM;
204 CALocalConnectivity_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
205 uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
207 CAResult_t res = CA_STATUS_FAILED;
208 uint32_t resSize = 0;
209 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
211 if (g_adapterHandler[index].GetnetInfo != NULL)
213 // #1. get information for each adapter
214 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
218 if (res == CA_STATUS_OK)
220 resSize += tempSize[index];
225 "%d adapter network info size is %" PRIu32 " res:%d",
232 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
236 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
240 return CA_STATUS_FAILED;
243 // #3. add data into result
246 CALocalConnectivity_t * resInfo = OICCalloc(resSize, sizeof(*resInfo));
247 CA_MEMORY_ALLOC_CHECK(resInfo);
253 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
256 if (tempSize[index] == 0)
263 sizeof(*resInfo) * tempSize[index]);
265 resInfo += tempSize[index];
268 OICFree(tempInfo[index]);
269 tempInfo[index] = NULL;
272 OIC_LOG(DEBUG, TAG, "each network info save success!");
275 // memory error label.
278 for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
281 OICFree(tempInfo[index]);
282 tempInfo[index] = NULL;
285 return CA_MEMORY_ALLOC_FAILED;
288 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
290 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
292 CAResult_t res = CA_STATUS_FAILED;
294 if (endpoint == NULL)
296 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
297 return CA_STATUS_INVALID_PARAM;
300 CATransportType_t type = endpoint->transportType;
302 int index = CAGetAdapterIndex(type);
306 OIC_LOG(ERROR, TAG, "unknown transport type!");
307 return CA_STATUS_INVALID_PARAM;
310 uint32_t sentDataLen = 0;
312 if (g_adapterHandler[index].sendData != NULL)
314 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
317 if (sentDataLen != -1)
324 CAResult_t CASendMulticastData(const void *data, uint32_t length)
326 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
328 CAResult_t res = CA_STATUS_FAILED;
329 u_arraylist_t *list = CAGetSelectedNetworkList();
333 OIC_LOG(DEBUG, TAG, "No selected network");
334 return CA_STATUS_FAILED;
338 for (i = 0; i < u_arraylist_length(list); i++)
340 void* ptrType = u_arraylist_get(list, i);
347 CATransportType_t connType = *(CATransportType_t *) ptrType;
349 int index = CAGetAdapterIndex(connType);
353 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
357 uint32_t sentDataLen = 0;
359 if (g_adapterHandler[index].sendDataToAll != NULL)
361 void *payload = (void *) OICMalloc(length);
364 OIC_LOG(ERROR, TAG, "Out of memory!");
365 return CA_MEMORY_ALLOC_FAILED;
367 memcpy(payload, data, length);
368 sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
372 if (sentDataLen == length)
378 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
385 CAResult_t CAStartListeningServerAdapters()
387 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
389 u_arraylist_t *list = CAGetSelectedNetworkList();
392 OIC_LOG(ERROR, TAG, "No selected network");
393 return CA_STATUS_FAILED;
397 for (i = 0; i < u_arraylist_length(list); i++)
399 void* ptrType = u_arraylist_get(list, i);
406 CATransportType_t connType = *(CATransportType_t *) ptrType;
408 int index = CAGetAdapterIndex(connType);
411 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
415 if (g_adapterHandler[index].startListenServer != NULL)
417 g_adapterHandler[index].startListenServer();
424 CAResult_t CAStartDiscoveryServerAdapters()
426 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
428 u_arraylist_t *list = CAGetSelectedNetworkList();
432 OIC_LOG(ERROR, TAG, "No selected network");
433 return CA_STATUS_FAILED;
437 for (i = 0; i < u_arraylist_length(list); i++)
439 void* ptrType = u_arraylist_get(list, i);
446 CATransportType_t connType = *(CATransportType_t *) ptrType;
448 int index = CAGetAdapterIndex(connType);
452 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
456 if (g_adapterHandler[index].startDiscoveryServer != NULL)
458 g_adapterHandler[index].startDiscoveryServer();
465 void CATerminateAdapters()
467 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
470 for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
472 if (g_adapterHandler[index].terminate != NULL)
474 g_adapterHandler[index].terminate();