Imported Upstream version 0.9.2
[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     *clone = *rep;
67
68     if (rep->info.token)
69     {
70         char *temp = NULL;
71
72         // allocate token field
73         uint8_t len = rep->info.tokenLength;
74
75         if (len)
76         {
77             temp = (char *) OICCalloc(len, sizeof(char));
78             if (!temp)
79             {
80                 OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
81
82                 CADestroyRequestInfoInternal(clone);
83
84                 return NULL;
85             }
86             memcpy(temp, rep->info.token, len);
87         }
88
89         // save the token
90         clone->info.token = temp;
91         clone->info.tokenLength = len;
92     }
93
94     if (NULL != rep->info.options && 0 < rep->info.numOptions)
95     {
96         // save the options
97         clone->info.options =
98             (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
99         if (NULL == clone->info.options)
100         {
101             OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
102             OICFree(clone->info.token);
103             OICFree(clone);
104             return NULL;
105         }
106         memcpy(clone->info.options, rep->info.options,
107                sizeof(CAHeaderOption_t) * rep->info.numOptions);
108     }
109     else
110     {
111         clone->info.options = NULL;
112         clone->info.numOptions = 0;
113     }
114
115     if (NULL != rep->info.payload)
116     {
117         // allocate payload field
118         uint8_t *temp = OICMalloc(rep->info.payloadSize);
119         if (NULL == temp)
120         {
121             OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
122
123             CADestroyRequestInfoInternal(clone);
124
125             return NULL;
126         }
127         memcpy(temp, rep->info.payload, rep->info.payloadSize);
128
129         // save the payload
130         clone->info.payload = temp;
131     }
132
133     if (NULL != rep->info.resourceUri)
134     {
135         // allocate payload field
136         char *temp = OICStrdup(rep->info.resourceUri);
137         if (NULL == temp)
138         {
139             OIC_LOG(ERROR, TAG, "CACloneRequestInfo Out of memory");
140
141             CADestroyRequestInfoInternal(clone);
142
143             return NULL;
144         }
145
146         // save the resourceUri
147         clone->info.resourceUri = temp;
148     }
149
150     return clone;
151 }
152
153 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
154 {
155     if (NULL == rep)
156     {
157         OIC_LOG(ERROR, TAG, "Response pointer is NULL");
158         return NULL;
159     }
160
161     // check the result value of response info.
162     // Keep this check in sync with CAResponseResult_t
163     switch (rep->result)
164     {
165         case CA_EMPTY:
166         case CA_SUCCESS:
167         case CA_CREATED:
168         case CA_DELETED:
169         case CA_VALID:
170         case CA_CHANGED:
171         case CA_CONTENT:
172         case CA_BAD_REQ:
173         case CA_UNAUTHORIZED_REQ:
174         case CA_BAD_OPT:
175         case CA_FORBIDDEN_REQ:
176         case CA_NOT_FOUND:
177         case CA_INTERNAL_SERVER_ERROR:
178         case CA_RETRANSMIT_TIMEOUT:
179             break;
180
181         default:
182             OIC_LOG_V(ERROR, TAG, "Response code  %u is invalid", rep->result);
183             return NULL;
184     }
185
186     // allocate the response info structure.
187     CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
188     if (NULL == clone)
189     {
190         OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
191         return NULL;
192     }
193     *clone = *rep;
194
195     if (rep->info.token)
196     {
197         char *temp = NULL;
198
199         // allocate token field
200         uint8_t len = rep->info.tokenLength;
201
202         if (len)
203         {
204             temp = (char *) OICCalloc(len, sizeof(char));
205             if (!temp)
206             {
207                 OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
208
209                 CADestroyResponseInfoInternal(clone);
210
211                 return NULL;
212             }
213             memcpy(temp, rep->info.token, len);
214         }
215         // save the token
216         clone->info.token = temp;
217         clone->info.tokenLength = len;
218     }
219
220     if (NULL != rep->info.options && rep->info.numOptions)
221     {
222         // save the options
223         clone->info.options =
224                 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
225
226         if (NULL == clone->info.options)
227         {
228             OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
229
230             OICFree(clone->info.token);
231             OICFree(clone);
232             return NULL;
233         }
234         memcpy(clone->info.options, rep->info.options,
235                 sizeof(CAHeaderOption_t) * rep->info.numOptions);
236     }
237     else
238     {
239         clone->info.options = NULL;
240         clone->info.numOptions = 0;
241     }
242
243     if (NULL != rep->info.payload)
244     {
245         // allocate payload field
246         uint8_t *temp = (uint8_t *) OICMalloc(rep->info.payloadSize);
247         if (NULL == temp)
248         {
249             OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
250
251             CADestroyResponseInfoInternal(clone);
252
253             return NULL;
254         }
255         memcpy(temp, rep->info.payload, rep->info.payloadSize);
256
257         // save the payload
258         clone->info.payload = temp;
259     }
260
261     if (NULL != rep->info.resourceUri)
262     {
263         // allocate payload field
264         char *temp = OICStrdup(rep->info.resourceUri);
265         if (NULL == temp)
266         {
267             OIC_LOG(ERROR, TAG, "CACloneResponseInfo Out of memory");
268
269             CADestroyResponseInfoInternal(clone);
270
271             return NULL;
272         }
273
274         // save the resourceUri
275         clone->info.resourceUri = temp;
276     }
277
278     return clone;
279 }
280
281 CAEndpoint_t *CACreateEndpointObject(CATransportFlags_t flags,
282                                      CATransportAdapter_t adapter,
283                                      const char *address,
284                                      uint16_t port)
285 {
286     CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
287     if (NULL == info)
288     {
289         OIC_LOG(ERROR, TAG, "Memory allocation failed !");
290         return NULL;
291     }
292
293     if (address)
294     {
295         OICStrcpy(info->addr, sizeof(info->addr), address);
296         info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
297     }
298     info->flags = flags;
299     info->adapter = adapter;
300     info->port = port;
301
302     return info;
303 }
304
305 void CAFreeEndpoint(CAEndpoint_t *rep)
306 {
307     OICFree(rep);
308 }
309
310 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
311 {
312     if (NULL == rep)
313     {
314         OIC_LOG(ERROR, TAG, "parameter is null");
315         return;
316     }
317
318     // free token field
319     OICFree(rep->info.token);
320
321     // free options field
322     OICFree((CAHeaderOption_t *) rep->info.options);
323
324     // free payload field
325     OICFree((char *) rep->info.payload);
326
327     // free uri
328     OICFree(rep->info.resourceUri);
329
330     OICFree(rep);
331 }
332
333 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
334 {
335     if (NULL == rep)
336     {
337         OIC_LOG(ERROR, TAG, "parameter is null");
338         return;
339     }
340
341     // free token field
342     OICFree(rep->info.token);
343
344     // free options field
345     if (rep->info.options != NULL && rep->info.numOptions)
346     {
347         OICFree((CAHeaderOption_t *) rep->info.options);
348     }
349
350     // free payload field
351     OICFree((char *) rep->info.payload);
352
353     // free uri
354     OICFree(rep->info.resourceUri);
355
356     OICFree(rep);
357 }
358