removing redundant code
authorjnashok <jn.ashok@samsung.com>
Fri, 7 Aug 2015 06:16:42 +0000 (15:16 +0900)
committerJon A. Cruz <jonc@osg.samsung.com>
Mon, 17 Aug 2015 17:44:38 +0000 (17:44 +0000)
Removing the redundant code in remote handler

Change-Id: I23d624eec6cf4b215f78fc74ab046a6eaa5468ae
Signed-off-by: jnashok <jn.ashok@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2133
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
Reviewed-by: jihwan seo <jihwan.seo@samsung.com>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/connectivity/common/src/caremotehandler.c

index 48e5f56..cca8bd8 100644 (file)
@@ -63,94 +63,16 @@ CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
         return NULL;
     }
 
-    *clone = *rep;
-
-    if (rep->info.token)
-    {
-        char *temp = NULL;
-
-        // allocate token field
-        uint8_t len = rep->info.tokenLength;
-
-        if (len)
-        {
-            temp = (char *) OICCalloc(len, sizeof(char));
-            if (!temp)
-            {
-                OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
-
-                CADestroyRequestInfoInternal(clone);
-
-                return NULL;
-            }
-            memcpy(temp, rep->info.token, len);
-        }
-
-        // save the token
-        clone->info.token = temp;
-        clone->info.tokenLength = len;
-    }
-
-    if (NULL != rep->info.options && 0 < rep->info.numOptions)
-    {
-        // save the options
-        clone->info.options =
-            (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
-        if (NULL == clone->info.options)
-        {
-            OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
-            OICFree(clone->info.token);
-            OICFree(clone);
-            return NULL;
-        }
-        memcpy(clone->info.options, rep->info.options,
-               sizeof(CAHeaderOption_t) * rep->info.numOptions);
-    }
-    else
-    {
-        clone->info.options = NULL;
-        clone->info.numOptions = 0;
-    }
-
-    if (NULL != rep->info.payload && 0 < rep->info.payloadSize)
+    CAResult_t result = CACloneInfo(&rep->info, &clone->info);
+    if(CA_STATUS_OK != result)
     {
-        // allocate payload field
-        uint8_t *temp = OICMalloc(rep->info.payloadSize);
-        if (NULL == temp)
-        {
-            OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
-
-            CADestroyRequestInfoInternal(clone);
-
-            return NULL;
-        }
-        memcpy(temp, rep->info.payload, rep->info.payloadSize);
-
-        // save the payload
-        clone->info.payload = temp;
-    }
-    else
-    {
-        clone->info.payload = NULL;
-        clone->info.payloadSize = 0;
+        OIC_LOG(ERROR, TAG, "CACloneRequestInfo error in CACloneInfo");
+        CADestroyRequestInfoInternal(clone);
+        return NULL;
     }
 
-    if (NULL != rep->info.resourceUri)
-    {
-        // allocate payload field
-        char *temp = OICStrdup(rep->info.resourceUri);
-        if (NULL == temp)
-        {
-            OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
-
-            CADestroyRequestInfoInternal(clone);
-
-            return NULL;
-        }
-
-        // save the resourceUri
-        clone->info.resourceUri = temp;
-    }
+    clone->method = rep->method;
+    clone->isMulticast = rep->isMulticast;
 
     return clone;
 }
@@ -198,96 +120,16 @@ CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
         OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
         return NULL;
     }
-    *clone = *rep;
 
