Fix -Wreturn-type build warning
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleclient.c
old mode 100644 (file)
new mode 100755 (executable)
index 87c47b8..7c5f6c8
  */
 #define TAG "OIC_CA_LE_CLIENT"
 
+#define RETRY_COUNT 1
 #define MICROSECS_PER_SEC 1000000
 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
 
+//For custom uuid ble server
+#define CA_GATT_CUSTOM_UUID "4209"
+#define CA_GATT_CUSTOM_UUID2 "4204"
+#define CUSTOM_UUID_LEN 4
+#define MICROSECS_PER_SEC 1000000
+
+static const int samsung_code = 117;
+static int g_retrycount = 0;
+static int retry_flag = 0;
 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
 
 /**
+ * Mutex to call connect only after disconnect during retry
+ */
+static oc_mutex g_isDisconnectedMutex = NULL;
+
+/**
+ * Condition for calling connect during connection retry
+ */
+static oc_cond g_LEDisconnectedCond = NULL;
+
+/**
  * Flag to check if scanning is in progress
  */
 static bool g_isScanningInProgress = false;
@@ -153,6 +173,16 @@ static oc_mutex g_threadWriteCharacteristicMutex = NULL;
 static oc_cond g_threadWriteCharacteristicCond = NULL;
 
 /**
+ * Mutex to synchronize the task for MTU Changed.
+ */
+static oc_mutex g_threadMTUChangedMutex = NULL;
+
+/**
+ * Condition for MTU Changed.
+ */
+static oc_cond g_threadMTUChangedCond = NULL;
+
+/**
  * Flag to check status of write characteristic.
  */
 static bool g_isSignalSetFlag = false;
@@ -205,6 +235,17 @@ void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
+void CALEGattClientMTUChangedCb(bt_gatt_client_h client_handle, const bt_gatt_client_att_mtu_info_s *mtu_info, void *user_data)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    oc_mutex_lock(g_threadMTUChangedMutex);
+    OIC_LOG(DEBUG, TAG, "MTU changed signal");
+    oc_cond_signal(g_threadMTUChangedCond);
+    oc_mutex_unlock(g_threadMTUChangedMutex);
+    OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+
 void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userData)
 {
     (void)reqHandle;
@@ -247,6 +288,8 @@ CAResult_t CALEGattInitiateConnection(const char *remoteAddress)
         return CA_STATUS_FAILED;
     }
     g_isConnectionInProgress = true;
+    // Set gatt connect retry count
+    g_retrycount = RETRY_COUNT;
     oc_mutex_unlock(g_isConnectionInProgressMutex);
 
     // Pause the scanning
@@ -293,9 +336,19 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
     if (!connected)
     {
         OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress);
-        oc_mutex_lock(g_LEServerListMutex);
-        CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
-        oc_mutex_unlock(g_LEServerListMutex);
+        if(!retry_flag)
+        {
+            oc_mutex_lock(g_LEServerListMutex);
+            CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress);
+            oc_mutex_unlock(g_LEServerListMutex);
+        }
+        else
+        {
+            oc_mutex_lock(g_isDisconnectedMutex);
+            oc_cond_signal(g_LEDisconnectedCond);
+            oc_mutex_unlock(g_isDisconnectedMutex);
+        }
+        retry_flag = 0;
     }
     else
     {
@@ -305,18 +358,6 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
         g_isConnectionInProgress = false;
         oc_mutex_unlock(g_isConnectionInProgressMutex);
 
-        // Resume the scanning
-        oc_mutex_lock(g_scanMutex);
-        if (g_isMulticastInProgress || g_isUnicastScanInProgress)
-        {
-            CAResult_t ret = CALEGattStartDeviceScanning();
-            if (CA_STATUS_OK != ret)
-            {
-                OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning Failed");
-            }
-        }
-        oc_mutex_unlock(g_scanMutex);
-
         LEServerInfo *serverInfo = NULL;
         oc_mutex_lock(g_LEServerListMutex);
         if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
@@ -328,7 +369,6 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
 
         serverInfo->status = LE_STATUS_CONNECTED;
         oc_mutex_unlock(g_LEServerListMutex);
-
         oc_mutex_lock(g_LEClientThreadPoolMutex);
         if (NULL == g_LEClientThreadPool)
         {
@@ -382,6 +422,39 @@ static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanI
         }
         OICFree(uuids);
     }
