Change scan and connect sequence for Tizen LE Client (VD). 52/194852/1
authoragrkush <kush.agrawal@samsung.com>
Wed, 5 Dec 2018 14:57:22 +0000 (20:27 +0530)
committerAmit KS <amit.s12@samsung.com>
Fri, 7 Dec 2018 11:18:07 +0000 (16:48 +0530)
1. Scan Will be initiated at time of starting le client.
2. Connect will be initiated before sending data.

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/355
(cherry picked from commit 1929cb2f5ba51de21343857dc30dcefa370a6f80)

Change-Id: I3d69cc621185daa4c5580ceb545ce8d4c5eebde5
Signed-off-by: agrkush <kush.agrawal@samsung.com>
Signed-off-by: Amit KS <amit.s12@samsung.com>
resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient_vd.c

index 2b9410e20a405052191e898bff6d6c5b96fd43cd..ec69d56c0cf2b8ac3b1c3af0d26856d77c7c2525 100644 (file)
 #define MICROSECS_PER_SEC 1000000
 #define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
 
+//For custom uuid ble server
+#define CA_GATT_CUSTOM_UUID "75004209-0000-0000-0000-000000000000"
+#define CA_GATT_CUSTOM_UUID2 "75004204-0000-0000-0000-000000000000"
+#define CUSTOM_UUID_LEN 9
+
 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
 
 /**
@@ -177,6 +182,7 @@ static GMainLoop *g_eventLoop = NULL;
  * Reference to threadpool
  */
 static ca_thread_pool_t g_LEClientThreadPool = NULL;
+CAResult_t CALEClientScanThread();
 
 void CALEGattCharacteristicChangedCb(bt_gatt_h characteristic,
                                      char *value,
@@ -305,8 +311,9 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
         g_isConnectionInProgress = false;
         oc_mutex_unlock(g_isConnectionInProgressMutex);
 
-        // Resume the scanning
-        oc_mutex_lock(g_scanMutex);
+         
+        // TODO:Disabling scanning for now.Need to check.
+        /*oc_mutex_lock(g_scanMutex);
         if (g_isMulticastInProgress || g_isUnicastScanInProgress)
         {
             CAResult_t ret = CALEGattStartDeviceScanning();
@@ -316,7 +323,7 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
             }
         }
         oc_mutex_unlock(g_scanMutex);
-
+*/
         LEServerInfo *serverInfo = NULL;
         oc_mutex_lock(g_LEServerListMutex);
         if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
@@ -377,6 +384,11 @@ static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanI
                 OIC_LOG_V(DEBUG, TAG, "Service[%s] Found in %s",
                           uuids[i], scanInfo->remote_address);
                 ret = true;
+            }else if(0 == strncasecmp(uuids[i], service_uuid,CUSTOM_UUID_LEN))
+            {
+                OIC_LOG_V(DEBUG, TAG, "Custom Service[%s] Found in %s",
+                          uuids[i], scanInfo->remote_address);
+                ret = true;
             }
             OICFree(uuids[i]);
         }
@@ -401,7 +413,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,81 +436,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 (CA_STATUS_OK != ret)
     {
-        // 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;
-        }
-
-        bt_gatt_client_h clientHandle = NULL;
-        int32_t ret = bt_gatt_client_create(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(remoteAddress);
-            return CA_STATUS_FAILED;
-        }
-        serverInfo->clientHandle = clientHandle;
-
-        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,
+                      "Newly discovered device with address [%s] ", scanInfo->remote_address);
 
-    if (isMulticastInProgress)
-    {
-        if (CA_STATUS_OK != ret)
+        if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
+            CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
+            CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
         {
-            OIC_LOG_V(DEBUG, TAG,
-                      "Newly discovered device with address [%s], adding to list", scanInfo->remote_address);
+            OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
 
             char *addr = OICStrdup(scanInfo->remote_address);
             if (NULL == addr)
@@ -526,51 +475,8 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s
                 CAFreeLEServerInfo(serverInfo);
                 return;
             }
-
-            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)
-                {
-                    OIC_LOG(ERROR, TAG, "multicastData is NULL");
-                    continue;
-                }
-                if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList,
-                                                      multicastData->data, multicastData->dataLength))
-                {
-                    OIC_LOG(ERROR, TAG, "Failed to add to pending list");
-                    continue;
-                }
-            }
-            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);
     OIC_LOG(DEBUG, TAG, "OUT");
 }
@@ -646,12 +552,50 @@ CAResult_t CAStartLEGattClient()
         oc_mutex_unlock(g_LEClientThreadPoolMutex);
         return CA_STATUS_FAILED;
     }
+
+    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;
+    }
     oc_mutex_unlock(g_LEClientThreadPoolMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
+CAResult_t 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 CA_STATUS_FAILED;
+        }
+        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 CA_STATUS_OK;
+}
+
 void CAStartTimerThread(void *data)
 {
     (void)data;
@@ -1196,7 +1140,6 @@ exit:
 void CADiscoverLEServicesThread(void *remoteAddress)
 {
     OIC_LOG(DEBUG, TAG, "IN");
-
     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
 
     char *address  = (char *)remoteAddress;
@@ -1229,7 +1172,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
     }
 
     bt_gatt_h serviceHandle = NULL;
-    ret = bt_gatt_client_get_service(serverInfo->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,
@@ -1267,7 +1210,6 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
         return CA_STATUS_FAILED;
     }
 
-
     //TODO: This data has to be freed while unsetting the callback.
     char *addr = OICStrdup(remoteAddress);
     if (NULL == addr)
@@ -1340,34 +1282,6 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                       "Device with address [%s] not yet found, initiating scan",
                       remoteAddress);
 
-            char *addr = OICStrdup(remoteAddress);
-            if (NULL == addr)
-            {
-                oc_mutex_unlock(g_LEServerListMutex);
-                OIC_LOG(ERROR, TAG, "Device address is NULL");
-                return CA_STATUS_FAILED;
-            }
-
-            serverInfo = (LEServerInfo *)OICCalloc(1, sizeof(LEServerInfo));
-            if (NULL == serverInfo)
-            {
-                oc_mutex_unlock(g_LEServerListMutex);
-                OIC_LOG(ERROR, TAG, "Calloc failed");
-                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);
-                CAFreeLEServerInfo(serverInfo);
-                return CA_STATUS_FAILED;
-            }
-
             if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
             {
                 oc_mutex_unlock(g_LEServerListMutex);
@@ -1411,14 +1325,14 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 OIC_LOG(ERROR, TAG, "Could not add data to pending list");
                 return CA_STATUS_FAILED;
             }
-
+            
             bt_gatt_client_h clientHandle = NULL;
-            int32_t ret = bt_gatt_client_create(remoteAddress, &clientHandle);
+            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(remoteAddress);
+                          "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
+                CALEGattDisConnect(serverInfo->remoteAddress);
                 return CA_STATUS_FAILED;
             }
             serverInfo->clientHandle = clientHandle;