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,
117 #endif /* ETHERNET_ADAPTER */
120 CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
121 #endif /* WIFI_ADAPTER */
124 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
125 #endif /* EDR_ADAPTER */
128 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback, handle);
129 #endif /* LE_ADAPTER */
133 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
135 OIC_LOG(DEBUG, TAG, "Set packet received callback");
137 gNetworkPacketReceivedCallback = callback;
140 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
142 OIC_LOG(DEBUG, TAG, "Set network change callback");
144 gNetworkChangeCallback = callback;
147 void CAStartAdapter(CAConnectivityType_t cType)
149 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
153 index = CAGetAdapterIndex(cType);
157 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
161 if (gAdapterHandler[index].startAdapter != NULL)
163 gAdapterHandler[index].startAdapter();
167 void CAStopAdapter(CAConnectivityType_t cType)
169 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
173 index = CAGetAdapterIndex(cType);
177 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
181 if (gAdapterHandler[index].stopAdapter != NULL)
183 gAdapterHandler[index].stopAdapter();
187 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
189 CAResult_t res = CA_STATUS_FAILED;
193 CALocalConnectivity_t *resInfo = NULL;
194 uint32_t resSize = 0;
196 CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
197 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
199 memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
200 memset(tempSize, 0, sizeof(uint32_t) * CA_CONNECTIVITY_TYPE_NUM);
202 // #1. get information each adapter
203 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
205 if (gAdapterHandler[index].GetnetInfo != NULL)
207 res = gAdapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
209 OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
210 tempSize[index], res);
215 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
218 if (tempInfo[index] == NULL || tempSize[index] <= 0)
224 resSize += tempSize[index];
227 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
231 res = CA_STATUS_FAILED;
235 // #3. add data into result
237 resInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * resSize);
238 CA_MEMORY_ALLOC_CHECK(resInfo);
239 memset(resInfo, 0, sizeof(CALocalConnectivity_t) * resSize);
242 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
245 if (tempInfo[index] == NULL || tempSize[index] <= 0)
250 memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
252 i += tempSize[index];
255 OICFree(tempInfo[index]);
262 OIC_LOG_V(DEBUG, TAG, "each network info save success!");
266 // memory error label.
269 return CA_MEMORY_ALLOC_FAILED;
272 CAResult_t CASendUnicastData(CARemoteEndpoint_t* endpoint, void* data, uint32_t length)
274 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
277 CAResult_t res = CA_STATUS_FAILED;
279 if (endpoint == NULL)
281 OIC_LOG_V(DEBUG, TAG, "Invalid endpoint");
282 return CA_STATUS_INVALID_PARAM;
285 CAConnectivityType_t type = endpoint->connectivityType;
287 index = CAGetAdapterIndex(type);
291 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
292 return CA_STATUS_INVALID_PARAM;
295 if (gAdapterHandler[index].sendData != NULL)
297 res = gAdapterHandler[index].sendData(endpoint, data, length);
299 //For Unicast , data will be deleted by adapters
301 CADestroyRemoteEndpointInternal(endpoint);
306 CAResult_t CASendMulticastData(void *data, uint32_t length)
308 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
312 CAResult_t res = CA_STATUS_FAILED;
313 u_arraylist_t *list = CAGetSelectedNetworkList();
317 OIC_LOG(DEBUG, TAG, "No selected network");
318 return CA_STATUS_FAILED;
321 for (i = 0; i < u_arraylist_length(list); i++)
323 type = *(uint8_t*) u_arraylist_get(list, i);
325 index = CAGetAdapterIndex(type);
329 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
333 if (gAdapterHandler[index].sendDataToAll != NULL)
335 void* payload = (void*) OICMalloc(length);
336 memcpy(payload, data, length);
337 res = gAdapterHandler[index].sendDataToAll(payload, length);
343 CAResult_t CAStartListeningServerAdapters()
345 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
349 u_arraylist_t *list = CAGetSelectedNetworkList();
353 OIC_LOG(DEBUG, TAG, "No selected network");
354 return CA_STATUS_FAILED;
357 for (i = 0; i < u_arraylist_length(list); i++)
359 type = *(uint8_t*) u_arraylist_get(list, i);
361 index = CAGetAdapterIndex(type);
365 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
369 if (gAdapterHandler[index].startListenServer != NULL)
371 gAdapterHandler[index].startListenServer();
378 CAResult_t CAStartDiscoveryServerAdapters()
380 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
384 u_arraylist_t *list = CAGetSelectedNetworkList();
388 OIC_LOG(DEBUG, TAG, "No selected network");
389 return CA_STATUS_FAILED;
392 for (i = 0; i < u_arraylist_length(list); i++)
394 type = *(uint8_t*) u_arraylist_get(list, i);
396 index = CAGetAdapterIndex(type);
400 OIC_LOG_V(DEBUG, TAG, "unknown connectivity type!");
404 if (gAdapterHandler[index].startDiscoverServer != NULL)
406 gAdapterHandler[index].startDiscoverServer();
413 void CATerminateAdapters()
415 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
419 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
421 if (gAdapterHandler[index].terminate != NULL)
423 gAdapterHandler[index].terminate();