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 ******************************************************************/
28 #include<sys/socket.h>
29 #include<netinet/in.h>
32 #include "caadapterutils.h"
33 #include "oic_string.h"
34 #include "oic_malloc.h"
37 * Logging tag for module name
39 #define TAG "OIC_CA_LE_UTIL"
42 * Number of services connected.
44 static int32_t g_numberOfServiceConnected = 0;
46 /*void CAIncrementRegisteredServiceCount()
48 g_numberOfServiceConnected++;
51 CAResult_t CAAddLEDataToList(LEDataList **dataList, const void *data, uint32_t dataLength)
53 OIC_LOG(DEBUG, TAG, "IN");
55 VERIFY_NON_NULL(dataList, TAG, "Data list is null");
56 VERIFY_NON_NULL(data, TAG, "data is null");
60 OIC_LOG(ERROR, TAG, "Invalid input: data length is zero!");
61 return CA_STATUS_INVALID_PARAM;
64 LEDataList *pending_data = (LEDataList *) OICMalloc(sizeof(LEDataList));
65 if (NULL == pending_data)
67 OIC_LOG(ERROR, TAG, "OICMalloc failed (data list)!");
68 return CA_MEMORY_ALLOC_FAILED;
71 pending_data->data = (LEData *) OICMalloc(sizeof(LEData));
72 if (NULL == pending_data->data)
74 OIC_LOG(ERROR, TAG, "OICMalloc failed (data node)!");
75 OICFree(pending_data);
76 return CA_MEMORY_ALLOC_FAILED;
79 pending_data->next = NULL;
80 pending_data->data->data = (void *) OICMalloc(dataLength); //data
81 if (NULL == pending_data->data->data)
83 OIC_LOG(ERROR, TAG, "OICMalloc failed (data)!");
84 OICFree(pending_data->data);
85 OICFree(pending_data);
86 return CA_MEMORY_ALLOC_FAILED;
89 memcpy(pending_data->data->data, data, dataLength);
90 pending_data->data->dataLength = dataLength;
92 if (NULL == *dataList)
94 *dataList = pending_data;
98 LEDataList *curNode = *dataList;
99 while (curNode->next != NULL)
101 curNode = curNode->next;
103 curNode->next = pending_data;
106 OIC_LOG(DEBUG, TAG, "OUT");
111 void CARemoveLEDataFromList(LEDataList **dataList)
113 OIC_LOG(DEBUG, TAG, "IN");
115 VERIFY_NON_NULL(dataList, TAG, "Data list is null");
119 LEDataList *curNode = *dataList;
120 *dataList = (*dataList)->next;
122 //Delete the first node
123 CADestroyLEData(curNode->data);
127 OIC_LOG(DEBUG, TAG, "OUT");
130 void CADestroyLEDataList(LEDataList **dataList)
132 OIC_LOG(DEBUG, TAG, "IN");
134 VERIFY_NON_NULL_VOID(dataList, TAG, "Data list is null");
138 LEDataList *curNode = *dataList;
139 *dataList = (*dataList)->next;
141 CADestroyLEData(curNode->data);
147 OIC_LOG(DEBUG, TAG, "OUT");
150 void CADestroyLEData(LEData *data)
159 CAResult_t CAAddLEServerInfoToList(LEServerInfoList **serverList,
160 LEServerInfo *leServerInfo)
163 OIC_LOG(DEBUG, TAG, "IN");
165 VERIFY_NON_NULL(serverList, TAG, "serverList");
166 VERIFY_NON_NULL(leServerInfo, TAG, "leServerInfo");
168 LEServerInfoList *node = (LEServerInfoList *) OICCalloc(1, sizeof(LEServerInfoList));
171 OIC_LOG(ERROR, TAG, "Calloc failed!");
172 return CA_STATUS_FAILED;
175 node->serverInfo = leServerInfo;
178 if (*serverList == NULL) // Empty list
182 else // Add at front end
184 node->next = *serverList;
188 OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list",
189 leServerInfo->remoteAddress);
191 OIC_LOG(DEBUG, TAG, "OUT");
196 void CARemoveLEServerInfoFromList(LEServerInfoList **serverList,
197 const char *remoteAddress)
199 OIC_LOG(DEBUG, TAG, "IN");
200 VERIFY_NON_NULL_VOID(serverList, TAG, "serverList");
201 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remoteAddress");
203 LEServerInfoList *temp = *serverList;
204 LEServerInfoList *prev = NULL;
207 if (!strcasecmp(temp->serverInfo->remoteAddress, remoteAddress))
211 *serverList = temp->next;
215 prev->next = temp->next;
218 CAFreeLEServerInfo(temp->serverInfo);
220 OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", remoteAddress);
227 OIC_LOG(DEBUG, TAG, "OUT");
230 CAResult_t CAGetLEServerInfo(LEServerInfoList *serverList, const char *leAddress,
231 LEServerInfo **leServerInfo)
234 OIC_LOG(DEBUG, TAG, "IN");
236 VERIFY_NON_NULL(leServerInfo, TAG, "leClientInfo");
237 VERIFY_NON_NULL(leAddress, TAG, "leAddress");
239 if (NULL == serverList)
241 OIC_LOG(DEBUG, TAG, "Server list is empty");
242 return CA_STATUS_FAILED;
245 LEServerInfoList *cur = serverList;
246 *leServerInfo = NULL;
249 if (!strcasecmp(cur->serverInfo->remoteAddress , leAddress))
251 *leServerInfo = cur->serverInfo;
252 OIC_LOG(DEBUG, TAG, "OUT");
259 OIC_LOG(DEBUG, TAG, " OUT");
260 return CA_STATUS_FAILED;
265 void CAFreeLEServerList(LEServerInfoList *serverList)
267 OIC_LOG(DEBUG, TAG, "IN");
270 LEServerInfoList *temp = serverList;
271 serverList = serverList->next;
272 CAFreeLEServerInfo(temp->serverInfo);
275 OIC_LOG(DEBUG, TAG, "OUT");
278 void CAFreeLEServerInfo(LEServerInfo *leServerInfo)
280 OIC_LOG(DEBUG, TAG, "IN");
283 if (leServerInfo->clientHandle)
285 bt_gatt_client_destroy(leServerInfo->clientHandle);
288 if (leServerInfo->pendingDataList)
290 CADestroyLEDataList(&(leServerInfo->pendingDataList));
293 if (leServerInfo->status > LE_STATUS_CONNECTED)
294 { int32_t ret = bt_gatt_disconnect(leServerInfo->remoteAddress);
296 if (BT_ERROR_NONE != ret)
298 OIC_LOG_V(ERROR, TAG,
299 "bt_gatt_disconnect Failed with ret value [%d]",
304 OICFree(leServerInfo->remoteAddress);
305 OICFree(leServerInfo);
307 OIC_LOG(DEBUG, TAG, "OUT");
310 CAResult_t CAAddLEClientInfoToList(LEClientInfoList **clientList,
313 OIC_LOG(DEBUG, TAG, "IN");
314 VERIFY_NON_NULL(clientList, TAG, "clientList");
315 VERIFY_NON_NULL(clientAddress, TAG, "clientAddress");
317 LEClientInfoList *node = (LEClientInfoList *) OICCalloc(1, sizeof(LEClientInfoList));
320 OIC_LOG(ERROR, TAG, "Malloc failed!");
321 return CA_STATUS_FAILED;
324 node->remoteAddress= clientAddress;
327 if (*clientList == NULL) // Empty list
331 else // Add at front end
333 node->next = *clientList;
337 OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list", clientAddress);
338 OIC_LOG(DEBUG, TAG, "OUT");
342 CAResult_t CAIsLEClientInfoInList(LEClientInfoList *clientList,
343 const char *clientAddress)
345 OIC_LOG(DEBUG, TAG, "IN");
347 LEClientInfoList *temp = clientList;
350 if (!strcasecmp(temp->remoteAddress, clientAddress))
357 OIC_LOG(DEBUG, TAG, "OUT");
358 return CA_STATUS_FAILED;
361 void CARemoveLEClientInfoFromList(LEClientInfoList **clientList,
362 const char *clientAddress)
364 OIC_LOG(DEBUG, TAG, "IN");
365 VERIFY_NON_NULL_VOID(clientAddress, TAG, "clientAddress");
367 LEClientInfoList *temp = *clientList;
368 LEClientInfoList *prev = NULL;
371 if (!strcasecmp(temp->remoteAddress, clientAddress))
375 *clientList = temp->next;
379 prev->next = temp->next;
381 OICFree(temp->remoteAddress);
383 OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", clientAddress);
390 OIC_LOG(DEBUG, TAG, "OUT");
393 void CADisconnectAllClient(LEClientInfoList *clientList)
395 OIC_LOG(DEBUG, TAG, "IN");
398 LEClientInfoList *temp = clientList;
399 clientList = clientList->next;
400 if (temp->remoteAddress)
402 int32_t ret = bt_gatt_disconnect(temp->remoteAddress);
404 if (BT_ERROR_NONE != ret)
406 OIC_LOG_V(ERROR, TAG,
407 "bt_gatt_disconnect Failed with ret value [%d]",
411 OICFree(temp->remoteAddress);
415 OIC_LOG(DEBUG, TAG, "OUT");
418 const char *CALEGetErrorMsg(bt_error_e err)
420 const char *errStr = NULL;
425 errStr = "BT_ERROR_NONE";
427 case BT_ERROR_CANCELLED:
428 errStr = "BT_ERROR_CANCELLED";
430 case BT_ERROR_INVALID_PARAMETER:
431 errStr = "BT_ERROR_INVALID_PARAMETER";
433 case BT_ERROR_OUT_OF_MEMORY:
434 errStr = "BT_ERROR_OUT_OF_MEMORY";
436 case BT_ERROR_RESOURCE_BUSY:
437 errStr = "BT_ERROR_RESOURCE_BUSY";
439 case BT_ERROR_TIMED_OUT:
440 errStr = "BT_ERROR_TIMED_OUT";
442 case BT_ERROR_NOW_IN_PROGRESS:
443 errStr = "BT_ERROR_NOW_IN_PROGRESS";
445 case BT_ERROR_NOT_INITIALIZED:
446 errStr = "BT_ERROR_NOT_INITIALIZED";
448 case BT_ERROR_NOT_ENABLED:
449 errStr = "BT_ERROR_NOT_ENABLED";
451 case BT_ERROR_ALREADY_DONE:
452 errStr = "BT_ERROR_ALREADY_DONE";
454 case BT_ERROR_OPERATION_FAILED:
455 errStr = "BT_ERROR_OPERATION_FAILED";
457 case BT_ERROR_NOT_IN_PROGRESS:
458 errStr = "BT_ERROR_NOT_IN_PROGRESS";
460 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
461 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
463 case BT_ERROR_AUTH_REJECTED:
464 errStr = "BT_ERROR_AUTH_REJECTED";
466 case BT_ERROR_AUTH_FAILED:
467 errStr = "BT_ERROR_AUTH_FAILED";
469 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
470 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
472 case BT_ERROR_SERVICE_SEARCH_FAILED:
473 errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
475 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
476 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
478 case BT_ERROR_PERMISSION_DENIED:
479 errStr = "BT_ERROR_PERMISSION_DENIED";
481 case BT_ERROR_SERVICE_NOT_FOUND:
482 errStr = "BT_ERROR_SERVICE_NOT_FOUND";
484 case BT_ERROR_NOT_SUPPORTED:
485 errStr = "BT_ERROR_NOT_SUPPORTED";
487 case BT_ERROR_QUOTA_EXCEEDED:
488 errStr = "BT_ERROR_QUOTA_EXCEEDED";
490 case BT_ERROR_NO_DATA:
491 errStr = "BT_ERROR_NO_DATA";
494 errStr = "BT_ERROR_AGAIN";
497 errStr = "NOT Defined";