[IOT-583] fixed Option Parameter is not support
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / camessagehandler.c
index 9552827..200a9f4 100644 (file)
 #include "camessagehandler.h"
 #include "caremotehandler.h"
 #include "cainterfacecontroller.h"
+#include "caprotocolmessage.h"
+#include "caretransmission.h"
+#include "caadapterutils.h"
 #include "uqueue.h"
 #include "logger.h"
 #include "config.h" /* for coap protocol */
-#include "coap.h"
-#include "uthreadpool.h" /* for thread pool */
-#include "umutex.h"
+#include "cathreadpool.h" /* for thread pool */
+#include "caqueueingthread.h"
+#include "camutex.h"
 #include "oic_malloc.h"
+#include "oic_string.h"
+#include "canetworkconfigurator.h"
 
 #define TAG PCF("CA")
+#define SINGLE_HANDLE
 
-#define MEMORY_ALLOCK_CHECK(arg) { if (arg == NULL) {OIC_LOG_V(DEBUG, TAG, "memory error"); goto memory_error_exit;} }
-#define MAX_ACTION_NUM   300
+#define MAX_THREAD_POOL_SIZE    20
 
-#define MAX_THREAD_POOL_SIZE    10
-
-#ifndef TRUE
-#define TRUE    1
-#endif
-
-#ifndef FALSE
-#define FALSE   0
-#endif
-
-typedef struct
+typedef enum
 {
-    int32_t actionId;
-    CARemoteEndpoint_t* remoteEndpoint;
-    CARequestInfo_t* requestInfo;
-    CAResponseInfo_t* responseInfo;
-} CAData_t;
+    SEND_TYPE_MULTICAST = 0, SEND_TYPE_UNICAST
+} CASendDataType_t;
 
-typedef void (*CAThreadTask)(CAData_t* data);
+typedef enum
+{
+    CA_REQUEST_DATA = 1,
+    CA_RESPONSE_DATA = 2,
+    CA_ERROR_DATA = 3,
+} CADataType_t;
 
 typedef struct
 {
-    u_mutex threadMutex;
-    u_cond threadCond;
-    CAThreadTask threadTask;
-    int32_t isStop;
-    u_queue_t* dataQueue;
-} CAThread_t;
+    CASendDataType_t type;
+    CAEndpoint_t *remoteEndpoint;
+    CARequestInfo_t *requestInfo;
+    CAResponseInfo_t *responseInfo;
+    CAErrorInfo_t *errorInfo;
+    CAHeaderOption_t *options;
+    CADataType_t dataType;
+    uint8_t numOptions;
+} CAData_t;
 
 // thread pool handle
-static u_thread_pool_t gThreadPoolHandle = NULL;
+static ca_thread_pool_t g_threadPoolHandle = NULL;
 
 // message handler main thread
-static CAThread_t gSendThread;
+static CAQueueingThread_t g_sendThread;
+static CAQueueingThread_t g_receiveThread;
 
-// message handler callback
-static int32_t gCurrentActionId = 0;
-static CAMessageHandlerCallback gHandlerCallback = NULL;
+static CARetransmission_t g_retransmissionContext;
 
 // handler field
-static CARequestCallback gRequestHandler = NULL;
-static CAResponseCallback gResponseHandler = NULL;
+static CARequestCallback g_requestHandler = NULL;
+static CAResponseCallback g_responseHandler = NULL;
+static CAErrorCallback g_errorHandler = NULL;
 
-static u_queue_t* gMessageQueue = NULL;
-static u_mutex gMessageQueueMutex = NULL;
+static void CAErrorHandler(const CAEndpoint_t *endpoint,
+                           const void *data, uint32_t dataLen,
+                           CAResult_t result);
 
-static void CAAddReceiveData(CAData_t* data)
+static bool CAIsSelectedNetworkAvailable()
 {
-    OIC_LOG_V(DEBUG, TAG, "CAAddReceiveData");
-
-    // create thread data
-    u_queue_message_t* message = (u_queue_message_t*) OICMalloc(sizeof(u_queue_message_t));
-
-    if (message == NULL)
+    u_arraylist_t *list = CAGetSelectedNetworkList();
+    if (!list || list->length == 0)
     {
-        OIC_LOG_V(DEBUG, TAG, "memory error!!");
-        return;
+        OIC_LOG(ERROR, TAG, "No selected network");
+        return false;
     }
-    memset(message, 0, sizeof(u_queue_message_t));
-
-    message->msg = data;
-    message->size = sizeof(CAData_t);
-
-    // mutex lock
-    u_mutex_lock(gMessageQueueMutex);
-
-    // add thread data into list
-    u_queue_add_element(gMessageQueue, message);
 
-    // mutex unlock
-    u_mutex_unlock(gMessageQueueMutex);
+    return true;
 }
 
-static void CAAddSendData(CAData_t* data)
+static void CATimeoutCallback(const CAEndpoint_t *endpoint, const void *pdu, uint32_t size)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAAddSendData!!");
+    OIC_LOG(DEBUG, TAG, "IN");
+    VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
+    VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
 
-    // create thread data
-    u_queue_message_t* message = (u_queue_message_t*) OICMalloc(sizeof(u_queue_message_t));
-
-    if (message == NULL)
+    CAEndpoint_t* ep = CACloneEndpoint(endpoint);
+    if (NULL == ep)
     {
-        OIC_LOG_V(DEBUG, TAG, "memory error!!");
+        OIC_LOG(ERROR, TAG, "clone failed");
         return;
     }
-    memset(message, 0, sizeof(u_queue_message_t));
 
