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 "cableutil.h"
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 * @def TZ_BLE_CLIENT_UTIL_TAG
38 * @brief Logging tag for module name
40 #define TZ_BLE_CLIENT_UTIL_TAG "TZ_BLE_GATT_CLIENT_UTIL"
43 * @var g_numberOfServiceConnected
44 * @brief Number of services connected.
46 static int32_t g_numberOfServiceConnected = 0;
48 void CAIncrementRegisteredServiceCount()
50 g_numberOfServiceConnected++;
53 void CADecrementRegisteredServiceCount()
55 g_numberOfServiceConnected--;
58 void CAResetRegisteredServiceCount()
60 g_numberOfServiceConnected = 0;
63 int32_t CAGetRegisteredServiceCount()
65 return g_numberOfServiceConnected ;
68 CAResult_t CACreateBLEServiceInfo(const char *bdAddress, bt_gatt_attribute_h service,
69 BLEServiceInfo **bleServiceInfo)
71 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
73 VERIFY_NON_NULL(bdAddress, NULL, "Param bdAddress is NULL");
74 VERIFY_NON_NULL(service, NULL, "Param service is NULL");
75 VERIFY_NON_NULL(bleServiceInfo, NULL, "Param bleServiceInfo is NULL");
77 *bleServiceInfo = (BLEServiceInfo *) OICCalloc(1, sizeof(BLEServiceInfo));
78 if (NULL == *bleServiceInfo)
80 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "Malloc failed!");
81 return CA_STATUS_FAILED;
84 size_t len = strlen(bdAddress);
85 (*bleServiceInfo)->bdAddress = (char *) OICMalloc(sizeof(char) * (len + 1));
87 if (NULL == (*bleServiceInfo)->bdAddress)
89 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "Malloc failed!");
90 OICFree(*bleServiceInfo);
91 return CA_STATUS_FAILED;
94 strncpy((*bleServiceInfo)->bdAddress, bdAddress, len + 1);
98 int32_t ret = bt_gatt_clone_attribute_handle(&((*bleServiceInfo)->service_clone), service);
100 if (BT_ERROR_NONE != ret)
102 OIC_LOG_V(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "service handle clone failed with ret [%d]",
104 OICFree((*bleServiceInfo)->bdAddress);
105 OICFree(*bleServiceInfo);
106 return CA_STATUS_FAILED;
110 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
115 CAResult_t CAAppendBLECharInfo( bt_gatt_attribute_h characteristic, CHAR_TYPE type,
116 BLEServiceInfo *bleServiceInfo)
118 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
120 VERIFY_NON_NULL(characteristic, NULL, "Param characteristic is NULL");
121 VERIFY_NON_NULL(bleServiceInfo, NULL, "Param bleServiceInfo is NULL");
123 if (BLE_GATT_READ_CHAR == type )
125 int ret = bt_gatt_clone_attribute_handle(&((bleServiceInfo)->read_char),
127 if (BT_ERROR_NONE != ret)
129 OIC_LOG_V(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "read_char clone failed with ret [%d]",
131 return CA_STATUS_FAILED;
134 else if (BLE_GATT_WRITE_CHAR == type)
136 int ret = bt_gatt_clone_attribute_handle(&((bleServiceInfo)->write_char),
138 if (BT_ERROR_NONE != ret)
140 OIC_LOG_V(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "write_char clone failed with ret [%d]",
142 return CA_STATUS_FAILED;
146 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
151 CAResult_t CAAddBLEServiceInfoToList(BLEServiceList **serviceList,
152 BLEServiceInfo *bleServiceInfo)
155 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
157 VERIFY_NON_NULL(serviceList, NULL, "Param serviceList is NULL");
158 VERIFY_NON_NULL(bleServiceInfo, NULL, "Param bleServiceInfo is NULL");
160 BLEServiceList *node = (BLEServiceList *) OICCalloc(1, sizeof(BLEServiceList));
163 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "Malloc failed!");
164 return CA_STATUS_FAILED;
167 node->serviceInfo = bleServiceInfo;
170 if (*serviceList == NULL) // Empty list
174 else // Add at front end
176 node->next = *serviceList;
180 CAIncrementRegisteredServiceCount();
182 OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "Device [%s] added to list",
183 bleServiceInfo->bdAddress);
185 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
190 CAResult_t CARemoveBLEServiceInfoToList(BLEServiceList **serviceList,
191 BLEServiceInfo *bleServiceInfo,
192 const char *bdAddress)
195 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
197 VERIFY_NON_NULL(serviceList, NULL, "Param serviceList is NULL");
198 VERIFY_NON_NULL(*serviceList, NULL, "Param *serviceList is NULL");
199 VERIFY_NON_NULL(bdAddress, NULL, "Param bdAddress is NULL");
201 BLEServiceList *prev = NULL;
202 BLEServiceList *cur = *serviceList;
205 if (!strcasecmp(cur->serviceInfo->bdAddress, bdAddress))
207 if (cur == *serviceList)
209 *serviceList = cur->next;
212 CAFreeBLEServiceList(cur);
213 CADecrementRegisteredServiceCount();
214 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
219 prev->next = cur->next;
222 CAFreeBLEServiceList(cur);
223 CADecrementRegisteredServiceCount();
224 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
234 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, " OUT");
235 return CA_STATUS_FAILED;
238 CAResult_t CAGetBLEServiceInfo(BLEServiceList *serviceList, const char *bdAddress,
239 BLEServiceInfo **bleServiceInfo)
242 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
244 VERIFY_NON_NULL(serviceList, NULL, "Param serviceList is NULL");
245 VERIFY_NON_NULL(bleServiceInfo, NULL, "Param bleServiceInfo is NULL");
246 VERIFY_NON_NULL(bdAddress, NULL, "Param bdAddress is NULL");
249 BLEServiceList *cur = serviceList;
250 *bleServiceInfo = NULL;
253 if (!strcasecmp(cur->serviceInfo->bdAddress, bdAddress))
255 *bleServiceInfo = cur->serviceInfo;
256 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
263 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, " OUT");
264 return CA_STATUS_FAILED;
267 CAResult_t CAGetBLEServiceInfoByPosition(BLEServiceList *serviceList, int32_t position,
268 BLEServiceInfo **bleServiceInfo)
270 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
272 VERIFY_NON_NULL(serviceList, NULL, "Param serviceList is NULL");
273 VERIFY_NON_NULL(bleServiceInfo, NULL, "Param bleServiceInfo is NULL");
277 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "Position Invalid input !");
278 return CA_STATUS_INVALID_PARAM;
281 *bleServiceInfo = NULL;
283 BLEServiceList *cur = serviceList;
286 if (position == count)
288 *bleServiceInfo = cur->serviceInfo;
289 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
295 return CA_STATUS_FAILED;
298 void CAFreeBLEServiceList(BLEServiceList *serviceList)
300 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
303 BLEServiceList *temp = serviceList;
304 serviceList = serviceList->next;
305 CAFreeBLEServiceInfo(temp->serviceInfo);
308 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
311 void CAFreeBLEServiceInfo(BLEServiceInfo *bleServiceInfo)
313 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
316 if (bleServiceInfo->bdAddress)
318 bt_gatt_disconnect(bleServiceInfo->bdAddress);
319 OICFree(bleServiceInfo->bdAddress);
320 bt_gatt_destroy_attribute_handle(bleServiceInfo->service_clone);
321 bt_gatt_destroy_attribute_handle(bleServiceInfo->read_char);
322 bt_gatt_destroy_attribute_handle(bleServiceInfo->write_char);
324 OICFree(bleServiceInfo);
326 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
329 CAResult_t CAVerifyOICServiceByUUID(const char* serviceUUID)
331 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
333 VERIFY_NON_NULL(serviceUUID, NULL, "Param serviceHandle is NULL");
335 if (strcasecmp(serviceUUID, OIC_BLE_SERVICE_ID) != 0)
337 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "It is not OIC service!");
338 return CA_STATUS_FAILED;
340 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
344 CAResult_t CAVerifyOICServiceByServiceHandle(bt_gatt_attribute_h serviceHandle)
346 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN");
348 VERIFY_NON_NULL(serviceHandle, NULL, "Param serviceHandle is NULL");
351 int ret = bt_gatt_get_service_uuid(serviceHandle, &uuid);
353 if (0 != ret || NULL == uuid)
355 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "bt_gatt_get_service_uuid failed !");
356 return CA_STATUS_FAILED;
359 if (strcasecmp(uuid, OIC_BLE_SERVICE_ID) != 0)
361 OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "It is not OIC service!");
363 return CA_STATUS_FAILED;
367 OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT");
371 const char *CABTGetErrorMsg(bt_error_e err)
373 const char *errStr = NULL;
378 errStr = "BT_ERROR_NONE";
380 case BT_ERROR_CANCELLED:
381 errStr = "BT_ERROR_CANCELLED";
383 case BT_ERROR_INVALID_PARAMETER:
384 errStr = "BT_ERROR_INVALID_PARAMETER";
386 case BT_ERROR_OUT_OF_MEMORY:
387 errStr = "BT_ERROR_OUT_OF_MEMORY";
389 case BT_ERROR_RESOURCE_BUSY:
390 errStr = "BT_ERROR_RESOURCE_BUSY";
392 case BT_ERROR_TIMED_OUT:
393 errStr = "BT_ERROR_TIMED_OUT";
395 case BT_ERROR_NOW_IN_PROGRESS:
396 errStr = "BT_ERROR_NOW_IN_PROGRESS";
398 case BT_ERROR_NOT_INITIALIZED:
399 errStr = "BT_ERROR_NOT_INITIALIZED";
401 case BT_ERROR_NOT_ENABLED:
402 errStr = "BT_ERROR_NOT_ENABLED";
404 case BT_ERROR_ALREADY_DONE:
405 errStr = "BT_ERROR_ALREADY_DONE";
407 case BT_ERROR_OPERATION_FAILED:
408 errStr = "BT_ERROR_OPERATION_FAILED";
410 case BT_ERROR_NOT_IN_PROGRESS:
411 errStr = "BT_ERROR_NOT_IN_PROGRESS";
413 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
414 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
416 case BT_ERROR_AUTH_REJECTED:
417 errStr = "BT_ERROR_AUTH_REJECTED";
419 case BT_ERROR_AUTH_FAILED:
420 errStr = "BT_ERROR_AUTH_FAILED";
422 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
423 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
425 case BT_ERROR_SERVICE_SEARCH_FAILED:
426 errStr = "BT_ERROR_SERVICE_SEARCH_FAILED";
428 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
429 errStr = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
431 case BT_ERROR_PERMISSION_DENIED:
432 errStr = "BT_ERROR_PERMISSION_DENIED";
434 case BT_ERROR_SERVICE_NOT_FOUND:
435 errStr = "BT_ERROR_SERVICE_NOT_FOUND";
437 case BT_ERROR_NOT_SUPPORTED:
438 errStr = "BT_ERROR_NOT_SUPPORTED";
441 errStr = "NOT Defined";