Fixes in Tizen LE Client
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleclient_vd.c
index ebb97e6..a79a307 100644 (file)
 #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
+#define CA_GATT_CUSTOM_UUID "4209"
+#define CA_GATT_CUSTOM_UUID2 "4204"
+#define CUSTOM_UUID_LEN 4
+
+static const int samsung_code = 117;
 
 uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC;
 
@@ -158,6 +160,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;
@@ -210,6 +222,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;
@@ -310,7 +333,7 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
         g_isConnectionInProgress = false;
         oc_mutex_unlock(g_isConnectionInProgressMutex);
 
-         
+
         // TODO:Disabling scanning for now.Need to check.
         /*oc_mutex_lock(g_scanMutex);
         if (g_isMulticastInProgress || g_isUnicastScanInProgress)
@@ -383,16 +406,37 @@ 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]);
         }
         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)
+        {
+            char *compare_man_data = OICMalloc((man_data_len*2)+1);
+            int pos =0;
+            for(int i=0;i<man_data_len;i++){
+                pos += sprintf(compare_man_data+pos, "%.2x", man_data[i]);
+            }
+            compare_man_data[(man_data_len*2)+1]='\0';
+            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;
 }
 
@@ -435,19 +479,19 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s
     }
     oc_mutex_unlock(g_isScanningInProgressMutex);
 
-    LEServerInfo *serverInfo = NULL;
-    oc_mutex_lock(g_LEServerListMutex);
-    CAResult_t ret = CAGetLEServerInfo(g_LEServerList, scanInfo->remote_address, &serverInfo);
-    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] ", scanInfo->remote_address);
+        OIC_LOG_V(DEBUG, TAG, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
 
-        if (CALEIsHaveService(scanInfo, CA_GATT_SERVICE_UUID) ||
-            CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID)||
-            CALEIsHaveService(scanInfo, CA_GATT_CUSTOM_UUID2))
+        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, "Device [%s] supports OIC or custom service", scanInfo->remote_address);
+            OIC_LOG_V(DEBUG, TAG,
+                      "Newly discovered device with address [%s] ", scanInfo->remote_address);
 
             char *addr = OICStrdup(scanInfo->remote_address);
             if (NULL == addr)
@@ -474,7 +518,34 @@ 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(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_V(ERROR, TAG,
+                              "bt_gatt_client_create Failed with ret value [%s] ", CALEGetErrorMsg(ret));
+                    CALEGattDisConnect(serverInfo->remoteAddress);
+                    oc_mutex_unlock(g_LEServerListMutex);
+                    return ;
+                }
+                serverInfo->clientHandle = clientHandle;
+
+                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;
+                    CADestroyLEDataList(&serverInfo->pendingDataList);
+                    oc_mutex_unlock(g_LEServerListMutex);
+                    return ;
+                 }
+             }
+         }
     }
     oc_mutex_unlock(g_LEServerListMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
@@ -567,7 +638,7 @@ CAResult_t CAStartLEGattClient()
     return CA_STATUS_OK;
 }
 
-CAResult_t CALEClientScanThread()
+void CALEClientScanThread()
 {
     oc_mutex_lock(g_scanMutex);
     if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
@@ -577,7 +648,7 @@ CAResult_t CALEClientScanThread()
         {
             oc_mutex_unlock(g_scanMutex);
             OIC_LOG(ERROR, TAG, "CALEGattStartDeviceScanning failed");
-            return CA_STATUS_FAILED;
+            return ;
         }
         g_isUnicastScanInProgress = true;
         // Start Timer
@@ -592,7 +663,7 @@ CAResult_t CALEClientScanThread()
     oc_mutex_unlock(g_scanMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
-    return CA_STATUS_OK;
+    return ;
 }
 
 void CAStartTimerThread(void *data)
@@ -826,6 +897,16 @@ 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_startTimerCond)
     {
         g_startTimerCond = oc_cond_new();
@@ -856,6 +937,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;
 }
@@ -894,6 +986,9 @@ void CATerminateGattClientMutexVariables()
     oc_mutex_free(g_threadWriteCharacteristicMutex);
     g_threadWriteCharacteristicMutex = NULL;
 
+    oc_mutex_free(g_threadMTUChangedMutex);
+    g_threadMTUChangedMutex = NULL;
+
     oc_cond_free(g_startTimerCond);
     g_startTimerCond = NULL;
 
@@ -904,6 +999,9 @@ void CATerminateGattClientMutexVariables()
     g_threadWriteCharacteristicCond = NULL;
     g_isSignalSetFlag = false;
 
+    oc_cond_free(g_threadMTUChangedCond);
+    g_threadMTUChangedCond = NULL;
+
     OIC_LOG(DEBUG,  TAG, "OUT");
 }
 
@@ -980,6 +1078,7 @@ void CALEGattStopDeviceScanning()
             return;
         }
         g_isScanningInProgress = false;
+        g_isUnicastScanInProgress= false;
     }
     else
     {
@@ -1139,6 +1238,7 @@ exit:
 void CADiscoverLEServicesThread(void *remoteAddress)
 {
     OIC_LOG(DEBUG, TAG, "IN");
+
     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
 
     char *address  = (char *)remoteAddress;
@@ -1161,6 +1261,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
                         "remote address is NULL", CA_STATUS_FAILED);
 
     LEServerInfo *serverInfo = NULL;
+    unsigned int mtu_size;
     oc_mutex_lock(g_LEServerListMutex);
     if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo))
     {
@@ -1176,9 +1277,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
     {
         OIC_LOG_V(ERROR, TAG,
                   "bt_gatt_client_get_service Failed with ret value [%s] ", CALEGetErrorMsg(ret));
-        bt_gatt_client_destroy(serverInfo->clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
     // Server will read data on this characteristic.
@@ -1190,9 +1289,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(serverInfo->clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
     // Server will notify data on this characteristic.
@@ -1204,9 +1301,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(serverInfo->clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
     //TODO: This data has to be freed while unsetting the callback.
@@ -1214,9 +1309,7 @@ CAResult_t CALEGattDiscoverServices(const char *remoteAddress)
     if (NULL == addr)
     {
         OIC_LOG(ERROR, TAG, "addr is NULL");
-        bt_gatt_client_destroy(serverInfo->clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
     ret = bt_gatt_client_set_characteristic_value_changed_cb(readChrHandle,
@@ -1227,15 +1320,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(serverInfo->clientHandle);
-        CALEGattDisConnect(remoteAddress);
-        return CA_STATUS_FAILED;
+        goto error_exit;
     }
 
+    ret = bt_gatt_client_set_att_mtu_changed_cb(serverInfo->clientHandle,
+            CALEGattClientMTUChangedCb,
+            (void *)serverInfo->remoteAddress);
+    if (BT_ERROR_NONE != ret)
+    {
+        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;
+    }
+
+    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)
     {
@@ -1255,6 +1384,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,
@@ -1281,6 +1417,32 @@ 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", remoteAddress);
+                CAFreeLEServerInfo(serverInfo);
+                return CA_STATUS_FAILED;
+            }
+
             if (CA_STATUS_OK != CAAddLEDataToList(&serverInfo->pendingDataList, data, dataLen))
             {
                 oc_mutex_unlock(g_LEServerListMutex);
@@ -1288,8 +1450,6 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
                 return CA_STATUS_FAILED;
             }
 
-            oc_mutex_unlock(g_LEServerListMutex);
-
             oc_mutex_lock(g_scanMutex);
             if (!g_isMulticastInProgress && !g_isUnicastScanInProgress)
             {
@@ -1312,8 +1472,6 @@ CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress,
             }
             oc_mutex_unlock(g_scanMutex);
 
-            OIC_LOG(DEBUG, TAG, "OUT");
-            return CA_STATUS_OK;
         }
 
         if (serverInfo->status == LE_STATUS_DISCOVERED)
@@ -1324,7 +1482,7 @@ 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(serverInfo->remoteAddress, &clientHandle);
             if (BT_ERROR_NONE != ret || NULL == clientHandle)
@@ -1462,3 +1620,22 @@ 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);
+            OIC_LOG_V(INFO, TAG, "Test: returning Mtu Size [%d]", serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE);
+            oc_mutex_unlock(g_LEServerListMutex);
+            //return serverInfo->mtu_size;
+            return serverInfo->mtu_size - CA_BLE_MTU_HEADER_SIZE;
+        }
+    }
+    oc_mutex_unlock(g_LEServerListMutex);
+    return CA_DEFAULT_BLE_MTU_SIZE;
+
+}