-    message->msg = data;
-    message->size = sizeof(CAData_t);
+    CAResponseInfo_t* resInfo = (CAResponseInfo_t*)OICCalloc(1, sizeof(CAResponseInfo_t));
 
-    // mutex lock
-    u_mutex_lock(gSendThread.threadMutex);
+    if (NULL == resInfo)
+    {
+        OIC_LOG(ERROR, TAG, "calloc failed");
+        CAAdapterFreeEndpoint(ep);
+        return;
+    }
 
-    // add thread data into list
-    u_queue_add_element(gSendThread.dataQueue, message);
+    resInfo->result = CA_RETRANSMIT_TIMEOUT;
+    resInfo->info.type = CAGetMessageTypeFromPduBinaryData(pdu, size);
+    resInfo->info.messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
+    CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *) pdu, &(resInfo->info));
+    if (CA_STATUS_OK != res)
+    {
+        OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
+        OICFree(resInfo->info.token);
+        OICFree(resInfo);
+        CAAdapterFreeEndpoint(ep);
+        return;
+    }
 
-    // notity the thread
-    u_cond_signal(gSendThread.threadCond);
+    CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
+    if (NULL == cadata)
+    {
+        OIC_LOG(ERROR, TAG, "memory allocation failed !");
+        CAAdapterFreeEndpoint(ep);
+        OICFree(resInfo);
+        return;
+    }
+
+    cadata->type = SEND_TYPE_UNICAST;
+    cadata->remoteEndpoint = ep;
+    cadata->requestInfo = NULL;
+    cadata->responseInfo = resInfo;
 
-    // mutex unlock
-    u_mutex_unlock(gSendThread.threadMutex);
+    CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
+    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
-static void CAStopSendThread()
+static void CADataDestroyer(void *data, uint32_t size)
 {
-    OIC_LOG_V(DEBUG, TAG, "CAStopSendThread request!!");
+    OIC_LOG(DEBUG, TAG, "IN");
+    CAData_t *cadata = (CAData_t *) data;
+
+    if (NULL == cadata)
+    {
+        OIC_LOG(ERROR, TAG, "cadata is NULL");
+        return;
+    }
 
-    // mutex lock
-    u_mutex_lock(gSendThread.threadMutex);
+    if (NULL != cadata->remoteEndpoint)
+    {
+        CADestroyEndpointInternal(cadata->remoteEndpoint);
+    }
 
-    // set stop flag
-    gSendThread.isStop = TRUE;
+    if (NULL != cadata->requestInfo)
+    {
+        CADestroyRequestInfoInternal((CARequestInfo_t *) cadata->requestInfo);
+    }
 
-    // notity the thread
-    u_cond_signal(gSendThread.threadCond);
+    if (NULL != cadata->responseInfo)
+    {
+        CADestroyResponseInfoInternal((CAResponseInfo_t *) cadata->responseInfo);
+    }
 
-    // mutex unlock
-    u_mutex_unlock(gSendThread.threadMutex);
+    if (NULL != cadata->errorInfo)
+    {
+       CAInfo_t *info = &cadata->errorInfo->info;
+       OICFree(info->token);
+       OICFree(info->options);
+       OICFree(info->payload);
+       OICFree(cadata->errorInfo);
+    }
+
+    OICFree(cadata);
+    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
-static void CASendThreadProcess(CAData_t* data)
+static void CAReceiveThreadProcess(void *threadData)
 {
-    if (data == NULL)
+    OIC_LOG(DEBUG, TAG, "IN");
+    // Currently not supported
+    // This will be enabled when RI supports multi threading
+#ifndef SINGLE_HANDLE
+    CAData_t *data = (CAData_t *) threadData;
+
+    if (NULL == data)
     {
-        OIC_LOG(DEBUG, TAG, "thread data error!!");
+        OIC_LOG(ERROR, TAG, "thread data error!!");
         return;
     }
 
-    if (NULL == data->remoteEndpoint)
+    // parse the data and call the callbacks.
+    // #1 parse the data
+    // #2 get endpoint
+    CAEndpoint_t *rep = (CAEndpoint_t *)(data->remoteEndpoint);
+
+    if (NULL == rep)
     {
-        OIC_LOG(DEBUG, TAG, "remoteEndpoint is null");
+        OIC_LOG(ERROR, TAG, "remoteEndpoint error!!");
         return;
     }
 
-    OIC_LOG_V(DEBUG, TAG, "thread action id : %d", data->actionId);
+    if (data->requestInfo && g_requestHandler)
+    {
+        g_requestHandler(rep, data->requestInfo);
+    }
+    else if (data->responseInfo && g_responseHandler)
+    {
+        g_responseHandler(rep, data->responseInfo);
+    }
+    else if (data->errorInfo && g_errorHandler)
+    {
+        g_errorHandler(rep, data->errorInfo);
+    }
 
-    CADetachErrorCode code = FAIL;
-    int32_t res = 0;
+#endif /* SINGLE_HANDLE */
+    OIC_LOG(DEBUG, TAG, "OUT");
+}
 
-    if (data->requestInfo != NULL)
-    {
-        OIC_LOG(DEBUG, TAG, "requestInfo is available");
+static void CASendThreadProcess(void *threadData)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    CAData_t *data = (CAData_t *) threadData;
 
-        coap_pdu_t* pdu = NULL;
-        pdu = CAGeneratePdu(data->remoteEndpoint->resourceUri, data->requestInfo->method,
-                data->requestInfo->info);
+    VERIFY_NON_NULL_VOID(data, TAG, "data");
+    VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");
 
-        // interface controller function call.
-        if (NULL != pdu)
-        {
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
+    CAResult_t res = CA_STATUS_FAILED;
 
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
+    CASendDataType_t type = data->type;
 
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - buffer data : %s", pdu->hdr);
+    coap_pdu_t *pdu = NULL;
 
-            res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
-        }
-    }
-    else if (data->responseInfo != NULL)
+    if (SEND_TYPE_UNICAST == type)
     {
-        OIC_LOG_V(DEBUG, TAG, "responseInfo is available..");
 
-        coap_pdu_t* pdu = NULL;
+        if (NULL != data->requestInfo)
+        {
+            OIC_LOG(DEBUG, TAG, "requestInfo is available..");
+
+            pdu = CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
+        }
+        else if (NULL != data->responseInfo)
+        {
+            OIC_LOG(DEBUG, TAG, "responseInfo is available..");
 
-        pdu = CAGeneratePdu(data->remoteEndpoint->resourceUri, data->responseInfo->result,
-                data->responseInfo->info);
+            pdu = CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
+        }
+        else
+        {
+            OIC_LOG(DEBUG, TAG, "request info, response info is empty");
+        }
 
         // interface controller function call.
         if (NULL != pdu)
         {
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
-
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
-
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - buffer data : %x", pdu->hdr);
+            CALogPDUInfo(pdu);
 
             res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
+            if (CA_STATUS_OK != res)
+            {
+                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
+                CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
+                coap_delete_pdu(pdu);
+                return;
+            }
+            // for retransmission
+            res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
+                                           pdu->length);
+            if (CA_STATUS_OK != res)
+            {
+                OIC_LOG_V(INFO, TAG, "retransmission will be not working: %d", res);
+                coap_delete_pdu(pdu);
+                return;
+            }
+
+            coap_delete_pdu(pdu);
         }
     }
