replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caretransmission.c
index 37b97fb..611ee5d 100644 (file)
@@ -28,7 +28,6 @@
 // For details on compatibility and glibc support,
 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
 #define _DEFAULT_SOURCE
-#define _BSD_SOURCE
 
 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
 // causes header files to expose definitions
 #define _POSIX_C_SOURCE 200809L
 #endif
 
+#ifdef __TIZENRT__
+#include <tinyara/config.h>
+#endif
+
+#include "iotivity_config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef TB_LOG
+#include <inttypes.h>
+#endif
+
+#ifndef SINGLE_THREAD
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#include <time.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_SYS_TIMEB_H
+#include <sys/timeb.h>
+#endif
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
+#endif
 
 #if defined(__ANDROID__)
 #include <linux/time.h>
 #include "caremotehandler.h"
 #include "caprotocolmessage.h"
 #include "oic_malloc.h"
+#include "oic_time.h"
+#include "ocrandom.h"
 #include "logger.h"
 
-#define TAG PCF("CA")
+#define TAG "OIC_CA_RETRANS"
 
 typedef struct
 {
     uint64_t timeStamp;                 /**< last sent time. microseconds */
+#ifndef SINGLE_THREAD
     uint64_t timeout;                   /**< timeout value. microseconds */
+#endif
     uint8_t triedCount;                 /**< retransmission count */
     uint16_t messageId;                 /**< coap PDU message id */
+    CADataType_t dataType;              /**< data Type (Request/Response) */
     CAEndpoint_t *endpoint;             /**< remote endpoint */
     void *pdu;                          /**< coap PDU */
     uint32_t size;                      /**< coap PDU size */
 } CARetransmissionData_t;
 
 static const uint64_t USECS_PER_SEC = 1000000;
+static const uint64_t MSECS_PER_SEC = 1000;
 
+#ifndef SINGLE_THREAD
 /**
- * @brief   getCurrent monotonic time
- * @return  current time in microseconds
+ * @brief   timeout value is
+ *          between DEFAULT_ACK_TIMEOUT_SEC and
+ *          (DEFAULT_ACK_TIMEOUT_SEC * DEFAULT_RANDOM_FACTOR) second.
+ *          DEFAULT_RANDOM_FACTOR       1.5 (CoAP)
+ * @return  microseconds.
  */
-uint64_t getCurrentTimeInMicroSeconds();
+static uint64_t CAGetTimeoutValue()
+{
+    return ((DEFAULT_ACK_TIMEOUT_SEC * MSECS_PER_SEC)
+            + ((MSECS_PER_SEC * OCGetRandomByte()) >> 8)) * MSECS_PER_SEC;
+}
+
+CAResult_t CARetransmissionStart(CARetransmission_t *context)
+{
+    if (NULL == context)
+    {
+        OIC_LOG(ERROR, TAG, "context is empty");
+        return CA_STATUS_INVALID_PARAM;
+    }
+
+    if (NULL == context->threadPool)
+    {
+        OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
+        return CA_STATUS_INVALID_PARAM;
+    }
+#ifndef __TIZENRT__
+    CAResult_t res = ca_thread_pool_add_task(context->threadPool, CARetransmissionBaseRoutine,
+                                             context, NULL);
+#else
+    CAResult_t res = ca_thread_pool_add_task(context->threadPool, CARetransmissionBaseRoutine,
+                                             context, NULL, "IoT_Retransmit",
+                                             CONFIG_IOTIVITY_RETRANSMIT_PTHREAD_STACKSIZE);
+#endif
+    if (CA_STATUS_OK != res)
+    {
+        OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
+        return res;
+    }
+
+    return res;
+}
+#endif
 
 /**
  * @brief   check timeout routine
  * @param   currentTime     [IN]microseconds
- * @param   timeStamp       [IN]microseconds
- * @param   timeoutValue    [IN]microseconds
- * @param   triedCount      [IN]Number of retransmission tried
+ * @param   retData         [IN]retransmission data
  * @return  true if the timeout period has elapsed, false otherwise
  */
