1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "oic_malloc.h"
22 #include "caremotehandler.h"
27 CARemoteEndpoint_t* CACloneRemoteEndpoint(const CARemoteEndpoint_t* rep)
35 // allocate the remote end point structure.
36 CARemoteEndpoint_t* clone = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
39 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
42 memset(clone, 0, sizeof(CARemoteEndpoint_t));
43 memcpy(clone, rep, sizeof(CARemoteEndpoint_t));
45 if (rep->resourceUri != NULL)
47 // allocate reference uri field
48 len = strlen(rep->resourceUri);
50 temp = (char*) OICMalloc(sizeof(char) * (len + 1));
53 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
55 CADestroyRemoteEndpointInternal(clone);
59 memset(temp, 0, sizeof(char) * (len + 1));
60 strncpy(temp, rep->resourceUri, len);
63 clone->resourceUri = temp;
69 #define COAP_PREFIX "coap://"
70 #define COAP_PREFIX_LEN 7
74 static int32_t getCAAddress(const char* pAddress, CAAddress_t* outAddress)
76 if (pAddress == NULL || outAddress == NULL)
79 // simple parse, it will be change.
80 // 10.11.12.13:4545 (ip)
81 // 10:11:12:13:45:45 (mac)
83 int32_t len = strlen(pAddress);
90 for (i = 0; i < len; i++)
92 if (pAddress[i] == '.')
97 // found port number start index
98 if (isIp && pAddress[i] == ':')
107 strncpy(outAddress->IP.ipAddress, pAddress, ipLen == 0 ? len : ipLen);
110 outAddress->IP.port = atoi(pAddress + ipLen + 1);
112 OIC_LOG_V(DEBUG, TAG, "ip: %s,port: %d", outAddress->IP.ipAddress, outAddress->IP.port);
116 strncpy(outAddress->BT.btMacAddress, pAddress, CA_MACADDR_SIZE - 1);
118 OIC_LOG_V(DEBUG, TAG, "mac address : %s", outAddress->BT.btMacAddress);
124 CARemoteEndpoint_t* CACreateRemoteEndpointUriInternal(const CAURI_t uri)
127 // coap://10.11.12.13:4545/resource_uri
128 // coap://10:11:12:13:45:45/resource_uri
137 if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
139 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAP_PREFIX);
140 startIndex = COAP_PREFIX_LEN;
143 // #2. copy uri for parse
144 char* cloneUri = NULL;
145 int32_t len = strlen(uri) - startIndex;
149 OIC_LOG_V(DEBUG, TAG, "uri length is 0!");
153 cloneUri = (char*) OICMalloc(sizeof(char) * (len + 1));
154 if (cloneUri == NULL)
156 OIC_LOG_V(DEBUG, TAG, "memory error!!");
159 memset(cloneUri, 0, sizeof(char) * (len + 1));
160 memcpy(cloneUri, &uri[startIndex], sizeof(char) * (len + 1));
163 // #4. parse resource uri
164 char* pAddress = cloneUri;
165 char* pResourceUri = NULL;
168 for (i = 0; i < len; i++)
170 if (cloneUri[i] == '/')
175 pResourceUri = &cloneUri[i + 1];
182 OIC_LOG_V(DEBUG, TAG, "pAddress : %s", pAddress);
184 OIC_LOG_V(DEBUG, TAG, "pResourceUri : %s", pResourceUri == NULL ? "" : pResourceUri);
188 memset(&address, 0, sizeof(CAAddress_t));
190 int resType = getCAAddress(pAddress, &address);
193 OIC_LOG_V(DEBUG, TAG, "address parse error");
200 CAURI_t resourceUri = pResourceUri;
203 CAConnectivityType_t type;
214 CARemoteEndpoint_t* remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, address, type);
218 return remoteEndpoint;
221 CARemoteEndpoint_t* CACreateRemoteEndpointInternal(const CAURI_t resourceUri,
222 const CAAddress_t addr, const CAConnectivityType_t type)
227 if (resourceUri == NULL)
229 OIC_LOG_V(DEBUG, TAG, "uri is null value");
233 // allocate the remote end point structure.
234 CARemoteEndpoint_t* rep = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
238 OIC_LOG_V(DEBUG, TAG, "memory alloc error");
241 memset(rep, 0, sizeof(CARemoteEndpoint_t));
243 // allocate reference uri field
244 len = strlen(resourceUri);
246 temp = (char*) OICMalloc(sizeof(char) * (len + 1));
249 OIC_LOG_V(DEBUG, TAG, "memory alloc error");
251 CADestroyRemoteEndpointInternal(rep);
255 memset(temp, 0, sizeof(char) * (len + 1));
256 strncpy(temp, resourceUri, len);
259 rep->resourceUri = temp;
261 // save the addressInfo
262 memcpy(&(rep->addressInfo), &addr, sizeof(CAAddress_t));
265 rep->connectivityType = type;
270 CARequestInfo_t* CACloneRequestInfo(const CARequestInfo_t* rep)
278 // allocate the request info structure.
279 CARequestInfo_t* clone = (CARequestInfo_t*) OICMalloc(sizeof(CARequestInfo_t));
282 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
285 memset(clone, 0, sizeof(CARequestInfo_t));
286 memcpy(clone, rep, sizeof(CARequestInfo_t));
288 if (rep->info.token != NULL)
290 // allocate token field
291 len = strlen(rep->info.token);
293 temp = (char*) OICMalloc(sizeof(char) * (len + 1));
296 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
298 CADestroyRequestInfoInternal(clone);
302 memset(temp, 0, sizeof(char) * (len + 1));
303 strncpy(temp, rep->info.token, len);
306 clone->info.token = temp;
309 if (rep->info.options != NULL)
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));
317 if (rep->info.payload != NULL)
319 // allocate payload field
320 len = strlen(rep->info.payload);
322 temp = (char*) OICMalloc(sizeof(char) * (len + 1));
325 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
327 CADestroyRequestInfoInternal(clone);
331 memset(temp, 0, sizeof(char) * (len + 1));
332 strncpy(temp, rep->info.payload, len);
335 clone->info.payload = temp;
341 CAResponseInfo_t* CACloneResponseInfo(const CAResponseInfo_t* rep)
349 // allocate the response info structure.
350 CAResponseInfo_t* clone = (CAResponseInfo_t*) OICMalloc(sizeof(CAResponseInfo_t));
353 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
356 memset(clone, 0, sizeof(CAResponseInfo_t));
357 memcpy(clone, rep, sizeof(CAResponseInfo_t));
359 if (rep->info.token != NULL)
361 // allocate token field
362 len = strlen(rep->info.token);
364 temp = (char*) OICMalloc(sizeof(char) * (len + 1));
367 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
369 CADestroyResponseInfoInternal(clone);
373 memset(temp, 0, sizeof(char) * (len + 1));
374 strncpy(temp, rep->info.token, len);
377 clone->info.token = temp;
380 if (rep->info.options != NULL)
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));
388 if (rep->info.payload != NULL)
390 // allocate payload field
391 len = strlen(rep->info.payload);
393 temp = (char*) OICMalloc(sizeof(char) * (len + 1));
396 OIC_LOG_V(DEBUG, TAG, "memory alloc error!!");
398 CADestroyResponseInfoInternal(clone);
402 memset(temp, 0, sizeof(char) * (len + 1));
403 strncpy(temp, rep->info.payload, len);
406 clone->info.payload = temp;
412 void CADestroyRemoteEndpointInternal(CARemoteEndpoint_t* rep)
418 if (rep->resourceUri != NULL)
420 OICFree((char*) rep->resourceUri);
423 // free remote end point structure.
427 void CADestroyRequestInfoInternal(CARequestInfo_t* rep)
433 if (rep->info.token != NULL)
435 OICFree((char*) rep->info.token);
438 // free options field
439 if (rep->info.options != NULL)
441 OICFree((CAHeaderOption_t*) rep->info.options);
444 // free payload field
445 if (rep->info.payload != NULL)
447 OICFree((char*) rep->info.payload);
453 void CADestroyResponseInfoInternal(CAResponseInfo_t* rep)
459 if (rep->info.token != NULL)
461 OICFree((char*) rep->info.token);
464 // free options field
465 if (rep->info.options != NULL)
467 OICFree((CAHeaderOption_t*) rep->info.options);
470 // free payload field
471 if (rep->info.payload != NULL)
473 OICFree((char*) rep->info.payload);