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);
106 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
108 NSMessage * newMsg = NULL;
112 NS_LOG(ERROR, "Copy Msg is NULL");
116 newMsg = NSInitializeMessage();
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);
156 NSResult NSFreeSync(NSSyncInfo * obj)
168 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
170 NSSyncInfo * newMsg = NULL;
174 NS_LOG(ERROR, "Copy Msg is NULL");
178 newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
182 NS_LOG(ERROR, "newMsg is NULL");
186 newMsg->messageId = copyMsg->messageId;
187 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
188 newMsg->state = copyMsg->state;
193 NSResult NSFreeConsumer(NSConsumer * obj)
200 (obj->consumerId)[0] = '\0';
208 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
210 NSConsumer * newMsg = NULL;
214 NS_LOG(ERROR, "Copy Msg is NULL");
218 newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
222 NS_LOG(ERROR, "newMsg is NULL");
226 (newMsg->consumerId)[0] = '\0';
228 OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
233 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
234 const char * copyString)
238 OCRepPayloadSetPropString(*msgPayload, name, copyString);
242 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
247 OCRepPayloadSetPropInt(*msgPayload, name, value);
251 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
253 NS_LOG(DEBUG, "NSGetSyncInfo - IN");
254 char * providerId = NULL;
261 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
267 retSync->messageId = 0;
268 retSync->state = NS_SYNC_READ;
270 OCRepPayload * repPayload = (OCRepPayload *)payload;
271 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
277 if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
283 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
289 retSync->state = (NSSyncType) state;
290 OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
293 NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
294 NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
295 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
297 NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
302 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
304 uint8_t uuid[UUID_SIZE] = { 0, };
306 if (RAND_UUID_OK == OCGenerateUuid(uuid))
308 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
316 char * NSGetValueFromQuery(char *query, char * compareKey)
320 char *restOfQuery = NULL;
321 int numKeyValuePairsParsed = 0;
323 NS_LOG_V(INFO, "NS Query Params = %s", query);
325 if(!query || query[0] == '\0' || !strlen(query))
327 NS_LOG(ERROR, "query is null or \\0 or size is 0");
331 char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
335 if (numKeyValuePairsParsed >= 2)
337 NS_LOG(ERROR, "More than 2 queries params in URI.");
341 key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
345 NS_LOG(ERROR, "More than 2 queries params in URI.");
349 if (strcmp(key, compareKey) == 0)
351 NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
355 ++numKeyValuePairsParsed;
357 keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
363 NSResult NSFreeMalloc(char ** obj)
375 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
382 NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
386 NS_LOG(ERROR, "contents newObj is NULL");
390 if(copyObj->iconImage)
392 newObj->iconImage = OICStrdup(copyObj->iconImage);
398 NSResult NSFreeMediaContents(NSMediaContents * obj)
405 NSFreeMalloc(&(obj->iconImage));
411 NSMessage * NSInitializeMessage()
413 NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
417 NS_LOG(ERROR, "Msg is NULL");
421 msg->messageId = OICGetCurrentTime(TIME_IN_MS);
422 (msg->providerId)[0] = '\0';
424 msg->dateTime = NULL;
427 msg->contentText = NULL;
428 msg->sourceName = NULL;
429 msg->mediaContents = NULL;
435 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
437 if (!payload || !name)
442 OCRepPayloadValue* val = payload->values;
445 if (0 == strcmp(val->name, name))
455 NSTopicList * NSInitializeTopicList()
457 NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
461 NS_LOG(ERROR, "topicList is NULL");
465 (topicList->consumerId)[0] = '\0';
466 topicList->head = NULL;
467 topicList->tail = NULL;
472 NSResult NSFreeTopicList(NSTopicList * topicList)
479 //TODO:Free Topic List