Modify Tizen BLE 3.0 / 4.0 build option
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleserver_vd.c
old mode 100755 (executable)
new mode 100644 (file)
index 272f9c1..bcfe6d4
 #include "oic_malloc.h"
 #include "caleutil.h"
 #include "caadapterutils.h"
+#include <glib.h>
+#ifndef BLE_TIZEN_40
+#include <bluetooth_internal.h>
+#endif
 
 /**
  * Logging tag for module name
@@ -121,11 +125,6 @@ static LEClientInfoList *g_LEClientList = NULL;
  */
 static oc_mutex g_LEClientListMutex = NULL;
 
-/**
- * State of connect state
- */
-static bool g_LEConnectedState = false;
-
 void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddress)
 {
     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address");
@@ -134,7 +133,6 @@ void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddr
     if (connected)
     {
         OIC_LOG_V(DEBUG, TAG, "Connected to [%s]", remoteAddress);
-        g_LEConnectedState = true;
         char *addr = OICStrdup(remoteAddress);
         oc_mutex_lock(g_LEClientListMutex);
         res  = CAAddLEClientInfoToList(&g_LEClientList, addr);
@@ -161,7 +159,6 @@ void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddr
     else
     {
         OIC_LOG_V(DEBUG, TAG, "Disconnected from [%s]", remoteAddress);
-        g_LEConnectedState = false;
         oc_mutex_lock(g_LEClientListMutex);
         CARemoveLEClientInfoFromList(&g_LEClientList, remoteAddress);
         oc_mutex_unlock(g_LEClientListMutex);
@@ -602,6 +599,7 @@ CAResult_t CAInitLEGattServer()
         {
             OIC_LOG_V(ERROR, TAG, "bt_gatt_server_create failed with ret[%s]",
                       CALEGetErrorMsg(ret));
+            bt_gatt_server_deinitialize();
             return CA_STATUS_FAILED;
         }
     }
@@ -619,7 +617,8 @@ CAResult_t CADeInitLEGattServer()
     {
         OIC_LOG_V(ERROR, TAG, "bt_gatt_server_unregister_all_services failed with ret[%s]",
                   CALEGetErrorMsg(ret));
-        return CA_STATUS_FAILED;
+        // CONPRO-1181 continue even bt API fails during DeInit
+        //return CA_STATUS_FAILED;
     }
 
     ret = bt_gatt_server_destroy(g_gattServer);
@@ -627,7 +626,8 @@ CAResult_t CADeInitLEGattServer()
     {
         OIC_LOG_V(ERROR, TAG, "bt_gatt_server_destroy failed with ret[%s]",
                   CALEGetErrorMsg(ret));
-        return CA_STATUS_FAILED;
+        // CONPRO-1181 continue even bt API fails during DeInit
+        //return CA_STATUS_FAILED;
     }
     g_gattServer = NULL;
 
@@ -636,7 +636,8 @@ CAResult_t CADeInitLEGattServer()
     {
         OIC_LOG_V(ERROR, TAG, "bt_gatt_server_deinitialize failed with ret[%s]",
                   CALEGetErrorMsg(ret));
-        return CA_STATUS_FAILED;
+        // CONPRO-1181 continue even bt API fails during DeInit
+        //return CA_STATUS_FAILED;
     }
 
     OIC_LOG(DEBUG, TAG, "OUT");
@@ -856,12 +857,16 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
 
     oc_mutex_lock(g_leCharacteristicMutex);
 
+    /**
+      * NOTE : Currently this sequence of adding characterstic works both on android and tizen.
+      * With previous logic it was faling on android in some devices giving wrong attribute
+      * handle error. This issue needs to be fixed in Android BT layer but we have applied
+      * workaround for now so that it should work with both Android and Tizen devices.
+      */
     if (read)
     {
-        g_gattReadCharPath = charPath;
-    }
-    else
-    {
+            g_gattReadCharPath = charPath;
+    } else {
         char desc_value[2] = {0, 0};  // Notification enabled.
         bt_gatt_h descriptor = NULL;
         permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE;
@@ -907,12 +912,15 @@ CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_
 
     oc_mutex_lock(g_leCharacteristicMutex);
 
-    if (!g_LEConnectedState)
+    oc_mutex_lock(g_LEClientListMutex);
+    if (CA_STATUS_FAILED == CAIsLEClientInfoInList(g_LEClientList, address))
     {
-        OIC_LOG(ERROR, TAG, "g_LEConnectedState is false");
+        OIC_LOG_V(ERROR, TAG, "%s device is not connected", address);
+        oc_mutex_unlock(g_LEClientListMutex);
         oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
+    oc_mutex_unlock(g_LEClientListMutex);
 
     if (NULL  == g_gattReadCharPath)
     {
@@ -966,13 +974,6 @@ CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uin
 
     oc_mutex_lock(g_leCharacteristicMutex);
 
-    if (!g_LEConnectedState)
-    {
-        OIC_LOG(ERROR, TAG, "g_LEConnectedState is false");
-        oc_mutex_unlock(g_leCharacteristicMutex);
-        return CA_STATUS_FAILED;
-    }
-
     if (NULL  == g_gattReadCharPath)
     {
         OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
@@ -1043,14 +1044,44 @@ uint16_t CALEServerGetMtuSize(const char* address)
     OIC_LOG(DEBUG, TAG, "IN");
     VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
 
-    unsigned int mtu;
-    int ret = bt_device_get_att_mtu(address, &mtu);
+    unsigned int mtu = CA_DEFAULT_BLE_MTU_SIZE;
+    int ret = 0;
+
+#ifdef BLE_TIZEN_40
+    bt_gatt_client_h client = NULL;
+    ret = bt_gatt_client_create(address, &client);
+    if (0 != ret)
+    {
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_client_create failed with return [%s]", CALEGetErrorMsg(ret));
+        return CA_DEFAULT_BLE_MTU_SIZE;
+    }
+
+    ret = bt_gatt_client_get_att_mtu(client, &mtu);
+    if (0 != ret)
+    {
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_client_get_att_mtu failed with return [%s]", CALEGetErrorMsg(ret));
+        return CA_DEFAULT_BLE_MTU_SIZE;
+    }
+
+    ret = bt_gatt_client_destroy(client);
+    if (0 != ret)
+    {
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_client_destroy failed with return [%s]", CALEGetErrorMsg(ret));
+        return CA_DEFAULT_BLE_MTU_SIZE;
+    }
+#else
+    ret = bt_device_get_att_mtu(address, &mtu);
     if (0 != ret)
     {
         OIC_LOG_V(ERROR, TAG,
                   "bt_device_get_att_mtu failed with return [%s]", CALEGetErrorMsg(ret));
         return CA_DEFAULT_BLE_MTU_SIZE;
     }
+#endif // BLE_TIZEN_40
+
     OIC_LOG_V(INFO, TAG, "mtu size(including header) from bt_device_get_att_mtu is %d", mtu);
     OIC_LOG(DEBUG, TAG, "OUT");
     return mtu - CA_BLE_MTU_HEADER_SIZE;