1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25 #include "caremotehandler.h"
30 CAEndpoint_t *CACloneEndpoint(const CAEndpoint_t *rep)
34 OIC_LOG(ERROR, TAG, "parameter is null");
38 // allocate the remote end point structure.
39 CAEndpoint_t *clone = (CAEndpoint_t *)OICMalloc(sizeof (CAEndpoint_t));
42 OIC_LOG(ERROR, TAG, "CACloneRemoteEndpoint Out of memory");
50 CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
54 OIC_LOG(ERROR, TAG, "parameter is null");
58 // allocate the request info structure.
59 CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
62 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
66 CAResult_t result = CACloneInfo(&rep->info, &clone->info);
67 if(CA_STATUS_OK != result)
69 OIC_LOG(ERROR, TAG, "CACloneRequestInfo error in CACloneInfo");
70 CADestroyRequestInfoInternal(clone);
74 clone->method = rep->method;
75 clone->isMulticast = rep->isMulticast;
80 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
84 OIC_LOG(ERROR, TAG, "Response pointer is NULL");
88 // check the result value of response info.
89 // Keep this check in sync with CAResponseResult_t
101 case CA_UNAUTHORIZED_REQ:
103 case CA_FORBIDDEN_REQ:
105 case CA_NOT_ACCEPTABLE:
106 case CA_REQUEST_ENTITY_INCOMPLETE:
107 case CA_REQUEST_ENTITY_TOO_LARGE:
108 case CA_INTERNAL_SERVER_ERROR:
109 case CA_RETRANSMIT_TIMEOUT:
113 OIC_LOG_V(ERROR, TAG, "Response code %u is invalid", rep->result);
117 // allocate the response info structure.
118 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
121 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
125 CAResult_t result = CACloneInfo(&rep->info, &clone->info);
126 if(CA_STATUS_OK != result)
128 OIC_LOG(ERROR, TAG, "CACloneResponseInfo error in CACloneInfo");
129 CADestroyResponseInfoInternal(clone);
133 clone->isMulticast = rep->isMulticast;
134 clone->result = rep->result;
138 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
139 CATransportAdapter_t adapter,
143 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
146 OIC_LOG(ERROR, TAG, "Memory allocation failed !");
152 OICStrcpy(info->addr, sizeof(info->addr), address);
153 info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
156 info->adapter = adapter;
162 void CAFreeEndpoint(CAEndpoint_t *rep)
167 static void CADestroyInfoInternal(CAInfo_t *info)
170 OICFree(info->token);
172 info->tokenLength = 0;
174 // free options field
175 OICFree(info->options);
176 info->options = NULL;
177 info->numOptions = 0;
179 // free payload field
180 OICFree((char *) info->payload);
181 info->payload = NULL;
182 info->payloadSize = 0;
185 OICFree(info->resourceUri);
186 info->resourceUri = NULL;
189 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
193 OIC_LOG(ERROR, TAG, "parameter is null");
197 CADestroyInfoInternal(&rep->info);
201 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
205 OIC_LOG(ERROR, TAG, "parameter is null");
209 CADestroyInfoInternal(&rep->info);
213 void CADestroyErrorInfoInternal(CAErrorInfo_t *errorInfo)
215 if (NULL == errorInfo)
217 OIC_LOG(ERROR, TAG, "parameter is null");
221 CADestroyInfoInternal(&errorInfo->info);
225 CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
229 OIC_LOG(ERROR, TAG, "input parameter invalid");
230 return CA_STATUS_INVALID_PARAM;
233 memset(clone, 0 , sizeof(CAInfo_t));
235 //Do not free clone. we cannot declare it const, as the content is modified
236 if ((info->token) && (0 < info->tokenLength))
240 // allocate token field
241 uint8_t len = info->tokenLength;
243 temp = (char *) OICMalloc(len * sizeof(char));
246 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
247 return CA_MEMORY_ALLOC_FAILED;
250 memcpy(temp, info->token, len);
253 clone->tokenLength = len;
256 if (info->options && (0 < info->numOptions))
260 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * info->numOptions);
264 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
265 CADestroyInfoInternal(clone);
266 return CA_MEMORY_ALLOC_FAILED;
268 memcpy(clone->options, info->options, sizeof(CAHeaderOption_t) * info->numOptions);
269 clone->numOptions = info->numOptions;
272 if ((info->payload) && (0 < info->payloadSize))
274 // allocate payload field
275 uint8_t *temp = OICMalloc(info->payloadSize);
278 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
279 CADestroyInfoInternal(clone);
280 return CA_MEMORY_ALLOC_FAILED;
282 memcpy(temp, info->payload, info->payloadSize);
285 clone->payload = temp;
286 clone->payloadSize = info->payloadSize;
288 clone->payloadFormat = info->payloadFormat;
289 clone->acceptFormat = info->acceptFormat;
291 if (info->resourceUri)
293 // allocate payload field
294 char *temp = OICStrdup(info->resourceUri);
297 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
298 CADestroyInfoInternal(clone);
299 return CA_MEMORY_ALLOC_FAILED;
302 // save the resourceUri
303 clone->resourceUri = temp;
306 clone->messageId = info->messageId;
307 clone->type = info->type;