-    else
+    else if (SEND_TYPE_MULTICAST == type)
     {
         OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");
 
-        coap_pdu_t* pdu = NULL;
-        CAInfo_t info;
-        memset(&info, 0, sizeof(CAInfo_t));
-        pdu = CAGeneratePdu(data->remoteEndpoint->resourceUri, CA_GET, info);
+        CAInfo_t *info = &data->requestInfo->info;
+
+        info->options = data->options;
+        info->numOptions = data->numOptions;
 
+        pdu = CAGeneratePDU(CA_GET, info);
         if (NULL != pdu)
         {
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
-
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
-
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
-
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", pdu->hdr->id);
-
-            OIC_LOG_V(DEBUG, TAG, "PDU Maker - buffer data : %x", pdu->hdr);
-
-            res = CASendMulticastData(pdu->hdr, pdu->length);
+            CALogPDUInfo(pdu);
+
+            res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
+            if (CA_STATUS_OK != res)
+            {
+                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
+                CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
+                coap_delete_pdu(pdu);
+                return;
+            }
+
+            coap_delete_pdu(pdu);
         }
-
-    }
-
-    if (res)
-    {
-        code = SUCCESS;
     }
 
-    if (gHandlerCallback != NULL)
-    {
-        gHandlerCallback(data->actionId, code);
-    }
+    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
-static void* CAThreadBaseRoutine(void* treadData)
+static void CAReceivedPacketCallback(const CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
 {
-    OIC_LOG_V(DEBUG, TAG, "message handler main thread start..");
+    OIC_LOG(DEBUG, TAG, "IN");
+    VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
+    VERIFY_NON_NULL_VOID(data, TAG, "data");
 
-    CAThread_t* thread = (CAThread_t*) treadData;
+    uint32_t code = CA_NOT_FOUND;
+    coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
+    OICFree(data);
 
-    if (thread == NULL)
+    if (NULL == pdu)
     {
-        OIC_LOG_V(DEBUG, TAG, "thread data passing error!!");
-
-        return NULL;
+        OIC_LOG(ERROR, TAG, "Parse PDU failed");
+        return;
     }
 
-    while (!thread->isStop)
+    if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
     {
-        // mutex lock
-        u_mutex_lock(thread->threadMutex);
-
-        // if queue is empty, thread will wait
-        if (u_queue_get_size(thread->dataQueue) <= 0)
+        CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
+        if (NULL == ReqInfo)
         {
-            OIC_LOG_V(DEBUG, TAG, "wait..");
-            // wait
-            u_cond_wait(thread->threadCond, thread->threadMutex);
-
-            OIC_LOG_V(DEBUG, TAG, "wake up..");
+            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
+            coap_delete_pdu(pdu);
+            return;
         }
 
-        // mutex unlock
-        u_mutex_unlock(thread->threadMutex);
-
-        // check stop flag
-        if (thread->isStop)
-            continue;
-
-        // get data
-        u_queue_message_t* message = u_queue_get_element(thread->dataQueue);
-
-        CAData_t* data = (CAData_t*) message->msg;
-
-        // process data
-        thread->threadTask(data);
-    }
-
-    OIC_LOG_V(DEBUG, TAG, "message handler main thread end..");
-
-    return NULL;
-}
-
-static int32_t CAIncreaseActionId()
-{
-    ++gCurrentActionId;
+        CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
+        if (CA_STATUS_OK != res)
+        {
+            OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
+            OICFree(ReqInfo);
+            coap_delete_pdu(pdu);
+            return;
+        }
 
-    gCurrentActionId = (gCurrentActionId > MAX_ACTION_NUM) ? 0 : gCurrentActionId;
+        if (NULL != ReqInfo->info.options)
+        {
+            uint32_t i;
+            for (i = 0; i < ReqInfo->info.numOptions; i++)
+            {
+                OIC_LOG_V(DEBUG, TAG, "Request- optionID: %d", ReqInfo->info.options[i].optionID);
 
-    return gCurrentActionId;
-}
+                OIC_LOG_V(DEBUG, TAG, "Request- list: %s", ReqInfo->info.options[i].optionData);
+            }
+        }
 
-static void CAReceivedPacketCallback(CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
-{
-    OIC_LOG(DEBUG, TAG, "receivedPacketCallback in message handler!!");
+        if (NULL != ReqInfo->info.payload)
+        {
+            OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
+        }
+        OIC_LOG_V(DEBUG, TAG, "Request- code: %d", ReqInfo->method);
+        if (NULL != ReqInfo->info.token)
+        {
+            OIC_LOG(DEBUG, TAG, "Request- token:");
+            OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token,
+                           ReqInfo->info.tokenLength);
+        }
+        OIC_LOG_V(DEBUG, TAG, "Request- msgID : %d", ReqInfo->info.messageId);
+        // store the data at queue.
+        CAData_t *cadata = NULL;
+        cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
+        if (NULL == cadata)
+        {
+            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
+            CADestroyRequestInfoInternal(ReqInfo);
+            coap_delete_pdu(pdu);
+            return;
+        }
 
-    if (NULL == data)
-    {
-        OIC_LOG(DEBUG, TAG, "received data is null");
-        return;
+        cadata->type = SEND_TYPE_UNICAST;
+        cadata->remoteEndpoint = CAAdapterCloneEndpoint(endpoint);
+        cadata->requestInfo = ReqInfo;
+        cadata->responseInfo = NULL;
+        CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
     }
-
-    coap_pdu_t* pdu;
-    uint32_t code = CA_NOT_FOUND;
-    pdu = CAParsePDU(data, &code);
-
-    if (code == CA_GET || code == CA_POST || code == CA_PUT || code == CA_DELETE)
+    else
     {
-        CARequestInfo_t ReqInfo;
-        memset(&ReqInfo, 0, sizeof(CARequestInfo_t));
-        CAGetRequestInfoFromPdu(pdu, &ReqInfo);
-
-        if (NULL != ReqInfo.info.options && NULL != endpoint)
+        CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
+        if (NULL == ResInfo)
         {
-            OIC_LOG_V(DEBUG, TAG, "Request PDU - optionID: %d", ReqInfo.info.options->optionID);
+            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
+            coap_delete_pdu(pdu);
+            return;
+        }
 
-            OIC_LOG_V(DEBUG, TAG, "Request PDU - optionlist: %s", ReqInfo.info.options->optionData);
+        CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
+        if (CA_STATUS_OK != res)
+        {
+            OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
+            OICFree(ResInfo);
+            coap_delete_pdu(pdu);
+            return;
+        }
 
-            OIC_LOG_V(DEBUG, TAG, "Request PDU  - payload: %s", ReqInfo.info.payload);
+        if (NULL != ResInfo->info.options)
+        {
+            uint32_t i;
+            for (i = 0; i < ResInfo->info.numOptions; i++)
+            {
+                OIC_LOG_V(DEBUG, TAG, "Response- optionID: %d", ResInfo->info.options[i].optionID);
 
-            OIC_LOG_V(DEBUG, TAG, "Request PDU  - code: %d", ReqInfo.method);
+                OIC_LOG_V(DEBUG, TAG, "Response- list: %s", ResInfo->info.options[i].optionData);
+            }
+        }
 
-            endpoint->resourceUri = (char*) OICMalloc(strlen(ReqInfo.info.options->optionData) + 1);
-            memcpy(endpoint->resourceUri, ReqInfo.info.options->optionData,
-                    strlen(ReqInfo.info.options->optionData));
-            OIC_LOG_V(DEBUG, TAG, "added resource URI : %s", endpoint->resourceUri);
+        if (NULL != ResInfo->info.payload)
+        {
+            OIC_LOG_V(DEBUG, TAG, "Response- payload: %s", ResInfo->info.payload);
+        }
+        OIC_LOG_V(DEBUG, TAG, "Response- code: %d", ResInfo->result);
+        if (NULL != ResInfo->info.token)
+        {
+            OIC_LOG(DEBUG, TAG, "Response- token:");
+            OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ResInfo->info.token,
+                           ResInfo->info.tokenLength);
         }
+        OIC_LOG_V(DEBUG, TAG, "Response- msgID: %d", ResInfo->info.messageId);
 
         // store the data at queue.
-        CAData_t* cadata = NULL;
-        cadata = (CAData_t*) OICMalloc(sizeof(CAData_t));
-        memset(cadata, 0, sizeof(CAData_t));
-        cadata->actionId = 1;
-        cadata->remoteEndpoint = endpoint;
-        cadata->requestInfo = &ReqInfo;
-        cadata->responseInfo = NULL;
-        CAAddReceiveData(cadata);
-
-    }
-    else
-    {
-        CAResponseInfo_t ResInfo;
-        memset(&ResInfo, 0, sizeof(CARequestInfo_t));
-        CAGetResponseInfoFromPdu(pdu, &ResInfo);
-
-        if (NULL != ResInfo.info.options && NULL != endpoint)
+        CAData_t *cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
+        if (NULL == cadata)
         {
-            OIC_LOG_V(DEBUG, TAG, "Response PDU - optionID: %d", ResInfo.info.options->optionID);
-
-            OIC_LOG_V(DEBUG, TAG, "Response PDU - optionlist: %s", ResInfo.info.options->optionData);
+            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
+            CADestroyResponseInfoInternal(ResInfo);
+            coap_delete_pdu(pdu);
+            return;
+        }
 
-            OIC_LOG_V(DEBUG, TAG, "Response PDU - payload: %s", ResInfo.info.payload);
+        cadata->type = SEND_TYPE_UNICAST;
+        cadata->remoteEndpoint = CAAdapterCloneEndpoint(endpoint);
+        cadata->requestInfo = NULL;
 
-            OIC_LOG_V(DEBUG, TAG, "Response PDU - code: %d", ResInfo.result);
+        // for retransmission
+        void *retransmissionPdu = NULL;
+        CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
+                                     &retransmissionPdu);
 
-            endpoint->resourceUri = (char*) OICMalloc(strlen(ResInfo.info.options->optionData) + 1);
-            memcpy(endpoint->resourceUri, ResInfo.info.options->optionData,
-                    strlen(ResInfo.info.options->optionData));
-            OIC_LOG_V(DEBUG, TAG, "added resource URI : %s", endpoint->resourceUri);
+        // get token from saved data in retransmission list
+        if (retransmissionPdu && CA_EMPTY == code)
+        {
+            CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
+                                               &(ResInfo->info));
+            if (CA_STATUS_OK != res)
+            {
+                OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
+                OICFree(ResInfo->info.token);
+            }
         }
