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_VOID(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;
328 if (*clientList == NULL) // Empty list
332 else // Add at front end
334 node->next = *clientList;
338 OIC_LOG_V(DEBUG, TAG, "Device [%s] added to list", clientAddress);
339 OIC_LOG(DEBUG, TAG, "OUT");
343 uint16_t CAClientInfoGetMTUSize(LEClientInfoList *clientList,
344 const char *clientAddress)
346 OIC_LOG(DEBUG, TAG, "IN");
348 LEClientInfoList *temp = clientList;
351 if (!strcasecmp(temp->remoteAddress, clientAddress))
353 return temp->mtuSize;
358 OIC_LOG(DEBUG, TAG, "OUT");
362 CAResult_t CAClientInfoUpdateMTUSize(LEClientInfoList *clientList,
363 const char *clientAddress,uint16_t mtu_size)
365 OIC_LOG(DEBUG, TAG, "IN");
367 LEClientInfoList *temp = clientList;
370 if (!strcasecmp(temp->remoteAddress, clientAddress))
372 temp->mtuSize = mtu_size;
378 OIC_LOG(DEBUG, TAG, "OUT");
379 return CA_STATUS_FAILED;
382 CAResult_t CAIsLEClientInfoInList(LEClientInfoList *clientList,
383 const char *clientAddress)
385 OIC_LOG(DEBUG, TAG, "IN");
387 LEClientInfoList *temp = clientList;
390 if (!strcasecmp(temp->remoteAddress, clientAddress))
397 OIC_LOG(DEBUG, TAG, "OUT");
398 return CA_STATUS_FAILED;
401 void CARemoveLEClientInfoFromList(LEClientInfoList **clientList,
402 const char *clientAddress)
404 OIC_LOG(DEBUG, TAG, "IN");
405 VERIFY_NON_NULL_VOID(clientAddress, TAG, "clientAddress");
407 LEClientInfoList *temp = *clientList;
408 LEClientInfoList *prev = NULL;
411 if (!strcasecmp(temp->remoteAddress, clientAddress))
415 *clientList = temp->next;
419 prev->next = temp->next;
421 OICFree(temp->remoteAddress);
423 OIC_LOG_V(DEBUG, TAG, "Device [%s] removed from list", clientAddress);
430 OIC_LOG(DEBUG, TAG, "OUT");
433 void CADisconnectAllClient(LEClientInfoList *clientList)
435 OIC_LOG(DEBUG, TAG, "IN");
438 LEClientInfoList *temp = clientList;
439 clientList = clientList->next;
440 if (temp->remoteAddress)
442 int32_t ret = bt_gatt_disconnect(temp->remoteAddress);
444 if (BT_ERROR_NONE != ret)
446 OIC_LOG_V(ERROR, TAG,
447 "bt_gatt_disconnect Failed with ret value [%d]",
451 OICFree(temp->remoteAddress);
455 OIC_LOG(DEBUG, TAG, "OUT");
458 const char *CALEGetErrorMsg(bt_error_e err)
460 const char *errStr = NULL;
465 errStr = "BT_ERROR_NONE";
467 case BT_ERROR_CANCELLED:
468 errStr = "BT_ERROR_CANCELLED";
470 case BT_ERROR_INVALID_PARAMETER:
471 errStr = "BT_ERROR_INVALID_PARAMETER";
473 case BT_ERROR_OUT_OF_MEMORY:
474 errStr = "BT_ERROR_OUT_OF_MEMORY";
476 case BT_ERROR_RESOURCE_BUSY:
477 errStr = "BT_ERROR_RESOURCE_BUSY";
479 case BT_ERROR_TIMED_OUT:
480 errStr = "BT_ERROR_TIMED_OUT";
482 case BT_ERROR_NOW_IN_PROGRESS:
483 errStr = "BT_ERROR_NOW_IN_PROGRESS";
485 case BT_ERROR_NOT_INITIALIZED:
486 errStr = "BT_ERROR_NOT_INITIALIZED";
488 case BT_ERROR_NOT_ENABLED:
489 errStr = "BT_ERROR_NOT_ENABLED";
491 case BT_ERROR_ALREADY_DONE:
492 errStr = "BT_ERROR_ALREADY_DONE";
494 case BT_ERROR_OPERATION_FAILED:
495 errStr = "BT_ERROR_OPERATION_FAILED";
497 case BT_ERROR_NOT_IN_PROGRESS:
498 errStr = "BT_ERROR_NOT_IN_PROGRESS";
500 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
501 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
503 case BT_ERROR_AUTH_REJECTED:
504 errStr = "BT_ERROR_AUTH_REJECTED";
506 case BT_ERROR_AUTH_FAILED:
507 errStr = "BT_ERROR_AUTH_FAILED";
509 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
510 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
512 case BT_ERROR_SERVICE_SEARCH_FAILED:
513 errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
515 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
516 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
518 case BT_ERROR_PERMISSION_DENIED:
519 errStr = "BT_ERROR_PERMISSION_DENIED";
521 case BT_ERROR_SERVICE_NOT_FOUND:
522 errStr = "BT_ERROR_SERVICE_NOT_FOUND";
524 case BT_ERROR_NOT_SUPPORTED:
525 errStr = "BT_ERROR_NOT_SUPPORTED";
527 case BT_ERROR_QUOTA_EXCEEDED:
528 errStr = "BT_ERROR_QUOTA_EXCEEDED";
530 case BT_ERROR_NO_DATA:
531 errStr = "BT_ERROR_NO_DATA";
534 errStr = "BT_ERROR_AGAIN";
537 errStr = "NOT Defined";