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"
33 #include "canetworkconfigurator.h"
34 #include "oic_malloc.h"
37 #define TAG "CAIFCNT_ST"
39 #define MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(DEBUG, TAG, "Out of memory");\
40 goto memory_error_exit;} }
42 #define CA_CONNECTIVITY_TYPE_NUM 4
44 static CAConnectivityHandler_t gAdapterHandler[CA_CONNECTIVITY_TYPE_NUM];
46 static CANetworkPacketReceivedCallback gNetworkPacketReceivedCallback = NULL;
48 static CANetworkChangeCallback gNetworkChangeCallback = NULL;
50 static int8_t CAGetAdapterIndex(CAConnectivityType_t cType)
66 static void CARegisterCallback(CAConnectivityHandler_t handler, CAConnectivityType_t cType)
68 OIC_LOG(DEBUG, TAG, "IN");
71 index = CAGetAdapterIndex(cType);
75 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
79 memcpy(&gAdapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
81 OIC_LOG_V(DEBUG, TAG, "%d type adapter", cType);
82 OIC_LOG(DEBUG, TAG, "OUT");
85 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
88 OIC_LOG(DEBUG, TAG, "IN");
91 if (gNetworkPacketReceivedCallback != NULL)
93 gNetworkPacketReceivedCallback(endpoint, data, dataLen);
95 OIC_LOG(DEBUG, TAG, "OUT");
98 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
100 OIC_LOG(DEBUG, TAG, "IN");
102 // Call the callback.
103 if (gNetworkChangeCallback != NULL)
105 gNetworkChangeCallback(info, status);
107 OIC_LOG(DEBUG, TAG, "OUT");
110 void CAInitializeAdapters()
112 OIC_LOG(DEBUG, TAG, "IN");
114 memset(gAdapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
117 // Initialize adapters and register callback.
118 #ifdef ETHERNET_ADAPTER
119 CAInitializeEthernet(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
120 #endif /* ETHERNET_ADAPTER */
123 CAInitializeWifi(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
124 #endif /* WIFI_ADAPTER */
127 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
128 #endif /* EDR_ADAPTER */
131 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
132 #endif /* LE_ADAPTER */
133 OIC_LOG(DEBUG, TAG, "OUT");
137 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
139 OIC_LOG(DEBUG, TAG, "IN");
141 gNetworkPacketReceivedCallback = callback;
142 OIC_LOG(DEBUG, TAG, "OUT");
145 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
147 OIC_LOG(DEBUG, TAG, "IN");
149 gNetworkChangeCallback = callback;
150 OIC_LOG(DEBUG, TAG, "OUT");
153 void CAStartAdapter(CAConnectivityType_t cType)
155 OIC_LOG_V(DEBUG, TAG, "cType[%d]", cType);
159 index = CAGetAdapterIndex(cType);
163 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
167 if (gAdapterHandler[index].startAdapter != NULL)
169 gAdapterHandler[index].startAdapter();
171 OIC_LOG(DEBUG, TAG, "OUT");
174 void CAStopAdapter(CAConnectivityType_t cType)
176 OIC_LOG_V(DEBUG, TAG, "cType[%d]", cType);
180 index = CAGetAdapterIndex(cType);
184 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
188 if (gAdapterHandler[index].stopAdapter != NULL)
190 gAdapterHandler[index].stopAdapter();
192 OIC_LOG(DEBUG, TAG, "OUT");
195 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
197 OIC_LOG(DEBUG, TAG, "IN");
198 CAResult_t res = CA_STATUS_FAILED;
202 CALocalConnectivity_t *resInfo = NULL;
203 uint32_t resSize = 0;
205 CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM];
206 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM];
208 memset(tempInfo, 0, sizeof(CALocalConnectivity_t *) * CA_CONNECTIVITY_TYPE_NUM);
209 memset(tempSize, 0, sizeof(int8_t) * CA_CONNECTIVITY_TYPE_NUM);
211 // #1. get information each adapter
212 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
214 if (gAdapterHandler[index].GetnetInfo != NULL)
216 res = gAdapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
218 OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
223 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
226 if (tempInfo[index] == NULL || tempSize[index] <= 0)
232 resSize += tempSize[index];
235 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
242 // #3. add data into result
244 resInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * resSize);
245 MEMORY_ALLOC_CHECK(resInfo);
246 memset(resInfo, 0, sizeof(CALocalConnectivity_t) * resSize);
249 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
252 if (tempInfo[index] == NULL || tempSize[index] <= 0)
257 memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
259 i += tempSize[index];
262 OICFree(tempInfo[index]);
269 OIC_LOG(DEBUG, TAG, "OUT");
273 // memory error label.
276 return CA_MEMORY_ALLOC_FAILED;
279 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, void *data, uint32_t length)
281 OIC_LOG(DEBUG, TAG, "IN");
284 uint32_t sentDataLen = 0;
285 CAResult_t res = CA_STATUS_FAILED;
287 if (endpoint == NULL)
289 OIC_LOG(DEBUG, TAG, "RemoteEndpoint is NULL");
290 return CA_STATUS_INVALID_PARAM;
293 CAConnectivityType_t type = endpoint->connectivityType;
295 index = CAGetAdapterIndex(type);
299 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
300 return CA_STATUS_INVALID_PARAM;
303 if (gAdapterHandler[index].sendData != NULL)
305 sentDataLen = gAdapterHandler[index].sendData(endpoint, data, length);
308 if (sentDataLen == length)
313 OIC_LOG(DEBUG, TAG, "OUT");
317 CAResult_t CASendMulticastData(void *data, uint32_t length)
319 OIC_LOG(DEBUG, TAG, "IN");
322 CAConnectivityType_t connType;
324 uint32_t sentDataLen = 0;
325 CAResult_t res = CA_STATUS_FAILED;
326 u_arraylist_t *list = CAGetSelectedNetworkList();
330 OIC_LOG(DEBUG, TAG, "No selected network");
331 return CA_STATUS_FAILED;
334 void *ptrType = NULL;
335 for (i = 0; i < u_arraylist_length(list); i++)
337 ptrType = u_arraylist_get(list, i);
340 OIC_LOG(ERROR, TAG, "error");
341 return CA_STATUS_FAILED;
343 connType = *(CAConnectivityType_t *) ptrType;
345 index = CAGetAdapterIndex(connType);
349 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
353 if (gAdapterHandler[index].sendDataToAll != NULL)
355 sentDataLen = gAdapterHandler[index].sendDataToAll(data, length);
358 if (sentDataLen == length)
363 OIC_LOG(DEBUG, TAG, "OUT");
367 CAResult_t CAStartListeningServerAdapters()
369 OIC_LOG(DEBUG, TAG, "IN");
372 CAConnectivityType_t connType;
374 u_arraylist_t *list = CAGetSelectedNetworkList();
378 OIC_LOG(DEBUG, TAG, "No selected network");
379 return CA_STATUS_FAILED;
382 void *ptrType = NULL;
383 for (i = 0; i < u_arraylist_length(list); i++)
385 ptrType = u_arraylist_get(list, i);
388 OIC_LOG(ERROR, TAG, "error");
389 return CA_STATUS_FAILED;
391 connType = *(CAConnectivityType_t *) ptrType;
393 index = CAGetAdapterIndex(connType);
397 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
401 if (gAdapterHandler[index].startListenServer != NULL)
403 gAdapterHandler[index].startListenServer();
406 OIC_LOG(DEBUG, TAG, "OUT");
410 CAResult_t CAStartDiscoveryServerAdapters()
412 OIC_LOG(DEBUG, TAG, "IN");
415 CAConnectivityType_t connType;
417 u_arraylist_t *list = CAGetSelectedNetworkList();
421 OIC_LOG(DEBUG, TAG, "No selected network");
422 return CA_STATUS_FAILED;
425 void *ptrType = NULL;
426 for (i = 0; i < u_arraylist_length(list); i++)
428 ptrType = u_arraylist_get(list, i);
431 OIC_LOG(ERROR, TAG, "error");
432 return CA_STATUS_FAILED;
434 connType = *(CAConnectivityType_t *) ptrType;
436 index = CAGetAdapterIndex(connType);
440 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
444 if (gAdapterHandler[index].startDiscoverServer != NULL)
446 gAdapterHandler[index].startDiscoverServer();
449 OIC_LOG(DEBUG, TAG, "OUT");
453 void CATerminateAdapters()
455 OIC_LOG(DEBUG, TAG, "IN");
459 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
461 if (gAdapterHandler[index].terminate != NULL)
463 gAdapterHandler[index].terminate();
466 OIC_LOG(DEBUG, TAG, "OUT");
469 CAResult_t CAReadData()
473 CAConnectivityType_t connType;
476 u_arraylist_t *list = CAGetSelectedNetworkList();
480 return CA_STATUS_FAILED;
482 void *ptrType = NULL;
483 for (i = 0; i < u_arraylist_length(list); i++)
485 ptrType = u_arraylist_get(list, i);
488 OIC_LOG(ERROR, TAG, "error");
489 return CA_STATUS_FAILED;
491 connType = *(CAConnectivityType_t *) ptrType;
493 index = CAGetAdapterIndex(connType);
497 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
501 if (gAdapterHandler[index].readData != NULL)
503 gAdapterHandler[index].readData();