+        OICFree(retransmissionPdu);
+        cadata->responseInfo = ResInfo;
 
-        // store the data at queue.
-        CAData_t* cadata = NULL;
-        cadata = (CAData_t*) OICMalloc(sizeof(CAData_t));
-        memset(cadata, 0, sizeof(CAData_t));
-        cadata->actionId = 1;
-        cadata->remoteEndpoint = endpoint;
-        cadata->requestInfo = NULL;
-        cadata->responseInfo = &ResInfo;
-        CAAddReceiveData(cadata);
+        CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
     }
+
+    if (pdu)
+    {
+        coap_delete_pdu(pdu);
+    }
+    OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+static void CANetworkChangedCallback(const CAEndpoint_t *info, CANetworkStatus_t status)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+
+    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 void CAHandleRequestResponseCallbacks()
 {
-    OIC_LOG_V(DEBUG, TAG, "CAHandleRequestResponseCallbacks");
 
+#ifdef SINGLE_HANDLE
     // parse the data and call the callbacks.
     // #1 parse the data
     // #2 get endpoint
 
-    u_mutex_lock(gMessageQueueMutex);
+    ca_mutex_lock(g_receiveThread.threadMutex);
 
-    u_queue_message_t* item = u_queue_get_element(gMessageQueue);
+    u_queue_message_t *item = u_queue_get_element(g_receiveThread.dataQueue);
 
-    u_mutex_unlock(gMessageQueueMutex);
+    ca_mutex_unlock(g_receiveThread.threadMutex);
 
-    if (item == NULL)
+    if (NULL == item)
+    {
         return;
+    }
 
     // get values
-    voidmsg = item->msg;
+    void *msg = item->msg;
 
-    if (msg == NULL)
+    if (NULL == msg)
+    {
         return;
+    }
 
     // get endpoint
-    CAData_t* td = (CAData_t*) msg;
+    CAData_t *td = (CAData_t *) msg;
 
-    CARemoteEndpoint_t* rep = td->remoteEndpoint;
-
-    if (rep == NULL)
-        return;
-
-    if (td->requestInfo != NULL)
+    if (td->requestInfo && g_requestHandler)
     {
-        if (gRequestHandler)
-        {
-            gRequestHandler(rep, NULL);
-        }
+        OIC_LOG_V(DEBUG, TAG, "request callback : %d", td->requestInfo->info.numOptions);
+        g_requestHandler(td->remoteEndpoint, td->requestInfo);
     }
-
-    if (td->responseInfo != NULL)
+    else if (td->responseInfo && g_responseHandler)
     {
-        if (gResponseHandler)
-        {
-            gResponseHandler(rep, NULL);
-        }
+        OIC_LOG_V(DEBUG, TAG, "response callback : %d", td->responseInfo->info.numOptions);
+        g_responseHandler(td->remoteEndpoint, td->responseInfo);
+    }
+    else if (td->errorInfo && g_errorHandler)
+    {
+        OIC_LOG_V(DEBUG, TAG, "error callback error: %d", td->errorInfo->result);
+        g_errorHandler(td->remoteEndpoint, td->errorInfo);
     }
 
-    u_queue_remove_element(gMessageQueue);
+    CADataDestroyer(msg, sizeof(CAData_t));
+
+#endif
+    OIC_LOG(DEBUG, TAG, "CAHandleRequestResponseCallbacks OUT");
 }
 