+
+    if(ret == false){
+        char *man_data = NULL;
+        int man_data_len;
+        int man_id;
+        result = bt_adapter_le_get_scan_result_manufacturer_data(scanInfo,
+                              pkt_type, &man_id, &man_data, &man_data_len);
+
+        if (result == BT_ERROR_NONE && NULL != man_data)
+        {
+            int pos =0;
+            char *compare_man_data = OICCalloc(1, (man_data_len*2)+1);
+            if (!compare_man_data)
+            {
+                OIC_LOG(ERROR, TAG, "Memory allocation failed for compare_man_data");
+                OICFree(man_data);
+                return false;
+            }
+
+            for(int i=0;i<man_data_len;i++){
+                pos += snprintf(compare_man_data+pos, 2, "%.2x", man_data[i]);
+            }
+
+            if (man_id == samsung_code && 0 == strncasecmp(compare_man_data, service_uuid, CUSTOM_UUID_LEN))
+            {
+                OIC_LOG_V(DEBUG, TAG, "Manufacture Data[%s] Found in %s",
+                                       compare_man_data, scanInfo->remote_address);
+                ret = true;
+            }
+            OICFree(compare_man_data);
+            OICFree(man_data);
+        }
+    }
     return ret;
 }
 
@@ -401,7 +474,6 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s
                              void *userData)
 {
     (void)userData;
-
     OIC_LOG(DEBUG, TAG, "IN");
 
     VERIFY_NON_NULL_VOID(scanInfo, TAG, "scanInfo");
@@ -425,70 +497,19 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s
     }
     oc_mutex_unlock(g_isScanningInProgressMutex);
 
-    oc_mutex_lock(g_scanMutex);
-    bool isMulticastInProgress = g_isMulticastInProgress;
-    oc_mutex_unlock(g_scanMutex);
-
-    LEServerInfo *serverInfo = NULL;
-    oc_mutex_lock(g_LEServerListMutex);
-    CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
-
-    if (CA_STATUS_OK == ret && serverInfo->status == LE_STATUS_UNICAST_PENDING)
+    if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
+        CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
+        CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
     {
-        // Stop the scan if no other device is in unicast pending
-        // state and if multicast is not in progress
-        LEServerInfoList *curNode = g_LEServerList;
-        for (; curNode; curNode = curNode->next)
-        {
-            if (curNode->serverInfo == serverInfo)
-            {
-                continue;
-            }
-            if (curNode->serverInfo->status == LE_STATUS_UNICAST_PENDING)
-            {
-                break;
-            }
-        }
-
-        if (NULL == curNode)
-        {
-            oc_mutex_lock(g_scanMutex);
-            if (!g_isMulticastInProgress && g_isUnicastScanInProgress)
-            {
-                CALEGattStopDeviceScanning();
-                g_isUnicastScanInProgress = false;
-                oc_cond_signal(g_scanningTimeCond);
-            }
-            oc_mutex_unlock(g_scanMutex);
-        }
-
-        if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
-        {
-            serverInfo->status = LE_STATUS_INVALID;
-            OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress);
-            oc_mutex_unlock(g_LEServerListMutex);
-            return;
-        }
-
-        serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
-        if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
-        {
-            serverInfo->status = LE_STATUS_DISCOVERED;
-            OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
-            oc_mutex_unlock(g_LEServerListMutex);
-            return;
-        }
-        oc_mutex_unlock(g_LEServerListMutex);
-        OIC_LOG(DEBUG, TAG, "OUT");
-        return;
-    }
+        OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
 
