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 // check the method type of request info.
59 // Keep this check in sync with CAMethod_t
68 OIC_LOG_V(ERROR, TAG, "Method %u is invalid", rep->method);
72 // allocate the request info structure.
73 CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
76 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
80 CAResult_t result = CACloneInfo(&rep->info, &clone->info);
81 if(CA_STATUS_OK != result)
83 OIC_LOG(ERROR, TAG, "CACloneRequestInfo error in CACloneInfo");
84 CADestroyRequestInfoInternal(clone);
88 clone->method = rep->method;
89 clone->isMulticast = rep->isMulticast;
94 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
98 OIC_LOG(ERROR, TAG, "Response pointer is NULL");
102 // check the result value of response info.
103 // Keep this check in sync with CAResponseResult_t
114 case CA_UNAUTHORIZED_REQ:
116 case CA_FORBIDDEN_REQ:
118 case CA_METHOD_NOT_ALLOWED:
119 case CA_NOT_ACCEPTABLE:
120 case CA_REQUEST_ENTITY_INCOMPLETE:
121 case CA_REQUEST_ENTITY_TOO_LARGE:
122 case CA_INTERNAL_SERVER_ERROR:
123 case CA_NOT_IMPLEMENTED:
125 case CA_SERVICE_UNAVAILABLE:
126 case CA_RETRANSMIT_TIMEOUT:
127 case CA_PROXY_NOT_SUPPORTED:
130 OIC_LOG_V(ERROR, TAG, "Response code %u is invalid", rep->result);
134 // allocate the response info structure.
135 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
138 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
142 CAResult_t result = CACloneInfo(&rep->info, &clone->info);
143 if(CA_STATUS_OK != result)
145 OIC_LOG(ERROR, TAG, "CACloneResponseInfo error in CACloneInfo");
146 CADestroyResponseInfoInternal(clone);
150 clone->isMulticast = rep->isMulticast;
151 clone->result = rep->result;
155 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
156 CATransportAdapter_t adapter,
160 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
163 OIC_LOG(ERROR, TAG, "Memory allocation failed !");
169 OICStrcpy(info->addr, sizeof(info->addr), address);
170 info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
173 info->adapter = adapter;
179 void CAFreeEndpoint(CAEndpoint_t *rep)
184 static void CADestroyInfoInternal(CAInfo_t *info)
187 OICFree(info->token);
189 info->tokenLength = 0;
191 // free options field
192 OICFree(info->options);
193 info->options = NULL;
194 info->numOptions = 0;
196 // free payload field
197 OICFree((char *) info->payload);
198 info->payload = NULL;
199 info->payloadSize = 0;
202 OICFree(info->resourceUri);
203 info->resourceUri = NULL;
206 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
210 OIC_LOG(ERROR, TAG, "parameter is null");
214 CADestroyInfoInternal(&rep->info);
218 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
222 OIC_LOG(ERROR, TAG, "parameter is null");
226 CADestroyInfoInternal(&rep->info);
230 void CADestroyErrorInfoInternal(CAErrorInfo_t *errorInfo)
232 if (NULL == errorInfo)
234 OIC_LOG(ERROR, TAG, "parameter is null");
238 CADestroyInfoInternal(&errorInfo->info);
242 CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
246 OIC_LOG(ERROR, TAG, "input parameter invalid");
247 return CA_STATUS_INVALID_PARAM;
250 memset(clone, 0 , sizeof(CAInfo_t));
252 //Do not free clone. we cannot declare it const, as the content is modified
253 if ((info->token) && (0 < info->tokenLength))
255 // allocate token field
256 uint8_t len = info->tokenLength;
258 char *temp = (char *) OICMalloc(len * sizeof(char));
261 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
265 memcpy(temp, info->token, len);
268 clone->tokenLength = len;
271 if (info->options && (0 < info->numOptions))
275 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * info->numOptions);
279 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
282 memcpy(clone->options, info->options, sizeof(CAHeaderOption_t) * info->numOptions);
283 clone->numOptions = info->numOptions;
286 memcpy(&(clone->identity), &(info->identity), sizeof(info->identity));
288 if ((info->payload) && (0 < info->payloadSize))
290 // allocate payload field
291 uint8_t *temp = OICMalloc(info->payloadSize);
294 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
297 memcpy(temp, info->payload, info->payloadSize);
300 clone->payload = temp;
301 clone->payloadSize = info->payloadSize;
303 clone->payloadFormat = info->payloadFormat;
304 clone->acceptFormat = info->acceptFormat;
306 if (info->resourceUri)
308 // allocate payload field
309 char *temp = OICStrdup(info->resourceUri);
312 OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
316 // save the resourceUri
317 clone->resourceUri = temp;
320 #ifdef ROUTING_GATEWAY
321 clone->skipRetransmission = info->skipRetransmission;
324 clone->messageId = info->messageId;
325 clone->type = info->type;
330 CADestroyInfoInternal(clone);
331 return CA_MEMORY_ALLOC_FAILED;