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