f193168c60f9e65e48cb636e3e88e6aa0b90cb30
[platform/upstream/iotivity.git] / resource / csdk / connectivity / 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 "oic_malloc.h"
22 #include "caremotehandler.h"
23 #include "logger.h"
24
25 #define TAG PCF("CA")
26
27 CARemoteEndpoint_t* CACloneRemoteEndpoint(const CARemoteEndpoint_t* rep)
28 {
29     char* temp = NULL;
30     int len = 0;
31
32     if (rep == NULL)
33         return NULL;
34
35     // allocate the remote end point structure.
36     CARemoteEndpoint_t* clone = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
37     if (clone == NULL)
38     {
39         OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
40         return NULL;
41     }
42     memset(clone, 0, sizeof(CARemoteEndpoint_t));
43     memcpy(clone, rep, sizeof(CARemoteEndpoint_t));
44
45     if (rep->resourceUri != NULL)
46     {
47         // allocate reference uri field
48         len = strlen(rep->resourceUri);
49
50         temp = (char*) OICMalloc(sizeof(char) * (len + 1));
51         if (temp == NULL)
52         {
53             OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
54
55             CADestroyRemoteEndpointInternal(clone);
56
57             return NULL;
58         }
59         memset(temp, 0, sizeof(char) * (len + 1));
60         strncpy(temp, rep->resourceUri, len);
61
62         // save the uri
63         clone->resourceUri = temp;
64     }
65
66     return clone;
67 }
68
69 #define COAP_PREFIX         "coap://"
70 #define COAP_PREFIX_LEN     7
71
72 // return 1 : ip
73 // return 0 : mac
74 static int32_t getCAAddress(const char* pAddress, CAAddress_t* outAddress)
75 {
76     if (pAddress == NULL || outAddress == NULL)
77         return -1;
78
79     // simple parse, it will be change.
80     // 10.11.12.13:4545 (ip)
81     // 10:11:12:13:45:45 (mac)
82
83     int32_t len = strlen(pAddress);
84
85     int32_t isIp = 0;
86     int32_t ipLen = 0;
87     int32_t port = 0;
88
89     int i = 0;
90     for (i = 0; i < len; i++)
91     {
92         if (pAddress[i] == '.')
93         {
94             isIp = 1;
95         }
96
97         // found port number start index
98         if (isIp && pAddress[i] == ':')
99         {
100             ipLen = i;
101             break;
102         }
103     }
104
105     if (isIp)
106     {
107         strncpy(outAddress->IP.ipAddress, pAddress, ipLen == 0 ? len : ipLen);
108
109         if (ipLen > 0)
110             outAddress->IP.port = atoi(pAddress + ipLen + 1);
111
112         OIC_LOG_V(DEBUG, TAG, "ip: %s,port: %d", outAddress->IP.ipAddress, outAddress->IP.port);
113     }
114     else
115     {
116         strncpy(outAddress->BT.btMacAddress, pAddress, CA_MACADDR_SIZE - 1);
117
118         OIC_LOG_V(DEBUG, TAG, "mac address : %s", outAddress->BT.btMacAddress);
119     }
120
121     return isIp;
122 }
123
124 CARemoteEndpoint_t* CACreateRemoteEndpointUriInternal(const CAURI_t uri)
125 {
126     // support URI type
127     // coap://10.11.12.13:4545/resource_uri
128     // coap://10:11:12:13:45:45/resource_uri
129
130     if (uri == NULL)
131         return NULL;
132
133     // parse uri
134     // #1. check prefix
135     int startIndex = 0;
136
137     if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
138     {
139         OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAP_PREFIX);
140         startIndex = COAP_PREFIX_LEN;
141     }
142
143     // #2. copy uri for parse
144     char* cloneUri = NULL;
145     int32_t len = strlen(uri) - startIndex;
146
147     if (len <= 0)
148     {
149         OIC_LOG_V(DEBUG, TAG, "uri length is 0!");
150         return NULL;
151     }
152
153     cloneUri = (char*) OICMalloc(sizeof(char) * (len + 1));
154     if (cloneUri == NULL)
155     {
156         OIC_LOG_V(DEBUG, TAG, "memory error!!");
157         return NULL;
158     }
159     memset(cloneUri, 0, sizeof(char) * (len + 1));
160     memcpy(cloneUri, &uri[startIndex], sizeof(char) * (len + 1));
161
162     // #3. parse address
163     // #4. parse resource uri
164     char* pAddress = cloneUri;
165     char* pResourceUri = NULL;
166
167     int32_t i = 0;
168     for (i = 0; i < len; i++)
169     {
170         if (cloneUri[i] == '/')
171         {
172             // separate
173             cloneUri[i] = 0;
174
175             pResourceUri = &cloneUri[i + 1];
176
177             break;
178         }
179
180     }
181
182     OIC_LOG_V(DEBUG, TAG, "pAddress : %s", pAddress);
183
184     OIC_LOG_V(DEBUG, TAG, "pResourceUri : %s", pResourceUri == NULL ? "" : pResourceUri);
185
186     // address
187     CAAddress_t address;
188     memset(&address, 0, sizeof(CAAddress_t));
189
190     int resType = getCAAddress(pAddress, &address);
191     if (resType == -1)
192     {
193         OIC_LOG_V(DEBUG, TAG, "address parse error");
194
195         OICFree(cloneUri);
196         return NULL;
197     }
198
199     // resource uri
200     CAURI_t resourceUri = pResourceUri;
201
202     // connectivity type
203     CAConnectivityType_t type;
204
205     if (resType == 1)
206     {
207         type = CA_WIFI;
208     }
209     else
210     {
211         type = CA_EDR;
212     }
213
214     CARemoteEndpoint_t* remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, address, type);
215
216     OICFree(cloneUri);
217
218     return remoteEndpoint;
219 }
220
221 CARemoteEndpoint_t* CACreateRemoteEndpointInternal(const CAURI_t resourceUri,
222         const CAAddress_t addr, const CAConnectivityType_t type)
223 {
224     char* temp = NULL;
225     int len = 0;
226
227     if (resourceUri == NULL)
228     {
229         OIC_LOG_V(DEBUG, TAG, "uri is null value");
230         return NULL;
231     }
232
233     // allocate the remote end point structure.
234     CARemoteEndpoint_t* rep = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
235
236     if (rep == NULL)
237     {
238         OIC_LOG_V(DEBUG, TAG, "memory alloc error");
239         return NULL;
240     }
241     memset(rep, 0, sizeof(CARemoteEndpoint_t));
242
243     // allocate reference uri field
244     len = strlen(resourceUri);
245
246     temp = (char*) OICMalloc(sizeof(char) * (len + 1));
247     if (temp == NULL)
248     {
249         OIC_LOG_V(DEBUG, TAG, "memory alloc error");
250
251         CADestroyRemoteEndpointInternal(rep);
252
253         return NULL;
254     }
255     memset(temp, 0, sizeof(char) * (len + 1));
256     strncpy(temp, resourceUri, len);
257
258     // save the uri
259     rep->resourceUri = temp;
260
261     // save the addressInfo
262     memcpy(&(rep->addressInfo), &addr, sizeof(CAAddress_t));
263
264     // save the type
265     rep->connectivityType = type;
266
267     return rep;
268 }
269
270 CARequestInfo_t* CACloneRequestInfo(const CARequestInfo_t* rep)
271 {
272     char* temp = NULL;
273     int len = 0;
274
275     if (rep == NULL)
276         return NULL;
277
278     // allocate the request info structure.
279     CARequestInfo_t* clone = (CARequestInfo_t*) OICMalloc(sizeof(CARequestInfo_t));
280     if (clone == NULL)
281     {
282         OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
283         return NULL;
284     }
285     memset(clone, 0, sizeof(CARequestInfo_t));
286     memcpy(clone, rep, sizeof(CARequestInfo_t));
287
288     if (rep->info.token != NULL)
289     {
290         // allocate token field
291         len = strlen(rep->info.token);
292
293         temp = (char*) OICMalloc(sizeof(char) * (len + 1));
294         if (temp == NULL)
295         {
296             OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
297
298             CADestroyRequestInfoInternal(clone);
299
300             return NULL;
301         }
302         memset(temp, 0, sizeof(char) * (len + 1));
303         strncpy(temp, rep->info.token, len);
304
305         // save the token
306         clone->info.token = temp;
307     }
308
309     if (rep->info.options != NULL)
310     {
311         // save the options
312         clone->info.options = (CAHeaderOption_t*) OICMalloc(sizeof(CAHeaderOption_t));
313         memset(clone->info.options, 0, sizeof(CAHeaderOption_t));
314         memcpy(clone->info.options, rep->info.options, sizeof(CAHeaderOption_t));
315     }
316
317     if (rep->info.payload != NULL)
318     {
319         // allocate payload field
320         len = strlen(rep->info.payload);
321
322         temp = (char*) OICMalloc(sizeof(char) * (len + 1));
323         if (temp == NULL)
324         {
325             OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
326
327             CADestroyRequestInfoInternal(clone);
328
329             return NULL;
330         }
331         memset(temp, 0, sizeof(char) * (len + 1));
332         strncpy(temp, rep->info.payload, len);
333
334         // save the payload
335         clone->info.payload = temp;
336     }
337
338     return clone;
339 }
340
341 CAResponseInfo_t* CACloneResponseInfo(const CAResponseInfo_t* rep)
342 {
343     char* temp = NULL;
344     int len = 0;
345
346     if (rep == NULL)
347         return NULL;
348
349     // allocate the response info structure.
350     CAResponseInfo_t* clone = (CAResponseInfo_t*) OICMalloc(sizeof(CAResponseInfo_t));
351     if (clone == NULL)
352     {
353         OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
354         return NULL;
355     }
356     memset(clone, 0, sizeof(CAResponseInfo_t));
357     memcpy(clone, rep, sizeof(CAResponseInfo_t));
358
359     if (rep->info.token != NULL)
360     {
361         // allocate token field
362         len = strlen(rep->info.token);
363
364         temp = (char*) OICMalloc(sizeof(char) * (len + 1));
365         if (temp == NULL)
366         {
367             OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
368
369             CADestroyResponseInfoInternal(clone);
370
371             return NULL;
372         }
373         memset(temp, 0, sizeof(char) * (len + 1));
374         strncpy(temp, rep->info.token, len);
375
376         // save the token
377         clone->info.token = temp;
378     }
379
380     if (rep->info.options != NULL)
381     {
382         // save the options
383         clone->info.options = (CAHeaderOption_t*) OICMalloc(sizeof(CAHeaderOption_t));
384         memset(clone->info.options, 0, sizeof(CAHeaderOption_t));
385         memcpy(clone->info.options, rep->info.options, sizeof(CAHeaderOption_t));
386     }
387
388     if (rep->info.payload != NULL)
389     {
390         // allocate payload field
391         len = strlen(rep->info.payload);
392
393         temp = (char*) OICMalloc(sizeof(char) * (len + 1));
394         if (temp == NULL)
395         {
396             OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
397
398             CADestroyResponseInfoInternal(clone);
399
400             return NULL;
401         }
402         memset(temp, 0, sizeof(char) * (len + 1));
403         strncpy(temp, rep->info.payload, len);
404
405         // save the payload
406         clone->info.payload = temp;
407     }
408
409     return clone;
410 }
411
412 void CADestroyRemoteEndpointInternal(CARemoteEndpoint_t* rep)
413 {
414     if (rep == NULL)
415         return;
416
417     // free uri field
418     if (rep->resourceUri != NULL)
419     {
420         OICFree((char*) rep->resourceUri);
421     }
422
423     // free remote end point structure.
424     OICFree(rep);
425 }
426
427 void CADestroyRequestInfoInternal(CARequestInfo_t* rep)
428 {
429     if (rep == NULL)
430         return;
431
432     // free token field
433     if (rep->info.token != NULL)
434     {
435         OICFree((char*) rep->info.token);
436     }
437
438     // free options field
439     if (rep->info.options != NULL)
440     {
441         OICFree((CAHeaderOption_t*) rep->info.options);
442     }
443
444     // free payload field
445     if (rep->info.payload != NULL)
446     {
447         OICFree((char*) rep->info.payload);
448     }
449
450     OICFree(rep);
451 }
452
453 void CADestroyResponseInfoInternal(CAResponseInfo_t* rep)
454 {
455     if (rep == NULL)
456         return;
457
458     // free token field
459     if (rep->info.token != NULL)
460     {
461         OICFree((char*) rep->info.token);
462     }
463
464     // free options field
465     if (rep->info.options != NULL)
466     {
467         OICFree((CAHeaderOption_t*) rep->info.options);
468     }
469
470     // free payload field
471     if (rep->info.payload != NULL)
472     {
473         OICFree((char*) rep->info.payload);
474     }
475
476     OICFree(rep);
477 }