-int32_t CADetachRequestMessage(const CARemoteEndpoint_t* object, const CARequestInfo_t* request)
+CAResult_t CADetachRequestMessage(const CAEndpoint_t *object, const CARequestInfo_t *request)
 {
-    OIC_LOG_V(DEBUG, TAG, "CADetachRequestMessage");
+    OIC_LOG(DEBUG, TAG, "IN");
 
-    if (object == NULL || request == NULL)
+    VERIFY_NON_NULL(object, TAG, "object");
+    VERIFY_NON_NULL(request, TAG, "request");
+
+    if (false == CAIsSelectedNetworkAvailable())
     {
-        return -1;
+        return CA_STATUS_FAILED;
     }
 
-    int32_t id = 0;
-
-    // create action id
-    id = CAIncreaseActionId();
-
-    CAData_t* data = (CAData_t*) OICMalloc(sizeof(CAData_t));
-    MEMORY_ALLOCK_CHECK(data);
-
-    // initialize
-    memset(data, 0, sizeof(CAData_t));
+    CAEndpoint_t *remoteEndpoint = NULL;
+    CARequestInfo_t *requestInfo = NULL;
+    CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
+    CA_MEMORY_ALLOC_CHECK(data);
 
     // clone remote endpoint
-    CARemoteEndpoint_t* remoteEndpoint = CACloneRemoteEndpoint(object);
-    MEMORY_ALLOCK_CHECK(remoteEndpoint);
+    remoteEndpoint = CACloneEndpoint(object);
+    CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
 
     // clone request info
-    CARequestInfo_t* requestInfo = CACloneRequestInfo(request);
-    MEMORY_ALLOCK_CHECK(requestInfo);
+    requestInfo = CACloneRequestInfo(request);
+    CA_MEMORY_ALLOC_CHECK(requestInfo);
 
     // save data
-    data->actionId = id;
+    data->type = request->isMulticast ? SEND_TYPE_MULTICAST : SEND_TYPE_UNICAST;
     data->remoteEndpoint = remoteEndpoint;
     data->requestInfo = requestInfo;
     data->responseInfo = NULL;
+    data->options = NULL;
+    data->numOptions = 0;
+    if (NULL != requestInfo->info.options && 0 < requestInfo->info.numOptions)
+    {
+        uint8_t numOptions = requestInfo->info.numOptions;
+        // copy data
+        CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
+                                                                        * numOptions);
+        CA_MEMORY_ALLOC_CHECK(headerOption);
 
-    // add thread
-    CAAddSendData(data);
-
-    return id;
+        memcpy(headerOption, requestInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
 
-    // memory error label.
-    memory_error_exit:
+        data->options = headerOption;
+        data->numOptions = numOptions;
+    }
 
-    CADestroyRemoteEndpointInternal(remoteEndpoint);
+    // add thread
+    CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
 
+// memory error label.
+memory_error_exit:
+    CAAdapterFreeEndpoint(remoteEndpoint);
     CADestroyRequestInfoInternal(requestInfo);
 
-    if (data != NULL)
-    {
-        OICFree(data);
-    }
-
-    return -1;
+    OICFree(data);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_MEMORY_ALLOC_FAILED;
 }
 
