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 "caipadapter_singlethread.h"
29 #include "caedradapter_singlethread.h"
30 #include "caleadapter_singlethread.h"
31 #include "caadapterutils.h"
33 #include "canetworkconfigurator.h"
34 #include "oic_malloc.h"
37 #define TAG "CAIFCNT_ST"
39 #define CA_MEMORY_ALLOC_CHECK(arg) { if (arg == NULL) {OIC_LOG(ERROR, TAG, "Out of memory");\
40 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 CAErrorHandleCallback g_errorHandleCallback = NULL;
52 static int CAGetAdapterIndex(CATransportAdapter_t cType)
58 case CA_ADAPTER_GATT_BTLE:
60 case CA_ADAPTER_RFCOMM_BTEDR:
64 OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4");
69 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType)
71 OIC_LOG(DEBUG, TAG, "IN");
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(DEBUG, TAG, "unknown connectivity type!");
95 g_adapterHandler[index] = handler;
97 OIC_LOG_V(DEBUG, TAG, "%d type adapter", cType);
98 OIC_LOG(DEBUG, TAG, "OUT");
101 static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data,
104 OIC_LOG(DEBUG, TAG, "IN");
106 // Call the callback.
107 if (g_networkPacketReceivedCallback != NULL)
109 g_networkPacketReceivedCallback(endpoint, data, dataLen);
111 OIC_LOG(DEBUG, TAG, "OUT");
114 void CASetErrorHandleCallback(CAErrorHandleCallback errorCallback)
116 OIC_LOG(DEBUG, TAG, "Set error handle callback");
117 g_errorHandleCallback = errorCallback;
120 static void CANetworkChangedCallback(CAEndpoint_t *info, CANetworkStatus_t status)
122 OIC_LOG(DEBUG, TAG, "IN");
124 // Call the callback.
125 if (g_networkChangeCallback != NULL)
127 g_networkChangeCallback(info, status);
129 OIC_LOG(DEBUG, TAG, "OUT");
132 void CAInitializeAdapters()
134 OIC_LOG(DEBUG, TAG, "IN");
136 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
138 // Initialize adapters and register callback.
140 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
141 #endif /* IP_ADAPTER */
144 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
145 #endif /* EDR_ADAPTER */
148 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
149 #endif /* LE_ADAPTER */
151 OIC_LOG(DEBUG, TAG, "OUT");
154 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
156 OIC_LOG(DEBUG, TAG, "IN");
158 g_networkPacketReceivedCallback = callback;
159 OIC_LOG(DEBUG, TAG, "OUT");
162 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
164 OIC_LOG(DEBUG, TAG, "IN");
166 g_networkChangeCallback = callback;
167 OIC_LOG(DEBUG, TAG, "OUT");
170 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
172 OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
174 int index = CAGetAdapterIndex(transportType);
178 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
179 return CA_STATUS_FAILED;
182 if (g_adapterHandler[index].startAdapter != NULL)
184 g_adapterHandler[index].startAdapter();
186 OIC_LOG(DEBUG, TAG, "OUT");
190 void CAStopAdapter(CATransportAdapter_t transportType)
192 OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
194 int index = CAGetAdapterIndex(transportType);
198 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
202 if (g_adapterHandler[index].stopAdapter != NULL)
204 g_adapterHandler[index].stopAdapter();
206 OIC_LOG(DEBUG, TAG, "OUT");
209 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
211 OIC_LOG(DEBUG, TAG, "IN");
212 VERIFY_NON_NULL(info, TAG, "info");
213 VERIFY_NON_NULL(size, TAG, "size");
215 CAEndpoint_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
216 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
218 CAResult_t res = CA_STATUS_FAILED;
219 // #1. get information each adapter
220 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
222 if (g_adapterHandler[index].GetnetInfo != NULL)
224 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
226 OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
230 uint32_t resSize = 0;
231 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
234 if (tempInfo[index] == NULL || tempSize[index] <= 0)
240 resSize += tempSize[index];
243 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
247 if (CA_ADAPTER_NOT_ENABLED == res || CA_NOT_SUPPORTED == res)
251 return CA_STATUS_FAILED;
254 // #3. add data into result
256 CAEndpoint_t *resInfo = (CAEndpoint_t *)OICCalloc(resSize, sizeof(CAEndpoint_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(CAEndpoint_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 CAEndpoint_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, "Endpoint is NULL");
305 return CA_STATUS_INVALID_PARAM;
308 CATransportAdapter_t type = endpoint->adapter;
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 CAEndpoint_t *endpoint, 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);
354 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
356 int index = CAGetAdapterIndex(connType);
360 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
364 uint32_t sentDataLen = 0;
365 if (g_adapterHandler[index].sendDataToAll != NULL)
367 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, data, length);
370 if (sentDataLen == length)
375 OIC_LOG(DEBUG, TAG, "OUT");
379 CAResult_t CAStartListeningServerAdapters()
381 OIC_LOG(DEBUG, TAG, "IN");
383 u_arraylist_t *list = CAGetSelectedNetworkList();
387 OIC_LOG(DEBUG, TAG, "No selected network");
388 return CA_STATUS_FAILED;
391 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
393 void* ptrType = u_arraylist_get(list, i);
396 OIC_LOG(ERROR, TAG, "Invalid conn type");
400 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
402 int index = CAGetAdapterIndex(connType);
406 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
410 if (g_adapterHandler[index].startListenServer != NULL)
412 g_adapterHandler[index].startListenServer();
415 OIC_LOG(DEBUG, TAG, "OUT");
419 CAResult_t CAStartDiscoveryServerAdapters()
421 OIC_LOG(DEBUG, TAG, "IN");
423 u_arraylist_t *list = CAGetSelectedNetworkList();
427 OIC_LOG(DEBUG, TAG, "No selected network");
428 return CA_STATUS_FAILED;
431 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
433 void* ptrType = u_arraylist_get(list, i);
439 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
441 int index = CAGetAdapterIndex(connType);
445 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
449 if (g_adapterHandler[index].startDiscoveryServer != NULL)
451 g_adapterHandler[index].startDiscoveryServer();
454 OIC_LOG(DEBUG, TAG, "OUT");
458 void CATerminateAdapters()
460 OIC_LOG(DEBUG, TAG, "IN");
464 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
466 if (g_adapterHandler[index].stopAdapter != NULL)
468 g_adapterHandler[index].stopAdapter();
470 if (g_adapterHandler[index].terminate != NULL)
472 g_adapterHandler[index].terminate();
476 OIC_LOG(DEBUG, TAG, "OUT");
479 CAResult_t CAReadData()
481 OIC_LOG(DEBUG, TAG, "IN");
482 u_arraylist_t *list = CAGetSelectedNetworkList();
486 return CA_STATUS_FAILED;
489 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
491 void *ptrType = u_arraylist_get(list, i);
494 OIC_LOG(ERROR, TAG, "get list fail");
495 return CA_STATUS_FAILED;
498 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
500 int index = CAGetAdapterIndex(connType);
504 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
508 if (g_adapterHandler[index].readData != NULL)
510 g_adapterHandler[index].readData();
514 OIC_LOG(DEBUG, TAG, "OUT");