ca_mutex_unlock(g_bleServiceListMutex);
+ char *uuid = NULL;
+ bt_gatt_get_service_uuid(characteristic, &uuid);
- if (BLE_GATT_READ_CHAR_INDEX == inputIndex) // Server will read on this characteristics.
- {
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "new read Characteristics is obtained [%s]",
- (char *)characteristic);
+ VERIFY_NON_NULL_RET(uuid, TZ_BLE_CLIENT_TAG, "uuid is NULL", false);
- CAResult_t retVal = CAAppendBLECharInfo(characteristic, BLE_GATT_READ_CHAR, bleServiceInfo);
- if (CA_STATUS_OK != retVal)
- {
- OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "CAAppendBLECharInfo failed ");
- return false;
- }
- char *uuid = NULL;
+ OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "New Characteristics[%s] of uuid[%s] is obtained",
+ (char *)characteristic, uuid);
- bt_gatt_get_service_uuid(characteristic, &uuid);
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "new read Characteristics uuid is obtained [%s]",
- uuid);
- }
- else if (BLE_GATT_WRITE_CHAR_INDEX == inputIndex) // Server will write on this characterisctics
+ if(0 == strcasecmp(uuid, CA_BLE_READ_CHAR_UUID)) // Server will read on this characterisctics
{
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "new write Characteristics is obtained [%s]",
- (char *)characteristic);
-
- CAResult_t retVal = CAAppendBLECharInfo(characteristic, BLE_GATT_WRITE_CHAR, bleServiceInfo);
+ OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG , "Read characteristics is obtained");
+ OICFree(uuid);
+ CAResult_t retVal = CAAppendBLECharInfo(characteristic, BLE_GATT_READ_CHAR, bleServiceInfo);
if (CA_STATUS_OK != retVal)
{
OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "CAAppendBLECharInfo failed! ");
return false;
}
- char *uuid = NULL;
-
- bt_gatt_get_service_uuid(characteristic, &uuid);
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "new write Characteristics uuid is obtained [%s]",
- uuid);
stGattServiceInfo_t *stTemp = (stGattServiceInfo_t *)OICCalloc(1,
sizeof(stGattServiceInfo_t));
}
ca_mutex_unlock(g_bleClientThreadPoolMutex);
}
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
- return true;
-}
-
-void CABtGattBondCreatedCb(int result, bt_device_info_s *device_info, void *user_data)
-{
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
- if (BT_ERROR_NONE != result)
- {
- OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
- " create_bond Failed as [%s ]", CABTGetErrorMsg(result));
- }
- else
+ else if (0 == strcasecmp(uuid, CA_BLE_WRITE_CHAR_UUID)) // Server will write on this characteristics.
{
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "bond with remote device is created.");
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Callback: The number of service : %d.",
- device_info->service_count);
-
- VERIFY_NON_NULL_VOID(device_info, TZ_BLE_CLIENT_TAG,
- "device_info is NULL");
- VERIFY_NON_NULL_VOID(device_info->remote_address, TZ_BLE_CLIENT_TAG,
- "device_info->remote_address is NULL");
-
- BLEServiceInfo *bleServiceInfo = NULL;
-
- ca_mutex_lock(g_bleServiceListMutex);
- CAResult_t retVal = CAGetBLEServiceInfo(g_bLEServiceList, device_info->remote_address,
- &bleServiceInfo);
+ OICFree(uuid);
+ OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG , "Write characteristics is obtained");
+ CAResult_t retVal = CAAppendBLECharInfo(characteristic, BLE_GATT_WRITE_CHAR, bleServiceInfo);
if (CA_STATUS_OK != retVal)
{
- OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "CAGetBLEServiceInfo failed! ");
- return;
- }
-
- VERIFY_NON_NULL_VOID(bleServiceInfo, TZ_BLE_CLIENT_TAG,
- "bleServiceInfo is NULL");
-
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "Its OIC service");
-
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
- "serviceInfo remote address [%s]", bleServiceInfo->bdAddress);
-
- stGattServiceInfo_t *stTemp = (stGattServiceInfo_t *)OICCalloc(1,
- sizeof(stGattServiceInfo_t));
-
- VERIFY_NON_NULL_VOID(stTemp, TZ_BLE_CLIENT_TAG, "malloc failed");
-
- bt_gatt_clone_attribute_handle(&(stTemp->serviceInfo), bleServiceInfo->service_clone);
-
- size_t len = strlen(bleServiceInfo->bdAddress);
- stTemp->address = (char *)OICMalloc(sizeof(char) * (len + 1));
- if (NULL == stTemp->address)
- {
- OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "Malloc failed!");
- bt_gatt_destroy_attribute_handle(stTemp->serviceInfo);
- OICFree(stTemp);
- return;
- }
-
- strncpy(stTemp->address, bleServiceInfo->bdAddress, len + 1);
-
- ca_mutex_lock(g_bleClientThreadPoolMutex);
- if (NULL == g_bleClientThreadPool)
- {
- OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "g_bleClientThreadPool is NULL");
- bt_gatt_destroy_attribute_handle(stTemp->serviceInfo);
- OICFree(stTemp->address);
- OICFree(stTemp);
- ca_mutex_unlock(g_bleClientThreadPoolMutex);
- return;
- }
-
- CAResult_t ret = ca_thread_pool_add_task(g_bleClientThreadPool,
- CADiscoverCharThread, stTemp);
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
- "ca_thread_pool_add_task failed with ret [%d]", ret);
- bt_gatt_destroy_attribute_handle(stTemp->serviceInfo);
- OICFree(stTemp->address);
- OICFree(stTemp);
- ca_mutex_unlock(g_bleClientThreadPoolMutex);
- return;
+ OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "CAAppendBLECharInfo failed ");
+ return false;
}
- ca_mutex_unlock(g_bleClientThreadPoolMutex);
-
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
- "Callback: is_bonded - %d.", device_info->is_bonded);
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
- "Callback: is_connected - %d.", device_info->is_connected);
}
+
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
+ return true;
}
bool CABleGattPrimaryServiceCb(bt_gatt_attribute_h service, int index, int count,
ca_mutex_unlock(g_bleClientThreadPoolMutex);
return false;
}
+ bt_gatt_clone_attribute_handle(&(stTemp->serviceInfo), service);
result = ca_thread_pool_add_task(g_bleClientThreadPool,
- CAGATTCreateBondThread,
+ CADiscoverCharThread,
stTemp);
if (CA_STATUS_OK != result)
{
return;
}
+ CABleGattUnSetCallbacks();
+
+ CABleGattUnWatchCharacteristicChanges();
+
+ CABleGattStopDeviceDiscovery();
+
+ g_isBleGattClientStarted = false;
+
GMainContext *context_event_loop = NULL;
// Required for waking up the thread which is running in gmain loop
if (NULL != g_eventLoop)
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
ca_mutex_lock(g_bleClientStateMutex);
+ ca_mutex_lock(g_bleServerBDAddressMutex);
OICFree(g_remoteAddress);
ca_mutex_unlock(g_bleServerBDAddressMutex);
- CABleGattUnWatchCharacteristicChanges();
-
- CABleGattUnSetCallbacks();
-
- CABleGattStopDeviceDiscovery();
-
ca_mutex_lock(g_bleServiceListMutex);
CAFreeBLEServiceList(g_bLEServiceList);
g_bLEServiceList = NULL;
CAResetRegisteredServiceCount();
- g_isBleGattClientStarted = false;
ca_mutex_unlock(g_bleClientStateMutex);
CATerminateGattClientMutexVariables();
return CA_STATUS_FAILED;
}
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "Setting bt_device_set_bond_created_cb");
-
- ret = bt_device_set_bond_created_cb(CABtGattBondCreatedCb, NULL);
- if (BT_ERROR_NONE != ret)
- {
- OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG, " bt_device_set_bond_created_cb Failed as [%s ]",
- CABTGetErrorMsg(ret));
- return CA_STATUS_FAILED;
- }
-
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
return CA_STATUS_OK;
}
bt_gatt_unset_characteristic_changed_cb();
- bt_device_unset_bond_created_cb();
-
bt_gatt_unset_connection_state_changed_cb();
bt_adapter_le_unset_device_discovery_state_changed_cb();
CAResult_t CABleGattStartDeviceDiscovery()
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
+ bool isDiscovering = false;
- int ret = bt_adapter_le_start_device_discovery();
+ int ret = bt_adapter_le_is_discovering(&isDiscovering);
if (BT_ERROR_NONE != ret)
{
- OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_start_device_discovery Failed");
+ OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_is_discovering Failed");
return CA_STATUS_FAILED;
}
+ if(!isDiscovering)
+ {
+ ret = bt_adapter_le_start_device_discovery();
+ if (BT_ERROR_NONE != ret)
+ {
+ OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_start_device_discovery Failed Ret: %d, %x", ret, ret);
+ return CA_STATUS_FAILED;
+ }
+ }
+
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
return CA_STATUS_OK;
}
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
- bt_adapter_le_stop_device_discovery();
+ bool isDiscovering = false;
+
+ int ret = bt_adapter_le_is_discovering(&isDiscovering);
+ if (BT_ERROR_NONE != ret)
+ {
+ OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_is_discovering Failed");
+ return;
+ }
+
+ if(isDiscovering)
+ {
+ ret = bt_adapter_le_stop_device_discovery();
+ if (BT_ERROR_NONE != ret)
+ {
+ OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_stop_device_discovery Failed");
+ return;
+ }
+ }
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
}
else
{
- OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
+ OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
"bt_gatt_foreach_primary_services success for address [%s]", remoteAddress);
}
return CA_STATUS_OK;
}
-void CAGATTCreateBondThread(void *stServiceInfo)
-{
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
-
- VERIFY_NON_NULL_VOID(stServiceInfo, TZ_BLE_CLIENT_TAG, "stServiceInfo is NULL");
-
- stGattServiceInfo_t *stTemp = (stGattServiceInfo_t *)stServiceInfo;
-
- VERIFY_NON_NULL_VOID(stTemp, TZ_BLE_CLIENT_TAG, "stTemp is NULL");
-
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "remote address [%s]",
- stTemp->address);
-
- CAResult_t result = CABleGATTCreateBond(stTemp->address);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG ,
- "CABleGattDiscoverCharacteristics failed!");
- OICFree(stTemp->address);
- OICFree(stTemp);
- return;
- }
- OICFree(stTemp->address);
- OICFree(stTemp);
-
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
-}
-
-CAResult_t CABleGATTCreateBond(const char *remoteAddress)
-{
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
-
- VERIFY_NON_NULL_RET(remoteAddress, TZ_BLE_CLIENT_TAG,
- "remote address is NULL", CA_STATUS_FAILED);
-
- int ret = bt_device_create_bond(remoteAddress);
-
- if (BT_ERROR_NONE != ret)
- {
- OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
- "bt_device_create_bond Failed with ret value [%d] ", ret);
- return CA_STATUS_FAILED;
- }
- else
- {
- OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
- " bt_device_create_bond query success for address [%s]", remoteAddress);
- }
- OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
-
- return CA_STATUS_OK;
-}
-
CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
const char *data, const uint32_t dataLen,
CALETransferType_t type, const int32_t position)
"Updating the data of length [%d] to [%s]", dataLen,
bleServiceInfo->bdAddress);
- int result = bt_gatt_set_characteristic_value_request(bleServiceInfo->read_char, (unsigned char *)data,
- dataLen,
- CABleGattCharacteristicWriteCb);
+ int result = bt_gatt_set_characteristic_value(bleServiceInfo->write_char, (unsigned char *)data,
+ dataLen);
if (BT_ERROR_NONE != result)
{
OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
- "bt_gatt_set_characteristic_value_request Failed with return val [%d]",
+ "bt_gatt_set_characteristic_value Failed with return val [%d]",
result);
return CA_STATUS_FAILED;
}
#define CA_BLE_SERVICE_UUID "713D0000-503E-4C75-BA94-3148F18D941E"
/**
- * @def CA_BLE_READ_CHAR_UUID
- * @brief UUID of read characteristic. This UUID is common across all platform for LE transport.
- */
-#define CA_BLE_READ_CHAR_UUID "713D0002-503E-4C75-BA94-3148F18D941E"
-
-/**
- * @def CA_BLE_WRITE_CHAR_UUID
- * @brief UUID of write characteristic. This UUID is common across all platform for LE transport.
- */
-#define CA_BLE_WRITE_CHAR_UUID "713D0003-503E-4C75-BA94-3148F18D941E"
-
-/**
* @def CA_BLE_INITIAL_BUF_SIZE
* @brief Initial buffer size for Gatt Server.
*/
return CA_STATUS_OK;
}
- GMainContext *context_event_loop = NULL;
- // Required for waking up the thread which is running in gmain loop
- if ( NULL != g_eventLoop)
- {
- context_event_loop = g_main_loop_get_context(g_eventLoop);
- }
- if (context_event_loop)
- {
- OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "g_eventLoop context %x", context_event_loop);
- g_main_context_wakeup(context_event_loop);
-
- // Kill g main loops and kill threads
- g_main_loop_quit(g_eventLoop);
- }
- else
- {
- OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_eventLoop context is NULL");
- }
-
- ca_mutex_unlock(g_bleServerStateMutex);
-
- OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-void CATerminateBleGattServer()
-{
- OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
-
- ca_mutex_lock(g_bleServerStateMutex);
-
g_isBleGattServerStarted = false;
if (NULL != g_hAdvertiser )
{
OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "_bt_gatt_deinit_service failed with ret [%d]", res);
}
+ GMainContext *context_event_loop = NULL;
+ // Required for waking up the thread which is running in gmain loop
+ if (NULL != g_eventLoop)
+ {
+ context_event_loop = g_main_loop_get_context(g_eventLoop);
+
+ if (context_event_loop)
+ {
+ OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "g_eventLoop context %x", context_event_loop);
+ g_main_context_wakeup(context_event_loop);
+
+ // Kill g main loops and kill threads
+ g_main_loop_quit(g_eventLoop);
+ }
+ }
+ else
+ {
+ OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_eventLoop context is NULL");
+ }
+
+ ca_mutex_unlock(g_bleServerStateMutex);
+
+ OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
+void CATerminateBleGattServer()
+{
+ OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN");
+
+ ca_mutex_lock(g_bleServerStateMutex);
+
// free service Path(unique identifier for ble service)
ca_mutex_lock(g_bleServiceMutex);
OICFree(g_gattSvcPath);
ca_mutex_lock(g_bleCharacteristicMutex);
- if (NULL == g_gattWriteCharPath)
+ if (NULL == g_gattReadCharPath)
{
- OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "gGattWriteCharPath is NULL");
+ OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattReadCharPath is NULL");
ca_mutex_unlock(g_bleCharacteristicMutex);
return CA_STATUS_FAILED;
}
strncpy(data, charValue, charValueLen);
OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%s] dataLen [%d]",
- (const char *)g_gattWriteCharPath, data, charValueLen);
+ (const char *)g_gattReadCharPath, data, charValueLen);
- int ret = bt_gatt_update_characteristic(g_gattWriteCharPath, data, charValueLen, address);
+ int ret = bt_gatt_update_characteristic(g_gattReadCharPath, data, charValueLen, address);
if (0 != ret)
{
OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,
ca_mutex_lock(g_bleCharacteristicMutex);
- if (NULL == g_gattWriteCharPath)
+ if (NULL == g_gattReadCharPath)
{
- OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattWriteCharPath is NULL");
+ OIC_LOG(ERROR, TZ_BLE_SERVER_TAG, "g_gattReadCharPath is NULL");
ca_mutex_unlock(g_bleCharacteristicMutex);
return CA_STATUS_FAILED;
}
strncpy(data, charValue, charValueLen + 1);
OIC_LOG_V(DEBUG, TZ_BLE_SERVER_TAG, "updating characteristics char [%s] data [%s] dataLen [%d]",
- (const char *)g_gattWriteCharPath, data, charValueLen);
+ (const char *)g_gattReadCharPath, data, charValueLen);
- int ret = bt_gatt_update_characteristic(g_gattWriteCharPath, data, charValueLen, NULL);
+ int ret = bt_gatt_update_characteristic(g_gattReadCharPath, data, charValueLen, NULL);
if (0 != ret)
{
OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG,