IOT-2539 clean unused-function in caleadapter.c 33/22933/1
authorGeorge Nash <george.nash@intel.com>
Fri, 20 Oct 2017 19:46:07 +0000 (12:46 -0700)
committerGeorge Nash <george.nash@intel.com>
Fri, 20 Oct 2017 19:57:40 +0000 (12:57 -0700)
The static functions:
  CAInitLEServerQueue
  CAInitLEServerReceiverQueue
  CAInitLEServerSenderQueue
  CALEServerDataReceiverHandler
  CALEServerSendDataThread

Are all only called if both ROUTING_GATWAY and
SINGLE_THREAD are not defined. When the are not
called gcc will produce the -Werror=unused-function
warning.

Rather than spatering the macros all over the file
I moved all of the functions into a single block

The code in the functions are not modified.

Bug: https://jira.iotivity.org/browse/IOT-2539
Change-Id: Ic15e4e91028b87fb8d2680f2e64c724d1fae25cc
Signed-off-by: George Nash <george.nash@intel.com>
resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c

index 5e0491d..1dcc4be 100644 (file)
@@ -336,19 +336,6 @@ static CAQueueingThread_t *g_bleServerReceiverQueue = NULL;
  */
 static CAQueueingThread_t *g_bleClientReceiverQueue = NULL;
 
-/**
- * This function will be associated with the sender queue for
- * GattServer.
- *
- * This function will fragment the data to the MTU of the transport
- * and send the data in fragments to the adapters. The function will
- * be blocked until all data is sent out from the adapter.
- *
- * @param[in] threadData Data pushed to the queue which contains the
- *                       info about RemoteEndpoint and Data.
- */
-static void CALEServerSendDataThread(void *threadData);
-
 /**
  * This function will be associated with the sender queue for
  * GattClient.
@@ -373,18 +360,6 @@ static void CALEClientSendDataThread(void *threadData);
  */
 static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType);
 
-/**
- * This function will be associated with the receiver queue for
- * GattServer.
- *
- * This function will call the function CALEDataReceiverHandler()
- * with server type to defragment the received data.
- *
- * @param[in] threadData Data pushed to the queue which contains the
- *                       info about RemoteEndpoint and Data.
- */
-static void CALEServerDataReceiverHandler(void *threadData);
-
 /**
  * This function will be associated with the receiver queue for
  * GattClient.
@@ -411,7 +386,32 @@ static void CAStopLEQueues();
  */
 static void CATerminateLEQueues();
 
-#ifndef SINGLE_THREAD
+#if !defined(ROUTING_GATEWAY) && !defined(SINGLE_THREAD)
+/**
+ * This function will be associated with the sender queue for
+ * GattServer.
+ *
+ * This function will fragment the data to the MTU of the transport
+ * and send the data in fragments to the adapters. The function will
+ * be blocked until all data is sent out from the adapter.
+ *
+ * @param[in] threadData Data pushed to the queue which contains the
+ *                       info about RemoteEndpoint and Data.
+ */
+static void CALEServerSendDataThread(void *threadData);
+
+/**
+ * This function will be associated with the receiver queue for
+ * GattServer.
+ *
+ * This function will call the function CALEDataReceiverHandler()
+ * with server type to defragment the received data.
+ *
+ * @param[in] threadData Data pushed to the queue which contains the
+ *                       info about RemoteEndpoint and Data.
+ */
+static void CALEServerDataReceiverHandler(void *threadData);
+
 /**
  * This function will initalize the Receiver and Sender queues for
  * GattServer. This function will in turn call the functions
@@ -424,34 +424,48 @@ static void CATerminateLEQueues();
  * @retval ::CA_STATUS_FAILED Operation failed.
  */
 static CAResult_t CAInitLEServerQueues();
-#endif // not SINGLE_THREAD
 
 /**
- * This function will initalize the Receiver and Sender queues for
- * GattClient. This function will inturn call the functions
- * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
- * initialize the queues.
+ * This function will initalize the Receiver queue for
+ * GattServer. This will initialize the queue to process the function
+ * CABLEServerSendDataThread() when ever the task is added to this
+ * queue.
  *
  * @return ::CA_STATUS_OK or Appropriate error code.
  * @retval ::CA_STATUS_OK  Successful.
  * @retval ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
  * @retval ::CA_STATUS_FAILED Operation failed.
- *
  */
-static CAResult_t CAInitLEClientQueues();
+static CAResult_t CAInitLEServerSenderQueue();
 
 /**
- * This function will initalize the Receiver queue for
+ * This function will initialize the Receiver queue for
  * GattServer. This will initialize the queue to process the function
- * CABLEServerSendDataThread() when ever the task is added to this
+ * CALEServerDataReceiverHandler() when ever the task is added to this
  * queue.
  *
+ * @return ::CA_STATUS_OK or Appropriate error code
+ * @retval ::CA_STATUS_OK  Successful
+ * @retval ::CA_STATUS_INVALID_PARAM  Invalid input arguments
+ * @retval ::CA_STATUS_FAILED Operation failed
+ *
+ */
+static CAResult_t CAInitLEServerReceiverQueue();
+#endif // not ROUTING_GATEWAY not SINGLE_THREAD
+
+/**
+ * This function will initalize the Receiver and Sender queues for
+ * GattClient. This function will inturn call the functions
+ * CAInitBleClientReceiverQueue() and CAInitBleClientSenderQueue() to
+ * initialize the queues.
+ *
  * @return ::CA_STATUS_OK or Appropriate error code.
  * @retval ::CA_STATUS_OK  Successful.
  * @retval ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
  * @retval ::CA_STATUS_FAILED Operation failed.
+ *
  */
-static CAResult_t CAInitLEServerSenderQueue();
+static CAResult_t CAInitLEClientQueues();
 
 /**
  * This function will initalize the Receiver queue for
@@ -466,20 +480,6 @@ static CAResult_t CAInitLEServerSenderQueue();
  */
 static CAResult_t CAInitLEClientSenderQueue();
 
