X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fnotification%2Fsrc%2Fcommon%2FNSUtil.c;h=8efb307da90527edb028325086f5500e6d608284;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=ec689ecb8354d0f240114c82de7dadfa90906988;hpb=e5337af1b857936fce8295ffe468e1accdfc8350;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/notification/src/common/NSUtil.c b/service/notification/src/common/NSUtil.c index ec689ec..8efb307 100755 --- a/service/notification/src/common/NSUtil.c +++ b/service/notification/src/common/NSUtil.c @@ -29,13 +29,16 @@ OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *ent if (copyOfRequest) { - // Do shallow copy memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest)); + copyOfRequest->payload = NULL; + copyOfRequest->query = NULL; + copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0; + copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL; - if (copyOfRequest->query) + if (entityHandlerRequest->query) { copyOfRequest->query = OICStrdup(entityHandlerRequest->query); - if(!copyOfRequest->query) + if (!copyOfRequest->query) { NS_LOG(ERROR, "Copy failed due to allocation failure"); OICFree(copyOfRequest); @@ -48,10 +51,6 @@ OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *ent copyOfRequest->payload = (OCPayload *) (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload)); } - - // Ignore vendor specific header options for example - copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0; - copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL; } if (copyOfRequest) @@ -97,7 +96,7 @@ NSResult NSFreeMessage(NSMessage * obj) NSFreeMalloc(&(obj->sourceName)); NSFreeMalloc(&(obj->topic)); NSFreeMediaContents(obj->mediaContents); - + OCRepPayloadDestroy(obj->extraInfo); OICFree(obj); return NS_OK; @@ -107,18 +106,19 @@ NSMessage * NSDuplicateMessage(NSMessage * copyMsg) { NSMessage * newMsg = NULL; - if(copyMsg == NULL) + if (copyMsg == NULL) { NS_LOG(ERROR, "Copy Msg is NULL"); return NULL; } newMsg = NSInitializeMessage(); + NS_VERIFY_NOT_NULL(newMsg, NULL); newMsg->messageId = copyMsg->messageId; OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId); - if(copyMsg->dateTime) + if (copyMsg->dateTime) { newMsg->dateTime = OICStrdup(copyMsg->dateTime); } @@ -150,6 +150,11 @@ NSMessage * NSDuplicateMessage(NSMessage * copyMsg) newMsg->topic = OICStrdup(copyMsg->topic); } + if (copyMsg->extraInfo) + { + newMsg->extraInfo = OCRepPayloadClone(copyMsg->extraInfo); + } + return newMsg; } @@ -169,7 +174,7 @@ NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg) { NSSyncInfo * newMsg = NULL; - if(!copyMsg) + if (!copyMsg) { NS_LOG(ERROR, "Copy Msg is NULL"); return NULL; @@ -177,7 +182,7 @@ NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg) newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo)); - if(!newMsg) + if (!newMsg) { NS_LOG(ERROR, "newMsg is NULL"); return NULL; @@ -209,7 +214,7 @@ NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg) { NSConsumer * newMsg = NULL; - if(copyMsg == NULL) + if (copyMsg == NULL) { NS_LOG(ERROR, "Copy Msg is NULL"); return NULL; @@ -217,7 +222,7 @@ NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg) newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer)); - if(!newMsg) + if (!newMsg) { NS_LOG(ERROR, "newMsg is NULL"); return NULL; @@ -233,7 +238,7 @@ NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg) void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name, const char * copyString) { - if(copyString) + if (copyString) { OCRepPayloadSetPropString(*msgPayload, name, copyString); } @@ -242,7 +247,7 @@ void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name, void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name, int64_t value) { - if(value) + if (value) { OCRepPayloadSetPropInt(*msgPayload, name, value); } @@ -254,11 +259,13 @@ NSSyncInfo * NSGetSyncInfo(OCPayload * payload) char * providerId = NULL; int64_t state = 0; - if(!payload) + if (!payload) { return NULL; } + NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo)); + if (!retSync) { return NULL; @@ -266,9 +273,10 @@ NSSyncInfo * NSGetSyncInfo(OCPayload * payload) retSync->messageId = 0; retSync->state = NS_SYNC_READ; - OCRepPayload * repPayload = (OCRepPayload *)payload; - if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId)) + + if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, + (int64_t *)&retSync->messageId)) { OICFree(retSync); return NULL; @@ -290,7 +298,7 @@ NSSyncInfo * NSGetSyncInfo(OCPayload * payload) OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId); OICFree(providerId); - NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId); + NS_LOG_V(INFO_PRIVATE, "Provider ID : %s", retSync->providerId); NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId); NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state); @@ -322,7 +330,7 @@ char * NSGetValueFromQuery(char *query, char * compareKey) NS_LOG_V(INFO, "NS Query Params = %s", query); - if(!query || query[0] == '\0' || !strlen(query)) + if (!query || query[0] == '\0' || !strlen(query)) { NS_LOG(ERROR, "query is null or \\0 or size is 0"); return NULL; @@ -362,7 +370,7 @@ char * NSGetValueFromQuery(char *query, char * compareKey) NSResult NSFreeMalloc(char ** obj) { - if(*obj) + if (*obj) { OICFree(*obj); *obj = NULL; @@ -374,20 +382,20 @@ NSResult NSFreeMalloc(char ** obj) NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj) { - if(!copyObj) + if (!copyObj) { return NULL; } NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents)); - if(!newObj) + if (!newObj) { NS_LOG(ERROR, "contents newObj is NULL"); return NULL; } - if(copyObj->iconImage) + if (copyObj->iconImage) { newObj->iconImage = OICStrdup(copyObj->iconImage); } @@ -397,7 +405,7 @@ NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj) NSResult NSFreeMediaContents(NSMediaContents * obj) { - if(!obj) + if (!obj) { return NS_FAIL; } @@ -412,13 +420,14 @@ NSMessage * NSInitializeMessage() { NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage)); - if(!msg) + if (!msg) { NS_LOG(ERROR, "Msg is NULL"); return NULL; } msg->messageId = OICGetCurrentTime(TIME_IN_MS); + msg->messageId = msg->messageId & 0x000000007FFFFFFF; (msg->providerId)[0] = '\0'; msg->type = 0; msg->dateTime = NULL; @@ -428,6 +437,7 @@ NSMessage * NSInitializeMessage() msg->sourceName = NULL; msg->mediaContents = NULL; msg->topic = NULL; + msg->extraInfo = NULL; return msg; } @@ -440,7 +450,8 @@ OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* n } OCRepPayloadValue* val = payload->values; - while(val) + + while (val) { if (0 == strcmp(val->name, name)) { @@ -456,7 +467,7 @@ NSTopicList * NSInitializeTopicList() { NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList)); - if(!topicList) + if (!topicList) { NS_LOG(ERROR, "topicList is NULL"); return NULL; @@ -469,16 +480,142 @@ NSTopicList * NSInitializeTopicList() return topicList; } -NSResult NSFreeTopicList(NSTopicList * topicList) +OCDevAddr * NSChangeAddress(const char * inputaddress) { - if (!topicList) + NS_VERIFY_NOT_NULL(inputaddress, NULL); + + char * address = (char *)inputaddress; + char * schema = strstr(inputaddress, "//"); + if (schema) { - return NS_ERROR; + address = schema + 2; + } + size_t prefixLen = schema - inputaddress; + if (prefixLen <= 0) + { + NS_LOG(ERROR, "Invalid Input address."); + return NULL; + } + + OCTransportFlags flags = OC_DEFAULT_FLAGS; + OCTransportAdapter adapter = OC_ADAPTER_IP; + if (strstr(inputaddress, "coap+tcp://")) + { + NS_LOG(DEBUG, "address : TCP"); + adapter = OC_ADAPTER_TCP; + } + else if (strstr(inputaddress, "coaps://")) + { + NS_LOG(DEBUG, "address : UDP + SECURED"); + flags |= OC_FLAG_SECURE; + } + else if (strstr(inputaddress, "coaps+tcp://")) + { + NS_LOG(DEBUG, "address : TCP + SECURED"); + flags |= OC_FLAG_SECURE; + adapter = OC_ADAPTER_TCP; + } + else if (strstr(inputaddress, "coap://")) + { + NS_LOG(DEBUG, "address : UDP"); + } + else + { + NS_LOG(ERROR, "Invalid CoAP Schema."); + return NULL; } - //TODO:Free Topic List + OCDevAddr * retAddr = NULL; + retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr)); + NS_VERIFY_NOT_NULL(retAddr, NULL); + char * start = address; + char * end = address; + if (address[0] == '[') + { + flags |= OC_IP_USE_V6; + end = strchr(++address, ']'); + if (!end || end <= start) + { + NS_LOG(ERROR, "Invalid Input Address - IPv6."); + NSOICFree(retAddr); + return NULL; + } + memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE); + OICStrcpy(retAddr->addr, (size_t)(end-start), address); + } + else + { + flags |= OC_IP_USE_V4; + end = strchr(address, ':'); + if (!end || end <= start) + { + NS_LOG(ERROR, "Invalid Input Address - IPv4."); + NSOICFree(retAddr); + return NULL; + } + char * end2 = strchr(end + 1, ':'); + if (end2) + { + NS_LOG(ERROR, "Invalid Input Address - IPv4."); + NSOICFree(retAddr); + return NULL; + } + memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE); + OICStrcpy(retAddr->addr, (size_t)(end-start)+1, address); + } - return NS_OK; + retAddr->adapter = adapter; + retAddr->flags = flags; + + address = end + 1; + int tmp = 0; + if (flags & OC_IP_USE_V6) + { + address++; + } + uint16_t port = address[tmp++] - '0'; + + while(true) + { + if (address[tmp] == '\0' || address[tmp] > '9' || address[tmp] < '0') + { + break; + } + if (tmp >= 5 || (port >= 6553 && (address[tmp] -'0') >= 6)) + { + NS_LOG_V(ERROR, "Invalid Input Address - Port. %d", tmp+1); + NSOICFree(retAddr); + return NULL; + } + port *= 10; + port += address[tmp++] - '0'; + } + + retAddr->port = port; + + NS_LOG(DEBUG, "Change Address for TCP request"); + NS_LOG_V(INFO_PRIVATE, "Origin : %s", inputaddress); + NS_LOG_V(INFO_PRIVATE, "Changed Addr : %s", retAddr->addr); + NS_LOG_V(INFO_PRIVATE, "Changed Port : %d", retAddr->port); + + return retAddr; +} + +bool NSOCResultToSuccess(OCStackResult ret) +{ + switch (ret) + { + case OC_STACK_OK: + case OC_STACK_RESOURCE_CREATED: + case OC_STACK_RESOURCE_DELETED: + case OC_STACK_PRESENCE_STOPPED: + case OC_STACK_CONTINUE: + case OC_STACK_RESOURCE_CHANGED: + return true; + default: + NS_LOG_V(DEBUG, "OCStackResult : %d", (int)ret); + return false; + } }