-int32_t CADetachResponseMessage(const CARemoteEndpoint_t* object, const CAResponseInfo_t* response)
+CAResult_t CADetachResponseMessage(const CAEndpoint_t *object,
+                                   const CAResponseInfo_t *response)
 {
-    OIC_LOG_V(DEBUG, TAG, "CADetachResponseMessage");
+    OIC_LOG(DEBUG, TAG, "IN");
+    VERIFY_NON_NULL(object, TAG, "object");
+    VERIFY_NON_NULL(response, TAG, "response");
 
-    if (object == NULL || response == NULL)
+    if (false == CAIsSelectedNetworkAvailable())
     {
-        return -1;
+        return CA_STATUS_FAILED;
     }
 
-    int32_t id = 0;
-
-    // create action id
-    id = CAIncreaseActionId();
+    CAEndpoint_t *remoteEndpoint = NULL;
+    CAResponseInfo_t *responseInfo = NULL;
 
-    CAData_t* data = (CAData_t*) OICMalloc(sizeof(CAData_t));
-    MEMORY_ALLOCK_CHECK(data);
-
-    // initialize
-    memset(data, 0, sizeof(CAData_t));
+    // allocate & initialize
+    CAData_t *data = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
+    CA_MEMORY_ALLOC_CHECK(data);
 
     // clone remote endpoint
-    CARemoteEndpoint_t* remoteEndpoint = CACloneRemoteEndpoint(object);
-    MEMORY_ALLOCK_CHECK(remoteEndpoint);
+    remoteEndpoint = CACloneEndpoint(object);
+    CA_MEMORY_ALLOC_CHECK(remoteEndpoint);
 
     // clone response info
-    CAResponseInfo_t* responseInfo = CACloneResponseInfo(response);
-    MEMORY_ALLOCK_CHECK(responseInfo);
+    responseInfo = CACloneResponseInfo(response);
+    CA_MEMORY_ALLOC_CHECK(responseInfo);
 
     // save data
-    data->actionId = id;
+    data->type = SEND_TYPE_UNICAST;
     data->remoteEndpoint = remoteEndpoint;
     data->requestInfo = NULL;
     data->responseInfo = responseInfo;
-
-    // add thread
-    CAAddSendData(data);
-
-    return id;
-
-    // memory error label.
-    memory_error_exit:
-
-    CADestroyRemoteEndpointInternal(remoteEndpoint);
-
-    CADestroyResponseInfoInternal(responseInfo);
-
-    if (data != NULL)
+    data->options = NULL;
+    data->numOptions = 0;
+    if (NULL != responseInfo->info.options && 0 < responseInfo->info.numOptions)
     {
-        OICFree(data);
-    }
+        uint8_t numOptions = responseInfo->info.numOptions;
+        // copy data
+        CAHeaderOption_t *headerOption = (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t)
+                                                                        * numOptions);
+        CA_MEMORY_ALLOC_CHECK(headerOption);
 
-    return -1;
-}
+        memcpy(headerOption, responseInfo->info.options, sizeof(CAHeaderOption_t) * numOptions);
 
-int32_t CADetachMessageResourceUri(const CAURI_t resourceUri)
-{
-    if (resourceUri == NULL)
-    {
-        return -1;
+        data->options = headerOption;
+        data->numOptions = numOptions;
     }
 
-    int32_t id = 0;
-
-    // create action id
-    id = CAIncreaseActionId();
-
-    CAData_t* data = (CAData_t*) OICMalloc(sizeof(CAData_t));
-    MEMORY_ALLOCK_CHECK(data);
-
-    // initialize
-    memset(data, 0, sizeof(CAData_t));
-
-    CAAddress_t addr;
-    memset(&addr, 0, sizeof(CAAddress_t));
-    CARemoteEndpoint_t* remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, addr,
-            CA_ETHERNET | CA_WIFI | CA_EDR | CA_LE);
-
-    // save data
-    data->actionId = id;
-    data->remoteEndpoint = remoteEndpoint;
-    data->requestInfo = NULL;
-    data->responseInfo = NULL;
-
     // add thread