-/**
- * This function will initialize the Receiver queue for
- * GattServer. This will initialize the queue to process the function
- * CALEServerDataReceiverHandler() when ever the task is added to this
- * queue.
- *
- * @return ::CA_STATUS_OK or Appropriate error code
- * @retval ::CA_STATUS_OK  Successful
- * @retval ::CA_STATUS_INVALID_PARAM  Invalid input arguments
- * @retval ::CA_STATUS_FAILED Operation failed
- *
- */
-static CAResult_t CAInitLEServerReceiverQueue();
-
 /**
  * This function will initialize the Receiver queue for
  * GattClient. This will initialize the queue to process the function
@@ -579,73 +579,112 @@ static CAResult_t CALEGetPortsFromSenderInfo(const char *leAddress,
                                             u_arraylist_t *portList);
 #endif
 
-#ifndef SINGLE_THREAD
-static CAResult_t CAInitLEServerQueues()
+static CAResult_t CAInitLEClientQueues()
 {
     oc_mutex_lock(g_bleAdapterThreadPoolMutex);
 
-    CAResult_t result = CAInitLEServerSenderQueue();
+    CAResult_t result = CAInitLEClientSenderQueue();
     if (CA_STATUS_OK != result)
     {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
         oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
         return CA_STATUS_FAILED;
     }
 
-    g_bleServerSenderInfo = u_arraylist_create();
-    if (!g_bleServerSenderInfo)
+    g_bleClientSenderInfo = u_arraylist_create();
+    if (!g_bleClientSenderInfo)
     {
         OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
         oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    result = CAInitLEServerReceiverQueue();
+    result = CAInitLEClientReceiverQueue();
     if (CA_STATUS_OK != result)
     {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerReceiverQueue failed");
-        u_arraylist_free(&g_bleServerSenderInfo);
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue failed");
+        u_arraylist_free(&g_bleClientSenderInfo);
         oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
         return CA_STATUS_FAILED;
     }
 
-    g_dataBleServerReceiverHandlerState = true;
+    g_dataBleClientReceiverHandlerState = true;
 
     oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
     return CA_STATUS_OK;
 }
-#endif // not SINGLE_THREAD
 
-static CAResult_t CAInitLEClientQueues()
+static CAResult_t CAInitLEClientReceiverQueue()
+{
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue");
+    // Check if the message queue is already initialized
+    if (g_bleClientReceiverQueue)
+    {
+        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
+        return CA_STATUS_OK;
+    }
+
+    // Create recv message queue
+    g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
+    if (!g_bleClientReceiverQueue)
+    {
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+        return CA_MEMORY_ALLOC_FAILED;
+    }
+
+    if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue,
+                                                   g_bleAdapterThreadPool,
+                                                   CALEClientDataReceiverHandler,
+                                                   CALEDataDestroyer))
+    {
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize client receiver queue thread");
+        OICFree(g_bleClientReceiverQueue);
+        g_bleClientReceiverQueue = NULL;
+        return CA_STATUS_FAILED;
+    }
+
+    if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
+    {
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
+        OICFree(g_bleClientReceiverQueue);
+        g_bleClientReceiverQueue = NULL;
+        return CA_STATUS_FAILED;
+    }
+
+    return CA_STATUS_OK;
+}
+
+#if !defined(ROUTING_GATEWAY) && !defined(SINGLE_THREAD)
+static CAResult_t CAInitLEServerQueues()
 {
     oc_mutex_lock(g_bleAdapterThreadPoolMutex);
 
-    CAResult_t result = CAInitLEClientSenderQueue();
+    CAResult_t result = CAInitLEServerSenderQueue();
     if (CA_STATUS_OK != result)
     {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleClientSenderQueue failed");
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitBleServerSenderQueue failed");
         oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
         return CA_STATUS_FAILED;
     }
 
-    g_bleClientSenderInfo = u_arraylist_create();
-    if (!g_bleClientSenderInfo)
+    g_bleServerSenderInfo = u_arraylist_create();
+    if (!g_bleServerSenderInfo)
     {
         OIC_LOG(ERROR, CALEADAPTER_TAG, "memory allocation failed!");
         oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    result = CAInitLEClientReceiverQueue();
+    result = CAInitLEServerReceiverQueue();
     if (CA_STATUS_OK != result)
     {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue failed");
-        u_arraylist_free(&g_bleClientSenderInfo);
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitLEServerReceiverQueue failed");
+        u_arraylist_free(&g_bleServerSenderInfo);
         oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
         return CA_STATUS_FAILED;
     }
 
-    g_dataBleClientReceiverHandlerState = true;
+    g_dataBleServerReceiverHandlerState = true;
 
     oc_mutex_unlock(g_bleAdapterThreadPoolMutex);
     return CA_STATUS_OK;
@@ -691,46 +730,6 @@ static CAResult_t CAInitLEServerReceiverQueue()
     return CA_STATUS_OK;
 }
 
-static CAResult_t CAInitLEClientReceiverQueue()
-{
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAInitLEClientReceiverQueue");
-    // Check if the message queue is already initialized
-    if (g_bleClientReceiverQueue)
-    {
-        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
-        return CA_STATUS_OK;
-    }
-
-    // Create recv message queue
-    g_bleClientReceiverQueue = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
-    if (!g_bleClientReceiverQueue)
-    {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
-        return CA_MEMORY_ALLOC_FAILED;
-    }
-
-    if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientReceiverQueue,
-                                                   g_bleAdapterThreadPool,
-                                                   CALEClientDataReceiverHandler,
-                                                   CALEDataDestroyer))
-    {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize client receiver queue thread");
-        OICFree(g_bleClientReceiverQueue);
-        g_bleClientReceiverQueue = NULL;
-        return CA_STATUS_FAILED;
-    }
-
-    if (CA_STATUS_OK != CAQueueingThreadStart(g_bleClientReceiverQueue))
-    {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "ca_thread_pool_add_task failed ");
-        OICFree(g_bleClientReceiverQueue);
-        g_bleClientReceiverQueue = NULL;
-        return CA_STATUS_FAILED;
-    }
-
-    return CA_STATUS_OK;
-}
-
 static CAResult_t CAInitLEServerSenderQueue()
 {
     OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAInitLEServerSenderQueue");
@@ -763,845 +762,845 @@ static CAResult_t CAInitLEServerSenderQueue()
     return CA_STATUS_OK;
 }
 
-static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
-{
-    const size_t length = u_arraylist_length(*list);
-    for (size_t i = 0; i < length; ++i)
-    {
-        CABLESenderInfo_t * const info =
-                (CABLESenderInfo_t *) u_arraylist_get(*list, i);
-        if (info)
-         {
-             OICFree(info->defragData);
-             CAFreeEndpoint(info->remoteEndpoint);
-             OICFree(info);
-         }
-    }
-    u_arraylist_free(list);
-}
-
-static void CALEClearSenderInfo()
+static void CALEServerDataReceiverHandler(void *threadData)
 {
-    CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
-    CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
+    CALEDataReceiverHandler(threadData, ADAPTER_SERVER);
 }
 
-static CAResult_t CAInitLEClientSenderQueue()
+static void CALEServerSendDataThread(void *threadData)
 {
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAInitLEClientSenderQueue");
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
 
-    if (g_bleClientSendQueueHandle)
+    CALEData_t * const bleData = (CALEData_t *) threadData;
+    if (!bleData)
     {
-        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
-        return CA_STATUS_OK;
+        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
+        return;
     }
 
-    // Create send message queue
-    g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
-    if (!g_bleClientSendQueueHandle)
+    if (!bleData->remoteEndpoint)
     {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
-        return CA_MEMORY_ALLOC_FAILED;
-    }
-
-    if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
-                                                   g_bleAdapterThreadPool,
-                                                   CALEClientSendDataThread, CALEDataDestroyer))
-    {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
-        OICFree(g_bleClientSendQueueHandle);
-        g_bleClientSendQueueHandle = NULL;
-        return CA_STATUS_FAILED;
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid endpoint of bledata!");
+        return;
     }
-    return CA_STATUS_OK;
-}
 
-static void CAStopLEQueues()
-{
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
+#if defined(__TIZEN__) || defined(__ANDROID__)
+    // get MTU size
+    g_mtuSize = CALEServerGetMtuSize(bleData->remoteEndpoint->addr);
+#endif
+    OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
 
-    oc_mutex_lock(g_bleServerReceiveDataMutex);
-    if (NULL != g_bleServerReceiverQueue)
-    {
-        CAQueueingThreadStop(g_bleServerReceiverQueue);
-    }
-    oc_mutex_unlock(g_bleServerReceiveDataMutex);
+    uint32_t midPacketCount = 0;
+    size_t remainingLen = 0;
+    size_t totalLength = 0;
+    CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
 
-    oc_mutex_lock(g_bleClientReceiveDataMutex);
-    if (NULL != g_bleClientReceiverQueue)
+    CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen,
+                                                           &midPacketCount,
+                                                           &remainingLen,
+                                                           &totalLength,
+                                                           g_mtuSize);
+
+    if (CA_STATUS_OK != result)
     {
-        CAQueueingThreadStop(g_bleClientReceiverQueue);
+        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                  "CAGenerateVariableForFragmentation failed, result [%d]", result);
+        if (g_errorHandler)
+        {
+            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+        }
+        return;
     }
-    oc_mutex_unlock(g_bleClientReceiveDataMutex);
-
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
-}
 
-static void CATerminateLEQueues()
-{
-    CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
-    OICFree(g_bleClientSendQueueHandle);
-    g_bleClientSendQueueHandle = NULL;
-
-    CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
-    OICFree(g_bleServerSendQueueHandle);
-    g_bleServerSendQueueHandle = NULL;
+    OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
+              "Packet info: data size[%d] midPacketCount[%u] remainingLen[%" PRIuPTR "] totalLength[%" PRIuPTR "]",
+              bleData->dataLen, midPacketCount, remainingLen, totalLength);
 
-    CAQueueingThreadDestroy(g_bleServerReceiverQueue);
-    OICFree(g_bleServerReceiverQueue);
-    g_bleServerReceiverQueue = NULL;
+    OIC_LOG_V(DEBUG,
+              CALEADAPTER_TAG,
+              "Server total Data length with header is [%" PRIuPTR "]",
+              totalLength);
 
-    CAQueueingThreadDestroy(g_bleClientReceiverQueue);
-    OICFree(g_bleClientReceiverQueue);
-    g_bleClientReceiverQueue = NULL;
+    uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
+    uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
 
-    CALEClearSenderInfo();
-}
+    if (NULL != bleData->remoteEndpoint) //Unicast Data
+    {
+        secureFlag = (bleData->remoteEndpoint->flags & CA_SECURE) ?
+            CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE;
 
-static CAResult_t CALEGetSenderInfo(const char *leAddress,
-                                    const uint16_t port,
-                                    u_arraylist_t *senderInfoList,
-                                    CABLESenderInfo_t **senderInfo,
-                                    size_t *senderIndex)
-{
-    VERIFY_NON_NULL_RET(leAddress,
-                        CALEADAPTER_TAG,
-                        "NULL BLE address argument",
-                        CA_STATUS_INVALID_PARAM);
-    VERIFY_NON_NULL_RET(senderIndex,
-                        CALEADAPTER_TAG,
-                        "NULL index argument",
-                        CA_STATUS_INVALID_PARAM);
+        OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This Packet is secure? %d", secureFlag);
+        result = CAGenerateHeader(dataHeader,
+                                  CA_BLE_PACKET_START,
+                                  g_localBLESourcePort,
+                                  secureFlag,
+                                  bleData->remoteEndpoint->port);
+    }
+    else                                //Multicast Data
+    {
+        result = CAGenerateHeader(dataHeader,
+                                  CA_BLE_PACKET_START,
+                                  g_localBLESourcePort,
+                                  secureFlag,
+                                  CA_BLE_MULTICAST_PORT);
+    }
 
-    const size_t listLength = u_arraylist_length(senderInfoList);
-    const size_t addrLength = strlen(leAddress);
-    for (size_t index = 0; index < listLength; index++)
+    if (CA_STATUS_OK != result)
     {
-        CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
-        if (!info || !(info->remoteEndpoint))
+        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                  "CAGenerateHeader failed, result [%d]", result);
+        if (g_errorHandler)
         {
-            continue;
+            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
         }
+        return;
+    }
 
-        if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
+    uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
+    result = CAGenerateHeaderPayloadLength(lengthHeader,
+                                           CA_BLE_LENGTH_HEADER_SIZE,
+                                           bleData->dataLen);
+
+    if (CA_STATUS_OK != result)
+    {
+        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                  "CAGenerateHeaderPayloadLength failed, result [%d]", result);
+        if (g_errorHandler)
         {
-            if (info->remoteEndpoint->port == port)
-            {
-                *senderIndex = index;
-                if (senderInfo)
-                {
-                    *senderInfo = info;
-                }
-                return CA_STATUS_OK;
-            }
+            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
         }
+        return;
     }
 
-    return CA_STATUS_FAILED;
-}
-
-static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType)
-{
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEDataReceiverHandler");
-
-    oc_mutex bleReceiveDataMutex = NULL;
-    bool dataBleReceiverHandlerState = false;
-#ifdef __WITH_DTLS__
-    CADataType_t dataType = CA_REQUEST_DATA;
-#endif
-
-    switch (receiverType)
+    uint32_t length = 0;
+    uint32_t dataLen = 0;
+    if (g_mtuSize > totalLength)
     {
-        case ADAPTER_CLIENT:
-            bleReceiveDataMutex = g_bleClientReceiveDataMutex;
-            dataBleReceiverHandlerState = g_dataBleClientReceiverHandlerState;
-#ifdef __WITH_DTLS__
-            dataType = CA_REQUEST_DATA;
-#endif
-            break;
-        case ADAPTER_SERVER:
-            bleReceiveDataMutex = g_bleServerReceiveDataMutex;
-            dataBleReceiverHandlerState = g_dataBleServerReceiverHandlerState;
-#ifdef __WITH_DTLS__
-            dataType = CA_RESPONSE_DATA;
-#endif
-            break;
-        default:
-            OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Unsupported receiver type : %d", receiverType);
-            return;
+        length = totalLength;
+        dataLen = bleData->dataLen;
     }
-
-    oc_mutex_lock(bleReceiveDataMutex);
-
-    if (dataBleReceiverHandlerState)
+    else
     {
-        CALEData_t *bleData = (CALEData_t *) threadData;
-        if (!bleData)
-        {
-            OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
-            oc_mutex_unlock(bleReceiveDataMutex);
-            return;
-        }
+        length = g_mtuSize;
+        dataLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
+    }
 
-        if (!(bleData->senderInfo))
-        {
-            OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
-            oc_mutex_unlock(bleReceiveDataMutex);
-            return;
-        }
+    result = CAMakeFirstDataSegment(dataSegment,
+                                    bleData->data, dataLen,
+                                    dataHeader, lengthHeader);
 
-        if (!(bleData->remoteEndpoint))
+    if (CA_STATUS_OK != result)
+    {
+        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                  "Making data segment failed, result [%d]", result);
+        if (g_errorHandler)
         {
-            OIC_LOG(ERROR, CALEADAPTER_TAG, "RemoteEndPoint NULL!!");
-            oc_mutex_unlock(bleReceiveDataMutex);
-            return;
+            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
         }
+        return;
+    }
 
-        CABLESenderInfo_t *senderInfo = NULL;
-        size_t senderIndex = 0;
+    const uint32_t iter = midPacketCount;
+    uint32_t index = 0;
 
-        //packet parsing
-        CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
-        CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
-        uint16_t sourcePort = 0;
-        uint16_t destPort = 0;
+    // Send the first segment with the header.
+    if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
+    {
+        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
 
-        CAParseHeader(bleData->data, &startFlag, &sourcePort, &secureFlag, &destPort);
-        OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
-                  "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
-                  startFlag, sourcePort, secureFlag, destPort);
+        result = CAUpdateCharacteristicsToGattClient(
+                    bleData->remoteEndpoint->addr, dataSegment, length);
 
-        if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
+        if (CA_STATUS_OK != result)
         {
-            OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                      "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
-                      g_localBLESourcePort, destPort);
-            oc_mutex_unlock(bleReceiveDataMutex);
+            OIC_LOG_V(ERROR,
+                      CALEADAPTER_TAG,
+                      "Update characteristics failed, result [%d]",
+                      result);
+            if (g_errorHandler)
+            {
+                g_errorHandler(bleData->remoteEndpoint,
+                               bleData->data,
+                               bleData->dataLen,
+                               result);
+            }
             return;
         }
 
-        bleData->remoteEndpoint->port = sourcePort;
+        OIC_LOG_V(DEBUG,
+                  CALEADAPTER_TAG,
+                  "Server Sent Unicast First Data - data length [%u]",
+                  length);
 
-        if (CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
-                                              bleData->remoteEndpoint->port,
-                                              bleData->senderInfo,
-                                              &senderInfo, &senderIndex))
-        {
-            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s:%X]",
-                      bleData->remoteEndpoint->addr, bleData->remoteEndpoint->port);
-        }
-        else
+        result = CAGenerateHeader(dataHeader,
+                                  CA_BLE_PACKET_NOT_START,
+                                  g_localBLESourcePort,
+                                  secureFlag,
+                                  bleData->remoteEndpoint->port);
+
+        if (CA_STATUS_OK != result)
         {
-            if (startFlag)
+            OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                      "CAGenerateHeader failed, result [%d]", result);
+            if (g_errorHandler)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG,
-                        "This packet is start packet but exist senderInfo. Remove senderInfo");
-                u_arraylist_remove(bleData->senderInfo, senderIndex);
-                OICFree(senderInfo->defragData);
-                OICFree(senderInfo);
-                senderInfo = NULL;
-                senderIndex = 0;
+                g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
             }
+            return;
         }
 
-        if (!senderInfo)
+        for (index = 0; index < iter; index++)
         {
-            uint32_t totalLength = 0;
-            if (startFlag)
-            {
-                CAParseHeaderPayloadLength(bleData->data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
-            }
-            else
-            {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "This packet is wrong packet! ignore.");
-                oc_mutex_unlock(bleReceiveDataMutex);
-                return;
-            }
+            // Send the remaining header.
+            result = CAMakeRemainDataSegment(dataSegment,
+                                             g_mtuSize - CA_BLE_HEADER_SIZE,
+                                             bleData->data,
+                                             bleData->dataLen,
+                                             index,
+                                             dataHeader,
+                                             g_mtuSize);
 
-            CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
-            if (!newSender)
+            if (CA_STATUS_OK != result)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
-                oc_mutex_unlock(bleReceiveDataMutex);
+                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                            "Making data segment failed, result [%d]", result);
+                if (g_errorHandler)
+                {
+                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                }
                 return;
             }
-            newSender->recvDataLen = 0;
-            newSender->totalDataLen = 0;
-            newSender->defragData = NULL;
-            newSender->remoteEndpoint = NULL;
-
-            OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
 
-            newSender->totalDataLen = totalLength;
+            result =
+                CAUpdateCharacteristicsToGattClient(
+                    bleData->remoteEndpoint->addr,
+                    dataSegment,
+                    g_mtuSize);
 
-            if (!(newSender->totalDataLen))
+            if (CA_STATUS_OK != result)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
-                OICFree(newSender);
-                oc_mutex_unlock(bleReceiveDataMutex);
+                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                            "Update characteristics failed, result [%d]", result);
+                if (g_errorHandler)
+                {
+                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                }
                 return;
             }
+            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
+                                               g_mtuSize);
+        }
 
-            size_t dataOnlyLen =
-                bleData->dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
-            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
-                      newSender->totalDataLen);
-            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%" PRIuPTR "] bytes",
-                      dataOnlyLen);
-
-            newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
-                                              sizeof(*newSender->defragData));
+        if (remainingLen && (totalLength > g_mtuSize))
+        {
+            // send the last segment of the data (Ex: 22 bytes of 622
+            // bytes of data when MTU is 200)
+            result = CAMakeRemainDataSegment(dataSegment,
+                                             remainingLen,
+                                             bleData->data,
+                                             bleData->dataLen,
+                                             index,
+                                             dataHeader,
+                                             g_mtuSize);
 
-            if (NULL == newSender->defragData)
+            if (CA_STATUS_OK != result)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
-                OICFree(newSender);
-                oc_mutex_unlock(bleReceiveDataMutex);
+                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                            "Making data segment failed, result [%d]", result);
+                if (g_errorHandler)
+                {
+                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                }
                 return;
             }
-            CATransportFlags_t flags = CA_DEFAULT_FLAGS;
-#ifdef __WITH_DTLS__
-            if (CA_BLE_PACKET_SECURE ==  secureFlag)
-            {
-                flags |= CA_SECURE;
-            }
-#endif
 
-            const char *remoteAddress = bleData->remoteEndpoint->addr;
-            newSender->remoteEndpoint = CACreateEndpointObject(flags,
-                                                               CA_ADAPTER_GATT_BTLE,
-                                                               remoteAddress,
-                                                               bleData->remoteEndpoint->port);
-
-            if (NULL == newSender->remoteEndpoint)
-            {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
-                OICFree(newSender->defragData);
-                OICFree(newSender);
-                oc_mutex_unlock(bleReceiveDataMutex);
-                return;
-            }
+            result = CAUpdateCharacteristicsToGattClient(
+                         bleData->remoteEndpoint->addr,
+                         dataSegment,
+                         remainingLen + CA_BLE_HEADER_SIZE);
 
-            if (newSender->recvDataLen + dataOnlyLen > newSender->totalDataLen)
+            if (CA_STATUS_OK != result)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
-                OICFree(newSender->defragData);
-                CAFreeEndpoint(newSender->remoteEndpoint);
-                OICFree(newSender);
-                oc_mutex_unlock(bleReceiveDataMutex);
+                OIC_LOG_V(ERROR,
+                          CALEADAPTER_TAG,
+                          "Update characteristics failed, result [%d]",
+                          result);
+                if (g_errorHandler)
+                {
+                    g_errorHandler(bleData->remoteEndpoint,
+                                   bleData->data,
+                                   bleData->dataLen,
+                                   result);
+                }
                 return;
             }
-            memcpy(newSender->defragData,
-                   bleData->data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
-                   dataOnlyLen);
-            newSender->recvDataLen += dataOnlyLen;
+            OIC_LOG_V(DEBUG,
+                      CALEADAPTER_TAG,
+                      "Server Sent Unicast Last Data - data length [%" PRIuPTR "]",
+                      remainingLen + CA_BLE_HEADER_SIZE);
+        }
+     }
+    else
+    {
+        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
+#if defined(__TIZEN__)
+        // @todo
+        // tizen ble should also disabled when Tizen 3.0 is updated.
+        result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
+        if (CA_STATUS_OK != result)
+        {
+            OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+                      result);
+            CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+            return;
+        }
 
-            u_arraylist_add(bleData->senderInfo,(void *)newSender);
+        result = CAGenerateHeader(dataHeader,
+                                  CA_BLE_PACKET_NOT_START,
+                                  g_localBLESourcePort,
+                                  secureFlag,
+                                  CA_BLE_MULTICAST_PORT);
 
-            //Getting newSender index position in bleSenderInfo array list
-            if (CA_STATUS_OK !=
-                    CALEGetSenderInfo(newSender->remoteEndpoint->addr,
-                                      newSender->remoteEndpoint->port,
-                                      bleData->senderInfo,
-                                      NULL, &senderIndex))
+        if (CA_STATUS_OK != result)
+        {
+            OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                      "CAGenerateHeader failed, result [%d]", result);
+            if (g_errorHandler)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
-                OICFree(newSender->defragData);
-                CAFreeEndpoint(newSender->remoteEndpoint);
-                OICFree(newSender);
-                oc_mutex_unlock(bleReceiveDataMutex);
-                return;
+                g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
             }
-            senderInfo = newSender;
+            return;
         }
-        else
+        OIC_LOG_V(DEBUG,
+                  CALEADAPTER_TAG,
+                  "Server Sent Multicast First Data - data length [%zu]",
+                  length);
+
+        for (index = 0; index < iter; index++)
         {
-            size_t dataOnlyLen = bleData->dataLen - CA_BLE_HEADER_SIZE;
-            if (senderInfo->recvDataLen + dataOnlyLen > senderInfo->totalDataLen)
+            // Send the remaining header.
+            result = CAMakeRemainDataSegment(dataSegment,
+                                             CA_BLE_NORMAL_SEGMENT_PAYLOAD_SIZE,
+                                             bleData->data,
+                                             bleData->dataLen,
+                                             index,
+                                             dataHeader);
+
+            if (CA_STATUS_OK != result)
             {
                 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                          "Data Length exceeding error!! Receiving [%" PRIuPTR "] total length [%u]",
-                          senderInfo->recvDataLen + dataOnlyLen, senderInfo->totalDataLen);
-                u_arraylist_remove(bleData->senderInfo, senderIndex);
-                OICFree(senderInfo->defragData);
-                OICFree(senderInfo);
-                oc_mutex_unlock(bleReceiveDataMutex);
+                            "Making data segment failed, result [%d]", result);
+                if (g_errorHandler)
+                {
+                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                }
                 return;
             }
-            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%" PRIuPTR "]",
-                      dataOnlyLen);
-            memcpy(senderInfo->defragData + senderInfo->recvDataLen,
-                   bleData->data + CA_BLE_HEADER_SIZE,
-                   dataOnlyLen);
-            senderInfo->recvDataLen += dataOnlyLen;
-            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength  [%d] received Datalen [%d]",
-                                                senderInfo->totalDataLen, senderInfo->recvDataLen);
-        }
 
-        if (senderInfo->totalDataLen == senderInfo->recvDataLen)
-        {
-            oc_mutex_lock(g_bleAdapterReqRespCbMutex);
-            if (NULL == g_networkPacketReceivedCallback)
-            {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
+            result = CAUpdateCharacteristicsToAllGattClients(
+                         dataSegment,
+                         CA_SUPPORTED_BLE_MTU_SIZE);
 
-                u_arraylist_remove(bleData->senderInfo, senderIndex);
-                OICFree(senderInfo->defragData);
-                OICFree(senderInfo);
-                oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
-                oc_mutex_unlock(bleReceiveDataMutex);
+            if (CA_STATUS_OK != result)
+            {
+                OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+                          result);
+                CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
                 return;
             }
+            OIC_LOG_V(DEBUG,
+                      CALEADAPTER_TAG,
+                      "Server Sent Multicast %d Data - data length [%zu]",
+                      index + 1,
+                      CA_SUPPORTED_BLE_MTU_SIZE);
+        }
 
-            OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Received data up !");
-
-            const CASecureEndpoint_t tmp =
-                {
-                    .endpoint = *senderInfo->remoteEndpoint
-                };
+        if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+        {
+            // send the last segment of the data (Ex: 22 bytes of 622
+            // bytes of data when MTU is 200)
+            result = CAMakeRemainDataSegment(dataSegment,
+                                             remainingLen,
+                                             bleData->data,
+                                             bleData->dataLen,
+                                             index,
+                                             dataHeader);
 
-#ifdef __WITH_DTLS__
-            if (CA_SECURE & tmp.endpoint.flags)
+            if (CA_STATUS_OK != result)
             {
-                OIC_LOG(ERROR, CALEADAPTER_TAG, "Secure data received");
-                g_dataType = dataType;
-
-                if (CA_STATUS_FAILED == CAdecryptSsl(&tmp,
-                                                senderInfo->defragData,
-                                                senderInfo->recvDataLen))
-                {
-                    OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl failed");
-                }
-                else
+                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                            "Making data segment failed, result [%d]", result);
+                if (g_errorHandler)
                 {
-                    OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl successed");
+                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
                 }
-                OICFree(senderInfo->defragData);
+                return;
             }
-            else
+
+            result = CAUpdateCharacteristicsToAllGattClients(
+                         dataSegment,
+                         remainingLen + CA_BLE_HEADER_SIZE);
+
+            if (CA_STATUS_OK != result)
             {
-#endif
-                OIC_LOG(DEBUG, CALEADAPTER_TAG, "Non-Secure data received");
-                g_networkPacketReceivedCallback(&tmp,
-                                                senderInfo->defragData,
-                                                senderInfo->recvDataLen);
-#ifdef __WITH_DTLS__
+                OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+                          result);
+                CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+                return;
             }
-#endif
-
-            oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
-            u_arraylist_remove(bleData->senderInfo, senderIndex);
-            senderInfo->remoteEndpoint = NULL;
-            senderInfo->defragData = NULL;
-            OICFree(senderInfo);
+            OIC_LOG_V(DEBUG,
+                      CALEADAPTER_TAG,
+                      "Server Sent Multicast Last Data - data length [%" PRIuPTR "]",
+                      remainingLen + CA_BLE_HEADER_SIZE);
         }
+#else
+        OIC_LOG(DEBUG, CALEADAPTER_TAG, "BLE Multicast is not supported");
+#endif
     }
-    oc_mutex_unlock(bleReceiveDataMutex);
+
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
 }
+#endif // not ROUTING_GATEWAY not SINGLE_THREAD
 
+static void CALEClearSenderInfoImpl(u_arraylist_t ** list)
+{
+    const size_t length = u_arraylist_length(*list);
+    for (size_t i = 0; i < length; ++i)
+    {
+        CABLESenderInfo_t * const info =
+                (CABLESenderInfo_t *) u_arraylist_get(*list, i);
+        if (info)
+         {
+             OICFree(info->defragData);
+             CAFreeEndpoint(info->remoteEndpoint);
+             OICFree(info);
+         }
+    }
+    u_arraylist_free(list);
+}
 
-static void CALEServerDataReceiverHandler(void *threadData)
+static void CALEClearSenderInfo()
 {
-    CALEDataReceiverHandler(threadData, ADAPTER_SERVER);
+    CALEClearSenderInfoImpl(&g_bleServerSenderInfo);
+    CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
 }
 
-static void CALEClientDataReceiverHandler(void *threadData)
+static CAResult_t CAInitLEClientSenderQueue()
 {
-    CALEDataReceiverHandler(threadData, ADAPTER_CLIENT);
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "CAInitLEClientSenderQueue");
+
+    if (g_bleClientSendQueueHandle)
+    {
+        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Already queue is initialized!");
+        return CA_STATUS_OK;
+    }
+
+    // Create send message queue
+    g_bleClientSendQueueHandle = (CAQueueingThread_t *) OICMalloc(sizeof(CAQueueingThread_t));
+    if (!g_bleClientSendQueueHandle)
+    {
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed!");
+        return CA_MEMORY_ALLOC_FAILED;
+    }
+
+    if (CA_STATUS_OK != CAQueueingThreadInitialize(g_bleClientSendQueueHandle,
+                                                   g_bleAdapterThreadPool,
+                                                   CALEClientSendDataThread, CALEDataDestroyer))
+    {
+        OIC_LOG(ERROR, CALEADAPTER_TAG, "Failed to Initialize send queue thread");
+        OICFree(g_bleClientSendQueueHandle);
+        g_bleClientSendQueueHandle = NULL;
+        return CA_STATUS_FAILED;
+    }
+    return CA_STATUS_OK;
 }
 
-static void CALEServerSendDataThread(void *threadData)
+static void CAStopLEQueues()
 {
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEServerSendDataThread");
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAStopLEQueues");
 
-    CALEData_t * const bleData = (CALEData_t *) threadData;
-    if (!bleData)
+    oc_mutex_lock(g_bleServerReceiveDataMutex);
+    if (NULL != g_bleServerReceiverQueue)
     {
-        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bledata!");
-        return;
+        CAQueueingThreadStop(g_bleServerReceiverQueue);
     }
+    oc_mutex_unlock(g_bleServerReceiveDataMutex);
 
-    if (!bleData->remoteEndpoint)
+    oc_mutex_lock(g_bleClientReceiveDataMutex);
+    if (NULL != g_bleClientReceiverQueue)
     {
-        OIC_LOG(ERROR, CALEADAPTER_TAG, "Invalid endpoint of bledata!");
-        return;
+        CAQueueingThreadStop(g_bleClientReceiverQueue);
     }
+    oc_mutex_unlock(g_bleClientReceiveDataMutex);
 
-#if defined(__TIZEN__) || defined(__ANDROID__)
-    // get MTU size
-    g_mtuSize = CALEServerGetMtuSize(bleData->remoteEndpoint->addr);
-#endif
-    OIC_LOG_V(INFO, CALEADAPTER_TAG, "MTU size [%d]", g_mtuSize);
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CAStopLEQueues");
+}
 
-    uint32_t midPacketCount = 0;
-    size_t remainingLen = 0;
-    size_t totalLength = 0;
-    CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
+static void CATerminateLEQueues()
+{
+    CAQueueingThreadDestroy(g_bleClientSendQueueHandle);
+    OICFree(g_bleClientSendQueueHandle);
+    g_bleClientSendQueueHandle = NULL;
 
-    CAResult_t result = CAGenerateVariableForFragmentation(bleData->dataLen,
-                                                           &midPacketCount,
-                                                           &remainingLen,
-                                                           &totalLength,
-                                                           g_mtuSize);
+    CAQueueingThreadDestroy(g_bleServerSendQueueHandle);
+    OICFree(g_bleServerSendQueueHandle);
+    g_bleServerSendQueueHandle = NULL;
 
-    if (CA_STATUS_OK != result)
+    CAQueueingThreadDestroy(g_bleServerReceiverQueue);
+    OICFree(g_bleServerReceiverQueue);
+    g_bleServerReceiverQueue = NULL;
+
+    CAQueueingThreadDestroy(g_bleClientReceiverQueue);
+    OICFree(g_bleClientReceiverQueue);
+    g_bleClientReceiverQueue = NULL;
+
+    CALEClearSenderInfo();
+}
+
+static CAResult_t CALEGetSenderInfo(const char *leAddress,
+                                    const uint16_t port,
+                                    u_arraylist_t *senderInfoList,
+                                    CABLESenderInfo_t **senderInfo,
+                                    size_t *senderIndex)
+{
+    VERIFY_NON_NULL_RET(leAddress,
+                        CALEADAPTER_TAG,
+                        "NULL BLE address argument",
+                        CA_STATUS_INVALID_PARAM);
+    VERIFY_NON_NULL_RET(senderIndex,
+                        CALEADAPTER_TAG,
+                        "NULL index argument",
+                        CA_STATUS_INVALID_PARAM);
+
+    const size_t listLength = u_arraylist_length(senderInfoList);
+    const size_t addrLength = strlen(leAddress);
+    for (size_t index = 0; index < listLength; index++)
     {
-        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                  "CAGenerateVariableForFragmentation failed, result [%d]", result);
-        if (g_errorHandler)
+        CABLESenderInfo_t *info = (CABLESenderInfo_t *) u_arraylist_get(senderInfoList, index);
+        if (!info || !(info->remoteEndpoint))
         {
-            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+            continue;
+        }
+
+        if (!strncmp(info->remoteEndpoint->addr, leAddress, addrLength))
+        {
+            if (info->remoteEndpoint->port == port)
+            {
+                *senderIndex = index;
+                if (senderInfo)
+                {
+                    *senderInfo = info;
+                }
+                return CA_STATUS_OK;
+            }
         }
-        return;
     }
 
-    OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
-              "Packet info: data size[%d] midPacketCount[%u] remainingLen[%" PRIuPTR "] totalLength[%" PRIuPTR "]",
-              bleData->dataLen, midPacketCount, remainingLen, totalLength);
-
-    OIC_LOG_V(DEBUG,
-              CALEADAPTER_TAG,
-              "Server total Data length with header is [%" PRIuPTR "]",
-              totalLength);
+    return CA_STATUS_FAILED;
+}
 
-    uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
-    uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
+static void CALEDataReceiverHandler(void *threadData, CABLEAdapter_t receiverType)
+{
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEDataReceiverHandler");
 
-    if (NULL != bleData->remoteEndpoint) //Unicast Data
-    {
-        secureFlag = (bleData->remoteEndpoint->flags & CA_SECURE) ?
-            CA_BLE_PACKET_SECURE : CA_BLE_PACKET_NON_SECURE;
+    oc_mutex bleReceiveDataMutex = NULL;
+    bool dataBleReceiverHandlerState = false;
+#ifdef __WITH_DTLS__
+    CADataType_t dataType = CA_REQUEST_DATA;
+#endif
 
-        OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This Packet is secure? %d", secureFlag);
-        result = CAGenerateHeader(dataHeader,
-                                  CA_BLE_PACKET_START,
-                                  g_localBLESourcePort,
-                                  secureFlag,
-                                  bleData->remoteEndpoint->port);
-    }
-    else                                //Multicast Data
+    switch (receiverType)
     {
-        result = CAGenerateHeader(dataHeader,
-                                  CA_BLE_PACKET_START,
-                                  g_localBLESourcePort,
-                                  secureFlag,
-                                  CA_BLE_MULTICAST_PORT);
+        case ADAPTER_CLIENT:
+            bleReceiveDataMutex = g_bleClientReceiveDataMutex;
+            dataBleReceiverHandlerState = g_dataBleClientReceiverHandlerState;
+#ifdef __WITH_DTLS__
+            dataType = CA_REQUEST_DATA;
+#endif
+            break;
+        case ADAPTER_SERVER:
+            bleReceiveDataMutex = g_bleServerReceiveDataMutex;
+            dataBleReceiverHandlerState = g_dataBleServerReceiverHandlerState;
+#ifdef __WITH_DTLS__
+            dataType = CA_RESPONSE_DATA;
+#endif
+            break;
+        default:
+            OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Unsupported receiver type : %d", receiverType);
+            return;
     }
 
-    if (CA_STATUS_OK != result)
+    oc_mutex_lock(bleReceiveDataMutex);
+
+    if (dataBleReceiverHandlerState)
     {
-        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                  "CAGenerateHeader failed, result [%d]", result);
-        if (g_errorHandler)
+        CALEData_t *bleData = (CALEData_t *) threadData;
+        if (!bleData)
         {
-            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+            OIC_LOG(DEBUG, CALEADAPTER_TAG, "Invalid bleData!");
+            oc_mutex_unlock(bleReceiveDataMutex);
+            return;
         }
-        return;
-    }
-
-    uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
-    result = CAGenerateHeaderPayloadLength(lengthHeader,
-                                           CA_BLE_LENGTH_HEADER_SIZE,
-                                           bleData->dataLen);
 
-    if (CA_STATUS_OK != result)
-    {
-        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                  "CAGenerateHeaderPayloadLength failed, result [%d]", result);
-        if (g_errorHandler)
+        if (!(bleData->senderInfo))
         {
-            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+            OIC_LOG(ERROR, CALEADAPTER_TAG, "sender info is not available");
+            oc_mutex_unlock(bleReceiveDataMutex);
+            return;
         }
-        return;
-    }
-
-    uint32_t length = 0;
-    uint32_t dataLen = 0;
-    if (g_mtuSize > totalLength)
-    {
-        length = totalLength;
-        dataLen = bleData->dataLen;
-    }
-    else
-    {
-        length = g_mtuSize;
-        dataLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
-    }
-
-    result = CAMakeFirstDataSegment(dataSegment,
-                                    bleData->data, dataLen,
-                                    dataHeader, lengthHeader);
 
-    if (CA_STATUS_OK != result)
-    {
-        OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                  "Making data segment failed, result [%d]", result);
-        if (g_errorHandler)
+        if (!(bleData->remoteEndpoint))
         {
-            g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+            OIC_LOG(ERROR, CALEADAPTER_TAG, "RemoteEndPoint NULL!!");
+            oc_mutex_unlock(bleReceiveDataMutex);
+            return;
         }
-        return;
-    }
 
-    const uint32_t iter = midPacketCount;
-    uint32_t index = 0;
+        CABLESenderInfo_t *senderInfo = NULL;
+        size_t senderIndex = 0;
 
-    // Send the first segment with the header.
-    if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
-    {
-        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
+        //packet parsing
+        CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
+        CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
+        uint16_t sourcePort = 0;
+        uint16_t destPort = 0;
 
-        result = CAUpdateCharacteristicsToGattClient(
-                    bleData->remoteEndpoint->addr, dataSegment, length);
+        CAParseHeader(bleData->data, &startFlag, &sourcePort, &secureFlag, &destPort);
+        OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
+                  "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
+                  startFlag, sourcePort, secureFlag, destPort);
 
-        if (CA_STATUS_OK != result)
+        if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
         {
-            OIC_LOG_V(ERROR,
-                      CALEADAPTER_TAG,
-                      "Update characteristics failed, result [%d]",
-                      result);
-            if (g_errorHandler)
-            {
-                g_errorHandler(bleData->remoteEndpoint,
-                               bleData->data,
-                               bleData->dataLen,
-                               result);
-            }
+            OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+                      "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
+                      g_localBLESourcePort, destPort);
+            oc_mutex_unlock(bleReceiveDataMutex);
             return;
         }
 
-        OIC_LOG_V(DEBUG,
-                  CALEADAPTER_TAG,
-                  "Server Sent Unicast First Data - data length [%u]",
-                  length);
-
-        result = CAGenerateHeader(dataHeader,
-                                  CA_BLE_PACKET_NOT_START,
-                                  g_localBLESourcePort,
-                                  secureFlag,
-                                  bleData->remoteEndpoint->port);
+        bleData->remoteEndpoint->port = sourcePort;
 
-        if (CA_STATUS_OK != result)
+        if (CA_STATUS_OK != CALEGetSenderInfo(bleData->remoteEndpoint->addr,
+                                              bleData->remoteEndpoint->port,
+                                              bleData->senderInfo,
+                                              &senderInfo, &senderIndex))
         {
-            OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                      "CAGenerateHeader failed, result [%d]", result);
-            if (g_errorHandler)
+            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "This is a new client [%s:%X]",
+                      bleData->remoteEndpoint->addr, bleData->remoteEndpoint->port);
+        }
+        else
+        {
+            if (startFlag)
             {
-                g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                OIC_LOG(ERROR, CALEADAPTER_TAG,
+                        "This packet is start packet but exist senderInfo. Remove senderInfo");
+                u_arraylist_remove(bleData->senderInfo, senderIndex);
+                OICFree(senderInfo->defragData);
+                OICFree(senderInfo);
+                senderInfo = NULL;
+                senderIndex = 0;
             }
-            return;
         }
 
-        for (index = 0; index < iter; index++)
+        if (!senderInfo)
         {
-            // Send the remaining header.
-            result = CAMakeRemainDataSegment(dataSegment,
-                                             g_mtuSize - CA_BLE_HEADER_SIZE,
-                                             bleData->data,
-                                             bleData->dataLen,
-                                             index,
-                                             dataHeader,
-                                             g_mtuSize);
+            uint32_t totalLength = 0;
+            if (startFlag)
+            {
+                CAParseHeaderPayloadLength(bleData->data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
+            }
+            else
+            {
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "This packet is wrong packet! ignore.");
+                oc_mutex_unlock(bleReceiveDataMutex);
+                return;
+            }
 
-            if (CA_STATUS_OK != result)
+            CABLESenderInfo_t *newSender = OICMalloc(sizeof(CABLESenderInfo_t));
+            if (!newSender)
             {
-                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                            "Making data segment failed, result [%d]", result);
-                if (g_errorHandler)
-                {
-                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
-                }
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
+                oc_mutex_unlock(bleReceiveDataMutex);
                 return;
             }
+            newSender->recvDataLen = 0;
+            newSender->totalDataLen = 0;
+            newSender->defragData = NULL;
+            newSender->remoteEndpoint = NULL;
 
-            result =
-                CAUpdateCharacteristicsToGattClient(
-                    bleData->remoteEndpoint->addr,
-                    dataSegment,
-                    g_mtuSize);
+            OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
 
-            if (CA_STATUS_OK != result)
+            newSender->totalDataLen = totalLength;
+
+            if (!(newSender->totalDataLen))
+            {
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
+                OICFree(newSender);
+                oc_mutex_unlock(bleReceiveDataMutex);
+                return;
+            }
+
+            size_t dataOnlyLen =
+                bleData->dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
+            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
+                      newSender->totalDataLen);
+            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%" PRIuPTR "] bytes",
+                      dataOnlyLen);
+
+            newSender->defragData = OICCalloc(newSender->totalDataLen + 1,
+                                              sizeof(*newSender->defragData));
+
+            if (NULL == newSender->defragData)
             {
-                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                            "Update characteristics failed, result [%d]", result);
-                if (g_errorHandler)
-                {
-                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
-                }
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
+                OICFree(newSender);
+                oc_mutex_unlock(bleReceiveDataMutex);
                 return;
             }
-            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
-                                               g_mtuSize);
-        }
+            CATransportFlags_t flags = CA_DEFAULT_FLAGS;
+#ifdef __WITH_DTLS__
+            if (CA_BLE_PACKET_SECURE ==  secureFlag)
+            {
+                flags |= CA_SECURE;
+            }
+#endif
 
-        if (remainingLen && (totalLength > g_mtuSize))
-        {
-            // send the last segment of the data (Ex: 22 bytes of 622
-            // bytes of data when MTU is 200)
-            result = CAMakeRemainDataSegment(dataSegment,
-                                             remainingLen,
-                                             bleData->data,
-                                             bleData->dataLen,
-                                             index,
-                                             dataHeader,
-                                             g_mtuSize);
+            const char *remoteAddress = bleData->remoteEndpoint->addr;
+            newSender->remoteEndpoint = CACreateEndpointObject(flags,
+                                                               CA_ADAPTER_GATT_BTLE,
+                                                               remoteAddress,
+                                                               bleData->remoteEndpoint->port);
 
-            if (CA_STATUS_OK != result)
+            if (NULL == newSender->remoteEndpoint)
             {
-                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                            "Making data segment failed, result [%d]", result);
-                if (g_errorHandler)
-                {
-                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
-                }
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "remoteEndpoint is NULL!");
+                OICFree(newSender->defragData);
+                OICFree(newSender);
+                oc_mutex_unlock(bleReceiveDataMutex);
                 return;
             }
 
-            result = CAUpdateCharacteristicsToGattClient(
-                         bleData->remoteEndpoint->addr,
-                         dataSegment,
-                         remainingLen + CA_BLE_HEADER_SIZE);
-
-            if (CA_STATUS_OK != result)
+            if (newSender->recvDataLen + dataOnlyLen > newSender->totalDataLen)
             {
-                OIC_LOG_V(ERROR,
-                          CALEADAPTER_TAG,
-                          "Update characteristics failed, result [%d]",
-                          result);
-                if (g_errorHandler)
-                {
-                    g_errorHandler(bleData->remoteEndpoint,
-                                   bleData->data,
-                                   bleData->dataLen,
-                                   result);
-                }
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
+                OICFree(newSender->defragData);
+                CAFreeEndpoint(newSender->remoteEndpoint);
+                OICFree(newSender);
+                oc_mutex_unlock(bleReceiveDataMutex);
                 return;
             }
-            OIC_LOG_V(DEBUG,
-                      CALEADAPTER_TAG,
-                      "Server Sent Unicast Last Data - data length [%" PRIuPTR "]",
-                      remainingLen + CA_BLE_HEADER_SIZE);
-        }
-     }
-    else
-    {
-        OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
-#if defined(__TIZEN__)
-        // @todo
-        // tizen ble should also disabled when Tizen 3.0 is updated.
-        result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
-        if (CA_STATUS_OK != result)
-        {
-            OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
-                      result);
-            CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
-            return;
-        }
+            memcpy(newSender->defragData,
+                   bleData->data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
+                   dataOnlyLen);
+            newSender->recvDataLen += dataOnlyLen;
 
-        result = CAGenerateHeader(dataHeader,
-                                  CA_BLE_PACKET_NOT_START,
-                                  g_localBLESourcePort,
-                                  secureFlag,
-                                  CA_BLE_MULTICAST_PORT);
+            u_arraylist_add(bleData->senderInfo,(void *)newSender);
 
-        if (CA_STATUS_OK != result)
-        {
-            OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                      "CAGenerateHeader failed, result [%d]", result);
-            if (g_errorHandler)
+            //Getting newSender index position in bleSenderInfo array list
+            if (CA_STATUS_OK !=
+                    CALEGetSenderInfo(newSender->remoteEndpoint->addr,
+                                      newSender->remoteEndpoint->port,
+                                      bleData->senderInfo,
+                                      NULL, &senderIndex))
             {
-                g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "Existing sender index not found!!");
+                OICFree(newSender->defragData);
+                CAFreeEndpoint(newSender->remoteEndpoint);
+                OICFree(newSender);
+                oc_mutex_unlock(bleReceiveDataMutex);
+                return;
             }
-            return;
+            senderInfo = newSender;
         }
-        OIC_LOG_V(DEBUG,
-                  CALEADAPTER_TAG,
-                  "Server Sent Multicast First Data - data length [%zu]",
-                  length);
-
-        for (index = 0; index < iter; index++)
+        else
         {
-            // Send the remaining header.
-            result = CAMakeRemainDataSegment(dataSegment,
-                                             CA_BLE_NORMAL_SEGMENT_PAYLOAD_SIZE,
-                                             bleData->data,
-                                             bleData->dataLen,
-                                             index,
-                                             dataHeader);
-
-            if (CA_STATUS_OK != result)
+            size_t dataOnlyLen = bleData->dataLen - CA_BLE_HEADER_SIZE;
+            if (senderInfo->recvDataLen + dataOnlyLen > senderInfo->totalDataLen)
             {
                 OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                            "Making data segment failed, result [%d]", result);
-                if (g_errorHandler)
-                {
-                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
-                }
+                          "Data Length exceeding error!! Receiving [%" PRIuPTR "] total length [%u]",
+                          senderInfo->recvDataLen + dataOnlyLen, senderInfo->totalDataLen);
+                u_arraylist_remove(bleData->senderInfo, senderIndex);
+                OICFree(senderInfo->defragData);
+                OICFree(senderInfo);
+                oc_mutex_unlock(bleReceiveDataMutex);
                 return;
             }
+            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%" PRIuPTR "]",
+                      dataOnlyLen);
+            memcpy(senderInfo->defragData + senderInfo->recvDataLen,
+                   bleData->data + CA_BLE_HEADER_SIZE,
+                   dataOnlyLen);
+            senderInfo->recvDataLen += dataOnlyLen;
+            OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "totalDatalength  [%d] received Datalen [%d]",
+                                                senderInfo->totalDataLen, senderInfo->recvDataLen);
+        }
 
-            result = CAUpdateCharacteristicsToAllGattClients(
-                         dataSegment,
-                         CA_SUPPORTED_BLE_MTU_SIZE);
-
-            if (CA_STATUS_OK != result)
+        if (senderInfo->totalDataLen == senderInfo->recvDataLen)
+        {
+            oc_mutex_lock(g_bleAdapterReqRespCbMutex);
+            if (NULL == g_networkPacketReceivedCallback)
             {
-                OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
-                          result);
-                CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "gReqRespCallback is NULL!");
+
+                u_arraylist_remove(bleData->senderInfo, senderIndex);
+                OICFree(senderInfo->defragData);
+                OICFree(senderInfo);
+                oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
+                oc_mutex_unlock(bleReceiveDataMutex);
                 return;
             }
-            OIC_LOG_V(DEBUG,
-                      CALEADAPTER_TAG,
-                      "Server Sent Multicast %d Data - data length [%zu]",
-                      index + 1,
-                      CA_SUPPORTED_BLE_MTU_SIZE);
-        }
 
-        if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
-        {
-            // send the last segment of the data (Ex: 22 bytes of 622
-            // bytes of data when MTU is 200)
-            result = CAMakeRemainDataSegment(dataSegment,
-                                             remainingLen,
-                                             bleData->data,
-                                             bleData->dataLen,
-                                             index,
-                                             dataHeader);
+            OIC_LOG(DEBUG, CALEADAPTER_TAG, "[CALEDataReceiverHandler] Received data up !");
 
-            if (CA_STATUS_OK != result)
+            const CASecureEndpoint_t tmp =
+                {
+                    .endpoint = *senderInfo->remoteEndpoint
+                };
+
+#ifdef __WITH_DTLS__
+            if (CA_SECURE & tmp.endpoint.flags)
             {
-                OIC_LOG_V(ERROR, CALEADAPTER_TAG,
-                            "Making data segment failed, result [%d]", result);
-                if (g_errorHandler)
+                OIC_LOG(ERROR, CALEADAPTER_TAG, "Secure data received");
+                g_dataType = dataType;
+
+                if (CA_STATUS_FAILED == CAdecryptSsl(&tmp,
+                                                senderInfo->defragData,
+                                                senderInfo->recvDataLen))
                 {
-                    g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+                    OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl failed");
                 }
-                return;
+                else
+                {
+                    OIC_LOG(ERROR, CALEADAPTER_TAG, "CAdecryptSsl successed");
+                }
+                OICFree(senderInfo->defragData);
             }
-
-            result = CAUpdateCharacteristicsToAllGattClients(
-                         dataSegment,
-                         remainingLen + CA_BLE_HEADER_SIZE);
-
-            if (CA_STATUS_OK != result)
+            else
             {
-                OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
-                          result);
-                CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
-                return;
+#endif
+                OIC_LOG(DEBUG, CALEADAPTER_TAG, "Non-Secure data received");
+                g_networkPacketReceivedCallback(&tmp,
+                                                senderInfo->defragData,
+                                                senderInfo->recvDataLen);
+#ifdef __WITH_DTLS__
             }
-            OIC_LOG_V(DEBUG,
-                      CALEADAPTER_TAG,
-                      "Server Sent Multicast Last Data - data length [%" PRIuPTR "]",
-                      remainingLen + CA_BLE_HEADER_SIZE);
-        }
-#else
-        OIC_LOG(DEBUG, CALEADAPTER_TAG, "BLE Multicast is not supported");
 #endif
+
+            oc_mutex_unlock(g_bleAdapterReqRespCbMutex);
+            u_arraylist_remove(bleData->senderInfo, senderIndex);
+            senderInfo->remoteEndpoint = NULL;
+            senderInfo->defragData = NULL;
+            OICFree(senderInfo);
+        }
     }
+    oc_mutex_unlock(bleReceiveDataMutex);
+}
 
-    OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT - CALEServerSendDataThread");
+static void CALEClientDataReceiverHandler(void *threadData)
+{
+    CALEDataReceiverHandler(threadData, ADAPTER_CLIENT);
 }
 
 static void CALEClientSendDataThread(void *threadData)