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");
72 // allocate token field
73 uint8_t len = rep->info.tokenLength;
77 temp = (char *) OICCalloc(len, sizeof(char));
80 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
82 CADestroyRequestInfoInternal(clone);
86 memcpy(temp, rep->info.token, len);
90 clone->info.token = temp;
91 clone->info.tokenLength = len;
94 if (NULL != rep->info.options && 0 < rep->info.numOptions)
98 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
99 if (NULL == clone->info.options)
101 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
102 OICFree(clone->info.token);
106 memcpy(clone->info.options, rep->info.options,
107 sizeof(CAHeaderOption_t) * rep->info.numOptions);
111 clone->info.options = NULL;
112 clone->info.numOptions = 0;
115 if (NULL != rep->info.payload)
117 // allocate payload field
118 uint8_t *temp = OICMalloc(rep->info.payloadSize);
121 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
123 CADestroyRequestInfoInternal(clone);
127 memcpy(temp, rep->info.payload, rep->info.payloadSize);
130 clone->info.payload = temp;
133 if (NULL != rep->info.resourceUri)
135 // allocate payload field
136 char *temp = OICStrdup(rep->info.resourceUri);
139 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
141 CADestroyRequestInfoInternal(clone);
146 // save the resourceUri
147 clone->info.resourceUri = temp;
153 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
157 OIC_LOG(ERROR, TAG, "Response pointer is NULL");
161 // check the result value of response info.
162 // Keep this check in sync with CAResponseResult_t
174 case CA_UNAUTHORIZED_REQ:
176 case CA_FORBIDDEN_REQ:
178 case CA_REQUEST_ENTITY_INCOMPLETE:
179 case CA_REQUEST_ENTITY_TOO_LARGE:
180 case CA_INTERNAL_SERVER_ERROR:
181 case CA_RETRANSMIT_TIMEOUT:
185 OIC_LOG_V(ERROR, TAG, "Response code %u is invalid", rep->result);
189 // allocate the response info structure.
190 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
193 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
202 // allocate token field
203 uint8_t len = rep->info.tokenLength;
207 temp = (char *) OICCalloc(len, sizeof(char));
210 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
212 CADestroyResponseInfoInternal(clone);
216 memcpy(temp, rep->info.token, len);
219 clone->info.token = temp;
220 clone->info.tokenLength = len;
223 if (NULL != rep->info.options && rep->info.numOptions)
226 clone->info.options =
227 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
229 if (NULL == clone->info.options)
231 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
233 OICFree(clone->info.token);
237 memcpy(clone->info.options, rep->info.options,
238 sizeof(CAHeaderOption_t) * rep->info.numOptions);
242 clone->info.options = NULL;
243 clone->info.numOptions = 0;
246 if (NULL != rep->info.payload)
248 // allocate payload field
249 uint8_t *temp = (uint8_t *) OICMalloc(rep->info.payloadSize);
252 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
254 CADestroyResponseInfoInternal(clone);
258 memcpy(temp, rep->info.payload, rep->info.payloadSize);
261 clone->info.payload = temp;
264 if (NULL != rep->info.resourceUri)
266 // allocate payload field
267 char *temp = OICStrdup(rep->info.resourceUri);
270 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
272 CADestroyResponseInfoInternal(clone);
277 // save the resourceUri
278 clone->info.resourceUri = temp;
284 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
285 CATransportAdapter_t adapter,
289 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
292 OIC_LOG(ERROR, TAG, "Memory allocation failed !");
298 OICStrcpy(info->addr, sizeof(info->addr), address);
299 info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
302 info->adapter = adapter;
308 void CAFreeEndpoint(CAEndpoint_t *rep)
313 static void CADestroyInfoInternal(CAInfo_t *info)
316 OICFree(info->token);
318 info->tokenLength = 0;
320 // free options field
321 OICFree(info->options);
322 info->options = NULL;
323 info->numOptions = 0;
326 // free payload field
327 OICFree((char *) info->payload);
328 info->payload = NULL;
329 info->payloadSize = 0;
332 OICFree(info->resourceUri);
333 info->resourceUri = NULL;
336 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
340 OIC_LOG(ERROR, TAG, "parameter is null");
344 CADestroyInfoInternal(&rep->info);
348 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
352 OIC_LOG(ERROR, TAG, "parameter is null");
356 CADestroyInfoInternal(&rep->info);
360 void CADestroyErrorInfoInternal(CAErrorInfo_t *errorInfo)
362 if (NULL == errorInfo)
364 OIC_LOG(ERROR, TAG, "parameter is null");
368 CADestroyInfoInternal(&errorInfo->info);
372 CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
376 OIC_LOG(ERROR, TAG, "input parameter invalid");
377 return CA_STATUS_INVALID_PARAM;
380 //Do not free clone. we cannot declare it const, as the content is modified
381 if ((info->token) && (0 < info->tokenLength))
385 // allocate token field
386 uint8_t len = info->tokenLength;
388 temp = (char *) OICMalloc(len * sizeof(char));
391 OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
392 return CA_MEMORY_ALLOC_FAILED;
395 memcpy(temp, info->token, len);
398 clone->tokenLength = len;
401 if (info->options && (0 < info->numOptions))
405 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * info->numOptions);
409 OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
410 CADestroyInfoInternal(clone);
411 return CA_MEMORY_ALLOC_FAILED;
413 memcpy(clone->options, info->options, sizeof(CAHeaderOption_t) * info->numOptions);
418 // allocate payload field
419 uint8_t *temp = OICMalloc(info->payloadSize);
422 OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
423 CADestroyInfoInternal(clone);
424 return CA_MEMORY_ALLOC_FAILED;
426 memcpy(temp, info->payload, info->payloadSize);
429 clone->payload = temp;
432 if (info->resourceUri)
434 // allocate payload field
435 char *temp = OICStrdup(info->resourceUri);
438 OIC_LOG(ERROR, TAG, "CAClonePayloadInfo Out of memory");
439 CADestroyInfoInternal(clone);
440 return CA_MEMORY_ALLOC_FAILED;
443 // save the resourceUri
444 clone->resourceUri = temp;