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 "caethernetadapter.h"
28 #include "caedradapter.h"
29 #include "caleadapter.h"
30 #include "cawifiadapter.h"
31 #include "canetworkconfigurator.h"
32 #include "caremotehandler.h"
33 #include "oic_malloc.h"
35 #include "uthreadpool.h"
39 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
40 {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
42 #define CA_CONNECTIVITY_TYPE_NUM 4
44 static CAConnectivityHandler_t g_adapterHandler[CA_CONNECTIVITY_TYPE_NUM];
46 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
48 static CANetworkChangeCallback g_networkChangeCallback = NULL;
50 static int CAGetAdapterIndex(CAConnectivityType_t cType)
64 OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4");
69 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
71 OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
73 if(handler.startAdapter == NULL ||
74 handler.startListenServer == NULL ||
75 handler.startDiscoveryServer == NULL ||
76 handler.sendData == NULL ||
77 handler.sendDataToAll == NULL ||
78 handler.GetnetInfo == NULL ||
79 handler.readData == NULL ||
80 handler.stopAdapter == NULL ||
81 handler.terminate == NULL)
83 OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
87 int index = CAGetAdapterIndex(cType);
91 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
95 memcpy(&g_adapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
97 OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
100 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
103 OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
105 // Call the callback.
106 if (g_networkPacketReceivedCallback != NULL)
108 g_networkPacketReceivedCallback(endpoint, data, dataLen);
117 OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
121 static void CANetworkChangedCallback(CALocalConnectivity_t *info,
122 CANetworkStatus_t status)
124 OIC_LOG(DEBUG, TAG, "Network Changed callback");
126 // Call the callback.
127 if (g_networkChangeCallback != NULL)
129 g_networkChangeCallback(info, status);
133 void CAInitializeAdapters(u_thread_pool_t handle)
135 OIC_LOG(DEBUG, TAG, "initialize adapters..");
137 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
139 // Initialize adapters and register callback.
140 #ifdef ETHERNET_ADAPTER
141 CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
143 #endif /* ETHERNET_ADAPTER */
146 CAInitializeWIFI(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
148 #endif /* WIFI_ADAPTER */
151 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
153 #endif /* EDR_ADAPTER */
156 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
158 #endif /* LE_ADAPTER */
162 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
164 OIC_LOG(DEBUG, TAG, "Set packet received callback");
166 g_networkPacketReceivedCallback = callback;
169 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
171 OIC_LOG(DEBUG, TAG, "Set network change callback");
173 g_networkChangeCallback = callback;
176 CAResult_t CAStartAdapter(CAConnectivityType_t cType)
178 OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", cType);
180 int index = CAGetAdapterIndex(cType);
184 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
185 return CA_STATUS_FAILED;
188 if (g_adapterHandler[index].startAdapter != NULL)
190 g_adapterHandler[index].startAdapter();
196 void CAStopAdapter(CAConnectivityType_t cType)
198 OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CAConnectivityType[%d]", cType);
200 int index = CAGetAdapterIndex(cType);
204 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
208 if (g_adapterHandler[index].stopAdapter != NULL)
210 g_adapterHandler[index].stopAdapter();
214 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
216 CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
217 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
219 memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
220 memset(tempSize, 0, sizeof(uint32_t) * CA_CONNECTIVITY_TYPE_NUM);
222 // #1. get information each adapter
224 CAResult_t res = CA_STATUS_FAILED;
225 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
227 if (g_adapterHandler[index].GetnetInfo != NULL)
229 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
231 OIC_LOG_V(DEBUG, TAG, "%d adapter network info size is %d res:%d", index,
232 tempSize[index], res);
236 uint32_t resSize = 0;
237 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
240 if (tempInfo[index] == NULL || tempSize[index] <= 0)
246 resSize += tempSize[index];
249 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
253 if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
257 return CA_STATUS_FAILED;
260 // #3. add data into result
263 CALocalConnectivity_t *resInfo = (CALocalConnectivity_t *) OICCalloc(
264 resSize, sizeof(CALocalConnectivity_t));
265 CA_MEMORY_ALLOC_CHECK(resInfo);
268 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
271 if (tempInfo[index] == NULL || tempSize[index] <= 0)
276 memcpy(resInfo + pos, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
278 pos += tempSize[index];
281 OICFree(tempInfo[index]);
282 tempInfo[index] = NULL;
296 OIC_LOG(DEBUG, TAG, "each network info save success!");
299 // memory error label.
302 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
305 OICFree(tempInfo[index]);
306 tempInfo[index] = NULL;
309 return CA_MEMORY_ALLOC_FAILED;
312 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
314 OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
316 CAResult_t res = CA_STATUS_FAILED;
318 if (endpoint == NULL)
320 OIC_LOG(DEBUG, TAG, "Invalid endpoint");
321 return CA_STATUS_INVALID_PARAM;
324 CAConnectivityType_t type = endpoint->connectivityType;
326 int index = CAGetAdapterIndex(type);
330 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
331 return CA_STATUS_INVALID_PARAM;
334 uint32_t sentDataLen = 0;
336 if (g_adapterHandler[index].sendData != NULL)
338 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
341 if (sentDataLen != -1)
348 CAResult_t CASendMulticastData(const void *data, uint32_t length)
350 OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
352 CAResult_t res = CA_STATUS_FAILED;
353 u_arraylist_t *list = CAGetSelectedNetworkList();
357 OIC_LOG(DEBUG, TAG, "No selected network");
358 return CA_STATUS_FAILED;
362 for (i = 0; i < u_arraylist_length(list); i++)
364 void* ptrType = u_arraylist_get(list, i);
371 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
373 int index = CAGetAdapterIndex(connType);
377 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
381 uint32_t sentDataLen = 0;
383 if (g_adapterHandler[index].sendDataToAll != NULL)
385 void *payload = (void *) OICMalloc(length);
388 OIC_LOG(ERROR, TAG, "Out of memory!");
389 return CA_MEMORY_ALLOC_FAILED;
391 memcpy(payload, data, length);
392 sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
396 if (sentDataLen == length)
402 OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
409 CAResult_t CAStartListeningServerAdapters()
411 OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
413 u_arraylist_t *list = CAGetSelectedNetworkList();
416 OIC_LOG(ERROR, TAG, "No selected network");
417 return CA_STATUS_FAILED;
421 for (i = 0; i < u_arraylist_length(list); i++)
423 void* ptrType = u_arraylist_get(list, i);
430 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
432 int index = CAGetAdapterIndex(connType);
435 OIC_LOG(ERROR, TAG, "unknown connectivity type!");
439 if (g_adapterHandler[index].startListenServer != NULL)
441 g_adapterHandler[index].startListenServer();
448 CAResult_t CAStartDiscoveryServerAdapters()
450 OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
452 u_arraylist_t *list = CAGetSelectedNetworkList();
456 OIC_LOG(ERROR, TAG, "No selected network");
457 return CA_STATUS_FAILED;
461 for (i = 0; i < u_arraylist_length(list); i++)
463 void* ptrType = u_arraylist_get(list, i);
470 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
472 int index = CAGetAdapterIndex(connType);
476 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
480 if (g_adapterHandler[index].startDiscoveryServer != NULL)
482 g_adapterHandler[index].startDiscoveryServer();
489 void CATerminateAdapters()
491 OIC_LOG(DEBUG, TAG, "terminate all adapters..");
494 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
496 if (g_adapterHandler[index].stopAdapter != NULL)
498 g_adapterHandler[index].stopAdapter();
500 if (g_adapterHandler[index].terminate != NULL)
502 g_adapterHandler[index].terminate();