Changing logic of fetching MTU size in server size.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / caleserver.c
index 0fa538b..019984d 100644 (file)
 #include "caleserver.h"
 #include "cacommon.h"
 #include "cacommonutil.h"
-#include "camutex.h"
+#include "octhread.h"
 #include "caqueueingthread.h"
 #include "cagattservice.h"
 #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;
@@ -82,28 +78,28 @@ static bool g_isLEGattServerStarted = false;
 /**
  * Mutex to synchronize the calls to start and stop server.
  */
-static ca_mutex g_leServerStateMutex = NULL;
+static oc_mutex g_leServerStateMutex = NULL;
 
 /**
  * Mutex to synchronize writing operations on the characteristics.
  */
-static  ca_mutex g_leCharacteristicMutex = NULL;
+static  oc_mutex g_leCharacteristicMutex = NULL;
 
 /**
  * Mutex to synchronize to creation of OIC service.
  */
-static  ca_mutex g_leServiceMutex = NULL;
+static  oc_mutex g_leServiceMutex = NULL;
 
 /**
  * Mutex to synchronize access to the requestResponse callback to be called
  * when the data needs to be sent from GATTClient.
  */
-static  ca_mutex g_leReqRespCbMutex = NULL;
+static  oc_mutex g_leReqRespCbMutex = NULL;
 
 /**
  * Mutex to synchronize the task to be pushed to thread pool.
  */
-static ca_mutex g_leServerThreadPoolMutex = NULL;
+static oc_mutex g_leServerThreadPoolMutex = NULL;
 
 /**
  * Reference to threadpool.
@@ -123,37 +119,52 @@ static LEClientInfoList *g_LEClientList = NULL;
 /**
  * Mutex to synchronize access to LE ClientList.
  */
-static ca_mutex g_LEClientListMutex = NULL;
+static oc_mutex g_LEClientListMutex = NULL;
 
 void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddress)
 {
     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address");
 
+    CAResult_t res = CA_STATUS_OK;
     if (connected)
     {
         OIC_LOG_V(DEBUG, TAG, "Connected to [%s]", remoteAddress);
         char *addr = OICStrdup(remoteAddress);
-        ca_mutex_lock(g_LEClientListMutex);
-        CAResult_t result  = CAAddLEClientInfoToList(&g_LEClientList, addr);
-        if (CA_STATUS_OK != result)
+        oc_mutex_lock(g_LEClientListMutex);
+        res  = CAAddLEClientInfoToList(&g_LEClientList, addr);
+        if (CA_STATUS_OK != res)
         {
             OIC_LOG(ERROR, TAG, "CAAddLEClientInfoToList failed");
-            ca_mutex_unlock(g_LEClientListMutex);
+            oc_mutex_unlock(g_LEClientListMutex);
             OICFree(addr);
             return;
         }
-        ca_mutex_unlock(g_LEClientListMutex);
+        oc_mutex_unlock(g_LEClientListMutex);
+
+        res = CALEStopAdvertise();
+        if (CA_STATUS_OK != res)
+        {
+            OIC_LOG_V(ERROR, TAG, "Failed to stop advertising [%d]", res);
+            return;
+        }
     }
     else
     {
         OIC_LOG_V(DEBUG, TAG, "Disconnected from [%s]", remoteAddress);
-        ca_mutex_lock(g_LEClientListMutex);
+        oc_mutex_lock(g_LEClientListMutex);
         CARemoveLEClientInfoFromList(&g_LEClientList, remoteAddress);
-        ca_mutex_unlock(g_LEClientListMutex);
+        oc_mutex_unlock(g_LEClientListMutex);
+
+        res = CALEStartAdvertise();
+        if (CA_STATUS_OK != res)
+        {
+            OIC_LOG_V(ERROR, TAG, "Failed to start advertising [%d]", res);
+            return;
+        }
     }
 }
 