-    CAAddSendData(data);
-
-    return id;
+    CAQueueingThreadAddData(&g_sendThread, data, sizeof(CAData_t));
 
-    // memory error label.
-    memory_error_exit:
-
-    CADestroyRemoteEndpointInternal(remoteEndpoint);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
 
-    if (data != NULL)
-    {
-        OICFree(data);
-    }
+// memory error label.
+memory_error_exit:
+    CAAdapterFreeEndpoint(remoteEndpoint);
+    CADestroyResponseInfoInternal(responseInfo);
+    OICFree(data);
+    OIC_LOG(DEBUG, TAG, "OUT");
 
-    return -1;
+    return CA_MEMORY_ALLOC_FAILED;
 }
 
-void CASetMessageHandlerCallback(CAMessageHandlerCallback callback)
+CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
+                                      uint8_t tokenLength, const CAHeaderOption_t *options,
+                                      uint8_t numOptions)
 {
-    OIC_LOG_V(DEBUG, TAG, "set message handler callback.");
-
-    gHandlerCallback = callback;
+    return CA_NOT_SUPPORTED;
 }
 
-void CASetRequestResponseCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
+void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
+                             CAErrorCallback errroHandler)
 {
-    OIC_LOG_V(DEBUG, TAG, "set request, response handler callback.");
-
-    gRequestHandler = ReqHandler;
-    gResponseHandler = RespHandler;
+    OIC_LOG(DEBUG, TAG, "IN");
+    g_requestHandler = ReqHandler;
+    g_responseHandler = RespHandler;
+    g_errorHandler = errroHandler;
+    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 CAResult_t CAInitializeMessageHandler()
 {
+    OIC_LOG(DEBUG, TAG, "IN");
     CASetPacketReceivedCallback(CAReceivedPacketCallback);
 
+    CASetNetworkChangeCallback(CANetworkChangedCallback);
+    CASetErrorHandleCallback(CAErrorHandler);
+
     // create thread pool
-    CAResult_t res;
-    res = u_thread_pool_init(MAX_THREAD_POOL_SIZE, &gThreadPoolHandle);
+    CAResult_t res = ca_thread_pool_init(MAX_THREAD_POOL_SIZE, &g_threadPoolHandle);
 
     if (res != CA_STATUS_OK)
     {
-        OIC_LOG_V(DEBUG, TAG, "thread pool initialize error.");
+        OIC_LOG(ERROR, TAG, "thread pool initialize error.");
         return res;
     }
 
     // send thread initialize
-    memset(&gSendThread, 0, sizeof(CAThread_t));
+    if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_sendThread, g_threadPoolHandle,
+                                                   CASendThreadProcess, CADataDestroyer))
+    {
+        OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
+        return CA_STATUS_FAILED;
+    }
 
-    // mutex init
-    u_mutex_init();
+    // start send thread
+    res = CAQueueingThreadStart(&g_sendThread);
 
-    // set send thread data
-    gSendThread.dataQueue = u_queue_create();
-    gSendThread.threadMutex = u_mutex_new();
-    gSendThread.threadCond = u_cond_new();
-    gSendThread.isStop = FALSE;
-    gSendThread.threadTask = CASendThreadProcess;
+    if (res != CA_STATUS_OK)
+    {
+        OIC_LOG(ERROR, TAG, "thread start error(send thread).");
+        ca_thread_pool_free(g_threadPoolHandle);
+        g_threadPoolHandle = NULL;
+        return res;
+    }
 
-    // start send thread
-    res = u_thread_pool_add_task(gThreadPoolHandle, CAThreadBaseRoutine, &gSendThread);
+    // receive thread initialize
+    if (CA_STATUS_OK != CAQueueingThreadInitialize(&g_receiveThread, g_threadPoolHandle,
+                                                   CAReceiveThreadProcess, CADataDestroyer))
+    {
+        OIC_LOG(ERROR, TAG, "Failed to Initialize receive queue thread");
+        return CA_STATUS_FAILED;
+    }
+
+#ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
+    // start receive thread
+    res = CAQueueingThreadStart(&gReceiveThread);
 
     if (res != CA_STATUS_OK)
     {
-        OIC_LOG_V(DEBUG, TAG, "thread pool add task error.");
+        OIC_LOG(ERROR, TAG, "thread start error(receive thread).");
         return res;
     }
+#endif
 
-    // set receive queue
-    gMessageQueue = u_queue_create();
-    gMessageQueueMutex = u_mutex_new();
+    // retransmission initialize
+    CARetransmissionInitialize(&g_retransmissionContext, g_threadPoolHandle, CASendUnicastData,
+                               CATimeoutCallback, NULL);
 
-    // initialize interface adapters by controller
-    CAInitializeAdapters();
+    // start retransmission
+    res = CARetransmissionStart(&g_retransmissionContext);
 
