Changing logic of fetching MTU size in server size.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleserver.c
index 09f36d4..019984d 100644 (file)
@@ -27,6 +27,7 @@
 #include "oic_string.h"
 #include "oic_malloc.h"
 #include "caleutil.h"
+#include <glib.h>
 
 /**
  * Logging tag for module name
 #define TAG "OIC_CA_LE_SERVER"
 
 /**
- * Initial buffer size for Gatt Server.
- */
-#define CA_LE_INITIAL_BUF_SIZE 512
-
-/**
  * The handle of the OIC server.
  */
 static bt_gatt_server_h g_gattServer = NULL;
@@ -168,7 +164,7 @@ void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddr
     }
 }
 
-void CALEServerNotificationSentCB(int result, char *remote_address, bt_gatt_server_h server,
+void CALEServerNotificationSentCB(int result, const char *remote_address, bt_gatt_server_h server,
                                   bt_gatt_h characteristic, bool completed, void *user_data)
 {
     OIC_LOG_V(DEBUG, TAG, "Notification to the device[%s] result[%d]", remote_address, result);
@@ -209,7 +205,7 @@ CAResult_t CAStartLEGattServer()
 
     // For Read Characteristics.
     ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue,
-                                              CA_LE_INITIAL_BUF_SIZE, true);
+                                              sizeof(charReadValue), true);
     if (CA_STATUS_OK != ret)
     {
         OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
@@ -223,7 +219,7 @@ CAResult_t CAStartLEGattServer()
 
 
     ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue,
-            CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics.
+            sizeof(charWriteValue), false); // For Write Characteristics.
     if (CA_STATUS_OK != ret )
     {
         OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret);
@@ -569,6 +565,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;
         }
     }
@@ -586,7 +583,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);
@@ -594,7 +592,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;
 
@@ -603,7 +602,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");
@@ -649,8 +649,9 @@ CAResult_t CAAddNewLEServiceInGattServer(const char *serviceUUID)
     return CA_STATUS_OK;
 }
 
-void CALEGattRemoteCharacteristicWriteCb(char *remoteAddress, bt_gatt_server_h server,
-                                         bt_gatt_h charPath, int offset, char *charValue,
+void CALEGattRemoteCharacteristicWriteCb(const char *remoteAddress, int request_id,
+                                         bt_gatt_server_h server, bt_gatt_h charPath,
+                                         bool response_needed, int offset, const char *charValue,
                                          int charValueLen, void *userData)
 {
     OIC_LOG(INFO, TAG, "IN - WriteCharCB");
@@ -688,6 +689,16 @@ void CALEGattRemoteCharacteristicWriteCb(char *remoteAddress, bt_gatt_server_h s
                                     &sentLength);
     oc_mutex_unlock(g_leReqRespCbMutex);
     OICFree(data);
+
+    OIC_LOG_V(INFO, TAG, "response needed flag: %d", response_needed);
+    if (response_needed)
+    {
+        OIC_LOG(INFO, TAG, "send response to remote client");
+        bt_gatt_server_send_response(request_id,
+                                     BT_GATT_REQUEST_TYPE_WRITE, offset,
+                                     BT_ERROR_NONE, NULL, 0);
+    }
+
     OIC_LOG(INFO, TAG, "OUT - WriteCharCB");
 }
 
@@ -707,12 +718,20 @@ CAResult_t CARegisterLEServicewithGattServer(const bt_gatt_h svcPath)
         return CA_STATUS_FAILED;
     }
 
-    ret = bt_gatt_server_set_value_changed_cb(g_gattWriteCharPath,
-                                              CALEGattRemoteCharacteristicWriteCb, NULL);
+    ret = bt_gatt_server_start();
+    if (0 != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "bt_gatt_server_start failed with ret[%s]",
+                  CALEGetErrorMsg(ret));
+        return CA_STATUS_FAILED;
+    }
+
+    ret = bt_gatt_server_set_write_value_requested_cb(g_gattWriteCharPath,
+                                                      CALEGattRemoteCharacteristicWriteCb, NULL);
 
     if (0 != ret)
     {
-        OIC_LOG_V(ERROR, TAG, "bt_gatt_server_set_value_changed_cb failed with ret[%s]",
+        OIC_LOG_V(ERROR, TAG, "bt_gatt_server_set_write_value_requested_cb failed with ret[%s]",
                   CALEGetErrorMsg(ret));
         return CA_STATUS_FAILED;
     }
@@ -755,11 +774,13 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
 
     if (read)
     {
-        ret = bt_gatt_server_set_notification_state_change_cb(charPath, CALENotificationCb, NULL);
+        ret = bt_gatt_server_set_characteristic_notification_state_change_cb(charPath,
+                                                                             CALENotificationCb,
+                                                                             NULL);
         if (0 != ret)
         {
             OIC_LOG_V(ERROR, TAG,
-                      "bt_gatt_server_set_notification_state_change_cb  failed with ret[%s]",
+                      "bt_gatt_server_set_characteristic_notification_state_change_cb  failed with ret[%s]",
                       CALEGetErrorMsg(ret));
             return CA_STATUS_FAILED;
         }
@@ -844,12 +865,13 @@ CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_
         return CA_STATUS_FAILED;
     }
 
-    ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
-                                NULL);
+    ret = bt_gatt_server_notify_characteristic_changed_value(g_gattReadCharPath,
+                                                             CALEServerNotificationSentCB,
+                                                             address, NULL);
     if (0 != ret)
     {
         OIC_LOG_V(ERROR, TAG,
-                  "bt_gatt_server_notify failed with return [%s]", CALEGetErrorMsg(ret));
+                  "bt_gatt_server_notify_characteristic_changed_value failed with return [%s]", CALEGetErrorMsg(ret));
         oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
@@ -883,12 +905,13 @@ CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uin
         return CA_STATUS_FAILED;
     }
 
-    ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
-                                NULL);
+    ret = bt_gatt_server_notify_characteristic_changed_value(g_gattReadCharPath,
+                                                             CALEServerNotificationSentCB,
+                                                             NULL, NULL);
     if (0 != ret)
     {
         OIC_LOG_V(ERROR, TAG,
-                  "bt_gatt_server_notify failed with return[%s]", CALEGetErrorMsg(ret));
+                  "bt_gatt_server_notify_characteristic_changed_value failed with return[%s]", CALEGetErrorMsg(ret));
         oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
@@ -926,9 +949,35 @@ uint16_t CALEServerGetMtuSize(const char* address)
     OIC_LOG(DEBUG, TAG, "IN");
     VERIFY_NON_NULL_RET(address, TAG, "address is null",
                         CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE);
-    //@Todo
-    OIC_LOG(INFO, TAG,
-            "bt_device_get_att_mtu is not supported");
-    return CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_MTU_HEADER_SIZE;
-}
 
+    unsigned int mtu = CA_DEFAULT_BLE_MTU_SIZE;
+    int ret = 0;
+
+    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;
+    }
+
+    OIC_LOG_V(INFO, TAG, "mtu size(including header) from bt_device_get_att_mtu is %d", mtu);
+    return mtu - CA_BLE_MTU_HEADER_SIZE;
+}