X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fcommon%2Fsrc%2Fcaremotehandler.c;h=a39b7d48cecc5652f1795012a22447a840a10d34;hb=17c68b2fd1e74586f85e552eeab4e32dc121f8a0;hp=49eb44409f27ed9b5f6d8167a137ab8767be5efd;hpb=8c01dff2c5bc5496f7dc1632c498943ec6ecb015;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/common/src/caremotehandler.c b/resource/csdk/connectivity/common/src/caremotehandler.c index 49eb444..a39b7d4 100644 --- a/resource/csdk/connectivity/common/src/caremotehandler.c +++ b/resource/csdk/connectivity/common/src/caremotehandler.c @@ -63,89 +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 + CAResult_t result = CACloneInfo(&rep->info, &clone->info); + if(CA_STATUS_OK != result) { - clone->info.options = NULL; - clone->info.numOptions = 0; - } - - if (NULL != rep->info.payload) - { - // 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; + 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; } @@ -163,21 +90,23 @@ CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep) switch (rep->result) { case CA_EMPTY: - case CA_SUCCESS: case CA_CREATED: case CA_DELETED: case CA_VALID: - case CA_CHANGED: case CA_CONTENT: + case CA_CHANGED: + case CA_CONTINUE: case CA_BAD_REQ: case CA_UNAUTHORIZED_REQ: case CA_BAD_OPT: case CA_FORBIDDEN_REQ: case CA_NOT_FOUND: + case CA_NOT_ACCEPTABLE: + case CA_REQUEST_ENTITY_INCOMPLETE: + case CA_REQUEST_ENTITY_TOO_LARGE: case CA_INTERNAL_SERVER_ERROR: case CA_RETRANSMIT_TIMEOUT: break; - default: OIC_LOG_V(ERROR, TAG, "Response code %u is invalid", rep->result); return NULL; @@ -190,91 +119,17 @@ CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep) OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory"); 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, "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) + CAResult_t result = CACloneInfo(&rep->info, &clone->info); + if(CA_STATUS_OK != result) { - // 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; - } - - 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->isMulticast = rep->isMulticast; + clone->result = rep->result; return clone; } @@ -307,26 +162,37 @@ void CAFreeEndpoint(CAEndpoint_t *rep) OICFree(rep); } -void CADestroyRequestInfoInternal(CARequestInfo_t *rep) +static void CADestroyInfoInternal(CAInfo_t *info) { - if (NULL == rep) - { - OIC_LOG(ERROR, TAG, "parameter is null"); - return; - } - // free token field - OICFree(rep->info.token); + OICFree(info->token); + info->token = NULL; + info->tokenLength = 0; // free options field - OICFree((CAHeaderOption_t *) rep->info.options); + OICFree(info->options); + info->options = NULL; + info->numOptions = 0; // free payload field - OICFree((char *) rep->info.payload); + OICFree((char *) info->payload); + info->payload = NULL; + info->payloadSize = 0; // free uri - OICFree(rep->info.resourceUri); + OICFree(info->resourceUri); + info->resourceUri = NULL; +} + +void CADestroyRequestInfoInternal(CARequestInfo_t *rep) +{ + if (NULL == rep) + { + OIC_LOG(ERROR, TAG, "parameter is null"); + return; + } + CADestroyInfoInternal(&rep->info); OICFree(rep); } @@ -338,21 +204,110 @@ void CADestroyResponseInfoInternal(CAResponseInfo_t *rep) return; } - // free token field - OICFree(rep->info.token); + CADestroyInfoInternal(&rep->info); + OICFree(rep); +} - // free options field - if (rep->info.options != NULL && rep->info.numOptions) +void CADestroyErrorInfoInternal(CAErrorInfo_t *errorInfo) +{ + if (NULL == errorInfo) { - OICFree((CAHeaderOption_t *) rep->info.options); + OIC_LOG(ERROR, TAG, "parameter is null"); + return; } - // free payload field - OICFree((char *) rep->info.payload); + CADestroyInfoInternal(&errorInfo->info); + OICFree(errorInfo); +} - // free uri - OICFree(rep->info.resourceUri); +CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone) +{ + if (!info || !clone) + { + OIC_LOG(ERROR, TAG, "input parameter invalid"); + return CA_STATUS_INVALID_PARAM; + } - OICFree(rep); -} + 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)) + { + char *temp = NULL; + + // allocate token field + uint8_t len = info->tokenLength; + + temp = (char *) OICMalloc(len * sizeof(char)); + if (!temp) + { + OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); + return CA_MEMORY_ALLOC_FAILED; + } + + memcpy(temp, info->token, len); + // save the token + clone->token = temp; + clone->tokenLength = len; + } + + if (info->options && (0 < info->numOptions)) + { + // save the options + clone->options = + (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * info->numOptions); + if (!clone->options) + { + 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)) + { + // allocate payload field + uint8_t *temp = OICMalloc(info->payloadSize); + if (!temp) + { + OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); + CADestroyInfoInternal(clone); + return CA_MEMORY_ALLOC_FAILED; + } + memcpy(temp, info->payload, info->payloadSize); + + // save the payload + clone->payload = temp; + clone->payloadSize = info->payloadSize; + } + clone->payloadFormat = info->payloadFormat; + clone->acceptFormat = info->acceptFormat; + + if (info->resourceUri) + { + // allocate payload field + char *temp = OICStrdup(info->resourceUri); + if (!temp) + { + OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory"); + CADestroyInfoInternal(clone); + return CA_MEMORY_ALLOC_FAILED; + } + + // save the resourceUri + clone->resourceUri = temp; + } + +#ifdef ROUTING_GATEWAY + clone->skipRetransmission = info->skipRetransmission; +#endif + + clone->messageId = info->messageId; + clone->type = info->type; + + return CA_STATUS_OK; + +}