-static bool CACheckTimeout(uint64_t currentTime, uint64_t timeStamp, uint64_t timeoutValue,
-                           uint8_t triedCount)
+static bool CACheckTimeout(uint64_t currentTime, CARetransmissionData_t *retData)
 {
+#ifndef SINGLE_THREAD
     // #1. calculate timeout
-    uint32_t milliTimeoutValue = timeoutValue * 0.001;
-    uint64_t timeout = (milliTimeoutValue << triedCount) * (uint64_t) 1000;
+    uint32_t milliTimeoutValue = retData->timeout * 0.001;
+    uint64_t timeout = ((uint64_t) milliTimeoutValue << retData->triedCount) * MSECS_PER_SEC;
 
-    if (currentTime >= timeStamp + timeout)
+    if (currentTime >= retData->timeStamp + timeout)
     {
-        OIC_LOG_V(DEBUG, TAG, "%d microseconds time out!!, tried count(%ld)", timeout, triedCount);
+#ifndef __TIZENRT__
+        OIC_LOG_V(DEBUG, TAG, "%" PRIu64 " microseconds time out!!, tried count(%d)",
+                  timeout, retData->triedCount);
+#endif
         return true;
     }
+#else
+    // #1. calculate timeout
+    uint64_t timeOut = (2 << retData->triedCount) * USECS_PER_SEC;
 
+    if (currentTime >= retData->timeStamp + timeOut)
+    {
+        OIC_LOG_V(DEBUG, TAG, "timeout=%d, tried cnt=%d",
+                  (2 << retData->triedCount), retData->triedCount);
+        return true;
+    }
+#endif
     return false;
 }
 
-/**
- * @brief   timeout value is
- *          between DEFAULT_ACK_TIMEOUT_SEC and
- *          (DEFAULT_ACK_TIMEOUT_SEC * DEFAULT_RANDOM_FACTOR) second.
- *          DEFAULT_RANDOM_FACTOR       1.5 (CoAP)
- * @return  microseconds.
- */
-static uint64_t CAGetTimeoutValue()
-{
-    return ((DEFAULT_ACK_TIMEOUT_SEC * 1000) + ((1000 * (random() & 0xFF)) >> 8)) *
-            (uint64_t) 1000;
-}
-
 static void CACheckRetransmissionList(CARetransmission_t *context)
 {
     if (NULL == context)
     {
-        OIC_LOG(ERROR, TAG, "context is null..");
+        OIC_LOG(ERROR, TAG, "context is null");
         return;
     }
 
     // mutex lock
-    ca_mutex_lock(context->threadMutex);
+    oc_mutex_lock(context->threadMutex);
 
     uint32_t i = 0;
     uint32_t len = u_arraylist_length(context->dataList);
@@ -140,16 +202,17 @@ static void CACheckRetransmissionList(CARetransmission_t *context)
             continue;
         }
 
-        uint64_t currentTime = getCurrentTimeInMicroSeconds();
+        uint64_t currentTime = OICGetCurrentTime(TIME_IN_US);
 