-    if (rep->info.token)
+    CAResult_t result = CACloneInfo(&rep->info, &clone->info);
+    if(CA_STATUS_OK != result)
     {
-        char *temp = NULL;
-
-        // allocate token field
-        uint8_t len = rep->info.tokenLength;
-
-        if (len)
-        {
-            temp = (char *) OICCalloc(len, sizeof(char));
-            if (!temp)
-            {
-                OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
-
-                CADestroyResponseInfoInternal(clone);
-
-                return NULL;
-            }
-            memcpy(temp, rep->info.token, len);
-        }
-        // save the token
-        clone->info.token = temp;
-        clone->info.tokenLength = len;
-    }
-
-    if (NULL != rep->info.options && rep->info.numOptions)
-    {
-        // save the options
-        clone->info.options =
-                (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
-
-        if (NULL == clone->info.options)
-        {
-            OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
-
-            OICFree(clone->info.token);
-            OICFree(clone);
-            return NULL;
-        }
-        memcpy(clone->info.options, rep->info.options,
-                sizeof(CAHeaderOption_t) * rep->info.numOptions);
-    }
-    else
-    {
-        clone->info.options = NULL;
-        clone->info.numOptions = 0;
-    }
-
-    if (NULL != rep->info.payload && 0 < rep->info.payloadSize)
-    {
-        // allocate payload field
-        uint8_t *temp = (uint8_t *) OICMalloc(rep->info.payloadSize);
-        if (NULL == temp)
-        {
-            OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
-
-            CADestroyResponseInfoInternal(clone);
-
-            return NULL;
-        }
-        memcpy(temp, rep->info.payload, rep->info.payloadSize);
-
-        // save the payload
-        clone->info.payload = temp;
-    }
-    else
-    {
-        clone->info.payload = NULL;
-        clone->info.payloadSize = 0;
-    }
-
-    if (NULL != rep->info.resourceUri)
-    {
-        // allocate payload field
-        char *temp = OICStrdup(rep->info.resourceUri);
-        if (NULL == temp)
-        {
-            OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
-
-            CADestroyResponseInfoInternal(clone);
-
-            return NULL;
-        }
-
-        // save the resourceUri
-        clone->info.resourceUri = temp;
+        OIC_LOG(ERROR, TAG, "CACloneResponseInfo error in CACloneInfo");
+        CADestroyResponseInfoInternal(clone);
+        return NULL;
     }
 
+    clone->result = rep->result;
     return clone;
 }
 
@@ -386,6 +228,8 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
         return CA_STATUS_INVALID_PARAM;
     }
 
+    memset(clone, 0 , sizeof(CAInfo_t));
+
     //Do not free clone. we cannot declare it const, as the content is modified
     if ((info->token) && (0 < info->tokenLength))
     {
@@ -397,7 +241,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
         temp = (char *) OICMalloc(len * sizeof(char));
         if (!temp)
         {
-            OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
+            OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
             return CA_MEMORY_ALLOC_FAILED;
         }
 
@@ -415,20 +259,21 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
 
         if (!clone->options)
         {
-            OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
+            OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
             CADestroyInfoInternal(clone);
             return CA_MEMORY_ALLOC_FAILED;
         }
         memcpy(clone->options, info->options, sizeof(CAHeaderOption_t) * info->numOptions);
+        clone->numOptions = info->numOptions;
     }
 
-    if (info->payload && 0 < info->payloadSize)
+    if ((info->payload) && (0 < info->payloadSize))
     {
         // allocate payload field
         uint8_t *temp = OICMalloc(info->payloadSize);
         if (!temp)
         {
-            OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
+            OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
             CADestroyInfoInternal(clone);
             return CA_MEMORY_ALLOC_FAILED;
         }
@@ -436,11 +281,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
 
         // save the payload
         clone->payload = temp;
-    }
-    else
-    {
-        clone->payload = NULL;
-        clone->payloadSize = 0;
+        clone->payloadSize = info->payloadSize;
     }
 
     if (info->resourceUri)
@@ -449,7 +290,7 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
         char *temp = OICStrdup(info->resourceUri);
         if (!temp)
         {
-            OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
+            OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
             CADestroyInfoInternal(clone);
             return CA_MEMORY_ALLOC_FAILED;
         }
@@ -458,6 +299,9 @@ CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
         clone->resourceUri = temp;
     }
 
+    clone->messageId = info->messageId;
+    clone->type = info->type;
+
     return CA_STATUS_OK;
 
 }