Merge notification service from master branch
[platform/upstream/iotivity.git] / service / notification / src / common / NSUtil.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSUtil.h"
22
23 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
24 {
25     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
26
27     OCEntityHandlerRequest *copyOfRequest =
28             (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
29
30     if (copyOfRequest)
31     {
32         // Do shallow copy
33         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
34
35         if (copyOfRequest->query)
36         {
37             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
38             if(!copyOfRequest->query)
39             {
40                 NS_LOG(ERROR, "Copy failed due to allocation failure");
41                 OICFree(copyOfRequest);
42                 return NULL;
43             }
44         }
45
46         if (entityHandlerRequest->payload)
47         {
48             copyOfRequest->payload = (OCPayload *)
49                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
50         }
51
52         // Ignore vendor specific header options for example
53         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
54         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
55     }
56
57     if (copyOfRequest)
58     {
59         NS_LOG(DEBUG, "Copied client request");
60     }
61     else
62     {
63         NS_LOG(DEBUG, "Error copying client request");
64     }
65
66     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
67     return copyOfRequest;
68 }
69
70 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
71 {
72     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
73
74     OICFree(entityHandlerRequest->query);
75     OCPayloadDestroy(entityHandlerRequest->payload);
76     OICFree(entityHandlerRequest);
77
78     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
79
80     return NS_OK;
81 }
82
83 NSResult NSFreeMessage(NSMessage * obj)
84 {
85     if (!obj)
86     {
87         return NS_ERROR;
88     }
89
90     obj->messageId = 0;
91     (obj->providerId)[0] = '\0';
92
93     NSFreeMalloc(&(obj->dateTime));
94     obj->ttl = 0;
95     NSFreeMalloc(&(obj->title));
96     NSFreeMalloc(&(obj->contentText));
97     NSFreeMalloc(&(obj->sourceName));
98     NSFreeMalloc(&(obj->topic));
99     NSFreeMediaContents(obj->mediaContents);
100     OCRepPayloadDestroy(obj->extraInfo);
101     OICFree(obj);
102
103     return NS_OK;
104 }
105
106 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
107 {
108     NSMessage * newMsg = NULL;
109
110     if(copyMsg == NULL)
111     {
112         NS_LOG(ERROR, "Copy Msg is NULL");
113         return NULL;
114     }
115
116     newMsg = NSInitializeMessage();
117     NS_VERIFY_NOT_NULL(newMsg, NULL);
118
119     newMsg->messageId = copyMsg->messageId;
120     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
121
122     if(copyMsg->dateTime)
123     {
124         newMsg->dateTime = OICStrdup(copyMsg->dateTime);
125     }
126
127     newMsg->ttl = copyMsg->ttl;
128
129     if (copyMsg->title)
130     {
131         newMsg->title = OICStrdup(copyMsg->title);
132     }
133
134     if (copyMsg->contentText)
135     {
136         newMsg->contentText = OICStrdup(copyMsg->contentText);
137     }
138
139     if (copyMsg->sourceName)
140     {
141        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
142     }
143
144     if (copyMsg->mediaContents)
145     {
146        newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
147     }
148
149     if (copyMsg->topic)
150     {
151         newMsg->topic = OICStrdup(copyMsg->topic);
152     }
153
154     if (copyMsg->extraInfo)
155     {
156         newMsg->extraInfo = OCRepPayloadClone(copyMsg->extraInfo);
157     }
158
159     return newMsg;
160 }
161
162 NSResult NSFreeSync(NSSyncInfo * obj)
163 {
164     if (!obj)
165     {
166         return NS_ERROR;
167     }
168
169     OICFree(obj);
170
171     return NS_OK;
172 }
173
174 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
175 {
176     NSSyncInfo * newMsg = NULL;
177
178     if(!copyMsg)
179     {
180         NS_LOG(ERROR, "Copy Msg is NULL");
181         return NULL;
182     }
183
184     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
185
186     if(!newMsg)
187     {
188         NS_LOG(ERROR, "newMsg is NULL");
189         return NULL;
190     }
191
192     newMsg->messageId = copyMsg->messageId;
193     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
194     newMsg->state = copyMsg->state;
195
196     return newMsg;
197 }
198
199 NSResult NSFreeConsumer(NSConsumer * obj)
200 {
201     if (!obj)
202     {
203         return NS_ERROR;
204     }
205
206     (obj->consumerId)[0] = '\0';
207
208     OICFree(obj);
209     obj = NULL;
210
211     return NS_OK;
212 }
213
214 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
215 {
216     NSConsumer * newMsg = NULL;
217
218     if(copyMsg == NULL)
219     {
220         NS_LOG(ERROR, "Copy Msg is NULL");
221         return NULL;
222     }
223
224     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
225
226     if(!newMsg)
227     {
228         NS_LOG(ERROR, "newMsg is NULL");
229         return NULL;
230     }
231
232     (newMsg->consumerId)[0] = '\0';
233
234     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
235
236     return newMsg;
237 }
238
239 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
240         const char * copyString)
241 {
242     if(copyString)
243     {
244         OCRepPayloadSetPropString(*msgPayload, name, copyString);
245     }
246 }
247
248 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
249         int64_t value)
250 {
251     if(value)
252     {
253         OCRepPayloadSetPropInt(*msgPayload, name, value);
254     }
255 }
256
257 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
258 {
259     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
260     char * providerId = NULL;
261     int64_t state = 0;
262
263     if(!payload)
264     {
265         return NULL;
266     }
267     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
268     if (!retSync)
269     {
270         return NULL;
271     }
272
273     retSync->messageId = 0;
274     retSync->state = NS_SYNC_READ;
275
276     OCRepPayload * repPayload = (OCRepPayload *)payload;
277     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID,
278             (int64_t *)&retSync->messageId))
279     {
280         OICFree(retSync);
281         return NULL;
282     }
283
284     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
285     {
286         OICFree(retSync);
287         return NULL;
288     }
289
290     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
291     {
292         OICFree(retSync);
293         return NULL;
294     }
295
296     retSync->state = (NSSyncType) state;
297     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
298     OICFree(providerId);
299
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);
303
304     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
305
306     return retSync;
307 }
308
309 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
310 {
311     uint8_t uuid[UUID_SIZE] = { 0, };
312
313     if (RAND_UUID_OK == OCGenerateUuid(uuid))
314     {
315         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
316         {
317             return NS_OK;
318         }
319     }
320     return NS_ERROR;
321 }
322
323 char * NSGetValueFromQuery(char *query, char * compareKey)
324 {
325     char *key = NULL;
326     char *value = NULL;
327     char *restOfQuery = NULL;
328     int numKeyValuePairsParsed = 0;
329
330     NS_LOG_V(INFO, "NS Query Params = %s", query);
331
332     if(!query || query[0] == '\0' || !strlen(query))
333     {
334         NS_LOG(ERROR, "query is null or \\0 or size is 0");
335         return NULL;
336     }
337
338     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
339
340     while(keyValuePair)
341     {
342         if (numKeyValuePairsParsed >= 2)
343         {
344             NS_LOG(ERROR, "More than 2 queries params in URI.");
345             return NULL;
346         }
347
348         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
349
350         if (!key || !value)
351         {
352             NS_LOG(ERROR, "More than 2 queries params in URI.");
353             return NULL;
354         }
355
356         if (strcmp(key, compareKey) == 0)
357         {
358             NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
359             return value;
360         }
361
362         ++numKeyValuePairsParsed;
363
364         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
365     }
366
367     return NULL;
368 }
369
370 NSResult NSFreeMalloc(char ** obj)
371 {
372     if(*obj)
373     {
374         OICFree(*obj);
375         *obj = NULL;
376         return NS_OK;
377     }
378
379     return NS_FAIL;
380 }
381
382 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
383 {
384     if(!copyObj)
385     {
386         return NULL;
387     }
388
389     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
390
391     if(!newObj)
392     {
393         NS_LOG(ERROR, "contents newObj is NULL");
394         return NULL;
395     }
396
397     if(copyObj->iconImage)
398     {
399         newObj->iconImage = OICStrdup(copyObj->iconImage);
400     }
401
402     return newObj;
403 }
404
405 NSResult NSFreeMediaContents(NSMediaContents * obj)
406 {
407     if(!obj)
408     {
409         return NS_FAIL;
410     }
411
412     NSFreeMalloc(&(obj->iconImage));
413     OICFree(obj);
414
415     return NS_OK;
416 }
417
418 NSMessage * NSInitializeMessage()
419 {
420     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
421
422     if(!msg)
423     {
424         NS_LOG(ERROR, "Msg is NULL");
425         return NULL;
426     }
427
428     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
429     msg->messageId = msg->messageId & 0x000000007FFFFFFF;
430     (msg->providerId)[0] = '\0';
431     msg->type = 0;
432     msg->dateTime = NULL;
433     msg->ttl = 0;
434     msg->title = NULL;
435     msg->contentText = NULL;
436     msg->sourceName = NULL;
437     msg->mediaContents = NULL;
438     msg->topic = NULL;
439     msg->extraInfo = NULL;
440
441     return msg;
442 }
443
444 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
445 {
446     if (!payload || !name)
447     {
448         return NULL;
449     }
450
451     OCRepPayloadValue* val = payload->values;
452     while(val)
453     {
454         if (0 == strcmp(val->name, name))
455         {
456             return val;
457         }
458         val = val->next;
459     }
460
461     return NULL;
462 }
463
464 NSTopicList * NSInitializeTopicList()
465 {
466     NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
467
468     if(!topicList)
469     {
470         NS_LOG(ERROR, "topicList is NULL");
471         return NULL;
472     }
473
474     (topicList->consumerId)[0] = '\0';
475     topicList->head = NULL;
476     topicList->tail = NULL;
477
478     return topicList;
479 }
480
481 NSResult NSFreeTopicList(NSTopicList * topicList)
482 {
483     if (!topicList)
484     {
485         return NS_ERROR;
486     }
487
488     //TODO:Free Topic List
489
490
491     return NS_OK;
492 }
493