-        if (CACheckTimeout(currentTime, retData->timeStamp, retData->timeout, retData->triedCount))
+        if (CACheckTimeout(currentTime, retData))
         {
             // #2. if time's up, send the data.
             if (NULL != context->dataSendMethod)
             {
-                OIC_LOG_V(DEBUG, TAG, "retransmission CON data!!, message id(%d)",
+                OIC_LOG_V(DEBUG, TAG, "retransmission CON data!!, msgid=%d",
                           retData->messageId);
-                context->dataSendMethod(retData->endpoint, retData->pdu, retData->size);
+                context->dataSendMethod(retData->endpoint, retData->pdu,
+                                        retData->size, retData->dataType);
             }
 
             // #3. increase the retransmission count and update timestamp.
@@ -161,58 +224,64 @@ static void CACheckRetransmissionList(CARetransmission_t *context)
         if (retData->triedCount >= context->config.tryingCount)
         {
             CARetransmissionData_t *removedData = u_arraylist_remove(context->dataList, i);
-
-            if (NULL != removedData)
+            if (NULL == removedData)
             {
-                OIC_LOG_V(DEBUG, TAG, "max trying count, remove retransmission CON data!!,\
-                          message id(%d)", removedData->messageId);
-
-                // callback for retransmit timeout
-                if (NULL != context->timeoutCallback)
-                {
-                    context->timeoutCallback(removedData->endpoint, removedData->pdu,
-                                             removedData->size);
-                }
-
-                CADestroyEndpointInternal(removedData->endpoint);
-                OICFree(removedData->pdu);
-
-                OICFree(removedData);
-
-                // modify loop value.
-                len = u_arraylist_length(context->dataList);
-
-                --i;
+                OIC_LOG(ERROR, TAG, "Removed data is NULL");
+                // mutex unlock
+                oc_mutex_unlock(context->threadMutex);
+                return;
             }
-            else
+            OIC_LOG_V(DEBUG, TAG, "max trying count, remove RTCON data,"
+                      "msgid=%d", removedData->messageId);
+
+            // callback for retransmit timeout
+            if (NULL != context->timeoutCallback)
             {
-                OIC_LOG(ERROR, TAG, "arraylist remove error");
+                context->timeoutCallback(removedData->endpoint, removedData->pdu,
+                                         removedData->size);
             }
 
+            CAFreeEndpoint(removedData->endpoint);
+            OICFree(removedData->pdu);
+
+            OICFree(removedData);
+
+            // modify loop value.
+            len = u_arraylist_length(context->dataList);
+            --i;
         }
     }
 
     // mutex unlock
-    ca_mutex_unlock(context->threadMutex);
+    oc_mutex_unlock(context->threadMutex);
 }
 
