-/******************************************************************
+/* ****************************************************************
*
* Copyright 2014 Samsung Electronics All Rights Reserved.
*
#include "uarraylist.h"
#include "caqueueingthread.h"
#include "caadapterutils.h"
-#include "camsgparser.h"
+#include "cafragmentation.h"
+#include "cagattservice.h"
#include "oic_string.h"
#include "oic_malloc.h"
/**
- * @def TZ_BLE_CLIENT_TAG
- * @brief Logging tag for module name
+ * Logging tag for module name.
*/
#define TZ_BLE_CLIENT_TAG "TZ_BLE_GATT_CLIENT"
/**
- * @var BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG
- * @brief Its the constant value for characteristic descriptor from spec.
- */
-#define BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG "2902"
-
-/**
- * @var g_bLEServiceList
- * @brief This contains the list of OIC services a client connect tot.
+ * This contains the list of OIC services a client connect to.
*/
static BLEServiceList *g_bLEServiceList = NULL;
/**
- * @var g_isBleGattClientStarted
- * @brief Boolean variable to keep the state of the GATT Client.
+ * Boolean variable to keep the state of the GATT Client.
*/
static bool g_isBleGattClientStarted = false;
/**
- * @var g_bleServiceListMutex
- * @brief Mutex to synchronize access to BleServiceList.
+ * Mutex to synchronize access to BleServiceList.
*/
static ca_mutex g_bleServiceListMutex = NULL;
/**
- * @var g_bleReqRespClientCbMutex
- * @brief Mutex to synchronize access to the requestResponse callback to be called
- * when the data needs to be sent from GATTClient.
+ * Mutex to synchronize access to the requestResponse callback to be called
+ * when the data needs to be sent from GATTClient.
*/
static ca_mutex g_bleReqRespClientCbMutex = NULL;
/**
- * @var g_bleReqRespClientCbMutex
- * @brief Mutex to synchronize access to the requestResponse callback to be called
- * when the data needs to be sent from GATTClient.
+ * Mutex to synchronize access to the requestResponse callback to be called
+ * when the data needs to be sent from GATTClient.
*/
static ca_mutex g_bleClientConnectMutex = NULL;
/**
- * @var g_bleClientStateMutex
- * @brief Mutex to synchronize the calls to be done to the platform from GATTClient
- * interfaces from different threads.
+ * Mutex to synchronize the calls to be done to the platform from GATTClient
+ * interfaces from different threads.
*/
static ca_mutex g_bleClientStateMutex = NULL;
/**
- * @var g_bleServerBDAddressMutex
- * @brief Mutex to synchronize the Server BD Address update on client side.
+ * Mutex to synchronize the Server BD Address update on client side.
*/
static ca_mutex g_bleServerBDAddressMutex = NULL;
/**
- * @var g_bleClientSendCondWait
- * @brief Condition used for notifying handler the presence of data in send queue.
+ * Condition used for notifying handler the presence of data in send queue.
*/
static ca_cond g_bleClientSendCondWait = NULL;
/**
- * @var g_bleClientThreadPoolMutex
- * @brief Mutex to synchronize the task to be pushed to thread pool.
+ * Mutex to synchronize the task to be pushed to thread pool.
*/
static ca_mutex g_bleClientThreadPoolMutex = NULL;
/**
- * @var gNetworkPacketReceivedClientCallback
- * @brief Maintains the callback to be notified on receival of network packets from other
- * BLE devices
+ * Maintains the callback to be notified on receival of network packets
+ * from other BLE devices
+ */
+static CABLEDataReceivedCallback g_bleClientDataReceivedCallback = NULL;
+
+/**
+ * callback to update the error to le adapter
*/
-static CABLEClientDataReceivedCallback g_bleClientDataReceivedCallback = NULL;
+static CABLEErrorHandleCallback g_clientErrorCallback;
/**
- * @var g_eventLoop
- * @brief gmainLoop to manage the threads to receive the callback from the platfrom.
+ * gmainLoop to manage the threads to receive the callback from the platfrom.
*/
static GMainLoop *g_eventLoop = NULL;
/**
- * @var g_bleClientThreadPool
- * @brief reference to threadpool
+ * reference to threadpool.
*/
static ca_thread_pool_t g_bleClientThreadPool = NULL;
/**
- * @struct stGattServiceInfo_t
- * @brief structure to map the service attribute to BD Address.
+ * structure to map the service attribute to BD Address.
*/
typedef struct gattService
{
} stGattServiceInfo_t;
/**
- * @var g_remoteAddress
- * @brief Remote address of Gatt Server
+ * Remote address of Gatt Server.
*/
static char *g_remoteAddress = NULL;
void CABleGattCharacteristicChangedCb(bt_gatt_attribute_h characteristic,
unsigned char *value,
- int valueLen, void *userData)
+ int valueLen,
+ void *userData)
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
ca_mutex_lock(g_bleServerBDAddressMutex);
uint32_t sentLength = 0;
- g_bleClientDataReceivedCallback(g_remoteAddress, OIC_BLE_SERVICE_ID,
- value, valueLen, &sentLength);
+ g_bleClientDataReceivedCallback(g_remoteAddress, value, valueLen,
+ &sentLength);
OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Sent data Length is %d", sentLength);
ca_mutex_unlock(g_bleServerBDAddressMutex);
OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "New Characteristics[%s] of uuid[%s] is obtained",
(char *)characteristic, uuid);
- if(0 == strcasecmp(uuid, CA_BLE_READ_CHAR_UUID)) // Server will read on this characterisctics
+ if(0 == strcasecmp(uuid, CA_GATT_RESPONSE_CHRC_UUID)) // Server will read on this characterisctics
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG , "Read characteristics is obtained");
OICFree(uuid);
VERIFY_NON_NULL_RET(stTemp, TZ_BLE_CLIENT_TAG, "calloc failed!", false);
- size_t len = strlen(bdAddress);
- stTemp->address = (char *)OICMalloc(sizeof(char) * (len + 1));
+ stTemp->address = OICStrdup(bdAddress);
if (NULL == stTemp->address)
{
OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "Malloc failed!");
return false;
}
- strncpy(stTemp->address, bdAddress, len + 1);
bt_gatt_clone_attribute_handle(&(stTemp->serviceInfo), characteristic);
ca_mutex_lock(g_bleClientThreadPoolMutex);
}
ca_mutex_unlock(g_bleClientThreadPoolMutex);
}
- else if (0 == strcasecmp(uuid, CA_BLE_WRITE_CHAR_UUID)) // Server will write on this characteristics.
+ else if (0 == strcasecmp(uuid, CA_GATT_REQUEST_CHRC_UUID)) // Server will write on this characteristics.
{
OICFree(uuid);
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG , "Write characteristics is obtained");
return false;
}
}
+ else
+ {
+ OICFree(uuid);
+ OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "service_uuid characteristics is UNKNOWN");
+ return false;
+ }
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
return true;
VERIFY_NON_NULL_RET(stTemp, TZ_BLE_CLIENT_TAG, "Calloc Failed", false);
char *bdAddress = (char *)userData;
- size_t len = strlen(bdAddress);
- stTemp->address = (char *)OICMalloc(sizeof(char) * (len + 1));
+ stTemp->address = OICStrdup(bdAddress);
if (NULL == stTemp->address)
{
OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG , "Malloc failed!");
return false;
}
- strncpy(stTemp->address, bdAddress, len + 1);
-
BLEServiceInfo *bleServiceInfo = NULL;
result = CACreateBLEServiceInfo(bdAddress, service, &bleServiceInfo);
CAResult_t res = CAVerifyOICServiceByUUID(discoveryInfo->service_uuid[i]);
if (CA_STATUS_OK == res)
{
-
- size_t len = strlen(discoveryInfo->remote_address);
-
- char *addr = (char *)OICMalloc(sizeof(char) * (len + 1));
+ char *addr = OICStrdup(discoveryInfo->remote_address);
VERIFY_NON_NULL_VOID(addr, TZ_BLE_CLIENT_TAG, "Malloc failed");
- strncpy(addr, discoveryInfo->remote_address, len + 1);
OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
"Trying to do Gatt connection to [%s]", addr);
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
-void CASetBleClientThreadPoolHandle(ca_thread_pool_t handle)
+void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
-void CASetBLEReqRespClientCallback(CABLEClientDataReceivedCallback callback)
+void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback)
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
-CAResult_t CAStartBLEGattClient()
+
+void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
+{
+ g_clientErrorCallback = callback;
+}
+
+CAResult_t CAStartLEGattClient()
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
{
OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "CABleSetScanParameter Failed");
ca_mutex_unlock(g_bleClientStateMutex);
- CATerminateBLEGattClient();
+ CATerminateLEGattClient();
return;
}
{
OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "CABleGattSetCallbacks Failed");
ca_mutex_unlock(g_bleClientStateMutex);
- CATerminateBLEGattClient();
+ CATerminateLEGattClient();
return;
}
{
OIC_LOG(ERROR, TZ_BLE_CLIENT_TAG, "bt_adapter_le_start_device_discovery Failed");
ca_mutex_unlock(g_bleClientStateMutex);
- CATerminateBLEGattClient();
+ CATerminateLEGattClient();
return;
}
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
-void CAStopBLEGattClient()
+void CAStopLEGattClient()
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT");
}
-void CATerminateBLEGattClient()
+void CATerminateLEGattClient()
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
ca_mutex_lock(g_bleClientStateMutex);
VERIFY_NON_NULL_RET(remoteAddress, TZ_BLE_CLIENT_TAG,
"remote address is NULL", CA_STATUS_FAILED);
- size_t len = strlen(remoteAddress);
- char *addr = (char *)OICMalloc(sizeof(char) * (len + 1));
+ char *addr = OICStrdup(remoteAddress);
VERIFY_NON_NULL_RET(addr, TZ_BLE_CLIENT_TAG, "Malloc failed", CA_STATUS_FAILED);
- strncpy(addr, remoteAddress, len + 1);
-
int32_t ret = bt_gatt_foreach_primary_services(remoteAddress, CABleGattPrimaryServiceCb,
(void *)addr); // addr memory will be free in callback.
if (BT_ERROR_NONE != ret)
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
- VERIFY_NON_NULL_RET(service, NULL, "service is NULL", CA_STATUS_FAILED);
+ VERIFY_NON_NULL_RET(service, TZ_BLE_CLIENT_TAG, "service is NULL", CA_STATUS_FAILED);
- VERIFY_NON_NULL_RET(remoteAddress, NULL, "remoteAddress is NULL", CA_STATUS_FAILED);
+ VERIFY_NON_NULL_RET(remoteAddress, TZ_BLE_CLIENT_TAG, "remoteAddress is NULL", CA_STATUS_FAILED);
- size_t len = strlen(remoteAddress);
-
- char *addr = (char *)OICMalloc(sizeof(char) * (len + 1));
+ char *addr = OICStrdup(remoteAddress);
VERIFY_NON_NULL_RET(addr, TZ_BLE_CLIENT_TAG, "Malloc failed", CA_STATUS_FAILED);
- strncpy(addr, remoteAddress, len + 1);
int32_t ret = bt_gatt_discover_characteristics(service, CABleGattCharacteristicsDiscoveredCb,
(void *)addr); // addr will be freed in callback.
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
- VERIFY_NON_NULL_RET(service, NULL, "service is NULL", CA_STATUS_FAILED);
+ VERIFY_NON_NULL_RET(service, TZ_BLE_CLIENT_TAG, "service is NULL", CA_STATUS_FAILED);
int ret = bt_gatt_discover_characteristic_descriptor(service,
CABleGattDescriptorDiscoveredCb, NULL);
OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "desc x3 [%x]", stGattCharDescInfo->desc[3]);
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG strUUID is [%s]",
+ OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "CA_GATT_CONFIGURATION_DESC_UUID strUUID is [%s]",
strUUID);
- //if (!strncmp(strUUID, BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG, 2))
+ //if (!strncmp(strUUID, CA_GATT_CONFIGURATION_DESC_UUID, 2))
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "setting notification/indication for descriptor");
}
CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
- const char *data, const uint32_t dataLen,
- CALETransferType_t type, const int32_t position)
+ const uint8_t *data, uint32_t dataLen,
+ CALETransferType_t type, int32_t position)
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
- VERIFY_NON_NULL(data, NULL, "data is NULL");
+ VERIFY_NON_NULL(data, TZ_BLE_CLIENT_TAG, "data is NULL");
if (0 >= dataLen)
{
ca_mutex_lock(g_bleServiceListMutex);
if ( LE_UNICAST == type)
{
- VERIFY_NON_NULL(remoteAddress, NULL, "remoteAddress is NULL");
+ VERIFY_NON_NULL(remoteAddress, TZ_BLE_CLIENT_TAG, "remoteAddress is NULL");
ret = CAGetBLEServiceInfo(g_bLEServiceList, remoteAddress, &bleServiceInfo);
}
return CA_STATUS_FAILED;
}
- VERIFY_NON_NULL(bleServiceInfo, NULL, "bleServiceInfo is NULL");
+ VERIFY_NON_NULL(bleServiceInfo, TZ_BLE_CLIENT_TAG, "bleServiceInfo is NULL");
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Updating the data of length [%d] to [%s] ", dataLen,
+ OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Updating the data of length [%u] to [%s] ", dataLen,
bleServiceInfo->bdAddress);
OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG, "Updating to write char [%s]",
bleServiceInfo->read_char);
- OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_TAG,
- "Updating the data of length [%d] to [%s]", dataLen,
- bleServiceInfo->bdAddress);
-
int result = bt_gatt_set_characteristic_value(bleServiceInfo->write_char, (unsigned char *)data,
dataLen);
if (BT_ERROR_NONE != result)
return CA_STATUS_OK;
}
-CAResult_t CAUpdateCharacteristicsToAllGattServers(const char *data,
+CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data,
uint32_t dataLen)
{
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "IN");
- VERIFY_NON_NULL(data, NULL, "data is NULL");
+ VERIFY_NON_NULL(data, TZ_BLE_CLIENT_TAG, "data is NULL");
if (0 >= dataLen)
{
/*remoteAddress will be NULL.
Since we have to send to all destinations. pos will be used for getting remote address.
*/
- int32_t ret = CAUpdateCharacteristicsToGattServer(NULL, data, dataLen, LE_MULTICAST, pos);
+ CAResult_t ret = CAUpdateCharacteristicsToGattServer(NULL, data, dataLen, LE_MULTICAST, pos);
if (CA_STATUS_OK != ret)
{
OIC_LOG_V(ERROR, TZ_BLE_CLIENT_TAG,
"CAUpdateCharacteristicsToGattServer Failed with return val [%d] ", ret);
+ g_clientErrorCallback(NULL, data, dataLen, ret);
+ continue;
}
}
OIC_LOG(DEBUG, TZ_BLE_CLIENT_TAG, "OUT ");
return CA_STATUS_OK;
}
-
-