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
173 case CA_UNAUTHORIZED_REQ:
175 case CA_FORBIDDEN_REQ:
177 case CA_INTERNAL_SERVER_ERROR:
178 case CA_RETRANSMIT_TIMEOUT:
182 OIC_LOG_V(ERROR, TAG, "Response code %u is invalid", rep->result);
186 // allocate the response info structure.
187 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
190 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
199 // allocate token field
200 uint8_t len = rep->info.tokenLength;
204 temp = (char *) OICCalloc(len, sizeof(char));
207 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
209 CADestroyResponseInfoInternal(clone);
213 memcpy(temp, rep->info.token, len);
216 clone->info.token = temp;
217 clone->info.tokenLength = len;
220 if (NULL != rep->info.options && rep->info.numOptions)
223 clone->info.options =
224 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
226 if (NULL == clone->info.options)
228 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
230 OICFree(clone->info.token);
234 memcpy(clone->info.options, rep->info.options,
235 sizeof(CAHeaderOption_t) * rep->info.numOptions);
239 clone->info.options = NULL;
240 clone->info.numOptions = 0;
243 if (NULL != rep->info.payload)
245 // allocate payload field
246 uint8_t *temp = (uint8_t *) OICMalloc(rep->info.payloadSize);
249 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
251 CADestroyResponseInfoInternal(clone);
255 memcpy(temp, rep->info.payload, rep->info.payloadSize);
258 clone->info.payload = temp;
261 if (NULL != rep->info.resourceUri)
263 // allocate payload field
264 char *temp = OICStrdup(rep->info.resourceUri);
267 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
269 CADestroyResponseInfoInternal(clone);
274 // save the resourceUri
275 clone->info.resourceUri = temp;
281 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
282 CATransportAdapter_t adapter,
286 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
289 OIC_LOG(ERROR, TAG, "Memory allocation failed !");
295 OICStrcpy(info->addr, sizeof(info->addr), address);
296 info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
299 info->adapter = adapter;
305 void CAFreeEndpoint(CAEndpoint_t *rep)
310 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
314 OIC_LOG(ERROR, TAG, "parameter is null");
319 OICFree(rep->info.token);
321 // free options field
322 OICFree((CAHeaderOption_t *) rep->info.options);
324 // free payload field
325 OICFree((char *) rep->info.payload);
328 OICFree(rep->info.resourceUri);
333 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
337 OIC_LOG(ERROR, TAG, "parameter is null");
342 OICFree(rep->info.token);
344 // free options field
345 if (rep->info.options != NULL && rep->info.numOptions)
347 OICFree((CAHeaderOption_t *) rep->info.options);
350 // free payload field
351 OICFree((char *) rep->info.payload);
354 OICFree(rep->info.resourceUri);