-static void CARetransmissionBaseRoutine(void *threadValue)
+void CARetransmissionBaseRoutine(void *threadValue)
 {
-    OIC_LOG(DEBUG, TAG, "retransmission main thread start..");
+    OIC_LOG(DEBUG, TAG, "retransmission main thread start");
 
     CARetransmission_t *context = (CARetransmission_t *) threadValue;
 
     if (NULL == context)
     {
-        OIC_LOG(ERROR, TAG, "thread data passing error!!");
+        OIC_LOG(ERROR, TAG, "thread data passing error");
 
         return;
     }
 
+#ifdef SINGLE_THREAD
+    if (true == context->isStop)
+    {
+        OIC_LOG(DEBUG, TAG, "thread stopped");
+        return;
+    }
+    CACheckRetransmissionList(context);
+#else
+
     while (!context->isStop)
     {
         // mutex lock
-        ca_mutex_lock(context->threadMutex);
+        oc_mutex_lock(context->threadMutex);
 
         if (!context->isStop && u_arraylist_length(context->dataList) <= 0)
         {
@@ -220,19 +289,21 @@ static void CARetransmissionBaseRoutine(void *threadValue)
             OIC_LOG(DEBUG, TAG, "wait..there is no retransmission data.");
 
             // wait
-            ca_cond_wait(context->threadCond, context->threadMutex);
+            oc_cond_wait(context->threadCond, context->threadMutex);
 
             OIC_LOG(DEBUG, TAG, "wake up..");
         }
         else if (!context->isStop)
         {
             // check each RETRANSMISSION_CHECK_PERIOD_SEC time.
-            OIC_LOG_V(DEBUG, TAG, "wait..(%ld)microseconds",
+#ifndef __TIZENRT__
+            OIC_LOG_V(DEBUG, TAG, "wait..(%" PRIu64 ")microseconds",
                       RETRANSMISSION_CHECK_PERIOD_SEC * (uint64_t) USECS_PER_SEC);
+#endif
 
             // wait
             uint64_t absTime = RETRANSMISSION_CHECK_PERIOD_SEC * (uint64_t) USECS_PER_SEC;
-            ca_cond_wait_for(context->threadCond, context->threadMutex, absTime );
+            oc_cond_wait_for(context->threadCond, context->threadMutex, absTime );
         }
         else
         {
@@ -240,7 +311,7 @@ static void CARetransmissionBaseRoutine(void *threadValue)
         }
 
         // mutex unlock
-        ca_mutex_unlock(context->threadMutex);
+        oc_mutex_unlock(context->threadMutex);
 
         // check stop flag
         if (context->isStop)
@@ -251,52 +322,49 @@ static void CARetransmissionBaseRoutine(void *threadValue)
         CACheckRetransmissionList(context);
     }
 
-    ca_mutex_lock(context->threadMutex);
-    ca_cond_signal(context->threadCond);
-    ca_mutex_unlock(context->threadMutex);
+    oc_mutex_lock(context->threadMutex);
+    oc_cond_signal(context->threadCond);
+    oc_mutex_unlock(context->threadMutex);
 
-    OIC_LOG(DEBUG, TAG, "retransmission main thread end..");
+#endif
+    OIC_LOG(DEBUG, TAG, "retransmission main thread end");
 
 }
 
-CAResult_t CARetransmissionInitialize(CARetransmission_t *context, ca_thread_pool_t handle,
+CAResult_t CARetransmissionInitialize(CARetransmission_t *context,
+                                      ca_thread_pool_t handle,
                                       CADataSendMethod_t retransmissionSendMethod,
                                       CATimeoutCallback_t timeoutCallback,
                                       CARetransmissionConfig_t* config)
 {
     if (NULL == context)
     {
-        OIC_LOG(ERROR, TAG, "thread instance is empty..");
+        OIC_LOG(ERROR, TAG, "thread instance is empty");
         return CA_STATUS_INVALID_PARAM;
     }
-
+#ifndef SINGLE_THREAD
     if (NULL == handle)
     {
-        OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
+        OIC_LOG(ERROR, TAG, "thread pool handle is empty");
         return CA_STATUS_INVALID_PARAM;
     }
-
-    OIC_LOG(DEBUG, TAG, "thread initialize..");
+#endif
+    OIC_LOG(DEBUG, TAG, "thread initialize");
 
     memset(context, 0, sizeof(CARetransmission_t));
 
-    CARetransmissionConfig_t cfg = { 0 };
+    CARetransmissionConfig_t cfg = { .supportType = DEFAULT_RETRANSMISSION_TYPE,
+                                     .tryingCount = DEFAULT_RETRANSMISSION_COUNT };
 
-    if (NULL == config)
-    {
-        // setDefault
-        cfg.supportType = DEFAULT_RETRANSMISSION_TYPE;
-        cfg.tryingCount = DEFAULT_MAX_RETRANSMIT;
-    }
-    else
+    if (config)
     {
         cfg = *config;
     }
 
     // set send thread data
     context->threadPool = handle;
-    context->threadMutex = ca_mutex_new();
-    context->threadCond = ca_cond_new();
+    context->threadMutex = oc_mutex_new();
+    context->threadCond = oc_cond_new();
     context->dataSendMethod = retransmissionSendMethod;
     context->timeoutCallback = timeoutCallback;
     context->config = cfg;
@@ -306,47 +374,21 @@ CAResult_t CARetransmissionInitialize(CARetransmission_t *context, ca_thread_poo
     return CA_STATUS_OK;
 }
 
-CAResult_t CARetransmissionStart(CARetransmission_t *context)
-{
-    if (NULL == context)
-    {
-        OIC_LOG(ERROR, TAG, "context is empty..");
-        return CA_STATUS_INVALID_PARAM;
-    }
-
-    if (NULL == context->threadPool)
-    {
-        OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
-        return CA_STATUS_INVALID_PARAM;
-    }
-
-    CAResult_t res = ca_thread_pool_add_task(context->threadPool, CARetransmissionBaseRoutine,
-                                            context);
-
-    if (CA_STATUS_OK != res)
-    {
-        OIC_LOG(ERROR, TAG, "thread pool add task error(send thread).");
-        return res;
-    }
-
-    return res;
-}
-
 CAResult_t CARetransmissionSentData(CARetransmission_t *context,
                                     const CAEndpoint_t *endpoint,
+                                    CADataType_t dataType,
                                     const void *pdu, uint32_t size)
 {
     if (NULL == context || NULL == endpoint || NULL == pdu)
     {
-        OIC_LOG(ERROR, TAG, "invalid parameter..");
+        OIC_LOG(ERROR, TAG, "invalid parameter");
         return CA_STATUS_INVALID_PARAM;
     }
 
     // #0. check support transport type
     if (!(context->config.supportType & endpoint->adapter))
     {
-        OIC_LOG_V(DEBUG, TAG, "not supported transport type for retransmission..(%d)",
-                  endpoint->adapter);
+        OIC_LOG_V(DEBUG, TAG, "not supported transport type=%d", endpoint->adapter);
         return CA_NOT_SUPPORTED;
     }
 
@@ -354,11 +396,11 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
     CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
     uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
 
-    OIC_LOG_V(DEBUG, TAG, "sent pdu, message type(%d), message id(%d)", type, messageId);
+    OIC_LOG_V(DEBUG, TAG, "sent pdu, msgtype=%d, msgid=%d", type, messageId);
 
     if (CA_MSG_CONFIRM != type)
     {
-        OIC_LOG(DEBUG, TAG, "not supported message type for retransmission..");
+        OIC_LOG(DEBUG, TAG, "not supported message type");
         return CA_NOT_SUPPORTED;
     }
 
@@ -368,7 +410,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
 
     if (NULL == retData)
     {
-        OIC_LOG(ERROR, TAG, "memory error!!");
+        OIC_LOG(ERROR, TAG, "memory error");
         return CA_MEMORY_ALLOC_FAILED;
     }
 
@@ -377,7 +419,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
     if (NULL == pduData)
     {
         OICFree(retData);
-        OIC_LOG(ERROR, TAG, "memory error!!");
+        OIC_LOG(ERROR, TAG, "memory error");
         return CA_MEMORY_ALLOC_FAILED;
     }
     memcpy(pduData, pdu, size);
@@ -388,21 +430,24 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
     {
         OICFree(retData);
         OICFree(pduData);
-        OIC_LOG(ERROR, TAG, "memory error!!");
+        OIC_LOG(ERROR, TAG, "memory error");
         return CA_MEMORY_ALLOC_FAILED;
     }
 
     // #2. add additional information. (time stamp, retransmission count...)
-    retData->timeStamp = getCurrentTimeInMicroSeconds();
+    retData->timeStamp = OICGetCurrentTime(TIME_IN_US);
+#ifndef SINGLE_THREAD
     retData->timeout = CAGetTimeoutValue();
+#endif
     retData->triedCount = 0;
     retData->messageId = messageId;
     retData->endpoint = remoteEndpoint;
     retData->pdu = pduData;
     retData->size = size;
-
+    retData->dataType = dataType;
+#ifndef SINGLE_THREAD
     // mutex lock
-    ca_mutex_lock(context->threadMutex);
+    oc_mutex_lock(context->threadMutex);
 
     uint32_t i = 0;
     uint32_t len = u_arraylist_length(context->dataList);
@@ -424,7 +469,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
             OIC_LOG(ERROR, TAG, "Duplicate message ID");
 
             // mutex unlock
-            ca_mutex_unlock(context->threadMutex);
+            oc_mutex_unlock(context->threadMutex);
 
             OICFree(retData);
             OICFree(pduData);
@@ -433,14 +478,41 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
         }
     }
 
-    u_arraylist_add(context->dataList, (void *) retData);
+    bool res = u_arraylist_add(context->dataList, (void *) retData);
+    if (!res)
+    {
+        OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
+
+        oc_mutex_unlock(context->threadMutex);
+
+        OICFree(retData);
+        OICFree(pduData);
+        OICFree(remoteEndpoint);
+        return CA_MEMORY_ALLOC_FAILED;
+    }
 
     // notify the thread
-    ca_cond_signal(context->threadCond);
+    oc_cond_signal(context->threadCond);
 
     // mutex unlock
-    ca_mutex_unlock(context->threadMutex);
+    oc_mutex_unlock(context->threadMutex);
+
+#else
+    bool res = u_arraylist_add(context->dataList, (void *) retData);
+    if (!res)
+    {
+        OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
+
+        oc_mutex_unlock(context->threadMutex);
+
+        OICFree(retData);
+        OICFree(pduData);
+        OICFree(remoteEndpoint);
+        return CA_MEMORY_ALLOC_FAILED;
+    }
 
+    CACheckRetransmissionList(context);
+#endif
     return CA_STATUS_OK;
 }
 
@@ -448,18 +520,17 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
                                         const CAEndpoint_t *endpoint, const void *pdu,
                                         uint32_t size, void **retransmissionPdu)
 {
-    OIC_LOG(DEBUG, TAG, "IN - CARetransmissionReceivedData");
+    OIC_LOG(DEBUG, TAG, "IN");
     if (NULL == context || NULL == endpoint || NULL == pdu || NULL == retransmissionPdu)
     {
-        OIC_LOG(ERROR, TAG, "invalid parameter..");
+        OIC_LOG(ERROR, TAG, "invalid parameter");
         return CA_STATUS_INVALID_PARAM;
     }
 
     // #0. check support transport type
     if (!(context->config.supportType & endpoint->adapter))
     {
-        OIC_LOG_V(DEBUG, TAG, "not supported transport type for retransmission..(%d)",
-                  endpoint->adapter);
+        OIC_LOG_V(DEBUG, TAG, "not supported transport type=%d", endpoint->adapter);
         return CA_STATUS_OK;
     }
 
@@ -467,16 +538,19 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
     // ACK, RST --> remove the CON data
     CAMessageType_t type = CAGetMessageTypeFromPduBinaryData(pdu, size);
     uint16_t messageId = CAGetMessageIdFromPduBinaryData(pdu, size);
+    CAResponseResult_t code = CAGetCodeFromPduBinaryData(pdu, size);
 
-    OIC_LOG_V(DEBUG, TAG, "received pdu, message type(%d), message id(%d)", type, messageId);
+    OIC_LOG_V(DEBUG, TAG, "received pdu, msgtype=%d, msgid=%d, code=%d",
+              type, messageId, code);
 
-    if ((CA_MSG_ACKNOWLEDGE != type) && (CA_MSG_RESET != type))
+    if (((CA_MSG_ACKNOWLEDGE != type) && (CA_MSG_RESET != type))
+        || (CA_MSG_RESET == type && CA_EMPTY != code))
     {
         return CA_STATUS_OK;
     }
 
     // mutex lock
-    ca_mutex_lock(context->threadMutex);
+    oc_mutex_lock(context->threadMutex);
     uint32_t len = u_arraylist_length(context->dataList);
 
     // find index
@@ -499,14 +573,14 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
             // if retransmission was finish..token will be unavailable.
             if (CA_EMPTY == CAGetCodeFromPduBinaryData(pdu, size))
             {
-                OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
+                OIC_LOG(DEBUG, TAG, "code is CA_EMPTY");
 
                 if (NULL == retData->pdu)
                 {
                     OIC_LOG(ERROR, TAG, "retData->pdu is null");
                     OICFree(retData);
                     // mutex unlock
-                    ca_mutex_unlock(context->threadMutex);
+                    oc_mutex_unlock(context->threadMutex);
 
                     return CA_STATUS_FAILED;
                 }
@@ -516,10 +590,10 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
                 if ((*retransmissionPdu) == NULL)
                 {
                     OICFree(retData);
-                    OIC_LOG(ERROR, TAG, "memory error!!");
+                    OIC_LOG(ERROR, TAG, "memory error");
 
                     // mutex unlock
-                    ca_mutex_unlock(context->threadMutex);
+                    oc_mutex_unlock(context->threadMutex);
 
                     return CA_MEMORY_ALLOC_FAILED;
                 }
@@ -533,15 +607,14 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
                 OIC_LOG(ERROR, TAG, "Removed data is NULL");
 
                 // mutex unlock
-                ca_mutex_unlock(context->threadMutex);
+                oc_mutex_unlock(context->threadMutex);
 
                 return CA_STATUS_FAILED;
             }
 
-            OIC_LOG_V(DEBUG, TAG, "remove retransmission CON data!!, message id(%d)",
-                      messageId);
+            OIC_LOG_V(DEBUG, TAG, "remove RTCON data!!, msgid=%d", messageId);
 
-            CADestroyEndpointInternal(removedData->endpoint);
+            CAFreeEndpoint(removedData->endpoint);
             OICFree(removedData->pdu);
             OICFree(removedData);
 
@@ -550,9 +623,48 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
     }
 
     // mutex unlock
