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 char *temp = OICStrdup(rep->info.payload);
121 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
123 CADestroyRequestInfoInternal(clone);
129 clone->info.payload = temp;
135 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
139 OIC_LOG(ERROR, TAG, "Response pointer is NULL");
143 // check the result value of response info.
153 case CA_INTERNAL_SERVER_ERROR:
154 case CA_RETRANSMIT_TIMEOUT:
158 OIC_LOG(ERROR, TAG, "Response status code is invalid number");
162 // allocate the response info structure.
163 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
166 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
175 // allocate token field
176 uint8_t len = rep->info.tokenLength;
180 temp = (char *) OICCalloc(len, sizeof(char));
183 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
185 CADestroyResponseInfoInternal(clone);
189 memcpy(temp, rep->info.token, len);
192 clone->info.token = temp;
193 clone->info.tokenLength = len;
196 if (NULL != rep->info.options && rep->info.numOptions)
199 clone->info.options =
200 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
202 if (NULL == clone->info.options)
204 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
206 OICFree(clone->info.token);
210 memcpy(clone->info.options, rep->info.options,
211 sizeof(CAHeaderOption_t) * rep->info.numOptions);
215 clone->info.options = NULL;
216 clone->info.numOptions = 0;
219 if (NULL != rep->info.payload)
221 // allocate payload field
222 char *temp = OICStrdup(rep->info.payload);
225 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
227 CADestroyResponseInfoInternal(clone);
233 clone->info.payload = temp;
239 void CADestroyEndpointInternal(CAEndpoint_t *rep)
244 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
248 OIC_LOG(ERROR, TAG, "parameter is null");
253 OICFree(rep->info.token);
255 // free options field
256 OICFree((CAHeaderOption_t *) rep->info.options);
258 // free payload field
259 OICFree((char *) rep->info.payload);
264 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
268 OIC_LOG(ERROR, TAG, "parameter is null");
273 OICFree(rep->info.token);
275 // free options field
276 if (rep->info.options != NULL && rep->info.numOptions)
278 OICFree((CAHeaderOption_t *) rep->info.options);
281 // free payload field
282 OICFree((char *) rep->info.payload);