-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);
@@ -163,11 +174,11 @@ CAResult_t CAStartLEGattServer()
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    ca_mutex_lock(g_leServerStateMutex);
+    oc_mutex_lock(g_leServerStateMutex);
     if (true == g_isLEGattServerStarted)
     {
         OIC_LOG(ERROR, TAG, "Gatt Server is already running");
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         return CA_STATUS_OK;
     }
 
@@ -175,18 +186,16 @@ CAResult_t CAStartLEGattServer()
     if (CA_STATUS_OK != ret)
     {
         OIC_LOG_V(ERROR, TAG, "CAInitLEGattServer failed[%d]", ret);
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         CATerminateLEGattServer();
         return CA_STATUS_FAILED;
     }
 
-    char *serviceUUID = CA_GATT_SERVICE_UUID;
-
-    ret  = CAAddNewLEServiceInGattServer(serviceUUID);
+    ret  = CAAddNewLEServiceInGattServer(CA_GATT_SERVICE_UUID);
     if (CA_STATUS_OK != ret)
     {
         OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret);
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         CATerminateLEGattServer();
         return CA_STATUS_FAILED;
     }
@@ -196,11 +205,11 @@ 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);
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         CATerminateLEGattServer();
         return CA_STATUS_FAILED;
     }
@@ -210,11 +219,11 @@ 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);
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         CATerminateLEGattServer();
         return CA_STATUS_FAILED;
     }
@@ -223,23 +232,23 @@ CAResult_t CAStartLEGattServer()
     if (CA_STATUS_OK != ret )
     {
         OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret);
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         CATerminateLEGattServer();
         return CA_STATUS_FAILED;
     }
 
-    ret = CALEStartAdvertise(serviceUUID);
+    ret = CALEStartAdvertise();
     if (CA_STATUS_OK != ret)
     {
         OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret);
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         CATerminateLEGattServer();
         return CA_STATUS_FAILED;
     }
 
     g_isLEGattServerStarted = true;
 
-    ca_mutex_unlock(g_leServerStateMutex);
+    oc_mutex_unlock(g_leServerStateMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
@@ -249,14 +258,35 @@ void CALENotificationCb(bool notify, bt_gatt_server_h server, bt_gatt_h gatt_han
                         void *user_data)
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    if(notify)
+    if (notify)
     {
         OIC_LOG(DEBUG, TAG, "Notification is subscribed by the client");
+        CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
+                           true, "notifyChar success");
+    }
+    else
+    {
+        CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, "", 0, -1,
+                           false, "notifyChar failure");
+    }
+    OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+CAResult_t CALEStartAdvertise()
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+
+    CAResult_t res = CALEStartAdvertiseImpl(CA_GATT_SERVICE_UUID);
+    if (CA_STATUS_OK != res)
+    {
+        OIC_LOG_V(ERROR, TAG, "CALEStartAdvertiseImpl failed[%d]", res);
     }
+
     OIC_LOG(DEBUG, TAG, "OUT");
+    return res;
 }
 
-CAResult_t CALEStartAdvertise(const char *serviceUUID)
+CAResult_t CALEStartAdvertiseImpl(const char *serviceUUID)
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
@@ -332,21 +362,21 @@ CAResult_t CAStopLEGattServer()
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    ca_mutex_lock(g_leServerStateMutex);
+    oc_mutex_lock(g_leServerStateMutex);
 
     if (false == g_isLEGattServerStarted)
     {
         OIC_LOG(ERROR, TAG, "Gatt Server is not running to stop");
-        ca_mutex_unlock(g_leServerStateMutex);
+        oc_mutex_unlock(g_leServerStateMutex);
         return CA_STATUS_OK;
     }
 
     g_isLEGattServerStarted = false;
 
-    ca_mutex_lock(g_LEClientListMutex);
+    oc_mutex_lock(g_LEClientListMutex);
     CADisconnectAllClient(g_LEClientList);
     g_LEClientList = NULL;
-    ca_mutex_unlock(g_LEClientListMutex);
+    oc_mutex_unlock(g_LEClientListMutex);
 
     CAResult_t res = CALEStopAdvertise();
     {
@@ -380,7 +410,7 @@ CAResult_t CAStopLEGattServer()
         OIC_LOG(ERROR, TAG, "g_eventLoop context is NULL");
     }
 
