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 ******************************************************************/
23 #include "oic_malloc.h"
24 #include "caremotehandler.h"
29 CARemoteEndpoint_t *CACloneRemoteEndpoint(const CARemoteEndpoint_t *rep)
37 // allocate the remote end point structure.
38 CARemoteEndpoint_t *clone = (CARemoteEndpoint_t *) OICMalloc(sizeof(CARemoteEndpoint_t));
41 OIC_LOG(DEBUG, TAG, "CACloneRemoteEndpoint Out of memory");
44 memcpy(clone, rep, sizeof(CARemoteEndpoint_t));
46 if (rep->resourceUri != NULL)
48 // allocate reference uri field
49 len = strlen(rep->resourceUri);
51 temp = (char *) OICCalloc(len + 1, sizeof(char));
54 OIC_LOG(DEBUG, TAG, "CACloneRemoteEndpoint Out of memory");
56 CADestroyRemoteEndpointInternal(clone);
61 strncpy(temp, rep->resourceUri, len);
64 clone->resourceUri = temp;
70 #define COAP_PREFIX "coap://"
71 #define COAP_PREFIX_LEN 7
72 #define COAPS_PREFIX "coaps://"
73 #define COAPS_PREFIX_LEN 8
78 static int32_t getCAAddress(const char *pAddress, CAAddress_t *outAddress)
80 if (pAddress == NULL || outAddress == NULL)
83 // simple parse, it will be change.
84 // 10.11.12.13:4545 (ip)
85 // 10:11:12:13:45:45 (mac)
87 int32_t len = strlen(pAddress);
93 for (i = 0; i < len; i++)
95 if (pAddress[i] == '.')
100 // found port number start index
101 if (isIp && pAddress[i] == ':')
110 strncpy(outAddress->IP.ipAddress, pAddress, ipLen == 0 ? len : ipLen);
113 outAddress->IP.port = atoi(pAddress + ipLen + 1);
115 OIC_LOG_V(DEBUG, TAG, "ip: %s, port: %d", outAddress->IP.ipAddress, outAddress->IP.port);
119 strncpy(outAddress->BT.btMacAddress, pAddress, CA_MACADDR_SIZE - 1);
121 OIC_LOG_V(DEBUG, TAG, "mac address : %s", outAddress->BT.btMacAddress);
127 CARemoteEndpoint_t *CACreateRemoteEndpointUriInternal(const CAURI_t uri,
128 const CAConnectivityType_t connectivityType)
131 // coap://10.11.12.13:4545/resource_uri
132 // coap://10:11:12:13:45:45/resource_uri
140 bool secured = false;
141 if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
143 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAP_PREFIX);
144 startIndex = COAP_PREFIX_LEN;
147 if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
149 OIC_LOG_V(DEBUG, TAG, "uri has '%s' prefix.", COAPS_PREFIX);
150 startIndex = COAPS_PREFIX_LEN;
154 // #2. copy uri for parse
155 char *cloneUri = NULL;
156 int32_t len = strlen(uri) - startIndex;
160 OIC_LOG(DEBUG, TAG, "uri length is 0!");
164 cloneUri = (char *) OICCalloc(len + 1, sizeof(char));
165 if (cloneUri == NULL)
167 OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpointUriInternal Out of memory");
171 memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
174 // #4. parse resource uri
175 char *pAddress = cloneUri;
176 char *pResourceUri = NULL;
179 for (i = 0; i < len; i++)
181 if (cloneUri[i] == '/')
186 pResourceUri = &cloneUri[i + 1];
193 OIC_LOG_V(DEBUG, TAG, "pAddress : %s", pAddress);
195 OIC_LOG_V(DEBUG, TAG, "pResourceUri : %s", pResourceUri == NULL ? "" : pResourceUri);
198 CAAddress_t address = {};
200 int resType = getCAAddress(pAddress, &address);
203 OIC_LOG(DEBUG, TAG, "address parse error");
210 CAURI_t resourceUri = pResourceUri;
212 CARemoteEndpoint_t *remoteEndpoint = CACreateRemoteEndpointInternal(resourceUri, address,
214 if (remoteEndpoint == NULL)
216 OIC_LOG(DEBUG, TAG, "create remote endpoint fail");
221 remoteEndpoint->isSecured = secured;
225 OIC_LOG_V(DEBUG, TAG, "Remote endpoint successfully created [%d]!", remoteEndpoint->isSecured);
226 return remoteEndpoint;
229 CARemoteEndpoint_t *CACreateRemoteEndpointInternal(const CAURI_t resourceUri,
230 const CAAddress_t addr, const CAConnectivityType_t type)
235 if (resourceUri == NULL)
237 OIC_LOG(DEBUG, TAG, "uri is null value");
241 // allocate the remote end point structure.
242 CARemoteEndpoint_t *rep = (CARemoteEndpoint_t *) OICCalloc(1, sizeof(CARemoteEndpoint_t));
246 OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpointInternal of memory");
250 // allocate reference uri field
251 len = strlen(resourceUri);
253 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
256 OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpointInternal Out of memory");
258 CADestroyRemoteEndpointInternal(rep);
262 strncpy(temp, resourceUri, len);
266 rep->resourceUri = temp;
268 // save the addressInfo
269 memcpy(&(rep->addressInfo), &addr, sizeof(CAAddress_t));
272 rep->connectivityType = type;
277 CARequestInfo_t *CACloneRequestInfo(const CARequestInfo_t *rep)
282 // allocate the request info structure.
283 CARequestInfo_t *clone = (CARequestInfo_t *) OICMalloc(sizeof(CARequestInfo_t));
286 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
290 memcpy(clone, rep, sizeof(CARequestInfo_t));
296 // allocate token field
297 int len = rep->info.tokenLength;
301 temp = (char *) OICCalloc(len, sizeof(char));
304 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
306 CADestroyRequestInfoInternal(clone);
310 memcpy(temp, rep->info.token, len);
314 clone->info.token = temp;
315 clone->info.tokenLength = len;
318 if (rep->info.options != NULL && rep->info.numOptions > 0)
321 clone->info.options =
322 (CAHeaderOption_t *) OICMalloc(sizeof(CAHeaderOption_t) * rep->info.numOptions);
323 if (clone->info.options == NULL)
325 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
326 OICFree(clone->info.token);
330 memcpy(clone->info.options,
332 sizeof(CAHeaderOption_t) * rep->info.numOptions);
336 clone->info.options = NULL;
337 clone->info.numOptions = 0;
340 if (rep->info.payload != NULL)
344 // allocate payload field
345 int len = strlen(rep->info.payload);
347 temp = (char *) OICMalloc(sizeof(char) * (len + 1));
350 OIC_LOG(DEBUG, TAG, "CACloneRequestInfo Out of memory");
352 CADestroyRequestInfoInternal(clone);
356 strncpy(temp, rep->info.payload, len);
360 clone->info.payload = temp;
366 CAResponseInfo_t *CACloneResponseInfo(const CAResponseInfo_t *rep)
371 // allocate the response info structure.
372 CAResponseInfo_t *clone = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
375 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
378 memcpy(clone, rep, sizeof(CAResponseInfo_t));
384 // allocate token field
385 int len = rep->info.tokenLength;
389 temp = (char *) OICCalloc(len, sizeof(char));
392 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
394 CADestroyResponseInfoInternal(clone);
398 memcpy(temp, rep->info.token, len);
401 clone->info.token = temp;
402 clone->info.tokenLength = len;
405 if (rep->info.options != NULL && rep->info.numOptions)
408 clone->info.options = (CAHeaderOption_t *) OICMalloc(
409 sizeof(CAHeaderOption_t) * rep->info.numOptions);
411 if (clone->info.options == NULL)
413 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
414 OICFree(clone->info.token);
418 memcpy(clone->info.options, rep->info.options,
419 sizeof(CAHeaderOption_t) * rep->info.numOptions);
423 clone->info.options = NULL;
424 clone->info.numOptions = 0;
427 if (rep->info.payload != NULL)
430 // allocate payload field
431 int len = strlen(rep->info.payload);
433 temp = (char *) OICCalloc(len + 1, sizeof(char));
436 OIC_LOG(DEBUG, TAG, "CACloneResponseInfo Out of memory");
438 CADestroyResponseInfoInternal(clone);
442 strncpy(temp, rep->info.payload, len);
445 clone->info.payload = temp;
451 void CADestroyRemoteEndpointInternal(CARemoteEndpoint_t *rep)
457 OICFree((char *) rep->resourceUri);
459 // free remote end point structure.
463 void CADestroyRequestInfoInternal(CARequestInfo_t *rep)
469 OICFree(rep->info.token);
471 // free options field
472 OICFree((CAHeaderOption_t *) rep->info.options);
474 // free payload field
475 OICFree((char *) rep->info.payload);
480 void CADestroyResponseInfoInternal(CAResponseInfo_t *rep)
486 OICFree(rep->info.token);
488 // free options field
489 if (rep->info.options != NULL && rep->info.numOptions)
491 OICFree((CAHeaderOption_t *) rep->info.options);
494 // free payload field
495 OICFree((char *) rep->info.payload);