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.h"
29 #include "caedradapter_singlethread.h"
30 #include "caleadapter.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 3
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 3");
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,
142 #endif /* IP_ADAPTER */
145 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
146 #endif /* EDR_ADAPTER */
149 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
151 #endif /* LE_ADAPTER */
153 OIC_LOG(DEBUG, TAG, "OUT");
156 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
158 OIC_LOG(DEBUG, TAG, "IN");
160 g_networkPacketReceivedCallback = callback;
161 OIC_LOG(DEBUG, TAG, "OUT");
164 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
166 OIC_LOG(DEBUG, TAG, "IN");
168 g_networkChangeCallback = callback;
169 OIC_LOG(DEBUG, TAG, "OUT");
172 CAResult_t CAStartAdapter(CATransportAdapter_t transportType)
174 OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
176 int index = CAGetAdapterIndex(transportType);
180 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
181 return CA_STATUS_FAILED;
184 if (g_adapterHandler[index].startAdapter != NULL)
186 g_adapterHandler[index].startAdapter();
188 OIC_LOG(DEBUG, TAG, "OUT");
192 void CAStopAdapter(CATransportAdapter_t transportType)
194 OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
196 int index = CAGetAdapterIndex(transportType);
200 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
204 if (g_adapterHandler[index].stopAdapter != NULL)
206 g_adapterHandler[index].stopAdapter();
208 OIC_LOG(DEBUG, TAG, "OUT");
211 CAResult_t CAGetNetworkInfo(CAEndpoint_t **info, uint32_t *size)
213 OIC_LOG(DEBUG, TAG, "IN");
214 VERIFY_NON_NULL(info, TAG, "info");
215 VERIFY_NON_NULL(size, TAG, "size");
217 CAEndpoint_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
218 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
220 CAResult_t res = CA_STATUS_FAILED;
221 // #1. get information each adapter
222 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
224 if (g_adapterHandler[index].GetnetInfo != NULL)
226 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
228 OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
232 uint32_t resSize = 0;
233 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
236 if (tempInfo[index] == NULL || tempSize[index] <= 0)
242 resSize += tempSize[index];
245 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
249 if (CA_ADAPTER_NOT_ENABLED == res || CA_NOT_SUPPORTED == res)
253 return CA_STATUS_FAILED;
256 // #3. add data into result
258 CAEndpoint_t *resInfo = (CAEndpoint_t *)OICCalloc(resSize, sizeof(CAEndpoint_t));
259 CA_MEMORY_ALLOC_CHECK(resInfo);
262 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
265 if (tempInfo[index] == NULL || tempSize[index] <= 0)
270 memcpy(resInfo + i, tempInfo[index], sizeof(CAEndpoint_t) * tempSize[index]);
272 i += tempSize[index];
275 OICFree(tempInfo[index]);
282 OIC_LOG(DEBUG, TAG, "OUT");
286 // memory error label.
289 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
292 OICFree(tempInfo[index]);
295 return CA_MEMORY_ALLOC_FAILED;
298 CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
300 OIC_LOG(DEBUG, TAG, "IN");
302 CAResult_t res = CA_STATUS_FAILED;
304 if (endpoint == NULL)
306 OIC_LOG(DEBUG, TAG, "Endpoint is NULL");
307 return CA_STATUS_INVALID_PARAM;
310 CATransportAdapter_t type = endpoint->adapter;
312 int index = CAGetAdapterIndex(type);
316 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
317 return CA_STATUS_INVALID_PARAM;
320 uint32_t sentDataLen = 0;
321 if (g_adapterHandler[index].sendData != NULL)
323 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
326 if (sentDataLen == length)
331 OIC_LOG(DEBUG, TAG, "OUT");
335 CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length)
337 OIC_LOG(DEBUG, TAG, "IN");
339 CAResult_t res = CA_STATUS_FAILED;
340 u_arraylist_t *list = CAGetSelectedNetworkList();
344 OIC_LOG(DEBUG, TAG, "No selected network");
348 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
350 void* ptrType = u_arraylist_get(list, i);
356 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
358 int index = CAGetAdapterIndex(connType);
362 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
366 uint32_t sentDataLen = 0;
367 if (g_adapterHandler[index].sendDataToAll != NULL)
369 sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, data, length);
372 if (sentDataLen == length)
377 OIC_LOG(DEBUG, TAG, "OUT");
381 CAResult_t CAStartListeningServerAdapters()
383 OIC_LOG(DEBUG, TAG, "IN");
385 u_arraylist_t *list = CAGetSelectedNetworkList();
389 OIC_LOG(DEBUG, TAG, "No selected network");
390 return CA_STATUS_FAILED;
393 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
395 void* ptrType = u_arraylist_get(list, i);
398 OIC_LOG(ERROR, TAG, "Invalid conn type");
402 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
404 int index = CAGetAdapterIndex(connType);
408 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
412 if (g_adapterHandler[index].startListenServer != NULL)
414 g_adapterHandler[index].startListenServer();
417 OIC_LOG(DEBUG, TAG, "OUT");
421 CAResult_t CAStartDiscoveryServerAdapters()
423 OIC_LOG(DEBUG, TAG, "IN");
425 u_arraylist_t *list = CAGetSelectedNetworkList();
429 OIC_LOG(DEBUG, TAG, "No selected network");
430 return CA_STATUS_FAILED;
433 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
435 void* ptrType = u_arraylist_get(list, i);
441 CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType;
443 int index = CAGetAdapterIndex(connType);
447 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
451 if (g_adapterHandler[index].startDiscoveryServer != NULL)
453 g_adapterHandler[index].startDiscoveryServer();
456 OIC_LOG(DEBUG, TAG, "OUT");
460 void CATerminateAdapters()
462 OIC_LOG(DEBUG, TAG, "IN");
466 for (index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
468 if (g_adapterHandler[index].stopAdapter != NULL)
470 g_adapterHandler[index].stopAdapter();
472 if (g_adapterHandler[index].terminate != NULL)
474 g_adapterHandler[index].terminate();
478 OIC_LOG(DEBUG, TAG, "OUT");
481 CAResult_t CAReadData()
483 OIC_LOG(DEBUG, TAG, "IN");
484 u_arraylist_t *list = CAGetSelectedNetworkList();
488 return CA_STATUS_FAILED;
491 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
493 void *ptrType = u_arraylist_get(list, i);
496 OIC_LOG(ERROR, TAG, "get list fail");
497 return CA_STATUS_FAILED;
500 CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType;
502 int index = CAGetAdapterIndex(connType);
506 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
510 if (g_adapterHandler[index].readData != NULL)
512 g_adapterHandler[index].readData();
516 OIC_LOG(DEBUG, TAG, "OUT");