BLE Scan when GATT already connected. 98/221598/1
authorKush <kush.agrawal@samsung.com>
Tue, 15 Oct 2019 10:34:17 +0000 (16:04 +0530)
committerSudipto Bal <sudipto.bal@samsung.com>
Mon, 6 Jan 2020 05:43:10 +0000 (05:43 +0000)
If GATT is already connected by some other service
then Iotivity should use that GATT connection only.

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/commit/427b61effdc348e139c27fb314b44c011689e523
(cherry-picked from 427b61effdc348e139c27fb314b44c011689e523)

Change-Id: I8bd79cd7752a0312b1fa73a91cf2851c8af24c85
Signed-off-by: Kush <kush.agrawal@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 561e2bf..e52843f
@@ -1387,8 +1387,8 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
             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);
+                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;
             }
         }
@@ -1525,6 +1525,7 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
     }
 
     LEServerInfo *serverInfo = NULL;
+    bool isConnected = false;
     oc_mutex_lock(g_LEServerListMutex);
     if (LE_UNICAST == type)
     {
@@ -1570,28 +1571,72 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
 
         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);
-
         }
 
         if (serverInfo->status == LE_STATUS_DISCOVERED)
@@ -1610,6 +1655,7 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 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;