Update the stack to use the new CoAP return code 2.04/2.05 values.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / caremotehandler.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <string.h>
22
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25 #include "caremotehandler.h"
26 #include "logger.h"
27
28 #define TAG "CA"
29
30 CAEndpoint_t *CACloneEndpoint(const CAEndpoint_t *rep)
31 {
32     if (NULL == rep)
33     {
34         OIC_LOG(ERROR, TAG, "parameter is null");
35         return NULL;
36     }
37
38     // allocate the remote end point structure.
39     CAEndpoint_t *clone = (CAEndpoint_t *)OICMalloc(sizeof (CAEndpoint_t));
40     if (NULL == clone)
41     {
42         OIC_LOG(ERROR, TAG, "CACloneRemoteEndpoint Out of memory");
43         return NULL;
44     }
45     *clone = *rep;
46
47     return clone;
48 }
49
50 CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
51 {
52     if (NULL == rep)
53     {
54         OIC_LOG(ERROR, TAG, "parameter is null");
55         return NULL;
56     }
57
58     // allocate the request info structure.
59     CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
60     if (!clone)
61     {
62         OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
63         return NULL;
64     }
65
66     CAResult_t result = CACloneInfo(&rep->info, &clone->info);
67     if(CA_STATUS_OK != result)
68     {
69         OIC_LOG(ERROR, TAG, "CACloneRequestInfo error in CACloneInfo");
70         CADestroyRequestInfoInternal(clone);
71         return NULL;
72     }
73
74     clone->method = rep->method;
75     clone->isMulticast = rep->isMulticast;
76
77     return clone;
78 }
79
80 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
81 {
82     if (NULL == rep)
83     {
84         OIC_LOG(ERROR, TAG, "Response pointer is NULL");
85         return NULL;
86     }
87
88     // check the result value of response info.
89     // Keep this check in sync with CAResponseResult_t
90     switch (rep->result)
91     {
92         case CA_EMPTY:
93         case CA_CREATED:
94         case CA_DELETED:
95         case CA_VALID:
96         case CA_CONTENT:
97         case CA_CHANGED:
98         case CA_CONTINUE:
99         case CA_BAD_REQ:
100         case CA_UNAUTHORIZED_REQ:
101         case CA_BAD_OPT:
102         case CA_FORBIDDEN_REQ:
103         case CA_NOT_FOUND:
104         case CA_NOT_ACCEPTABLE:
105         case CA_REQUEST_ENTITY_INCOMPLETE:
106         case CA_REQUEST_ENTITY_TOO_LARGE:
107         case CA_INTERNAL_SERVER_ERROR:
108         case CA_RETRANSMIT_TIMEOUT:
109             break;
110
111         case CA_SUCCESS:
112         default:
113             OIC_LOG_V(ERROR, TAG, "Response code  %u is invalid", rep->result);
114             return NULL;
115     }
116
117     // allocate the response info structure.
118     CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
119     if (NULL == clone)
120     {
121         OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
122         return NULL;
123     }
124
125     CAResult_t result = CACloneInfo(&rep->info, &clone->info);
126     if(CA_STATUS_OK != result)
127     {
128         OIC_LOG(ERROR, TAG, "CACloneResponseInfo error in CACloneInfo");
129         CADestroyResponseInfoInternal(clone);
130         return NULL;
131     }
132
133     clone->isMulticast = rep->isMulticast;
134     clone->result = rep->result;
135     return clone;
136 }
137
138 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
139                                      CATransportAdapter_t adapter,
140                                      const char *address,
141                                      uint16_t port)
142 {
143     CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
144     if (NULL == info)
145     {
146         OIC_LOG(ERROR, TAG, "Memory allocation failed !");
147         return NULL;
148     }
149
150     if (address)
151     {
152         OICStrcpy(info->addr, sizeof(info->addr), address);
153         info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
154     }
155     info->flags = flags;
156     info->adapter = adapter;
157     info->port = port;
158
159     return info;
160 }
161
162 void CAFreeEndpoint(CAEndpoint_t *rep)
163 {
164     OICFree(rep);
165 }
166
167 static void CADestroyInfoInternal(CAInfo_t *info)
168 {
169     // free token field
170     OICFree(info->token);
171     info->token = NULL;
172     info->tokenLength = 0;
173
174     // free options field
175     OICFree(info->options);
176     info->options = NULL;
177     info->numOptions = 0;
178
179     // free payload field
180     OICFree((char *) info->payload);
181     info->payload = NULL;
182     info->payloadSize = 0;
183
184     // free uri
185     OICFree(info->resourceUri);
186     info->resourceUri = NULL;
187 }
188
189 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
190 {
191     if (NULL == rep)
192     {
193         OIC_LOG(ERROR, TAG, "parameter is null");
194         return;
195     }
196
197     CADestroyInfoInternal(&rep->info);
198     OICFree(rep);
199 }
200
201 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
202 {
203     if (NULL == rep)
204     {
205         OIC_LOG(ERROR, TAG, "parameter is null");
206         return;
207     }
208
209     CADestroyInfoInternal(&rep->info);
210     OICFree(rep);
211 }
212
213 void CADestroyErrorInfoInternal(CAErrorInfo_t *errorInfo)
214 {
215     if (NULL == errorInfo)
216     {
217         OIC_LOG(ERROR, TAG, "parameter is null");
218         return;
219     }
220
221     CADestroyInfoInternal(&errorInfo->info);
222     OICFree(errorInfo);
223 }
224
225 CAResult_t CACloneInfo(const CAInfo_t *info, CAInfo_t *clone)
226 {
227     if (!info || !clone)
228     {
229         OIC_LOG(ERROR, TAG, "input parameter invalid");
230         return CA_STATUS_INVALID_PARAM;
231     }
232
233     memset(clone, 0 , sizeof(CAInfo_t));
234
235     //Do not free clone. we cannot declare it const, as the content is modified
236     if ((info->token) && (0 < info->tokenLength))
237     {
238         char *temp = NULL;
239
240         // allocate token field
241         uint8_t len = info->tokenLength;
242
243         temp = (char *) OICMalloc(len * sizeof(char));
244         if (!temp)
245         {
246             OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
247             return CA_MEMORY_ALLOC_FAILED;
248         }
249
250         memcpy(temp, info->token, len);
251         // save the token
252         clone->token = temp;
253         clone->tokenLength = len;
254     }
255
256     if (info->options && (0 < info->numOptions))
257     {
258         // save the options
259         clone->options =
260             (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * info->numOptions);
261
262         if (!clone->options)
263         {
264             OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
265             CADestroyInfoInternal(clone);
266             return CA_MEMORY_ALLOC_FAILED;
267         }
268         memcpy(clone->options, info->options, sizeof(CAHeaderOption_t) * info->numOptions);
269         clone->numOptions = info->numOptions;
270     }
271
272     if ((info->payload) && (0 < info->payloadSize))
273     {
274         // allocate payload field
275         uint8_t *temp = OICMalloc(info->payloadSize);
276         if (!temp)
277         {
278             OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
279             CADestroyInfoInternal(clone);
280             return CA_MEMORY_ALLOC_FAILED;
281         }
282         memcpy(temp, info->payload, info->payloadSize);
283
284         // save the payload
285         clone->payload = temp;
286         clone->payloadSize = info->payloadSize;
287     }
288     clone->payloadFormat = info->payloadFormat;
289     clone->acceptFormat = info->acceptFormat;
290
291     if (info->resourceUri)
292     {
293         // allocate payload field
294         char *temp = OICStrdup(info->resourceUri);
295         if (!temp)
296         {
297             OIC_LOG(ERROR, TAG, "CACloneInfo Out of memory");
298             CADestroyInfoInternal(clone);
299             return CA_MEMORY_ALLOC_FAILED;
300         }
301
302         // save the resourceUri
303         clone->resourceUri = temp;
304     }
305
306     clone->messageId = info->messageId;
307     clone->type = info->type;
308
309     return CA_STATUS_OK;
310
311 }