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;
132 if (NULL != rep->info.resourceUri)
134 // allocate payload field
135 char *temp = OICStrdup(rep->info.resourceUri);
138 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
140 CADestroyRequestInfoInternal(clone);
145 // save the resourceUri
146 clone->info.resourceUri = temp;
152 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
156 OIC_LOG(ERROR, TAG, "Response pointer is NULL");
160 // check the result value of response info.
173 case CA_INTERNAL_SERVER_ERROR:
174 case CA_RETRANSMIT_TIMEOUT:
178 OIC_LOG(ERROR, TAG, "Response status code is invalid number");
182 // allocate the response info structure.
183 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
186 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
195 // allocate token field
196 uint8_t len = rep->info.tokenLength;
200 temp = (char *) OICCalloc(len, sizeof(char));
203 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
205 CADestroyResponseInfoInternal(clone);
209 memcpy(temp, rep->info.token, len);
212 clone->info.token = temp;
213 clone->info.tokenLength = len;
216 if (NULL != rep->info.options && rep->info.numOptions)
219 clone->info.options =
220 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
222 if (NULL == clone->info.options)
224 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
226 OICFree(clone->info.token);
230 memcpy(clone->info.options, rep->info.options,
231 sizeof(CAHeaderOption_t) * rep->info.numOptions);
235 clone->info.options = NULL;
236 clone->info.numOptions = 0;
239 if (NULL != rep->info.payload)
241 // allocate payload field
242 char *temp = OICStrdup(rep->info.payload);
245 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
247 CADestroyResponseInfoInternal(clone);
253 clone->info.payload = temp;
256 if (NULL != rep->info.resourceUri)
258 // allocate payload field
259 char *temp = OICStrdup(rep->info.resourceUri);
262 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
264 CADestroyResponseInfoInternal(clone);
269 // save the resourceUri
270 clone->info.resourceUri = temp;
276 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
277 CATransportAdapter_t adapter,
281 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
284 OIC_LOG(ERROR, TAG, "Memory allocation failed !");
290 OICStrcpy(info->addr, sizeof(info->addr), address);
291 info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
294 info->adapter = adapter;
300 void CAFreeEndpoint(CAEndpoint_t *rep)
305 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
309 OIC_LOG(ERROR, TAG, "parameter is null");
314 OICFree(rep->info.token);
316 // free options field
317 OICFree((CAHeaderOption_t *) rep->info.options);
319 // free payload field
320 OICFree((char *) rep->info.payload);
323 OICFree(rep->info.resourceUri);
328 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
332 OIC_LOG(ERROR, TAG, "parameter is null");
337 OICFree(rep->info.token);
339 // free options field
340 if (rep->info.options != NULL && rep->info.numOptions)
342 OICFree((CAHeaderOption_t *) rep->info.options);
345 // free payload field
346 OICFree((char *) rep->info.payload);
349 OICFree(rep->info.resourceUri);