-    ca_mutex_unlock(context->threadMutex);
+    oc_mutex_unlock(context->threadMutex);
+
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
+}
+
+CAResult_t CARetransmissionClearAdapterData(CARetransmission_t *context, CATransportAdapter_t type)
+{
+    if (NULL == context)
+    {
+        OIC_LOG(ERROR, TAG, "thread instance is empty..");
+        return CA_STATUS_INVALID_PARAM;
+    }
+
+    OIC_LOG_V(DEBUG, TAG, "clear queue data for Adapter : %d ", type);
+
+    // mutex lock
+    oc_mutex_lock(context->threadMutex);
+
+    uint32_t len = u_arraylist_length(context->dataList);
+
+    // find index
+    uint32_t i;
+    for (i = 0; i < len; i++)
+    {
+        // get data
+        CARetransmissionData_t *removedData = (CARetransmissionData_t *) u_arraylist_get(
+            context->dataList, i);
+
+        // free
+        if (NULL != removedData && NULL != removedData->endpoint &&
+                removedData->endpoint->adapter == type)
+        {
+            CAFreeEndpoint(removedData->endpoint);
+            OICFree(removedData->pdu);
+            OICFree(removedData);
+        }
+    }
+
+    // mutex unlock
+    oc_mutex_unlock(context->threadMutex);
 
-    OIC_LOG(DEBUG, TAG, "OUT - CARetransmissionReceivedData");
     return CA_STATUS_OK;
 }
 
