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));
33 memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
35 if (copyOfRequest->query)
37 copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
38 if(!copyOfRequest->query)
40 NS_LOG(ERROR, "Copy failed due to allocation failure");
41 OICFree(copyOfRequest);
46 if (entityHandlerRequest->payload)
48 copyOfRequest->payload = (OCPayload *)
49 (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
52 // Ignore vendor specific header options for example
53 copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
54 copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
59 NS_LOG(DEBUG, "Copied client request");
63 NS_LOG(DEBUG, "Error copying client request");
66 NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
70 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
72 NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
74 OICFree(entityHandlerRequest->query);
75 OCPayloadDestroy(entityHandlerRequest->payload);
76 OICFree(entityHandlerRequest);
78 NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
83 NSResult NSFreeMessage(NSMessage * obj)
91 (obj->providerId)[0] = '\0';
93 NSFreeMalloc(&(obj->dateTime));
95 NSFreeMalloc(&(obj->title));
96 NSFreeMalloc(&(obj->contentText));
97 NSFreeMalloc(&(obj->sourceName));
98 NSFreeMalloc(&(obj->topic));
99 NSFreeMediaContents(obj->mediaContents);
100 OCRepPayloadDestroy(obj->extraInfo);
106 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
108 NSMessage * newMsg = NULL;
112 NS_LOG(ERROR, "Copy Msg is NULL");
116 newMsg = NSInitializeMessage();
117 NS_VERIFY_NOT_NULL(newMsg, NULL);
119 newMsg->messageId = copyMsg->messageId;
120 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
122 if(copyMsg->dateTime)
124 newMsg->dateTime = OICStrdup(copyMsg->dateTime);
127 newMsg->ttl = copyMsg->ttl;
131 newMsg->title = OICStrdup(copyMsg->title);
134 if (copyMsg->contentText)
136 newMsg->contentText = OICStrdup(copyMsg->contentText);
139 if (copyMsg->sourceName)
141 newMsg->sourceName = OICStrdup(copyMsg->sourceName);
144 if (copyMsg->mediaContents)
146 newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
151 newMsg->topic = OICStrdup(copyMsg->topic);
154 if (copyMsg->extraInfo)
156 newMsg->extraInfo = OCRepPayloadClone(copyMsg->extraInfo);
162 NSResult NSFreeSync(NSSyncInfo * obj)
174 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
176 NSSyncInfo * newMsg = NULL;
180 NS_LOG(ERROR, "Copy Msg is NULL");
184 newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
188 NS_LOG(ERROR, "newMsg is NULL");
192 newMsg->messageId = copyMsg->messageId;
193 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
194 newMsg->state = copyMsg->state;
199 NSResult NSFreeConsumer(NSConsumer * obj)
206 (obj->consumerId)[0] = '\0';
214 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
216 NSConsumer * newMsg = NULL;
220 NS_LOG(ERROR, "Copy Msg is NULL");
224 newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
228 NS_LOG(ERROR, "newMsg is NULL");
232 (newMsg->consumerId)[0] = '\0';
234 OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
239 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
240 const char * copyString)
244 OCRepPayloadSetPropString(*msgPayload, name, copyString);
248 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
253 OCRepPayloadSetPropInt(*msgPayload, name, value);
257 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
259 NS_LOG(DEBUG, "NSGetSyncInfo - IN");
260 char * providerId = NULL;
267 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
273 retSync->messageId = 0;
274 retSync->state = NS_SYNC_READ;
276 OCRepPayload * repPayload = (OCRepPayload *)payload;
277 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID,
278 (int64_t *)&retSync->messageId))
284 if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
290 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
296 retSync->state = (NSSyncType) state;
297 OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
300 NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
301 NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
302 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
304 NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
309 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
311 uint8_t uuid[UUID_SIZE] = { 0, };
313 if (RAND_UUID_OK == OCGenerateUuid(uuid))
315 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
323 char * NSGetValueFromQuery(char *query, char * compareKey)
327 char *restOfQuery = NULL;
328 int numKeyValuePairsParsed = 0;
330 NS_LOG_V(INFO, "NS Query Params = %s", query);
332 if(!query || query[0] == '\0' || !strlen(query))
334 NS_LOG(ERROR, "query is null or \\0 or size is 0");
338 char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
342 if (numKeyValuePairsParsed >= 2)
344 NS_LOG(ERROR, "More than 2 queries params in URI.");
348 key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
352 NS_LOG(ERROR, "More than 2 queries params in URI.");
356 if (strcmp(key, compareKey) == 0)
358 NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
362 ++numKeyValuePairsParsed;
364 keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
370 NSResult NSFreeMalloc(char ** obj)
382 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
389 NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
393 NS_LOG(ERROR, "contents newObj is NULL");
397 if(copyObj->iconImage)
399 newObj->iconImage = OICStrdup(copyObj->iconImage);
405 NSResult NSFreeMediaContents(NSMediaContents * obj)
412 NSFreeMalloc(&(obj->iconImage));
418 NSMessage * NSInitializeMessage()
420 NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
424 NS_LOG(ERROR, "Msg is NULL");
428 msg->messageId = OICGetCurrentTime(TIME_IN_MS);
429 msg->messageId = msg->messageId & 0x000000007FFFFFFF;
430 (msg->providerId)[0] = '\0';
432 msg->dateTime = NULL;
435 msg->contentText = NULL;
436 msg->sourceName = NULL;
437 msg->mediaContents = NULL;
439 msg->extraInfo = NULL;
444 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
446 if (!payload || !name)
451 OCRepPayloadValue* val = payload->values;
454 if (0 == strcmp(val->name, name))
464 NSTopicList * NSInitializeTopicList()
466 NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
470 NS_LOG(ERROR, "topicList is NULL");
474 (topicList->consumerId)[0] = '\0';
475 topicList->head = NULL;
476 topicList->tail = NULL;
481 NSResult NSFreeTopicList(NSTopicList * topicList)
488 //TODO:Free Topic List