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 int CAGetAdapterIndex(CATransportType_t cType)
64 OIC_LOG(DEBUG, TAG, "CA_CONNECTIVITY_TYPE_NUM is not 4");
69 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportType_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 memcpy(&g_adapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
97 OIC_LOG_V(DEBUG, TAG, "%d type adapter", cType);
98 OIC_LOG(DEBUG, TAG, "OUT");
101 static void CAReceivedPacketCallback(CARemoteEndpoint_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 static void CANetworkChangedCallback(CALocalConnectivity_t *info, CANetworkStatus_t status)
116 OIC_LOG(DEBUG, TAG, "IN");
118 // Call the callback.
119 if (g_networkChangeCallback != NULL)
121 g_networkChangeCallback(info, status);
123 OIC_LOG(DEBUG, TAG, "OUT");
126 void CAInitializeAdapters()
128 OIC_LOG(DEBUG, TAG, "IN");
130 memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_CONNECTIVITY_TYPE_NUM);
132 // Initialize adapters and register callback.
134 CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
135 #endif /* IP_ADAPTER */
138 CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
139 #endif /* EDR_ADAPTER */
142 CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback);
143 #endif /* LE_ADAPTER */
145 OIC_LOG(DEBUG, TAG, "OUT");
148 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
150 OIC_LOG(DEBUG, TAG, "IN");
152 g_networkPacketReceivedCallback = callback;
153 OIC_LOG(DEBUG, TAG, "OUT");
156 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
158 OIC_LOG(DEBUG, TAG, "IN");
160 g_networkChangeCallback = callback;
161 OIC_LOG(DEBUG, TAG, "OUT");
164 CAResult_t CAStartAdapter(CATransportType_t transportType)
166 OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
168 int index = CAGetAdapterIndex(transportType);
172 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
173 return CA_STATUS_FAILED;
176 if (g_adapterHandler[index].startAdapter != NULL)
178 g_adapterHandler[index].startAdapter();
180 OIC_LOG(DEBUG, TAG, "OUT");
184 void CAStopAdapter(CATransportType_t transportType)
186 OIC_LOG_V(DEBUG, TAG, "transportType[%d]", transportType);
188 int index = CAGetAdapterIndex(transportType);
192 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
196 if (g_adapterHandler[index].stopAdapter != NULL)
198 g_adapterHandler[index].stopAdapter();
200 OIC_LOG(DEBUG, TAG, "OUT");
203 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
205 OIC_LOG(DEBUG, TAG, "IN");
206 VERIFY_NON_NULL(info, TAG, "info");
207 VERIFY_NON_NULL(size, TAG, "size");
209 CALocalConnectivity_t *tempInfo[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
210 uint32_t tempSize[CA_CONNECTIVITY_TYPE_NUM] = { 0 };
212 CAResult_t res = CA_STATUS_FAILED;
213 // #1. get information each adapter
214 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
216 if (g_adapterHandler[index].GetnetInfo != NULL)
218 res = g_adapterHandler[index].GetnetInfo(&tempInfo[index], &tempSize[index]);
220 OIC_LOG_V (DEBUG, TAG, "%d adapter network info size is %d", index, tempSize[index]);
224 uint32_t resSize = 0;
225 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
228 if (tempInfo[index] == NULL || tempSize[index] <= 0)
234 resSize += tempSize[index];
237 OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
241 if (CA_ADAPTER_NOT_ENABLED == res || CA_NOT_SUPPORTED == res)
245 return CA_STATUS_FAILED;
248 // #3. add data into result
250 CALocalConnectivity_t *resInfo =
251 (CALocalConnectivity_t *) OICCalloc(resSize, sizeof(CALocalConnectivity_t));
252 CA_MEMORY_ALLOC_CHECK(resInfo);
255 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
258 if (tempInfo[index] == NULL || tempSize[index] <= 0)
263 memcpy(resInfo + i, tempInfo[index], sizeof(CALocalConnectivity_t) * tempSize[index]);
265 i += tempSize[index];
268 OICFree(tempInfo[index]);
275 OIC_LOG(DEBUG, TAG, "OUT");
279 // memory error label.
282 for (uint8_t index = 0; index < CA_CONNECTIVITY_TYPE_NUM; index++)
285 OICFree(tempInfo[index]);
288 return CA_MEMORY_ALLOC_FAILED;
291 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
293 OIC_LOG(DEBUG, TAG, "IN");
295 CAResult_t res = CA_STATUS_FAILED;
297 if (endpoint == NULL)
299 OIC_LOG(DEBUG, TAG, "RemoteEndpoint is NULL");
300 return CA_STATUS_INVALID_PARAM;
303 CATransportType_t type = endpoint->transportType;
305 int index = CAGetAdapterIndex(type);
309 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
310 return CA_STATUS_INVALID_PARAM;
313 uint32_t sentDataLen = 0;
314 if (g_adapterHandler[index].sendData != NULL)
316 sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
319 if (sentDataLen == length)
324 OIC_LOG(DEBUG, TAG, "OUT");
328 CAResult_t CASendMulticastData(const void *data, uint32_t length)
330 OIC_LOG(DEBUG, TAG, "IN");
332 CAResult_t res = CA_STATUS_FAILED;
333 u_arraylist_t *list = CAGetSelectedNetworkList();
337 OIC_LOG(DEBUG, TAG, "No selected network");
341 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
343 void* ptrType = u_arraylist_get(list, i);
349 CATransportType_t connType = *(CATransportType_t *) ptrType;
351 int index = CAGetAdapterIndex(connType);
355 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
359 uint32_t sentDataLen = 0;
360 if (g_adapterHandler[index].sendDataToAll != NULL)
362 sentDataLen = g_adapterHandler[index].sendDataToAll(data, length);
365 if (sentDataLen == length)
370 OIC_LOG(DEBUG, TAG, "OUT");
374 CAResult_t CAStartListeningServerAdapters()
376 OIC_LOG(DEBUG, TAG, "IN");
378 u_arraylist_t *list = CAGetSelectedNetworkList();
382 OIC_LOG(DEBUG, TAG, "No selected network");
383 return CA_STATUS_FAILED;
386 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
388 void* ptrType = u_arraylist_get(list, i);
391 OIC_LOG(ERROR, TAG, "Invalid conn type");
395 CATransportType_t connType = *(CATransportType_t *) ptrType;
397 int index = CAGetAdapterIndex(connType);
401 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
405 if (g_adapterHandler[index].startListenServer != NULL)
407 g_adapterHandler[index].startListenServer();
410 OIC_LOG(DEBUG, TAG, "OUT");
414 CAResult_t CAStartDiscoveryServerAdapters()
416 OIC_LOG(DEBUG, TAG, "IN");
418 u_arraylist_t *list = CAGetSelectedNetworkList();
422 OIC_LOG(DEBUG, TAG, "No selected network");
423 return CA_STATUS_FAILED;
426 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
428 void* ptrType = u_arraylist_get(list, i);
434 CATransportType_t connType = *(CATransportType_t *) ptrType;
436 int index = CAGetAdapterIndex(connType);
440 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
444 if (g_adapterHandler[index].startDiscoveryServer != NULL)
446 g_adapterHandler[index].startDiscoveryServer();
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 (g_adapterHandler[index].stopAdapter != NULL)
463 g_adapterHandler[index].stopAdapter();
465 if (g_adapterHandler[index].terminate != NULL)
467 g_adapterHandler[index].terminate();
471 OIC_LOG(DEBUG, TAG, "OUT");
474 CAResult_t CAReadData()
476 OIC_LOG(DEBUG, TAG, "IN");
477 u_arraylist_t *list = CAGetSelectedNetworkList();
481 return CA_STATUS_FAILED;
484 for (uint8_t i = 0; i < u_arraylist_length(list); i++)
486 void *ptrType = u_arraylist_get(list, i);
489 OIC_LOG(ERROR, TAG, "get list fail");
490 return CA_STATUS_FAILED;
493 CATransportType_t connType = *(CATransportType_t *) ptrType;
495 int index = CAGetAdapterIndex(connType);
499 OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
503 if (g_adapterHandler[index].readData != NULL)
505 g_adapterHandler[index].readData();
509 OIC_LOG(DEBUG, TAG, "OUT");