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