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.
156 case CA_INTERNAL_SERVER_ERROR:
157 case CA_RETRANSMIT_TIMEOUT:
161 OIC_LOG(ERROR, TAG, "Response status code is invalid number");
165 // allocate the response info structure.
166 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
169 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
178 // allocate token field
179 uint8_t len = rep->info.tokenLength;
183 temp = (char *) OICCalloc(len, sizeof(char));
186 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
188 CADestroyResponseInfoInternal(clone);
192 memcpy(temp, rep->info.token, len);
195 clone->info.token = temp;
196 clone->info.tokenLength = len;
199 if (NULL != rep->info.options && rep->info.numOptions)
202 clone->info.options =
203 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
205 if (NULL == clone->info.options)
207 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
209 OICFree(clone->info.token);
213 memcpy(clone->info.options, rep->info.options,
214 sizeof(CAHeaderOption_t) * rep->info.numOptions);
218 clone->info.options = NULL;
219 clone->info.numOptions = 0;
222 if (NULL != rep->info.payload)
224 // allocate payload field
225 char *temp = OICStrdup(rep->info.payload);
228 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
230 CADestroyResponseInfoInternal(clone);
236 clone->info.payload = temp;
242 void CADestroyEndpointInternal(CAEndpoint_t *rep)
247 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
251 OIC_LOG(ERROR, TAG, "parameter is null");
256 OICFree(rep->info.token);
258 // free options field
259 OICFree((CAHeaderOption_t *) rep->info.options);
261 // free payload field
262 OICFree((char *) rep->info.payload);
267 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
271 OIC_LOG(ERROR, TAG, "parameter is null");
276 OICFree(rep->info.token);
278 // free options field
279 if (rep->info.options != NULL && rep->info.numOptions)
281 OICFree((CAHeaderOption_t *) rep->info.options);
284 // free payload field
285 OICFree((char *) rep->info.payload);