-    if (isMulticastInProgress)
-    {
+        LEServerInfo *serverInfo = NULL;
+        oc_mutex_lock(g_LEServerListMutex);
+        CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
         if (CA_STATUS_OK != ret)
         {
             OIC_LOG_V(DEBUG, TAG,
-                      "Newly discovered device with address [%s], adding to list", scanInfo->remote_address);
+                      "Newly discovered device with address [%s] ", scanInfo->remote_address);
 
             char *addr = OICStrdup(scanInfo->remote_address);
             if (NULL == addr)
@@ -515,52 +536,37 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s
                 CAFreeLEServerInfo(serverInfo);
                 return;
             }
+        }else {
+            OIC_LOG_V(DEBUG, TAG,
+                      "Device Present with address [%s] ", scanInfo->remote_address);
 
-            if (!CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID))
-            {
-                serverInfo->status = LE_STATUS_INVALID;
-                OIC_LOG_V(DEBUG, TAG, "Device [%s] does not support OIC service", serverInfo->remoteAddress);
-                oc_mutex_unlock(g_LEServerListMutex);
-                return;
-            }
-
-            oc_mutex_lock(g_multicastDataListMutex);
-            uint32_t lengthData = u_arraylist_length(g_multicastDataList);
-            for (uint32_t len = 0; len < lengthData; ++len)
-            {
-                LEData *multicastData = (LEData *)u_arraylist_get(g_multicastDataList, len);
-                if (NULL == multicastData)
+            if(serverInfo->status == LE_STATUS_UNICAST_PENDING){
+                bt_gatt_client_h clientHandle = NULL;
+                int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
+                if (BT_ERROR_NONE != ret || NULL == clientHandle)
                 {
-                    OIC_LOG(ERROR, TAG, "multicastData is NULL");
-                    continue;
+                    OIC_LOG_V(ERROR, TAG,
+                              "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
+                    CALEGattDisConnect(serverInfo->remoteAddress);
+                    oc_mutex_unlock(g_LEServerListMutex);
+                    return ;
                 }
-                if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList,
-                                                      multicastData->data, multicastData->dataLength))
+                serverInfo->clientHandle = clientHandle;
+
+                serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
+                if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
                 {
-                    OIC_LOG(ERROR, TAG, "Failed to add to pending list");
-                    continue;
+                    OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
+                    serverInfo->status = LE_STATUS_DISCOVERED;
+                    CADestroyLEDataList(&serverInfo->pendingDataList);
+                    oc_mutex_unlock(g_LEServerListMutex);
+                    return ;
                 }
             }
-            oc_mutex_unlock(g_multicastDataListMutex);
-        }
-
-        if (serverInfo->status == LE_STATUS_DISCOVERED)
-        {
-            // Initiate connection if not yet initiated
-            serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
-            if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
-            {
-                OIC_LOG_V(ERROR, TAG, "Could not initiate connection to [%s]", serverInfo->remoteAddress);
-                serverInfo->status = LE_STATUS_DISCOVERED;
-            }
-        }
-        else
-        {
-            OIC_LOG_V(DEBUG, TAG, "Device already discovered, status= [%d]", serverInfo->status);
         }
+        oc_mutex_unlock(g_LEServerListMutex);
     }
 
-    oc_mutex_unlock(g_LEServerListMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
@@ -635,12 +641,54 @@ CAResult_t CAStartLEGattClient()
         oc_mutex_unlock(g_LEClientThreadPoolMutex);
         return CA_STATUS_FAILED;
     }
