RESET/ACK code review update.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caretransmission.c
index e222d08..a80812a 100644 (file)
@@ -46,7 +46,7 @@
 #include "oic_malloc.h"
 #include "logger.h"
 
-#define TAG PCF("RET")
+#define TAG PCF("CA")
 
 typedef struct
 {
@@ -70,11 +70,11 @@ uint64_t getCurrentTimeInMicroSeconds();
  * @param   currentTime     [IN]microseconds
  * @param   timeStamp       [IN]microseconds
  * @param   timeoutValue    [IN]microseconds
- * @param   triedCount      [IN]
+ * @param   triedCount      [IN]Number of retransmission tried
  * @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)
+                           uint8_t triedCount)
 {
     // #1. calculate timeout
     uint32_t milliTimeoutValue = timeoutValue * 0.001;
@@ -104,17 +104,23 @@ static uint64_t CAGetTimeoutValue()
 
 static void CACheckRetransmissionList(CARetransmission_t *context)
 {
+    if (NULL == context)
+    {
+        OIC_LOG(ERROR, TAG, "context is null..");
+        return;
+    }
+
     // mutex lock
     u_mutex_lock(context->threadMutex);
 
-    int32_t i = 0;
+    uint32_t i = 0;
     uint32_t len = u_arraylist_length(context->dataList);
 
     for (i = 0; i < len; i++)
     {
         CARetransmissionData_t *retData = u_arraylist_get(context->dataList, i);
 
-        if (retData == NULL)
+        if (NULL == retData)
         {
             continue;
         }
@@ -144,13 +150,13 @@ static void CACheckRetransmissionList(CARetransmission_t *context)
             if (removedData != NULL)
             {
                 OIC_LOG_V(DEBUG, TAG, "max trying count, remove retransmission CON data!!,\
-                        message id(%d)", removedData->messageId);
+                          message id(%d)", removedData->messageId);
 
                 // callback for retransmit timeout
                 if (context->timeoutCallback != NULL)
                 {
                     context->timeoutCallback(removedData->endpoint, removedData->pdu,
-                            removedData->size);
+                                             removedData->size);
                 }
 
                 CADestroyRemoteEndpointInternal(removedData->endpoint);
@@ -181,7 +187,7 @@ static void CARetransmissionBaseRoutine(void *threadValue)
 
     CARetransmission_t *context = (CARetransmission_t *) threadValue;
 
-    if (context == NULL)
+    if (NULL == context)
     {
         OIC_LOG(ERROR, TAG, "thread data passing error!!");
 
@@ -207,7 +213,7 @@ static void CARetransmissionBaseRoutine(void *threadValue)
         {
             // check each RETRANSMISSION_CHECK_PERIOD_SEC time.
             OIC_LOG_V(DEBUG, TAG, "wait..(%d)microseconds",
-                        RETRANSMISSION_CHECK_PERIOD_SEC * (uint64_t) 1000000);
+                      RETRANSMISSION_CHECK_PERIOD_SEC * (uint64_t) 1000000);
 
             // wait
             u_cond_wait_until(context->threadCond, context->threadMutex,
@@ -233,19 +239,20 @@ static void CARetransmissionBaseRoutine(void *threadValue)
 }
 
 CAResult_t CARetransmissionInitialize(CARetransmission_t *context, u_thread_pool_t handle,
-        CADataSendMethod_t retransmissionSendMethod, CATimeoutCallback_t timeoutCallback,
-        CARetransmissionConfig_t* config)
+                                      CADataSendMethod_t retransmissionSendMethod,
+                                      CATimeoutCallback_t timeoutCallback,
+                                      CARetransmissionConfig_t* config)
 {
-    if (context == NULL)
+    if (NULL == context)
     {
         OIC_LOG(ERROR, TAG, "thread instance is empty..");
-        return CA_STATUS_FAILED;
+        return CA_STATUS_INVALID_PARAM;
     }
 
-    if (handle == NULL)
+    if (NULL == handle)
     {
         OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
-        return CA_STATUS_FAILED;
+        return CA_STATUS_INVALID_PARAM;
     }
 
     OIC_LOG(DEBUG, TAG, "thread initialize..");
@@ -254,7 +261,7 @@ CAResult_t CARetransmissionInitialize(CARetransmission_t *context, u_thread_pool
 
     CARetransmissionConfig_t cfg = { 0 };
 
-    if (config == NULL)
+    if (NULL == config)
     {
         // setDefault
         cfg.supportType = DEFAULT_RETRANSMISSION_TYPE;
@@ -280,16 +287,16 @@ CAResult_t CARetransmissionInitialize(CARetransmission_t *context, u_thread_pool
 
 CAResult_t CARetransmissionStart(CARetransmission_t *context)
 {
-    if (context == NULL)
+    if (NULL == context)
     {
         OIC_LOG(ERROR, TAG, "context is empty..");
-        return CA_STATUS_FAILED;
+        return CA_STATUS_INVALID_PARAM;
     }
 
-    if (context->threadPool == NULL)
+    if (NULL == context->threadPool)
     {
         OIC_LOG(ERROR, TAG, "thread pool handle is empty..");
-        return CA_STATUS_FAILED;
+        return CA_STATUS_INVALID_PARAM;
     }
 
     CAResult_t res = u_thread_pool_add_task(context->threadPool, CARetransmissionBaseRoutine,
@@ -305,9 +312,10 @@ CAResult_t CARetransmissionStart(CARetransmission_t *context)
 }
 
 CAResult_t CARetransmissionSentData(CARetransmission_t *context,
-    const CARemoteEndpoint_t* endpoint, const void* pdu, uint32_t size)
+                                    const CARemoteEndpoint_t* endpoint, const void* pdu,
+                                    uint32_t size)
 {
-    if (context == NULL || endpoint == NULL || pdu == NULL)
+    if (NULL == context || NULL == endpoint || NULL == pdu)
     {
         OIC_LOG(ERROR, TAG, "invalid parameter..");
         return CA_STATUS_INVALID_PARAM;
@@ -318,7 +326,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
     {
         OIC_LOG_V(DEBUG, TAG, "not supported connectivity type for retransmission..(%d)",
                   endpoint->connectivityType);
-        return CA_STATUS_OK;
+        return CA_NOT_SUPPORTED;
     }
 
     // #1. check PDU method type and get message id.
@@ -329,32 +337,33 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
 
     if (type != CA_MSG_CONFIRM)
     {
-        return CA_STATUS_OK;
+        OIC_LOG(DEBUG, TAG, "not supported message type for retransmission..");
+        return CA_NOT_SUPPORTED;
     }
 
     // create retransmission data
     CARetransmissionData_t *retData = (CARetransmissionData_t *) OICCalloc(
                                           1, sizeof(CARetransmissionData_t));
 
-    if (retData == NULL)
+    if (NULL == retData)
     {
         OIC_LOG(ERROR, TAG, "memory error!!");
         return CA_MEMORY_ALLOC_FAILED;
     }
 
     // copy PDU data
-    void *pduData = (void *) OICMalloc(sizeof(int8_t) * size);
-    if (pduData == NULL)
+    void *pduData = (void *) OICMalloc(size);
+    if (NULL == pduData)
     {
         OICFree(retData);
-        OIC_LOG(ERROR, TAG, "memory error!!");
+        OIC_LOG_V(ERROR, TAG, "memory error!!");
         return CA_MEMORY_ALLOC_FAILED;
     }
-    memcpy(pduData, pdu, sizeof(int8_t) * size);
+    memcpy(pduData, pdu, size);
 
     // clone remote endpoint
     CARemoteEndpoint_t *remoteEndpoint = CACloneRemoteEndpoint(endpoint);
-    if (remoteEndpoint == NULL)
+    if (NULL == remoteEndpoint)
     {
         OICFree(retData);
         OICFree(pduData);
@@ -382,14 +391,14 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
     {
         CARetransmissionData_t *currData = u_arraylist_get(context->dataList, i);
 
-        if (currData == NULL)
+        if (NULL == currData)
         {
             continue;
         }
 
         // found index
-        if (retData != NULL && (currData->endpoint->connectivityType == endpoint->connectivityType)
-            && currData->messageId == messageId)
+        if (NULL != currData->endpoint && currData->messageId == messageId
+            && (currData->endpoint->connectivityType == endpoint->connectivityType))
         {
             OIC_LOG(ERROR, TAG, "Duplicate message ID");
 
@@ -405,7 +414,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
 
     u_arraylist_add(context->dataList, (void *) retData);
 
-    // notity the thread
+    // notify the thread
     u_cond_signal(context->threadCond);
 
     // mutex unlock
@@ -416,9 +425,10 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context,
 
 CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
                                         const CARemoteEndpoint_t *endpoint, const void *pdu,
-                                        uint32_t size)
+                                        uint32_t size, void **retransmissionPdu)
 {
-    if (context == NULL || endpoint == NULL || pdu == NULL)
+    OIC_LOG_V(DEBUG, TAG, "IN - CARetransmissionReceivedData");
+    if (NULL == context || NULL == endpoint || NULL == pdu || NULL == retransmissionPdu)
     {
         OIC_LOG(ERROR, TAG, "invalid parameter..");
         return CA_STATUS_INVALID_PARAM;
@@ -453,25 +463,68 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
     // find index
     for (i = 0; i < len; i++)
     {
-        CARetransmissionData_t *retData = u_arraylist_get(context->dataList, i);
+        CARetransmissionData_t *retData = (CARetransmissionData_t *) u_arraylist_get(
+                context->dataList, i);
+
+        if (NULL == retData)
+        {
+            continue;
+        }
 
         // found index
-        if (retData != NULL && (retData->endpoint->connectivityType == endpoint->connectivityType)
-            && retData->messageId == messageId)
+        if (NULL != retData->endpoint && retData->messageId == messageId
+            && (retData->endpoint->connectivityType == endpoint->connectivityType))
         {
+            // get pdu data for getting token when CA_EMPTY(RST/ACK) is received from remote device
+            // if retransmission was finish..token will be unavailable.
+            if (CA_EMPTY == CAGetCodeFromPduBinaryData(pdu, size))
+            {
+                OIC_LOG(DEBUG, TAG, "code is CA_EMPTY..");
+
+                if (NULL == retData->pdu)
+                {
+                    OIC_LOG(ERROR, TAG, "retData->pdu is null");
+                    OICFree(retData);
+                    // mutex unlock
+                    u_mutex_unlock(context->threadMutex);
+
+                    return CA_STATUS_FAILED;
+                }
+
+                // copy PDU data
+                (*retransmissionPdu) = (void *) OICCalloc(1, retData->size);
+                if ((*retransmissionPdu) == NULL)
+                {
+                    OICFree(retData);
+                    OIC_LOG(ERROR, TAG, "memory error!!");
+
+                    // mutex unlock
+                    u_mutex_unlock(context->threadMutex);
+
+                    return CA_MEMORY_ALLOC_FAILED;
+                }
+                memcpy((*retransmissionPdu), retData->pdu, retData->size);
+            }
+
             // #2. remove data from list
             CARetransmissionData_t *removedData = u_arraylist_remove(context->dataList, i);
-
-            if (removedData != NULL)
+            if (NULL == removedData)
             {
-                OIC_LOG_V(DEBUG, TAG, "remove retransmission CON data!!, message id(%d)",
-                        messageId);
+                OIC_LOG(ERROR, TAG, "Removed data is NULL");
 
-                CADestroyRemoteEndpointInternal(removedData->endpoint);
-                OICFree(removedData->pdu);
+                // mutex unlock
+                u_mutex_unlock(context->threadMutex);
 
-                OICFree(removedData);
+                return CA_STATUS_FAILED;
             }
+
+            OIC_LOG_V(DEBUG, TAG, "remove retransmission CON data!!, message id(%d)",
+                      messageId);
+
+            CADestroyRemoteEndpointInternal(removedData->endpoint);
+            OICFree(removedData->pdu);
+            OICFree(removedData);
+
             break;
         }
     }
@@ -479,15 +532,16 @@ CAResult_t CARetransmissionReceivedData(CARetransmission_t *context,
     // mutex unlock
     u_mutex_unlock(context->threadMutex);
 
+    OIC_LOG(DEBUG, TAG, "OUT - CARetransmissionReceivedData");
     return CA_STATUS_OK;
 }
 
 CAResult_t CARetransmissionStop(CARetransmission_t *context)
 {
-    if (context == NULL)
+    if (NULL == context)
     {
         OIC_LOG(ERROR, TAG, "context is empty..");
-        return CA_STATUS_FAILED;
+        return CA_STATUS_INVALID_PARAM;
     }
 
     OIC_LOG(DEBUG, TAG, "retransmission stop request!!");
@@ -511,10 +565,10 @@ CAResult_t CARetransmissionStop(CARetransmission_t *context)
 
 CAResult_t CARetransmissionDestroy(CARetransmission_t *context)
 {
-    if (context == NULL)
+    if (NULL == context)
     {
         OIC_LOG(ERROR, TAG, "context is empty..");
-        return CA_STATUS_FAILED;
+        return CA_STATUS_INVALID_PARAM;
     }
 
     OIC_LOG(DEBUG, TAG, "retransmission context destroy..");
@@ -543,4 +597,3 @@ uint64_t getCurrentTimeInMicroSeconds()
 #endif
     return currentTime;
 }
-