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 "cawifiadapter.h"
28 #include "caethernetadapter.h"
29 #include "caedradapter.h"
30 #include "caleadapter.h"
31 #include "cawifiadapter.h"
32 #include "caethernetadapter.h"
33 #include "canetworkconfigurator.h"
34 #include "caremotehandler.h"
35 #include "oic_malloc.h"
37 #include "uthreadpool.h"
41 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG_V(DEBUG, TAG, "memory error"); goto memory_error_exit;} }
43 #define CA_CONNECTIVITY_TYPE_NUM 4
45 static CAConnectivityHandler_t gAdapterHandler[CA_CONNECTIVITY_TYPE_NUM];
47 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
49 static CANetworkChangeCallback gNetworkChangeCallback = NULL;
51 static int8_t CAGetAdapterIndex(CAConnectivityType_t cType)
67 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
69 OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
72 index = CAGetAdapterIndex(cType);
76 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
80 memcpy(&gAdapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
82 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
85 static void CAReceivedPacketCallback(CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
87 OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
90 if (gNetworkPacketReceivedCallback != NULL)
92 gNetworkPacketReceivedCallback(endpoint, data, dataLen);
96 static void CANetworkChangedCallback(CALocalConnectivity_t* info, CANetworkStatus_t status)
98 OIC_LOG(DEBUG, TAG, "Network Changed callback");
100 // Call the callback.
101 if (gNetworkChangeCallback != NULL)
103 gNetworkChangeCallback(info, status);
107 void CAInitializeAdapters(u_thread_pool_t handle)
109 OIC_LOG(DEBUG, TAG, "initialize adapters..");
111 memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
113 // Initialize adapters and register callback.
114 #ifdef ETHERNET_ADAPTER
115 CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
116 #endif /* ETHERNET_ADAPTER */
119 CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
120 #endif /* WIFI_ADAPTER */
123 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
124 #endif /* EDR_ADAPTER */
127 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
128 #endif /* LE_ADAPTER */
132 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
134 OIC_LOG(DEBUG, TAG, "Set packet received callback");
136 gNetworkPacketReceivedCallback = callback;
139 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
141 OIC_LOG(DEBUG, TAG, "Set network change callback");
143 gNetworkChangeCallback = callback;
146 void CAStartAdapter(CAConnectivityType_t cType)
148 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
152 index = CAGetAdapterIndex(cType);
156 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
160 if (gAdapterHandler[index].startAdapter != NULL)
162 gAdapterHandler[index].startAdapter();
166 void CAStopAdapter(CAConnectivityType_t cType)
168 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
172 index = CAGetAdapterIndex(cType);
176 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
180 if (gAdapterHandler[index].stopAdapter != NULL)
182 gAdapterHandler[index].stopAdapter();
186 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t* size)
188 CAResult_t res = CA_STATUS_FAILED;
192 CALocalConnectivity_t* resInfo = NULL;
193 uint32_t resSize = 0;
195 CALocalConnectivity_t* tempInfo[CA_CONNECTIVITY_TYPE_NUM];
196 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
198 memset(tempInfo, 0, sizeof(CALocalConnectivity_t*) * CA_CONNECTIVITY_TYPE_NUM);
199 memset(tempSize, 0, sizeof(int8_t) * CA_CONNECTIVITY_TYPE_NUM);
201 // #1. get information each adapter
202 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
204 if (gAdapterHandler[index].GetnetInfo != NULL)
206 res = gAdapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
208 OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
209 tempSize[index], res);
214 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
217 if (tempInfo[index] == NULL || tempSize[index] <= 0)
223 resSize += tempSize[index];
226 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
233 // #3. add data into result
235 resInfo = (CALocalConnectivity_t*) OICMalloc(sizeof(CALocalConnectivity_t) * resSize);
236 CA_MEMORY_ALLOC_CHECK(resInfo);
237 memset(resInfo, 0, sizeof(CALocalConnectivity_t) * resSize);
240 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
243 if (tempInfo[index] == NULL || tempSize[index] <= 0)
248 memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
250 i += tempSize[index];
253 OICFree(tempInfo[index]);
260 OIC_LOG_V(DEBUG, TAG, "each network info save success!");
264 // memory error label.
267 return CA_MEMORY_ALLOC_FAILED;
270 CAResult_t CASendUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t length)
272 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
275 CAResult_t res = CA_STATUS_FAILED;
277 if (endpoint == NULL)
279 OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
280 return CA_STATUS_INVALID_PARAM;
283 CAConnectivityType_t type = endpoint->connectivityType;
285 index = CAGetAdapterIndex(type);
289 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
290 return CA_STATUS_INVALID_PARAM;
293 if (gAdapterHandler[index].sendData != NULL)
295 res = gAdapterHandler[index].sendData(endpoint, data, length);
301 CAResult_t CASendMulticastData(void* data, uint32_t length)
303 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
307 CAResult_t res = CA_STATUS_FAILED;
308 u_arraylist_t *list = CAGetSelectedNetworkList();
312 OIC_LOG(DEBUG, TAG, "No selected network");
313 return CA_STATUS_FAILED;
316 for (i = 0; i < u_arraylist_length(list); i++)
318 type = *(int*) u_arraylist_get(list, i);
320 index = CAGetAdapterIndex(type);
324 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
328 if (gAdapterHandler[index].sendDataToAll != NULL)
330 res = gAdapterHandler[index].sendDataToAll(data, length);
337 CAResult_t CAStartListeningServerAdapters()
339 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
343 u_arraylist_t *list = CAGetSelectedNetworkList();
347 OIC_LOG(DEBUG, TAG, "No selected network");
348 return CA_STATUS_FAILED;
351 for (i = 0; i < u_arraylist_length(list); i++)
353 type = *(int*) u_arraylist_get(list, i);
355 index = CAGetAdapterIndex(type);
359 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
363 if (gAdapterHandler[index].startListenServer != NULL)
365 gAdapterHandler[index].startListenServer();
372 CAResult_t CAStartDiscoveryServerAdapters()
374 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
378 u_arraylist_t *list = CAGetSelectedNetworkList();
382 OIC_LOG(DEBUG, TAG, "No selected network");
383 return CA_STATUS_FAILED;
386 for (i = 0; i < u_arraylist_length(list); i++)
388 type = *(int*) u_arraylist_get(list, i);
390 index = CAGetAdapterIndex(type);
394 OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
398 if (gAdapterHandler[index].startDiscoverServer != NULL)
400 gAdapterHandler[index].startDiscoverServer();
407 void CATerminateAdapters()
409 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
413 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
415 if (gAdapterHandler[index].terminate != NULL)
417 gAdapterHandler[index].terminate();