+
+#ifdef TIZEN_VD
+    result= ca_thread_pool_add_task(g_LEClientThreadPool, CALEClientScanThread,
+                                    NULL, NULL);
+    if (CA_STATUS_OK != result)
+    {
+        OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed");
+        CATerminateGattClientMutexVariables();
+        oc_mutex_unlock(g_LEClientThreadPoolMutex);
+        return CA_STATUS_FAILED;
+    }
+#endif
     oc_mutex_unlock(g_LEClientThreadPoolMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
+#ifdef TIZEN_VD
+void CALEClientScanThread()
+{
+    oc_mutex_lock(g_scanMutex);
+    if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
+    {
+        CAResult_t result = CALEGattStartDeviceScanning();
+        if (CA_STATUS_OK != result)
+        {
+            oc_mutex_unlock(g_scanMutex);
+            OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
+            return ;
+        }
+        g_isUnicastScanInProgress = true;
+        // Start Timer
+        oc_cond_signal(g_startTimerCond);
+    }
+    else
+    {
+        g_isUnicastScanInProgress = true;
+        // Reset Timer
+        oc_cond_signal(g_scanningTimeCond);
+    }
+    oc_mutex_unlock(g_scanMutex);
+
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return ;
+}
+#endif
+
 void CAStartTimerThread(void *data)
 {
     (void)data;
@@ -872,6 +920,36 @@ CAResult_t CAInitGattClientMutexVariables()
         }
     }
 
+    if (NULL == g_threadMTUChangedMutex)
+    {
+        g_threadMTUChangedMutex = oc_mutex_new();
+        if (NULL == g_threadMTUChangedMutex)
+        {
+            OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
+            return CA_STATUS_FAILED;
+        }
+    }
+
+    if (NULL == g_isDisconnectedMutex)
+    {
+        g_isDisconnectedMutex = oc_mutex_new();
+        if (NULL == g_isDisconnectedMutex)
+        {
+            OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
+            return CA_STATUS_FAILED;
+        }
+    }
+
+    if (NULL == g_LEDisconnectedCond)
+    {
+        g_LEDisconnectedCond = oc_cond_new();
+        if (NULL == g_LEDisconnectedCond)
+        {
+            OIC_LOG(ERROR, TAG, "oc_cond_new failed");
+            return CA_STATUS_FAILED;
+        }
+    }
+
     if (NULL == g_startTimerCond)
     {
         g_startTimerCond = oc_cond_new();
@@ -902,6 +980,17 @@ CAResult_t CAInitGattClientMutexVariables()
         }
     }
 
+    if (NULL == g_threadMTUChangedCond)
+    {
+        g_threadMTUChangedCond = oc_cond_new();
+        if (NULL == g_threadMTUChangedCond)
+        {
+            OIC_LOG(ERROR, TAG, "oc_cond_new failed");
+            return CA_STATUS_FAILED;
+        }
+    }
+
+
     OIC_LOG(DEBUG,  TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -940,6 +1029,15 @@ void CATerminateGattClientMutexVariables()
     oc_mutex_free(g_threadWriteCharacteristicMutex);
     g_threadWriteCharacteristicMutex = NULL;
 
+    oc_mutex_free(g_threadMTUChangedMutex);
+    g_threadMTUChangedMutex = NULL;
+
+    oc_mutex_free(g_isDisconnectedMutex);
+    g_isDisconnectedMutex = NULL;
+
+    oc_cond_free(g_LEDisconnectedCond);
+    g_LEDisconnectedCond = NULL;
+
     oc_cond_free(g_startTimerCond);
     g_startTimerCond = NULL;
 
@@ -950,6 +1048,9 @@ void CATerminateGattClientMutexVariables()
     g_threadWriteCharacteristicCond = NULL;
     g_isSignalSetFlag = false;
 
+    oc_cond_free(g_threadMTUChangedCond);
+    g_threadMTUChangedCond = NULL;
+
     OIC_LOG(DEBUG,  TAG, "OUT");
 }
 
@@ -1026,6 +1127,7 @@ void CALEGattStopDeviceScanning()
             return;
         }
         g_isScanningInProgress = false;