@@ -567,18 +679,18 @@ CAResult_t CARetransmissionStop(CARetransmission_t *context)
     OIC_LOG(DEBUG, TAG, "retransmission stop request!!");
 
     // mutex lock
-    ca_mutex_lock(context->threadMutex);
+    oc_mutex_lock(context->threadMutex);
 
     // set stop flag
     context->isStop = true;
 
     // notify the thread
-    ca_cond_signal(context->threadCond);
+    oc_cond_signal(context->threadCond);
 
-    ca_cond_wait(context->threadCond, context->threadMutex);
+    oc_cond_wait(context->threadCond, context->threadMutex);
 
     // mutex unlock
-    ca_mutex_unlock(context->threadMutex);
+    oc_mutex_unlock(context->threadMutex);
 
     return CA_STATUS_OK;
 }
@@ -593,38 +705,10 @@ CAResult_t CARetransmissionDestroy(CARetransmission_t *context)
 
     OIC_LOG(DEBUG, TAG, "retransmission context destroy..");
 
-    ca_mutex_free(context->threadMutex);
+    oc_mutex_free(context->threadMutex);
     context->threadMutex = NULL;
-    ca_cond_free(context->threadCond);
+    oc_cond_free(context->threadCond);
     u_arraylist_free(&context->dataList);
 
     return CA_STATUS_OK;
 }
-
-uint64_t getCurrentTimeInMicroSeconds()
-{
-    OIC_LOG(DEBUG, TAG, "IN");
-    uint64_t currentTime = 0;
-
-#ifdef __ANDROID__
-    struct timespec getTs;
-
-    clock_gettime(CLOCK_MONOTONIC, &getTs);
-
-    currentTime = (getTs.tv_sec * (uint64_t)1000000000 + getTs.tv_nsec)/1000;
-    OIC_LOG_V(DEBUG, TAG, "current time = %ld", currentTime);
-#else
-#if _POSIX_TIMERS > 0
-    struct timespec ts;
-    clock_gettime(CLOCK_MONOTONIC, &ts);
-    currentTime = ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
-#else
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    currentTime = tv.tv_sec * USECS_PER_SEC + tv.tv_usec;
-#endif
-#endif
-
-    OIC_LOG(DEBUG, TAG, "OUT");
-    return currentTime;
-}