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 // free options field
319 OICFree(info->options);
321 // free payload field
322 OICFree((char *) info->payload);
325 OICFree(info->resourceUri);
328 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
332 OIC_LOG(ERROR, TAG, "parameter is null");
336 CADestroyInfoInternal(&rep->info);
340 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
344 OIC_LOG(ERROR, TAG, "parameter is null");
348 CADestroyInfoInternal(&rep->info);
352 void CADestroyErrorInfoInternal(CAErrorInfo_t *errorInfo)
354 if (NULL == errorInfo)
356 OIC_LOG(ERROR, TAG, "parameter is null");
360 CADestroyInfoInternal(&errorInfo->info);