+        g_isUnicastScanInProgress= false;
     }
     else
     {
@@ -1068,7 +1170,11 @@ CAResult_t CALEGattConnect(const char *remoteAddress)
     oc_mutex_lock(g_LEClientConnectMutex);
     CAResult_t result = CA_STATUS_OK;
 
+#ifdef TIZEN_VD
+    int ret = bt_gatt_connect(remoteAddress, true);
+#else
     int ret = bt_gatt_connect(remoteAddress, false);
+#endif
     if (BT_ERROR_NONE != ret)
     {
         OIC_LOG_V(ERROR, TAG, "bt_gatt_connect Failed with ret value [%s] ",
@@ -1200,6 +1306,30 @@ void CADiscoverLEServicesThread(void *remoteAddress)
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
+static int CALEWaittillDisconnect(oc_mutex mutex, oc_cond cv, int wait_seconds)
+{
+    OIC_LOG(DEBUG, TAG, "Waiting for server to be disconnected...");
+    oc_mutex_lock(mutex);
+    uint64_t wait_time = wait_seconds * MICROSECS_PER_SEC;
+    int ret = oc_cond_wait_for(cv, mutex, wait_time);
+    oc_mutex_unlock(mutex);
+    return ret;
+}
+
+static CAResult_t CALEGattConnectionRetry(const char *remoteAddress)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+
+    VERIFY_NON_NULL_RET(remoteAddress, TAG,
+                        "remote address is NULL", CA_STATUS_FAILED);
+
+    CALEGattDisConnect(remoteAddress);
+    CALEWaittillDisconnect(g_isDisconnectedMutex, g_LEDisconnectedCond, 10);
+    CAResult_t result = CALEGattConnect(remoteAddress);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return result;
+}
+
 CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
 {
     OIC_LOG(DEBUG, TAG, "IN");
@@ -1207,27 +1337,46 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
     VERIFY_NON_NULL_RET(remoteAddress, TAG,
                         "remote address is NULL", CA_STATUS_FAILED);
 
-    bt_gatt_client_h clientHandle = NULL;
-    int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
-    if (BT_ERROR_NONE != ret || NULL == clientHandle)
+    LEServerInfo *serverInfo = NULL;
+    unsigned int mtu_size;
+    oc_mutex_lock(g_LEServerListMutex);
+    if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
     {
-        OIC_LOG_V(ERROR, TAG,
-                  "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
+        oc_mutex_unlock(g_LEServerListMutex);
+        OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
         CALEGattDisConnect(remoteAddress);
         return CA_STATUS_FAILED;
     }
 
     bt_gatt_h serviceHandle = NULL;
-    ret = bt_gatt_client_get_service(clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
+    int32_t ret = bt_gatt_client_get_service(serverInfo->clientHandle, CA_GATT_SERVICE_UUID, &serviceHandle);
     if (BT_ERROR_NONE != ret || NULL == serviceHandle)
     {
         OIC_LOG_V(ERROR, TAG,
                   "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
-        bt_gatt_client_destroy(clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+
+        if(g_retrycount)
+        {
+            OIC_LOG(DEBUG, TAG, "Retry will be attempted to connect Gatt Server");
+            g_retrycount--;
+            retry_flag = 1;
+            OIC_LOG_V(DEBUG, TAG, "Retry count left %d time(s)", g_retrycount);
+            CAResult_t result = CALEGattConnectionRetry(remoteAddress);
+            if(result == CA_STATUS_FAILED)
+            {
+                goto error_exit;
+            }
+            else
+            {
+                oc_mutex_unlock(g_LEServerListMutex);
+                OIC_LOG(ERROR, TAG, "Previous connection attempt failed, attempting to retry again");
+                OIC_LOG_V(DEBUG, TAG, "Retry count left %d time(s)", g_retrycount);
+                return CA_STATUS_FAILED;
+            }
+        }
     }
 
+    retry_flag = 0;
     // Server will read data on this characteristic.
     bt_gatt_h writeChrHandle = NULL;
     ret = bt_gatt_service_get_characteristic(serviceHandle, CA_GATT_REQUEST_CHRC_UUID,
@@ -1237,9 +1386,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
         OIC_LOG_V(ERROR, TAG,
                   "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
                   CALEGetErrorMsg(ret));
-        bt_gatt_client_destroy(clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
     // Server will notify data on this characteristic.
@@ -1251,9 +1398,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
         OIC_LOG_V(ERROR, TAG,
                   "bt_gatt_service_get_characteristic Failed with ret value [%s] ",
                   CALEGetErrorMsg(ret));
-        bt_gatt_client_destroy(clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
 
@@ -1262,9 +1407,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
     if (NULL == addr)
     {
         OIC_LOG(ERROR, TAG, "addr is NULL");
-        bt_gatt_client_destroy(clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
     ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
@@ -1275,27 +1418,51 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
         OIC_LOG_V(ERROR, TAG,
                   "bt_gatt_client_set_characteristic_value_changed_cb Failed with ret value [%s]",
                   CALEGetErrorMsg(ret));
-        bt_gatt_client_destroy(clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
-    LEServerInfo *serverInfo = NULL;
-    oc_mutex_lock(g_LEServerListMutex);
-    if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
+    ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle,
+            CALEGattClientMTUChangedCb,
+            (void *)serverInfo->remoteAddress);
+    if (BT_ERROR_NONE != ret)
     {
-        oc_mutex_unlock(g_LEServerListMutex);
-        OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress);
-        bt_gatt_client_destroy(clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_client_set_att_mtu_changed_cb Failed with ret value [%s]",
+                  CALEGetErrorMsg(ret));
+        goto error_exit;
+    }
+
+    oc_mutex_lock(g_threadMTUChangedMutex);
+    ret = bt_gatt_client_request_att_mtu_change(serverInfo->clientHandle, CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE);
+    if (BT_ERROR_NONE != ret)
+    {
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_client_request_att_mtu_change Failed with ret value [%s]",
+                  CALEGetErrorMsg(ret));
+        oc_mutex_unlock(g_threadMTUChangedMutex);
+        goto error_exit;
     }
 
-    serverInfo->clientHandle = clientHandle;
+    OIC_LOG(DEBUG, TAG, "wait for callback to notify MTU Changed Callback");
+    oc_cond_wait(g_threadMTUChangedCond, g_threadMTUChangedMutex);
+    oc_mutex_unlock(g_threadMTUChangedMutex);
+
+    OIC_LOG(DEBUG, TAG, "Done MTU");
+    ret = bt_gatt_client_get_att_mtu(serverInfo->clientHandle, &mtu_size);
+    if (BT_ERROR_NONE != ret)
+    {
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_client_get_att_mtu Failed with ret value [%s]",
+                  CALEGetErrorMsg(ret));
+        goto error_exit;
+    }
+    OIC_LOG_V(DEBUG, TAG,"Negotiated MTU Size %d",mtu_size);
+
     serverInfo->serviceHandle = serviceHandle;
     serverInfo->readChar = readChrHandle;
     serverInfo->writeChar = writeChrHandle;
     serverInfo->status = LE_STATUS_SERVICES_DISCOVERED;
+    serverInfo->mtu_size = mtu_size;
 
     while (serverInfo->pendingDataList)
     {
@@ -1315,6 +1482,13 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
+
+error_exit:
+    bt_gatt_client_destroy(serverInfo->clientHandle);
+    serverInfo->clientHandle = NULL;
+    oc_mutex_unlock(g_LEServerListMutex);
+    CALEGattDisConnect(remoteAddress);
+    return CA_STATUS_FAILED;
 }
 
 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
@@ -1325,13 +1499,14 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
 
     VERIFY_NON_NULL(data, TAG, "data is NULL");
 
-    if (0 >= dataLen)
+    if (0 == dataLen)
     {
         OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input!");
         return CA_STATUS_INVALID_PARAM;
     }
 
     LEServerInfo *serverInfo = NULL;
+    bool isConnected = false;
     oc_mutex_lock(g_LEServerListMutex);
     if (LE_UNICAST == type)
     {
@@ -1348,7 +1523,6 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 OIC_LOG(ERROR, TAG, "Device address is NULL");
                 return CA_STATUS_FAILED;
             }
-
             serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
             if (NULL == serverInfo)
             {
@@ -1357,14 +1531,13 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 OICFree(addr);
                 return CA_STATUS_FAILED;
             }
-
             serverInfo->remoteAddress = addr;
             serverInfo->status = LE_STATUS_UNICAST_PENDING;
 
             if (CA_STATUS_OK != CAAddLEServerInfoToList(&g_LEServerList, serverInfo))
             {
                 oc_mutex_unlock(g_LEServerListMutex);
-                OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", serverInfo->remoteAddress);
+                OIC_LOG_V(ERROR, TAG, "Could not add [%s] to server list", remoteAddress);
                 CAFreeLEServerInfo(serverInfo);
                 return CA_STATUS_FAILED;
             }
@@ -1375,33 +1548,76 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
                 return CA_STATUS_FAILED;
             }
+        }
 
-            oc_mutex_unlock(g_LEServerListMutex);
+        if(serverInfo->status == LE_STATUS_UNICAST_PENDING)
+        {
 
-            oc_mutex_lock(g_scanMutex);
-            if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
+            int ret = bt_device_is_profile_connected(remoteAddress, BT_PROFILE_GATT, &isConnected);
+            if (BT_ERROR_NONE != ret)
             {
-                CAResult_t result = CALEGattStartDeviceScanning();
-                if (CA_STATUS_OK != result)
+                OIC_LOG_V(ERROR, TAG, "bt_device_is_profile_connected Failed with ret value [%s] ",
+                          CALEGetErrorMsg(ret));
+                oc_mutex_unlock(g_LEServerListMutex);
+                return CA_STATUS_FAILED;
+            }
+
+            if (isConnected){
+                OIC_LOG_V(DEBUG, TAG, "Already connected to address [%s]", remoteAddress);
+                serverInfo->status = LE_STATUS_CONNECTED;
+            } else {
+                oc_mutex_lock(g_scanMutex);
+                if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
+                {
+                    CAResult_t result = CALEGattStartDeviceScanning();
+                    if (CA_STATUS_OK != result)
+                    {
+                        oc_mutex_unlock(g_scanMutex);
+                        OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
+                        oc_mutex_unlock(g_LEServerListMutex);
+                        return CA_STATUS_FAILED;
+                    }
+                    g_isUnicastScanInProgress = true;
+                    // Start Timer
+                    oc_cond_signal(g_startTimerCond);
+                }
+                else
                 {
-                    oc_mutex_unlock(g_scanMutex);
-                    OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
-                    return CA_STATUS_FAILED;
+                    g_isUnicastScanInProgress = true;
+                    // Reset Timer
+                    oc_cond_signal(g_scanningTimeCond);
                 }
-                g_isUnicastScanInProgress = true;
-                // Start Timer
-                oc_cond_signal(g_startTimerCond);
+                oc_mutex_unlock(g_scanMutex);
+              }
+
+        }
+
+        if (serverInfo->status == LE_STATUS_CONNECTED)
+        {
+            oc_mutex_lock(g_LEClientThreadPoolMutex);
+            if (NULL == g_LEClientThreadPool)
+            {
+                oc_mutex_unlock(g_LEClientThreadPoolMutex);
+                OIC_LOG(ERROR, TAG, "g_LEClientThreadPool is NULL");
+                oc_mutex_unlock(g_LEServerListMutex);
+                return CA_STATUS_FAILED;
             }
-            else
+            char *addr = OICStrdup(remoteAddress);
+            if (NULL == addr)
             {
-                g_isUnicastScanInProgress = true;
-                // Reset Timer
-                oc_cond_signal(g_scanningTimeCond);
+                oc_mutex_unlock(g_LEClientThreadPoolMutex);
+                OIC_LOG(ERROR, TAG, "addr is NULL");
+                oc_mutex_unlock(g_LEServerListMutex);
+                return CA_STATUS_FAILED;
+            }
+            CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread,
+                                                     addr, NULL);
+            oc_mutex_unlock(g_LEClientThreadPoolMutex);
+            if (CA_STATUS_OK != ret)
+            {
+                OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret);
+                OICFree(addr);
             }
-            oc_mutex_unlock(g_scanMutex);
-
-            OIC_LOG(DEBUG, TAG, "OUT");
-            return CA_STATUS_OK;
         }
 
         if (serverInfo->status == LE_STATUS_DISCOVERED)
@@ -1413,6 +1629,18 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 return CA_STATUS_FAILED;
             }
 
+            bt_gatt_client_h clientHandle = NULL;
+            int32_t ret = bt_gatt_client_create(serverInfo->remoteAddress, &clientHandle);
+            if (BT_ERROR_NONE != ret || NULL == clientHandle)
+            {
+                OIC_LOG_V(ERROR, TAG,
+                          "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
+                CALEGattDisConnect(serverInfo->remoteAddress);
+                oc_mutex_unlock(g_LEServerListMutex);
+                return CA_STATUS_FAILED;
+            }
+            serverInfo->clientHandle = clientHandle;
+
             serverInfo->status = LE_STATUS_CONNECTION_INITIATED;
             if (CA_STATUS_OK != CALEGattInitiateConnection(serverInfo->remoteAddress))
             {
@@ -1458,7 +1686,7 @@ CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t
 
     VERIFY_NON_NULL(data, TAG, "data is NULL");
 
-    if (0 >= dataLen)
+    if (0 == dataLen)
     {
         OIC_LOG(ERROR, TAG, "dataLen is less than or equal zero. Invalid input !");
         return CA_STATUS_INVALID_PARAM;
@@ -1539,3 +1767,46 @@ exit:
     OIC_LOG(DEBUG, TAG, "OUT ");
     return CA_STATUS_OK;
 }
+
+uint16_t CALEClientGetMtuSize(const char* remote_address)
+{
+    LEServerInfo *serverInfo = NULL;
+    oc_mutex_lock(g_LEServerListMutex);
+    if (CA_STATUS_OK == CAGetLEServerInfo(g_LEServerList, remote_address, &serverInfo))
+    {
+        if (serverInfo->status == LE_STATUS_SERVICES_DISCOVERED){
+            OIC_LOG_V(DEBUG, TAG, "Mtu Size [%d]", serverInfo->mtu_size);
+            oc_mutex_unlock(g_LEServerListMutex);
+            OIC_LOG_V(INFO, TAG, "Returning Mtu Size [%d]", serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE);
+            return serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE;
+        }
+    }
+    oc_mutex_unlock(g_LEServerListMutex);
+    return CA_DEFAULT_BLE_MTU_SIZE;
+
+}
+
+bool CALEClientIsConnected(const char* address)
+{
+    (void)address;
+    //@Todo
+    return true;
+}
+
+CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize)
+{
+    (void)mtuSize;
+
+    VERIFY_NON_NULL(address, TAG, "address is null");
+    //@Todo
+    //it should be implemented after update Tizen 3.0
+    return CA_NOT_SUPPORTED;
+}
+
+CAResult_t CALEClientSendNegotiationMessage(const char* address)
+{
+    OIC_LOG_V(DEBUG, TAG, "CALEClientSendNegotiationMessage(%s)", address);
+    //@Todo
+    //it will be implemented when tizen public 3.0 is released.
+    return CA_NOT_SUPPORTED;
+}