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 NSFreeMediaContents(obj->mediaContents);
105 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
107 NSMessage * newMsg = NULL;
111 NS_LOG(ERROR, "Copy Msg is NULL");
115 newMsg = NSInitializeMessage();
117 newMsg->messageId = copyMsg->messageId;
118 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
120 if(copyMsg->dateTime)
122 newMsg->dateTime = OICStrdup(copyMsg->dateTime);
125 newMsg->ttl = copyMsg->ttl;
129 newMsg->title = OICStrdup(copyMsg->title);
132 if (copyMsg->contentText)
134 newMsg->contentText = OICStrdup(copyMsg->contentText);
137 if (copyMsg->sourceName)
139 newMsg->sourceName = OICStrdup(copyMsg->sourceName);
142 if (copyMsg->mediaContents)
144 newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
150 NSResult NSFreeSync(NSSyncInfo * obj)
162 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
164 NSSyncInfo * newMsg = NULL;
168 NS_LOG(ERROR, "Copy Msg is NULL");
172 newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
176 NS_LOG(ERROR, "newMsg is NULL");
180 newMsg->messageId = copyMsg->messageId;
181 OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
182 newMsg->state = copyMsg->state;
187 NSResult NSFreeConsumer(NSConsumer * obj)
194 (obj->consumerId)[0] = '\0';
202 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
204 NSConsumer * newMsg = NULL;
208 NS_LOG(ERROR, "Copy Msg is NULL");
212 newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
216 NS_LOG(ERROR, "newMsg is NULL");
220 (newMsg->consumerId)[0] = '\0';
222 OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
227 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
228 const char * copyString)
232 OCRepPayloadSetPropString(*msgPayload, name, copyString);
236 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
241 OCRepPayloadSetPropInt(*msgPayload, name, value);
245 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
247 NS_LOG(DEBUG, "NSGetSyncInfo - IN");
248 char * providerId = NULL;
255 NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
261 retSync->messageId = 0;
262 retSync->state = NS_SYNC_READ;
264 OCRepPayload * repPayload = (OCRepPayload *)payload;
265 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
271 if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
277 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
283 retSync->state = (NSSyncType) state;
284 OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
287 NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
288 NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
289 NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
291 NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
296 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
298 uint8_t uuid[UUID_SIZE] = { 0, };
300 if (RAND_UUID_OK == OCGenerateUuid(uuid))
302 if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
310 char * NSGetValueFromQuery(char *query, char * compareKey)
314 char *restOfQuery = NULL;
315 int numKeyValuePairsParsed = 0;
317 NS_LOG_V(INFO, "NS Query Params = %s", query);
319 if(!query || query[0] == '\0' || !strlen(query))
321 NS_LOG(ERROR, "query is null or \\0 or size is 0");
325 char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
329 if (numKeyValuePairsParsed >= 2)
331 NS_LOG(ERROR, "More than 2 queries params in URI.");
335 key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
339 NS_LOG(ERROR, "More than 2 queries params in URI.");
343 if (strcmp(key, compareKey) == 0)
345 NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
349 ++numKeyValuePairsParsed;
351 keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
357 NSResult NSFreeMalloc(char ** obj)
369 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
376 NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
380 NS_LOG(ERROR, "contents newObj is NULL");
384 if(copyObj->iconImage)
386 newObj->iconImage = OICStrdup(copyObj->iconImage);
392 NSResult NSFreeMediaContents(NSMediaContents * obj)
399 NSFreeMalloc(&(obj->iconImage));
405 NSMessage * NSInitializeMessage()
407 NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
411 NS_LOG(ERROR, "Msg is NULL");
415 msg->messageId = OICGetCurrentTime(TIME_IN_MS);
416 (msg->providerId)[0] = '\0';
418 msg->dateTime = NULL;
421 msg->contentText = NULL;
422 msg->sourceName = NULL;
423 msg->mediaContents = NULL;