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 ******************************************************************/
21 #include "cainterfacecontroller_singlethread.h"
28 #include "cawifiadapter_singlethread.h"
29 #include "caethernetadapter_singlethread.h"
30 #include "caedradapter_singlethread.h"
31 #include "caleadapter_singlethread.h"
32 #include "caadapterutils.h"
34 #include "canetworkconfigurator.h"
35 #include "oic_malloc.h"
38 #define TAG "CAIFCNT_ST"
40 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(ERROR, TAG, "Out of memory");\
41 goto memory_error_exit;} }
43 #define CA_CONNECTIVITY_TYPE_NUM 4
45 static CAConnectivityHandler_t g_adapterHandler[CA_CONNECTIVITY_TYPE_NUM];
47 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
49 static CANetworkChangeCallback g_networkChangeCallback = NULL;
51 static int CAGetAdapterIndex(CAConnectivityType_t cType)
65 OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4");
70 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
72 OIC_LOG(DEBUG, TAG, "IN");
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(DEBUG, TAG, "unknown connectivity type!");
96 memcpy(&g_adapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
98 OIC_LOG_V(DEBUG, TAG, "%d type adapter", cType);
99 OIC_LOG(DEBUG, TAG, "OUT");
102 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
105 OIC_LOG(DEBUG, TAG, "IN");
107 // Call the callback.
108 if (g_networkPacketReceivedCallback != NULL)
110 g_networkPacketReceivedCallback(endpoint, data, dataLen);
112 OIC_LOG(DEBUG, TAG, "OUT");
115 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
117 OIC_LOG(DEBUG, TAG, "IN");
119 // Call the callback.
120 if (g_networkChangeCallback != NULL)
122 g_networkChangeCallback(info, status);
124 OIC_LOG(DEBUG, TAG, "OUT");
127 void CAInitializeAdapters()
129 OIC_LOG(DEBUG, TAG, "IN");
131 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
133 // Initialize adapters and register callback.
134 #ifdef ETHERNET_ADAPTER
135 CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
136 #endif /* ETHERNET_ADAPTER */
139 CAInitializeWIFI(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
140 #endif /* WIFI_ADAPTER */
143 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
144 #endif /* EDR_ADAPTER */
147 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
148 #endif /* LE_ADAPTER */
149 OIC_LOG(DEBUG, TAG, "OUT");
153 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
155 OIC_LOG(DEBUG, TAG, "IN");
157 g_networkPacketReceivedCallback = callback;
158 OIC_LOG(DEBUG, TAG, "OUT");
161 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
163 OIC_LOG(DEBUG, TAG, "IN");
165 g_networkChangeCallback = callback;
166 OIC_LOG(DEBUG, TAG, "OUT");
169 CAResult_t CAStartAdapter(CAConnectivityType_t cType)
171 OIC_LOG_V(DEBUG, TAG, "cType[%d]", cType);
173 int index = CAGetAdapterIndex(cType);
177 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
178 return CA_STATUS_FAILED;
181 if (g_adapterHandler[index].startAdapter != NULL)
183 g_adapterHandler[index].startAdapter();
185 OIC_LOG(DEBUG, TAG, "OUT");
189 void CAStopAdapter(CAConnectivityType_t cType)
191 OIC_LOG_V(DEBUG, TAG, "cType[%d]", cType);
193 int index = CAGetAdapterIndex(cType);
197 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
201 if (g_adapterHandler[index].stopAdapter != NULL)
203 g_adapterHandler[index].stopAdapter();
205 OIC_LOG(DEBUG, TAG, "OUT");
208 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
210 OIC_LOG(DEBUG, TAG, "IN");
211 VERIFY_NON_NULL(info, TAG, "info");
212 VERIFY_NON_NULL(size, TAG, "size");
214 CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
215 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
217 CAResult_t res = CA_STATUS_FAILED;
218 // #1. get information each adapter
219 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
221 if (g_adapterHandler[index].GetnetInfo != NULL)
223 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
225 OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
229 uint32_t resSize = 0;
230 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
233 if (tempInfo[index] == NULL || tempSize[index] <= 0)
239 resSize += tempSize[index];
242 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
246 if (CA_ADAPTER_NOT_ENABLED == res || CA_NOT_SUPPORTED == res)
250 return CA_STATUS_FAILED;
253 // #3. add data into result
255 CALocalConnectivity_t *resInfo =
256 (CALocalConnectivity_t *) OICCalloc(resSize, sizeof(CALocalConnectivity_t));
257 CA_MEMORY_ALLOC_CHECK(resInfo);
260 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
263 if (tempInfo[index] == NULL || tempSize[index] <= 0)
268 memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
270 i += tempSize[index];
273 OICFree(tempInfo[index]);
280 OIC_LOG(DEBUG, TAG, "OUT");
284 // memory error label.
287 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
290 OICFree(tempInfo[index]);
293 return CA_MEMORY_ALLOC_FAILED;
296 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
298 OIC_LOG(DEBUG, TAG, "IN");
300 CAResult_t res = CA_STATUS_FAILED;
302 if (endpoint == NULL)
304 OIC_LOG(DEBUG, TAG, "RemoteEndpoint is NULL");
305 return CA_STATUS_INVALID_PARAM;
308 CAConnectivityType_t type = endpoint->connectivityType;
310 int index = CAGetAdapterIndex(type);
314 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
315 return CA_STATUS_INVALID_PARAM;
318 uint32_t sentDataLen = 0;
319 if (g_adapterHandler[index].sendData != NULL)
321 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
324 if (sentDataLen == length)
329 OIC_LOG(DEBUG, TAG, "OUT");
333 CAResult_t CASendMulticastData(const void *data, uint32_t length)
335 OIC_LOG(DEBUG, TAG, "IN");
337 CAResult_t res = CA_STATUS_FAILED;
338 u_arraylist_t *list = CAGetSelectedNetworkList();
342 OIC_LOG(DEBUG, TAG, "No selected network");
346 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
348 void* ptrType = u_arraylist_get(list, i);
353 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
355 int index = CAGetAdapterIndex(connType);
359 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
363 uint32_t sentDataLen = 0;
364 if (g_adapterHandler[index].sendDataToAll != NULL)
366 sentDataLen = g_adapterHandler[index].sendDataToAll(data, length);
369 if (sentDataLen == length)
374 OIC_LOG(DEBUG, TAG, "OUT");
378 CAResult_t CAStartListeningServerAdapters()
380 OIC_LOG(DEBUG, TAG, "IN");
382 u_arraylist_t *list = CAGetSelectedNetworkList();
386 OIC_LOG(DEBUG, TAG, "No selected network");
387 return CA_STATUS_FAILED;
390 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
392 void* ptrType = u_arraylist_get(list, i);
395 OIC_LOG(ERROR, TAG, "Invalid conn type");
398 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
400 int index = CAGetAdapterIndex(connType);
404 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
408 if (g_adapterHandler[index].startListenServer != NULL)
410 g_adapterHandler[index].startListenServer();
413 OIC_LOG(DEBUG, TAG, "OUT");
417 CAResult_t CAStartDiscoveryServerAdapters()
419 OIC_LOG(DEBUG, TAG, "IN");
421 u_arraylist_t *list = CAGetSelectedNetworkList();
425 OIC_LOG(DEBUG, TAG, "No selected network");
426 return CA_STATUS_FAILED;
429 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
431 void* ptrType = u_arraylist_get(list, i);
436 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
438 int index = CAGetAdapterIndex(connType);
442 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
446 if (g_adapterHandler[index].startDiscoveryServer != NULL)
448 g_adapterHandler[index].startDiscoveryServer();
451 OIC_LOG(DEBUG, TAG, "OUT");
455 void CATerminateAdapters()
457 OIC_LOG(DEBUG, TAG, "IN");
461 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
463 if (g_adapterHandler[index].stopAdapter != NULL)
465 g_adapterHandler[index].stopAdapter();
467 if (g_adapterHandler[index].terminate != NULL)
469 g_adapterHandler[index].terminate();
473 OIC_LOG(DEBUG, TAG, "OUT");
476 CAResult_t CAReadData()
478 OIC_LOG(DEBUG, TAG, "IN");
479 u_arraylist_t *list = CAGetSelectedNetworkList();
483 return CA_STATUS_FAILED;
486 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
488 void *ptrType = u_arraylist_get(list, i);
491 OIC_LOG(ERROR, TAG, "get list fail");
492 return CA_STATUS_FAILED;
495 CAConnectivityType_t connType = *(CAConnectivityType_t *) ptrType;
497 int index = CAGetAdapterIndex(connType);
501 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
505 if (g_adapterHandler[index].readData != NULL)
507 g_adapterHandler[index].readData();
511 OIC_LOG(DEBUG, TAG, "OUT");