+    if (res != CA_STATUS_OK)
+    {
+        OIC_LOG(ERROR, TAG, "thread start error(retransmission thread).");
+        return res;
+    }
+
+    // initialize interface adapters by controller
+    CAInitializeAdapters(g_threadPoolHandle);
+    OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
 void CATerminateMessageHandler()
 {
-    // terminate interface adapters by controller
-    CATerminateAdapters();
+    OIC_LOG(DEBUG, TAG, "IN");
+    CATransportAdapter_t connType;
+    u_arraylist_t *list = CAGetSelectedNetworkList();
+    uint32_t length = u_arraylist_length(list);
+
+    uint32_t i = 0;
+    for (i = 0; i < length; i++)
+    {
+        void* ptrType = u_arraylist_get(list, i);
+
+        if (NULL == ptrType)
+        {
+            continue;
+        }
+
+        connType = *(CATransportAdapter_t *)ptrType;
+        CAStopAdapter(connType);
+    }
+
+    // stop retransmission
+    if (NULL != g_retransmissionContext.threadMutex)
+    {
+        CARetransmissionStop(&g_retransmissionContext);
+    }
 
     // stop thread
-    CAStopSendThread();
+    // delete thread data
+    if (NULL != g_sendThread.threadMutex)
+    {
+        CAQueueingThreadStop(&g_sendThread);
+    }
 
+    // stop thread
     // delete thread data
-    u_mutex_free(gSendThread.threadMutex);
-    u_cond_free(gSendThread.threadCond);
-    u_queue_delete(gSendThread.dataQueue);
+    if (NULL != g_receiveThread.threadMutex)
+    {
+#ifndef SINGLE_HANDLE // This will be enabled when RI supports multi threading
+        CAQueueingThreadStop(&gReceiveThread);
+#endif
+    }
 
     // destroy thread pool
-    u_thread_pool_free(gThreadPoolHandle);
+    if (NULL != g_threadPoolHandle)
+    {
+        ca_thread_pool_free(g_threadPoolHandle);
+        g_threadPoolHandle = NULL;
+    }
+
+    CARetransmissionDestroy(&g_retransmissionContext);
+    CAQueueingThreadDestroy(&g_sendThread);
+    CAQueueingThreadDestroy(&g_receiveThread);
+
+    // terminate interface adapters by controller
+    CATerminateAdapters();
+
+    OIC_LOG(DEBUG, TAG, "OUT");
+}
+
+void CALogPDUInfo(coap_pdu_t *pdu)
+{
+    VERIFY_NON_NULL_VOID(pdu, TAG, "pdu");
+
+    OIC_LOG_V(DEBUG, TAG, "PDU Maker - payload : %s", pdu->data);
+
+    OIC_LOG_V(DEBUG, TAG, "PDU Maker - type : %d", pdu->hdr->type);
 
-    OIC_LOG_V(DEBUG, TAG, "message handler terminate completed!");
+    OIC_LOG_V(DEBUG, TAG, "PDU Maker - code : %d", pdu->hdr->code);
 
-    u_queue_delete(gMessageQueue);
-    u_mutex_free(gMessageQueueMutex);
+    OIC_LOG_V(DEBUG, TAG, "PDU Maker - id : %d", ntohs(pdu->hdr->id));
+
+    OIC_LOG(DEBUG, TAG, "PDU Maker - token :");
+
+    OIC_LOG_BUFFER(DEBUG, TAG, pdu->hdr->token, pdu->hdr->token_length);
 }
 
+void CAErrorHandler(const CAEndpoint_t *endpoint,
+                    const void *data, uint32_t dataLen,
+                    CAResult_t result)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
+    VERIFY_NON_NULL_VOID(data, TAG, "data");
+
+    uint32_t code = CA_NOT_FOUND;
+    //Do not free remoteEndpoint and data. Currently they will be freed in data thread
+    //Get PDU data
+    coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code);
+    if (NULL == pdu)
+    {
+        OIC_LOG(ERROR, TAG, "Parse PDU failed");
+        return;
+    }
+
+    CAErrorInfo_t *errorInfo = (CAErrorInfo_t *)OICCalloc(1, sizeof (CAErrorInfo_t));
+    if (NULL == errorInfo)
+    {
+        OIC_LOG(ERROR, TAG, "CAErrorHandler, Memory allocation failed!");
+        coap_delete_pdu(pdu);
+        return;
+    }
+
+    CAResult_t res = CAGetErrorInfoFromPDU(pdu, errorInfo);
+    if (CA_STATUS_OK != res)
+    {
+        OIC_LOG_V(ERROR, TAG, "CAGetErrorInfoFromPDU failed : %d", res);
+        OICFree(errorInfo);
+        coap_delete_pdu(pdu);
+       return;
+    }
+
+    errorInfo->result = result;
+    OIC_LOG_V(DEBUG, TAG, "error : %d", result);
+    if (NULL != errorInfo->info.payload)
+    {
+        OIC_LOG_V(DEBUG, TAG, "error, payload: %s", errorInfo->info.payload);
+    }
+
+    OIC_LOG(DEBUG, TAG, "error, token");
+    OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) errorInfo->info.token,
+                   errorInfo->info.tokenLength);
+    OIC_LOG_V(DEBUG, TAG, "CAErrorHandler, msgID : %d", errorInfo->info.messageId);
+
+    CAEndpoint_t *rep = NULL;
+    rep = CACloneEndpoint(endpoint);
+    if (!rep)
+    {
+        OIC_LOG(ERROR, TAG, "CAErrorHandler, CloneEndpoint Failed");
+        OICFree(errorInfo);
+        coap_delete_pdu(pdu);
+        return;
+    }
+
+    // store the data at queue.
+    CAData_t *cadata = NULL;
+    cadata = (CAData_t *) OICCalloc(1, sizeof(CAData_t));
+    if (NULL == cadata)
+    {
+        OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed !");
+        CADestroyEndpointInternal(rep);
+        OICFree(errorInfo);
+        coap_delete_pdu(pdu);
+        return;
+    }
+
+    cadata->remoteEndpoint = rep;
+    cadata->requestInfo = NULL;
+    cadata->responseInfo = NULL;
+    cadata->errorInfo = errorInfo;
+    cadata->dataType = CA_ERROR_DATA;
+
+    CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
+    coap_delete_pdu(pdu);
+
+    return;
+}