1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
25 NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
27 OCEntityHandlerRequest *copyOfRequest =
28 (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
32 memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
33 copyOfRequest->payload = NULL;
34 copyOfRequest->query = NULL;
35 copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
36 copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
38 if (entityHandlerRequest->query)
40 copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
41 if (!copyOfRequest->query)
43 NS_LOG(ERROR, "Copy failed due to allocation failure");
44 OICFree(copyOfRequest);
49 if (entityHandlerRequest->payload)
51 copyOfRequest->payload = (OCPayload *)
52 (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
58 NS_LOG(DEBUG, "Copied client request");
62 NS_LOG(DEBUG, "Error copying client request");
65 NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
69 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
71 NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
73 OICFree(entityHandlerRequest->query);
74 OCPayloadDestroy(entityHandlerRequest->payload);
75 OICFree(entityHandlerRequest);
77 NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
82 NSResult NSFreeMessage(NSMessage * obj)
90 (obj->providerId)[0] = '\0';
92 NSFreeMalloc(&(obj->dateTime));
94 NSFreeMalloc(&(obj->title));
95 NSFreeMalloc(&(obj->contentText));
96 NSFreeMalloc(&(obj->sourceName));
97 NSFreeMalloc(&(obj->topic));
98 NSFreeMediaContents(obj->mediaContents);
99 OCRepPayloadDestroy(obj->extraInfo);
105 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
107 NSMessage * newMsg = NULL;
111 NS_LOG(ERROR, "Copy Msg is NULL");
115 newMsg = NSInitializeMessage();
116 NS_VERIFY_NOT_NULL(newMsg, NULL);
118 newMsg->messageId = copyMsg->messageId;
119 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
121 if (copyMsg->dateTime)
123 newMsg->dateTime = OICStrdup(copyMsg->dateTime);
126 newMsg->ttl = copyMsg->ttl;
130 newMsg->title = OICStrdup(copyMsg->title);
133 if (copyMsg->contentText)
135 newMsg->contentText = OICStrdup(copyMsg->contentText);
138 if (copyMsg->sourceName)
140 newMsg->sourceName = OICStrdup(copyMsg->sourceName);
143 if (copyMsg->mediaContents)
145 newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
150 newMsg->topic = OICStrdup(copyMsg->topic);
153 if (copyMsg->extraInfo)
155 newMsg->extraInfo = OCRepPayloadClone(copyMsg->extraInfo);
161 NSResult NSFreeSync(NSSyncInfo * obj)
173 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
175 NSSyncInfo * newMsg = NULL;
179 NS_LOG(ERROR, "Copy Msg is NULL");
183 newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
187 NS_LOG(ERROR, "newMsg is NULL");
191 newMsg->messageId = copyMsg->messageId;
192 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
193 newMsg->state = copyMsg->state;
198 NSResult NSFreeConsumer(NSConsumer * obj)
205 (obj->consumerId)[0] = '\0';
213 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
215 NSConsumer * newMsg = NULL;
219 NS_LOG(ERROR, "Copy Msg is NULL");
223 newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
227 NS_LOG(ERROR, "newMsg is NULL");
231 (newMsg->consumerId)[0] = '\0';
233 OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
238 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
239 const char * copyString)
243 OCRepPayloadSetPropString(*msgPayload, name, copyString);
247 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
252 OCRepPayloadSetPropInt(*msgPayload, name, value);
256 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
258 NS_LOG(DEBUG, "NSGetSyncInfo - IN");
259 char * providerId = NULL;
267 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
274 retSync->messageId = 0;
275 retSync->state = NS_SYNC_READ;
276 OCRepPayload * repPayload = (OCRepPayload *)payload;
278 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID,
279 (int64_t *)&retSync->messageId))
285 if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
291 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
297 retSync->state = (NSSyncType) state;
298 OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
301 NS_LOG_V(INFO_PRIVATE, "Provider ID : %s", retSync->providerId);
302 NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
303 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
305 NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
310 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
312 uint8_t uuid[UUID_SIZE] = { 0, };
314 if (RAND_UUID_OK == OCGenerateUuid(uuid))
316 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
324 char * NSGetValueFromQuery(char *query, char * compareKey)
328 char *restOfQuery = NULL;
329 int numKeyValuePairsParsed = 0;
331 NS_LOG_V(INFO, "NS Query Params = %s", query);
333 if (!query || query[0] == '\0' || !strlen(query))
335 NS_LOG(ERROR, "query is null or \\0 or size is 0");
339 char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
343 if (numKeyValuePairsParsed >= 2)
345 NS_LOG(ERROR, "More than 2 queries params in URI.");
349 key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
353 NS_LOG(ERROR, "More than 2 queries params in URI.");
357 if (strcmp(key, compareKey) == 0)
359 NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
363 ++numKeyValuePairsParsed;
365 keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
371 NSResult NSFreeMalloc(char ** obj)
383 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
390 NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
394 NS_LOG(ERROR, "contents newObj is NULL");
398 if (copyObj->iconImage)
400 newObj->iconImage = OICStrdup(copyObj->iconImage);
406 NSResult NSFreeMediaContents(NSMediaContents * obj)
413 NSFreeMalloc(&(obj->iconImage));
419 NSMessage * NSInitializeMessage()
421 NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
425 NS_LOG(ERROR, "Msg is NULL");
429 msg->messageId = OICGetCurrentTime(TIME_IN_MS);
430 msg->messageId = msg->messageId & 0x000000007FFFFFFF;
431 (msg->providerId)[0] = '\0';
433 msg->dateTime = NULL;
436 msg->contentText = NULL;
437 msg->sourceName = NULL;
438 msg->mediaContents = NULL;
440 msg->extraInfo = NULL;
445 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
447 if (!payload || !name)
452 OCRepPayloadValue* val = payload->values;
456 if (0 == strcmp(val->name, name))
466 NSTopicList * NSInitializeTopicList()
468 NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
472 NS_LOG(ERROR, "topicList is NULL");
476 (topicList->consumerId)[0] = '\0';
477 topicList->head = NULL;
478 topicList->tail = NULL;
483 OCDevAddr * NSChangeAddress(const char * inputaddress)
485 NS_VERIFY_NOT_NULL(inputaddress, NULL);
487 char * address = (char *)inputaddress;
488 char * schema = strstr(inputaddress, "//");
491 address = schema + 2;
493 size_t prefixLen = schema - inputaddress;
496 NS_LOG(ERROR, "Invalid Input address.");
500 OCTransportFlags flags = OC_DEFAULT_FLAGS;
501 OCTransportAdapter adapter = OC_ADAPTER_IP;
502 if (strstr(inputaddress, "coap+tcp://"))
504 NS_LOG(DEBUG, "address : TCP");
505 adapter = OC_ADAPTER_TCP;
507 else if (strstr(inputaddress, "coaps://"))
509 NS_LOG(DEBUG, "address : UDP + SECURED");
510 flags |= OC_FLAG_SECURE;
512 else if (strstr(inputaddress, "coaps+tcp://"))
514 NS_LOG(DEBUG, "address : TCP + SECURED");
515 flags |= OC_FLAG_SECURE;
516 adapter = OC_ADAPTER_TCP;
518 else if (strstr(inputaddress, "coap://"))
520 NS_LOG(DEBUG, "address : UDP");
524 NS_LOG(ERROR, "Invalid CoAP Schema.");
528 OCDevAddr * retAddr = NULL;
529 retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
530 NS_VERIFY_NOT_NULL(retAddr, NULL);
532 char * start = address;
533 char * end = address;
534 if (address[0] == '[')
536 flags |= OC_IP_USE_V6;
537 end = strchr(++address, ']');
538 if (!end || end <= start)
540 NS_LOG(ERROR, "Invalid Input Address - IPv6.");
544 memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE);
545 OICStrcpy(retAddr->addr, (size_t)(end-start), address);
549 flags |= OC_IP_USE_V4;
550 end = strchr(address, ':');
551 if (!end || end <= start)
553 NS_LOG(ERROR, "Invalid Input Address - IPv4.");
557 char * end2 = strchr(end + 1, ':');
560 NS_LOG(ERROR, "Invalid Input Address - IPv4.");
564 memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE);
565 OICStrcpy(retAddr->addr, (size_t)(end-start)+1, address);
568 retAddr->adapter = adapter;
569 retAddr->flags = flags;
573 if (flags & OC_IP_USE_V6)
577 uint16_t port = address[tmp++] - '0';
581 if (address[tmp] == '\0' || address[tmp] > '9' || address[tmp] < '0')
585 if (tmp >= 5 || (port >= 6553 && (address[tmp] -'0') >= 6))
587 NS_LOG_V(ERROR, "Invalid Input Address - Port. %d", tmp+1);
592 port += address[tmp++] - '0';
595 retAddr->port = port;
597 NS_LOG(DEBUG, "Change Address for TCP request");
598 NS_LOG_V(INFO_PRIVATE, "Origin : %s", inputaddress);
599 NS_LOG_V(INFO_PRIVATE, "Changed Addr : %s", retAddr->addr);
600 NS_LOG_V(INFO_PRIVATE, "Changed Port : %d", retAddr->port);
605 bool NSOCResultToSuccess(OCStackResult ret)
610 case OC_STACK_RESOURCE_CREATED:
611 case OC_STACK_RESOURCE_DELETED:
613 case OC_STACK_PRESENCE_STOPPED:
615 case OC_STACK_CONTINUE:
616 case OC_STACK_RESOURCE_CHANGED:
619 NS_LOG_V(DEBUG, "OCStackResult : %d", (int)ret);