-    ca_mutex_unlock(g_leServerStateMutex);
+    oc_mutex_unlock(g_leServerStateMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
@@ -406,18 +436,18 @@ void CATerminateLEGattServer()
     OIC_LOG(DEBUG, TAG, "IN");
 
     // Service and characteristics path will be freed by the platform.
-    ca_mutex_lock(g_leServiceMutex);
+    oc_mutex_lock(g_leServiceMutex);
     g_gattSvcPath = NULL;
-    ca_mutex_unlock(g_leServiceMutex);
+    oc_mutex_unlock(g_leServiceMutex);
 
-    ca_mutex_lock(g_leCharacteristicMutex);
+    oc_mutex_lock(g_leCharacteristicMutex);
     g_gattReadCharPath = NULL;
     g_gattWriteCharPath = NULL;
-    ca_mutex_unlock(g_leCharacteristicMutex);
+    oc_mutex_unlock(g_leCharacteristicMutex);
 
-    ca_mutex_lock(g_leServerThreadPoolMutex);
+    oc_mutex_lock(g_leServerThreadPoolMutex);
     g_leServerThreadPool = NULL;
-    ca_mutex_unlock(g_leServerThreadPoolMutex);
+    oc_mutex_unlock(g_leServerThreadPoolMutex);
 
     // Terminating all mutex variables.
     CATerminateGattServerMutexVariables();
@@ -429,60 +459,60 @@ CAResult_t CAInitGattServerMutexVariables()
     OIC_LOG(DEBUG, TAG, "IN");
     if (NULL == g_leServerStateMutex)
     {
-        g_leServerStateMutex = ca_mutex_new();
+        g_leServerStateMutex = oc_mutex_new();
         if (NULL == g_leServerStateMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
 
     if (NULL == g_leServiceMutex)
     {
-        g_leServiceMutex = ca_mutex_new();
+        g_leServiceMutex = oc_mutex_new();
         if (NULL == g_leServiceMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
 
     if (NULL == g_leCharacteristicMutex)
     {
-        g_leCharacteristicMutex = ca_mutex_new();
+        g_leCharacteristicMutex = oc_mutex_new();
         if (NULL == g_leCharacteristicMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
 
     if (NULL == g_leReqRespCbMutex)
     {
-        g_leReqRespCbMutex = ca_mutex_new();
+        g_leReqRespCbMutex = oc_mutex_new();
         if (NULL == g_leReqRespCbMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
 
     if (NULL == g_leServerThreadPoolMutex)
     {
-        g_leServerThreadPoolMutex = ca_mutex_new();
+        g_leServerThreadPoolMutex = oc_mutex_new();
         if (NULL == g_leServerThreadPoolMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
 
     if (NULL == g_LEClientListMutex)
     {
-        g_LEClientListMutex = ca_mutex_new();
+        g_LEClientListMutex = oc_mutex_new();
         if (NULL == g_LEClientListMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
@@ -494,22 +524,22 @@ CAResult_t CAInitGattServerMutexVariables()
 void CATerminateGattServerMutexVariables()
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    ca_mutex_free(g_leServerStateMutex);
+    oc_mutex_free(g_leServerStateMutex);
     g_leServerStateMutex = NULL;
 
-    ca_mutex_free(g_leServiceMutex);
+    oc_mutex_free(g_leServiceMutex);
     g_leServiceMutex = NULL;
 
-    ca_mutex_free(g_leCharacteristicMutex);
+    oc_mutex_free(g_leCharacteristicMutex);
     g_leCharacteristicMutex = NULL;
 
-    ca_mutex_free(g_leReqRespCbMutex);
+    oc_mutex_free(g_leReqRespCbMutex);
     g_leReqRespCbMutex = NULL;
 
-    ca_mutex_free(g_leServerThreadPoolMutex);
+    oc_mutex_free(g_leServerThreadPoolMutex);
     g_leServerThreadPoolMutex = NULL;
 
-    ca_mutex_free(g_LEClientListMutex);
+    oc_mutex_free(g_LEClientListMutex);
     g_LEClientListMutex = NULL;
 
     OIC_LOG(DEBUG, TAG, "OUT");
@@ -535,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;
         }
     }
@@ -552,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);
@@ -560,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;
 
@@ -569,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");
@@ -579,9 +613,9 @@ CAResult_t CADeInitLEGattServer()
 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    ca_mutex_lock(g_leServerThreadPoolMutex);
+    oc_mutex_lock(g_leServerThreadPoolMutex);
     g_leServerThreadPool = handle;
-    ca_mutex_unlock(g_leServerThreadPoolMutex);
+    oc_mutex_unlock(g_leServerThreadPoolMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
@@ -595,16 +629,16 @@ CAResult_t CAAddNewLEServiceInGattServer(const char *serviceUUID)
 
     bt_gatt_service_type_e type = BT_GATT_SERVICE_TYPE_PRIMARY;
 
-    ca_mutex_lock(g_leServiceMutex);
+    oc_mutex_lock(g_leServiceMutex);
     int ret = bt_gatt_service_create(serviceUUID, type, &g_gattSvcPath);
     if (0 != ret)
     {
-        ca_mutex_unlock(g_leServiceMutex);
+        oc_mutex_unlock(g_leServiceMutex);
         OIC_LOG_V(ERROR, TAG, "bt_gatt_service_create failed with ret [%s]",
                     CALEGetErrorMsg(ret));
         return CA_STATUS_FAILED;
     }
-    ca_mutex_unlock(g_leServiceMutex);
+    oc_mutex_unlock(g_leServiceMutex);
 
     if (g_gattSvcPath)
     {
@@ -615,11 +649,12 @@ 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(DEBUG, TAG, "IN");
+    OIC_LOG(INFO, TAG, "IN - WriteCharCB");
 
     if (NULL == charValue || NULL == remoteAddress)
     {
@@ -639,22 +674,32 @@ void CALEGattRemoteCharacteristicWriteCb(char *remoteAddress, bt_gatt_server_h s
 
     memcpy(data, charValue, charValueLen);
 
-    ca_mutex_lock(g_leReqRespCbMutex);
+    oc_mutex_lock(g_leReqRespCbMutex);
     if (NULL == g_leServerDataReceivedCallback)
     {
         OIC_LOG(ERROR, TAG, "gReqRespCallback is NULL!");
-        ca_mutex_unlock(g_leReqRespCbMutex);
+        oc_mutex_unlock(g_leReqRespCbMutex);
         OICFree(data);
         return;
     }
 
-    OIC_LOG(DEBUG, TAG, "Sending data up !");
+    OIC_LOG(INFO, TAG, "Sending data up !");
     uint32_t sentLength = 0;
     g_leServerDataReceivedCallback(remoteAddress, data, charValueLen,
                                     &sentLength);
-    ca_mutex_unlock(g_leReqRespCbMutex);
+    oc_mutex_unlock(g_leReqRespCbMutex);
     OICFree(data);
-    OIC_LOG(DEBUG, TAG, "OUT");
+
+    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");
 }
 
 CAResult_t CARegisterLEServicewithGattServer(const bt_gatt_h svcPath)
@@ -673,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;
     }
@@ -697,7 +750,7 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
     int properties;
     if (read)
     {
-        properties = BT_GATT_PROPERTY_NOTIFY | BT_GATT_PROPERTY_READ;
+        properties = BT_GATT_PROPERTY_INDICATE | BT_GATT_PROPERTY_READ;
     }
     else
     {
@@ -721,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;
         }
@@ -740,7 +795,7 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
         return CA_STATUS_FAILED;
     }
 
-    ca_mutex_lock(g_leCharacteristicMutex);
+    oc_mutex_lock(g_leCharacteristicMutex);
 
     if (read)
     {
@@ -752,7 +807,7 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
                                         &descriptor);
         if (0 != ret)
         {
-            ca_mutex_unlock(g_leCharacteristicMutex);
+            oc_mutex_unlock(g_leCharacteristicMutex);
             OIC_LOG_V(ERROR, TAG,
                       "bt_gatt_descriptor_create  failed with ret[%s]",
                       CALEGetErrorMsg(ret));
@@ -762,7 +817,7 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
         ret = bt_gatt_characteristic_add_descriptor(charPath, descriptor);
         if (0 != ret)
         {
-            ca_mutex_unlock(g_leCharacteristicMutex);
+            oc_mutex_unlock(g_leCharacteristicMutex);
             OIC_LOG_V(ERROR, TAG,
                       "bt_gatt_characteristic_add_descriptor  failed with ret[%s]",
                       CALEGetErrorMsg(ret));
@@ -776,7 +831,7 @@ CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const ch
         g_gattWriteCharPath = charPath;
     }
 
-    ca_mutex_unlock(g_leCharacteristicMutex);
+    oc_mutex_unlock(g_leCharacteristicMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
@@ -792,12 +847,12 @@ CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_
 
     OIC_LOG_V(DEBUG, TAG, "Client's Unicast address for sending data [%s]", address);
 
-    ca_mutex_lock(g_leCharacteristicMutex);
+    oc_mutex_lock(g_leCharacteristicMutex);
 
     if (NULL  == g_gattReadCharPath)
     {
         OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
-        ca_mutex_unlock(g_leCharacteristicMutex);
+        oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
 
@@ -806,21 +861,22 @@ CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_
     {
         OIC_LOG_V(ERROR, TAG,
                   "bt_gatt_set_value failed with return [%s]", CALEGetErrorMsg(ret));
-        ca_mutex_unlock(g_leCharacteristicMutex);
+        oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
 
-    ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
-                                address, 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));
-        ca_mutex_unlock(g_leCharacteristicMutex);
+                  "bt_gatt_server_notify_characteristic_changed_value failed with return [%s]", CALEGetErrorMsg(ret));
+        oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
 
-    ca_mutex_unlock(g_leCharacteristicMutex);
+    oc_mutex_unlock(g_leCharacteristicMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
@@ -832,12 +888,12 @@ CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uin
 
     VERIFY_NON_NULL(charValue, TAG, "charValue");
 
-    ca_mutex_lock(g_leCharacteristicMutex);
+    oc_mutex_lock(g_leCharacteristicMutex);
 
     if (NULL  == g_gattReadCharPath)
     {
         OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL");
-        ca_mutex_unlock(g_leCharacteristicMutex);
+        oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
 
@@ -845,21 +901,22 @@ CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uin
     if (0 != ret)
     {
         OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return[%s]", CALEGetErrorMsg(ret));
-        ca_mutex_unlock(g_leCharacteristicMutex);
+        oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
 
-    ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB,
-                                NULL, 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));
-        ca_mutex_unlock(g_leCharacteristicMutex);
+                  "bt_gatt_server_notify_characteristic_changed_value failed with return[%s]", CALEGetErrorMsg(ret));
+        oc_mutex_unlock(g_leCharacteristicMutex);
         return CA_STATUS_FAILED;
     }
 
-    ca_mutex_unlock(g_leCharacteristicMutex);
+    oc_mutex_unlock(g_leCharacteristicMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
@@ -869,9 +926,9 @@ void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    ca_mutex_lock(g_leReqRespCbMutex);
+    oc_mutex_lock(g_leReqRespCbMutex);
     g_leServerDataReceivedCallback = callback;
-    ca_mutex_unlock(g_leReqRespCbMutex);
+    oc_mutex_unlock(g_leReqRespCbMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
 }
@@ -880,3 +937,47 @@ void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
 {
     g_serverErrorCallback = callback;
 }
+
+bool CALEServerIsConnected(const char* address)
+{
+    //@Todo
+    return true;
+}
+
+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);
+
+    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;
+}