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();
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;
266 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
272 retSync->messageId = 0;
273 retSync->state = NS_SYNC_READ;
275 OCRepPayload * repPayload = (OCRepPayload *)payload;
276 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
282 if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
288 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
294 retSync->state = (NSSyncType) state;
295 OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
298 NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
299 NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
300 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
302 NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
307 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
309 uint8_t uuid[UUID_SIZE] = { 0, };
311 if (RAND_UUID_OK == OCGenerateUuid(uuid))
313 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
321 char * NSGetValueFromQuery(char *query, char * compareKey)
325 char *restOfQuery = NULL;
326 int numKeyValuePairsParsed = 0;
328 NS_LOG_V(INFO, "NS Query Params = %s", query);
330 if(!query || query[0] == '\0' || !strlen(query))
332 NS_LOG(ERROR, "query is null or \\0 or size is 0");
336 char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
340 if (numKeyValuePairsParsed >= 2)
342 NS_LOG(ERROR, "More than 2 queries params in URI.");
346 key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
350 NS_LOG(ERROR, "More than 2 queries params in URI.");
354 if (strcmp(key, compareKey) == 0)
356 NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
360 ++numKeyValuePairsParsed;
362 keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
368 NSResult NSFreeMalloc(char ** obj)
380 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
387 NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
391 NS_LOG(ERROR, "contents newObj is NULL");
395 if(copyObj->iconImage)
397 newObj->iconImage = OICStrdup(copyObj->iconImage);
403 NSResult NSFreeMediaContents(NSMediaContents * obj)
410 NSFreeMalloc(&(obj->iconImage));
416 NSMessage * NSInitializeMessage()
418 NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
422 NS_LOG(ERROR, "Msg is NULL");
426 msg->messageId = OICGetCurrentTime(TIME_IN_MS);
427 msg->messageId = msg->messageId & 0x000000007FFFFFFF;
428 (msg->providerId)[0] = '\0';
430 msg->dateTime = NULL;
433 msg->contentText = NULL;
434 msg->sourceName = NULL;
435 msg->mediaContents = NULL;
437 msg->extraInfo = NULL;
442 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
444 if (!payload || !name)
449 OCRepPayloadValue* val = payload->values;
452 if (0 == strcmp(val->name, name))
462 NSTopicList * NSInitializeTopicList()
464 NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
468 NS_LOG(ERROR, "topicList is NULL");
472 (topicList->consumerId)[0] = '\0';
473 topicList->head = NULL;
474 topicList->tail = NULL;
479 NSResult NSFreeTopicList(NSTopicList * topicList)
486 //TODO:Free Topic List